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