]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_CalculationCase.cxx
Salome HOME
f8640f813db6e919e8599a72bc4114aa716b3894
[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 double HYDROData_CalculationCase::GetStricklerCoefficientForPoint( const gp_XY& thePoint ) const
897 {
898     double aCoeff = 0;
899     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
900     if ( !aDocument.IsNull() )
901         aCoeff = aDocument->GetDefaultStricklerCoefficient();
902
903     Handle(HYDROData_LandCover) aLandCover;
904     Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint, Standard_True );
905     if ( !aZone.IsNull() )
906     {
907         HYDROData_SequenceOfObjects anObjList = aZone->GetObjects();
908         if ( anObjList.Length() == 1 )
909             aLandCover = Handle(HYDROData_LandCover)::DownCast( anObjList.First() );
910         else
911             aLandCover = Handle(HYDROData_LandCover)::DownCast( aZone->GetMergeObject() );
912     }
913
914     if ( !aLandCover.IsNull() )
915     {
916         QString aType = aLandCover->GetStricklerType();
917         Handle(HYDROData_StricklerTable) aTable = GetStricklerTable();
918         if ( !aTable.IsNull() )
919         {
920             if ( aTable->GetTypes().contains( aType ) )
921                 aCoeff = aTable->Get( aType, aCoeff );
922         }
923     }
924
925     return aCoeff;
926 }
927
928 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint,
929                                                                         const bool theLandCover ) const
930 {
931   Handle(HYDROData_Region) aResRegion;
932
933   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint, theLandCover );
934   if ( !aZone.IsNull() )
935     aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
936
937   return aResRegion;
938 }
939
940 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint,
941                                                                     const bool theLandCover ) const
942 {
943   Handle(HYDROData_Zone) aResZone;
944
945   HYDROData_SequenceOfObjects aRegions = GetRegions( theLandCover );
946
947   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
948   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
949   {
950     Handle(HYDROData_Region) aRegion =
951       Handle(HYDROData_Region)::DownCast( anIter.Value() );
952     if ( aRegion.IsNull() )
953       continue;
954
955     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
956     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
957     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
958     {
959       Handle(HYDROData_Zone) aRegZone =
960         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
961       if ( aRegZone.IsNull() )
962         continue;
963
964       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
965       if ( aPointRelation != POINT_OUT )
966         aResZone = aRegZone; // We found the desired zone
967     }
968   }
969
970   return aResZone;
971 }
972
973 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
974   const gp_XY&                  thePoint,
975   const Handle(HYDROData_Zone)& theZone ) const
976 {
977   PointClassification aRes = POINT_OUT;
978   if ( theZone.IsNull() )
979     return aRes;
980
981   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
982   if ( aZoneFace.IsNull() )
983     return aRes;
984 #ifdef DEB_CLASS2D      
985           TopoDS_Compound aCmp;
986       BRep_Builder aBB;
987       aBB.MakeCompound(aCmp);
988           aBB.Add(aCmp, aZoneFace);
989           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
990           BRepBuilderAPI_MakeVertex aMk(aPnt);
991           aBB.Add(aCmp, aMk.Vertex());
992           BRepTools::Write(aCmp, "FCL2d.brep");
993 #endif  
994   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
995   if (State == TopAbs_OUT)
996     aRes =  POINT_OUT;
997   else if(State == TopAbs_IN)
998     aRes =  POINT_IN;
999   else if(State == TopAbs_ON)
1000     aRes =  POINT_ON;
1001   return aRes;
1002 }
1003
1004 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
1005                                                                   const QString& thePrefixOrName,
1006                                                                   const bool theLandCover,
1007                                                                   bool isPrefix )
1008 {
1009   TDF_Label aNewLab = myLab.FindChild( theLandCover ? DataTag_ChildLandCoverRegion : DataTag_ChildRegion ).NewChild();
1010   int aTag = aNewLab.Tag();
1011
1012   Handle(HYDROData_Region) aNewRegion =
1013     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
1014   AddRegion( aNewRegion, theLandCover );
1015
1016   QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
1017   aNewRegion->SetName( aRegionName );
1018
1019   return aNewRegion;
1020 }
1021
1022 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
1023 {
1024   TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
1025
1026   Handle(HYDROData_SplittedShapesGroup) aNewGroup =
1027     Handle(HYDROData_SplittedShapesGroup)::DownCast( 
1028       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
1029   AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
1030
1031   aNewGroup->SetName( theName );
1032
1033   return aNewGroup;
1034 }
1035
1036 QString HYDROData_CalculationCase::Export( int theStudyId ) const
1037 {
1038   GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
1039   SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
1040   
1041   QString aGeomObjEntry;
1042   bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry );
1043   return isOK ? aGeomObjEntry : QString();
1044 }
1045
1046 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
1047                                         SALOMEDS::Study_ptr theStudy,
1048                                         QString& theGeomObjEntry ) const
1049 {
1050   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
1051
1052   // Get groups definitions
1053   HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
1054
1055   HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
1056   for ( ; anIter.More(); anIter.Next() )
1057   {
1058     // Get shapes group
1059     Handle(HYDROData_ShapesGroup) aGroup =
1060       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
1061     if ( aGroup.IsNull() )
1062       continue;
1063
1064     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
1065
1066     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
1067     aGroup->GetShapes( aGroupDef.Shapes );
1068
1069     aSeqOfGroupsDefs.Append( aGroupDef );
1070   }
1071   
1072   // Get faces
1073   TopTools_ListOfShape aFaces;
1074   HYDROData_SequenceOfObjects aCaseRegions = GetRegions( false );
1075   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
1076   for ( ; aRegionIter.More(); aRegionIter.Next() )
1077   {
1078     Handle(HYDROData_Region) aRegion =
1079       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
1080     if( aRegion.IsNull() || !aRegion->IsSubmersible() )
1081       continue;
1082
1083     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
1084     aFaces.Append( aRegionShape );
1085   }
1086
1087   return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );
1088 }
1089
1090 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
1091                                         SALOMEDS::Study_ptr                           theStudy,
1092                                         const TopTools_ListOfShape&                   theFaces,
1093                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
1094                                         QString& theGeomObjEntry ) const
1095 {
1096   // Sew faces
1097   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
1098   aSewing.SetNonManifoldMode( Standard_False );
1099 #ifdef DEB_CALCULATION
1100   TCollection_AsciiString aNam("Sh_");
1101   int i=1;
1102 #endif
1103   TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
1104   for ( ; aFaceIter.More(); aFaceIter.Next() )
1105   {
1106     TopoDS_Shape aShape = aFaceIter.Value();
1107     if ( aShape.IsNull() )
1108       continue;
1109
1110     if ( aShape.ShapeType() == TopAbs_FACE )
1111     {
1112       aSewing.Add( aShape );
1113 #ifdef DEB_CALCULATION
1114       TCollection_AsciiString aName = aNam + ++i + ".brep";
1115       BRepTools::Write(aShape ,aName.ToCString());
1116 #endif
1117     }
1118     else
1119     {
1120 #ifdef DEB_CALCULATION
1121       int j = 1;
1122 #endif
1123       TopExp_Explorer anExp( aShape, TopAbs_FACE );
1124       for (; anExp.More(); anExp.Next() ) {
1125         aSewing.Add( anExp.Current() );
1126 #ifdef DEB_CALCULATION
1127
1128         TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
1129         BRepTools::Write(anExp.Current() ,aName.ToCString());
1130 #endif
1131       }
1132     }
1133   } // faces iterator
1134   
1135   aSewing.Perform();
1136   TopoDS_Shape aSewedShape = aSewing.SewedShape();
1137
1138   // If the sewed shape is empty - return false
1139   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1140     return false;
1141
1142 #ifdef DEB_CALCULATION
1143   BRepTools::Write(aSewedShape ,"Sew.brep");
1144 #endif
1145   // Publish the sewed shape
1146   QString aName = EXPORT_NAME;
1147   GEOM::GEOM_Object_ptr aMainShape = 
1148     publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
1149
1150   if ( aMainShape->_is_nil() )  
1151     return false;
1152
1153   if ( theGroupsDefs.IsEmpty() )
1154     return true;
1155
1156   // Create groups
1157   TopTools_IndexedMapOfShape aMapOfSubShapes;
1158   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1159
1160   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1161
1162   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1163   {
1164     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1165
1166     NCollection_Sequence<int> aGroupIndexes;
1167     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1168     {
1169       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1170 #ifdef DEB_CALCULATION
1171       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1172 #endif
1173       
1174       TopoDS_Shape aModifiedShape = aShape;
1175       if ( aSewing.IsModified( aShape ) )
1176         aModifiedShape = aSewing.Modified( aShape );
1177       else if ( aSewing.IsModifiedSubShape( aShape ) )
1178         aModifiedShape = aSewing.ModifiedSubShape( aShape );
1179
1180 #ifdef DEB_CALCULATION
1181       const TopLoc_Location& aL1 = aShape.Location();
1182       const TopLoc_Location& aL2 = aModifiedShape.Location();
1183       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1184 #endif
1185
1186       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1187       if ( anIndex > 0 ) {
1188         aGroupIndexes.Append( anIndex );
1189       } else {
1190 #ifdef DEB_CALCULATION    
1191         TCollection_AsciiString aNam("Lost_");
1192         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1193         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1194         {
1195            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1196            if ( aModifiedShape.IsPartner( aS ) )
1197            {
1198              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1199              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1200              BRepTools::Write(aS ,aName.ToCString());
1201             break;
1202            }
1203          }
1204         }
1205 #endif
1206       }
1207     }
1208     if ( !aGroupIndexes.IsEmpty() )
1209       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1210   }
1211  
1212   if ( !aGroupsData.IsEmpty() )
1213   {
1214     GEOM::GEOM_IGroupOperations_var aGroupOp = 
1215       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
1216
1217     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1218     for ( ; aMapIt.More(); aMapIt.Next() )
1219     {
1220       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
1221       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1222
1223       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1224       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1225         continue;
1226
1227       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1228       aGeomIndexes->length( aGroupIndexes.Length() );
1229
1230       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1231         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1232
1233       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1234       if ( aGroupOp->IsDone() )
1235       {
1236         SALOMEDS::SObject_var aGroupSO = 
1237           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1238       }
1239     }
1240   }
1241
1242   return true;
1243 }
1244
1245 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM( 
1246   GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
1247   const TopoDS_Shape& theShape, const QString& theName,
1248   QString& theGeomObjEntry ) const
1249 {
1250   theGeomObjEntry = "";
1251   GEOM::GEOM_Object_var aGeomObj;
1252
1253   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
1254        theShape.IsNull() ) {
1255     return aGeomObj._retn();
1256   }
1257
1258   std::ostringstream aStreamShape;
1259   // Write TopoDS_Shape in ASCII format to the stream
1260   BRepTools::Write( theShape, aStreamShape );
1261   // Returns the number of bytes that have been stored in the stream's buffer.
1262   int aSize = aStreamShape.str().size();
1263   // Allocate octect buffer of required size
1264   CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
1265   // Copy ostrstream content to the octect buffer
1266   memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
1267   // Create TMPFile
1268   SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
1269
1270   // Restore shape from the stream and get the GEOM object
1271   GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
1272   aGeomObj = anInsOp->RestoreShape( aSeqFile );
1273   
1274   // Puplish the GEOM object
1275   if ( !aGeomObj->_is_nil() ) {
1276     QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
1277
1278     SALOMEDS::SObject_var aResultSO = 
1279       theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), 
1280                                      aGeomObj, qPrintable( aName ) );
1281     if ( aResultSO->_is_nil() ) {
1282       aGeomObj = GEOM::GEOM_Object::_nil();
1283     }
1284     else
1285       theGeomObjEntry = aResultSO->GetID();
1286   }
1287
1288   return aGeomObj._retn();
1289 }
1290
1291 void HYDROData_CalculationCase::ClearRules( HYDROData_CalculationCase::DataTag theDataTag,
1292                                             const bool theIsSetToUpdate )
1293 {
1294   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1295   HYDROData_PriorityQueue::ClearRules( aRulesLab );
1296
1297   // Indicate model of the need to update splitting
1298   if ( theIsSetToUpdate ) {
1299     SetToUpdate( true );
1300   }
1301 }
1302
1303 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Entity)&    theObject1,
1304                                          HYDROData_PriorityType             thePriority,
1305                                          const Handle(HYDROData_Entity)&    theObject2,
1306                                          HYDROData_Zone::MergeType          theMergeType,
1307                                          HYDROData_CalculationCase::DataTag theDataTag )
1308 {
1309   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1310   HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1311
1312   // Indicate model of the need to update splitting
1313   SetToUpdate( true );
1314 }
1315
1316 QString HYDROData_CalculationCase::DumpRules() const
1317 {
1318   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1319   return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1320 }
1321
1322 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1323 {
1324   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1325   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1326
1327   // Indicate model of the need to update splitting
1328   SetToUpdate( true );
1329 }
1330
1331 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1332 {
1333   Handle(TDataStd_Integer) aModeAttr;
1334   bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1335   if( isOK )
1336     return ( AssignmentMode ) aModeAttr->Get();
1337   else
1338     return MANUAL;
1339 }
1340
1341 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1342                                                    QStringList& theScript ) const
1343 {
1344   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1345   HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1346 }
1347
1348 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1349 {
1350   return myLastWarning;
1351 }
1352
1353 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1354 {
1355   myLastWarning.Type = theType;
1356   myLastWarning.Data = theData;
1357 }
1358
1359 void HYDROData_CalculationCase::UpdateRegionsNames( const HYDROData_SequenceOfObjects& theRegions,
1360                                                     const QString& theOldCaseName,
1361                                                     const QString& theName )
1362 {
1363   HYDROData_SequenceOfObjects::Iterator anIter( theRegions );
1364   for ( ; anIter.More(); anIter.Next() )
1365   {
1366     Handle(HYDROData_Region) aRegion =
1367       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1368     if ( aRegion.IsNull() )
1369       continue;
1370
1371     HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegion );
1372
1373     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
1374     HYDROData_SequenceOfObjects::Iterator anIter( aZones );
1375     for ( ; anIter.More(); anIter.Next() )
1376     {
1377       Handle(HYDROData_Zone) aRegZone =
1378         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1379       if ( aRegZone.IsNull() )
1380         continue;
1381
1382       HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegZone );
1383     }
1384   }
1385 }
1386
1387 void HYDROData_CalculationCase::DumpRegionsToPython( QStringList& theResList,
1388                                                      MapOfTreatedObjects& theTreatedObjects,
1389                                                      const HYDROData_SequenceOfObjects& theRegions ) const
1390 {
1391   HYDROData_SequenceOfObjects::Iterator anIter;
1392   anIter.Init( theRegions );
1393   for ( ; anIter.More(); anIter.Next() )
1394   {
1395     Handle(HYDROData_Region) aRegion =
1396       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1397     if ( aRegion.IsNull() )
1398       continue;
1399
1400     theTreatedObjects.insert( aRegion->GetName(), aRegion );
1401     QStringList aRegDump = aRegion->DumpToPython( theTreatedObjects );
1402     theResList << aRegDump;
1403   }
1404 }
1405
1406 bool HYDROData_CalculationCase::GetRule( int theIndex, 
1407                                          Handle(HYDROData_Entity)&           theObject1,
1408                                          HYDROData_PriorityType&             thePriority,
1409                                          Handle(HYDROData_Entity)&           theObject2,
1410                                          HYDROData_Zone::MergeType&          theMergeType,
1411                                          HYDROData_CalculationCase::DataTag& theDataTag) const
1412 {
1413   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1414   return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1415     theObject1, thePriority, theObject2, theMergeType );
1416 }
1417
1418 void HYDROData_CalculationCase::SetAssignmentLandCoverMode( AssignmentMode theMode )
1419 {
1420   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentLandCoverMode );
1421   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1422
1423   // Indicate model of the need to update land covers partition
1424   SetToUpdate( true );
1425 }
1426
1427 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentLandCoverMode() const
1428 {
1429   Handle(TDataStd_Integer) aModeAttr;
1430   bool isOK = myLab.FindChild( DataTag_AssignmentLandCoverMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1431   if( isOK )
1432     return ( AssignmentMode ) aModeAttr->Get();
1433   else
1434     return MANUAL;
1435 }