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