Salome HOME
Get altitude from region implementation.
[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_Region)& theRegion ) const
532 {
533   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
534
535   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
536   if ( !aZone.IsNull() )
537   {
538     Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
539     if ( IsEqual( aRefRegion, theRegion ) )
540       aResAltitude = GetAltitudeForPoint( thePoint, aZone );
541   }
542
543   return aResAltitude;
544 }
545
546 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                  thePoint,
547                                                        const Handle(HYDROData_Zone)& theZone ) const
548 {
549   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
550   if ( theZone.IsNull() )
551     return aResAltitude;
552
553   HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
554   if ( !theZone->IsMergingNeed() )
555   {
556     aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
557   }
558   else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
559   {
560     return aResAltitude;
561   }
562
563   HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
564   if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
565   {
566     Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
567     if ( !aMergeAltitude.IsNull() )
568     {
569       if ( aZoneInterpolator != NULL )
570       {
571         aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
572         aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
573       }
574       else
575         aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
576     }
577   }
578   else
579   {
580     HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
581     HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
582     for ( ; anIter.More(); anIter.Next() )
583     {
584       Handle(HYDROData_Object) aZoneObj =
585         Handle(HYDROData_Object)::DownCast( anIter.Value() );
586       if ( aZoneObj.IsNull() )
587         continue;
588
589       Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
590       if ( anObjAltitude.IsNull() )
591         continue;
592
593       double aPointAltitude = 0.0;
594       if ( aZoneInterpolator != NULL )
595       {
596         aZoneInterpolator->SetAltitudeObject( anObjAltitude );
597         aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
598       }
599       else
600         aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
601
602       if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
603         continue;
604
605       if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
606       {
607         aResAltitude = aPointAltitude;
608         break;
609       }
610       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
611       {
612         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
613              aResAltitude > aPointAltitude )
614         {
615           aResAltitude = aPointAltitude;
616         }
617       }
618       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
619       {
620         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
621              aResAltitude < aPointAltitude )
622         {
623           aResAltitude = aPointAltitude;
624         }
625       }
626     }
627   }
628
629   return aResAltitude;
630 }
631
632 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
633   const NCollection_Sequence<gp_XY>& thePoints,
634   const Handle(HYDROData_Region)&    theRegion ) const
635 {
636   NCollection_Sequence<double> aResSeq;
637
638   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
639   {
640     const gp_XY& thePnt = thePoints.Value( i );
641     
642     double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
643     aResSeq.Append( anAltitude );
644   }
645
646   return aResSeq;
647 }
648
649 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
650   const NCollection_Sequence<gp_XY>& thePoints,
651   const Handle(HYDROData_Zone)&      theZone ) const
652 {
653   NCollection_Sequence<double> aResSeq;
654
655   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
656   {
657     const gp_XY& thePnt = thePoints.Value( i );
658     
659     double anAltitude = GetAltitudeForPoint( thePnt, theZone );
660     aResSeq.Append( anAltitude );
661   }
662
663   return aResSeq;
664 }
665
666 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
667 {
668   Handle(HYDROData_Region) aResRegion;
669
670   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
671   if ( !aZone.IsNull() )
672     aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
673
674   return aResRegion;
675 }
676
677 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
678 {
679   Handle(HYDROData_Zone) aResZone;
680
681   HYDROData_SequenceOfObjects aRegions = GetRegions();
682
683   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
684   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
685   {
686     Handle(HYDROData_Region) aRegion =
687       Handle(HYDROData_Region)::DownCast( anIter.Value() );
688     if ( aRegion.IsNull() )
689       continue;
690
691     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
692     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
693     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
694     {
695       Handle(HYDROData_Zone) aRegZone =
696         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
697       if ( aRegZone.IsNull() )
698         continue;
699
700       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
701       if ( aPointRelation != POINT_OUT )
702         aResZone = aRegZone; // We found the desired zone
703     }
704   }
705
706   return aResZone;
707 }
708
709 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
710   const gp_XY&                  thePoint,
711   const Handle(HYDROData_Zone)& theZone ) const
712 {
713   PointClassification aRes = POINT_OUT;
714   if ( theZone.IsNull() )
715     return aRes;
716
717   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
718   if ( aZoneFace.IsNull() )
719     return aRes;
720 #ifdef DEB_CLASS2D      
721           TopoDS_Compound aCmp;
722       BRep_Builder aBB;
723       aBB.MakeCompound(aCmp);
724           aBB.Add(aCmp, aZoneFace);
725           gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
726           BRepBuilderAPI_MakeVertex aMk(aPnt);
727           aBB.Add(aCmp, aMk.Vertex());
728           BRepTools::Write(aCmp, "FCL2d.brep");
729 #endif  
730   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
731   if (State == TopAbs_OUT)
732     aRes =  POINT_OUT;
733   else if(State == TopAbs_IN)
734     aRes =  POINT_IN;
735   else if(State == TopAbs_ON)
736     aRes =  POINT_ON;
737   return aRes;
738 }
739
740 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion()
741 {
742   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
743
744   Handle(HYDROData_Region) aNewRegion =
745     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
746   AddRegion( aNewRegion );
747
748   return aNewRegion;
749 }
750
751 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup()
752 {
753   TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
754
755   Handle(HYDROData_SplittedShapesGroup) aNewGroup =
756     Handle(HYDROData_SplittedShapesGroup)::DownCast( 
757       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
758   AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
759
760   return aNewGroup;
761 }
762
763 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
764                                         SALOMEDS::Study_ptr theStudy ) const
765 {
766   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
767
768   // Get groups definitions
769   HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
770
771   HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
772   for ( ; anIter.More(); anIter.Next() )
773   {
774     // Get shapes group
775     Handle(HYDROData_ShapesGroup) aGroup =
776       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
777     if ( aGroup.IsNull() )
778       continue;
779
780     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
781
782     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
783     aGroup->GetShapes( aGroupDef.Shapes );
784
785     aSeqOfGroupsDefs.Append( aGroupDef );
786   }
787   
788   // Get faces
789   TopTools_ListOfShape aFaces;
790   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
791   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
792   for ( ; aRegionIter.More(); aRegionIter.Next() )
793   {
794     Handle(HYDROData_Region) aRegion =
795       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
796     if( aRegion.IsNull() )
797       continue;
798
799     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
800     aFaces.Append( aRegionShape );
801   }
802
803   return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs );
804 }
805
806 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
807                                         SALOMEDS::Study_ptr                           theStudy,
808                                         const TopTools_ListOfShape&                   theFaces,
809                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs ) const
810 {
811   // Sew faces
812   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
813   aSewing.SetNonManifoldMode( Standard_False );
814 #ifdef DEB_CALCULATION
815   TCollection_AsciiString aNam("Sh_");
816   int i=1;
817 #endif
818   TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
819   for ( ; aFaceIter.More(); aFaceIter.Next() )
820   {
821     TopoDS_Shape aShape = aFaceIter.Value();
822     if ( aShape.IsNull() )
823       continue;
824
825     if ( aShape.ShapeType() == TopAbs_FACE )
826     {
827       aSewing.Add( aShape );
828 #ifdef DEB_CALCULATION
829       TCollection_AsciiString aName = aNam + ++i + ".brep";
830       BRepTools::Write(aShape ,aName.ToCString());
831 #endif
832     }
833     else
834     {
835 #ifdef DEB_CALCULATION
836       int j = 1;
837 #endif
838       TopExp_Explorer anExp( aShape, TopAbs_FACE );
839       for (; anExp.More(); anExp.Next() ) {
840         aSewing.Add( anExp.Current() );
841 #ifdef DEB_CALCULATION
842
843         TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
844         BRepTools::Write(anExp.Current() ,aName.ToCString());
845 #endif
846       }
847     }
848   } // faces iterator
849   
850   aSewing.Perform();
851   TopoDS_Shape aSewedShape = aSewing.SewedShape();
852
853   // If the sewed shape is empty - return false
854   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
855     return false;
856
857 #ifdef DEB_CALCULATION
858   BRepTools::Write(aSewedShape ,"Sew.brep");
859 #endif
860   // Publish the sewed shape
861   QString aName = EXPORT_NAME;
862   GEOM::GEOM_Object_ptr aMainShape = 
863     publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName );
864
865   if ( aMainShape->_is_nil() )  
866     return false;
867
868   if ( theGroupsDefs.IsEmpty() )
869     return true;
870
871   // Create groups
872   TopTools_IndexedMapOfShape aMapOfSubShapes;
873   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
874
875   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
876
877   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
878   {
879     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
880
881     NCollection_Sequence<int> aGroupIndexes;
882     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
883     {
884       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
885 #ifdef DEB_CALCULATION
886       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
887 #endif
888       
889       TopoDS_Shape aModifiedShape = aShape;
890       if ( aSewing.IsModified( aShape ) )
891         aModifiedShape = aSewing.Modified( aShape );
892       else if ( aSewing.IsModifiedSubShape( aShape ) )
893         aModifiedShape = aSewing.ModifiedSubShape( aShape );
894
895 #ifdef DEB_CALCULATION
896       const TopLoc_Location& aL1 = aShape.Location();
897       const TopLoc_Location& aL2 = aModifiedShape.Location();
898       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
899 #endif
900
901       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
902       if ( anIndex > 0 ) {
903         aGroupIndexes.Append( anIndex );
904       } else {
905 #ifdef DEB_CALCULATION    
906         TCollection_AsciiString aNam("Lost_");
907         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
908         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
909         {
910            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
911            if ( aModifiedShape.IsPartner( aS ) )
912            {
913              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
914              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
915              BRepTools::Write(aS ,aName.ToCString());
916             break;
917            }
918          }
919         }
920 #endif
921       }
922     }
923     if ( !aGroupIndexes.IsEmpty() )
924       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
925   }
926  
927   if ( !aGroupsData.IsEmpty() )
928   {
929     GEOM::GEOM_IGroupOperations_var aGroupOp = 
930       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
931
932     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
933     for ( ; aMapIt.More(); aMapIt.Next() )
934     {
935       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
936       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
937
938       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
939       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
940         continue;
941
942       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
943       aGeomIndexes->length( aGroupIndexes.Length() );
944
945       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
946         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
947
948       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
949       if ( aGroupOp->IsDone() )
950       {
951         SALOMEDS::SObject_var aGroupSO = 
952           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
953       }
954     }
955   }
956
957   return true;
958 }
959
960 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM( 
961   GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
962   const TopoDS_Shape& theShape, const QString& theName ) const
963 {
964   GEOM::GEOM_Object_var aGeomObj;
965
966   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
967        theShape.IsNull() ) {
968     return aGeomObj._retn();
969   }
970
971   std::ostringstream aStreamShape;
972   // Write TopoDS_Shape in ASCII format to the stream
973   BRepTools::Write( theShape, aStreamShape );
974   // Returns the number of bytes that have been stored in the stream's buffer.
975   int aSize = aStreamShape.str().size();
976   // Allocate octect buffer of required size
977   CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
978   // Copy ostrstream content to the octect buffer
979   memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
980   // Create TMPFile
981   SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
982
983   // Restore shape from the stream and get the GEOM object
984   GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
985   aGeomObj = anInsOp->RestoreShape( aSeqFile );
986   
987   // Puplish the GEOM object
988   if ( !aGeomObj->_is_nil() ) {
989     QString aName = GEOMBase::GetDefaultName( theName );
990
991     SALOMEDS::SObject_var aResultSO = 
992       theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), 
993                                      aGeomObj, qPrintable( aName ) );
994     if ( aResultSO->_is_nil() ) {
995       aGeomObj = GEOM::GEOM_Object::_nil();
996     }
997   }
998
999   return aGeomObj._retn();
1000  }