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