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