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