Salome HOME
Merge remote-tracking branch 'origin/BR_LAND_COVER' 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_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, DataTag_CustomRules );
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 =
375       Handle(HYDROData_Object)::DownCast( aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType ) );
376     if( aRegObj.IsNull() )
377       continue;
378     Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
379     if( aRegion.IsNull() )
380       continue;
381     Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
382
383     if( aSplitData.ObjectNames.count() > 1 && aMergeType==HYDROData_Zone::Merge_UNKNOWN )
384     {
385       qDebug( "Error in algorithm: unresolved conflicts" );
386     }
387
388     switch( aMergeType )
389     {
390     case HYDROData_Zone::Merge_ZMIN:
391     case HYDROData_Zone::Merge_ZMAX:
392       aRegionZone->SetMergeType( aMergeType );
393       break;
394     case HYDROData_Zone::Merge_Object:
395       aRegionZone->SetMergeType( aMergeType );
396       aRegionZone->RemoveMergeAltitude();
397       aRegionZone->SetMergeAltitude( aRegObj->GetAltitudeObject() );
398       break;
399     }
400   }
401
402   QStringList anObjectsWithEmptyRegions;
403   QMap<QString, Handle(HYDROData_Region)>::const_iterator
404     anIt = aRegionsMap.begin(), aLast = aRegionsMap.end();
405   for( ; anIt!=aLast; anIt++ )
406   {
407     Handle(HYDROData_Region) aRegion = anIt.value();
408     if( aRegion->GetZones().IsEmpty() )
409     {
410       QString aRegName = aRegion->GetName();
411       QString anObjName = aRegionNameToObjNameMap[aRegName];
412       anObjectsWithEmptyRegions.append( anObjName );
413     }
414   }
415   
416   if( !anObjectsWithEmptyRegions.empty() )
417   {
418     QString aData = anObjectsWithEmptyRegions.join( ", " );
419     SetWarning( WARN_EMPTY_REGIONS, aData );
420   }
421 }
422
423 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
424                                                      const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
425 {
426   QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
427
428   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
429   while( anIter.hasNext() )
430   {
431     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
432     // Create new edges group
433     if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
434       continue;
435
436     QString anObjName = aSplitData.ObjectNames.first();
437     if ( anObjName.isEmpty() )
438       continue;
439 #ifdef DEB_CALCULATION
440     QString aStr = aSplitData.ObjectNames.join(" "); 
441           cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl; 
442 #endif
443     Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
444     if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
445     {
446       aSplittedGroup = addNewSplittedGroup( CALCULATION_GROUPS_PREF + anObjName );
447       aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
448     }
449     else
450     {
451       aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
452     }
453     if ( aSplittedGroup.IsNull() )
454       continue;
455
456     aSplittedGroup->AddShape( aSplitData.Shape );
457   }
458 }
459
460 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
461 {
462   if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
463     return false; // Wrong type of object
464
465   if ( HasReference( theObject, DataTag_GeometryObject ) )
466     return false; // Object is already in reference list
467
468   AddReferenceObject( theObject, DataTag_GeometryObject );
469   
470   // Indicate model of the need to update splitting
471   SetToUpdate( true );
472
473   return true;
474 }
475
476 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
477 {
478   return GetReferenceObjects( DataTag_GeometryObject );
479 }
480
481 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
482 {
483   if ( theObject.IsNull() )
484     return;
485
486   RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
487
488   // Indicate model of the need to update splitting
489   SetToUpdate( true );
490 }
491
492 void HYDROData_CalculationCase::RemoveGeometryObjects()
493 {
494   ClearReferenceObjects( DataTag_GeometryObject );
495
496   // Indicate model of the need to update splitting
497   SetToUpdate( true );
498 }
499
500 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
501 {
502   if ( theGroup.IsNull() )
503     return false;
504
505   if ( HasReference( theGroup, DataTag_GeometryGroup ) )
506     return false; // Object is already in reference list
507
508   AddReferenceObject( theGroup, DataTag_GeometryGroup );
509   
510   // Indicate model of the need to update splitting
511   SetToUpdate( true );
512
513   return true;
514 }
515
516 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
517 {
518   return GetReferenceObjects( DataTag_GeometryGroup );
519 }
520
521 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
522 {
523   if ( theGroup.IsNull() )
524     return;
525
526   RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
527
528   // Indicate model of the need to update splitting
529   SetToUpdate( true );
530 }
531
532 void HYDROData_CalculationCase::RemoveGeometryGroups()
533 {
534   ClearReferenceObjects( DataTag_GeometryGroup );
535
536   // Indicate model of the need to update splitting
537   SetToUpdate( true );
538 }
539
540 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
541 {
542   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
543
544   SetReferenceObject( thePolyline, DataTag_Polyline );
545
546   // Indicate model of the need to update zones splitting
547   SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
548 }
549
550 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
551 {
552   return Handle(HYDROData_PolylineXY)::DownCast( 
553            GetReferenceObject( DataTag_Polyline ) );
554 }
555
556 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
557 {
558   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
559
560   ClearReferenceObjects( DataTag_Polyline );
561
562   // Indicate model of the need to update zones splitting
563   SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
564 }
565
566 void HYDROData_CalculationCase::SetStricklerTable( const Handle(HYDROData_StricklerTable)& theStricklerTable )
567 {
568   Handle(HYDROData_StricklerTable) aPrevStricklerTable = GetStricklerTable();
569
570   SetReferenceObject( theStricklerTable, DataTag_StricklerTable );
571
572   // Indicate model of the need to update land covers partition
573   SetToUpdate( !IsEqual( aPrevStricklerTable, theStricklerTable ) || IsMustBeUpdated() );
574 }
575
576 Handle(HYDROData_StricklerTable) HYDROData_CalculationCase::GetStricklerTable() const
577 {
578   return Handle(HYDROData_StricklerTable)::DownCast( 
579            GetReferenceObject( DataTag_StricklerTable ) );
580 }
581
582 void HYDROData_CalculationCase::RemoveStricklerTable()
583 {
584   Handle(HYDROData_StricklerTable) aPrevStricklerTable = GetStricklerTable();
585
586   ClearReferenceObjects( DataTag_StricklerTable );
587
588   // Indicate model of the need to update land covers partition
589   SetToUpdate( !aPrevStricklerTable.IsNull() || IsMustBeUpdated() );
590 }
591
592 bool HYDROData_CalculationCase::AddLandCover( const Handle(HYDROData_LandCover)& theLandCover )
593 {
594   if ( HasReference( theLandCover, DataTag_LandCover ) )
595     return false; // Land cover is already in reference list
596
597   AddReferenceObject( theLandCover, DataTag_LandCover );
598   
599   // Indicate model of the need to update land covers partition
600   SetToUpdate( true );
601
602   return true;
603 }
604
605 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetLandCovers() const
606 {
607   return GetReferenceObjects( DataTag_LandCover );
608 }
609
610 void HYDROData_CalculationCase::RemoveLandCover( const Handle(HYDROData_LandCover)& theLandCover )
611 {
612   if ( theLandCover.IsNull() )
613     return;
614
615   RemoveReferenceObject( theLandCover->Label(), DataTag_LandCover );
616
617   // Indicate model of the need to update land cover partition
618   SetToUpdate( true );
619 }
620
621 void HYDROData_CalculationCase::RemoveLandCovers()
622 {
623   ClearReferenceObjects( DataTag_LandCover );
624
625   // Indicate model of the need to update land cover partition
626   SetToUpdate( true );
627 }
628
629 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
630 {
631   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
632   Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
633   if ( aNewRegion.IsNull() )
634     return aNewRegion;
635
636   aNewRegion->AddZone( theZone );
637
638   return aNewRegion;
639 }
640
641 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
642 {
643   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
644
645   if ( theRegion.IsNull() )
646     return false;
647   
648   if ( HasReference( theRegion, DataTag_Region ) )
649     return false; // Object is already in reference list
650
651   // Move the region from other calculation
652   Handle(HYDROData_CalculationCase) aFatherCalc = 
653     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
654   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
655   {
656     Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
657     theRegion->CopyTo( aNewRegion );
658
659     aFatherCalc->RemoveRegion( theRegion );
660
661     theRegion->SetLabel( aNewRegion->Label() );
662   }
663   else
664   {
665     AddReferenceObject( theRegion, DataTag_Region );
666   }
667
668   return true;
669 }
670
671 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
672 {
673   return GetReferenceObjects( DataTag_Region );
674 }
675
676 void HYDROData_CalculationCase::UpdateRegionsOrder()
677 {
678   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
679   if ( aDocument.IsNull() )
680     return;
681
682   HYDROData_SequenceOfObjects aRegions = GetRegions();
683
684   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
685   for ( ; anIter.More(); anIter.Next() )
686   {
687     Handle(HYDROData_Region) aRegion =
688       Handle(HYDROData_Region)::DownCast( anIter.Value() );
689     if ( aRegion.IsNull() )
690       continue;
691
692     aRegion->SetName( "" );
693   }
694
695   QString aRegsPref = CALCULATION_REGIONS_PREF;
696
697   anIter.Init( aRegions );
698   for ( ; anIter.More(); anIter.Next() )
699   {
700     Handle(HYDROData_Region) aRegion =
701       Handle(HYDROData_Region)::DownCast( anIter.Value() );
702     if ( aRegion.IsNull() )
703       continue;
704
705     QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
706     aRegion->SetName( aRegionName );
707   }
708 }
709
710 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
711 {
712   if ( theRegion.IsNull() )
713     return;
714
715   RemoveReferenceObject( theRegion->Label(), DataTag_Region );
716
717   // Remove region from data model
718   Handle(HYDROData_CalculationCase) aFatherCalc = 
719     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
720   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
721     theRegion->Remove();
722 }
723
724 void HYDROData_CalculationCase::RemoveRegions()
725 {
726   myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
727 }
728
729 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
730 {
731   return GetReferenceObjects( DataTag_SplittedGroups );
732 }
733
734 void HYDROData_CalculationCase::RemoveSplittedGroups()
735 {
736   myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
737 }
738
739 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
740 {
741   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
742   return GetAltitudeForPoint( thePoint, aZone );
743 }
744
745 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                    thePoint,
746                                                        const Handle(HYDROData_Region)& theRegion ) const
747 {
748   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
749
750   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
751   if ( !aZone.IsNull() )
752   {
753     Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
754     if ( IsEqual( aRefRegion, theRegion ) )
755       aResAltitude = GetAltitudeForPoint( thePoint, aZone );
756   }
757
758   return aResAltitude;
759 }
760
761 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                  thePoint,
762                                                        const Handle(HYDROData_Zone)& theZone ) const
763 {
764   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
765   if ( theZone.IsNull() )
766     return aResAltitude;
767
768   HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
769   if ( !theZone->IsMergingNeed() )
770   {
771     aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
772   }
773   else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
774   {
775     return aResAltitude;
776   }
777
778   HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
779   if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
780   {
781     Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
782     if ( !aMergeAltitude.IsNull() )
783     {
784       if ( aZoneInterpolator != NULL )
785       {
786         aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
787         aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
788       }
789       else
790         aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
791     }
792   }
793   else
794   {
795     HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
796     HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
797     for ( ; anIter.More(); anIter.Next() )
798     {
799       Handle(HYDROData_Object) aZoneObj =
800         Handle(HYDROData_Object)::DownCast( anIter.Value() );
801       if ( aZoneObj.IsNull() )
802         continue;
803
804       Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
805       if ( anObjAltitude.IsNull() )
806         continue;
807
808       double aPointAltitude = 0.0;
809       if ( aZoneInterpolator != NULL )
810       {
811         aZoneInterpolator->SetAltitudeObject( anObjAltitude );
812         aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
813       }
814       else
815         aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
816
817       if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
818         continue;
819
820       if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
821       {
822         aResAltitude = aPointAltitude;
823         break;
824       }
825       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
826       {
827         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
828              aResAltitude > aPointAltitude )
829         {
830           aResAltitude = aPointAltitude;
831         }
832       }
833       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
834       {
835         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
836              aResAltitude < aPointAltitude )
837         {
838           aResAltitude = aPointAltitude;
839         }
840       }
841     }
842   }
843
844   return aResAltitude;
845 }
846
847 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
848   const NCollection_Sequence<gp_XY>& thePoints,
849   const Handle(HYDROData_Region)&    theRegion ) const
850 {
851   NCollection_Sequence<double> aResSeq;
852
853   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
854   {
855     const gp_XY& thePnt = thePoints.Value( i );
856     
857     double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
858     aResSeq.Append( anAltitude );
859   }
860
861   return aResSeq;
862 }
863
864 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
865   const NCollection_Sequence<gp_XY>& thePoints,
866   const Handle(HYDROData_Zone)&      theZone ) const
867 {
868   NCollection_Sequence<double> aResSeq;
869
870   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
871   {
872     const gp_XY& thePnt = thePoints.Value( i );
873     
874     double anAltitude = GetAltitudeForPoint( thePnt, theZone );
875     aResSeq.Append( anAltitude );
876   }
877
878   return aResSeq;
879 }
880
881 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
882 {
883   Handle(HYDROData_Region) aResRegion;
884
885   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
886   if ( !aZone.IsNull() )
887     aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
888
889   return aResRegion;
890 }
891
892 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
893 {
894   Handle(HYDROData_Zone) aResZone;
895
896   HYDROData_SequenceOfObjects aRegions = GetRegions();
897
898   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
899   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
900   {
901     Handle(HYDROData_Region) aRegion =
902       Handle(HYDROData_Region)::DownCast( anIter.Value() );
903     if ( aRegion.IsNull() )
904       continue;
905
906     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
907     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
908     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
909     {
910       Handle(HYDROData_Zone) aRegZone =
911         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
912       if ( aRegZone.IsNull() )
913         continue;
914
915       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
916       if ( aPointRelation != POINT_OUT )
917         aResZone = aRegZone; // We found the desired zone
918     }
919   }
920
921   return aResZone;
922 }
923
924 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
925   const gp_XY&                  thePoint,
926   const Handle(HYDROData_Zone)& theZone ) const
927 {
928   PointClassification aRes = POINT_OUT;
929   if ( theZone.IsNull() )
930     return aRes;
931
932   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
933   if ( aZoneFace.IsNull() )
934     return aRes;
935 #ifdef DEB_CLASS2D      
936           TopoDS_Compound aCmp;
937       BRep_Builder aBB;
938       aBB.MakeCompound(aCmp);
939           aBB.Add(aCmp, aZoneFace);
940           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
941           BRepBuilderAPI_MakeVertex aMk(aPnt);
942           aBB.Add(aCmp, aMk.Vertex());
943           BRepTools::Write(aCmp, "FCL2d.brep");
944 #endif  
945   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
946   if (State == TopAbs_OUT)
947     aRes =  POINT_OUT;
948   else if(State == TopAbs_IN)
949     aRes =  POINT_IN;
950   else if(State == TopAbs_ON)
951     aRes =  POINT_ON;
952   return aRes;
953 }
954
955 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
956                                                                   const QString& thePrefixOrName,
957                                                                   bool isPrefix )
958 {
959   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
960   int aTag = aNewLab.Tag();
961
962   Handle(HYDROData_Region) aNewRegion =
963     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
964   AddRegion( aNewRegion );
965
966   QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
967   aNewRegion->SetName( aRegionName );
968
969   return aNewRegion;
970 }
971
972 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
973 {
974   TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
975
976   Handle(HYDROData_SplittedShapesGroup) aNewGroup =
977     Handle(HYDROData_SplittedShapesGroup)::DownCast( 
978       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
979   AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
980
981   aNewGroup->SetName( theName );
982
983   return aNewGroup;
984 }
985
986 QString HYDROData_CalculationCase::Export( int theStudyId ) const
987 {
988   GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
989   SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
990   
991   QString aGeomObjEntry;
992   bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry );
993   return isOK ? aGeomObjEntry : QString();
994 }
995
996 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
997                                         SALOMEDS::Study_ptr theStudy,
998                                         QString& theGeomObjEntry ) const
999 {
1000   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
1001
1002   // Get groups definitions
1003   HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
1004
1005   HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
1006   for ( ; anIter.More(); anIter.Next() )
1007   {
1008     // Get shapes group
1009     Handle(HYDROData_ShapesGroup) aGroup =
1010       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
1011     if ( aGroup.IsNull() )
1012       continue;
1013
1014     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
1015
1016     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
1017     aGroup->GetShapes( aGroupDef.Shapes );
1018
1019     aSeqOfGroupsDefs.Append( aGroupDef );
1020   }
1021   
1022   // Get faces
1023   TopTools_ListOfShape aFaces;
1024   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
1025   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
1026   for ( ; aRegionIter.More(); aRegionIter.Next() )
1027   {
1028     Handle(HYDROData_Region) aRegion =
1029       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
1030     if( aRegion.IsNull() || !aRegion->IsSubmersible() )
1031       continue;
1032
1033     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
1034     aFaces.Append( aRegionShape );
1035   }
1036
1037   return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );
1038 }
1039
1040 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
1041                                         SALOMEDS::Study_ptr                           theStudy,
1042                                         const TopTools_ListOfShape&                   theFaces,
1043                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
1044                                         QString& theGeomObjEntry ) const
1045 {
1046   // Sew faces
1047   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
1048   aSewing.SetNonManifoldMode( Standard_False );
1049 #ifdef DEB_CALCULATION
1050   TCollection_AsciiString aNam("Sh_");
1051   int i=1;
1052 #endif
1053   TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
1054   for ( ; aFaceIter.More(); aFaceIter.Next() )
1055   {
1056     TopoDS_Shape aShape = aFaceIter.Value();
1057     if ( aShape.IsNull() )
1058       continue;
1059
1060     if ( aShape.ShapeType() == TopAbs_FACE )
1061     {
1062       aSewing.Add( aShape );
1063 #ifdef DEB_CALCULATION
1064       TCollection_AsciiString aName = aNam + ++i + ".brep";
1065       BRepTools::Write(aShape ,aName.ToCString());
1066 #endif
1067     }
1068     else
1069     {
1070 #ifdef DEB_CALCULATION
1071       int j = 1;
1072 #endif
1073       TopExp_Explorer anExp( aShape, TopAbs_FACE );
1074       for (; anExp.More(); anExp.Next() ) {
1075         aSewing.Add( anExp.Current() );
1076 #ifdef DEB_CALCULATION
1077
1078         TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
1079         BRepTools::Write(anExp.Current() ,aName.ToCString());
1080 #endif
1081       }
1082     }
1083   } // faces iterator
1084   
1085   aSewing.Perform();
1086   TopoDS_Shape aSewedShape = aSewing.SewedShape();
1087
1088   // If the sewed shape is empty - return false
1089   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1090     return false;
1091
1092 #ifdef DEB_CALCULATION
1093   BRepTools::Write(aSewedShape ,"Sew.brep");
1094 #endif
1095   // Publish the sewed shape
1096   QString aName = EXPORT_NAME;
1097   GEOM::GEOM_Object_ptr aMainShape = 
1098     HYDROData_GeomTool::publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
1099
1100   if ( aMainShape->_is_nil() )  
1101     return false;
1102
1103   if ( theGroupsDefs.IsEmpty() )
1104     return true;
1105
1106   // Create groups
1107   TopTools_IndexedMapOfShape aMapOfSubShapes;
1108   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1109
1110   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1111
1112   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1113   {
1114     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1115
1116     NCollection_Sequence<int> aGroupIndexes;
1117     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1118     {
1119       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1120 #ifdef DEB_CALCULATION
1121       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1122 #endif
1123       
1124       TopoDS_Shape aModifiedShape = aShape;
1125       if ( aSewing.IsModified( aShape ) )
1126         aModifiedShape = aSewing.Modified( aShape );
1127       else if ( aSewing.IsModifiedSubShape( aShape ) )
1128         aModifiedShape = aSewing.ModifiedSubShape( aShape );
1129
1130 #ifdef DEB_CALCULATION
1131       const TopLoc_Location& aL1 = aShape.Location();
1132       const TopLoc_Location& aL2 = aModifiedShape.Location();
1133       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1134 #endif
1135
1136       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1137       if ( anIndex > 0 ) {
1138         aGroupIndexes.Append( anIndex );
1139       } else {
1140 #ifdef DEB_CALCULATION    
1141         TCollection_AsciiString aNam("Lost_");
1142         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1143         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1144         {
1145            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1146            if ( aModifiedShape.IsPartner( aS ) )
1147            {
1148              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1149              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1150              BRepTools::Write(aS ,aName.ToCString());
1151             break;
1152            }
1153          }
1154         }
1155 #endif
1156       }
1157     }
1158     if ( !aGroupIndexes.IsEmpty() )
1159       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1160   }
1161  
1162   if ( !aGroupsData.IsEmpty() )
1163   {
1164     GEOM::GEOM_IGroupOperations_var aGroupOp = 
1165       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
1166
1167     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1168     for ( ; aMapIt.More(); aMapIt.Next() )
1169     {
1170       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
1171       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1172
1173       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1174       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1175         continue;
1176
1177       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1178       aGeomIndexes->length( aGroupIndexes.Length() );
1179
1180       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1181         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1182
1183       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1184       if ( aGroupOp->IsDone() )
1185       {
1186         SALOMEDS::SObject_var aGroupSO = 
1187           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1188       }
1189     }
1190   }
1191
1192   return true;
1193 }
1194
1195 void HYDROData_CalculationCase::ClearRules( HYDROData_CalculationCase::DataTag theDataTag,
1196                                             const bool theIsSetToUpdate )
1197 {
1198   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1199   HYDROData_PriorityQueue::ClearRules( aRulesLab );
1200
1201   // Indicate model of the need to update splitting
1202   if ( theIsSetToUpdate ) {
1203     SetToUpdate( true );
1204   }
1205 }
1206
1207 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Entity)&    theObject1,
1208                                          HYDROData_PriorityType             thePriority,
1209                                          const Handle(HYDROData_Entity)&    theObject2,
1210                                          HYDROData_Zone::MergeAltitudesType theMergeType,
1211                                          HYDROData_CalculationCase::DataTag theDataTag )
1212 {
1213   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1214   HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1215
1216   // Indicate model of the need to update splitting
1217   SetToUpdate( true );
1218 }
1219
1220 QString HYDROData_CalculationCase::DumpRules() const
1221 {
1222   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1223   return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1224 }
1225
1226 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1227 {
1228   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1229   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1230
1231   // Indicate model of the need to update splitting
1232   SetToUpdate( true );
1233 }
1234
1235 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1236 {
1237   Handle(TDataStd_Integer) aModeAttr;
1238   bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1239   if( isOK )
1240     return ( AssignmentMode ) aModeAttr->Get();
1241   else
1242     return MANUAL;
1243 }
1244
1245 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1246                                                    QStringList& theScript ) const
1247 {
1248   TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1249   HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1250 }
1251
1252 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1253 {
1254   return myLastWarning;
1255 }
1256
1257 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1258 {
1259   myLastWarning.Type = theType;
1260   myLastWarning.Data = theData;
1261 }
1262
1263 bool HYDROData_CalculationCase::GetRule( int theIndex, 
1264                                          Handle(HYDROData_Entity)&           theObject1,
1265                                          HYDROData_PriorityType&             thePriority,
1266                                          Handle(HYDROData_Entity)&           theObject2,
1267                                          HYDROData_Zone::MergeAltitudesType& theMergeType,
1268                                          HYDROData_CalculationCase::DataTag& theDataTag) const
1269 {
1270   TDF_Label aRulesLab = myLab.FindChild( theDataTag );
1271   return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1272     theObject1, thePriority, theObject2, theMergeType );
1273 }
1274
1275 void HYDROData_CalculationCase::SetAssignmentLandCoverMode( AssignmentMode theMode )
1276 {
1277   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentLandCoverMode );
1278   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1279
1280   // Indicate model of the need to update land covers partition
1281   SetToUpdate( true );
1282 }
1283
1284 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentLandCoverMode() const
1285 {
1286   Handle(TDataStd_Integer) aModeAttr;
1287   bool isOK = myLab.FindChild( DataTag_AssignmentLandCoverMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1288   if( isOK )
1289     return ( AssignmentMode ) aModeAttr->Get();
1290   else
1291     return MANUAL;
1292 }