]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_CalculationCase.cxx
Salome HOME
24.01.2014. Fix for bug of group edges history.
[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 #ifdef DEB_CALCULATION
271       QString aStr = aSplitData.ObjectNames.join(" "); 
272           cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl; 
273 #endif
274       Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
275       if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
276       {
277         aSplittedGroup = addNewSplittedGroup();
278
279         QString aCalcGroupName = CALCULATION_GROUPS_PREF + anObjName;
280         aSplittedGroup->SetName( aCalcGroupName );
281
282         aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
283       }
284       else
285       {
286         aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
287       }
288
289       if ( aSplittedGroup.IsNull() )
290         continue;
291
292       aSplittedGroup->AddShape( aSplitData.Shape );
293     }
294   }
295 }
296
297 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
298 {
299   if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
300     return false; // Wrong type of object
301
302   if ( HasReference( theObject, DataTag_GeometryObject ) )
303     return false; // Object is already in reference list
304
305   AddReferenceObject( theObject, DataTag_GeometryObject );
306   
307   // Indicate model of the need to update splitting
308   SetToUpdate( true );
309
310   return true;
311 }
312
313 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
314 {
315   return GetReferenceObjects( DataTag_GeometryObject );
316 }
317
318 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
319 {
320   if ( theObject.IsNull() )
321     return;
322
323   RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
324
325   // Indicate model of the need to update splitting
326   SetToUpdate( true );
327 }
328
329 void HYDROData_CalculationCase::RemoveGeometryObjects()
330 {
331   ClearReferenceObjects( DataTag_GeometryObject );
332
333   // Indicate model of the need to update splitting
334   SetToUpdate( true );
335 }
336
337 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
338 {
339   if ( theGroup.IsNull() )
340     return false;
341
342   if ( HasReference( theGroup, DataTag_GeometryGroup ) )
343     return false; // Object is already in reference list
344
345   AddReferenceObject( theGroup, DataTag_GeometryGroup );
346   
347   // Indicate model of the need to update splitting
348   SetToUpdate( true );
349
350   return true;
351 }
352
353 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
354 {
355   return GetReferenceObjects( DataTag_GeometryGroup );
356 }
357
358 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
359 {
360   if ( theGroup.IsNull() )
361     return;
362
363   RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
364
365   // Indicate model of the need to update splitting
366   SetToUpdate( true );
367 }
368
369 void HYDROData_CalculationCase::RemoveGeometryGroups()
370 {
371   ClearReferenceObjects( DataTag_GeometryGroup );
372
373   // Indicate model of the need to update splitting
374   SetToUpdate( true );
375 }
376
377 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
378 {
379   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
380
381   SetReferenceObject( thePolyline, DataTag_Polyline );
382
383   // Indicate model of the need to update zones splitting
384   SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
385 }
386
387 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
388 {
389   return Handle(HYDROData_PolylineXY)::DownCast( 
390            GetReferenceObject( DataTag_Polyline ) );
391 }
392
393 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
394 {
395   Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
396
397   ClearReferenceObjects( DataTag_Polyline );
398
399   // Indicate model of the need to update zones splitting
400   SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
401 }
402
403 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
404 {
405   Handle(HYDROData_Region) aNewRegion = addNewRegion();
406   if ( aNewRegion.IsNull() )
407     return aNewRegion;
408
409   // Generate new name for new region
410   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
411   if ( !aDocument.IsNull() )
412   {
413     QString aRegsPref = CALCULATION_REGIONS_PREF;
414
415     QString aNewRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
416     aNewRegion->SetName( aNewRegionName );
417   }
418
419   aNewRegion->AddZone( theZone );
420
421   return aNewRegion;
422 }
423
424 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
425 {
426   if ( theRegion.IsNull() )
427     return false;
428   
429   if ( HasReference( theRegion, DataTag_Region ) )
430     return false; // Object is already in reference list
431
432   // Move the region from other calculation
433   Handle(HYDROData_CalculationCase) aFatherCalc = 
434     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
435   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
436   {
437     Handle(HYDROData_Region) aNewRegion = addNewRegion();
438     theRegion->CopyTo( aNewRegion );
439
440     aFatherCalc->RemoveRegion( theRegion );
441
442     theRegion->SetLabel( aNewRegion->Label() );
443   }
444   else
445   {
446     AddReferenceObject( theRegion, DataTag_Region );
447   }
448
449   return true;
450 }
451
452 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
453 {
454   return GetReferenceObjects( DataTag_Region );
455 }
456
457 void HYDROData_CalculationCase::UpdateRegionsOrder()
458 {
459   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
460   if ( aDocument.IsNull() )
461     return;
462
463   HYDROData_SequenceOfObjects aRegions = GetRegions();
464
465   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
466   for ( ; anIter.More(); anIter.Next() )
467   {
468     Handle(HYDROData_Region) aRegion =
469       Handle(HYDROData_Region)::DownCast( anIter.Value() );
470     if ( aRegion.IsNull() )
471       continue;
472
473     aRegion->SetName( "" );
474   }
475
476   QString aRegsPref = CALCULATION_REGIONS_PREF;
477
478   anIter.Init( aRegions );
479   for ( ; anIter.More(); anIter.Next() )
480   {
481     Handle(HYDROData_Region) aRegion =
482       Handle(HYDROData_Region)::DownCast( anIter.Value() );
483     if ( aRegion.IsNull() )
484       continue;
485
486     QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
487     aRegion->SetName( aRegionName );
488   }
489 }
490
491 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
492 {
493   if ( theRegion.IsNull() )
494     return;
495
496   RemoveReferenceObject( theRegion->Label(), DataTag_Region );
497
498   // Remove region from data model
499   Handle(HYDROData_CalculationCase) aFatherCalc = 
500     Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
501   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
502     theRegion->Remove();
503 }
504
505 void HYDROData_CalculationCase::RemoveRegions()
506 {
507   myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
508 }
509
510 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
511 {
512   return GetReferenceObjects( DataTag_SplittedGroups );
513 }
514
515 void HYDROData_CalculationCase::RemoveSplittedGroups()
516 {
517   myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
518 }
519
520 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
521 {
522   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
523   return GetAltitudeForPoint( thePoint, aZone );
524 }
525
526 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                  thePoint,
527                                                        const Handle(HYDROData_Zone)& theZone ) const
528 {
529   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
530   if ( theZone.IsNull() )
531     return aResAltitude;
532
533   HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
534   if ( !theZone->IsMergingNeed() )
535   {
536     aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
537   }
538   else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
539   {
540     return aResAltitude;
541   }
542
543   if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
544   {
545     Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
546     if ( !aMergeAltitude.IsNull() )
547       aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
548   }
549   else
550   {
551     HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
552     HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
553     for ( ; anIter.More(); anIter.Next() )
554     {
555       Handle(HYDROData_Object) aZoneObj =
556         Handle(HYDROData_Object)::DownCast( anIter.Value() );
557       if ( aZoneObj.IsNull() )
558         continue;
559
560       Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
561       if ( anObjAltitude.IsNull() )
562         continue;
563
564       double aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
565       if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
566         continue;
567
568       if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
569       {
570         aResAltitude = aPointAltitude;
571         break;
572       }
573       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
574       {
575         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
576              aResAltitude > aPointAltitude )
577         {
578           aResAltitude = aPointAltitude;
579         }
580       }
581       else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
582       {
583         if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
584              aResAltitude < aPointAltitude )
585         {
586           aResAltitude = aPointAltitude;
587         }
588       }
589     }
590   }
591
592   return aResAltitude;
593 }
594
595 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints( 
596   const NCollection_Sequence<gp_XY>& thePoints,
597   const Handle(HYDROData_Zone)&      theZone ) const
598 {
599   NCollection_Sequence<double> aResSeq;
600
601   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
602   {
603     const gp_XY& thePnt = thePoints.Value( i );
604     
605     double anAltitude = GetAltitudeForPoint( thePnt, theZone );
606     aResSeq.Append( anAltitude );
607   }
608
609   return aResSeq;
610 }
611
612 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
613 {
614   Handle(HYDROData_Zone) aResZone;
615
616   HYDROData_SequenceOfObjects aRegions = GetRegions();
617
618   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
619   for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
620   {
621     Handle(HYDROData_Region) aRegion =
622       Handle(HYDROData_Region)::DownCast( anIter.Value() );
623     if ( aRegion.IsNull() )
624       continue;
625
626     HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
627     HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
628     for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
629     {
630       Handle(HYDROData_Zone) aRegZone =
631         Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
632       if ( aRegZone.IsNull() )
633         continue;
634
635       PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
636       if ( aPointRelation != POINT_OUT )
637         aResZone = aRegZone; // We found the desired zone
638     }
639   }
640
641   return aResZone;
642 }
643
644 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
645   const gp_XY&                  thePoint,
646   const Handle(HYDROData_Zone)& theZone ) const
647 {
648   PointClassification aRes = POINT_OUT;
649   if ( theZone.IsNull() )
650     return aRes;
651
652   TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
653   if ( aZoneFace.IsNull() )
654     return aRes;
655   
656   TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
657   if (State == TopAbs_OUT)
658     aRes =  POINT_OUT;
659   else if(State == TopAbs_IN)
660     aRes =  POINT_IN;
661   else if(State == TopAbs_ON)
662     aRes =  POINT_ON;
663   return aRes;
664 }
665
666 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion()
667 {
668   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
669
670   Handle(HYDROData_Region) aNewRegion =
671     Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
672   AddRegion( aNewRegion );
673
674   return aNewRegion;
675 }
676
677 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup()
678 {
679   TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
680
681   Handle(HYDROData_SplittedShapesGroup) aNewGroup =
682     Handle(HYDROData_SplittedShapesGroup)::DownCast( 
683       HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
684   AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
685
686   return aNewGroup;
687 }
688
689 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
690                                         SALOMEDS::Study_ptr theStudy ) const
691 {
692   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
693
694   // Get groups definitions
695   HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
696
697   HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
698   for ( ; anIter.More(); anIter.Next() )
699   {
700     // Get shapes group
701     Handle(HYDROData_ShapesGroup) aGroup =
702       Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
703     if ( aGroup.IsNull() )
704       continue;
705
706     HYDROData_ShapesGroup::GroupDefinition aGroupDef;
707
708     aGroupDef.Name = aGroup->GetName().toLatin1().constData();
709     aGroup->GetShapes( aGroupDef.Shapes );
710
711     aSeqOfGroupsDefs.Append( aGroupDef );
712   }
713   
714   // Get faces
715   TopTools_ListOfShape aFaces;
716   HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
717   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
718   for ( ; aRegionIter.More(); aRegionIter.Next() )
719   {
720     Handle(HYDROData_Region) aRegion =
721       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
722     if( aRegion.IsNull() )
723       continue;
724
725     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
726     aFaces.Append( aRegionShape );
727   }
728
729   return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs );
730 }
731
732 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
733                                         SALOMEDS::Study_ptr                           theStudy,
734                                         const TopTools_ListOfShape&                   theFaces,
735                                         const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs ) const
736 {
737   // Sew faces
738   BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
739   aSewing.SetNonManifoldMode( Standard_False );
740 #ifdef DEB_CALCULATION
741   TCollection_AsciiString aNam("Sh_");
742   int i=1;
743 #endif
744   TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
745   for ( ; aFaceIter.More(); aFaceIter.Next() )
746   {
747     TopoDS_Shape aShape = aFaceIter.Value();
748     if ( aShape.IsNull() )
749       continue;
750
751     if ( aShape.ShapeType() == TopAbs_FACE )
752     {
753       aSewing.Add( aShape );
754 #ifdef DEB_CALCULATION
755       TCollection_AsciiString aName = aNam + ++i + ".brep";
756       BRepTools::Write(aShape ,aName.ToCString());
757 #endif
758     }
759     else
760     {
761 #ifdef DEB_CALCULATION
762       int j = 1;
763 #endif
764       TopExp_Explorer anExp( aShape, TopAbs_FACE );
765       for (; anExp.More(); anExp.Next() ) {
766         aSewing.Add( anExp.Current() );
767 #ifdef DEB_CALCULATION
768
769         TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
770         BRepTools::Write(anExp.Current() ,aName.ToCString());
771 #endif
772       }
773     }
774   } // faces iterator
775   
776   aSewing.Perform();
777   TopoDS_Shape aSewedShape = aSewing.SewedShape();
778
779   // If the sewed shape is empty - return false
780   if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
781     return false;
782
783 #ifdef DEB_CALCULATION
784   BRepTools::Write(aSewedShape ,"Sew.brep");
785 #endif
786   // Publish the sewed shape
787   QString aName = EXPORT_NAME;
788   GEOM::GEOM_Object_ptr aMainShape = 
789     publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName );
790
791   if ( aMainShape->_is_nil() )  
792     return false;
793
794   if ( theGroupsDefs.IsEmpty() )
795     return true;
796
797   // Create groups
798   TopTools_IndexedMapOfShape aMapOfSubShapes;
799   TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
800
801   NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
802
803   for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
804   {
805     const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
806
807     NCollection_Sequence<int> aGroupIndexes;
808     for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
809     {
810       const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
811 #ifdef DEB_CALCULATION
812       cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
813 #endif
814       const TopoDS_Shape& aModifiedShape = aSewing.Modified( aShape );
815       if ( aModifiedShape.IsNull() )
816         continue;
817
818 #ifdef DEB_CALCULATION
819       const TopLoc_Location& aL1 = aShape.Location();
820       const TopLoc_Location& aL2 = aModifiedShape.Location();
821       cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
822 #endif
823
824       int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
825       if ( anIndex > 0 ) {
826         aGroupIndexes.Append( anIndex );
827       } else {
828 #ifdef DEB_CALCULATION    
829         TCollection_AsciiString aNam("Lost_");
830         if(!aMapOfSubShapes.Contains(aModifiedShape)) {
831         for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
832         {
833            const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
834            if ( aModifiedShape.IsPartner( aS ) )
835            {
836              cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
837              TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
838              BRepTools::Write(aS ,aName.ToCString());
839             break;
840            }
841          }
842         }
843 #endif
844       }
845     }
846     if ( !aGroupIndexes.IsEmpty() )
847       aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
848   }
849  
850   if ( !aGroupsData.IsEmpty() )
851   {
852     GEOM::GEOM_IGroupOperations_var aGroupOp = 
853       theGeomEngine->GetIGroupOperations( theStudy->StudyId() );  
854
855     NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
856     for ( ; aMapIt.More(); aMapIt.Next() )
857     {
858       const TCollection_AsciiString& aGroupName = aMapIt.Key(); 
859       const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
860
861       GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
862       if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
863         continue;
864
865       GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
866       aGeomIndexes->length( aGroupIndexes.Length() );
867
868       for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
869         aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
870
871       aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
872       if ( aGroupOp->IsDone() )
873       {
874         SALOMEDS::SObject_var aGroupSO = 
875           theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
876       }
877     }
878   }
879
880   return true;
881 }
882
883 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM( 
884   GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
885   const TopoDS_Shape& theShape, const QString& theName ) const
886 {
887   GEOM::GEOM_Object_var aGeomObj;
888
889   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
890        theShape.IsNull() ) {
891     return aGeomObj._retn();
892   }
893
894   std::ostringstream aStreamShape;
895   // Write TopoDS_Shape in ASCII format to the stream
896   BRepTools::Write( theShape, aStreamShape );
897   // Returns the number of bytes that have been stored in the stream's buffer.
898   int aSize = aStreamShape.str().size();
899   // Allocate octect buffer of required size
900   CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
901   // Copy ostrstream content to the octect buffer
902   memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
903   // Create TMPFile
904   SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
905
906   // Restore shape from the stream and get the GEOM object
907   GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
908   aGeomObj = anInsOp->RestoreShape( aSeqFile );
909   
910   // Puplish the GEOM object
911   if ( !aGeomObj->_is_nil() ) {
912     QString aName = GEOMBase::GetDefaultName( theName );
913
914     SALOMEDS::SObject_var aResultSO = 
915       theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), 
916                                      aGeomObj, qPrintable( aName ) );
917     if ( aResultSO->_is_nil() ) {
918       aGeomObj = GEOM::GEOM_Object::_nil();
919     }
920   }
921
922   return aGeomObj._retn();
923  }