]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_ImmersibleZone.cxx
Salome HOME
Common part of dump to python is moved to HYDROData_Entity.
[modules/hydro.git] / src / HYDROData / HYDROData_ImmersibleZone.cxx
1
2 #include "HYDROData_ImmersibleZone.h"
3
4 #include "HYDROData_IAltitudeObject.h"
5 #include "HYDROData_Document.h"
6 #include "HYDROData_ShapesGroup.h"
7 #include "HYDROData_PolylineXY.h"
8 #include "HYDROData_ShapesTool.h"
9
10 #include <BRepBuilderAPI_MakeFace.hxx>
11
12 #include <TopoDS.hxx>
13 #include <TopoDS_Face.hxx>
14 #include <TopoDS_Wire.hxx>
15 #include <TopoDS_Compound.hxx>
16 #include <TopExp_Explorer.hxx>
17 #include <TopTools_ListIteratorOfListOfShape.hxx>
18
19 #include <BRep_Builder.hxx>
20 #include <BRepAlgo_FaceRestrictor.hxx>
21 #include <BRepCheck_Analyzer.hxx>
22
23 #include <ShapeAnalysis.hxx>
24
25 #include <QColor>
26 #include <QStringList>
27
28 //#define HYDRODATA_IMZONE_DEB 1
29
30 IMPLEMENT_STANDARD_HANDLE(HYDROData_ImmersibleZone,HYDROData_NaturalObject)
31 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_ImmersibleZone,HYDROData_NaturalObject)
32
33
34 HYDROData_ImmersibleZone::HYDROData_ImmersibleZone()
35 : HYDROData_NaturalObject()
36 {
37 }
38
39 HYDROData_ImmersibleZone::~HYDROData_ImmersibleZone()
40 {
41 }
42
43 QStringList HYDROData_ImmersibleZone::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
44 {
45   QStringList aResList = HYDROData_Entity::DumpToPython( theTreatedObjects );
46   QString aZoneName = GetName();
47
48   QColor aFillingColor = GetFillingColor();
49   aResList << QString( "filling_color = QColor( %1, %2, %3, %4 );" )
50               .arg( aFillingColor.red()  ).arg( aFillingColor.green() )
51               .arg( aFillingColor.blue() ).arg( aFillingColor.alpha() );
52   aResList << QString( "%1.SetFillingColor( filling_color );" ).arg( aZoneName );
53   aResList << QString( "" );
54
55   QColor aBorderColor = GetBorderColor();
56   aResList << QString( "border_color = QColor( %1, %2, %3, %4 );" )
57               .arg( aBorderColor.red()  ).arg( aBorderColor.green() )
58               .arg( aBorderColor.blue() ).arg( aBorderColor.alpha() );
59   aResList << QString( "%1.SetBorderColor( border_color );" ).arg( aZoneName );
60   aResList << QString( "" );
61
62   Handle(HYDROData_IAltitudeObject) aRefAltitude = GetAltitudeObject();
63   setPythonReferenceObject( theTreatedObjects, aResList, aRefAltitude, "SetAltitudeObject" );
64
65   Handle(HYDROData_PolylineXY) aRefPolyline = GetPolyline();
66   setPythonReferenceObject( theTreatedObjects, aResList, aRefPolyline, "SetPolyline" );
67
68   return aResList;
69 }
70
71 HYDROData_SequenceOfObjects HYDROData_ImmersibleZone::GetAllReferenceObjects() const
72 {
73   HYDROData_SequenceOfObjects aResSeq = HYDROData_NaturalObject::GetAllReferenceObjects();
74
75   Handle(HYDROData_PolylineXY) aRefPolyline = GetPolyline();
76   if ( !aRefPolyline.IsNull() )
77     aResSeq.Append( aRefPolyline );
78
79   return aResSeq;
80 }
81
82 TopoDS_Shape HYDROData_ImmersibleZone::GetTopShape() const
83 {
84   return getTopShape();
85 }
86
87 void HYDROData_ImmersibleZone::Update()
88 {
89   HYDROData_NaturalObject::Update();
90   
91   TopoDS_Shape aResShape = generateTopShape();
92   SetTopShape( aResShape );
93
94   createGroupObjects();
95 }
96
97 TopoDS_Shape HYDROData_ImmersibleZone::generateTopShape() const
98 {
99   return generateTopShape( GetPolyline() );
100 }
101
102 TopoDS_Shape HYDROData_ImmersibleZone::generateTopShape( const Handle(HYDROData_PolylineXY)& aPolyline )
103 {
104   TopoDS_Face aResultFace = TopoDS_Face();
105
106   if( !aPolyline.IsNull() )
107   {
108     TopoDS_Shape aPolylineShape = aPolyline->GetShape();
109     TopTools_ListOfShape aWiresList;
110
111     if ( !aPolylineShape.IsNull() && 
112          aPolylineShape.ShapeType() == TopAbs_WIRE ) {
113       const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolylineShape );
114       if ( !aPolylineWire.IsNull() ) {
115         BRepBuilderAPI_MakeFace aMakeFace( aPolylineWire, Standard_True );
116         aMakeFace.Build();
117         if( aMakeFace.IsDone() ) {
118           return aMakeFace.Face();
119         }
120       }
121     } else {
122       TopExp_Explorer anExp( aPolylineShape, TopAbs_WIRE );
123       for ( ; anExp.More(); anExp.Next() ) {
124         if(!anExp.Current().IsNull()) {
125           const TopoDS_Wire& aWire = TopoDS::Wire( anExp.Current() );
126           aWiresList.Append( aWire );
127         }
128       }
129       if(aWiresList.IsEmpty())
130          return aResultFace;
131
132       BRepAlgo_FaceRestrictor aFR;
133       TopoDS_Face aRefFace;      
134       TopoDS_Shape aS = aWiresList.First();
135       BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire(aWiresList.First()), Standard_True );
136       aMakeFace.Build();
137       if( aMakeFace.IsDone() ) {
138         aRefFace = aMakeFace.Face();
139       }
140       if(aRefFace.IsNull())
141         return aResultFace;
142
143       aFR.Init(aRefFace,Standard_False, Standard_True);
144       TopTools_ListIteratorOfListOfShape anIt( aWiresList );
145       for ( ; anIt.More(); anIt.Next() ) {
146         TopoDS_Wire& aWire = TopoDS::Wire( anIt.Value() );
147         if ( aWire.IsNull() ) 
148             continue;
149         aFR.Add(aWire);
150       }
151       aFR.Perform();
152       if (aFR.IsDone()) {
153         for (; aFR.More(); aFR.Next()) {
154           aResultFace = aFR.Current();
155           break;
156         }
157       }
158     }
159   }
160
161   if( aResultFace.IsNull() )
162     return aResultFace;
163
164   BRepCheck_Analyzer anAnalyzer( aResultFace );
165   if( anAnalyzer.IsValid() && aResultFace.ShapeType()==TopAbs_FACE )
166     return aResultFace;
167   else
168     return TopoDS_Face();
169 }
170
171 void HYDROData_ImmersibleZone::createGroupObjects()
172 {
173   TopoDS_Shape aZoneShape = GetTopShape();
174   
175   // Temporary solution while the restriction for polylines is not implemented
176   // and shape for zone can be compound and not face only
177   if ( !aZoneShape.IsNull() && aZoneShape.ShapeType() != TopAbs_FACE )
178   {
179     TopExp_Explorer aZoneFaceExp( aZoneShape, TopAbs_FACE );
180     if ( aZoneFaceExp.More() )
181       aZoneShape = aZoneFaceExp.Current(); // Take only first face into account
182   }
183
184   if ( aZoneShape.IsNull() || aZoneShape.ShapeType() != TopAbs_FACE )
185     return;
186
187   TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
188   
189   TopoDS_Wire aZoneOuterWire = ShapeAnalysis::OuterWire( aZoneFace );
190
191   // Create outer edges group
192   QString anOutWiresGroupName = GetName() + "_Outer";
193
194   Handle(HYDROData_ShapesGroup) anOutWiresGroup = createGroupObject();
195   anOutWiresGroup->SetName( anOutWiresGroupName );
196
197   TopTools_SequenceOfShape anOuterEdges;
198   HYDROData_ShapesTool::ExploreShapeToShapes( aZoneOuterWire, TopAbs_EDGE, anOuterEdges );
199   anOutWiresGroup->SetShapes( anOuterEdges );
200
201   int anInnerCounter = 1;
202   TopExp_Explorer aZoneFaceExp( aZoneFace, TopAbs_WIRE );
203   for ( ; aZoneFaceExp.More(); aZoneFaceExp.Next() )
204   {
205     TopoDS_Wire aZoneWire = TopoDS::Wire( aZoneFaceExp.Current() );
206     if ( aZoneWire.IsEqual( aZoneOuterWire ) )
207       continue; // Skip the outer wire
208
209     TopTools_SequenceOfShape anInnerEdges;
210     HYDROData_ShapesTool::ExploreShapeToShapes( aZoneWire, TopAbs_EDGE, anInnerEdges );
211     if ( anInnerEdges.IsEmpty() )
212       continue;
213
214     QString anInWiresGroupName = GetName() + "_Inner_" + QString::number( anInnerCounter++ );
215
216     Handle(HYDROData_ShapesGroup) anInWiresGroup = createGroupObject();
217     anInWiresGroup->SetName( anInWiresGroupName );
218
219     anInWiresGroup->SetShapes( anInnerEdges );
220   }  
221 }
222
223 TopoDS_Shape HYDROData_ImmersibleZone::GetShape3D() const
224 {
225   return getTopShape();
226 }
227
228 QColor HYDROData_ImmersibleZone::DefaultFillingColor()
229 {
230   return QColor( Qt::darkBlue );
231 }
232
233 QColor HYDROData_ImmersibleZone::DefaultBorderColor()
234 {
235   return QColor( Qt::transparent );
236 }
237
238 QColor HYDROData_ImmersibleZone::getDefaultFillingColor() const
239 {
240   return DefaultFillingColor();
241 }
242
243 QColor HYDROData_ImmersibleZone::getDefaultBorderColor() const
244 {
245   return DefaultBorderColor();
246 }
247
248 void HYDROData_ImmersibleZone::SetPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
249 {
250   SetReferenceObject( thePolyline, DataTag_Polyline );
251   SetToUpdate( true );
252 }
253
254 Handle(HYDROData_PolylineXY) HYDROData_ImmersibleZone::GetPolyline() const
255 {
256   return Handle(HYDROData_PolylineXY)::DownCast( 
257            GetReferenceObject( DataTag_Polyline ) );
258 }
259
260 void HYDROData_ImmersibleZone::RemovePolyline()
261 {
262   ClearReferenceObjects( DataTag_Polyline );
263   SetToUpdate( true );
264 }
265
266