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