Salome HOME
Rename of calculation case groups (Bug #349).
[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_SplitToZonesTool.h"
12 #include "HYDROData_SplittedShapesGroup.h"
13 #include "HYDROData_Region.h"
14 #include "HYDROData_Tool.h"
15 #include "HYDROData_Zone.h"
16
17 #include <GEOMBase.h>
18
19 #include <QSet>
20
21 #include <TopoDS.hxx>
22 #include <TopoDS_Shell.hxx>
23 #include <TopoDS_Edge.hxx>
24
25 #include <BRep_Builder.hxx>
26 #include <BRepBuilderAPI_Sewing.hxx>
27 #include <BRepTopAdaptor_FClass2d.hxx>
28
29 #include <BRepTools.hxx>
30
31 #include <TopAbs.hxx>
32 #include <TopExp_Explorer.hxx>
33 #include <TopExp.hxx>
34 #include <TopTools_ListOfShape.hxx>
35 #include <TopTools_ListIteratorOfListOfShape.hxx>
36
37 //#define  DEB_CALCULATION 1
38 #ifdef DEB_CALCULATION
39 #include <BRepTools.hxx>
40 #include <TopLoc_Location.hxx>
41 #endif 
42 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
43 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
44 #define CALCULATION_GROUPS_PREF GetName() + "_"
45 //#define DEB_CLASS2D 1
46 #ifdef DEB_CLASS2D
47 #include <BRepBuilderAPI_MakeVertex.hxx>
48 #endif
49
50 #define EXPORT_NAME "HYDRO_" + GetName()
51
52 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
53 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
54
55 HYDROData_CalculationCase::HYDROData_CalculationCase()
56 : HYDROData_Entity()
57 {
58 }
59
60 HYDROData_CalculationCase::~HYDROData_CalculationCase()
61 {
62 }
63
64 void HYDROData_CalculationCase::SetName( const QString& theName )
65 {
66   QString anOldCaseName = GetName();
67   if ( anOldCaseName != theName )
68   {
69     HYDROData_SequenceOfObjects aRegions = GetRegions();
70
71     HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
72     for ( ; anIter.More(); anIter.Next() )
73     {
74       Handle(HYDROData_Region) aRegion =
75         Handle(HYDROData_Region)::DownCast( anIter.Value() );
76       if ( aRegion.IsNull() )
77         continue;
78
79       HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
80
81       HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
82       HYDROData_SequenceOfObjects::Iterator anIter( aZones );
83       for ( ; anIter.More(); anIter.Next() )
84       {
85         Handle(HYDROData_Zone) aRegZone =
86           Handle(HYDROData_Zone)::DownCast( anIter.Value() );
87         if ( aRegZone.IsNull() )
88           continue;
89
90         HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
91       }
92     }
93
94     HYDROData_SequenceOfObjects aGroups = GetSplittedGroups();
95
96     anIter.Init( aGroups );
97     for ( ; anIter.More(); anIter.Next() )
98     {
99       Handle(HYDROData_SplittedShapesGroup) aGroup =
100         Handle(HYDROData_SplittedShapesGroup)::DownCast( anIter.Value() );
101       if ( aGroup.IsNull() )
102         continue;
103
104       HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
105     }
106   }
107
108   HYDROData_Entity::SetName( theName );
109 }
110
111 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
112 {
113   QStringList aResList = dumpObjectCreation( theTreatedObjects );
114
115   QString aCalculName = GetObjPyName();
116
117   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
118   HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
119   for ( ; anIter.More(); anIter.Next() )
120   {
121     Handle(HYDROData_Object) aRefGeomObj =
122       Handle(HYDROData_Object)::DownCast( anIter.Value() );
123     setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
124   }
125   aResList << QString( "" );
126
127   QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
128
129   HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
130   anIter.Init( aGeomGroups );
131   for ( ; anIter.More(); anIter.Next() )
132   {
133     Handle(HYDROData_ShapesGroup) aGeomGroup =
134       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
135     if ( aGeomGroup.IsNull() )
136       continue;
137
138     Handle(HYDROData_Object) aFatherGeom =
139       Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
140     if ( aFatherGeom.IsNull() )
141       continue;
142
143     int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
144     aResList << QString( "%1 = %2.GetGroup( %3 );" )
145               .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
146
147     aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
148   }
149   aResList << QString( "" );
150
151   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
152   setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
153   aResList << QString( "" );
154
155   aResList << QString( "%1.Update();" ).arg( aCalculName );
156   aResList << QString( "" );
157
158   // Now we restore the regions and zones order
159   HYDROData_SequenceOfObjects aRegions = GetRegions();
160   anIter.Init( aRegions );
161   for ( ; anIter.More(); anIter.Next() )
162   {
163     Handle(HYDROData_Region) aRegion =
164       Handle(HYDROData_Region)::DownCast( anIter.Value() );
165     if ( aRegion.IsNull() )
166       continue;
167
168     QString aRegionName = aRegion->GetName();
169
170     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
171     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
172     for ( ; aZonesIter.More(); aZonesIter.Next() )
173     {
174       Handle(HYDROData_Zone) aRegZone =
175         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
176       if ( aRegZone.IsNull() )
177         continue;
178
179       // TODO
180     }
181   }
182   aResList << QString( "" );
183
184   return aResList;
185 }
186
187 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
188 {
189   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
190
191   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
192   if ( !aBoundaryPolyline.IsNull() )
193     aResSeq.Append( aBoundaryPolyline );
194
195   HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
196   aResSeq.Append( aSeqOfRegions );
197
198   return aResSeq;
199 }
200
201 void HYDROData_CalculationCase::Update()
202 {
203   HYDROData_Entity::Update();
204
205   // At first we remove previously created objects
206   RemoveRegions();
207   RemoveSplittedGroups();
208
209   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
210   if ( aDocument.IsNull() )
211     return;
212
213   Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
214   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
215   if ( aGeomObjects.IsEmpty() )
216     return;
217
218   HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
219
220   HYDROData_SplitToZonesTool::SplitDataList aSplitedObjects =
221     HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
222   if ( aSplitedObjects.isEmpty() )
223     return;
224
225   QString aRegsPref = CALCULATION_REGIONS_PREF;
226   QString aZonesPref = CALCULATION_ZONES_PREF;
227
228   QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
229
230   // Create result regions for case, by default one zone for one region
231   HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitedObjects );
232   while( anIter.hasNext() )
233   {
234     const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
235
236     if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
237     {
238       // Create new region
239       Handle(HYDROData_Region) aRegion = addNewRegion();
240
241       QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
242       aRegion->SetName( aRegionName );
243
244       // Add the zone for region
245       Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone();
246
247       QString aZoneName = HYDROData_Tool::GenerateObjectName( aDocument, aZonesPref );
248       aRegionZone->SetName( aZoneName );
249
250       aRegionZone->SetShape( aSplitData.Face() );
251
252       // Add the reference object for zone
253       for ( int i = 0, n = aSplitData.ObjectNames.length(); i < n; ++i )
254       {
255         const QString& anObjName = aSplitData.ObjectNames.at( i );
256         
257         Handle(HYDROData_Object) aRefObject = 
258           Handle(HYDROData_Object)::DownCast( aDocument->FindObjectByName( anObjName ) );
259         if ( aRefObject.IsNull() )
260           continue;
261
262         aRegionZone->AddGeometryObject( aRefObject );
263       }
264     }
265     else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
266     {
267       // Create new edges group
268       if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
269         continue;
270
271       QString anObjName = aSplitData.ObjectNames.first();
272       if ( anObjName.isEmpty() )
273         continue;
274 #ifdef DEB_CALCULATION
275       QString aStr = aSplitData.ObjectNames.join(" "); 
276           cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl; 
277 #endif
278       Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
279       if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
280       {
281         aSplittedGroup = addNewSplittedGroup();
282
283         QString aCalcGroupName = CALCULATION_GROUPS_PREF + anObjName;
284         aSplittedGroup->SetName( aCalcGroupName );
285
286         aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
287       }
288       else
289       {
290         aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
291       }
292
293       if ( aSplittedGroup.IsNull() )
294         continue;
295
296       aSplittedGroup->AddShape( aSplitData.Shape );
297     }
298   }
299 }
300
301 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
302 {
303   if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
304     return false; // Wrong type of object
305
306   if ( HasReference( theObject, DataTag_GeometryObject ) )
307     return false; // Object is already in reference list
308
309   AddReferenceObject( theObject, DataTag_GeometryObject );
310   
311   // Indicate model of the need to update splitting
312   SetToUpdate( true );
313
314   return true;
315 }
316
317 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
318 {
319   return GetReferenceObjects( DataTag_GeometryObject );
320 }
321
322 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
323 {
324   if ( theObject.IsNull() )
325     return;
326
327   RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
328
329   // Indicate model of the need to update splitting
330   SetToUpdate( true );
331 }
332
333 void HYDROData_CalculationCase::RemoveGeometryObjects()
334 {
335   ClearReferenceObjects( DataTag_GeometryObject );
336
337   // Indicate model of the need to update splitting
338   SetToUpdate( true );
339 }
340
341 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
342 {
343   if ( theGroup.IsNull() )
344     return false;
345
346   if ( HasReference( theGroup, DataTag_GeometryGroup ) )
347     return false; // Object is already in reference list
348
349   AddReferenceObject( theGroup, DataTag_GeometryGroup );
350   
351   // Indicate model of the need to update splitting
352   SetToUpdate( true );
353
354   return true;
355 }
356
357 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
358 {
359   return GetReferenceObjects( DataTag_GeometryGroup );
360 }
361
362 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
363 {
364   if ( theGroup.IsNull() )
365     return;
366
367   RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
368
369   // Indicate model of the need to update splitting
370   SetToUpdate( true );
371 }
372
373 void HYDROData_CalculationCase::RemoveGeometryGroups()
374 {
375   ClearReferenceObjects( DataTag_GeometryGroup );
376
377   // Indicate model of the need to update splitting
378   SetToUpdate( true );
379 }
380
381 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
382 {
383   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
384
385   SetReferenceObject( thePolyline, DataTag_Polyline );
386
387   // Indicate model of the need to update zones splitting
388   SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
389 }
390
391 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
392 {
393   return Handle(HYDROData_PolylineXY)::DownCast( 
394            GetReferenceObject( DataTag_Polyline ) );
395 }
396
397 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
398 {
399   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
400
401   ClearReferenceObjects( DataTag_Polyline );
402
403   // Indicate model of the need to update zones splitting
404   SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
405 }
406
407 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
408 {
409   Handle(HYDROData_Region) aNewRegion = addNewRegion();
410   if ( aNewRegion.IsNull() )
411     return aNewRegion;
412
413   // Generate new name for new region
414   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
415   if ( !aDocument.IsNull() )
416   {
417     QString aRegsPref = CALCULATION_REGIONS_PREF;
418
419     QString aNewRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
420     aNewRegion->SetName( aNewRegionName );
421   }
422
423   aNewRegion->AddZone( theZone );
424
425   return aNewRegion;
426 }
427
428 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
429 {
430   if ( theRegion.IsNull() )
431     return false;
432   
433   if ( HasReference( theRegion, DataTag_Region ) )
434     return false; // Object is already in reference list
435
436   // Move the region from other calculation
437   Handle(HYDROData_CalculationCase) aFatherCalc = 
438     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
439   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
440   {
441     Handle(HYDROData_Region) aNewRegion = addNewRegion();
442     theRegion->CopyTo( aNewRegion );
443
444     aFatherCalc->RemoveRegion( theRegion );
445
446     theRegion->SetLabel( aNewRegion->Label() );
447   }
448   else
449   {
450     AddReferenceObject( theRegion, DataTag_Region );
451   }
452
453   return true;
454 }
455
456 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
457 {
458   return GetReferenceObjects( DataTag_Region );
459 }
460
461 void HYDROData_CalculationCase::UpdateRegionsOrder()
462 {
463   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
464   if ( aDocument.IsNull() )
465     return;
466
467   HYDROData_SequenceOfObjects aRegions = GetRegions();
468
469   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
470   for ( ; anIter.More(); anIter.Next() )
471   {
472     Handle(HYDROData_Region) aRegion =
473       Handle(HYDROData_Region)::DownCast( anIter.Value() );
474     if ( aRegion.IsNull() )
475       continue;
476
477     aRegion->SetName( "" );
478   }
479
480   QString aRegsPref = CALCULATION_REGIONS_PREF;
481
482   anIter.Init( aRegions );
483   for ( ; anIter.More(); anIter.Next() )
484   {
485     Handle(HYDROData_Region) aRegion =
486       Handle(HYDROData_Region)::DownCast( anIter.Value() );
487     if ( aRegion.IsNull() )
488       continue;
489
490     QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
491     aRegion->SetName( aRegionName );
492   }
493 }
494
495 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
496 {
497   if ( theRegion.IsNull() )
498     return;
499
500   RemoveReferenceObject( theRegion->Label(), DataTag_Region );
501
502   // Remove region from data model
503   Handle(HYDROData_CalculationCase) aFatherCalc = 
504     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
505   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
506     theRegion->Remove();
507 }
508
509 void HYDROData_CalculationCase::RemoveRegions()
510 {
511   myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
512 }
513
514 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
515 {
516   return GetReferenceObjects( DataTag_SplittedGroups );
517 }
518
519 void HYDROData_CalculationCase::RemoveSplittedGroups()
520 {
521   myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
522 }
523
524 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
525 {
526   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
527   return GetAltitudeForPoint( thePoint, aZone );
528 }
529
530 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                  thePoint,
531                                                        const Handle(HYDROData_Zone)& theZone ) const
532 {
533   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
534   if ( theZone.IsNull() )
535     return aResAltitude;
536
537   HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
538   if ( !theZone->IsMergingNeed() )
539   {
540     aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
541   }
542   else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
543   {
544     return aResAltitude;
545   }
546
547   HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
548   if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
549   {
550     Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
551     if ( !aMergeAltitude.IsNull() )
552     {
553       if ( aZoneInterpolator != NULL )
554       {
555         aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
556         aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
557       }
558       else
559         aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
560     }
561   }
562   else
563   {
564     HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
565     HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
566     for ( ; anIter.More(); anIter.Next() )
567     {
568       Handle(HYDROData_Object) aZoneObj =
569         Handle(HYDROData_Object)::DownCast( anIter.Value() );
570       if ( aZoneObj.IsNull() )
571         continue;
572
573       Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
574       if ( anObjAltitude.IsNull() )
575         continue;
576
577       double aPointAltitude = 0.0;
578       if ( aZoneInterpolator != NULL )
579       {
580         aZoneInterpolator->SetAltitudeObject( anObjAltitude );
581         aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
582       }
583       else
584         aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
585
586       if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
587         continue;
588
589       if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
590       {
591         aResAltitude = aPointAltitude;
592         break;
593       }
594       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
595       {
596         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
597              aResAltitude > aPointAltitude )
598         {
599           aResAltitude = aPointAltitude;
600         }
601       }
602       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
603       {
604         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
605              aResAltitude < aPointAltitude )
606         {
607           aResAltitude = aPointAltitude;
608         }
609       }
610     }
611   }
612
613   return aResAltitude;
614 }
615
616 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
617   const NCollection_Sequence<gp_XY>& thePoints,
618   const Handle(HYDROData_Zone)&      theZone ) const
619 {
620   NCollection_Sequence<double> aResSeq;
621
622   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
623   {
624     const gp_XY& thePnt = thePoints.Value( i );
625     
626     double anAltitude = GetAltitudeForPoint( thePnt, theZone );
627     aResSeq.Append( anAltitude );
628   }
629
630   return aResSeq;
631 }
632
633 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
634 {
635   Handle(HYDROData_Zone) aResZone;
636
637   HYDROData_SequenceOfObjects aRegions = GetRegions();
638
639   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
640   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
641   {
642     Handle(HYDROData_Region) aRegion =
643       Handle(HYDROData_Region)::DownCast( anIter.Value() );
644     if ( aRegion.IsNull() )
645       continue;
646
647     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
648     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
649     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
650     {
651       Handle(HYDROData_Zone) aRegZone =
652         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
653       if ( aRegZone.IsNull() )
654         continue;
655
656       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
657       if ( aPointRelation != POINT_OUT )
658         aResZone = aRegZone; // We found the desired zone
659     }
660   }
661
662   return aResZone;
663 }
664
665 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
666   const gp_XY&                  thePoint,
667   const Handle(HYDROData_Zone)& theZone ) const
668 {
669   PointClassification aRes = POINT_OUT;
670   if ( theZone.IsNull() )
671     return aRes;
672
673   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
674   if ( aZoneFace.IsNull() )
675     return aRes;
676 #ifdef DEB_CLASS2D      
677           TopoDS_Compound aCmp;
678       BRep_Builder aBB;
679       aBB.MakeCompound(aCmp);
680           aBB.Add(aCmp, aZoneFace);
681           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
682           BRepBuilderAPI_MakeVertex aMk(aPnt);
683           aBB.Add(aCmp, aMk.Vertex());
684           BRepTools::Write(aCmp, "FCL2d.brep");
685 #endif  
686   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
687   if (State == TopAbs_OUT)
688     aRes =  POINT_OUT;
689   else if(State == TopAbs_IN)
690     aRes =  POINT_IN;
691   else if(State == TopAbs_ON)
692     aRes =  POINT_ON;
693   return aRes;
694 }
695
696 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion()
697 {
698   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
699
700   Handle(HYDROData_Region) aNewRegion =
701     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
702   AddRegion( aNewRegion );
703
704   return aNewRegion;
705 }
706
707 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup()
708 {
709   TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
710
711   Handle(HYDROData_SplittedShapesGroup) aNewGroup =
712     Handle(HYDROData_SplittedShapesGroup)::DownCast( 
713       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
714   AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
715
716   return aNewGroup;
717 }
718
719 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
720                                         SALOMEDS::Study_ptr theStudy ) const
721 {
722   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
723
724   // Get groups definitions
725   HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
726
727   HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
728   for ( ; anIter.More(); anIter.Next() )
729   {
730     // Get shapes group
731     Handle(HYDROData_ShapesGroup) aGroup =
732       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
733     if ( aGroup.IsNull() )
734       continue;
735
736     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
737
738     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
739     aGroup->GetShapes( aGroupDef.Shapes );
740
741     aSeqOfGroupsDefs.Append( aGroupDef );
742   }
743   
744   // Get faces
745   TopTools_ListOfShape aFaces;
746   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
747   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
748   for ( ; aRegionIter.More(); aRegionIter.Next() )
749   {
750     Handle(HYDROData_Region) aRegion =
751       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
752     if( aRegion.IsNull() )
753       continue;
754
755     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
756     aFaces.Append( aRegionShape );
757   }
758
759   return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs );
760 }
761
762 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
763                                         SALOMEDS::Study_ptr                           theStudy,
764                                         const TopTools_ListOfShape&                   theFaces,
765                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs ) const
766 {
767   // Sew faces
768   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
769   aSewing.SetNonManifoldMode( Standard_False );
770 #ifdef DEB_CALCULATION
771   TCollection_AsciiString aNam("Sh_");
772   int i=1;
773 #endif
774   TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
775   for ( ; aFaceIter.More(); aFaceIter.Next() )
776   {
777     TopoDS_Shape aShape = aFaceIter.Value();
778     if ( aShape.IsNull() )
779       continue;
780
781     if ( aShape.ShapeType() == TopAbs_FACE )
782     {
783       aSewing.Add( aShape );
784 #ifdef DEB_CALCULATION
785       TCollection_AsciiString aName = aNam + ++i + ".brep";
786       BRepTools::Write(aShape ,aName.ToCString());
787 #endif
788     }
789     else
790     {
791 #ifdef DEB_CALCULATION
792       int j = 1;
793 #endif
794       TopExp_Explorer anExp( aShape, TopAbs_FACE );
795       for (; anExp.More(); anExp.Next() ) {
796         aSewing.Add( anExp.Current() );
797 #ifdef DEB_CALCULATION
798
799         TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
800         BRepTools::Write(anExp.Current() ,aName.ToCString());
801 #endif
802       }
803     }
804   } // faces iterator
805   
806   aSewing.Perform();
807   TopoDS_Shape aSewedShape = aSewing.SewedShape();
808
809   // If the sewed shape is empty - return false
810   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
811     return false;
812
813 #ifdef DEB_CALCULATION
814   BRepTools::Write(aSewedShape ,"Sew.brep");
815 #endif
816   // Publish the sewed shape
817   QString aName = EXPORT_NAME;
818   GEOM::GEOM_Object_ptr aMainShape = 
819     publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName );
820
821   if ( aMainShape->_is_nil() )  
822     return false;
823
824   if ( theGroupsDefs.IsEmpty() )
825     return true;
826
827   // Create groups
828   TopTools_IndexedMapOfShape aMapOfSubShapes;
829   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
830
831   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
832
833   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
834   {
835     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
836
837     NCollection_Sequence<int> aGroupIndexes;
838     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
839     {
840       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
841 #ifdef DEB_CALCULATION
842       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
843 #endif
844       
845       TopoDS_Shape aModifiedShape = aShape;
846       if ( aSewing.IsModified( aShape ) )
847         aModifiedShape = aSewing.Modified( aShape );
848       else if ( aSewing.IsModifiedSubShape( aShape ) )
849         aModifiedShape = aSewing.ModifiedSubShape( aShape );
850
851 #ifdef DEB_CALCULATION
852       const TopLoc_Location& aL1 = aShape.Location();
853       const TopLoc_Location& aL2 = aModifiedShape.Location();
854       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
855 #endif
856
857       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
858       if ( anIndex > 0 ) {
859         aGroupIndexes.Append( anIndex );
860       } else {
861 #ifdef DEB_CALCULATION    
862         TCollection_AsciiString aNam("Lost_");
863         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
864         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
865         {
866            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
867            if ( aModifiedShape.IsPartner( aS ) )
868            {
869              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
870              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
871              BRepTools::Write(aS ,aName.ToCString());
872             break;
873            }
874          }
875         }
876 #endif
877       }
878     }
879     if ( !aGroupIndexes.IsEmpty() )
880       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
881   }
882  
883   if ( !aGroupsData.IsEmpty() )
884   {
885     GEOM::GEOM_IGroupOperations_var aGroupOp = 
886       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
887
888     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
889     for ( ; aMapIt.More(); aMapIt.Next() )
890     {
891       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
892       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
893
894       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
895       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
896         continue;
897
898       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
899       aGeomIndexes->length( aGroupIndexes.Length() );
900
901       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
902         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
903
904       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
905       if ( aGroupOp->IsDone() )
906       {
907         SALOMEDS::SObject_var aGroupSO = 
908           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
909       }
910     }
911   }
912
913   return true;
914 }
915
916 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM( 
917   GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
918   const TopoDS_Shape& theShape, const QString& theName ) const
919 {
920   GEOM::GEOM_Object_var aGeomObj;
921
922   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
923        theShape.IsNull() ) {
924     return aGeomObj._retn();
925   }
926
927   std::ostringstream aStreamShape;
928   // Write TopoDS_Shape in ASCII format to the stream
929   BRepTools::Write( theShape, aStreamShape );
930   // Returns the number of bytes that have been stored in the stream's buffer.
931   int aSize = aStreamShape.str().size();
932   // Allocate octect buffer of required size
933   CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
934   // Copy ostrstream content to the octect buffer
935   memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
936   // Create TMPFile
937   SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
938
939   // Restore shape from the stream and get the GEOM object
940   GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
941   aGeomObj = anInsOp->RestoreShape( aSeqFile );
942   
943   // Puplish the GEOM object
944   if ( !aGeomObj->_is_nil() ) {
945     QString aName = GEOMBase::GetDefaultName( theName );
946
947     SALOMEDS::SObject_var aResultSO = 
948       theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), 
949                                      aGeomObj, qPrintable( aName ) );
950     if ( aResultSO->_is_nil() ) {
951       aGeomObj = GEOM::GEOM_Object::_nil();
952     }
953   }
954
955   return aGeomObj._retn();
956  }