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