Salome HOME
b9c17a895048e8d78dcd6ff0f56b8d03f421540f
[modules/hydro.git] / src / HYDROData / HYDROData_CalculationCase.cxx
1
2 #include "HYDROData_CalculationCase.h"
3
4 #include "HYDROData_ArtificialObject.h"
5 #include "HYDROData_IAltitudeObject.h"
6 #include "HYDROData_Document.h"
7 #include "HYDROData_ShapesGroup.h"
8 #include "HYDROData_Iterator.h"
9 #include "HYDROData_NaturalObject.h"
10 #include "HYDROData_PolylineXY.h"
11 #include "HYDROData_SplittedShapesGroup.h"
12 #include "HYDROData_Region.h"
13 #include "HYDROData_Tool.h"
14
15 #include <GEOMBase.h>
16
17 #include <QSet>
18
19 #include <TopoDS.hxx>
20 #include <TopoDS_Shell.hxx>
21 #include <TopoDS_Edge.hxx>
22
23 #include <BRep_Builder.hxx>
24 #include <BRepBuilderAPI_Sewing.hxx>
25 #include <BRepTopAdaptor_FClass2d.hxx>
26
27 #include <BRepTools.hxx>
28
29 #include <TopAbs.hxx>
30 #include <TopExp_Explorer.hxx>
31 #include <TopExp.hxx>
32 #include <TopTools_ListOfShape.hxx>
33 #include <TopTools_ListIteratorOfListOfShape.hxx>
34 #include <TDataStd_Integer.hxx>
35
36 //#define  DEB_CALCULATION 1
37 #ifdef DEB_CALCULATION
38 #include <BRepTools.hxx>
39 #include <TopLoc_Location.hxx>
40 #endif 
41 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
42 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
43 #define CALCULATION_GROUPS_PREF GetName() + "_"
44 //#define DEB_CLASS2D 1
45 #ifdef DEB_CLASS2D
46 #include <BRepBuilderAPI_MakeVertex.hxx>
47 #endif
48
49 #define EXPORT_NAME "HYDRO_" + GetName()
50
51 #include <SALOME_NamingService.hxx>
52 #include <SALOME_LifeCycleCORBA.hxx>
53
54
55
56
57
58 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
59 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
60
61 HYDROData_CalculationCase::HYDROData_CalculationCase()
62 : HYDROData_Entity()
63 {
64 }
65
66 HYDROData_CalculationCase::~HYDROData_CalculationCase()
67 {
68 }
69
70 void HYDROData_CalculationCase::SetName( const QString& theName )
71 {
72   QString anOldCaseName = GetName();
73   if ( anOldCaseName != theName )
74   {
75     HYDROData_SequenceOfObjects aRegions = GetRegions();
76
77     HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
78     for ( ; anIter.More(); anIter.Next() )
79     {
80       Handle(HYDROData_Region) aRegion =
81         Handle(HYDROData_Region)::DownCast( anIter.Value() );
82       if ( aRegion.IsNull() )
83         continue;
84
85       HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
86
87       HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
88       HYDROData_SequenceOfObjects::Iterator anIter( aZones );
89       for ( ; anIter.More(); anIter.Next() )
90       {
91         Handle(HYDROData_Zone) aRegZone =
92           Handle(HYDROData_Zone)::DownCast( anIter.Value() );
93         if ( aRegZone.IsNull() )
94           continue;
95
96         HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
97       }
98     }
99
100     HYDROData_SequenceOfObjects aGroups = GetSplittedGroups();
101
102     anIter.Init( aGroups );
103     for ( ; anIter.More(); anIter.Next() )
104     {
105       Handle(HYDROData_SplittedShapesGroup) aGroup =
106         Handle(HYDROData_SplittedShapesGroup)::DownCast( anIter.Value() );
107       if ( aGroup.IsNull() )
108         continue;
109
110       HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
111     }
112   }
113
114   HYDROData_Entity::SetName( theName );
115 }
116
117 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
118 {
119   QStringList aResList = dumpObjectCreation( theTreatedObjects );
120   aResList.prepend( "# Calculation case" );
121
122   QString aCalculName = GetObjPyName();
123
124   AssignmentMode aMode = GetAssignmentMode();
125   QString aModeStr = aMode==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
126   aResList << QString( "%0.SetAssignmentMode( %1 )" ).arg( aCalculName ).arg( aModeStr );
127
128   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
129   HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
130   for ( ; anIter.More(); anIter.Next() )
131   {
132     Handle(HYDROData_Object) aRefGeomObj =
133       Handle(HYDROData_Object)::DownCast( anIter.Value() );
134     setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
135   }
136   aResList << QString( "" );
137
138   QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
139
140   HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
141   anIter.Init( aGeomGroups );
142   for ( ; anIter.More(); anIter.Next() )
143   {
144     Handle(HYDROData_ShapesGroup) aGeomGroup =
145       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
146     if ( aGeomGroup.IsNull() )
147       continue;
148
149     Handle(HYDROData_Object) aFatherGeom =
150       Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
151     if ( aFatherGeom.IsNull() )
152       continue;
153
154     int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
155     aResList << QString( "%1 = %2.GetGroup( %3 );" )
156               .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
157
158     aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
159   }
160
161   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
162   setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
163
164   if( aMode==AUTOMATIC )
165     DumpRulesToPython( aCalculName, aResList );
166
167   aResList << QString( "" );
168   aResList << "# Start the algorithm of the partition and assignment";
169   aResList << QString( "%1.Update();" ).arg( aCalculName );
170
171   if( aMode==MANUAL )
172   {
173     // Now we restore the regions and zones order
174     HYDROData_SequenceOfObjects aRegions = GetRegions();
175     anIter.Init( aRegions );
176     for ( ; anIter.More(); anIter.Next() )
177     {
178       Handle(HYDROData_Region) aRegion =
179         Handle(HYDROData_Region)::DownCast( anIter.Value() );
180       if ( aRegion.IsNull() )
181         continue;
182
183       QString aRegionName = aRegion->GetName();
184
185       HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
186       HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
187       for ( ; aZonesIter.More(); aZonesIter.Next() )
188       {
189         Handle(HYDROData_Zone) aRegZone =
190           Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
191         if ( aRegZone.IsNull() )
192           continue;
193
194         // TODO
195       }
196     }
197   }
198
199   // Export calculation case
200   aResList << QString( "" );
201   aResList << "# Export of the calculation case";
202   QString aStudyName = "theStudy";
203   QString anEntryVar = aCalculName + "_entry";
204   aResList << QString( "%1 = %2.Export( %3._get_StudyId() )" ).arg( anEntryVar ).arg( aCalculName ).arg( aStudyName );
205
206   // Get geometry object and print debug information
207   aResList << "";
208   aResList << "# Get geometry shape and print debug information";
209   aResList << "import GEOM";
210   aResList << QString( "print \"Entry:\", %1" ).arg( anEntryVar );
211   QString aGeomShapeName = aCalculName + "_geom";
212   aResList << QString( "%1 = salome.IDToObject( str( %2 ) )" ).arg( aGeomShapeName ).arg( anEntryVar );
213   aResList << QString( "print \"Geom shape:\", %1" ).arg( aGeomShapeName );
214   aResList << QString( "print \"Geom shape name:\", %1.GetName()" ).arg( aGeomShapeName );
215
216   DumpSampleMeshing( aResList, aStudyName, aGeomShapeName, aCalculName+"_mesh" );
217
218   aResList << QString( "" );
219   return aResList;
220 }
221
222 void HYDROData_CalculationCase::DumpSampleMeshing( QStringList& theResList,
223                                                    const QString& theStudyName,
224                                                    const QString& theGeomShapeName,
225                                                    const QString& theMeshName ) const
226 {
227   theResList << "";
228   theResList << "# Meshing";
229   theResList << "import SMESH, SALOMEDS";
230   theResList << "from salome.smesh import smeshBuilder";
231
232   theResList << QString( "smesh = smeshBuilder.New( %1 )" ).arg( theStudyName );
233   theResList << QString( "%1 = smesh.Mesh( %2 )" ).arg( theMeshName ).arg( theGeomShapeName );
234   theResList << QString( "MEFISTO_2D = %1.Triangle( algo=smeshBuilder.MEFISTO )" ).arg( theMeshName );
235   theResList << "Max_Element_Area_1 = MEFISTO_2D.MaxElementArea( 10 )";
236   theResList << QString( "Regular_1D = %1.Segment()" ).arg( theMeshName );
237   theResList << "Max_Size_1 = Regular_1D.MaxSize(10)";
238   theResList << QString( "isDone = %1.Compute()" ).arg( theMeshName );
239
240   theResList << "";
241   theResList << "# Set names of Mesh objects";
242   theResList << "smesh.SetName( MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D' )";
243   theResList << "smesh.SetName( Regular_1D.GetAlgorithm(), 'Regular_1D' )";
244   theResList << "smesh.SetName( Max_Size_1, 'Max Size_1' )";
245   theResList << "smesh.SetName( Max_Element_Area_1, 'Max. Element Area_1' )";
246   theResList << QString( "smesh.SetName( %1.GetMesh(), '%1' )" ).arg( theMeshName );
247 }
248
249 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
250 {
251   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
252
253   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
254   if ( !aBoundaryPolyline.IsNull() )
255     aResSeq.Append( aBoundaryPolyline );
256
257   HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
258   aResSeq.Append( aSeqOfRegions );
259
260   return aResSeq;
261 }
262
263 void HYDROData_CalculationCase::Update()
264 {
265   HYDROData_Entity::Update();
266   SetWarning();
267
268   // At first we remove previously created objects
269   RemoveRegions();
270   RemoveSplittedGroups();
271
272   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
273   if ( aDocument.IsNull() )
274     return;
275
276   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
277   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
278   if ( aGeomObjects.IsEmpty() )
279     return;
280
281   HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
282
283   HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
284     HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
285   if ( aSplitObjects.isEmpty() )
286     return;
287
288   HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
289
290   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
291   while( anIter.hasNext() )
292   {
293     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
294     if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
295       aZonesList.append( aSplitData );
296     else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
297       anEdgesList.append( aSplitData );
298   }
299
300   switch( GetAssignmentMode() )
301   {
302   case MANUAL:
303     CreateRegionsDef( aDocument, aZonesList );
304     break;
305   case AUTOMATIC:
306     CreateRegionsAuto( aDocument, aZonesList );
307     break;
308   }
309   CreateEdgeGroupsDef( aDocument, anEdgesList );
310 }
311
312 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
313                                                   const HYDROData_SplitToZonesTool::SplitDataList& theZones )
314 {
315   // Create result regions for case, by default one zone for one region
316   QString aRegsPref = CALCULATION_REGIONS_PREF;
317   QString aZonesPref = CALCULATION_ZONES_PREF;
318
319   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
320   while( anIter.hasNext() )
321   {
322     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
323     // Create new region
324     Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
325
326     // Add the zone for region
327     Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
328   }
329 }
330
331 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
332                                                    const HYDROData_SplitToZonesTool::SplitDataList& theZones )
333 {
334   QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
335   QMap<QString, QString> aRegionNameToObjNameMap;
336   QString aZonesPref = CALCULATION_ZONES_PREF;
337   HYDROData_PriorityQueue aPr( this );
338
339   // 1. First we create a default region for each object included into the calculation case
340   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
341   for( int i=aGeomObjects.Lower(), n=aGeomObjects.Upper(); i<=n; i++ )
342   {
343     Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( aGeomObjects.Value( i ) );
344     if( anObj.IsNull() )
345       continue;
346     QString anObjName = anObj->GetName();
347     QString aRegName = anObjName + "_reg";
348     Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
349     aRegionsMap.insert( anObjName, aRegion );
350     aRegionNameToObjNameMap.insert( aRegName, anObjName );
351   }
352
353   // 2. Now for each zone it is necessary to determine the most priority object
354   //    and assign to zone to corresponding region
355   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
356   while( anIter.hasNext() )
357   {
358     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
359     HYDROData_Zone::MergeAltitudesType aMergeType;
360     Handle(HYDROData_Object) aRegObj = aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType );
361     if( aRegObj.IsNull() )
362       continue;
363     Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
364     if( aRegion.IsNull() )
365       continue;
366     Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
367
368     if( aSplitData.ObjectNames.count() > 1 && aMergeType==HYDROData_Zone::Merge_UNKNOWN )
369     {
370       qDebug( "Error in algorithm: unresolved conflicts" );
371     }
372
373     switch( aMergeType )
374     {
375     case HYDROData_Zone::Merge_ZMIN:
376     case HYDROData_Zone::Merge_ZMAX:
377       aRegionZone->SetMergeType( aMergeType );
378       break;
379     case HYDROData_Zone::Merge_Object:
380       aRegionZone->SetMergeType( aMergeType );
381       aRegionZone->RemoveMergeAltitude();
382       aRegionZone->SetMergeAltitude( aRegObj->GetAltitudeObject() );
383       break;
384     }
385   }
386
387   QStringList anObjectsWithEmptyRegions;
388   QMap<QString, Handle(HYDROData_Region)>::const_iterator
389     anIt = aRegionsMap.begin(), aLast = aRegionsMap.end();
390   for( ; anIt!=aLast; anIt++ )
391   {
392     Handle(HYDROData_Region) aRegion = anIt.value();
393     if( aRegion->GetZones().IsEmpty() )
394     {
395       QString aRegName = aRegion->GetName();
396       QString anObjName = aRegionNameToObjNameMap[aRegName];
397       anObjectsWithEmptyRegions.append( anObjName );
398     }
399   }
400   
401   if( !anObjectsWithEmptyRegions.empty() )
402   {
403     QString aData = anObjectsWithEmptyRegions.join( ", " );
404     SetWarning( WARN_EMPTY_REGIONS, aData );
405   }
406 }
407
408 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
409                                                      const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
410 {
411   QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
412
413   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
414   while( anIter.hasNext() )
415   {
416     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
417     // Create new edges group
418     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
419       continue;
420
421     QString anObjName = aSplitData.ObjectNames.first();
422     if ( anObjName.isEmpty() )
423       continue;
424 #ifdef DEB_CALCULATION
425     QString aStr = aSplitData.ObjectNames.join(" "); 
426           cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl; 
427 #endif
428     Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
429     if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
430     {
431       aSplittedGroup = addNewSplittedGroup( CALCULATION_GROUPS_PREF + anObjName );
432       aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
433     }
434     else
435     {
436       aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
437     }
438     if ( aSplittedGroup.IsNull() )
439       continue;
440
441     aSplittedGroup->AddShape( aSplitData.Shape );
442   }
443 }
444
445 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
446 {
447   if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
448     return false; // Wrong type of object
449
450   if ( HasReference( theObject, DataTag_GeometryObject ) )
451     return false; // Object is already in reference list
452
453   AddReferenceObject( theObject, DataTag_GeometryObject );
454   
455   // Indicate model of the need to update splitting
456   SetToUpdate( true );
457
458   return true;
459 }
460
461 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
462 {
463   return GetReferenceObjects( DataTag_GeometryObject );
464 }
465
466 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
467 {
468   if ( theObject.IsNull() )
469     return;
470
471   RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
472
473   // Indicate model of the need to update splitting
474   SetToUpdate( true );
475 }
476
477 void HYDROData_CalculationCase::RemoveGeometryObjects()
478 {
479   ClearReferenceObjects( DataTag_GeometryObject );
480
481   // Indicate model of the need to update splitting
482   SetToUpdate( true );
483 }
484
485 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
486 {
487   if ( theGroup.IsNull() )
488     return false;
489
490   if ( HasReference( theGroup, DataTag_GeometryGroup ) )
491     return false; // Object is already in reference list
492
493   AddReferenceObject( theGroup, DataTag_GeometryGroup );
494   
495   // Indicate model of the need to update splitting
496   SetToUpdate( true );
497
498   return true;
499 }
500
501 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
502 {
503   return GetReferenceObjects( DataTag_GeometryGroup );
504 }
505
506 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
507 {
508   if ( theGroup.IsNull() )
509     return;
510
511   RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
512
513   // Indicate model of the need to update splitting
514   SetToUpdate( true );
515 }
516
517 void HYDROData_CalculationCase::RemoveGeometryGroups()
518 {
519   ClearReferenceObjects( DataTag_GeometryGroup );
520
521   // Indicate model of the need to update splitting
522   SetToUpdate( true );
523 }
524
525 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
526 {
527   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
528
529   SetReferenceObject( thePolyline, DataTag_Polyline );
530
531   // Indicate model of the need to update zones splitting
532   SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
533 }
534
535 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
536 {
537   return Handle(HYDROData_PolylineXY)::DownCast( 
538            GetReferenceObject( DataTag_Polyline ) );
539 }
540
541 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
542 {
543   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
544
545   ClearReferenceObjects( DataTag_Polyline );
546
547   // Indicate model of the need to update zones splitting
548   SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
549 }
550
551 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
552 {
553   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
554   Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
555   if ( aNewRegion.IsNull() )
556     return aNewRegion;
557
558   aNewRegion->AddZone( theZone );
559
560   return aNewRegion;
561 }
562
563 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
564 {
565   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
566
567   if ( theRegion.IsNull() )
568     return false;
569   
570   if ( HasReference( theRegion, DataTag_Region ) )
571     return false; // Object is already in reference list
572
573   // Move the region from other calculation
574   Handle(HYDROData_CalculationCase) aFatherCalc = 
575     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
576   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
577   {
578     Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
579     theRegion->CopyTo( aNewRegion );
580
581     aFatherCalc->RemoveRegion( theRegion );
582
583     theRegion->SetLabel( aNewRegion->Label() );
584   }
585   else
586   {
587     AddReferenceObject( theRegion, DataTag_Region );
588   }
589
590   return true;
591 }
592
593 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
594 {
595   return GetReferenceObjects( DataTag_Region );
596 }
597
598 void HYDROData_CalculationCase::UpdateRegionsOrder()
599 {
600   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
601   if ( aDocument.IsNull() )
602     return;
603
604   HYDROData_SequenceOfObjects aRegions = GetRegions();
605
606   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
607   for ( ; anIter.More(); anIter.Next() )
608   {
609     Handle(HYDROData_Region) aRegion =
610       Handle(HYDROData_Region)::DownCast( anIter.Value() );
611     if ( aRegion.IsNull() )
612       continue;
613
614     aRegion->SetName( "" );
615   }
616
617   QString aRegsPref = CALCULATION_REGIONS_PREF;
618
619   anIter.Init( aRegions );
620   for ( ; anIter.More(); anIter.Next() )
621   {
622     Handle(HYDROData_Region) aRegion =
623       Handle(HYDROData_Region)::DownCast( anIter.Value() );
624     if ( aRegion.IsNull() )
625       continue;
626
627     QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
628     aRegion->SetName( aRegionName );
629   }
630 }
631
632 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
633 {
634   if ( theRegion.IsNull() )
635     return;
636
637   RemoveReferenceObject( theRegion->Label(), DataTag_Region );
638
639   // Remove region from data model
640   Handle(HYDROData_CalculationCase) aFatherCalc = 
641     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
642   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
643     theRegion->Remove();
644 }
645
646 void HYDROData_CalculationCase::RemoveRegions()
647 {
648   myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
649 }
650
651 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
652 {
653   return GetReferenceObjects( DataTag_SplittedGroups );
654 }
655
656 void HYDROData_CalculationCase::RemoveSplittedGroups()
657 {
658   myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
659 }
660
661 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
662 {
663   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
664   return GetAltitudeForPoint( thePoint, aZone );
665 }
666
667 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                    thePoint,
668                                                        const Handle(HYDROData_Region)& theRegion ) const
669 {
670   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
671
672   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
673   if ( !aZone.IsNull() )
674   {
675     Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
676     if ( IsEqual( aRefRegion, theRegion ) )
677       aResAltitude = GetAltitudeForPoint( thePoint, aZone );
678   }
679
680   return aResAltitude;
681 }
682
683 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                  thePoint,
684                                                        const Handle(HYDROData_Zone)& theZone ) const
685 {
686   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
687   if ( theZone.IsNull() )
688     return aResAltitude;
689
690   HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
691   if ( !theZone->IsMergingNeed() )
692   {
693     aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
694   }
695   else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
696   {
697     return aResAltitude;
698   }
699
700   HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
701   if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
702   {
703     Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
704     if ( !aMergeAltitude.IsNull() )
705     {
706       if ( aZoneInterpolator != NULL )
707       {
708         aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
709         aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
710       }
711       else
712         aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
713     }
714   }
715   else
716   {
717     HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
718     HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
719     for ( ; anIter.More(); anIter.Next() )
720     {
721       Handle(HYDROData_Object) aZoneObj =
722         Handle(HYDROData_Object)::DownCast( anIter.Value() );
723       if ( aZoneObj.IsNull() )
724         continue;
725
726       Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
727       if ( anObjAltitude.IsNull() )
728         continue;
729
730       double aPointAltitude = 0.0;
731       if ( aZoneInterpolator != NULL )
732       {
733         aZoneInterpolator->SetAltitudeObject( anObjAltitude );
734         aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
735       }
736       else
737         aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
738
739       if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
740         continue;
741
742       if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
743       {
744         aResAltitude = aPointAltitude;
745         break;
746       }
747       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
748       {
749         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
750              aResAltitude > aPointAltitude )
751         {
752           aResAltitude = aPointAltitude;
753         }
754       }
755       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
756       {
757         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
758              aResAltitude < aPointAltitude )
759         {
760           aResAltitude = aPointAltitude;
761         }
762       }
763     }
764   }
765
766   return aResAltitude;
767 }
768
769 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
770   const NCollection_Sequence<gp_XY>& thePoints,
771   const Handle(HYDROData_Region)&    theRegion ) const
772 {
773   NCollection_Sequence<double> aResSeq;
774
775   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
776   {
777     const gp_XY& thePnt = thePoints.Value( i );
778     
779     double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
780     aResSeq.Append( anAltitude );
781   }
782
783   return aResSeq;
784 }
785
786 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
787   const NCollection_Sequence<gp_XY>& thePoints,
788   const Handle(HYDROData_Zone)&      theZone ) const
789 {
790   NCollection_Sequence<double> aResSeq;
791
792   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
793   {
794     const gp_XY& thePnt = thePoints.Value( i );
795     
796     double anAltitude = GetAltitudeForPoint( thePnt, theZone );
797     aResSeq.Append( anAltitude );
798   }
799
800   return aResSeq;
801 }
802
803 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
804 {
805   Handle(HYDROData_Region) aResRegion;
806
807   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
808   if ( !aZone.IsNull() )
809     aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
810
811   return aResRegion;
812 }
813
814 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
815 {
816   Handle(HYDROData_Zone) aResZone;
817
818   HYDROData_SequenceOfObjects aRegions = GetRegions();
819
820   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
821   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
822   {
823     Handle(HYDROData_Region) aRegion =
824       Handle(HYDROData_Region)::DownCast( anIter.Value() );
825     if ( aRegion.IsNull() )
826       continue;
827
828     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
829     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
830     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
831     {
832       Handle(HYDROData_Zone) aRegZone =
833         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
834       if ( aRegZone.IsNull() )
835         continue;
836
837       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
838       if ( aPointRelation != POINT_OUT )
839         aResZone = aRegZone; // We found the desired zone
840     }
841   }
842
843   return aResZone;
844 }
845
846 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
847   const gp_XY&                  thePoint,
848   const Handle(HYDROData_Zone)& theZone ) const
849 {
850   PointClassification aRes = POINT_OUT;
851   if ( theZone.IsNull() )
852     return aRes;
853
854   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
855   if ( aZoneFace.IsNull() )
856     return aRes;
857 #ifdef DEB_CLASS2D      
858           TopoDS_Compound aCmp;
859       BRep_Builder aBB;
860       aBB.MakeCompound(aCmp);
861           aBB.Add(aCmp, aZoneFace);
862           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
863           BRepBuilderAPI_MakeVertex aMk(aPnt);
864           aBB.Add(aCmp, aMk.Vertex());
865           BRepTools::Write(aCmp, "FCL2d.brep");
866 #endif  
867   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
868   if (State == TopAbs_OUT)
869     aRes =  POINT_OUT;
870   else if(State == TopAbs_IN)
871     aRes =  POINT_IN;
872   else if(State == TopAbs_ON)
873     aRes =  POINT_ON;
874   return aRes;
875 }
876
877 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
878                                                                   const QString& thePrefixOrName,
879                                                                   bool isPrefix )
880 {
881   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
882
883   Handle(HYDROData_Region) aNewRegion =
884     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
885   AddRegion( aNewRegion );
886
887   QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
888   aNewRegion->SetName( aRegionName );
889
890   return aNewRegion;
891 }
892
893 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
894 {
895   TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
896
897   Handle(HYDROData_SplittedShapesGroup) aNewGroup =
898     Handle(HYDROData_SplittedShapesGroup)::DownCast( 
899       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
900   AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
901
902   aNewGroup->SetName( theName );
903
904   return aNewGroup;
905 }
906
907 QString HYDROData_CalculationCase::Export( int theStudyId ) const
908 {
909   int argc = 0;
910   char** argv = 0;
911   CORBA::ORB_var anORB = CORBA::ORB_init( argc, argv, "omniORB4"/*CORBA::ORB_ID*/ );
912   SALOME_NamingService aNameService( anORB );
913   SALOME_LifeCycleCORBA aLCC( &aNameService );
914   Engines::EngineComponent_var aComponent = aLCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
915   GEOM::GEOM_Gen_var aGEOMEngine = GEOM::GEOM_Gen::_narrow( aComponent );
916   
917   CORBA::Object_var aSMObject = aNameService.Resolve("/myStudyManager");
918   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
919   SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID( theStudyId );
920
921   QString aGeomObjEntry;
922   bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry );
923   return isOK ? aGeomObjEntry : QString();
924 }
925
926 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
927                                         SALOMEDS::Study_ptr theStudy,
928                                         QString& theGeomObjEntry ) const
929 {
930   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
931
932   // Get groups definitions
933   HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
934
935   HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
936   for ( ; anIter.More(); anIter.Next() )
937   {
938     // Get shapes group
939     Handle(HYDROData_ShapesGroup) aGroup =
940       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
941     if ( aGroup.IsNull() )
942       continue;
943
944     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
945
946     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
947     aGroup->GetShapes( aGroupDef.Shapes );
948
949     aSeqOfGroupsDefs.Append( aGroupDef );
950   }
951   
952   // Get faces
953   TopTools_ListOfShape aFaces;
954   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
955   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
956   for ( ; aRegionIter.More(); aRegionIter.Next() )
957   {
958     Handle(HYDROData_Region) aRegion =
959       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
960     if( aRegion.IsNull() )
961       continue;
962
963     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
964     aFaces.Append( aRegionShape );
965   }
966
967   return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );
968 }
969
970 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
971                                         SALOMEDS::Study_ptr                           theStudy,
972                                         const TopTools_ListOfShape&                   theFaces,
973                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
974                                         QString& theGeomObjEntry ) const
975 {
976   // Sew faces
977   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
978   aSewing.SetNonManifoldMode( Standard_False );
979 #ifdef DEB_CALCULATION
980   TCollection_AsciiString aNam("Sh_");
981   int i=1;
982 #endif
983   TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
984   for ( ; aFaceIter.More(); aFaceIter.Next() )
985   {
986     TopoDS_Shape aShape = aFaceIter.Value();
987     if ( aShape.IsNull() )
988       continue;
989
990     if ( aShape.ShapeType() == TopAbs_FACE )
991     {
992       aSewing.Add( aShape );
993 #ifdef DEB_CALCULATION
994       TCollection_AsciiString aName = aNam + ++i + ".brep";
995       BRepTools::Write(aShape ,aName.ToCString());
996 #endif
997     }
998     else
999     {
1000 #ifdef DEB_CALCULATION
1001       int j = 1;
1002 #endif
1003       TopExp_Explorer anExp( aShape, TopAbs_FACE );
1004       for (; anExp.More(); anExp.Next() ) {
1005         aSewing.Add( anExp.Current() );
1006 #ifdef DEB_CALCULATION
1007
1008         TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
1009         BRepTools::Write(anExp.Current() ,aName.ToCString());
1010 #endif
1011       }
1012     }
1013   } // faces iterator
1014   
1015   aSewing.Perform();
1016   TopoDS_Shape aSewedShape = aSewing.SewedShape();
1017
1018   // If the sewed shape is empty - return false
1019   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1020     return false;
1021
1022 #ifdef DEB_CALCULATION
1023   BRepTools::Write(aSewedShape ,"Sew.brep");
1024 #endif
1025   // Publish the sewed shape
1026   QString aName = EXPORT_NAME;
1027   GEOM::GEOM_Object_ptr aMainShape = 
1028     publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
1029
1030   if ( aMainShape->_is_nil() )  
1031     return false;
1032
1033   if ( theGroupsDefs.IsEmpty() )
1034     return true;
1035
1036   // Create groups
1037   TopTools_IndexedMapOfShape aMapOfSubShapes;
1038   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1039
1040   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1041
1042   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1043   {
1044     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1045
1046     NCollection_Sequence<int> aGroupIndexes;
1047     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1048     {
1049       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1050 #ifdef DEB_CALCULATION
1051       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1052 #endif
1053       
1054       TopoDS_Shape aModifiedShape = aShape;
1055       if ( aSewing.IsModified( aShape ) )
1056         aModifiedShape = aSewing.Modified( aShape );
1057       else if ( aSewing.IsModifiedSubShape( aShape ) )
1058         aModifiedShape = aSewing.ModifiedSubShape( aShape );
1059
1060 #ifdef DEB_CALCULATION
1061       const TopLoc_Location& aL1 = aShape.Location();
1062       const TopLoc_Location& aL2 = aModifiedShape.Location();
1063       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1064 #endif
1065
1066       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1067       if ( anIndex > 0 ) {
1068         aGroupIndexes.Append( anIndex );
1069       } else {
1070 #ifdef DEB_CALCULATION    
1071         TCollection_AsciiString aNam("Lost_");
1072         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1073         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1074         {
1075            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1076            if ( aModifiedShape.IsPartner( aS ) )
1077            {
1078              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1079              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1080              BRepTools::Write(aS ,aName.ToCString());
1081             break;
1082            }
1083          }
1084         }
1085 #endif
1086       }
1087     }
1088     if ( !aGroupIndexes.IsEmpty() )
1089       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1090   }
1091  
1092   if ( !aGroupsData.IsEmpty() )
1093   {
1094     GEOM::GEOM_IGroupOperations_var aGroupOp = 
1095       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
1096
1097     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1098     for ( ; aMapIt.More(); aMapIt.Next() )
1099     {
1100       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
1101       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1102
1103       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1104       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1105         continue;
1106
1107       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1108       aGeomIndexes->length( aGroupIndexes.Length() );
1109
1110       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1111         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1112
1113       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1114       if ( aGroupOp->IsDone() )
1115       {
1116         SALOMEDS::SObject_var aGroupSO = 
1117           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1118       }
1119     }
1120   }
1121
1122   return true;
1123 }
1124
1125 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM( 
1126   GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
1127   const TopoDS_Shape& theShape, const QString& theName,
1128   QString& theGeomObjEntry ) const
1129 {
1130   theGeomObjEntry = "";
1131   GEOM::GEOM_Object_var aGeomObj;
1132
1133   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
1134        theShape.IsNull() ) {
1135     return aGeomObj._retn();
1136   }
1137
1138   std::ostringstream aStreamShape;
1139   // Write TopoDS_Shape in ASCII format to the stream
1140   BRepTools::Write( theShape, aStreamShape );
1141   // Returns the number of bytes that have been stored in the stream's buffer.
1142   int aSize = aStreamShape.str().size();
1143   // Allocate octect buffer of required size
1144   CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
1145   // Copy ostrstream content to the octect buffer
1146   memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
1147   // Create TMPFile
1148   SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
1149
1150   // Restore shape from the stream and get the GEOM object
1151   GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
1152   aGeomObj = anInsOp->RestoreShape( aSeqFile );
1153   
1154   // Puplish the GEOM object
1155   if ( !aGeomObj->_is_nil() ) {
1156     QString aName = GEOMBase::GetDefaultName( theName );
1157
1158     SALOMEDS::SObject_var aResultSO = 
1159       theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), 
1160                                      aGeomObj, qPrintable( aName ) );
1161     if ( aResultSO->_is_nil() ) {
1162       aGeomObj = GEOM::GEOM_Object::_nil();
1163     }
1164     else
1165       theGeomObjEntry = aResultSO->GetID();
1166   }
1167
1168   return aGeomObj._retn();
1169 }
1170
1171 void HYDROData_CalculationCase::ClearRules( const bool theIsSetToUpdate )
1172 {
1173   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1174   HYDROData_PriorityQueue::ClearRules( aRulesLab );
1175
1176   // Indicate model of the need to update splitting
1177   if ( theIsSetToUpdate ) {
1178     SetToUpdate( true );
1179   }
1180 }
1181
1182 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Object)&    theObject1,
1183                                          HYDROData_PriorityType             thePriority,
1184                                          const Handle(HYDROData_Object)&    theObject2,
1185                                          HYDROData_Zone::MergeAltitudesType theMergeType )
1186 {
1187   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1188   HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1189
1190   // Indicate model of the need to update splitting
1191   SetToUpdate( true );
1192 }
1193
1194 QString HYDROData_CalculationCase::DumpRules() const
1195 {
1196   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1197   return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1198 }
1199
1200 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1201 {
1202   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1203   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1204
1205   // Indicate model of the need to update splitting
1206   SetToUpdate( true );
1207 }
1208
1209 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1210 {
1211   Handle(TDataStd_Integer) aModeAttr;
1212   bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1213   if( isOK )
1214     return ( AssignmentMode ) aModeAttr->Get();
1215   else
1216     return MANUAL;
1217 }
1218
1219 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1220                                                    QStringList& theScript ) const
1221 {
1222   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1223   HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1224 }
1225
1226 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1227 {
1228   return myLastWarning;
1229 }
1230
1231 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1232 {
1233   myLastWarning.Type = theType;
1234   myLastWarning.Data = theData;
1235 }
1236
1237 bool HYDROData_CalculationCase::GetRule( int theIndex, 
1238                                          Handle(HYDROData_Object)&           theObject1,
1239                                          HYDROData_PriorityType&             thePriority,
1240                                          Handle(HYDROData_Object)&           theObject2,
1241                                          HYDROData_Zone::MergeAltitudesType& theMergeType ) const
1242 {
1243   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1244   return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1245     theObject1, thePriority, theObject2, theMergeType );
1246 }