Salome HOME
Merge branch 'BR_v14_rc' into BR_quadtree
[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
21 #include "HYDROData_ArtificialObject.h"
22 #include "HYDROData_IAltitudeObject.h"
23 #include "HYDROData_Document.h"
24 #include "HYDROData_ShapesGroup.h"
25 #include "HYDROData_Iterator.h"
26 #include "HYDROData_NaturalObject.h"
27 #include "HYDROData_PolylineXY.h"
28 #include "HYDROData_StricklerTable.h"
29 #include "HYDROData_LandCover.h"
30 #include "HYDROData_SplittedShapesGroup.h"
31 #include "HYDROData_Region.h"
32 #include "HYDROData_Tool.h"
33 #include "HYDROData_GeomTool.h"
34
35 #include <GEOMBase.h>
36
37 #include <QSet>
38
39 #include <TopoDS.hxx>
40 #include <TopoDS_Shell.hxx>
41 #include <TopoDS_Edge.hxx>
42
43 #include <BRep_Builder.hxx>
44 #include <BRepBuilderAPI_Sewing.hxx>
45 #include <BRepTopAdaptor_FClass2d.hxx>
46
47 #include <BRepTools.hxx>
48
49 #include <TopAbs.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopExp.hxx>
52 #include <TopTools_ListOfShape.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
54 #include <TDataStd_Integer.hxx>
55
56 //#define  DEB_CALCULATION 1
57 #ifdef DEB_CALCULATION
58 #include <BRepTools.hxx>
59 #include <TopLoc_Location.hxx>
60 #endif 
61 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
62 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
63 #define CALCULATION_GROUPS_PREF GetName() + "_"
64 //#define DEB_CLASS2D 1
65 #ifdef DEB_CLASS2D
66 #include <BRepBuilderAPI_MakeVertex.hxx>
67 #endif
68
69 #define EXPORT_NAME "HYDRO_" + GetName()
70
71 #include <SALOME_NamingService.hxx>
72 #include <SALOME_LifeCycleCORBA.hxx>
73
74 #define _DEVDEBUG_
75 #include "HYDRO_trace.hxx"
76
77 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
78 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
79
80 HYDROData_CalculationCase::HYDROData_CalculationCase()
81 : HYDROData_Entity()
82 {
83 }
84
85 HYDROData_CalculationCase::~HYDROData_CalculationCase()
86 {
87 }
88
89 void HYDROData_CalculationCase::SetName( const QString& theName )
90 {
91   QString anOldCaseName = GetName();
92   if ( anOldCaseName != theName )
93   {
94     HYDROData_SequenceOfObjects aRegions = GetRegions();
95
96     HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
97     for ( ; anIter.More(); anIter.Next() )
98     {
99       Handle(HYDROData_Region) aRegion =
100         Handle(HYDROData_Region)::DownCast( anIter.Value() );
101       if ( aRegion.IsNull() )
102         continue;
103
104       HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
105
106       HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
107       HYDROData_SequenceOfObjects::Iterator anIter( aZones );
108       for ( ; anIter.More(); anIter.Next() )
109       {
110         Handle(HYDROData_Zone) aRegZone =
111           Handle(HYDROData_Zone)::DownCast( anIter.Value() );
112         if ( aRegZone.IsNull() )
113           continue;
114
115         HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
116       }
117     }
118
119     HYDROData_SequenceOfObjects aGroups = GetSplittedGroups();
120
121     anIter.Init( aGroups );
122     for ( ; anIter.More(); anIter.Next() )
123     {
124       Handle(HYDROData_SplittedShapesGroup) aGroup =
125         Handle(HYDROData_SplittedShapesGroup)::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( MapOfTreatedObjects& theTreatedObjects ) const
137 {
138   QStringList aResList = dumpObjectCreation( theTreatedObjects );
139   aResList.prepend( "# Calculation case" );
140
141   QString aCalculName = GetObjPyName();
142
143   AssignmentMode aMode = GetAssignmentMode();
144   QString aModeStr = aMode==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
145   aResList << QString( "%0.SetAssignmentMode( %1 )" ).arg( aCalculName ).arg( aModeStr );
146
147   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
148   HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
149   for ( ; anIter.More(); anIter.Next() )
150   {
151     Handle(HYDROData_Object) aRefGeomObj =
152       Handle(HYDROData_Object)::DownCast( anIter.Value() );
153     setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
154   }
155   aResList << QString( "" );
156
157   QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
158
159   HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
160   anIter.Init( aGeomGroups );
161   for ( ; anIter.More(); anIter.Next() )
162   {
163     Handle(HYDROData_ShapesGroup) aGeomGroup =
164       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
165     if ( aGeomGroup.IsNull() )
166       continue;
167
168     Handle(HYDROData_Object) aFatherGeom =
169       Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
170     if ( aFatherGeom.IsNull() )
171       continue;
172
173     int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
174     aResList << QString( "%1 = %2.GetGroup( %3 );" )
175               .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
176
177     aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
178   }
179
180   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
181   setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
182
183   if( aMode==AUTOMATIC )
184     DumpRulesToPython( aCalculName, aResList );
185
186   aResList << QString( "" );
187   aResList << "# Start the algorithm of the partition and assignment";
188   aResList << QString( "%1.Update();" ).arg( aCalculName );
189
190   if( aMode==MANUAL )
191   {
192     // Now we restore the regions and zones order
193     HYDROData_SequenceOfObjects aRegions = GetRegions();
194     anIter.Init( aRegions );
195     for ( ; anIter.More(); anIter.Next() )
196     {
197       Handle(HYDROData_Region) aRegion =
198         Handle(HYDROData_Region)::DownCast( anIter.Value() );
199       if ( aRegion.IsNull() )
200         continue;
201
202       theTreatedObjects.insert( aRegion->GetName(), aRegion );
203       QStringList aRegDump = aRegion->DumpToPython( theTreatedObjects );
204       aResList << aRegDump;
205     }
206   }
207
208   // Export calculation case
209   aResList << QString( "" );
210   aResList << "# Export of the calculation case";
211   QString aStudyName = "theStudy";
212   QString anEntryVar = aCalculName + "_entry";
213   aResList << QString( "%1 = %2.Export( %3._get_StudyId() )" ).arg( anEntryVar ).arg( aCalculName ).arg( aStudyName );
214
215   // Get geometry object and print debug information
216   aResList << "";
217   aResList << "# Get geometry shape and print debug information";
218   aResList << "import GEOM";
219   aResList << QString( "print \"Entry:\", %1" ).arg( anEntryVar );
220   QString aGeomShapeName = aCalculName + "_geom";
221   aResList << QString( "%1 = salome.IDToObject( str( %2 ) )" ).arg( aGeomShapeName ).arg( anEntryVar );
222   aResList << QString( "print \"Geom shape:\", %1" ).arg( aGeomShapeName );
223   aResList << QString( "print \"Geom shape name:\", %1.GetName()" ).arg( aGeomShapeName );
224
225   DumpSampleMeshing( aResList, aStudyName, aGeomShapeName, aCalculName+"_mesh" );
226
227   aResList << QString( "" );
228   return aResList;
229 }
230
231 void HYDROData_CalculationCase::DumpSampleMeshing( QStringList& theResList,
232                                                    const QString& theStudyName,
233                                                    const QString& theGeomShapeName,
234                                                    const QString& theMeshName ) const
235 {
236   theResList << "";
237   theResList << "# Meshing";
238   theResList << "import SMESH, SALOMEDS";
239   theResList << "from salome.smesh import smeshBuilder";
240   theResList << "from salome.geom import geomBuilder";
241
242   theResList << QString( "smesh = smeshBuilder.New( %1 )" ).arg( theStudyName );
243   theResList << QString( "%1 = smesh.Mesh( %2 )" ).arg( theMeshName ).arg( theGeomShapeName );
244   theResList << QString( "MEFISTO_2D = %1.Triangle( algo=smeshBuilder.MEFISTO )" ).arg( theMeshName );
245   theResList << "Max_Element_Area_1 = MEFISTO_2D.MaxElementArea( 10 )";
246   theResList << QString( "Regular_1D = %1.Segment()" ).arg( theMeshName );
247   theResList << "Max_Size_1 = Regular_1D.MaxSize(10)";
248   theResList << QString( "isDone = %1.Compute()" ).arg( theMeshName );
249
250   theResList << "";
251   theResList << "# Set names of Mesh objects";
252   theResList << "smesh.SetName( MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D' )";
253   theResList << "smesh.SetName( Regular_1D.GetAlgorithm(), 'Regular_1D' )";
254   theResList << "smesh.SetName( Max_Size_1, 'Max Size_1' )";
255   theResList << "smesh.SetName( Max_Element_Area_1, 'Max. Element Area_1' )";
256   theResList << QString( "smesh.SetName( %1.GetMesh(), '%1' )" ).arg( theMeshName );
257
258   theResList << "";
259   theResList << "# Greate SMESH groups";
260   theResList << QString( "geompy = geomBuilder.New( %1 )" ).arg( theStudyName );
261   theResList << QString( "geom_groups = geompy.GetGroups( %1 )" ).arg( theGeomShapeName );
262   theResList << QString( "for group in geom_groups:" );
263   theResList << QString( "    smesh_group = %1.GroupOnGeom(group, group.GetName(), SMESH.EDGE)" )
264                 .arg( theMeshName );
265   theResList << QString( "    smesh.SetName(smesh_group, group.GetName())" );
266   theResList << QString( "    print \"SMESH group '%s': %s\" % (smesh_group.GetName(), smesh_group)" );
267 }
268
269 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
270 {
271   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
272
273   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
274   if ( !aBoundaryPolyline.IsNull() )
275     aResSeq.Append( aBoundaryPolyline );
276
277   HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
278   aResSeq.Append( aSeqOfRegions );
279
280   return aResSeq;
281 }
282
283 void HYDROData_CalculationCase::Update()
284 {
285   HYDROData_Entity::Update();
286   SetWarning();
287
288   // At first we remove previously created objects
289   RemoveRegions();
290   RemoveSplittedGroups();
291
292   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
293   if ( aDocument.IsNull() )
294     return;
295
296   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
297   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
298   if ( aGeomObjects.IsEmpty() )
299     return;
300
301   HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
302
303   HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
304     HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
305   if ( aSplitObjects.isEmpty() )
306     return;
307
308   HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
309
310   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
311   while( anIter.hasNext() )
312   {
313     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
314     if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
315       aZonesList.append( aSplitData );
316     else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
317       anEdgesList.append( aSplitData );
318   }
319
320   switch( GetAssignmentMode() )
321   {
322   case MANUAL:
323     CreateRegionsDef( aDocument, aZonesList );
324     break;
325   case AUTOMATIC:
326     CreateRegionsAuto( aDocument, aZonesList );
327     break;
328   }
329   CreateEdgeGroupsDef( aDocument, anEdgesList );
330 }
331
332 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
333                                                   const HYDROData_SplitToZonesTool::SplitDataList& theZones )
334 {
335   // Create result regions for case, by default one zone for one region
336   QString aRegsPref = CALCULATION_REGIONS_PREF;
337   QString aZonesPref = CALCULATION_ZONES_PREF;
338
339   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
340   while( anIter.hasNext() )
341   {
342     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
343     // Create new region
344     Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
345
346     // Add the zone for region
347     Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
348   }
349 }
350
351 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
352                                                    const HYDROData_SplitToZonesTool::SplitDataList& theZones )
353 {
354   QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
355   QMap<QString, QString> aRegionNameToObjNameMap;
356   QString aZonesPref = CALCULATION_ZONES_PREF;
357   HYDROData_PriorityQueue aPr( this, DataTag_CustomRules );
358
359   // 1. First we create a default region for each object included into the calculation case
360   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
361   for( int i=aGeomObjects.Lower(), n=aGeomObjects.Upper(); i<=n; i++ )
362   {
363     Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( aGeomObjects.Value( i ) );
364     if( anObj.IsNull() )
365       continue;
366     QString anObjName = anObj->GetName();
367     QString aRegName = anObjName + "_reg";
368     Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
369     aRegionsMap.insert( anObjName, aRegion );
370     aRegionNameToObjNameMap.insert( aRegName, anObjName );
371   }
372
373   // 2. Now for each zone it is necessary to determine the most priority object
374   //    and assign to zone to corresponding region
375   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
376   while( anIter.hasNext() )
377   {
378     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
379     HYDROData_Zone::MergeAltitudesType aMergeType;
380     Handle(HYDROData_Object) aRegObj =
381       Handle(HYDROData_Object)::DownCast( aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType ) );
382     if( aRegObj.IsNull() )
383       continue;
384     Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
385     if( aRegion.IsNull() )
386       continue;
387     Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
388
389     if( aSplitData.ObjectNames.count() > 1 && aMergeType==HYDROData_Zone::Merge_UNKNOWN )
390     {
391       qDebug( "Error in algorithm: unresolved conflicts" );
392     }
393
394     switch( aMergeType )
395     {
396     case HYDROData_Zone::Merge_ZMIN:
397     case HYDROData_Zone::Merge_ZMAX:
398       aRegionZone->SetMergeType( aMergeType );
399       break;
400     case HYDROData_Zone::Merge_Object:
401       aRegionZone->SetMergeType( aMergeType );
402       aRegionZone->RemoveMergeAltitude();
403       aRegionZone->SetMergeAltitude( aRegObj->GetAltitudeObject() );
404       break;
405     }
406   }
407
408   QStringList anObjectsWithEmptyRegions;
409   QMap<QString, Handle(HYDROData_Region)>::const_iterator
410     anIt = aRegionsMap.begin(), aLast = aRegionsMap.end();
411   for( ; anIt!=aLast; anIt++ )
412   {
413     Handle(HYDROData_Region) aRegion = anIt.value();
414     if( aRegion->GetZones().IsEmpty() )
415     {
416       QString aRegName = aRegion->GetName();
417       QString anObjName = aRegionNameToObjNameMap[aRegName];
418       anObjectsWithEmptyRegions.append( anObjName );
419     }
420   }
421   
422   if( !anObjectsWithEmptyRegions.empty() )
423   {
424     QString aData = anObjectsWithEmptyRegions.join( ", " );
425     SetWarning( WARN_EMPTY_REGIONS, aData );
426   }
427 }
428
429 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
430                                                      const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
431 {
432   QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
433
434   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
435   while( anIter.hasNext() )
436   {
437     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
438     // Create new edges group
439     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
440       continue;
441
442     QString anObjName = aSplitData.ObjectNames.first();
443     if ( anObjName.isEmpty() )
444       continue;
445 #ifdef DEB_CALCULATION
446     QString aStr = aSplitData.ObjectNames.join(" "); 
447           cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl; 
448 #endif
449     Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
450     if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
451     {
452       aSplittedGroup = addNewSplittedGroup( CALCULATION_GROUPS_PREF + anObjName );
453       aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
454     }
455     else
456     {
457       aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
458     }
459     if ( aSplittedGroup.IsNull() )
460       continue;
461
462     aSplittedGroup->AddShape( aSplitData.Shape );
463   }
464 }
465
466 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
467 {
468   if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
469     return false; // Wrong type of object
470
471   if ( HasReference( theObject, DataTag_GeometryObject ) )
472     return false; // Object is already in reference list
473
474   AddReferenceObject( theObject, DataTag_GeometryObject );
475   
476   // Indicate model of the need to update splitting
477   SetToUpdate( true );
478
479   return true;
480 }
481
482 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
483 {
484   return GetReferenceObjects( DataTag_GeometryObject );
485 }
486
487 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
488 {
489   if ( theObject.IsNull() )
490     return;
491
492   RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
493
494   // Indicate model of the need to update splitting
495   SetToUpdate( true );
496 }
497
498 void HYDROData_CalculationCase::RemoveGeometryObjects()
499 {
500   ClearReferenceObjects( DataTag_GeometryObject );
501
502   // Indicate model of the need to update splitting
503   SetToUpdate( true );
504 }
505
506 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
507 {
508   if ( theGroup.IsNull() )
509     return false;
510
511   if ( HasReference( theGroup, DataTag_GeometryGroup ) )
512     return false; // Object is already in reference list
513
514   AddReferenceObject( theGroup, DataTag_GeometryGroup );
515   
516   // Indicate model of the need to update splitting
517   SetToUpdate( true );
518
519   return true;
520 }
521
522 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
523 {
524   return GetReferenceObjects( DataTag_GeometryGroup );
525 }
526
527 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
528 {
529   if ( theGroup.IsNull() )
530     return;
531
532   RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
533
534   // Indicate model of the need to update splitting
535   SetToUpdate( true );
536 }
537
538 void HYDROData_CalculationCase::RemoveGeometryGroups()
539 {
540   ClearReferenceObjects( DataTag_GeometryGroup );
541
542   // Indicate model of the need to update splitting
543   SetToUpdate( true );
544 }
545
546 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
547 {
548   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
549
550   SetReferenceObject( thePolyline, DataTag_Polyline );
551
552   // Indicate model of the need to update zones splitting
553   SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
554 }
555
556 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
557 {
558   return Handle(HYDROData_PolylineXY)::DownCast( 
559            GetReferenceObject( DataTag_Polyline ) );
560 }
561
562 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
563 {
564   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
565
566   ClearReferenceObjects( DataTag_Polyline );
567
568   // Indicate model of the need to update zones splitting
569   SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
570 }
571
572 void HYDROData_CalculationCase::SetStricklerTable( const Handle(HYDROData_StricklerTable)& theStricklerTable )
573 {
574   Handle(HYDROData_StricklerTable) aPrevStricklerTable = GetStricklerTable();
575
576   SetReferenceObject( theStricklerTable, DataTag_StricklerTable );
577
578   // Indicate model of the need to update land covers partition
579   SetToUpdate( !IsEqual( aPrevStricklerTable, theStricklerTable ) || IsMustBeUpdated() );
580 }
581
582 Handle(HYDROData_StricklerTable) HYDROData_CalculationCase::GetStricklerTable() const
583 {
584   return Handle(HYDROData_StricklerTable)::DownCast( 
585            GetReferenceObject( DataTag_StricklerTable ) );
586 }
587
588 void HYDROData_CalculationCase::RemoveStricklerTable()
589 {
590   Handle(HYDROData_StricklerTable) aPrevStricklerTable = GetStricklerTable();
591
592   ClearReferenceObjects( DataTag_StricklerTable );
593
594   // Indicate model of the need to update land covers partition
595   SetToUpdate( !aPrevStricklerTable.IsNull() || IsMustBeUpdated() );
596 }
597
598 bool HYDROData_CalculationCase::AddLandCover( const Handle(HYDROData_LandCover)& theLandCover )
599 {
600   if ( HasReference( theLandCover, DataTag_LandCover ) )
601     return false; // Land cover is already in reference list
602
603   AddReferenceObject( theLandCover, DataTag_LandCover );
604   
605   // Indicate model of the need to update land covers partition
606   SetToUpdate( true );
607
608   return true;
609 }
610
611 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetLandCovers() const
612 {
613   return GetReferenceObjects( DataTag_LandCover );
614 }
615
616 void HYDROData_CalculationCase::RemoveLandCover( const Handle(HYDROData_LandCover)& theLandCover )
617 {
618   if ( theLandCover.IsNull() )
619     return;
620
621   RemoveReferenceObject( theLandCover->Label(), DataTag_LandCover );
622
623   // Indicate model of the need to update land cover partition
624   SetToUpdate( true );
625 }
626
627 void HYDROData_CalculationCase::RemoveLandCovers()
628 {
629   ClearReferenceObjects( DataTag_LandCover );
630
631   // Indicate model of the need to update land cover partition
632   SetToUpdate( true );
633 }
634
635 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
636 {
637   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
638   Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
639   if ( aNewRegion.IsNull() )
640     return aNewRegion;
641
642   aNewRegion->AddZone( theZone );
643
644   return aNewRegion;
645 }
646
647 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
648 {
649   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
650
651   if ( theRegion.IsNull() )
652     return false;
653   
654   if ( HasReference( theRegion, DataTag_Region ) )
655     return false; // Object is already in reference list
656
657   // Move the region from other calculation
658   Handle(HYDROData_CalculationCase) aFatherCalc = 
659     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
660   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
661   {
662     Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
663     theRegion->CopyTo( aNewRegion );
664
665     aFatherCalc->RemoveRegion( theRegion );
666
667     theRegion->SetLabel( aNewRegion->Label() );
668   }
669   else
670   {
671     AddReferenceObject( theRegion, DataTag_Region );
672   }
673
674   return true;
675 }
676
677 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
678 {
679   return GetReferenceObjects( DataTag_Region );
680 }
681
682 void HYDROData_CalculationCase::UpdateRegionsOrder()
683 {
684   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
685   if ( aDocument.IsNull() )
686     return;
687
688   HYDROData_SequenceOfObjects aRegions = GetRegions();
689
690   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
691   for ( ; anIter.More(); anIter.Next() )
692   {
693     Handle(HYDROData_Region) aRegion =
694       Handle(HYDROData_Region)::DownCast( anIter.Value() );
695     if ( aRegion.IsNull() )
696       continue;
697
698     aRegion->SetName( "" );
699   }
700
701   QString aRegsPref = CALCULATION_REGIONS_PREF;
702
703   anIter.Init( aRegions );
704   for ( ; anIter.More(); anIter.Next() )
705   {
706     Handle(HYDROData_Region) aRegion =
707       Handle(HYDROData_Region)::DownCast( anIter.Value() );
708     if ( aRegion.IsNull() )
709       continue;
710
711     QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
712     aRegion->SetName( aRegionName );
713   }
714 }
715
716 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
717 {
718   if ( theRegion.IsNull() )
719     return;
720
721   RemoveReferenceObject( theRegion->Label(), DataTag_Region );
722
723   // Remove region from data model
724   Handle(HYDROData_CalculationCase) aFatherCalc = 
725     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
726   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
727     theRegion->Remove();
728 }
729
730 void HYDROData_CalculationCase::RemoveRegions()
731 {
732   myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
733 }
734
735 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
736 {
737   return GetReferenceObjects( DataTag_SplittedGroups );
738 }
739
740 void HYDROData_CalculationCase::RemoveSplittedGroups()
741 {
742   myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
743 }
744
745 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
746 {
747   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
748   return GetAltitudeForPoint( thePoint, aZone );
749 }
750
751 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                    thePoint,
752                                                        const Handle(HYDROData_Region)& theRegion ) const
753 {
754   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
755
756   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
757   if ( !aZone.IsNull() )
758   {
759     //DEBTRACE("GetAltitudeForPoint Region " << theRegion->GetName().toStdString() << " Zone " << aZone->GetName().toStdString());
760     Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
761     if ( IsEqual( aRefRegion, theRegion ) )
762       aResAltitude = GetAltitudeForPoint( thePoint, aZone );
763     else
764       {
765         DEBTRACE("GetAltitudeForPoint Region " << aRefRegion->GetName().toStdString() << " Zone " << aZone->GetName().toStdString() << " ---------------------------");
766         aResAltitude = GetAltitudeForPoint( thePoint, aZone );
767       }
768   }
769   else
770     {
771       DEBTRACE(" --- GetAltitudeForPoint No Zone ---");
772     }
773
774   return aResAltitude;
775 }
776
777 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                  thePoint,
778                                                        const Handle(HYDROData_Zone)& theZone ) const
779 {
780   //DEBTRACE("GetAltitudeForPoint Zone " << theZone->GetName().toStdString());
781   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
782   if ( theZone.IsNull() )
783     return aResAltitude;
784
785   HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
786   if ( !theZone->IsMergingNeed() )
787   {
788     aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
789   }
790   else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
791   {
792     return aResAltitude;
793   }
794
795   HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
796   if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
797   {
798     Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
799     if ( !aMergeAltitude.IsNull() )
800     {
801       if ( aZoneInterpolator != NULL )
802       {
803         aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
804         aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
805       }
806       else
807         aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
808     }
809   }
810   else
811   {
812     HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
813     HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
814     for ( ; anIter.More(); anIter.Next() )
815     {
816       Handle(HYDROData_Object) aZoneObj =
817         Handle(HYDROData_Object)::DownCast( anIter.Value() );
818       if ( aZoneObj.IsNull() )
819         continue;
820
821       Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
822       if ( anObjAltitude.IsNull() )
823         continue;
824
825       double aPointAltitude = 0.0;
826       if ( aZoneInterpolator != NULL )
827       {
828         aZoneInterpolator->SetAltitudeObject( anObjAltitude );
829         aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
830       }
831       else
832         aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
833
834       if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
835         continue;
836
837       if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
838       {
839         aResAltitude = aPointAltitude;
840         break;
841       }
842       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
843       {
844         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
845              aResAltitude > aPointAltitude )
846         {
847           aResAltitude = aPointAltitude;
848         }
849       }
850       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
851       {
852         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
853              aResAltitude < aPointAltitude )
854         {
855           aResAltitude = aPointAltitude;
856         }
857       }
858     }
859   }
860
861   return aResAltitude;
862 }
863
864 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
865   const NCollection_Sequence<gp_XY>& thePoints,
866   const Handle(HYDROData_Region)&    theRegion ) const
867 {
868   //DEBTRACE("HYDROData_CalculationCase::GetAltitudesForPoints " << theRegion->GetName().toStdString());
869   NCollection_Sequence<double> aResSeq;
870
871   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
872   {
873     const gp_XY& thePnt = thePoints.Value( i );
874     
875     double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
876     aResSeq.Append( anAltitude );
877   }
878
879   return aResSeq;
880 }
881
882 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
883   const NCollection_Sequence<gp_XY>& thePoints,
884   const Handle(HYDROData_Zone)&      theZone ) const
885 {
886   NCollection_Sequence<double> aResSeq;
887
888   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
889   {
890     const gp_XY& thePnt = thePoints.Value( i );
891     
892     double anAltitude = GetAltitudeForPoint( thePnt, theZone );
893     aResSeq.Append( anAltitude );
894   }
895
896   return aResSeq;
897 }
898
899 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
900 {
901   Handle(HYDROData_Region) aResRegion;
902
903   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
904   if ( !aZone.IsNull() )
905     aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
906
907   return aResRegion;
908 }
909
910 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
911 {
912   Handle(HYDROData_Zone) aResZone;
913
914   HYDROData_SequenceOfObjects aRegions = GetRegions();
915
916   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
917   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
918   {
919     Handle(HYDROData_Region) aRegion =
920       Handle(HYDROData_Region)::DownCast( anIter.Value() );
921     if ( aRegion.IsNull() )
922       continue;
923
924     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
925     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
926     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
927     {
928       Handle(HYDROData_Zone) aRegZone =
929         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
930       if ( aRegZone.IsNull() )
931         continue;
932
933       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
934       if ( aPointRelation != POINT_OUT )
935         aResZone = aRegZone; // We found the desired zone
936     }
937   }
938
939   return aResZone;
940 }
941
942 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
943   const gp_XY&                  thePoint,
944   const Handle(HYDROData_Zone)& theZone ) const
945 {
946   PointClassification aRes = POINT_OUT;
947   if ( theZone.IsNull() )
948     return aRes;
949
950   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
951   if ( aZoneFace.IsNull() )
952     return aRes;
953 #ifdef DEB_CLASS2D      
954           TopoDS_Compound aCmp;
955       BRep_Builder aBB;
956       aBB.MakeCompound(aCmp);
957           aBB.Add(aCmp, aZoneFace);
958           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
959           BRepBuilderAPI_MakeVertex aMk(aPnt);
960           aBB.Add(aCmp, aMk.Vertex());
961           BRepTools::Write(aCmp, "FCL2d.brep");
962 #endif  
963   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
964   if (State == TopAbs_OUT)
965     aRes =  POINT_OUT;
966   else if(State == TopAbs_IN)
967     aRes =  POINT_IN;
968   else if(State == TopAbs_ON)
969     aRes =  POINT_ON;
970   return aRes;
971 }
972
973 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
974                                                                   const QString& thePrefixOrName,
975                                                                   bool isPrefix )
976 {
977   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
978   int aTag = aNewLab.Tag();
979
980   Handle(HYDROData_Region) aNewRegion =
981     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
982   AddRegion( aNewRegion );
983
984   QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
985   aNewRegion->SetName( aRegionName );
986
987   return aNewRegion;
988 }
989
990 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
991 {
992   TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
993
994   Handle(HYDROData_SplittedShapesGroup) aNewGroup =
995     Handle(HYDROData_SplittedShapesGroup)::DownCast( 
996       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
997   AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
998
999   aNewGroup->SetName( theName );
1000
1001   return aNewGroup;
1002 }
1003
1004 QString HYDROData_CalculationCase::Export( int theStudyId ) const
1005 {
1006   GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
1007   SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
1008   
1009   QString aGeomObjEntry;
1010   bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry );
1011   return isOK ? aGeomObjEntry : QString();
1012 }
1013
1014 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
1015                                         SALOMEDS::Study_ptr theStudy,
1016                                         QString& theGeomObjEntry ) const
1017 {
1018   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
1019
1020   // Get groups definitions
1021   HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
1022
1023   HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
1024   for ( ; anIter.More(); anIter.Next() )
1025   {
1026     // Get shapes group
1027     Handle(HYDROData_ShapesGroup) aGroup =
1028       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
1029     if ( aGroup.IsNull() )
1030       continue;
1031
1032     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
1033
1034     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
1035     aGroup->GetShapes( aGroupDef.Shapes );
1036
1037     aSeqOfGroupsDefs.Append( aGroupDef );
1038   }
1039   
1040   // Get faces
1041   TopTools_ListOfShape aFaces;
1042   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
1043   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
1044   for ( ; aRegionIter.More(); aRegionIter.Next() )
1045   {
1046     Handle(HYDROData_Region) aRegion =
1047       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
1048     if( aRegion.IsNull() || !aRegion->IsSubmersible() )
1049       continue;
1050
1051     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
1052     aFaces.Append( aRegionShape );
1053   }
1054
1055   return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );
1056 }
1057
1058 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
1059                                         SALOMEDS::Study_ptr                           theStudy,
1060                                         const TopTools_ListOfShape&                   theFaces,
1061                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
1062                                         QString& theGeomObjEntry ) const
1063 {
1064   // Sew faces
1065   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
1066   aSewing.SetNonManifoldMode( Standard_False );
1067 #ifdef DEB_CALCULATION
1068   TCollection_AsciiString aNam("Sh_");
1069   int i=1;
1070 #endif
1071   TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
1072   for ( ; aFaceIter.More(); aFaceIter.Next() )
1073   {
1074     TopoDS_Shape aShape = aFaceIter.Value();
1075     if ( aShape.IsNull() )
1076       continue;
1077
1078     if ( aShape.ShapeType() == TopAbs_FACE )
1079     {
1080       aSewing.Add( aShape );
1081 #ifdef DEB_CALCULATION
1082       TCollection_AsciiString aName = aNam + ++i + ".brep";
1083       BRepTools::Write(aShape ,aName.ToCString());
1084 #endif
1085     }
1086     else
1087     {
1088 #ifdef DEB_CALCULATION
1089       int j = 1;
1090 #endif
1091       TopExp_Explorer anExp( aShape, TopAbs_FACE );
1092       for (; anExp.More(); anExp.Next() ) {
1093         aSewing.Add( anExp.Current() );
1094 #ifdef DEB_CALCULATION
1095
1096         TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
1097         BRepTools::Write(anExp.Current() ,aName.ToCString());
1098 #endif
1099       }
1100     }
1101   } // faces iterator
1102   
1103   aSewing.Perform();
1104   TopoDS_Shape aSewedShape = aSewing.SewedShape();
1105
1106   // If the sewed shape is empty - return false
1107   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1108     return false;
1109
1110 #ifdef DEB_CALCULATION
1111   BRepTools::Write(aSewedShape ,"Sew.brep");
1112 #endif
1113   // Publish the sewed shape
1114   QString aName = EXPORT_NAME;
1115   GEOM::GEOM_Object_ptr aMainShape = 
1116     HYDROData_GeomTool::publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
1117
1118   if ( aMainShape->_is_nil() )  
1119     return false;
1120
1121   if ( theGroupsDefs.IsEmpty() )
1122     return true;
1123
1124   // Create groups
1125   TopTools_IndexedMapOfShape aMapOfSubShapes;
1126   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1127
1128   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1129
1130   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1131   {
1132     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1133
1134     NCollection_Sequence<int> aGroupIndexes;
1135     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1136     {
1137       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1138 #ifdef DEB_CALCULATION
1139       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1140 #endif
1141       
1142       TopoDS_Shape aModifiedShape = aShape;
1143       if ( aSewing.IsModified( aShape ) )
1144         aModifiedShape = aSewing.Modified( aShape );
1145       else if ( aSewing.IsModifiedSubShape( aShape ) )
1146         aModifiedShape = aSewing.ModifiedSubShape( aShape );
1147
1148 #ifdef DEB_CALCULATION
1149       const TopLoc_Location& aL1 = aShape.Location();
1150       const TopLoc_Location& aL2 = aModifiedShape.Location();
1151       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1152 #endif
1153
1154       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1155       if ( anIndex > 0 ) {
1156         aGroupIndexes.Append( anIndex );
1157       } else {
1158 #ifdef DEB_CALCULATION    
1159         TCollection_AsciiString aNam("Lost_");
1160         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1161         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1162         {
1163            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1164            if ( aModifiedShape.IsPartner( aS ) )
1165            {
1166              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1167              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1168              BRepTools::Write(aS ,aName.ToCString());
1169             break;
1170            }
1171          }
1172         }
1173 #endif
1174       }
1175     }
1176     if ( !aGroupIndexes.IsEmpty() )
1177       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1178   }
1179  
1180   if ( !aGroupsData.IsEmpty() )
1181   {
1182     GEOM::GEOM_IGroupOperations_var aGroupOp = 
1183       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
1184
1185     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1186     for ( ; aMapIt.More(); aMapIt.Next() )
1187     {
1188       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
1189       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1190
1191       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1192       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1193         continue;
1194
1195       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1196       aGeomIndexes->length( aGroupIndexes.Length() );
1197
1198       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1199         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1200
1201       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1202       if ( aGroupOp->IsDone() )
1203       {
1204         SALOMEDS::SObject_var aGroupSO = 
1205           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1206       }
1207     }
1208   }
1209
1210   return true;
1211 }
1212
1213 void HYDROData_CalculationCase::ClearRules( HYDROData_CalculationCase::DataTag theDataTag,
1214                                             const bool theIsSetToUpdate )
1215 {
1216   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1217   HYDROData_PriorityQueue::ClearRules( aRulesLab );
1218
1219   // Indicate model of the need to update splitting
1220   if ( theIsSetToUpdate ) {
1221     SetToUpdate( true );
1222   }
1223 }
1224
1225 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Entity)&    theObject1,
1226                                          HYDROData_PriorityType             thePriority,
1227                                          const Handle(HYDROData_Entity)&    theObject2,
1228                                          HYDROData_Zone::MergeAltitudesType theMergeType,
1229                                          HYDROData_CalculationCase::DataTag theDataTag )
1230 {
1231   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1232   HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1233
1234   // Indicate model of the need to update splitting
1235   SetToUpdate( true );
1236 }
1237
1238 QString HYDROData_CalculationCase::DumpRules() const
1239 {
1240   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1241   return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1242 }
1243
1244 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1245 {
1246   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1247   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1248
1249   // Indicate model of the need to update splitting
1250   SetToUpdate( true );
1251 }
1252
1253 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1254 {
1255   Handle(TDataStd_Integer) aModeAttr;
1256   bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1257   if( isOK )
1258     return ( AssignmentMode ) aModeAttr->Get();
1259   else
1260     return MANUAL;
1261 }
1262
1263 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1264                                                    QStringList& theScript ) const
1265 {
1266   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1267   HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1268 }
1269
1270 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1271 {
1272   return myLastWarning;
1273 }
1274
1275 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1276 {
1277   myLastWarning.Type = theType;
1278   myLastWarning.Data = theData;
1279 }
1280
1281 bool HYDROData_CalculationCase::GetRule( int theIndex, 
1282                                          Handle(HYDROData_Entity)&           theObject1,
1283                                          HYDROData_PriorityType&             thePriority,
1284                                          Handle(HYDROData_Entity)&           theObject2,
1285                                          HYDROData_Zone::MergeAltitudesType& theMergeType,
1286                                          HYDROData_CalculationCase::DataTag& theDataTag) const
1287 {
1288   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1289   return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1290     theObject1, thePriority, theObject2, theMergeType );
1291 }
1292
1293 void HYDROData_CalculationCase::SetAssignmentLandCoverMode( AssignmentMode theMode )
1294 {
1295   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentLandCoverMode );
1296   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1297
1298   // Indicate model of the need to update land covers partition
1299   SetToUpdate( true );
1300 }
1301
1302 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentLandCoverMode() const
1303 {
1304   Handle(TDataStd_Integer) aModeAttr;
1305   bool isOK = myLab.FindChild( DataTag_AssignmentLandCoverMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1306   if( isOK )
1307     return ( AssignmentMode ) aModeAttr->Get();
1308   else
1309     return MANUAL;
1310 }