Salome HOME
The data model has been rolled back to previous version.
[modules/hydro.git] / src / HYDROData / HYDROData_Region.cxx
1
2 #include "HYDROData_Region.h"
3
4 #include "HYDROData_CalculationCase.h"
5 #include "HYDROData_Document.h"
6 #include "HYDROData_Iterator.h"
7 #include "HYDROData_Zone.h"
8
9 #include <TopoDS.hxx>
10 #include <TopoDS_Shape.hxx>
11 #include <TopoDS_Shell.hxx>
12 #include <TopoDS_Face.hxx>
13 #include <TopTools_ListOfShape.hxx>
14 #include <TopTools_ListIteratorOfListOfShape.hxx>
15 #include <TopTools_IndexedMapOfShape.hxx>
16 #include <TopExp.hxx>
17 #include <BRep_Builder.hxx>
18 #include <BRepAlgoAPI_Fuse.hxx>
19 #include <ShapeUpgrade_UnifySameDomain.hxx>
20
21 #include <QStringList>
22
23 #define PYTHON_REGION_ID "KIND_REGION"
24
25 IMPLEMENT_STANDARD_HANDLE(HYDROData_Region, HYDROData_Entity)
26 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Region, HYDROData_Entity)
27
28
29 HYDROData_Region::HYDROData_Region()
30  : HYDROData_Entity()
31 {
32 }
33
34 HYDROData_Region::~HYDROData_Region()
35 {
36 }
37
38 QStringList HYDROData_Region::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
39 {
40   QStringList aResList;
41
42   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
43   if ( aDocument.IsNull() )
44     return aResList;
45
46   QString aDocName = aDocument->GetDocPyName();
47   QString aRegionName = GetName();
48
49   aResList << QString( "%1 = %2.CreateObject( %3 );" )
50               .arg( aRegionName ).arg( aDocName ).arg( PYTHON_REGION_ID );
51   aResList << QString( "%1.SetName( \"%2\" );" )
52               .arg( aRegionName ).arg( aRegionName );
53   aResList << QString( "" );
54
55   HYDROData_SequenceOfObjects aZones = GetZones();
56   HYDROData_SequenceOfObjects::Iterator anIter( aZones );
57   for ( ; anIter.More(); anIter.Next() )
58   {
59     Handle(HYDROData_Zone) aRefZone =
60       Handle(HYDROData_Zone)::DownCast( anIter.Value() );
61     if ( !aRefZone.IsNull() )
62       setPythonReferenceObject( theTreatedObjects, aResList, aRefZone, "AddZone" );
63   }
64   aResList << QString( "" );
65
66   return aResList;
67 }
68
69 void HYDROData_Region::Remove()
70 {
71   Handle(HYDROData_CalculationCase) aFatherCalc = 
72     Handle(HYDROData_CalculationCase)::DownCast( GetFatherObject() );
73
74   HYDROData_Entity::Remove();
75
76   if ( !aFatherCalc.IsNull() )
77     aFatherCalc->UpdateRegionsOrder();
78 }
79
80 bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
81 {
82   if ( theZone.IsNull() )
83     return false;
84   
85   if ( HasReference( theZone, DataTag_Zone ) )
86     return false; // Object is already in reference list
87
88   // Move the zone from other region
89   Handle(HYDROData_Region) aFatherRegion = 
90     Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
91   if ( !aFatherRegion.IsNull() && aFatherRegion->Label() != myLab )
92   {
93     Handle(HYDROData_Zone) aNewZone = addNewZone();
94     theZone->CopyTo( aNewZone );
95
96     aFatherRegion->RemoveZone( theZone );
97
98     theZone->SetLabel( aNewZone->Label() );
99   }
100   else
101   {
102     AddReferenceObject( theZone, DataTag_Zone );
103   }
104
105   return true;
106 }
107
108 HYDROData_SequenceOfObjects HYDROData_Region::GetZones() const
109 {
110   return GetReferenceObjects( DataTag_Zone );
111 }
112
113 void HYDROData_Region::RemoveZone( const Handle(HYDROData_Zone)& theZone )
114 {
115   if ( theZone.IsNull() )
116     return;
117
118   RemoveReferenceObject( theZone->Label(), DataTag_Zone );
119
120   // Remove zone from data model
121   Handle(HYDROData_Region) aFatherRegion = 
122     Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
123   if ( !aFatherRegion.IsNull() && aFatherRegion->Label() == myLab )
124     theZone->Remove();
125
126   // If the last zone has been removed from region we remove this region
127   HYDROData_SequenceOfObjects aRefZones = GetZones();
128   if ( aRefZones.IsEmpty() )
129     Remove();
130 }
131
132 void HYDROData_Region::RemoveZones()
133 {
134   ClearReferenceObjects( DataTag_Zone );
135   myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
136 }
137
138 Handle(HYDROData_Zone) HYDROData_Region::addNewZone()
139 {
140   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
141
142   Handle(HYDROData_Zone) aNewZone =
143     Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
144   AddZone( aNewZone );
145
146   return aNewZone;
147 }
148
149 TopoDS_Shape HYDROData_Region::GetShape() const
150 {
151   TopoDS_Shape aResShape;
152
153   // Unite the region zones (each zone is a face) into one face (united face)
154   // If the zones can't be united into the single face - unite them into shell
155
156   // Collect the list of region faces
157   TopTools_ListOfShape aRegionFacesList;
158
159   HYDROData_SequenceOfObjects aZones = GetZones();
160   HYDROData_SequenceOfObjects::Iterator aZoneIter( aZones );
161   for ( ; aZoneIter.More(); aZoneIter.Next() ) {
162     Handle(HYDROData_Zone) aZone =
163       Handle(HYDROData_Zone)::DownCast( aZoneIter.Value() );
164   
165     if ( aZone.IsNull() ) {
166       continue;
167     }
168
169     TopoDS_Face aFace = TopoDS::Face( aZone->GetShape() );
170     if ( !aFace.IsNull() ) {
171       aRegionFacesList.Append( aFace );
172     }
173   } // zones iterator
174   
175   // Check number of faces
176   int aNbFaces = aRegionFacesList.Extent();
177   if ( aNbFaces > 0 ) {
178     // The unite region face
179     TopoDS_Face aRegionFace;
180   
181     if ( aNbFaces == 1 ) {
182       aRegionFace = TopoDS::Face( aRegionFacesList.First() );
183     } else {
184       // Fuse faces into one
185       TopoDS_Shape aFuseShape;
186       TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
187       for ( ; aFaceIter.More(); aFaceIter.Next() ) {
188         if ( aFuseShape.IsNull() ) {
189           aFuseShape = aFaceIter.Value();
190         } else {
191           BRepAlgoAPI_Fuse aFuse(aFuseShape, aFaceIter.Value());
192           if ( !aFuse.IsDone() ) {
193             aFuseShape.Nullify();
194             break;
195           }
196           aFuseShape = aFuse.Shape();
197         }
198       } // faces iterator
199
200       // Check the result of fuse operation
201       if ( !aFuseShape.IsNull() ) {
202         ShapeUpgrade_UnifySameDomain anUnifier( aFuseShape );
203         anUnifier.Build();
204         TopoDS_Shape anUnitedShape = anUnifier.Shape();
205
206         TopTools_IndexedMapOfShape aMapOfFaces;
207         TopExp::MapShapes( anUnitedShape, TopAbs_FACE, aMapOfFaces );
208         if ( aMapOfFaces.Extent() == 1 ) {
209           aRegionFace = TopoDS::Face( aMapOfFaces(1) );
210         }
211       }
212     }
213
214     if ( !aRegionFace.IsNull() ) { // result shape is a face
215       aResShape = aRegionFace;
216     } else { // result shape is a shell
217       TopoDS_Shell aShell;
218       BRep_Builder aBuilder;
219       aBuilder.MakeShell( aShell );
220
221       TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
222       for ( ; aFaceIter.More(); aFaceIter.Next() ) {
223         aBuilder.Add( aShell, aFaceIter.Value() );
224       }
225
226       aResShape = aShell;
227     }
228   }
229   
230   return aResShape;
231 }