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