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