Salome HOME
refs #651: wrapping land cover map by Python
[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   // TODO
582   Handle(HYDROData_LandCoverMap) aLandCoverMap = NULL;
583   return aLandCoverMap;
584 }
585
586 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
587 {
588   Changed( Geom_No );
589   Changed( Geom_No );
590   Changed( Geom_No );
591   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
592   Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
593   if ( aNewRegion.IsNull() )
594     return aNewRegion;
595
596   aNewRegion->AddZone( theZone );
597
598   return aNewRegion;
599 }
600
601 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
602 {
603   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
604
605   if ( theRegion.IsNull() )
606     return false;
607   
608   HYDROData_CalculationCase::DataTag aDataTag = DataTag_Region;
609
610   if ( HasReference( theRegion, aDataTag ) )
611     return false; // Object is already in reference list
612
613   // Move the region from other calculation
614   Handle(HYDROData_CalculationCase) aFatherCalc = 
615     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
616   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
617   {
618     Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
619     theRegion->CopyTo( aNewRegion, true );
620
621     aFatherCalc->RemoveRegion( theRegion );
622
623     theRegion->SetLabel( aNewRegion->Label() );
624   }
625   else
626   {
627     AddReferenceObject( theRegion, aDataTag );
628   }
629
630   return true;
631 }
632
633 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
634 {
635   return GetReferenceObjects( DataTag_Region );
636 }
637
638 void HYDROData_CalculationCase::UpdateRegionsOrder()
639 {
640   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
641   if ( aDocument.IsNull() )
642     return;
643
644   HYDROData_SequenceOfObjects aRegions = GetRegions();
645   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
646   for ( ; anIter.More(); anIter.Next() )
647   {
648     Handle(HYDROData_Region) aRegion =
649       Handle(HYDROData_Region)::DownCast( anIter.Value() );
650     if ( aRegion.IsNull() )
651       continue;
652
653     aRegion->SetName( "" );
654   }
655
656   QString aRegsPref = CALCULATION_REGIONS_PREF;
657
658   anIter.Init( aRegions );
659   for ( ; anIter.More(); anIter.Next() )
660   {
661     Handle(HYDROData_Region) aRegion =
662       Handle(HYDROData_Region)::DownCast( anIter.Value() );
663     if ( aRegion.IsNull() )
664       continue;
665
666     QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
667     aRegion->SetName( aRegionName );
668   }
669 }
670
671 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
672 {
673   if ( theRegion.IsNull() )
674     return;
675
676   HYDROData_CalculationCase::DataTag aDataTag = DataTag_Region;
677   RemoveReferenceObject( theRegion->Label(), aDataTag );
678
679   // Remove region from data model
680   Handle(HYDROData_CalculationCase) aFatherCalc = 
681     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
682   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
683     theRegion->Remove();
684 }
685
686 void HYDROData_CalculationCase::RemoveRegions()
687 {
688   myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
689 }
690
691 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
692 {
693   return GetReferenceObjects( DataTag_SplittedGroups );
694 }
695
696 void HYDROData_CalculationCase::RemoveSplittedGroups()
697 {
698   myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
699 }
700
701 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
702 {
703   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
704   return GetAltitudeForPoint( thePoint, aZone );
705 }
706
707 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                    thePoint,
708                                                        const Handle(HYDROData_Region)& theRegion ) const
709 {
710   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
711
712   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
713   if ( !aZone.IsNull() )
714   {
715     Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
716     if ( IsEqual( aRefRegion, theRegion ) )
717       aResAltitude = GetAltitudeForPoint( thePoint, aZone );
718   }
719
720   return aResAltitude;
721 }
722
723 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                  thePoint,
724                                                        const Handle(HYDROData_Zone)& theZone ) const
725 {
726   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
727   if ( theZone.IsNull() )
728     return aResAltitude;
729
730   HYDROData_Zone::MergeType aZoneMergeType = theZone->GetMergeType();
731   if ( !theZone->IsMergingNeed() )
732   {
733     aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
734   }
735   else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
736   {
737     return aResAltitude;
738   }
739
740   HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
741   if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
742   {
743     Handle(HYDROData_IAltitudeObject) aMergeAltitude = 
744       Handle(HYDROData_IAltitudeObject)::DownCast( theZone->GetMergeObject() );
745     if ( !aMergeAltitude.IsNull() )
746     {
747       if ( aZoneInterpolator != NULL )
748       {
749         aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
750         aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
751       }
752       else
753         aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
754     }
755   }
756   else
757   {
758     HYDROData_SequenceOfObjects aZoneObjects = theZone->GetObjects();
759     HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
760     for ( ; anIter.More(); anIter.Next() )
761     {
762       Handle(HYDROData_Object) aZoneObj =
763         Handle(HYDROData_Object)::DownCast( anIter.Value() );
764       if ( aZoneObj.IsNull() )
765         continue;
766
767       Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
768       if ( anObjAltitude.IsNull() )
769         continue;
770
771       double aPointAltitude = 0.0;
772       if ( aZoneInterpolator != NULL )
773       {
774         aZoneInterpolator->SetAltitudeObject( anObjAltitude );
775         aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
776       }
777       else
778         aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
779
780       if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
781         continue;
782
783       if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
784       {
785         aResAltitude = aPointAltitude;
786         break;
787       }
788       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
789       {
790         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
791              aResAltitude > aPointAltitude )
792         {
793           aResAltitude = aPointAltitude;
794         }
795       }
796       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
797       {
798         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
799              aResAltitude < aPointAltitude )
800         {
801           aResAltitude = aPointAltitude;
802         }
803       }
804     }
805   }
806
807   return aResAltitude;
808 }
809
810 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
811   const NCollection_Sequence<gp_XY>& thePoints,
812   const Handle(HYDROData_Region)&    theRegion ) const
813 {
814   NCollection_Sequence<double> aResSeq;
815
816   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
817   {
818     const gp_XY& thePnt = thePoints.Value( i );
819     
820     double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
821     aResSeq.Append( anAltitude );
822   }
823
824   return aResSeq;
825 }
826
827 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
828   const NCollection_Sequence<gp_XY>& thePoints,
829   const Handle(HYDROData_Zone)&      theZone ) const
830 {
831   NCollection_Sequence<double> aResSeq;
832
833   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
834   {
835     const gp_XY& thePnt = thePoints.Value( i );
836     
837     double anAltitude = GetAltitudeForPoint( thePnt, theZone );
838     aResSeq.Append( anAltitude );
839   }
840
841   return aResSeq;
842 }
843
844 double HYDROData_CalculationCase::GetStricklerCoefficientForPoint( const gp_XY& thePoint ) const
845 {
846   //TODO: #652
847   return 0.0;
848 }
849
850 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
851 {
852   Handle(HYDROData_Region) aResRegion;
853
854   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
855   if ( !aZone.IsNull() )
856     aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
857
858   return aResRegion;
859 }
860
861 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
862 {
863   Handle(HYDROData_Zone) aResZone;
864
865   HYDROData_SequenceOfObjects aRegions = GetRegions();
866
867   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
868   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
869   {
870     Handle(HYDROData_Region) aRegion =
871       Handle(HYDROData_Region)::DownCast( anIter.Value() );
872     if ( aRegion.IsNull() )
873       continue;
874
875     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
876     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
877     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
878     {
879       Handle(HYDROData_Zone) aRegZone =
880         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
881       if ( aRegZone.IsNull() )
882         continue;
883
884       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
885       if ( aPointRelation != POINT_OUT )
886         aResZone = aRegZone; // We found the desired zone
887     }
888   }
889
890   return aResZone;
891 }
892
893 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
894   const gp_XY&                  thePoint,
895   const Handle(HYDROData_Zone)& theZone ) const
896 {
897   PointClassification aRes = POINT_OUT;
898   if ( theZone.IsNull() )
899     return aRes;
900
901   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
902   if ( aZoneFace.IsNull() )
903     return aRes;
904 #ifdef DEB_CLASS2D      
905           TopoDS_Compound aCmp;
906       BRep_Builder aBB;
907       aBB.MakeCompound(aCmp);
908           aBB.Add(aCmp, aZoneFace);
909           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
910           BRepBuilderAPI_MakeVertex aMk(aPnt);
911           aBB.Add(aCmp, aMk.Vertex());
912           BRepTools::Write(aCmp, "FCL2d.brep");
913 #endif  
914   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
915   if (State == TopAbs_OUT)
916     aRes =  POINT_OUT;
917   else if(State == TopAbs_IN)
918     aRes =  POINT_IN;
919   else if(State == TopAbs_ON)
920     aRes =  POINT_ON;
921   return aRes;
922 }
923
924 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
925                                                                   const QString& thePrefixOrName,
926                                                                   bool isPrefix )
927 {
928   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
929   int aTag = aNewLab.Tag();
930
931   Handle(HYDROData_Region) aNewRegion =
932     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
933   AddRegion( aNewRegion );
934
935   QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
936   aNewRegion->SetName( aRegionName );
937
938   return aNewRegion;
939 }
940
941 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
942 {
943   TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
944
945   Handle(HYDROData_SplittedShapesGroup) aNewGroup =
946     Handle(HYDROData_SplittedShapesGroup)::DownCast( 
947       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
948   AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
949
950   aNewGroup->SetName( theName );
951
952   return aNewGroup;
953 }
954
955 QString HYDROData_CalculationCase::Export( int theStudyId ) const
956 {
957 #ifdef LIGHT_MODE
958   return "";
959 #else
960   GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
961   SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
962   
963   QString aGeomObjEntry, anErrorMsg;
964   bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry, anErrorMsg );
965   return isOK ? aGeomObjEntry : QString();
966 #endif
967 }
968
969 #ifndef LIGHT_MODE
970 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
971                                         SALOMEDS::Study_ptr theStudy,
972                                         QString& theGeomObjEntry,
973                                         QString& theErrorMsg ) const
974 {
975   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
976
977   // Get groups definitions
978   HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
979
980   HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
981   for ( ; anIter.More(); anIter.Next() )
982   {
983     // Get shapes group
984     Handle(HYDROData_ShapesGroup) aGroup =
985       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
986     if ( aGroup.IsNull() )
987       continue;
988
989     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
990
991     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
992     aGroup->GetShapes( aGroupDef.Shapes );
993
994     aSeqOfGroupsDefs.Append( aGroupDef );
995   }
996   
997   // Get faces
998   bool isAllNotSubmersible = true;
999   TopTools_ListOfShape aFaces;
1000   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
1001   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
1002   for ( ; aRegionIter.More(); aRegionIter.Next() )
1003   {
1004     Handle(HYDROData_Region) aRegion =
1005       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
1006     if( aRegion.IsNull() || !aRegion->IsSubmersible() )
1007       continue;
1008     
1009     if ( isAllNotSubmersible )
1010       isAllNotSubmersible = false;
1011
1012     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
1013     aFaces.Append( aRegionShape );
1014   }
1015
1016   bool aRes = false;
1017
1018   if ( aCaseRegions.IsEmpty() ) {
1019     theErrorMsg = QString("the list of regions is empty.");
1020   } else if ( isAllNotSubmersible ) {
1021     theErrorMsg = QString("there are no submersible regions.");
1022   } else {
1023     aRes = Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );;
1024   }
1025
1026   return aRes;
1027 }
1028
1029 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
1030                                         SALOMEDS::Study_ptr                           theStudy,
1031                                         const TopTools_ListOfShape&                   theFaces,
1032                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
1033                                         QString& theGeomObjEntry ) const
1034 {
1035   // Sew faces
1036   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
1037   aSewing.SetNonManifoldMode( Standard_False );
1038 #ifdef DEB_CALCULATION
1039   TCollection_AsciiString aNam("Sh_");
1040   int i=1;
1041 #endif
1042   TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
1043   for ( ; aFaceIter.More(); aFaceIter.Next() )
1044   {
1045     TopoDS_Shape aShape = aFaceIter.Value();
1046     if ( aShape.IsNull() )
1047       continue;
1048
1049     if ( aShape.ShapeType() == TopAbs_FACE )
1050     {
1051       aSewing.Add( aShape );
1052 #ifdef DEB_CALCULATION
1053       TCollection_AsciiString aName = aNam + ++i + ".brep";
1054       BRepTools::Write(aShape ,aName.ToCString());
1055 #endif
1056     }
1057     else
1058     {
1059 #ifdef DEB_CALCULATION
1060       int j = 1;
1061 #endif
1062       TopExp_Explorer anExp( aShape, TopAbs_FACE );
1063       for (; anExp.More(); anExp.Next() ) {
1064         aSewing.Add( anExp.Current() );
1065 #ifdef DEB_CALCULATION
1066
1067         TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
1068         BRepTools::Write(anExp.Current() ,aName.ToCString());
1069 #endif
1070       }
1071     }
1072   } // faces iterator
1073   
1074   aSewing.Perform();
1075   TopoDS_Shape aSewedShape = aSewing.SewedShape();
1076
1077   // If the sewed shape is empty - return false
1078   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1079     return false;
1080
1081 #ifdef DEB_CALCULATION
1082   BRepTools::Write(aSewedShape ,"Sew.brep");
1083 #endif
1084   // Publish the sewed shape
1085   QString aName = EXPORT_NAME;
1086   GEOM::GEOM_Object_ptr aMainShape = 
1087     HYDROData_GeomTool::publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
1088
1089   if ( aMainShape->_is_nil() )  
1090     return false;
1091
1092   if ( theGroupsDefs.IsEmpty() )
1093     return true;
1094
1095   // Create groups
1096   TopTools_IndexedMapOfShape aMapOfSubShapes;
1097   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1098
1099   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1100
1101   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1102   {
1103     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1104
1105     NCollection_Sequence<int> aGroupIndexes;
1106     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1107     {
1108       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1109 #ifdef DEB_CALCULATION
1110       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1111 #endif
1112       
1113       TopoDS_Shape aModifiedShape = aShape;
1114       if ( aSewing.IsModified( aShape ) )
1115         aModifiedShape = aSewing.Modified( aShape );
1116       else if ( aSewing.IsModifiedSubShape( aShape ) )
1117         aModifiedShape = aSewing.ModifiedSubShape( aShape );
1118
1119 #ifdef DEB_CALCULATION
1120       const TopLoc_Location& aL1 = aShape.Location();
1121       const TopLoc_Location& aL2 = aModifiedShape.Location();
1122       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1123 #endif
1124
1125       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1126       if ( anIndex > 0 ) {
1127         aGroupIndexes.Append( anIndex );
1128       } else {
1129 #ifdef DEB_CALCULATION    
1130         TCollection_AsciiString aNam("Lost_");
1131         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1132         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1133         {
1134            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1135            if ( aModifiedShape.IsPartner( aS ) )
1136            {
1137              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1138              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1139              BRepTools::Write(aS ,aName.ToCString());
1140             break;
1141            }
1142          }
1143         }
1144 #endif
1145       }
1146     }
1147     if ( !aGroupIndexes.IsEmpty() )
1148       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1149   }
1150  
1151   if ( !aGroupsData.IsEmpty() )
1152   {
1153     GEOM::GEOM_IGroupOperations_var aGroupOp = 
1154       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
1155
1156     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1157     for ( ; aMapIt.More(); aMapIt.Next() )
1158     {
1159       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
1160       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1161
1162       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1163       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1164         continue;
1165
1166       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1167       aGeomIndexes->length( aGroupIndexes.Length() );
1168
1169       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1170         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1171
1172       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1173       if ( aGroupOp->IsDone() )
1174       {
1175         SALOMEDS::SObject_var aGroupSO = 
1176           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1177       }
1178     }
1179   }
1180
1181   return true;
1182 }
1183 #endif
1184
1185 void HYDROData_CalculationCase::ClearRules( HYDROData_CalculationCase::DataTag theDataTag,
1186                                             const bool theIsSetToUpdate )
1187 {
1188   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1189   HYDROData_PriorityQueue::ClearRules( aRulesLab );
1190
1191   // Indicate model of the need to update splitting
1192   if ( theIsSetToUpdate ) {
1193     Changed( Geom_2d );
1194   }
1195 }
1196
1197 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Entity)&    theObject1,
1198                                          HYDROData_PriorityType             thePriority,
1199                                          const Handle(HYDROData_Entity)&    theObject2,
1200                                          HYDROData_Zone::MergeType          theMergeType,
1201                                          HYDROData_CalculationCase::DataTag theDataTag )
1202 {
1203   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1204   HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1205
1206   // Indicate model of the need to update splitting
1207   Changed( Geom_2d );
1208 }
1209
1210 QString HYDROData_CalculationCase::DumpRules() const
1211 {
1212   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1213   return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1214 }
1215
1216 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1217 {
1218   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1219   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1220
1221   // Indicate model of the need to update splitting
1222   Changed( Geom_2d );
1223 }
1224
1225 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1226 {
1227   Handle(TDataStd_Integer) aModeAttr;
1228   bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1229   if( isOK )
1230     return ( AssignmentMode ) aModeAttr->Get();
1231   else
1232     return MANUAL;
1233 }
1234
1235 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1236                                                    QStringList& theScript ) const
1237 {
1238   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1239   HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1240 }
1241
1242 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1243 {
1244   return myLastWarning;
1245 }
1246
1247 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1248 {
1249   myLastWarning.Type = theType;
1250   myLastWarning.Data = theData;
1251 }
1252
1253 void HYDROData_CalculationCase::UpdateRegionsNames( const HYDROData_SequenceOfObjects& theRegions,
1254                                                     const QString& theOldCaseName,
1255                                                     const QString& theName )
1256 {
1257   HYDROData_SequenceOfObjects::Iterator anIter( theRegions );
1258   for ( ; anIter.More(); anIter.Next() )
1259   {
1260     Handle(HYDROData_Region) aRegion =
1261       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1262     if ( aRegion.IsNull() )
1263       continue;
1264
1265     HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegion );
1266
1267     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
1268     HYDROData_SequenceOfObjects::Iterator anIter( aZones );
1269     for ( ; anIter.More(); anIter.Next() )
1270     {
1271       Handle(HYDROData_Zone) aRegZone =
1272         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1273       if ( aRegZone.IsNull() )
1274         continue;
1275
1276       HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegZone );
1277     }
1278   }
1279 }
1280
1281 void HYDROData_CalculationCase::DumpRegionsToPython( QStringList& theResList,
1282                                                      MapOfTreatedObjects& theTreatedObjects,
1283                                                      const HYDROData_SequenceOfObjects& theRegions ) const
1284 {
1285   HYDROData_SequenceOfObjects::Iterator anIter;
1286   anIter.Init( theRegions );
1287   for ( ; anIter.More(); anIter.Next() )
1288   {
1289     Handle(HYDROData_Region) aRegion =
1290       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1291     if ( aRegion.IsNull() )
1292       continue;
1293
1294     theTreatedObjects.insert( aRegion->GetName(), aRegion );
1295     QStringList aRegDump = aRegion->DumpToPython( theTreatedObjects );
1296     theResList << aRegDump;
1297   }
1298 }
1299
1300 bool HYDROData_CalculationCase::GetRule( int theIndex, 
1301                                          Handle(HYDROData_Entity)&           theObject1,
1302                                          HYDROData_PriorityType&             thePriority,
1303                                          Handle(HYDROData_Entity)&           theObject2,
1304                                          HYDROData_Zone::MergeType&          theMergeType,
1305                                          HYDROData_CalculationCase::DataTag& theDataTag) const
1306 {
1307   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1308   return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1309     theObject1, thePriority, theObject2, theMergeType );
1310 }