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