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