Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[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 aStudyName = "theStudy";
216   QString anEntryVar = aCalculName + "_entry";
217   aResList << QString( "%1 = %2.Export( %3._get_StudyId() )" ).arg( anEntryVar ).arg( aCalculName ).arg( aStudyName );
218
219   // Get geometry object and print debug information
220   aResList << "";
221   aResList << "# Get geometry shape and print debug information";
222   aResList << "import GEOM";
223   aResList << QString( "print \"Entry:\", %1" ).arg( anEntryVar );
224   QString aGeomShapeName = aCalculName + "_geom";
225   aResList << QString( "HYDRO_%1 = salome.IDToObject( str( %2 ) )" ).arg( GetName() ).arg( anEntryVar );
226   aResList << QString( "print \"Geom shape:\", HYDRO_%1" ).arg( GetName() );
227   aResList << QString( "print \"Geom shape name:\", HYDRO_%1.GetName()" ).arg( GetName() );
228
229
230   //DumpSampleMeshing( aResList, aStudyName, aGeomShapeName, aCalculName+"_mesh" );
231
232   aResList << QString( "" );
233   return aResList;
234 }
235
236 void HYDROData_CalculationCase::DumpSampleMeshing( QStringList& theResList,
237                                                    const QString& theStudyName,
238                                                    const QString& theGeomShapeName,
239                                                    const QString& theMeshName ) const
240 {
241   theResList << "";
242   theResList << "# Meshing";
243   theResList << "import SMESH, SALOMEDS";
244   theResList << "from salome.smesh import smeshBuilder";
245   theResList << "from salome.geom import geomBuilder";
246
247   theResList << QString( "smesh = smeshBuilder.New( %1 )" ).arg( theStudyName );
248   theResList << QString( "%1 = smesh.Mesh( %2 )" ).arg( theMeshName ).arg( theGeomShapeName );
249   theResList << QString( "MEFISTO_2D = %1.Triangle( algo=smeshBuilder.MEFISTO )" ).arg( theMeshName );
250   theResList << "Max_Element_Area_1 = MEFISTO_2D.MaxElementArea( 10 )";
251   theResList << QString( "Regular_1D = %1.Segment()" ).arg( theMeshName );
252   theResList << "Max_Size_1 = Regular_1D.MaxSize(10)";
253   theResList << QString( "isDone = %1.Compute()" ).arg( theMeshName );
254
255   theResList << "";
256   theResList << "# Set names of Mesh objects";
257   theResList << "smesh.SetName( MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D' )";
258   theResList << "smesh.SetName( Regular_1D.GetAlgorithm(), 'Regular_1D' )";
259   theResList << "smesh.SetName( Max_Size_1, 'Max Size_1' )";
260   theResList << "smesh.SetName( Max_Element_Area_1, 'Max. Element Area_1' )";
261   theResList << QString( "smesh.SetName( %1.GetMesh(), '%1' )" ).arg( theMeshName );
262
263   theResList << "";
264   theResList << "# Greate SMESH groups";
265   theResList << QString( "geompy = geomBuilder.New( %1 )" ).arg( theStudyName );
266   theResList << QString( "geom_groups = geompy.GetGroups( %1 )" ).arg( theGeomShapeName );
267   theResList << QString( "for group in geom_groups:" );
268   theResList << QString( "    smesh_group = %1.GroupOnGeom(group, group.GetName(), SMESH.EDGE)" )
269                 .arg( theMeshName );
270   theResList << QString( "    smesh.SetName(smesh_group, group.GetName())" );
271   theResList << QString( "    print \"SMESH group '%s': %s\" % (smesh_group.GetName(), smesh_group)" );
272 }
273
274 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
275 {
276   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
277
278   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
279   if ( !aBoundaryPolyline.IsNull() )
280     aResSeq.Append( aBoundaryPolyline );
281
282   HYDROData_SequenceOfObjects aSeqOfGeomObjs = GetGeometryObjects();
283   aResSeq.Append( aSeqOfGeomObjs );
284
285   // Regions
286   HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
287   aResSeq.Append( aSeqOfRegions );
288
289   return aResSeq;
290 }
291
292 static void FilterEdgesByIncludeSelectionBoundaryPolygons( const HYDROData_SplitToZonesTool::SplitDataList& anEdgesList,
293                                                            HYDROData_SequenceOfObjects aBoundaryPolygons,
294                                                            HYDROData_SplitToZonesTool::SplitDataList& outEdgesList)
295 {
296   //perform boundary condition polygons on EdgesList
297   TopTools_SequenceOfShape IncTools, SelectionTools;
298   HYDROData_SplitToZonesTool::SplitDataListIterator anIter(anEdgesList);
299   for (int i=1; i<=aBoundaryPolygons.Size();i++)
300   {
301     Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( aBoundaryPolygons(i));
302     TopoDS_Shape aPolyTopShape = aBCPoly->GetTopShape();
303     int bType = aBCPoly->GetBoundaryType();
304     if (bType == 2) 
305       IncTools.Append(aPolyTopShape);
306     else if (bType == 3)
307       SelectionTools.Append(aPolyTopShape);
308   }
309   
310   while( anIter.hasNext() )
311   {
312     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
313     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
314       continue;
315     if (aSplitData.Shape.ShapeType() != TopAbs_EDGE)
316       continue;
317     if (HYDROData_BoundaryPolygonTools::IncludeTool(IncTools, aSplitData.Shape) && HYDROData_BoundaryPolygonTools::SelectionTool(SelectionTools, aSplitData.Shape))
318       outEdgesList.append(aSplitData);
319   }
320 }
321
322
323 static void SplitEdgesByBoundaryPolygons( const HYDROData_SplitToZonesTool::SplitDataList& anEdgesList,
324                                           const HYDROData_SequenceOfObjects& aBoundaryPolygons,
325                                           NCollection_IndexedDataMap<TopoDS_Shape, TopoDS_Shape, TopTools_ShapeMapHasher>& ObjToRes,
326                                           HYDROData_SplitToZonesTool::SplitDataList& outBoundaryPolygonEdgesList)
327 {
328   //perform boundary condition polygons on EdgesList
329   TopTools_SequenceOfShape CutTools;
330   NCollection_DataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher> BPolyToName;
331
332   ObjToRes.Clear();
333   HYDROData_SplitToZonesTool::SplitDataListIterator anIter(anEdgesList);
334   for (int i=1; i<=aBoundaryPolygons.Size();i++)
335   {
336     Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( aBoundaryPolygons(i));
337     TopoDS_Shape aPolyTopShape = aBCPoly->GetTopShape();
338     int bType = aBCPoly->GetBoundaryType();
339     if (bType == 1) 
340     {
341       CutTools.Append(aPolyTopShape);
342       QString bp_name = aBCPoly->GetName();
343       BPolyToName.Bind(aPolyTopShape, bp_name);
344     }
345   }
346   
347   while( anIter.hasNext() )
348   {
349     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
350     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
351       continue;
352     if (aSplitData.Shape.ShapeType() != TopAbs_EDGE)
353       continue;
354     ObjToRes.Add(aSplitData.Shape, TopoDS_Shape());
355   }
356
357   NCollection_IndexedDataMap<TopoDS_Shape, TopTools_MapOfShape, TopTools_ShapeMapHasher> BPFaceToCutEdges;
358   HYDROData_BoundaryPolygonTools::CutTool(CutTools, ObjToRes, BPFaceToCutEdges);
359   //
360   for (int i = 1; i <= CutTools.Length(); i++ )
361   {
362     TopoDS_Shape F = CutTools(i);
363     const QString* name = BPolyToName.Seek(F);
364     const TopTools_MapOfShape* EdgesIn = BPFaceToCutEdges.Seek(F); // EdgesIn : edges inside boudnary polygon F
365     if (name && EdgesIn)
366     {
367       TopTools_MapIteratorOfMapOfShape it(*EdgesIn);
368       for (;it.More();it.Next())
369       {
370         TopoDS_Edge E = TopoDS::Edge(it.Value());
371         if (!E.IsNull())
372         {
373           HYDROData_SplitToZonesTool::SplitData SD(HYDROData_SplitToZonesTool::SplitData::Data_Edge, E, *name);          
374           outBoundaryPolygonEdgesList.append(SD);
375         }
376       }
377     }
378   }
379 }
380
381
382 static void PerformEdgeReplInZones(const HYDROData_SplitToZonesTool::SplitDataList& ZoneList,
383                                    const NCollection_IndexedDataMap<TopoDS_Shape, TopoDS_Shape, TopTools_ShapeMapHasher>& ObjToRes,
384                                    HYDROData_SplitToZonesTool::SplitDataList& outZoneList)
385 {
386   HYDROData_SplitToZonesTool::SplitDataListIterator it( ZoneList ); 
387   BRepTools_ReShape reshaper;
388   for (;it.hasNext();)
389   {
390     const HYDROData_SplitToZonesTool::SplitData& aSplitData = it.next();
391     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
392       continue;
393
394     TopoDS_Shape mS = aSplitData.Shape;
395     for (int i=1; i<=ObjToRes.Extent();i++)
396     {
397       TopoDS_Shape K = ObjToRes.FindKey(i);
398       TopoDS_Shape V = ObjToRes.FindFromIndex(i);
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( myLab );
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( myLab );
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( myLab );
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( myLab );
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::usleep(500);
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::usleep(500);
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 QString HYDROData_CalculationCase::Export( int theStudyId ) const
1294 {
1295 #ifdef LIGHT_MODE
1296   return "";
1297 #else
1298   GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
1299   SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
1300   
1301   QString aGeomObjEntry, anErrorMsg;
1302   QString statMess;
1303   bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry, anErrorMsg, statMess );
1304   return isOK ? aGeomObjEntry : QString();
1305 #endif
1306 }
1307
1308 #ifndef LIGHT_MODE
1309 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
1310                                         SALOMEDS::Study_ptr theStudy,
1311                                         QString& theGeomObjEntry,
1312                                         QString& theErrorMsg,
1313                                         QString& statMess) const
1314 {
1315   DEBTRACE("Export");
1316   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
1317
1318   // Get groups definitions
1319   HYDROData_SequenceOfObjects aSplitGroups = GetSplitGroups();
1320
1321   TopTools_SequenceOfShape IntSh; //internal edges
1322   HYDROData_SequenceOfObjects::Iterator anIter( aSplitGroups );
1323   for ( ; anIter.More(); anIter.Next() )
1324   {
1325     // Get shapes group
1326     Handle(HYDROData_ShapesGroup) aGroup =
1327       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
1328     if ( aGroup.IsNull() )
1329       continue;
1330
1331     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
1332
1333     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
1334     aGroup->GetShapes( aGroupDef.Shapes );
1335
1336     aSeqOfGroupsDefs.Append( aGroupDef );
1337
1338     Handle(HYDROData_SplitShapesGroup) aSSGroup = Handle(HYDROData_SplitShapesGroup)::DownCast( anIter.Value() );
1339     TopTools_SequenceOfShape dummy;
1340     if (!aSSGroup.IsNull())
1341       if (aSSGroup->GetInternal())
1342       {
1343         aSSGroup->GetShapes(dummy);
1344         IntSh.Append(dummy);
1345       }
1346
1347   }
1348   
1349   // Get faces
1350   bool isAllNotSubmersible = true;
1351   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
1352   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
1353   NCollection_IndexedDataMap<TopoDS_Shape, QString> aShToNames;
1354   for ( ; aRegionIter.More(); aRegionIter.Next() )
1355   {
1356     Handle(HYDROData_Region) aRegion =
1357       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
1358     if( aRegion.IsNull() || !aRegion->IsSubmersible() )
1359       continue;
1360     
1361     if ( isAllNotSubmersible )
1362       isAllNotSubmersible = false;
1363
1364     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs, &IntSh );
1365     aShToNames.Add( aRegionShape, aRegion->GetName() );
1366   }
1367
1368   bool aRes = false;
1369
1370   if ( aCaseRegions.IsEmpty() ) {
1371     theErrorMsg = QString("the list of regions is empty.");
1372   } else if ( isAllNotSubmersible ) {
1373     theErrorMsg = QString("there are no submersible regions.");
1374   } else {
1375     aRes = Export( theGeomEngine, theStudy, aShToNames, aSeqOfGroupsDefs, theGeomObjEntry );;
1376   }
1377
1378   if( aRes && !GetLandCoverMap().IsNull() && !GetStricklerTable().IsNull() )
1379   {
1380     QString aTelemacFileName = GetName() + ".telemac";
1381     aRes = GetLandCoverMap()->ExportTelemac( aTelemacFileName, 1E-2, GetStricklerTable(), statMess );
1382     if (!aRes)
1383       theErrorMsg = QString( "The export to TELEMAC %1 failed" ).arg( aTelemacFileName );
1384   }
1385   return aRes;
1386 }
1387
1388 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
1389                                         SALOMEDS::Study_ptr                           theStudy,
1390                                         const NCollection_IndexedDataMap<TopoDS_Shape, QString>& aShToName,
1391                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
1392                                         QString& theGeomObjEntry ) const
1393 {
1394   // Sew faces
1395   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
1396   aSewing.SetNonManifoldMode( Standard_False );
1397 #ifdef DEB_CALCULATION
1398   TCollection_AsciiString aNam("Sh_");
1399   int i=1;
1400 #endif
1401   TopTools_DataMapOfShapeListOfShape SH2M;
1402   for ( int i = 1; i <= aShToName.Extent(); i++ )
1403   {
1404     const TopoDS_Shape& aShape = aShToName.FindKey(i);
1405     if ( aShape.IsNull() )
1406       continue;
1407
1408     SH2M.Bind(aShape, TopTools_ListOfShape());
1409     TopTools_ListOfShape& LM = SH2M.ChangeFind(aShape);
1410     if ( aShape.ShapeType() == TopAbs_FACE || aShape.ShapeType() == TopAbs_SHELL )
1411     {
1412       aSewing.Add( aShape );
1413       LM.Append(aShape);
1414     }
1415     else if (aShape.ShapeType() == TopAbs_COMPOUND)
1416     {
1417       TopExp_Explorer anExp( aShape, TopAbs_SHELL );
1418       for (; anExp.More(); anExp.Next() )
1419       {
1420         aSewing.Add( anExp.Current() );
1421         LM.Append(anExp.Current());
1422       }
1423       anExp.Init( aShape, TopAbs_FACE, TopAbs_SHELL );
1424       for (; anExp.More(); anExp.Next() )
1425       {
1426         aSewing.Add( anExp.Current() );
1427         LM.Append(anExp.Current());
1428       }
1429     }
1430   } // faces iterator
1431   
1432   aSewing.Perform();
1433   TopoDS_Shape aSewedShape = aSewing.SewedShape();
1434
1435   NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher> aFacesToNameModif;
1436
1437   for ( int i = 1; i <= aShToName.Extent(); i++ )
1438   {
1439     const TopoDS_Shape& CurShape = aShToName.FindKey(i);
1440     const QString& Qstr = aShToName.FindFromIndex(i);
1441     const TopTools_ListOfShape& LM = SH2M(CurShape);
1442     for (TopTools_ListIteratorOfListOfShape it(LM); it.More(); it.Next())
1443     {
1444       const TopoDS_Shape& csh = it.Value();
1445       if (aSewing.IsModified(csh))
1446         aFacesToNameModif.Add(aSewing.Modified(csh), Qstr);
1447       else
1448         aFacesToNameModif.Add(csh, Qstr);
1449     }
1450   }
1451
1452  
1453   // If the sewed shape is empty - return false
1454   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1455     return false;
1456
1457 #ifdef DEB_CALCULATION
1458   BRepTools::Write(aSewedShape ,"Sew.brep");
1459 #endif  // Publish the sewed shape
1460   QString aName = EXPORT_NAME;
1461   GEOM::GEOM_Object_ptr aMainShape = 
1462     HYDROData_GeomTool::ExplodeShapeInGEOMandPublish( theGeomEngine, theStudy, aSewedShape, aFacesToNameModif, aName, theGeomObjEntry );
1463
1464   if ( aMainShape->_is_nil() )  
1465     return false;
1466
1467   if ( theGroupsDefs.IsEmpty() )
1468     return true;
1469
1470   // Create groups
1471   TopTools_IndexedMapOfShape aMapOfSubShapes;
1472   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1473
1474   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1475
1476   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1477   {
1478     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1479
1480     NCollection_Sequence<int> aGroupIndexes;
1481     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1482     {
1483       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1484 #ifdef DEB_CALCULATION
1485       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1486 #endif
1487       
1488       TopoDS_Shape aModifiedShape = aShape;
1489       if ( aSewing.IsModified( aShape ) )
1490         aModifiedShape = aSewing.Modified( aShape );
1491       else if ( aSewing.IsModifiedSubShape( aShape ) )
1492         aModifiedShape = aSewing.ModifiedSubShape( aShape );
1493
1494 #ifdef DEB_CALCULATION
1495       const TopLoc_Location& aL1 = aShape.Location();
1496       const TopLoc_Location& aL2 = aModifiedShape.Location();
1497       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1498 #endif
1499
1500       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1501       if ( anIndex > 0 ) {
1502         aGroupIndexes.Append( anIndex );
1503       } else {
1504 #ifdef DEB_CALCULATION    
1505         TCollection_AsciiString aNam("Lost_");
1506         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1507         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1508         {
1509            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1510            if ( aModifiedShape.IsPartner( aS ) )
1511            {
1512              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1513              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1514              BRepTools::Write(aS ,aName.ToCString());
1515             break;
1516            }
1517          }
1518         }
1519 #endif
1520       }
1521     }
1522     if ( !aGroupIndexes.IsEmpty() )
1523       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1524   }
1525  
1526   if ( !aGroupsData.IsEmpty() )
1527   {
1528     GEOM::GEOM_IGroupOperations_var aGroupOp = 
1529       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
1530
1531     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1532     for ( ; aMapIt.More(); aMapIt.Next() )
1533     {
1534       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
1535       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1536
1537       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1538       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1539         continue;
1540
1541       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1542       aGeomIndexes->length( aGroupIndexes.Length() );
1543
1544       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1545         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1546
1547       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1548       if ( aGroupOp->IsDone() )
1549       {
1550         SALOMEDS::SObject_var aGroupSO = 
1551           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1552       }
1553     }
1554   }
1555
1556   return true;
1557 }
1558 #endif
1559
1560 void HYDROData_CalculationCase::ClearRules( HYDROData_CalculationCase::DataTag theDataTag,
1561                                             const bool theIsSetToUpdate )
1562 {
1563   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1564   HYDROData_PriorityQueue::ClearRules( aRulesLab );
1565
1566   // Indicate model of the need to update splitting
1567   if ( theIsSetToUpdate ) {
1568     Changed( Geom_2d );
1569   }
1570 }
1571
1572 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Entity)&    theObject1,
1573                                          HYDROData_PriorityType             thePriority,
1574                                          const Handle(HYDROData_Entity)&    theObject2,
1575                                          HYDROData_Zone::MergeType          theMergeType,
1576                                          HYDROData_CalculationCase::DataTag theDataTag )
1577 {
1578   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1579   HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1580
1581   // Indicate model of the need to update splitting
1582   Changed( Geom_2d );
1583 }
1584
1585 QString HYDROData_CalculationCase::DumpRules() const
1586 {
1587   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1588   return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1589 }
1590
1591 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1592 {
1593   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1594   Handle(TDataStd_Integer) anAttr = TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1595   anAttr->SetID(TDataStd_Integer::GetID());
1596   // Indicate model of the need to update splitting
1597   Changed( Geom_2d );
1598 }
1599
1600 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1601 {
1602   Handle(TDataStd_Integer) aModeAttr;
1603   bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1604   if( isOK )
1605     return ( AssignmentMode ) aModeAttr->Get();
1606   else
1607     return MANUAL;
1608 }
1609
1610 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1611                                                    QStringList& theScript ) const
1612 {
1613   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1614   HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1615 }
1616
1617 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1618 {
1619   return myLastWarning;
1620 }
1621
1622 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1623 {
1624   myLastWarning.Type = theType;
1625   myLastWarning.Data = theData;
1626 }
1627
1628 void HYDROData_CalculationCase::UpdateRegionsNames( const HYDROData_SequenceOfObjects& theRegions,
1629                                                     const QString& theOldCaseName,
1630                                                     const QString& theName )
1631 {
1632   HYDROData_SequenceOfObjects::Iterator anIter( theRegions );
1633   for ( ; anIter.More(); anIter.Next() )
1634   {
1635     Handle(HYDROData_Region) aRegion =
1636       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1637     if ( aRegion.IsNull() )
1638       continue;
1639
1640     HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegion );
1641
1642     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
1643     HYDROData_SequenceOfObjects::Iterator anIter( aZones );
1644     for ( ; anIter.More(); anIter.Next() )
1645     {
1646       Handle(HYDROData_Zone) aRegZone =
1647         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1648       if ( aRegZone.IsNull() )
1649         continue;
1650
1651       HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegZone );
1652     }
1653   }
1654 }
1655
1656 void HYDROData_CalculationCase::DumpRegionsToPython( QStringList& theResList,
1657                                                      const QString& thePyScriptPath,
1658                                                      MapOfTreatedObjects& theTreatedObjects,
1659                                                      const HYDROData_SequenceOfObjects& theRegions ) const
1660 {
1661   HYDROData_SequenceOfObjects::Iterator anIter;
1662   anIter.Init(theRegions);
1663   for (int ireg = 1; anIter.More(); anIter.Next(), ireg++)
1664     {
1665       Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast(anIter.Value());
1666       if (aRegion.IsNull())
1667         continue;
1668       QString defRegName = this->GetName();
1669       QString regSuffix = QString("_Reg_%1").arg(ireg);
1670       defRegName += regSuffix;
1671       theTreatedObjects.insert(aRegion->GetName(), aRegion);
1672       QStringList aRegDump = aRegion->DumpToPython(thePyScriptPath, theTreatedObjects, defRegName);
1673       theResList << aRegDump;
1674     }
1675   for (anIter.Init(theRegions); anIter.More(); anIter.Next())
1676     {
1677       Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast(anIter.Value());
1678       if (aRegion.IsNull())
1679         continue;
1680       QStringList aRegDump;
1681       aRegion->SetNameInDumpPython(aRegDump);
1682       theResList << aRegDump;
1683     }
1684 }
1685
1686 bool HYDROData_CalculationCase::GetRule( int theIndex, 
1687                                          Handle(HYDROData_Entity)&           theObject1,
1688                                          HYDROData_PriorityType&             thePriority,
1689                                          Handle(HYDROData_Entity)&           theObject2,
1690                                          HYDROData_Zone::MergeType&          theMergeType,
1691                                          HYDROData_CalculationCase::DataTag& theDataTag) const
1692 {
1693   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1694   return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1695     theObject1, thePriority, theObject2, theMergeType );
1696 }
1697
1698 bool HYDROData_CalculationCase::AddInterPoly( const Handle(HYDROData_PolylineXY)& theInterPolyline )
1699 {
1700   HYDROData_CalculationCase::DataTag aDataTag = DataTag_InterPoly;
1701
1702   if ( HasReference( theInterPolyline, aDataTag ) )
1703     return false; 
1704
1705   AddReferenceObject( theInterPolyline, aDataTag );
1706
1707   Changed( Geom_2d );
1708
1709   return true;
1710 }
1711
1712 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetInterPolyObjects() const
1713 {
1714   return GetReferenceObjects( DataTag_InterPoly ); 
1715 }
1716
1717 void HYDROData_CalculationCase::RemoveInterPolyObject( const Handle(HYDROData_PolylineXY)& theInterPolyline ) 
1718 {
1719   if ( theInterPolyline.IsNull() )
1720     return;
1721
1722   RemoveReferenceObject( theInterPolyline->Label(), DataTag_InterPoly );
1723
1724   Changed( Geom_2d );
1725 }
1726
1727 bool HYDROData_CalculationCase::AddBoundaryPolygon( const Handle(HYDROData_BCPolygon)& theBCPolygon )
1728 {
1729   HYDROData_CalculationCase::DataTag aDataTag = DataTag_BCPolygon;
1730
1731   if ( HasReference( theBCPolygon, aDataTag ) )
1732     return false; 
1733
1734   AddReferenceObject( theBCPolygon, aDataTag );
1735
1736   Changed( Geom_2d );
1737
1738   return true;
1739 }
1740
1741 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetBoundaryPolygons() const
1742 {
1743   return GetReferenceObjects( DataTag_BCPolygon ); 
1744 }
1745
1746 void HYDROData_CalculationCase::RemoveBoundaryPolygon( const Handle(HYDROData_BCPolygon)& theBCPolygon ) 
1747 {
1748   if ( theBCPolygon.IsNull() )
1749     return;
1750
1751   RemoveReferenceObject( theBCPolygon->Label(), DataTag_BCPolygon );
1752
1753   Changed( Geom_2d );
1754 }
1755
1756
1757
1758