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