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