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