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