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