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