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