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