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