Salome HOME
debug, dialogs not availables when HYDRO module interface is not loaded
[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   //DEBTRACE("aZIProgress=" << aZIProgress);
1052   if ( aZIProgress ) {
1053     aZIProgress->Reset();
1054   }
1055
1056   QFuture<void> aFuture = QtConcurrent::run([&]() {
1057     int aNbPoints = thePoints.Length();
1058
1059     Message_ProgressSentry aPSentry(HYDROData_Tool::GetZIProgress(), "GetAltitudesForPoints", 0, aNbPoints, 1);
1060     for ( int i = 1, n = aNbPoints; i <= n && aPSentry.More(); ++i, aPSentry.Next() )
1061     {
1062       const gp_XY& thePnt = thePoints.Value( i );
1063     
1064       double anAltitude = GetAltitudeForPoint( thePnt, theRegion, theMethod );
1065       aResSeq.Append( anAltitude );
1066     }
1067   });
1068
1069   while( aFuture.isRunning() ) {
1070     if ( aZIProgress ) {
1071       aZIProgress->Show( Standard_True );
1072       QThread::usleep(500);
1073     }
1074   }
1075   //DEBTRACE("aZIProgress=" << aZIProgress);
1076   if ( aZIProgress )
1077     aZIProgress->Show( Standard_True );
1078
1079   return aResSeq;
1080 }
1081
1082 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
1083   const NCollection_Sequence<gp_XY>& thePoints,
1084   const Handle(HYDROData_Zone)&      theZone,
1085   int theMethod) const
1086 {
1087   NCollection_Sequence<double> aResSeq;
1088
1089   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1090   {
1091     const gp_XY& thePnt = thePoints.Value( i );
1092     
1093     double anAltitude = GetAltitudeForPoint( thePnt, theZone, theMethod );
1094     aResSeq.Append( anAltitude );
1095   }
1096
1097   return aResSeq;
1098 }
1099
1100 double HYDROData_CalculationCase::GetStricklerCoefficientForPoint( const gp_XY& thePoint ) const
1101 {
1102   Handle( HYDROData_LandCoverMap ) aMap = GetLandCoverMap();
1103   Handle( HYDROData_StricklerTable ) aTable = GetStricklerTable();
1104   if( aMap.IsNull() )
1105     return 0.0;
1106
1107   QString aType;
1108   aMap->FindByPoint( thePoint, aType );
1109   double aCoeff = aTable->Get( aType, 0.0 );
1110   return aCoeff;
1111 }
1112
1113 std::vector<double> HYDROData_CalculationCase::GetStricklerCoefficientForPoints(const std::vector<gp_XY>& thePoints,
1114   double DefValue, bool UseMax ) const
1115 {
1116   DEBTRACE("GetStricklerCoefficientForPoints");
1117   Handle( HYDROData_LandCoverMap ) aLCM = GetLandCoverMap();
1118   Handle( HYDROData_StricklerTable ) aTable = GetStricklerTable();
1119   std::vector<double> theCoeffs;
1120   DEBTRACE("aLCM.IsNull() " << aLCM.IsNull());
1121   DEBTRACE("aTable.IsNull() "<< aTable.IsNull());
1122   if( aLCM.IsNull() || aTable.IsNull() )
1123     return theCoeffs;
1124
1125   Handle(Message_ProgressIndicator) aSIProgress = HYDROData_Tool::GetSIProgress();
1126   if ( aSIProgress ) {
1127     aSIProgress->Reset();
1128   }
1129
1130   QFuture<void> aFuture = QtConcurrent::run([&]() {
1131     aLCM->ClassifyPoints(thePoints, aTable, theCoeffs, DefValue, UseMax );
1132   });
1133
1134   while( aFuture.isRunning() ) {
1135     if ( aSIProgress ) {
1136       aSIProgress->Show( Standard_True );
1137       QThread::usleep(500);
1138     }
1139   }
1140
1141   return theCoeffs;
1142 }
1143
1144 std::vector<int> HYDROData_CalculationCase::GetStricklerTypeForPoints( const std::vector<gp_XY>& thePoints ) const
1145 {
1146   Handle( HYDROData_LandCoverMap ) aLCM = GetLandCoverMap();
1147   Handle( HYDROData_StricklerTable ) aTable = GetStricklerTable();
1148   std::vector<int> types;
1149   if( aLCM.IsNull() || aTable.IsNull() )
1150     return types;
1151
1152   aLCM->ClassifyPoints(thePoints, aTable, types );
1153   return types;
1154 }
1155
1156 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
1157 {
1158   Handle(HYDROData_Region) aResRegion;
1159
1160   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
1161   if ( !aZone.IsNull() )
1162     aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
1163
1164   return aResRegion;
1165 }
1166
1167 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
1168 {
1169   Handle(HYDROData_Zone) aResZone;
1170
1171   HYDROData_SequenceOfObjects aRegions = GetRegions();
1172
1173   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
1174   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
1175   {
1176     Handle(HYDROData_Region) aRegion =
1177       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1178     if ( aRegion.IsNull() )
1179       continue;
1180
1181     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
1182     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
1183     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
1184     {
1185       Handle(HYDROData_Zone) aRegZone =
1186         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
1187       if ( aRegZone.IsNull() )
1188         continue;
1189
1190       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
1191       if ( aPointRelation != POINT_OUT )
1192         aResZone = aRegZone; // We found the desired zone
1193     }
1194   }
1195
1196   return aResZone;
1197 }
1198
1199 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
1200   const gp_XY&                  thePoint,
1201   const Handle(HYDROData_Zone)& theZone ) const
1202 {
1203   PointClassification aRes = POINT_OUT;
1204   if ( theZone.IsNull() )
1205     return aRes;
1206
1207   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
1208   if ( aZoneFace.IsNull() )
1209     return aRes;
1210 #ifdef DEB_CLASS2D      
1211           TopoDS_Compound aCmp;
1212       BRep_Builder aBB;
1213       aBB.MakeCompound(aCmp);
1214           aBB.Add(aCmp, aZoneFace);
1215           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
1216           BRepBuilderAPI_MakeVertex aMk(aPnt);
1217           aBB.Add(aCmp, aMk.Vertex());
1218           BRepTools::Write(aCmp, "FCL2d.brep");
1219 #endif  
1220   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
1221   if (State == TopAbs_OUT)
1222     aRes =  POINT_OUT;
1223   else if(State == TopAbs_IN)
1224     aRes =  POINT_IN;
1225   else if(State == TopAbs_ON)
1226     aRes =  POINT_ON;
1227   return aRes;
1228 }
1229
1230 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
1231                                                                   const QString& thePrefixOrName,
1232                                                                   bool isPrefix )
1233 {
1234   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
1235   int aTag = aNewLab.Tag();
1236
1237   Handle(HYDROData_Region) aNewRegion =
1238     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
1239   AddRegion( aNewRegion );
1240
1241   QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
1242   aNewRegion->SetName( aRegionName, true );
1243
1244   return aNewRegion;
1245 }
1246
1247 Handle(HYDROData_SplitShapesGroup) HYDROData_CalculationCase::addNewSplitGroup( const QString& theName )
1248 {
1249   TDF_Label aNewLab = myLab.FindChild( DataTag_SplitGroups ).NewChild();
1250
1251   Handle(HYDROData_SplitShapesGroup) aNewGroup =
1252     Handle(HYDROData_SplitShapesGroup)::DownCast( 
1253       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLIT_GROUP ) );
1254   AddReferenceObject( aNewGroup, DataTag_SplitGroups );
1255
1256   aNewGroup->SetName( theName );
1257
1258   return aNewGroup;
1259 }
1260
1261 QString HYDROData_CalculationCase::Export( int theStudyId ) const
1262 {
1263 #ifdef LIGHT_MODE
1264   return "";
1265 #else
1266   GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
1267   SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
1268   
1269   QString aGeomObjEntry, anErrorMsg;
1270   QString statMess;
1271   bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry, anErrorMsg, statMess );
1272   return isOK ? aGeomObjEntry : QString();
1273 #endif
1274 }
1275
1276 #ifndef LIGHT_MODE
1277 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
1278                                         SALOMEDS::Study_ptr theStudy,
1279                                         QString& theGeomObjEntry,
1280                                         QString& theErrorMsg,
1281                                         QString& statMess) const
1282 {
1283   DEBTRACE("Export");
1284   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
1285
1286   // Get groups definitions
1287   HYDROData_SequenceOfObjects aSplitGroups = GetSplitGroups();
1288
1289   TopTools_SequenceOfShape IntSh; //internal edges
1290   HYDROData_SequenceOfObjects::Iterator anIter( aSplitGroups );
1291   for ( ; anIter.More(); anIter.Next() )
1292   {
1293     // Get shapes group
1294     Handle(HYDROData_ShapesGroup) aGroup =
1295       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
1296     if ( aGroup.IsNull() )
1297       continue;
1298
1299     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
1300
1301     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
1302     aGroup->GetShapes( aGroupDef.Shapes );
1303
1304     aSeqOfGroupsDefs.Append( aGroupDef );
1305
1306     Handle(HYDROData_SplitShapesGroup) aSSGroup = Handle(HYDROData_SplitShapesGroup)::DownCast( anIter.Value() );
1307     TopTools_SequenceOfShape dummy;
1308     if (!aSSGroup.IsNull())
1309       if (aSSGroup->GetInternal())
1310       {
1311         aSSGroup->GetShapes(dummy);
1312         IntSh.Append(dummy);
1313       }
1314
1315   }
1316   
1317   // Get faces
1318   bool isAllNotSubmersible = true;
1319   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
1320   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
1321   NCollection_IndexedDataMap<TopoDS_Shape, QString> aShToNames;
1322   for ( ; aRegionIter.More(); aRegionIter.Next() )
1323   {
1324     Handle(HYDROData_Region) aRegion =
1325       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
1326     if( aRegion.IsNull() || !aRegion->IsSubmersible() )
1327       continue;
1328     
1329     if ( isAllNotSubmersible )
1330       isAllNotSubmersible = false;
1331
1332     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs, &IntSh );
1333     aShToNames.Add( aRegionShape, aRegion->GetName() );
1334   }
1335
1336   bool aRes = false;
1337
1338   if ( aCaseRegions.IsEmpty() ) {
1339     theErrorMsg = QString("the list of regions is empty.");
1340   } else if ( isAllNotSubmersible ) {
1341     theErrorMsg = QString("there are no submersible regions.");
1342   } else {
1343     aRes = Export( theGeomEngine, theStudy, aShToNames, aSeqOfGroupsDefs, theGeomObjEntry );;
1344   }
1345
1346   if( aRes && !GetLandCoverMap().IsNull() && !GetStricklerTable().IsNull() )
1347   {
1348     QString aTelemacFileName = GetName() + ".telemac";
1349     aRes = GetLandCoverMap()->ExportTelemac( aTelemacFileName, 1E-2, GetStricklerTable(), statMess );
1350     if (!aRes)
1351       theErrorMsg = QString( "The export to TELEMAC %1 failed" ).arg( aTelemacFileName );
1352   }
1353   return aRes;
1354 }
1355
1356 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
1357                                         SALOMEDS::Study_ptr                           theStudy,
1358                                         const NCollection_IndexedDataMap<TopoDS_Shape, QString>& aShToName,
1359                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
1360                                         QString& theGeomObjEntry ) const
1361 {
1362   // Sew faces
1363   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
1364   aSewing.SetNonManifoldMode( Standard_False );
1365 #ifdef DEB_CALCULATION
1366   TCollection_AsciiString aNam("Sh_");
1367   int i=1;
1368 #endif
1369   TopTools_DataMapOfShapeListOfShape SH2M;
1370   for ( int i = 1; i <= aShToName.Extent(); i++ )
1371   {
1372     const TopoDS_Shape& aShape = aShToName.FindKey(i);
1373     if ( aShape.IsNull() )
1374       continue;
1375
1376     SH2M.Bind(aShape, TopTools_ListOfShape());
1377     TopTools_ListOfShape& LM = SH2M.ChangeFind(aShape);
1378     if ( aShape.ShapeType() == TopAbs_FACE || aShape.ShapeType() == TopAbs_SHELL )
1379     {
1380       aSewing.Add( aShape );
1381       LM.Append(aShape);
1382     }
1383     else if (aShape.ShapeType() == TopAbs_COMPOUND)
1384     {
1385       TopExp_Explorer anExp( aShape, TopAbs_SHELL );
1386       for (; anExp.More(); anExp.Next() )
1387       {
1388         aSewing.Add( anExp.Current() );
1389         LM.Append(anExp.Current());
1390       }
1391       anExp.Init( aShape, TopAbs_FACE, TopAbs_SHELL );
1392       for (; anExp.More(); anExp.Next() )
1393       {
1394         aSewing.Add( anExp.Current() );
1395         LM.Append(anExp.Current());
1396       }
1397     }
1398   } // faces iterator
1399   
1400   aSewing.Perform();
1401   TopoDS_Shape aSewedShape = aSewing.SewedShape();
1402
1403   NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher> aFacesToNameModif;
1404
1405   for ( int i = 1; i <= aShToName.Extent(); i++ )
1406   {
1407     const TopoDS_Shape& CurShape = aShToName.FindKey(i);
1408     const QString& Qstr = aShToName.FindFromIndex(i);
1409     const TopTools_ListOfShape& LM = SH2M(CurShape);
1410     for (TopTools_ListIteratorOfListOfShape it(LM); it.More(); it.Next())
1411     {
1412       const TopoDS_Shape& csh = it.Value();
1413       if (aSewing.IsModified(csh))
1414         aFacesToNameModif.Add(aSewing.Modified(csh), Qstr);
1415       else
1416         aFacesToNameModif.Add(csh, Qstr);
1417     }
1418   }
1419
1420  
1421   // If the sewed shape is empty - return false
1422   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1423     return false;
1424
1425 #ifdef DEB_CALCULATION
1426   BRepTools::Write(aSewedShape ,"Sew.brep");
1427 #endif  // Publish the sewed shape
1428   QString aName = EXPORT_NAME;
1429   GEOM::GEOM_Object_ptr aMainShape = 
1430     HYDROData_GeomTool::ExplodeShapeInGEOMandPublish( theGeomEngine, theStudy, aSewedShape, aFacesToNameModif, aName, theGeomObjEntry );
1431
1432   if ( aMainShape->_is_nil() )  
1433     return false;
1434
1435   if ( theGroupsDefs.IsEmpty() )
1436     return true;
1437
1438   // Create groups
1439   TopTools_IndexedMapOfShape aMapOfSubShapes;
1440   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1441
1442   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1443
1444   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1445   {
1446     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1447
1448     NCollection_Sequence<int> aGroupIndexes;
1449     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1450     {
1451       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1452 #ifdef DEB_CALCULATION
1453       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1454 #endif
1455       
1456       TopoDS_Shape aModifiedShape = aShape;
1457       if ( aSewing.IsModified( aShape ) )
1458         aModifiedShape = aSewing.Modified( aShape );
1459       else if ( aSewing.IsModifiedSubShape( aShape ) )
1460         aModifiedShape = aSewing.ModifiedSubShape( aShape );
1461
1462 #ifdef DEB_CALCULATION
1463       const TopLoc_Location& aL1 = aShape.Location();
1464       const TopLoc_Location& aL2 = aModifiedShape.Location();
1465       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1466 #endif
1467
1468       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1469       if ( anIndex > 0 ) {
1470         aGroupIndexes.Append( anIndex );
1471       } else {
1472 #ifdef DEB_CALCULATION    
1473         TCollection_AsciiString aNam("Lost_");
1474         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1475         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1476         {
1477            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1478            if ( aModifiedShape.IsPartner( aS ) )
1479            {
1480              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1481              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1482              BRepTools::Write(aS ,aName.ToCString());
1483             break;
1484            }
1485          }
1486         }
1487 #endif
1488       }
1489     }
1490     if ( !aGroupIndexes.IsEmpty() )
1491       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1492   }
1493  
1494   if ( !aGroupsData.IsEmpty() )
1495   {
1496     GEOM::GEOM_IGroupOperations_var aGroupOp = 
1497       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
1498
1499     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1500     for ( ; aMapIt.More(); aMapIt.Next() )
1501     {
1502       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
1503       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1504
1505       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1506       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1507         continue;
1508
1509       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1510       aGeomIndexes->length( aGroupIndexes.Length() );
1511
1512       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1513         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1514
1515       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1516       if ( aGroupOp->IsDone() )
1517       {
1518         SALOMEDS::SObject_var aGroupSO = 
1519           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1520       }
1521     }
1522   }
1523
1524   return true;
1525 }
1526 #endif
1527
1528 void HYDROData_CalculationCase::ClearRules( HYDROData_CalculationCase::DataTag theDataTag,
1529                                             const bool theIsSetToUpdate )
1530 {
1531   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1532   HYDROData_PriorityQueue::ClearRules( aRulesLab );
1533
1534   // Indicate model of the need to update splitting
1535   if ( theIsSetToUpdate ) {
1536     Changed( Geom_2d );
1537   }
1538 }
1539
1540 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Entity)&    theObject1,
1541                                          HYDROData_PriorityType             thePriority,
1542                                          const Handle(HYDROData_Entity)&    theObject2,
1543                                          HYDROData_Zone::MergeType          theMergeType,
1544                                          HYDROData_CalculationCase::DataTag theDataTag )
1545 {
1546   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1547   HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1548
1549   // Indicate model of the need to update splitting
1550   Changed( Geom_2d );
1551 }
1552
1553 QString HYDROData_CalculationCase::DumpRules() const
1554 {
1555   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1556   return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1557 }
1558
1559 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1560 {
1561   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1562   Handle(TDataStd_Integer) anAttr = TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1563   anAttr->SetID(TDataStd_Integer::GetID());
1564   // Indicate model of the need to update splitting
1565   Changed( Geom_2d );
1566 }
1567
1568 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1569 {
1570   Handle(TDataStd_Integer) aModeAttr;
1571   bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1572   if( isOK )
1573     return ( AssignmentMode ) aModeAttr->Get();
1574   else
1575     return MANUAL;
1576 }
1577
1578 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1579                                                    QStringList& theScript ) const
1580 {
1581   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1582   HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1583 }
1584
1585 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1586 {
1587   return myLastWarning;
1588 }
1589
1590 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1591 {
1592   myLastWarning.Type = theType;
1593   myLastWarning.Data = theData;
1594 }
1595
1596 void HYDROData_CalculationCase::UpdateRegionsNames( const HYDROData_SequenceOfObjects& theRegions,
1597                                                     const QString& theOldCaseName,
1598                                                     const QString& theName )
1599 {
1600   HYDROData_SequenceOfObjects::Iterator anIter( theRegions );
1601   for ( ; anIter.More(); anIter.Next() )
1602   {
1603     Handle(HYDROData_Region) aRegion =
1604       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1605     if ( aRegion.IsNull() )
1606       continue;
1607
1608     HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegion );
1609
1610     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
1611     HYDROData_SequenceOfObjects::Iterator anIter( aZones );
1612     for ( ; anIter.More(); anIter.Next() )
1613     {
1614       Handle(HYDROData_Zone) aRegZone =
1615         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1616       if ( aRegZone.IsNull() )
1617         continue;
1618
1619       HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegZone );
1620     }
1621   }
1622 }
1623
1624 void HYDROData_CalculationCase::DumpRegionsToPython( QStringList& theResList,
1625                                                      const QString& thePyScriptPath,
1626                                                      MapOfTreatedObjects& theTreatedObjects,
1627                                                      const HYDROData_SequenceOfObjects& theRegions ) const
1628 {
1629   HYDROData_SequenceOfObjects::Iterator anIter;
1630   anIter.Init(theRegions);
1631   for (int ireg = 1; anIter.More(); anIter.Next(), ireg++)
1632     {
1633       Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast(anIter.Value());
1634       if (aRegion.IsNull())
1635         continue;
1636       QString defRegName = this->GetName();
1637       QString regSuffix = QString("_Reg_%1").arg(ireg);
1638       defRegName += regSuffix;
1639       theTreatedObjects.insert(aRegion->GetName(), aRegion);
1640       QStringList aRegDump = aRegion->DumpToPython(thePyScriptPath, theTreatedObjects, defRegName);
1641       theResList << aRegDump;
1642     }
1643   for (anIter.Init(theRegions); anIter.More(); anIter.Next())
1644     {
1645       Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast(anIter.Value());
1646       if (aRegion.IsNull())
1647         continue;
1648       QStringList aRegDump;
1649       aRegion->SetNameInDumpPython(aRegDump);
1650       theResList << aRegDump;
1651     }
1652 }
1653
1654 bool HYDROData_CalculationCase::GetRule( int theIndex, 
1655                                          Handle(HYDROData_Entity)&           theObject1,
1656                                          HYDROData_PriorityType&             thePriority,
1657                                          Handle(HYDROData_Entity)&           theObject2,
1658                                          HYDROData_Zone::MergeType&          theMergeType,
1659                                          HYDROData_CalculationCase::DataTag& theDataTag) const
1660 {
1661   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1662   return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1663     theObject1, thePriority, theObject2, theMergeType );
1664 }
1665
1666 bool HYDROData_CalculationCase::AddInterPoly( const Handle(HYDROData_PolylineXY)& theInterPolyline )
1667 {
1668   HYDROData_CalculationCase::DataTag aDataTag = DataTag_InterPoly;
1669
1670   if ( HasReference( theInterPolyline, aDataTag ) )
1671     return false; 
1672
1673   AddReferenceObject( theInterPolyline, aDataTag );
1674
1675   Changed( Geom_2d );
1676
1677   return true;
1678 }
1679
1680 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetInterPolyObjects() const
1681 {
1682   return GetReferenceObjects( DataTag_InterPoly ); 
1683 }
1684
1685 void HYDROData_CalculationCase::RemoveInterPolyObject( const Handle(HYDROData_PolylineXY)& theInterPolyline ) 
1686 {
1687   if ( theInterPolyline.IsNull() )
1688     return;
1689
1690   RemoveReferenceObject( theInterPolyline->Label(), DataTag_InterPoly );
1691
1692   Changed( Geom_2d );
1693 }
1694
1695 bool HYDROData_CalculationCase::AddBoundaryPolygon( const Handle(HYDROData_BCPolygon)& theBCPolygon )
1696 {
1697   HYDROData_CalculationCase::DataTag aDataTag = DataTag_BCPolygon;
1698
1699   if ( HasReference( theBCPolygon, aDataTag ) )
1700     return false; 
1701
1702   AddReferenceObject( theBCPolygon, aDataTag );
1703
1704   Changed( Geom_2d );
1705
1706   return true;
1707 }
1708
1709 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetBoundaryPolygons() const
1710 {
1711   return GetReferenceObjects( DataTag_BCPolygon ); 
1712 }
1713
1714 void HYDROData_CalculationCase::RemoveBoundaryPolygon( const Handle(HYDROData_BCPolygon)& theBCPolygon ) 
1715 {
1716   if ( theBCPolygon.IsNull() )
1717     return;
1718
1719   RemoveReferenceObject( theBCPolygon->Label(), DataTag_BCPolygon );
1720
1721   Changed( Geom_2d );
1722 }
1723
1724
1725
1726