Salome HOME
1bd1057d6dd42ccc548a86640b9901e5ff4e2f9b
[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   aRegions.Append( GetRegions( true ) );
694
695   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
696   for ( ; anIter.More(); anIter.Next() )
697   {
698     Handle(HYDROData_Region) aRegion =
699       Handle(HYDROData_Region)::DownCast( anIter.Value() );
700     if ( aRegion.IsNull() )
701       continue;
702
703     aRegion->SetName( "" );
704   }
705
706   QString aRegsPref = CALCULATION_REGIONS_PREF;
707
708   anIter.Init( aRegions );
709   for ( ; anIter.More(); anIter.Next() )
710   {
711     Handle(HYDROData_Region) aRegion =
712       Handle(HYDROData_Region)::DownCast( anIter.Value() );
713     if ( aRegion.IsNull() )
714       continue;
715
716     QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
717     aRegion->SetName( aRegionName );
718   }
719 }
720
721 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion,
722                                               const bool theLandCover )
723 {
724   if ( theRegion.IsNull() )
725     return;
726
727   HYDROData_CalculationCase::DataTag aDataTag = 
728     theLandCover ? DataTag_LandCoverRegion : DataTag_Region;
729   RemoveReferenceObject( theRegion->Label(), aDataTag );
730
731   // Remove region from data model
732   Handle(HYDROData_CalculationCase) aFatherCalc = 
733     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
734   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
735     theRegion->Remove();
736 }
737
738 void HYDROData_CalculationCase::RemoveRegions( const bool theLandCover )
739 {
740   myLab.FindChild( theLandCover ? DataTag_ChildLandCoverRegion : DataTag_ChildRegion ).ForgetAllAttributes();
741 }
742
743 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
744 {
745   return GetReferenceObjects( DataTag_SplittedGroups );
746 }
747
748 void HYDROData_CalculationCase::RemoveSplittedGroups()
749 {
750   myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
751 }
752
753 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
754 {
755   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint, false );
756   return GetAltitudeForPoint( thePoint, aZone );
757 }
758
759 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                    thePoint,
760                                                        const Handle(HYDROData_Region)& theRegion ) const
761 {
762   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
763
764   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint, false );
765   if ( !aZone.IsNull() )
766   {
767     Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
768     if ( IsEqual( aRefRegion, theRegion ) )
769       aResAltitude = GetAltitudeForPoint( thePoint, aZone );
770   }
771
772   return aResAltitude;
773 }
774
775 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                  thePoint,
776                                                        const Handle(HYDROData_Zone)& theZone ) const
777 {
778   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
779   if ( theZone.IsNull() )
780     return aResAltitude;
781
782   HYDROData_Zone::MergeType aZoneMergeType = theZone->GetMergeType();
783   if ( !theZone->IsMergingNeed() )
784   {
785     aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
786   }
787   else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
788   {
789     return aResAltitude;
790   }
791
792   HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
793   if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
794   {
795     Handle(HYDROData_IAltitudeObject) aMergeAltitude = 
796       Handle(HYDROData_IAltitudeObject)::DownCast( theZone->GetMergeObject() );
797     if ( !aMergeAltitude.IsNull() )
798     {
799       if ( aZoneInterpolator != NULL )
800       {
801         aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
802         aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
803       }
804       else
805         aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
806     }
807   }
808   else
809   {
810     HYDROData_SequenceOfObjects aZoneObjects = theZone->GetObjects();
811     HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
812     for ( ; anIter.More(); anIter.Next() )
813     {
814       Handle(HYDROData_Object) aZoneObj =
815         Handle(HYDROData_Object)::DownCast( anIter.Value() );
816       if ( aZoneObj.IsNull() )
817         continue;
818
819       Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
820       if ( anObjAltitude.IsNull() )
821         continue;
822
823       double aPointAltitude = 0.0;
824       if ( aZoneInterpolator != NULL )
825       {
826         aZoneInterpolator->SetAltitudeObject( anObjAltitude );
827         aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
828       }
829       else
830         aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
831
832       if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
833         continue;
834
835       if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
836       {
837         aResAltitude = aPointAltitude;
838         break;
839       }
840       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
841       {
842         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
843              aResAltitude > aPointAltitude )
844         {
845           aResAltitude = aPointAltitude;
846         }
847       }
848       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
849       {
850         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
851              aResAltitude < aPointAltitude )
852         {
853           aResAltitude = aPointAltitude;
854         }
855       }
856     }
857   }
858
859   return aResAltitude;
860 }
861
862 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
863   const NCollection_Sequence<gp_XY>& thePoints,
864   const Handle(HYDROData_Region)&    theRegion ) const
865 {
866   NCollection_Sequence<double> aResSeq;
867
868   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
869   {
870     const gp_XY& thePnt = thePoints.Value( i );
871     
872     double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
873     aResSeq.Append( anAltitude );
874   }
875
876   return aResSeq;
877 }
878
879 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
880   const NCollection_Sequence<gp_XY>& thePoints,
881   const Handle(HYDROData_Zone)&      theZone ) const
882 {
883   NCollection_Sequence<double> aResSeq;
884
885   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
886   {
887     const gp_XY& thePnt = thePoints.Value( i );
888     
889     double anAltitude = GetAltitudeForPoint( thePnt, theZone );
890     aResSeq.Append( anAltitude );
891   }
892
893   return aResSeq;
894 }
895
896 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint,
897                                                                         const bool theLandCover ) const
898 {
899   Handle(HYDROData_Region) aResRegion;
900
901   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint, theLandCover );
902   if ( !aZone.IsNull() )
903     aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
904
905   return aResRegion;
906 }
907
908 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint,
909                                                                     const bool theLandCover ) const
910 {
911   Handle(HYDROData_Zone) aResZone;
912
913   HYDROData_SequenceOfObjects aRegions = GetRegions( theLandCover );
914
915   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
916   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
917   {
918     Handle(HYDROData_Region) aRegion =
919       Handle(HYDROData_Region)::DownCast( anIter.Value() );
920     if ( aRegion.IsNull() )
921       continue;
922
923     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
924     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
925     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
926     {
927       Handle(HYDROData_Zone) aRegZone =
928         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
929       if ( aRegZone.IsNull() )
930         continue;
931
932       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
933       if ( aPointRelation != POINT_OUT )
934         aResZone = aRegZone; // We found the desired zone
935     }
936   }
937
938   return aResZone;
939 }
940
941 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
942   const gp_XY&                  thePoint,
943   const Handle(HYDROData_Zone)& theZone ) const
944 {
945   PointClassification aRes = POINT_OUT;
946   if ( theZone.IsNull() )
947     return aRes;
948
949   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
950   if ( aZoneFace.IsNull() )
951     return aRes;
952 #ifdef DEB_CLASS2D      
953           TopoDS_Compound aCmp;
954       BRep_Builder aBB;
955       aBB.MakeCompound(aCmp);
956           aBB.Add(aCmp, aZoneFace);
957           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
958           BRepBuilderAPI_MakeVertex aMk(aPnt);
959           aBB.Add(aCmp, aMk.Vertex());
960           BRepTools::Write(aCmp, "FCL2d.brep");
961 #endif  
962   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
963   if (State == TopAbs_OUT)
964     aRes =  POINT_OUT;
965   else if(State == TopAbs_IN)
966     aRes =  POINT_IN;
967   else if(State == TopAbs_ON)
968     aRes =  POINT_ON;
969   return aRes;
970 }
971
972 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
973                                                                   const QString& thePrefixOrName,
974                                                                   const bool theLandCover,
975                                                                   bool isPrefix )
976 {
977   TDF_Label aNewLab = myLab.FindChild( theLandCover ? DataTag_ChildLandCoverRegion : 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, theLandCover );
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( false );
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::MergeType          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 void HYDROData_CalculationCase::UpdateRegionsNames( const HYDROData_SequenceOfObjects& theRegions,
1282                                                     const QString& theOldCaseName,
1283                                                     const QString& theName )
1284 {
1285   HYDROData_SequenceOfObjects::Iterator anIter( theRegions );
1286   for ( ; anIter.More(); anIter.Next() )
1287   {
1288     Handle(HYDROData_Region) aRegion =
1289       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1290     if ( aRegion.IsNull() )
1291       continue;
1292
1293     HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegion );
1294
1295     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
1296     HYDROData_SequenceOfObjects::Iterator anIter( aZones );
1297     for ( ; anIter.More(); anIter.Next() )
1298     {
1299       Handle(HYDROData_Zone) aRegZone =
1300         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1301       if ( aRegZone.IsNull() )
1302         continue;
1303
1304       HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegZone );
1305     }
1306   }
1307 }
1308
1309 void HYDROData_CalculationCase::DumpRegionsToPython( QStringList& theResList,
1310                                                      MapOfTreatedObjects& theTreatedObjects,
1311                                                      const HYDROData_SequenceOfObjects& theRegions ) const
1312 {
1313   HYDROData_SequenceOfObjects::Iterator anIter;
1314   anIter.Init( theRegions );
1315   for ( ; anIter.More(); anIter.Next() )
1316   {
1317     Handle(HYDROData_Region) aRegion =
1318       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1319     if ( aRegion.IsNull() )
1320       continue;
1321
1322     theTreatedObjects.insert( aRegion->GetName(), aRegion );
1323     QStringList aRegDump = aRegion->DumpToPython( theTreatedObjects );
1324     theResList << aRegDump;
1325   }
1326 }
1327
1328 bool HYDROData_CalculationCase::GetRule( int theIndex, 
1329                                          Handle(HYDROData_Entity)&           theObject1,
1330                                          HYDROData_PriorityType&             thePriority,
1331                                          Handle(HYDROData_Entity)&           theObject2,
1332                                          HYDROData_Zone::MergeType&          theMergeType,
1333                                          HYDROData_CalculationCase::DataTag& theDataTag) const
1334 {
1335   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1336   return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1337     theObject1, thePriority, theObject2, theMergeType );
1338 }
1339
1340 void HYDROData_CalculationCase::SetAssignmentLandCoverMode( AssignmentMode theMode )
1341 {
1342   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentLandCoverMode );
1343   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1344
1345   // Indicate model of the need to update land covers partition
1346   SetToUpdate( true );
1347 }
1348
1349 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentLandCoverMode() const
1350 {
1351   Handle(TDataStd_Integer) aModeAttr;
1352   bool isOK = myLab.FindChild( DataTag_AssignmentLandCoverMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1353   if( isOK )
1354     return ( AssignmentMode ) aModeAttr->Get();
1355   else
1356     return MANUAL;
1357 }