Salome HOME
implementation of the strickler types (integer codes) for points
[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 std::vector<int> HYDROData_CalculationCase::GetStricklerTypeForPoints( const std::vector<gp_XY>& thePoints ) const
936 {
937   Handle( HYDROData_LandCoverMap ) aLCM = GetLandCoverMap();
938   Handle( HYDROData_StricklerTable ) aTable = GetStricklerTable();
939   std::vector<int> types;
940   if( aLCM.IsNull() || aTable.IsNull() )
941     return types;
942
943   aLCM->ClassifyPoints(thePoints, aTable, types );
944   return types;
945 }
946
947 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
948 {
949   Handle(HYDROData_Region) aResRegion;
950
951   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
952   if ( !aZone.IsNull() )
953     aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
954
955   return aResRegion;
956 }
957
958 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
959 {
960   Handle(HYDROData_Zone) aResZone;
961
962   HYDROData_SequenceOfObjects aRegions = GetRegions();
963
964   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
965   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
966   {
967     Handle(HYDROData_Region) aRegion =
968       Handle(HYDROData_Region)::DownCast( anIter.Value() );
969     if ( aRegion.IsNull() )
970       continue;
971
972     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
973     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
974     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
975     {
976       Handle(HYDROData_Zone) aRegZone =
977         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
978       if ( aRegZone.IsNull() )
979         continue;
980
981       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
982       if ( aPointRelation != POINT_OUT )
983         aResZone = aRegZone; // We found the desired zone
984     }
985   }
986
987   return aResZone;
988 }
989
990 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
991   const gp_XY&                  thePoint,
992   const Handle(HYDROData_Zone)& theZone ) const
993 {
994   PointClassification aRes = POINT_OUT;
995   if ( theZone.IsNull() )
996     return aRes;
997
998   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
999   if ( aZoneFace.IsNull() )
1000     return aRes;
1001 #ifdef DEB_CLASS2D      
1002           TopoDS_Compound aCmp;
1003       BRep_Builder aBB;
1004       aBB.MakeCompound(aCmp);
1005           aBB.Add(aCmp, aZoneFace);
1006           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
1007           BRepBuilderAPI_MakeVertex aMk(aPnt);
1008           aBB.Add(aCmp, aMk.Vertex());
1009           BRepTools::Write(aCmp, "FCL2d.brep");
1010 #endif  
1011   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
1012   if (State == TopAbs_OUT)
1013     aRes =  POINT_OUT;
1014   else if(State == TopAbs_IN)
1015     aRes =  POINT_IN;
1016   else if(State == TopAbs_ON)
1017     aRes =  POINT_ON;
1018   return aRes;
1019 }
1020
1021 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
1022                                                                   const QString& thePrefixOrName,
1023                                                                   bool isPrefix )
1024 {
1025   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
1026   int aTag = aNewLab.Tag();
1027
1028   Handle(HYDROData_Region) aNewRegion =
1029     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
1030   AddRegion( aNewRegion );
1031
1032   QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
1033   aNewRegion->SetName( aRegionName, true );
1034
1035   return aNewRegion;
1036 }
1037
1038 Handle(HYDROData_SplitShapesGroup) HYDROData_CalculationCase::addNewSplitGroup( const QString& theName )
1039 {
1040   TDF_Label aNewLab = myLab.FindChild( DataTag_SplitGroups ).NewChild();
1041
1042   Handle(HYDROData_SplitShapesGroup) aNewGroup =
1043     Handle(HYDROData_SplitShapesGroup)::DownCast( 
1044       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLIT_GROUP ) );
1045   AddReferenceObject( aNewGroup, DataTag_SplitGroups );
1046
1047   aNewGroup->SetName( theName );
1048
1049   return aNewGroup;
1050 }
1051
1052 QString HYDROData_CalculationCase::Export( int theStudyId ) const
1053 {
1054 #ifdef LIGHT_MODE
1055   return "";
1056 #else
1057   GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
1058   SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
1059   
1060   QString aGeomObjEntry, anErrorMsg;
1061   QString statMess;
1062   bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry, anErrorMsg, statMess );
1063   return isOK ? aGeomObjEntry : QString();
1064 #endif
1065 }
1066
1067 #ifndef LIGHT_MODE
1068 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
1069                                         SALOMEDS::Study_ptr theStudy,
1070                                         QString& theGeomObjEntry,
1071                                         QString& theErrorMsg,
1072                                         QString& statMess) const
1073 {
1074   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
1075
1076   // Get groups definitions
1077   HYDROData_SequenceOfObjects aSplitGroups = GetSplitGroups();
1078
1079   TopTools_SequenceOfShape IntSh; //internal edges
1080   HYDROData_SequenceOfObjects::Iterator anIter( aSplitGroups );
1081   for ( ; anIter.More(); anIter.Next() )
1082   {
1083     // Get shapes group
1084     Handle(HYDROData_ShapesGroup) aGroup =
1085       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
1086     if ( aGroup.IsNull() )
1087       continue;
1088
1089     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
1090
1091     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
1092     aGroup->GetShapes( aGroupDef.Shapes );
1093
1094     aSeqOfGroupsDefs.Append( aGroupDef );
1095
1096     Handle(HYDROData_SplitShapesGroup) aSSGroup = Handle(HYDROData_SplitShapesGroup)::DownCast( anIter.Value() );
1097     TopTools_SequenceOfShape dummy;
1098     if (!aSSGroup.IsNull())
1099       if (aSSGroup->GetInternal())
1100       {
1101         aSSGroup->GetShapes(dummy);
1102         IntSh.Append(dummy);
1103       }
1104
1105   }
1106   
1107   // Get faces
1108   bool isAllNotSubmersible = true;
1109   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
1110   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
1111   NCollection_IndexedDataMap<TopoDS_Shape, QString> aShToNames;
1112   for ( ; aRegionIter.More(); aRegionIter.Next() )
1113   {
1114     Handle(HYDROData_Region) aRegion =
1115       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
1116     if( aRegion.IsNull() || !aRegion->IsSubmersible() )
1117       continue;
1118     
1119     if ( isAllNotSubmersible )
1120       isAllNotSubmersible = false;
1121
1122     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs, &IntSh );
1123     aShToNames.Add( aRegionShape, aRegion->GetName() );
1124   }
1125
1126   bool aRes = false;
1127
1128   if ( aCaseRegions.IsEmpty() ) {
1129     theErrorMsg = QString("the list of regions is empty.");
1130   } else if ( isAllNotSubmersible ) {
1131     theErrorMsg = QString("there are no submersible regions.");
1132   } else {
1133     aRes = Export( theGeomEngine, theStudy, aShToNames, aSeqOfGroupsDefs, theGeomObjEntry );;
1134   }
1135
1136   if( aRes && !GetLandCoverMap().IsNull() && !GetStricklerTable().IsNull() )
1137   {
1138     QString aTelemacFileName = GetName() + ".telemac";
1139     aRes = GetLandCoverMap()->ExportTelemac( aTelemacFileName, 1E-2, GetStricklerTable(), statMess );
1140     if (!aRes)
1141       theErrorMsg = QString( "The export to TELEMAC %1 failed" ).arg( aTelemacFileName );
1142   }
1143   return aRes;
1144 }
1145
1146 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
1147                                         SALOMEDS::Study_ptr                           theStudy,
1148                                         const NCollection_IndexedDataMap<TopoDS_Shape, QString>& aShToName,
1149                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
1150                                         QString& theGeomObjEntry ) const
1151 {
1152   // Sew faces
1153   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
1154   aSewing.SetNonManifoldMode( Standard_False );
1155 #ifdef DEB_CALCULATION
1156   TCollection_AsciiString aNam("Sh_");
1157   int i=1;
1158 #endif
1159   TopTools_DataMapOfShapeListOfShape SH2M;
1160   for ( int i = 1; i <= aShToName.Extent(); i++ )
1161   {
1162     const TopoDS_Shape& aShape = aShToName.FindKey(i);
1163     if ( aShape.IsNull() )
1164       continue;
1165     
1166     SH2M.Bind(aShape, TopTools_ListOfShape());
1167     TopTools_ListOfShape& LM = SH2M.ChangeFind(aShape);
1168     if ( aShape.ShapeType() == TopAbs_FACE || aShape.ShapeType() == TopAbs_SHELL )
1169     {
1170       aSewing.Add( aShape );
1171       LM.Append(aShape);
1172     }
1173     else if (aShape.ShapeType() == TopAbs_COMPOUND)
1174     {
1175       TopExp_Explorer anExp( aShape, TopAbs_SHELL );
1176       for (; anExp.More(); anExp.Next() )
1177       {
1178         aSewing.Add( anExp.Current() );
1179         LM.Append(anExp.Current());
1180       }
1181       anExp.Init( aShape, TopAbs_FACE, TopAbs_SHELL );
1182       {
1183         aSewing.Add( anExp.Current() );
1184         LM.Append(anExp.Current());
1185       }
1186     }
1187   } // faces iterator
1188   
1189   aSewing.Perform();
1190   TopoDS_Shape aSewedShape = aSewing.SewedShape();
1191
1192   NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher> aFacesToNameModif;
1193
1194   for ( int i = 1; i <= aShToName.Extent(); i++ )
1195   {
1196     const TopoDS_Shape& CurShape = aShToName.FindKey(i);
1197     const QString& Qstr = aShToName.FindFromIndex(i);
1198     const TopTools_ListOfShape& LM = SH2M(CurShape);
1199     for (TopTools_ListIteratorOfListOfShape it(LM); it.More(); it.Next())
1200     {
1201       const TopoDS_Shape& csh = it.Value();
1202       if (aSewing.IsModified(csh))
1203         aFacesToNameModif.Add(aSewing.Modified(csh), Qstr);
1204       else
1205         aFacesToNameModif.Add(csh, Qstr);
1206     }
1207   }
1208
1209  
1210   // If the sewed shape is empty - return false
1211   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1212     return false;
1213
1214 #ifdef DEB_CALCULATION
1215   BRepTools::Write(aSewedShape ,"Sew.brep");
1216 #endif
1217   // Publish the sewed shape
1218   QString aName = EXPORT_NAME;
1219   GEOM::GEOM_Object_ptr aMainShape = 
1220     HYDROData_GeomTool::ExplodeShapeInGEOMandPublish( theGeomEngine, theStudy, aSewedShape, aFacesToNameModif, aName, theGeomObjEntry );
1221
1222   if ( aMainShape->_is_nil() )  
1223     return false;
1224
1225   if ( theGroupsDefs.IsEmpty() )
1226     return true;
1227
1228   // Create groups
1229   TopTools_IndexedMapOfShape aMapOfSubShapes;
1230   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1231
1232   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1233
1234   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1235   {
1236     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1237
1238     NCollection_Sequence<int> aGroupIndexes;
1239     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1240     {
1241       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1242 #ifdef DEB_CALCULATION
1243       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1244 #endif
1245       
1246       TopoDS_Shape aModifiedShape = aShape;
1247       if ( aSewing.IsModified( aShape ) )
1248         aModifiedShape = aSewing.Modified( aShape );
1249       else if ( aSewing.IsModifiedSubShape( aShape ) )
1250         aModifiedShape = aSewing.ModifiedSubShape( aShape );
1251
1252 #ifdef DEB_CALCULATION
1253       const TopLoc_Location& aL1 = aShape.Location();
1254       const TopLoc_Location& aL2 = aModifiedShape.Location();
1255       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1256 #endif
1257
1258       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1259       if ( anIndex > 0 ) {
1260         aGroupIndexes.Append( anIndex );
1261       } else {
1262 #ifdef DEB_CALCULATION    
1263         TCollection_AsciiString aNam("Lost_");
1264         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1265         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1266         {
1267            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1268            if ( aModifiedShape.IsPartner( aS ) )
1269            {
1270              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1271              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1272              BRepTools::Write(aS ,aName.ToCString());
1273             break;
1274            }
1275          }
1276         }
1277 #endif
1278       }
1279     }
1280     if ( !aGroupIndexes.IsEmpty() )
1281       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1282   }
1283  
1284   if ( !aGroupsData.IsEmpty() )
1285   {
1286     GEOM::GEOM_IGroupOperations_var aGroupOp = 
1287       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
1288
1289     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1290     for ( ; aMapIt.More(); aMapIt.Next() )
1291     {
1292       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
1293       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1294
1295       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1296       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1297         continue;
1298
1299       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1300       aGeomIndexes->length( aGroupIndexes.Length() );
1301
1302       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1303         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1304
1305       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1306       if ( aGroupOp->IsDone() )
1307       {
1308         SALOMEDS::SObject_var aGroupSO = 
1309           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1310       }
1311     }
1312   }
1313
1314   return true;
1315 }
1316 #endif
1317
1318 void HYDROData_CalculationCase::ClearRules( HYDROData_CalculationCase::DataTag theDataTag,
1319                                             const bool theIsSetToUpdate )
1320 {
1321   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1322   HYDROData_PriorityQueue::ClearRules( aRulesLab );
1323
1324   // Indicate model of the need to update splitting
1325   if ( theIsSetToUpdate ) {
1326     Changed( Geom_2d );
1327   }
1328 }
1329
1330 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Entity)&    theObject1,
1331                                          HYDROData_PriorityType             thePriority,
1332                                          const Handle(HYDROData_Entity)&    theObject2,
1333                                          HYDROData_Zone::MergeType          theMergeType,
1334                                          HYDROData_CalculationCase::DataTag theDataTag )
1335 {
1336   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1337   HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1338
1339   // Indicate model of the need to update splitting
1340   Changed( Geom_2d );
1341 }
1342
1343 QString HYDROData_CalculationCase::DumpRules() const
1344 {
1345   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1346   return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1347 }
1348
1349 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1350 {
1351   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1352   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1353
1354   // Indicate model of the need to update splitting
1355   Changed( Geom_2d );
1356 }
1357
1358 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1359 {
1360   Handle(TDataStd_Integer) aModeAttr;
1361   bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1362   if( isOK )
1363     return ( AssignmentMode ) aModeAttr->Get();
1364   else
1365     return MANUAL;
1366 }
1367
1368 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1369                                                    QStringList& theScript ) const
1370 {
1371   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1372   HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1373 }
1374
1375 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1376 {
1377   return myLastWarning;
1378 }
1379
1380 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1381 {
1382   myLastWarning.Type = theType;
1383   myLastWarning.Data = theData;
1384 }
1385
1386 void HYDROData_CalculationCase::UpdateRegionsNames( const HYDROData_SequenceOfObjects& theRegions,
1387                                                     const QString& theOldCaseName,
1388                                                     const QString& theName )
1389 {
1390   HYDROData_SequenceOfObjects::Iterator anIter( theRegions );
1391   for ( ; anIter.More(); anIter.Next() )
1392   {
1393     Handle(HYDROData_Region) aRegion =
1394       Handle(HYDROData_Region)::DownCast( anIter.Value() );
1395     if ( aRegion.IsNull() )
1396       continue;
1397
1398     HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegion );
1399
1400     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
1401     HYDROData_SequenceOfObjects::Iterator anIter( aZones );
1402     for ( ; anIter.More(); anIter.Next() )
1403     {
1404       Handle(HYDROData_Zone) aRegZone =
1405         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1406       if ( aRegZone.IsNull() )
1407         continue;
1408
1409       HYDROData_Tool::UpdateChildObjectName( theOldCaseName, theName, aRegZone );
1410     }
1411   }
1412 }
1413
1414 void HYDROData_CalculationCase::DumpRegionsToPython( QStringList& theResList,
1415                                                      const QString& thePyScriptPath,
1416                                                      MapOfTreatedObjects& theTreatedObjects,
1417                                                      const HYDROData_SequenceOfObjects& theRegions ) const
1418 {
1419   HYDROData_SequenceOfObjects::Iterator anIter;
1420   anIter.Init(theRegions);
1421   for (int ireg = 1; anIter.More(); anIter.Next(), ireg++)
1422     {
1423       Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast(anIter.Value());
1424       if (aRegion.IsNull())
1425         continue;
1426       QString defRegName = this->GetName();
1427       QString regSuffix = QString("_Reg_%1").arg(ireg);
1428       defRegName += regSuffix;
1429       theTreatedObjects.insert(aRegion->GetName(), aRegion);
1430       QStringList aRegDump = aRegion->DumpToPython(thePyScriptPath, theTreatedObjects, defRegName);
1431       theResList << aRegDump;
1432     }
1433   for (anIter.Init(theRegions); anIter.More(); anIter.Next())
1434     {
1435       Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast(anIter.Value());
1436       if (aRegion.IsNull())
1437         continue;
1438       QStringList aRegDump;
1439       aRegion->SetNameInDumpPython(aRegDump);
1440       theResList << aRegDump;
1441     }
1442 }
1443
1444 bool HYDROData_CalculationCase::GetRule( int theIndex, 
1445                                          Handle(HYDROData_Entity)&           theObject1,
1446                                          HYDROData_PriorityType&             thePriority,
1447                                          Handle(HYDROData_Entity)&           theObject2,
1448                                          HYDROData_Zone::MergeType&          theMergeType,
1449                                          HYDROData_CalculationCase::DataTag& theDataTag) const
1450 {
1451   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1452   return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1453     theObject1, thePriority, theObject2, theMergeType );
1454 }
1455
1456 bool HYDROData_CalculationCase::AddInterPoly( const Handle(HYDROData_PolylineXY)& theInterPolyline )
1457 {
1458   HYDROData_CalculationCase::DataTag aDataTag = DataTag_InterPoly;
1459
1460   if ( HasReference( theInterPolyline, aDataTag ) )
1461     return false; 
1462
1463   AddReferenceObject( theInterPolyline, aDataTag );
1464
1465   Changed( Geom_2d );
1466
1467   return true;
1468 }
1469
1470 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetInterPolyObjects() const
1471 {
1472   return GetReferenceObjects( DataTag_InterPoly ); 
1473 }
1474
1475 void HYDROData_CalculationCase::RemoveInterPolyObject( const Handle(HYDROData_PolylineXY)& theInterPolyline ) 
1476 {
1477   if ( theInterPolyline.IsNull() )
1478     return;
1479
1480   RemoveReferenceObject( theInterPolyline->Label(), DataTag_InterPoly );
1481
1482   Changed( Geom_2d );
1483 }
1484
1485
1486