]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_CalculationCase.cxx
Salome HOME
Merge branch 'BR_H2018_2' of https://codev-tuleap.cea.fr/plugins/git/salome/hydro...
[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 #include "HYDROData_ArtificialObject.h"
21 #include "HYDROData_IAltitudeObject.h"
22 #include "HYDROData_Document.h"
23 #include "HYDROData_ShapesGroup.h"
24 #include "HYDROData_Iterator.h"
25 #include "HYDROData_NaturalObject.h"
26 #include "HYDROData_PolylineXY.h"
27 #include "HYDROData_StricklerTable.h"
28 #include "HYDROData_LandCoverMap.h"
29 #include "HYDROData_SplitShapesGroup.h"
30 #include "HYDROData_Region.h"
31 #include "HYDROData_Tool.h"
32 #include "HYDROData_GeomTool.h"
33 #include <HYDROData_Tool.h>
34 #include <HYDROData_BCPolygon.h>
35 #include <HYDROData_BoundaryPolygonTools.h>
36
37 #ifdef WIN32
38   #pragma warning ( disable: 4251 )
39 #endif
40
41 #ifndef LIGHT_MODE
42 #include <GEOMBase.h>
43 #endif
44
45 #ifdef WIN32
46   #pragma warning ( default: 4251 )
47 #endif
48
49 #include <QSet>
50 #include <QFuture>
51 #include <QtConcurrent/QtConcurrent>
52 #include <QThread>
53
54 #include <TopoDS.hxx>
55 #include <TopoDS_Shell.hxx>
56 #include <TopoDS_Edge.hxx>
57
58 #include <BRep_Builder.hxx>
59 #include <BRepBuilderAPI_Sewing.hxx>
60 #include <BRepTopAdaptor_FClass2d.hxx>
61 #include <BRepTools_ReShape.hxx>
62 #include <BRepLib_MakeWire.hxx>
63
64 #include <BRepTools.hxx>
65
66 #include <TopAbs.hxx>
67 #include <TopExp_Explorer.hxx>
68 #include <TopExp.hxx>
69 #include <TopTools_ListOfShape.hxx>
70 #include <TopTools_ListIteratorOfListOfShape.hxx>
71 #include <TDataStd_Integer.hxx>
72
73 #include <Message_ProgressSentry.hxx>
74
75 //#define  DEB_CALCULATION 1
76 #ifdef DEB_CALCULATION
77 #include <BRepTools.hxx>
78 #include <TopLoc_Location.hxx>
79 #endif 
80 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
81 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
82 #define CALCULATION_GROUPS_PREF GetName() + "_"
83 //#define DEB_CLASS2D 1
84 #ifdef DEB_CLASS2D
85 #include <BRepBuilderAPI_MakeVertex.hxx>
86 #endif
87
88 #define EXPORT_NAME "HYDRO_" + GetName()
89
90 #ifndef LIGHT_MODE
91 #include <SALOME_NamingService.hxx>
92 #include <SALOME_LifeCycleCORBA.hxx>
93 #endif
94
95 //#define _DEVDEBUG_
96 #include "HYDRO_trace.hxx"
97
98 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
99
100 HYDROData_CalculationCase::HYDROData_CalculationCase()
101 : HYDROData_Entity( Geom_2d_and_groups )
102 {
103 }
104
105 HYDROData_CalculationCase::~HYDROData_CalculationCase()
106 {
107 }
108
109 void HYDROData_CalculationCase::SetName( const QString& theName )
110 {
111   QString anOldCaseName = GetName();
112   if ( anOldCaseName != theName )
113   {
114     // Update names of regions and its zones
115     UpdateRegionsNames( GetRegions(), anOldCaseName, theName );
116
117     HYDROData_SequenceOfObjects aGroups = GetSplitGroups();
118
119     HYDROData_SequenceOfObjects::Iterator anIter;
120     anIter.Init( aGroups );
121     for ( ; anIter.More(); anIter.Next() )
122     {
123       Handle(HYDROData_SplitShapesGroup) aGroup =
124         Handle(HYDROData_SplitShapesGroup)::DownCast( anIter.Value() );
125       if ( aGroup.IsNull() )
126         continue;
127
128       HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
129     }
130   }
131
132   HYDROData_Entity::SetName( theName );
133 }
134
135 QStringList HYDROData_CalculationCase::DumpToPython( const QString&       thePyScriptPath,
136                                                      MapOfTreatedObjects& theTreatedObjects ) const
137 {
138   QStringList aResList = dumpObjectCreation( theTreatedObjects );
139   aResList.prepend( "# Calculation case" );
140
141   QString aCalculName = GetObjPyName();
142
143   AssignmentMode aMode = GetAssignmentMode();  
144   QString aModeStr = aMode==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
145   aResList << QString( "%0.SetAssignmentMode( %1 )" ).arg( aCalculName ).arg( aModeStr );
146
147   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
148   HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
149   for ( ; anIter.More(); anIter.Next() )
150   {
151     Handle(HYDROData_Object) aRefGeomObj =
152       Handle(HYDROData_Object)::DownCast( anIter.Value() );
153     setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
154   }
155   aResList << QString( "" );
156
157   QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
158
159   HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
160   anIter.Init( aGeomGroups );
161   for ( ; anIter.More(); anIter.Next() )
162   {
163     Handle(HYDROData_ShapesGroup) aGeomGroup =
164       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
165     if ( aGeomGroup.IsNull() )
166       continue;
167
168     Handle(HYDROData_Object) aFatherGeom =
169       Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
170     if ( aFatherGeom.IsNull() )
171       continue;
172
173     int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
174     aResList << QString( "%1 = %2.GetGroup( %3 )" )
175               .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
176
177     aResList << QString( "%1.AddGeometryGroup( %2 )" ).arg( aCalculName ).arg( aGroupName );
178   }
179
180   HYDROData_SequenceOfObjects aBPolygons = GetBoundaryPolygons();
181   for (int i = 1; i <= aBPolygons.Size(); i++ )
182   {
183     Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( aBPolygons(i) );
184     setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aBCPoly, "AddBoundaryPolygon" );
185   }
186   aResList << QString( "" );
187
188   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
189   setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
190
191   Handle(HYDROData_StricklerTable) aStricklerTable = GetStricklerTable();
192   setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aStricklerTable, "SetStricklerTable" );
193
194   Handle(HYDROData_LandCoverMap) aLandCoverMap = GetLandCoverMap();
195   setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aLandCoverMap, "SetLandCoverMap" );
196
197   if( aMode==AUTOMATIC )
198     DumpRulesToPython( aCalculName, aResList );
199
200   aResList << QString( "" );
201   aResList << "# Start the algorithm of the partition and assignment";
202   aResList << QString( "%1.Update()" ).arg( aCalculName );
203
204   if( aMode==MANUAL )
205   {
206     // Now we restore the
207     // - regions and zones order
208     DumpRegionsToPython( aResList, thePyScriptPath, theTreatedObjects, GetRegions() );   
209   }
210
211   // Export calculation case
212   aResList << QString( "" );
213   aResList << "# Export of the calculation case";
214   QString aStudyName = "theStudy";
215   QString anEntryVar = aCalculName + "_entry";
216   aResList << QString( "%1 = %2.Export( %3._get_StudyId() )" ).arg( anEntryVar ).arg( aCalculName ).arg( aStudyName );
217
218   // Get geometry object and print debug information
219   aResList << "";
220   aResList << "# Get geometry shape and print debug information";
221   aResList << "import GEOM";
222   aResList << QString( "print \"Entry:\", %1" ).arg( anEntryVar );
223   QString aGeomShapeName = aCalculName + "_geom";
224   aResList << QString( "HYDRO_%1 = salome.IDToObject( str( %2 ) )" ).arg( GetName() ).arg( anEntryVar );
225   aResList << QString( "print \"Geom shape:\", HYDRO_%1" ).arg( GetName() );
226   aResList << QString( "print \"Geom shape name:\", HYDRO_%1.GetName()" ).arg( GetName() );
227
228
229   //DumpSampleMeshing( aResList, aStudyName, aGeomShapeName, aCalculName+"_mesh" );
230
231   aResList << QString( "" );
232   return aResList;
233 }
234
235 void HYDROData_CalculationCase::DumpSampleMeshing( QStringList& theResList,
236                                                    const QString& theStudyName,
237                                                    const QString& theGeomShapeName,
238                                                    const QString& theMeshName ) const
239 {
240   theResList << "";
241   theResList << "# Meshing";
242   theResList << "import SMESH, SALOMEDS";
243   theResList << "from salome.smesh import smeshBuilder";
244   theResList << "from salome.geom import geomBuilder";
245
246   theResList << QString( "smesh = smeshBuilder.New( %1 )" ).arg( theStudyName );
247   theResList << QString( "%1 = smesh.Mesh( %2 )" ).arg( theMeshName ).arg( theGeomShapeName );
248   theResList << QString( "MEFISTO_2D = %1.Triangle( algo=smeshBuilder.MEFISTO )" ).arg( theMeshName );
249   theResList << "Max_Element_Area_1 = MEFISTO_2D.MaxElementArea( 10 )";
250   theResList << QString( "Regular_1D = %1.Segment()" ).arg( theMeshName );
251   theResList << "Max_Size_1 = Regular_1D.MaxSize(10)";
252   theResList << QString( "isDone = %1.Compute()" ).arg( theMeshName );
253
254   theResList << "";
255   theResList << "# Set names of Mesh objects";
256   theResList << "smesh.SetName( MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D' )";
257   theResList << "smesh.SetName( Regular_1D.GetAlgorithm(), 'Regular_1D' )";
258   theResList << "smesh.SetName( Max_Size_1, 'Max Size_1' )";
259   theResList << "smesh.SetName( Max_Element_Area_1, 'Max. Element Area_1' )";
260   theResList << QString( "smesh.SetName( %1.GetMesh(), '%1' )" ).arg( theMeshName );
261
262   theResList << "";
263   theResList << "# Greate SMESH groups";
264   theResList << QString( "geompy = geomBuilder.New( %1 )" ).arg( theStudyName );
265   theResList << QString( "geom_groups = geompy.GetGroups( %1 )" ).arg( theGeomShapeName );
266   theResList << QString( "for group in geom_groups:" );
267   theResList << QString( "    smesh_group = %1.GroupOnGeom(group, group.GetName(), SMESH.EDGE)" )
268                 .arg( theMeshName );
269   theResList << QString( "    smesh.SetName(smesh_group, group.GetName())" );
270   theResList << QString( "    print \"SMESH group '%s': %s\" % (smesh_group.GetName(), smesh_group)" );
271 }
272
273 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
274 {
275   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
276
277   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
278   if ( !aBoundaryPolyline.IsNull() )
279     aResSeq.Append( aBoundaryPolyline );
280
281   HYDROData_SequenceOfObjects aSeqOfGeomObjs = GetGeometryObjects();
282   aResSeq.Append( aSeqOfGeomObjs );
283
284   // Regions
285   HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
286   aResSeq.Append( aSeqOfRegions );
287
288   return aResSeq;
289 }
290
291 static void FilterEdgesByIncludeSelectionBoundaryPolygons( const HYDROData_SplitToZonesTool::SplitDataList& anEdgesList,
292                                                            HYDROData_SequenceOfObjects aBoundaryPolygons,
293                                                            HYDROData_SplitToZonesTool::SplitDataList& outEdgesList)
294 {
295   //perform boundary condition polygons on EdgesList
296   TopTools_SequenceOfShape IncTools, SelectionTools;
297   HYDROData_SplitToZonesTool::SplitDataListIterator anIter(anEdgesList);
298   for (int i=1; i<=aBoundaryPolygons.Size();i++)
299   {
300     Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( aBoundaryPolygons(i));
301     TopoDS_Shape aPolyTopShape = aBCPoly->GetTopShape();
302     int bType = aBCPoly->GetBoundaryType();
303     if (bType == 2) 
304       IncTools.Append(aPolyTopShape);
305     else if (bType == 3)
306       SelectionTools.Append(aPolyTopShape);
307   }
308   
309   while( anIter.hasNext() )
310   {
311     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
312     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
313       continue;
314     if (aSplitData.Shape.ShapeType() != TopAbs_EDGE)
315       continue;
316     if (HYDROData_BoundaryPolygonTools::IncludeTool(IncTools, aSplitData.Shape) && HYDROData_BoundaryPolygonTools::SelectionTool(SelectionTools, aSplitData.Shape))
317       outEdgesList.append(aSplitData);
318   }
319 }
320
321
322 static void SplitEdgesByBoundaryPolygons( const HYDROData_SplitToZonesTool::SplitDataList& anEdgesList,
323                                           const HYDROData_SequenceOfObjects& aBoundaryPolygons,
324                                           NCollection_IndexedDataMap<TopoDS_Shape, TopoDS_Shape>& ObjToRes)
325 {
326   //perform boundary condition polygons on EdgesList
327   TopTools_SequenceOfShape CutTools;
328   ObjToRes.Clear();
329   HYDROData_SplitToZonesTool::SplitDataListIterator anIter(anEdgesList);
330   for (int i=1; i<=aBoundaryPolygons.Size();i++)
331   {
332     Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( aBoundaryPolygons(i));
333     TopoDS_Shape aPolyTopShape = aBCPoly->GetTopShape();
334     int bType = aBCPoly->GetBoundaryType();
335     if (bType == 1) 
336       CutTools.Append(aPolyTopShape);
337   }
338   
339   while( anIter.hasNext() )
340   {
341     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
342     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
343       continue;
344     if (aSplitData.Shape.ShapeType() != TopAbs_EDGE)
345       continue;
346     ObjToRes.Add(aSplitData.Shape, TopoDS_Shape());
347   }
348
349   HYDROData_BoundaryPolygonTools::CutTool(CutTools, ObjToRes);
350 }
351
352
353 static void PerformEdgeReplInZones(const HYDROData_SplitToZonesTool::SplitDataList& ZoneList,
354                                    const NCollection_IndexedDataMap<TopoDS_Shape, TopoDS_Shape>& ObjToRes,
355                                    HYDROData_SplitToZonesTool::SplitDataList& outZoneList)
356 {
357   HYDROData_SplitToZonesTool::SplitDataListIterator it( ZoneList ); 
358   BRepTools_ReShape reshaper;
359   for (;it.hasNext();)
360   {
361     const HYDROData_SplitToZonesTool::SplitData& aSplitData = it.next();
362     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
363       continue;
364
365     TopoDS_Shape mS = aSplitData.Shape;
366     for (int i=1; i<=ObjToRes.Extent();i++)
367     {
368       TopoDS_Shape K = ObjToRes.FindKey(i);
369       TopoDS_Shape V = ObjToRes.FindFromIndex(i);
370       if (V.ShapeType() != TopAbs_EDGE && V.ShapeType() != TopAbs_WIRE && V.ShapeType() != TopAbs_COMPOUND)
371         continue;
372       if (V.ShapeType() == TopAbs_COMPOUND)
373       {
374         TopExp_Explorer exp(V, TopAbs_EDGE);
375         TopTools_ListOfShape lE;
376         for (;exp.More();exp.Next())
377           lE.Append(exp.Current());
378         if (lE.Extent() == 1)
379           V = lE.First();
380         else if (lE.Extent() > 1)
381         {
382           BRepLib_MakeWire MW;
383           MW.Add(lE);
384           if (MW.IsDone())
385             V = MW.Wire();
386           else
387             continue;
388         }
389         else 
390           continue;
391       }
392       reshaper.Replace(K, V);
393     }
394     TopoDS_Shape nS = reshaper.Apply(mS);
395     HYDROData_SplitToZonesTool::SplitData aNS(aSplitData.Type, nS, aSplitData.ObjectNames);
396     outZoneList.append(aNS);
397   } 
398 }
399
400 static void CreateNewEdgeList( const HYDROData_SplitToZonesTool::SplitDataList& theEdges,
401                                const NCollection_IndexedDataMap<TopoDS_Shape, TopoDS_Shape>& ObjToRes,
402                                HYDROData_SplitToZonesTool::SplitDataList& newEdges)
403 {
404   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
405   while( anIter.hasNext() )
406   {
407     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
408     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
409       continue;
410
411     const TopoDS_Shape* aDivShape = ObjToRes.Seek(aSplitData.Shape);
412     if (aDivShape)
413     {
414       TopExp_Explorer exp(*aDivShape, TopAbs_EDGE);
415       for (;exp.More();exp.Next())
416       {
417         HYDROData_SplitToZonesTool::SplitData anNewSData(aSplitData.Type, exp.Current(), aSplitData.ObjectNames);
418         newEdges.append(anNewSData);
419       }
420     }
421     else
422       newEdges.append(aSplitData);
423   }
424 }
425
426
427 void HYDROData_CalculationCase::Update()
428 {
429   HYDROData_Entity::Update();
430   SetWarning();
431
432   // At first we remove previously created objects
433   RemoveRegions();
434   RemoveSplitGroups();
435
436   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
437   if ( aDocument.IsNull() )
438     return;
439
440   // Split to zones
441   HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
442
443   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
444
445   HYDROData_SequenceOfObjects InterPolys = GetInterPolyObjects();
446
447   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
448   if ( !aGeomObjects.IsEmpty() ) {
449     HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
450
451     HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
452       HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline, InterPolys );
453     if ( !aSplitObjects.isEmpty() ) {
454       HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
455       while( anIter.hasNext() ) {
456         const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
457         if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
458           aZonesList.append( aSplitData );
459         else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge ||
460                   aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_IntEdge)
461           anEdgesList.append( aSplitData );
462       }
463     }
464   }
465
466   //
467   //split edges by boundary polygons
468   HYDROData_SequenceOfObjects aBoundaryPolygons = GetBoundaryPolygons();
469   //edge to splitted edge (compound of edges or original edge)
470   NCollection_IndexedDataMap<TopoDS_Shape, TopoDS_Shape> ObjToRes;
471   //split edge list by BP
472   SplitEdgesByBoundaryPolygons(anEdgesList, aBoundaryPolygons, ObjToRes);
473   HYDROData_SplitToZonesTool::SplitDataList aNewZonesList;
474   //replace splitted edges in zone list (faces)
475   PerformEdgeReplInZones(aZonesList, ObjToRes, aNewZonesList);
476   //
477   //create new edges list based on splitting info from ObjToRes
478   HYDROData_SplitToZonesTool::SplitDataList newEdgesList1,newEdgesList2;
479   CreateNewEdgeList(anEdgesList, ObjToRes, newEdgesList1);
480   //filter out edges list by include&selection tools 
481   FilterEdgesByIncludeSelectionBoundaryPolygons(newEdgesList1,aBoundaryPolygons,newEdgesList2);
482   
483   switch( GetAssignmentMode() )
484   {
485   case MANUAL:
486     CreateRegionsDef( aDocument,aNewZonesList );    
487     break;
488   case AUTOMATIC:
489     CreateRegionsAuto( aDocument,aNewZonesList );    
490     break;
491   }
492
493   CreateEdgeGroupsDef( aDocument, newEdgesList2 );
494 }
495
496 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
497                                                   const HYDROData_SplitToZonesTool::SplitDataList& theZones )
498 {
499   // Create result regions for case, by default one zone for one region
500   QString aRegsPref = CALCULATION_REGIONS_PREF;
501   QString aZonesPref = CALCULATION_ZONES_PREF;
502
503   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
504   while( anIter.hasNext() )
505   {
506     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
507     // Create new region
508     Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
509
510     // Add the zone for region
511     Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
512   }
513 }
514
515 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
516                                                    const HYDROData_SplitToZonesTool::SplitDataList& theZones )
517 {
518   DEBTRACE("HYDROData_CalculationCase::CreateRegionsAuto");
519   QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
520   QMap<QString, QString> aRegionNameToObjNameMap;
521   QString aZonesPref = CALCULATION_ZONES_PREF;
522   HYDROData_PriorityQueue aPr( this, DataTag_CustomRules );
523
524   // 1. First we create a default region for each object included into the calculation case
525   HYDROData_SequenceOfObjects anObjects = GetGeometryObjects();
526   for( int i = anObjects.Lower(), n = anObjects.Upper(); i<=n; i++ )
527   {
528     Handle(HYDROData_Entity) anObj = anObjects.Value( i );
529     if( anObj.IsNull() )
530       continue;
531     QString anObjName = anObj->GetName();
532     QString aRegName = anObjName + "_reg";
533     Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
534     aRegionsMap.insert( anObjName, aRegion );
535     aRegionNameToObjNameMap.insert( aRegName, anObjName );
536   }
537
538   // 2. Now for each zone it is necessary to determine the most priority object
539   //    and assign to zone to corresponding region
540   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
541   while( anIter.hasNext() )
542   {
543     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
544     HYDROData_Zone::MergeType aMergeType;
545     Handle(HYDROData_Entity) aRegObj = aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType );
546     if( aRegObj.IsNull() )
547       continue;
548     Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
549     if( aRegion.IsNull() )
550       continue;
551     Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
552
553     if( aSplitData.ObjectNames.count() > 1 && aMergeType==HYDROData_Zone::Merge_UNKNOWN )
554     {
555       qDebug( "Error in algorithm: unresolved conflicts" );
556     }
557
558     Handle(HYDROData_Entity) aMergeEntity = aRegObj;
559     Handle(HYDROData_Object) aMergeObject = Handle(HYDROData_Object)::DownCast( aMergeEntity );
560     if ( !aMergeObject.IsNull() ) {
561       DEBTRACE("aMergeEntity " << aMergeEntity->GetName().toStdString());
562       aMergeEntity = aMergeObject->GetAltitudeObject();
563     }
564
565     switch( aMergeType )
566     {
567     case HYDROData_Zone::Merge_ZMIN:
568     case HYDROData_Zone::Merge_ZMAX:
569       aRegionZone->SetMergeType( aMergeType );
570       break;
571     case HYDROData_Zone::Merge_Object:
572       aRegionZone->SetMergeType( aMergeType );
573       aRegionZone->RemoveMergeObject();
574       aRegionZone->SetMergeObject( aMergeEntity );
575       break;
576     }
577   }
578
579   QStringList anObjectsWithEmptyRegions;
580   QMap<QString, Handle(HYDROData_Region)>::const_iterator
581     anIt = aRegionsMap.begin(), aLast = aRegionsMap.end();
582   for( ; anIt!=aLast; anIt++ )
583   {
584     Handle(HYDROData_Region) aRegion = anIt.value();
585     if( aRegion->GetZones().IsEmpty() )
586     {
587       QString aRegName = aRegion->GetName();
588       QString anObjName = aRegionNameToObjNameMap[aRegName];
589       anObjectsWithEmptyRegions.append( anObjName );
590     }
591   }
592   
593   if( !anObjectsWithEmptyRegions.empty() )
594   {
595     QString aData = anObjectsWithEmptyRegions.join( ", " );
596     SetWarning( WARN_EMPTY_REGIONS, aData );
597   }
598 }
599
600 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
601                                                      const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
602 {
603   QMap<QString,Handle(HYDROData_SplitShapesGroup)> aSplitEdgesGroupsMap;
604
605   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
606   while( anIter.hasNext() )
607   {
608     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
609     // Create new edges group
610     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
611       continue;
612
613     QString anObjName = aSplitData.ObjectNames.first();
614     if ( anObjName.isEmpty() )
615       continue;
616 #ifdef DEB_CALCULATION
617     QString aStr = aSplitData.ObjectNames.join(" "); 
618           cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl; 
619 #endif
620     Handle(HYDROData_SplitShapesGroup) aSplitGroup;
621     if ( !aSplitEdgesGroupsMap.contains( anObjName ) )
622     {
623       aSplitGroup = addNewSplitGroup( CALCULATION_GROUPS_PREF + anObjName );
624       aSplitEdgesGroupsMap.insert( anObjName, aSplitGroup );
625     }
626     else
627     {
628       aSplitGroup = aSplitEdgesGroupsMap[ anObjName ];
629     }
630     if ( aSplitGroup.IsNull() )
631       continue;
632
633       aSplitGroup->AddShape( aSplitData.Shape );
634
635     TopTools_SequenceOfShape theShapes;
636     aSplitGroup->GetShapes(theShapes);
637
638     if (aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_IntEdge)
639       aSplitGroup->SetInternal(true); 
640   }
641 }
642
643 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
644 {
645   if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
646     return false; // Wrong type of object
647
648   if ( HasReference( theObject, DataTag_GeometryObject ) )
649     return false; // Object is already in reference list
650
651   AddReferenceObject( theObject, DataTag_GeometryObject );
652   
653   // Indicate model of the need to update splitting
654   Changed( Geom_2d );
655
656   return true;
657 }
658
659 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
660 {
661   return GetReferenceObjects( DataTag_GeometryObject );
662 }
663
664 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
665 {
666   if ( theObject.IsNull() )
667     return;
668
669   RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
670
671   // Indicate model of the need to update splitting
672   Changed( Geom_2d );
673 }
674
675 void HYDROData_CalculationCase::RemoveGeometryObjects()
676 {
677   ClearReferenceObjects( DataTag_GeometryObject );
678
679   // Indicate model of the need to update splitting
680   Changed( Geom_2d );
681 }
682
683 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
684 {
685   if ( theGroup.IsNull() )
686     return false;
687
688   if ( HasReference( theGroup, DataTag_GeometryGroup ) )
689     return false; // Object is already in reference list
690
691   AddReferenceObject( theGroup, DataTag_GeometryGroup );
692   
693   // Indicate model of the need to update splitting
694   Changed( Geom_Groups );
695
696   return true;
697 }
698
699 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
700 {
701   return GetReferenceObjects( DataTag_GeometryGroup );
702 }
703
704 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
705 {
706   if ( theGroup.IsNull() )
707     return;
708
709   RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
710
711   // Indicate model of the need to update splitting
712   Changed( Geom_Groups );
713 }
714
715 void HYDROData_CalculationCase::RemoveGeometryGroups()
716 {
717   ClearReferenceObjects( DataTag_GeometryGroup );
718
719   // Indicate model of the need to update splitting
720   Changed( Geom_Groups );
721 }
722
723 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
724 {
725   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
726
727   SetReferenceObject( thePolyline, DataTag_Polyline );
728
729   // Indicate model of the need to update zones splitting
730   if( !IsEqual( aPrevPolyline, thePolyline ) )
731     Changed( Geom_2d );
732 }
733
734 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
735 {
736   return Handle(HYDROData_PolylineXY)::DownCast( 
737            GetReferenceObject( DataTag_Polyline ) );
738 }
739
740 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
741 {
742   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
743
744   ClearReferenceObjects( DataTag_Polyline );
745
746   // Indicate model of the need to update zones splitting
747   Changed( Geom_2d );
748 }
749
750 void HYDROData_CalculationCase::SetStricklerTable( const Handle(HYDROData_StricklerTable)& theStricklerTable )
751 {
752   Handle(HYDROData_StricklerTable) aPrevStricklerTable = GetStricklerTable();
753
754   SetReferenceObject( theStricklerTable, DataTag_StricklerTable );
755
756   // Indicate model of the need to update land covers partition
757   if( !IsEqual( aPrevStricklerTable, theStricklerTable ) )
758     Changed( Geom_No );
759 }
760
761 Handle(HYDROData_StricklerTable) HYDROData_CalculationCase::GetStricklerTable() const
762 {
763   return Handle(HYDROData_StricklerTable)::DownCast( 
764            GetReferenceObject( DataTag_StricklerTable ) );
765 }
766
767 void HYDROData_CalculationCase::RemoveStricklerTable()
768 {
769   Handle(HYDROData_StricklerTable) aPrevStricklerTable = GetStricklerTable();
770
771   ClearReferenceObjects( DataTag_StricklerTable );
772
773   // Indicate model of the need to update land covers partition
774   Changed( Geom_No );
775 }
776
777 Handle(HYDROData_LandCoverMap) HYDROData_CalculationCase::GetLandCoverMap() const
778 {
779   Handle(HYDROData_LandCoverMap) aMap = Handle(HYDROData_LandCoverMap)::DownCast(
780     GetReferenceObject( DataTag_LandCoverMap ) );
781   DEBTRACE("GetLandCoverMap " << aMap.IsNull());
782   return aMap;
783 }
784
785 void HYDROData_CalculationCase::SetLandCoverMap( const Handle(HYDROData_LandCoverMap)& theMap )
786 {
787   SetReferenceObject( theMap, DataTag_LandCoverMap );
788 }
789
790 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
791 {
792   Changed( Geom_No );
793   Changed( Geom_No );
794   Changed( Geom_No );
795   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
796   Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
797   if ( aNewRegion.IsNull() )
798     return aNewRegion;
799
800   aNewRegion->AddZone( theZone );
801
802   return aNewRegion;
803 }
804
805 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
806 {
807   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
808
809   if ( theRegion.IsNull() )
810     return false;
811   
812   HYDROData_CalculationCase::DataTag aDataTag = DataTag_Region;
813
814   if ( HasReference( theRegion, aDataTag ) )
815     return false; // Object is already in reference list
816
817   // Move the region from other calculation
818   Handle(HYDROData_CalculationCase) aFatherCalc = 
819     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
820   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
821   {
822     Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
823     theRegion->CopyTo( aNewRegion, true );
824
825     aFatherCalc->RemoveRegion( theRegion );
826
827     theRegion->SetLabel( aNewRegion->Label() );
828   }
829   else
830   {
831     AddReferenceObject( theRegion, aDataTag );
832   }
833
834   return true;
835 }
836
837 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
838 {
839   return GetReferenceObjects( DataTag_Region );
840 }
841
842 void HYDROData_CalculationCase::UpdateRegionsOrder()
843 {
844   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
845   if ( aDocument.IsNull() )
846     return;
847
848   QString aRegsPref = CALCULATION_REGIONS_PREF;
849   std::map<int, Handle(HYDROData_Region)> IndToRegion; //index in the name of region to region itself 
850   HYDROData_SequenceOfObjects aRegions = GetRegions();
851   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
852   QString aName;
853   int aVal;
854   for ( ; anIter.More(); anIter.Next() )
855   {
856     Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast( anIter.Value() );
857     if ( aRegion.IsNull() )
858       continue;
859
860     if (HYDROData_Tool::ExtractGeneratedObjectName(aRegion->GetName(), aVal, aName) && aName == aRegsPref)
861       IndToRegion[aVal] = aRegion;
862   }
863
864   int nbR = aRegions.Length();
865   std::map<int, Handle(HYDROData_Region)>::iterator it = IndToRegion.begin();
866   for( int i = 1; it != IndToRegion.end(); ++it )
867     if (it->first <= nbR + 1)
868     {
869       QString aNewName = QString("%1_%2").arg(aRegsPref).arg(QString::number(i));
870       it->second->SetName(aNewName);
871       i++;
872     }
873 }
874
875 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
876 {
877   if ( theRegion.IsNull() )
878     return;
879
880   HYDROData_CalculationCase::DataTag aDataTag = DataTag_Region;
881   RemoveReferenceObject( theRegion->Label(), aDataTag );
882
883   // Remove region from data model
884   Handle(HYDROData_CalculationCase) aFatherCalc = 
885     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
886   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
887     theRegion->Remove();
888 }
889
890 void HYDROData_CalculationCase::RemoveRegions()
891 {
892   myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
893 }
894
895 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplitGroups() const
896 {
897   return GetReferenceObjects( DataTag_SplitGroups );
898 }
899
900 void HYDROData_CalculationCase::RemoveSplitGroups()
901 {
902   myLab.FindChild( DataTag_SplitGroups ).ForgetAllAttributes();
903 }
904
905 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
906 {
907   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
908   return GetAltitudeForPoint( thePoint, aZone );
909 }
910
911 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                    thePoint,
912                                                        const Handle(HYDROData_Region)& theRegion,
913                                                        int theMethod) const
914 {
915   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
916
917   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
918   if ( !aZone.IsNull() )
919   {
920     DEBTRACE("GetAltitudeForPoint Region " << theRegion->GetName().toStdString() << " Zone " << aZone->GetName().toStdString());
921     Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
922     if ( IsEqual( aRefRegion, theRegion ) )
923       aResAltitude = GetAltitudeForPoint( thePoint, aZone, theMethod );
924     else
925       {
926         DEBTRACE("GetAltitudeForPoint Region " << aRefRegion->GetName().toStdString() << " Zone " << aZone->GetName().toStdString() << " ---------------------------");
927         aResAltitude = GetAltitudeForPoint( thePoint, aZone, theMethod );
928       }
929   }
930   else
931     {
932       DEBTRACE(" --- GetAltitudeForPoint No Zone ---");
933     }
934
935   return aResAltitude;
936 }
937
938 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                  thePoint,
939                                                        const Handle(HYDROData_Zone)& theZone,
940                                                        int theMethod) const
941 {
942   DEBTRACE("GetAltitudeForPoint Zone " << theZone->GetName().toStdString());
943   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
944   if ( theZone.IsNull() )
945   {
946         DEBTRACE("Zone nulle");
947     return aResAltitude;
948   }
949
950   HYDROData_Zone::MergeType aZoneMergeType = theZone->GetMergeType();
951   DEBTRACE("aZoneMergeType " << aZoneMergeType);
952   if ( !theZone->IsMergingNeed() )
953   {
954     aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
955     DEBTRACE("---");
956   }
957   else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
958   {
959         DEBTRACE("GetAltitudeForPoint Zone " << theZone->GetName().toStdString() << " Merge_UNKNOWN");
960     return aResAltitude;
961   }
962
963   HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
964   if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
965   {
966     Handle(HYDROData_IAltitudeObject) aMergeAltitude = 
967       Handle(HYDROData_IAltitudeObject)::DownCast( theZone->GetMergeObject() );
968     if ( !aMergeAltitude.IsNull() )
969     {
970       if ( aZoneInterpolator != NULL )
971       {
972         DEBTRACE("aZoneInterpolator != NULL");
973         aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
974         aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
975       }
976       else
977       {
978         DEBTRACE("aZoneInterpolator == NULL");
979         aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
980       }
981     }
982   }
983   else
984   {
985         DEBTRACE("aZoneMergeType != HYDROData_Zone::Merge_Object");
986     HYDROData_SequenceOfObjects aZoneObjects = theZone->GetObjects();
987     HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
988     for ( ; anIter.More(); anIter.Next() )
989     {
990       Handle(HYDROData_Object) aZoneObj =
991         Handle(HYDROData_Object)::DownCast( anIter.Value() );
992       if ( aZoneObj.IsNull() )
993         continue;
994
995       Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
996       if ( anObjAltitude.IsNull() )
997         continue;
998
999       double aPointAltitude = 0.0;
1000       if ( aZoneInterpolator != NULL )
1001       {
1002         DEBTRACE("aZoneInterpolator != NULL");
1003         aZoneInterpolator->SetAltitudeObject( anObjAltitude );
1004         aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
1005       }
1006       else
1007       {
1008         DEBTRACE("aZoneInterpolator == NULL");
1009         aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint, theMethod );
1010       }
1011
1012       if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
1013         continue;
1014
1015       if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
1016       {
1017         aResAltitude = aPointAltitude;
1018         break;
1019       }
1020       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
1021       {
1022         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
1023              aResAltitude > aPointAltitude )
1024         {
1025           aResAltitude = aPointAltitude;
1026         }
1027       }
1028       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
1029       {
1030         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
1031              aResAltitude < aPointAltitude )
1032         {
1033           aResAltitude = aPointAltitude;
1034         }
1035       }
1036     }
1037   }
1038
1039   return aResAltitude;
1040 }
1041
1042 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
1043   const NCollection_Sequence<gp_XY>& thePoints,
1044   const Handle(HYDROData_Region)&    theRegion,
1045   int theMethod) const
1046 {
1047   DEBTRACE("HYDROData_CalculationCase::GetAltitudesForPoints " << theRegion->GetName().toStdString());
1048   NCollection_Sequence<double> aResSeq;
1049
1050   Handle(Message_ProgressIndicator) aZIProgress = HYDROData_Tool::GetZIProgress();
1051   if ( aZIProgress ) {
1052     aZIProgress->Reset();
1053   }
1054
1055   QFuture<void> aFuture = QtConcurrent::run([&]() {
1056     int aNbPoints = thePoints.Length();
1057
1058     Message_ProgressSentry aPSentry(HYDROData_Tool::GetZIProgress(), "GetAltitudesForPoints", 0, aNbPoints, 1);
1059     for ( int i = 1, n = aNbPoints; i <= n && aPSentry.More(); ++i, aPSentry.Next() )
1060     {
1061       const gp_XY& thePnt = thePoints.Value( i );
1062     
1063       double anAltitude = GetAltitudeForPoint( thePnt, theRegion, theMethod );
1064       aResSeq.Append( anAltitude );
1065     }
1066   });
1067
1068   while( aFuture.isRunning() ) {
1069     if ( aZIProgress ) {
1070       aZIProgress->Show( Standard_True );
1071       QThread::usleep(500);
1072     }
1073   }
1074   aZIProgress->Show( Standard_True );
1075
1076   return aResSeq;
1077 }
1078
1079 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
1080   const NCollection_Sequence<gp_XY>& thePoints,
1081   const Handle(HYDROData_Zone)&      theZone,
1082   int theMethod) const
1083 {
1084   NCollection_Sequence<double> aResSeq;
1085
1086   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1087   {
1088     const gp_XY& thePnt = thePoints.Value( i );
1089     
1090     double anAltitude = GetAltitudeForPoint( thePnt, theZone, theMethod );
1091     aResSeq.Append( anAltitude );
1092   }
1093
1094   return aResSeq;
1095 }
1096
1097 double HYDROData_CalculationCase::GetStricklerCoefficientForPoint( const gp_XY& thePoint ) const
1098 {
1099   Handle( HYDROData_LandCoverMap ) aMap = GetLandCoverMap();
1100   Handle( HYDROData_StricklerTable ) aTable = GetStricklerTable();
1101   if( aMap.IsNull() )
1102     return 0.0;
1103
1104   QString aType;
1105   aMap->FindByPoint( thePoint, aType );
1106   double aCoeff = aTable->Get( aType, 0.0 );
1107   return aCoeff;
1108 }
1109
1110 std::vector<double> HYDROData_CalculationCase::GetStricklerCoefficientForPoints(const std::vector<gp_XY>& thePoints,
1111   double DefValue, bool UseMax ) const
1112 {
1113   DEBTRACE("GetStricklerCoefficientForPoints");
1114   Handle( HYDROData_LandCoverMap ) aLCM = GetLandCoverMap();
1115   Handle( HYDROData_StricklerTable ) aTable = GetStricklerTable();
1116   std::vector<double> theCoeffs;
1117   DEBTRACE("aLCM.IsNull() " << aLCM.IsNull());
1118   DEBTRACE("aTable.IsNull() "<< aTable.IsNull());
1119   if( aLCM.IsNull() || aTable.IsNull() )
1120     return theCoeffs;
1121
1122   Handle(Message_ProgressIndicator) aSIProgress = HYDROData_Tool::GetSIProgress();
1123   if ( aSIProgress ) {
1124     aSIProgress->Reset();
1125   }
1126
1127   QFuture<void> aFuture = QtConcurrent::run([&]() {
1128     aLCM->ClassifyPoints(thePoints, aTable, theCoeffs, DefValue, UseMax );
1129   });
1130
1131   while( aFuture.isRunning() ) {
1132     if ( aSIProgress ) {
1133       aSIProgress->Show( Standard_True );
1134       QThread::usleep(500);
1135     }
1136   }
1137
1138   return theCoeffs;
1139 }
1140
1141 std::vector<int> HYDROData_CalculationCase::GetStricklerTypeForPoints( const std::vector<gp_XY>& thePoints ) const
1142 {
1143   Handle( HYDROData_LandCoverMap ) aLCM = GetLandCoverMap();
1144   Handle( HYDROData_StricklerTable ) aTable = GetStricklerTable();
1145   std::vector<int> types;
1146   if( aLCM.IsNull() || aTable.IsNull() )
1147     return types;
1148
1149   aLCM->ClassifyPoints(thePoints, aTable, types );
1150   return types;
1151 }
1152
1153 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
1154 {
1155   Handle(HYDROData_Region) aResRegion;
1156
1157   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
1158   if ( !aZone.IsNull() )
1159     aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
1160
1161   return aResRegion;
1162 }
1163
1164 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
1165 {
1166   Handle(HYDROData_Zone) aResZone;
1167
1168   HYDROData_SequenceOfObjects aRegions = GetRegions();
1169
1170   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
1171   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
1172   {
1173     Handle(HYDROData_Region) aRegion =
1174       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1175     if ( aRegion.IsNull() )
1176       continue;
1177
1178     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
1179     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1180     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
1181     {
1182       Handle(HYDROData_Zone) aRegZone =
1183         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
1184       if ( aRegZone.IsNull() )
1185         continue;
1186
1187       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
1188       if ( aPointRelation != POINT_OUT )
1189         aResZone = aRegZone; // We found the desired zone
1190     }
1191   }
1192
1193   return aResZone;
1194 }
1195
1196 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
1197   const gp_XY&                  thePoint,
1198   const Handle(HYDROData_Zone)& theZone ) const
1199 {
1200   PointClassification aRes = POINT_OUT;
1201   if ( theZone.IsNull() )
1202     return aRes;
1203
1204   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
1205   if ( aZoneFace.IsNull() )
1206     return aRes;
1207 #ifdef DEB_CLASS2D      
1208           TopoDS_Compound aCmp;
1209       BRep_Builder aBB;
1210       aBB.MakeCompound(aCmp);
1211           aBB.Add(aCmp, aZoneFace);
1212           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
1213           BRepBuilderAPI_MakeVertex aMk(aPnt);
1214           aBB.Add(aCmp, aMk.Vertex());
1215           BRepTools::Write(aCmp, "FCL2d.brep");
1216 #endif  
1217   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
1218   if (State == TopAbs_OUT)
1219     aRes =  POINT_OUT;
1220   else if(State == TopAbs_IN)
1221     aRes =  POINT_IN;
1222   else if(State == TopAbs_ON)
1223     aRes =  POINT_ON;
1224   return aRes;
1225 }
1226
1227 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
1228                                                                   const QString& thePrefixOrName,
1229                                                                   bool isPrefix )
1230 {
1231   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
1232   int aTag = aNewLab.Tag();
1233
1234   Handle(HYDROData_Region) aNewRegion =
1235     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
1236   AddRegion( aNewRegion );
1237
1238   QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
1239   aNewRegion->SetName( aRegionName, true );
1240
1241   return aNewRegion;
1242 }
1243
1244 Handle(HYDROData_SplitShapesGroup) HYDROData_CalculationCase::addNewSplitGroup( const QString& theName )
1245 {
1246   TDF_Label aNewLab = myLab.FindChild( DataTag_SplitGroups ).NewChild();
1247
1248   Handle(HYDROData_SplitShapesGroup) aNewGroup =
1249     Handle(HYDROData_SplitShapesGroup)::DownCast( 
1250       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLIT_GROUP ) );
1251   AddReferenceObject( aNewGroup, DataTag_SplitGroups );
1252
1253   aNewGroup->SetName( theName );
1254
1255   return aNewGroup;
1256 }
1257
1258 QString HYDROData_CalculationCase::Export( int theStudyId ) const
1259 {
1260 #ifdef LIGHT_MODE
1261   return "";
1262 #else
1263   GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
1264   SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
1265   
1266   QString aGeomObjEntry, anErrorMsg;
1267   QString statMess;
1268   bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry, anErrorMsg, statMess );
1269   return isOK ? aGeomObjEntry : QString();
1270 #endif
1271 }
1272
1273 #ifndef LIGHT_MODE
1274 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
1275                                         SALOMEDS::Study_ptr theStudy,
1276                                         QString& theGeomObjEntry,
1277                                         QString& theErrorMsg,
1278                                         QString& statMess) const
1279 {
1280   DEBTRACE("Export");
1281   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
1282
1283   // Get groups definitions
1284   HYDROData_SequenceOfObjects aSplitGroups = GetSplitGroups();
1285
1286   TopTools_SequenceOfShape IntSh; //internal edges
1287   HYDROData_SequenceOfObjects::Iterator anIter( aSplitGroups );
1288   for ( ; anIter.More(); anIter.Next() )
1289   {
1290     // Get shapes group
1291     Handle(HYDROData_ShapesGroup) aGroup =
1292       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
1293     if ( aGroup.IsNull() )
1294       continue;
1295
1296     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
1297
1298     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
1299     aGroup->GetShapes( aGroupDef.Shapes );
1300
1301     aSeqOfGroupsDefs.Append( aGroupDef );
1302
1303     Handle(HYDROData_SplitShapesGroup) aSSGroup = Handle(HYDROData_SplitShapesGroup)::DownCast( anIter.Value() );
1304     TopTools_SequenceOfShape dummy;
1305     if (!aSSGroup.IsNull())
1306       if (aSSGroup->GetInternal())
1307       {
1308         aSSGroup->GetShapes(dummy);
1309         IntSh.Append(dummy);
1310       }
1311
1312   }
1313   
1314   // Get faces
1315   bool isAllNotSubmersible = true;
1316   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
1317   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
1318   NCollection_IndexedDataMap<TopoDS_Shape, QString> aShToNames;
1319   for ( ; aRegionIter.More(); aRegionIter.Next() )
1320   {
1321     Handle(HYDROData_Region) aRegion =
1322       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
1323     if( aRegion.IsNull() || !aRegion->IsSubmersible() )
1324       continue;
1325     
1326     if ( isAllNotSubmersible )
1327       isAllNotSubmersible = false;
1328
1329     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs, &IntSh );
1330     aShToNames.Add( aRegionShape, aRegion->GetName() );
1331   }
1332
1333   bool aRes = false;
1334
1335   if ( aCaseRegions.IsEmpty() ) {
1336     theErrorMsg = QString("the list of regions is empty.");
1337   } else if ( isAllNotSubmersible ) {
1338     theErrorMsg = QString("there are no submersible regions.");
1339   } else {
1340     aRes = Export( theGeomEngine, theStudy, aShToNames, aSeqOfGroupsDefs, theGeomObjEntry );;
1341   }
1342
1343   if( aRes && !GetLandCoverMap().IsNull() && !GetStricklerTable().IsNull() )
1344   {
1345     QString aTelemacFileName = GetName() + ".telemac";
1346     aRes = GetLandCoverMap()->ExportTelemac( aTelemacFileName, 1E-2, GetStricklerTable(), statMess );
1347     if (!aRes)
1348       theErrorMsg = QString( "The export to TELEMAC %1 failed" ).arg( aTelemacFileName );
1349   }
1350   return aRes;
1351 }
1352
1353 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
1354                                         SALOMEDS::Study_ptr                           theStudy,
1355                                         const NCollection_IndexedDataMap<TopoDS_Shape, QString>& aShToName,
1356                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
1357                                         QString& theGeomObjEntry ) const
1358 {
1359   // Sew faces
1360   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
1361   aSewing.SetNonManifoldMode( Standard_False );
1362 #ifdef DEB_CALCULATION
1363   TCollection_AsciiString aNam("Sh_");
1364   int i=1;
1365 #endif
1366   TopTools_DataMapOfShapeListOfShape SH2M;
1367   for ( int i = 1; i <= aShToName.Extent(); i++ )
1368   {
1369     const TopoDS_Shape& aShape = aShToName.FindKey(i);
1370     if ( aShape.IsNull() )
1371       continue;
1372
1373     SH2M.Bind(aShape, TopTools_ListOfShape());
1374     TopTools_ListOfShape& LM = SH2M.ChangeFind(aShape);
1375     if ( aShape.ShapeType() == TopAbs_FACE || aShape.ShapeType() == TopAbs_SHELL )
1376     {
1377       aSewing.Add( aShape );
1378       LM.Append(aShape);
1379     }
1380     else if (aShape.ShapeType() == TopAbs_COMPOUND)
1381     {
1382       TopExp_Explorer anExp( aShape, TopAbs_SHELL );
1383       for (; anExp.More(); anExp.Next() )
1384       {
1385         aSewing.Add( anExp.Current() );
1386         LM.Append(anExp.Current());
1387       }
1388       anExp.Init( aShape, TopAbs_FACE, TopAbs_SHELL );
1389       for (; anExp.More(); anExp.Next() )
1390       {
1391         aSewing.Add( anExp.Current() );
1392         LM.Append(anExp.Current());
1393       }
1394     }
1395   } // faces iterator
1396   
1397   aSewing.Perform();
1398   TopoDS_Shape aSewedShape = aSewing.SewedShape();
1399
1400   NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher> aFacesToNameModif;
1401
1402   for ( int i = 1; i <= aShToName.Extent(); i++ )
1403   {
1404     const TopoDS_Shape& CurShape = aShToName.FindKey(i);
1405     const QString& Qstr = aShToName.FindFromIndex(i);
1406     const TopTools_ListOfShape& LM = SH2M(CurShape);
1407     for (TopTools_ListIteratorOfListOfShape it(LM); it.More(); it.Next())
1408     {
1409       const TopoDS_Shape& csh = it.Value();
1410       if (aSewing.IsModified(csh))
1411         aFacesToNameModif.Add(aSewing.Modified(csh), Qstr);
1412       else
1413         aFacesToNameModif.Add(csh, Qstr);
1414     }
1415   }
1416
1417  
1418   // If the sewed shape is empty - return false
1419   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1420     return false;
1421
1422 #ifdef DEB_CALCULATION
1423   BRepTools::Write(aSewedShape ,"Sew.brep");
1424 #endif  // Publish the sewed shape
1425   QString aName = EXPORT_NAME;
1426   GEOM::GEOM_Object_ptr aMainShape = 
1427     HYDROData_GeomTool::ExplodeShapeInGEOMandPublish( theGeomEngine, theStudy, aSewedShape, aFacesToNameModif, aName, theGeomObjEntry );
1428
1429   if ( aMainShape->_is_nil() )  
1430     return false;
1431
1432   if ( theGroupsDefs.IsEmpty() )
1433     return true;
1434
1435   // Create groups
1436   TopTools_IndexedMapOfShape aMapOfSubShapes;
1437   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1438
1439   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1440
1441   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1442   {
1443     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1444
1445     NCollection_Sequence<int> aGroupIndexes;
1446     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1447     {
1448       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1449 #ifdef DEB_CALCULATION
1450       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1451 #endif
1452       
1453       TopoDS_Shape aModifiedShape = aShape;
1454       if ( aSewing.IsModified( aShape ) )
1455         aModifiedShape = aSewing.Modified( aShape );
1456       else if ( aSewing.IsModifiedSubShape( aShape ) )
1457         aModifiedShape = aSewing.ModifiedSubShape( aShape );
1458
1459 #ifdef DEB_CALCULATION
1460       const TopLoc_Location& aL1 = aShape.Location();
1461       const TopLoc_Location& aL2 = aModifiedShape.Location();
1462       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1463 #endif
1464
1465       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1466       if ( anIndex > 0 ) {
1467         aGroupIndexes.Append( anIndex );
1468       } else {
1469 #ifdef DEB_CALCULATION    
1470         TCollection_AsciiString aNam("Lost_");
1471         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1472         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1473         {
1474            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1475            if ( aModifiedShape.IsPartner( aS ) )
1476            {
1477              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1478              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1479              BRepTools::Write(aS ,aName.ToCString());
1480             break;
1481            }
1482          }
1483         }
1484 #endif
1485       }
1486     }
1487     if ( !aGroupIndexes.IsEmpty() )
1488       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1489   }
1490  
1491   if ( !aGroupsData.IsEmpty() )
1492   {
1493     GEOM::GEOM_IGroupOperations_var aGroupOp = 
1494       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
1495
1496     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1497     for ( ; aMapIt.More(); aMapIt.Next() )
1498     {
1499       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
1500       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1501
1502       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1503       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1504         continue;
1505
1506       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1507       aGeomIndexes->length( aGroupIndexes.Length() );
1508
1509       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1510         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1511
1512       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1513       if ( aGroupOp->IsDone() )
1514       {
1515         SALOMEDS::SObject_var aGroupSO = 
1516           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1517       }
1518     }
1519   }
1520
1521   return true;
1522 }
1523 #endif
1524
1525 void HYDROData_CalculationCase::ClearRules( HYDROData_CalculationCase::DataTag theDataTag,
1526                                             const bool theIsSetToUpdate )
1527 {
1528   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1529   HYDROData_PriorityQueue::ClearRules( aRulesLab );
1530
1531   // Indicate model of the need to update splitting
1532   if ( theIsSetToUpdate ) {
1533     Changed( Geom_2d );
1534   }
1535 }
1536
1537 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Entity)&    theObject1,
1538                                          HYDROData_PriorityType             thePriority,
1539                                          const Handle(HYDROData_Entity)&    theObject2,
1540                                          HYDROData_Zone::MergeType          theMergeType,
1541                                          HYDROData_CalculationCase::DataTag theDataTag )
1542 {
1543   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1544   HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1545
1546   // Indicate model of the need to update splitting
1547   Changed( Geom_2d );
1548 }
1549
1550 QString HYDROData_CalculationCase::DumpRules() const
1551 {
1552   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1553   return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1554 }
1555
1556 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1557 {
1558   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1559   Handle(TDataStd_Integer) anAttr = TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1560   anAttr->SetID(TDataStd_Integer::GetID());
1561   // Indicate model of the need to update splitting
1562   Changed( Geom_2d );
1563 }
1564
1565 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1566 {
1567   Handle(TDataStd_Integer) aModeAttr;
1568   bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1569   if( isOK )
1570     return ( AssignmentMode ) aModeAttr->Get();
1571   else
1572     return MANUAL;
1573 }
1574
1575 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1576                                                    QStringList& theScript ) const
1577 {
1578   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1579   HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1580 }
1581
1582 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1583 {
1584   return myLastWarning;
1585 }
1586
1587 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1588 {
1589   myLastWarning.Type = theType;
1590   myLastWarning.Data = theData;
1591 }
1592
1593 void HYDROData_CalculationCase::UpdateRegionsNames( const HYDROData_SequenceOfObjects& theRegions,
1594                                                     const QString& theOldCaseName,
1595                                                     const QString& theName )
1596 {
1597   HYDROData_SequenceOfObjects::Iterator anIter( theRegions );
1598   for ( ; anIter.More(); anIter.Next() )
1599   {
1600     Handle(HYDROData_Region) aRegion =
1601       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1602     if ( aRegion.IsNull() )
1603       continue;
1604
1605     HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegion );
1606
1607     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
1608     HYDROData_SequenceOfObjects::Iterator anIter( aZones );
1609     for ( ; anIter.More(); anIter.Next() )
1610     {
1611       Handle(HYDROData_Zone) aRegZone =
1612         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1613       if ( aRegZone.IsNull() )
1614         continue;
1615
1616       HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegZone );
1617     }
1618   }
1619 }
1620
1621 void HYDROData_CalculationCase::DumpRegionsToPython( QStringList& theResList,
1622                                                      const QString& thePyScriptPath,
1623                                                      MapOfTreatedObjects& theTreatedObjects,
1624                                                      const HYDROData_SequenceOfObjects& theRegions ) const
1625 {
1626   HYDROData_SequenceOfObjects::Iterator anIter;
1627   anIter.Init(theRegions);
1628   for (int ireg = 1; anIter.More(); anIter.Next(), ireg++)
1629     {
1630       Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast(anIter.Value());
1631       if (aRegion.IsNull())
1632         continue;
1633       QString defRegName = this->GetName();
1634       QString regSuffix = QString("_Reg_%1").arg(ireg);
1635       defRegName += regSuffix;
1636       theTreatedObjects.insert(aRegion->GetName(), aRegion);
1637       QStringList aRegDump = aRegion->DumpToPython(thePyScriptPath, theTreatedObjects, defRegName);
1638       theResList << aRegDump;
1639     }
1640   for (anIter.Init(theRegions); anIter.More(); anIter.Next())
1641     {
1642       Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast(anIter.Value());
1643       if (aRegion.IsNull())
1644         continue;
1645       QStringList aRegDump;
1646       aRegion->SetNameInDumpPython(aRegDump);
1647       theResList << aRegDump;
1648     }
1649 }
1650
1651 bool HYDROData_CalculationCase::GetRule( int theIndex, 
1652                                          Handle(HYDROData_Entity)&           theObject1,
1653                                          HYDROData_PriorityType&             thePriority,
1654                                          Handle(HYDROData_Entity)&           theObject2,
1655                                          HYDROData_Zone::MergeType&          theMergeType,
1656                                          HYDROData_CalculationCase::DataTag& theDataTag) const
1657 {
1658   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1659   return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1660     theObject1, thePriority, theObject2, theMergeType );
1661 }
1662
1663 bool HYDROData_CalculationCase::AddInterPoly( const Handle(HYDROData_PolylineXY)& theInterPolyline )
1664 {
1665   HYDROData_CalculationCase::DataTag aDataTag = DataTag_InterPoly;
1666
1667   if ( HasReference( theInterPolyline, aDataTag ) )
1668     return false; 
1669
1670   AddReferenceObject( theInterPolyline, aDataTag );
1671
1672   Changed( Geom_2d );
1673
1674   return true;
1675 }
1676
1677 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetInterPolyObjects() const
1678 {
1679   return GetReferenceObjects( DataTag_InterPoly ); 
1680 }
1681
1682 void HYDROData_CalculationCase::RemoveInterPolyObject( const Handle(HYDROData_PolylineXY)& theInterPolyline ) 
1683 {
1684   if ( theInterPolyline.IsNull() )
1685     return;
1686
1687   RemoveReferenceObject( theInterPolyline->Label(), DataTag_InterPoly );
1688
1689   Changed( Geom_2d );
1690 }
1691
1692 bool HYDROData_CalculationCase::AddBoundaryPolygon( const Handle(HYDROData_BCPolygon)& theBCPolygon )
1693 {
1694   HYDROData_CalculationCase::DataTag aDataTag = DataTag_BCPolygon;
1695
1696   if ( HasReference( theBCPolygon, aDataTag ) )
1697     return false; 
1698
1699   AddReferenceObject( theBCPolygon, aDataTag );
1700
1701   Changed( Geom_2d );
1702
1703   return true;
1704 }
1705
1706 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetBoundaryPolygons() const
1707 {
1708   return GetReferenceObjects( DataTag_BCPolygon ); 
1709 }
1710
1711 void HYDROData_CalculationCase::RemoveBoundaryPolygon( const Handle(HYDROData_BCPolygon)& theBCPolygon ) 
1712 {
1713   if ( theBCPolygon.IsNull() )
1714     return;
1715
1716   RemoveReferenceObject( theBCPolygon->Label(), DataTag_BCPolygon );
1717
1718   Changed( Geom_2d );
1719 }
1720
1721
1722
1723