Salome HOME
patch for correct compilation on Linux
[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::CanRemove()
81 {
82   return false;
83 }
84
85 HYDROData_SequenceOfObjects HYDROData_Region::GetAllReferenceObjects() const
86 {
87   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
88
89   HYDROData_SequenceOfObjects aSeqOfZones = GetZones();
90   aResSeq.Append( aSeqOfZones );
91
92   return aResSeq;
93 }
94
95 bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
96 {
97   if ( theZone.IsNull() )
98     return false;
99   
100   if ( HasReference( theZone, DataTag_Zone ) )
101     return false; // Object is already in reference list
102
103   // Move the zone from other region
104   Handle(HYDROData_Region) aFatherRegion = 
105     Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
106   if ( !aFatherRegion.IsNull() && aFatherRegion->Label() != myLab )
107   {
108     Handle(HYDROData_Zone) aNewZone = addNewZone();
109     theZone->CopyTo( aNewZone );
110
111     aFatherRegion->RemoveZone( theZone );
112
113     theZone->SetLabel( aNewZone->Label() );
114   }
115   else
116   {
117     AddReferenceObject( theZone, DataTag_Zone );
118   }
119
120   return true;
121 }
122
123 HYDROData_SequenceOfObjects HYDROData_Region::GetZones() const
124 {
125   return GetReferenceObjects( DataTag_Zone );
126 }
127
128 void HYDROData_Region::RemoveZone( const Handle(HYDROData_Zone)& theZone )
129 {
130   if ( theZone.IsNull() )
131     return;
132
133   RemoveReferenceObject( theZone->Label(), DataTag_Zone );
134
135   // Remove zone from data model
136   Handle(HYDROData_Region) aFatherRegion = 
137     Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
138   if ( !aFatherRegion.IsNull() && aFatherRegion->Label() == myLab )
139     theZone->Remove();
140
141   // If the last zone has been removed from region we remove this region
142   HYDROData_SequenceOfObjects aRefZones = GetZones();
143   if ( aRefZones.IsEmpty() )
144     Remove();
145 }
146
147 void HYDROData_Region::RemoveZones()
148 {
149   ClearReferenceObjects( DataTag_Zone );
150   myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
151 }
152
153 Handle(HYDROData_Zone) HYDROData_Region::addNewZone()
154 {
155   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
156
157   Handle(HYDROData_Zone) aNewZone =
158     Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
159   AddZone( aNewZone );
160
161   return aNewZone;
162 }
163
164 TopoDS_Shape HYDROData_Region::GetShape() const
165 {
166   TopoDS_Shape aResShape;
167
168   // Unite the region zones (each zone is a face) into one face (united face)
169   // If the zones can't be united into the single face - unite them into shell
170
171   // Collect the list of region faces
172   TopTools_ListOfShape aRegionFacesList;
173
174   HYDROData_SequenceOfObjects aZones = GetZones();
175   HYDROData_SequenceOfObjects::Iterator aZoneIter( aZones );
176   for ( ; aZoneIter.More(); aZoneIter.Next() ) {
177     Handle(HYDROData_Zone) aZone =
178       Handle(HYDROData_Zone)::DownCast( aZoneIter.Value() );
179   
180     if ( aZone.IsNull() ) {
181       continue;
182     }
183
184     TopoDS_Face aFace = TopoDS::Face( aZone->GetShape() );
185     if ( !aFace.IsNull() ) {
186       aRegionFacesList.Append( aFace );
187     }
188   } // zones iterator
189   
190   // Check number of faces
191   int aNbFaces = aRegionFacesList.Extent();
192   if ( aNbFaces > 0 ) {
193     // The unite region face
194     TopoDS_Face aRegionFace;
195   
196     if ( aNbFaces == 1 ) {
197       aRegionFace = TopoDS::Face( aRegionFacesList.First() );
198     } else {
199       // Fuse faces into one
200       TopoDS_Shape aFuseShape;
201       TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
202       for ( ; aFaceIter.More(); aFaceIter.Next() ) {
203         if ( aFuseShape.IsNull() ) {
204           aFuseShape = aFaceIter.Value();
205         } else {
206           BRepAlgoAPI_Fuse aFuse(aFuseShape, aFaceIter.Value());
207           if ( !aFuse.IsDone() ) {
208             aFuseShape.Nullify();
209             break;
210           }
211           aFuseShape = aFuse.Shape();
212         }
213       } // faces iterator
214
215       // Check the result of fuse operation
216       if ( !aFuseShape.IsNull() ) {
217         ShapeUpgrade_UnifySameDomain anUnifier( aFuseShape );
218         anUnifier.Build();
219         TopoDS_Shape anUnitedShape = anUnifier.Shape();
220
221         TopTools_IndexedMapOfShape aMapOfFaces;
222         TopExp::MapShapes( anUnitedShape, TopAbs_FACE, aMapOfFaces );
223         if ( aMapOfFaces.Extent() == 1 ) {
224           aRegionFace = TopoDS::Face( aMapOfFaces(1) );
225         }
226       }
227     }
228
229     if ( !aRegionFace.IsNull() ) { // result shape is a face
230       aResShape = aRegionFace;
231     } else { // result shape is a shell
232       TopoDS_Shell aShell;
233       BRep_Builder aBuilder;
234       aBuilder.MakeShell( aShell );
235
236       TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
237       for ( ; aFaceIter.More(); aFaceIter.Next() ) {
238         aBuilder.Add( aShell, aFaceIter.Value() );
239       }
240
241       aResShape = aShell;
242     }
243   }
244   
245   return aResShape;
246 }