Salome HOME
#refs 522 (very draft) //import only
[modules/hydro.git] / src / HYDROData / HYDROData_ImmersibleZone.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROData_ImmersibleZone.h"
24
25 #include "HYDROData_IAltitudeObject.h"
26 #include "HYDROData_Document.h"
27 #include "HYDROData_ShapesGroup.h"
28 #include "HYDROData_PolylineXY.h"
29 #include "HYDROData_ShapesTool.h"
30
31 #include <BRepBuilderAPI_MakeFace.hxx>
32
33 #include <TopoDS.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <TopoDS_Wire.hxx>
36 #include <TopoDS_Compound.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39
40 #include <BRep_Builder.hxx>
41 #include <BRepAlgo_FaceRestrictor.hxx>
42 #include <BRepCheck_Analyzer.hxx>
43
44 #include <ShapeAnalysis.hxx>
45
46 #include <QColor>
47 #include <QStringList>
48
49 //#define HYDRODATA_IMZONE_DEB 1
50
51 IMPLEMENT_STANDARD_HANDLE(HYDROData_ImmersibleZone,HYDROData_NaturalObject)
52 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_ImmersibleZone,HYDROData_NaturalObject)
53
54
55 HYDROData_ImmersibleZone::HYDROData_ImmersibleZone()
56 : HYDROData_NaturalObject()
57 {
58 }
59
60 HYDROData_ImmersibleZone::~HYDROData_ImmersibleZone()
61 {
62 }
63
64 QStringList HYDROData_ImmersibleZone::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
65 {
66   QStringList aResList = dumpObjectCreation( theTreatedObjects );
67   
68   QString aZoneName = GetObjPyName();
69
70   Handle(HYDROData_IAltitudeObject) aRefAltitude = GetAltitudeObject();
71   setPythonReferenceObject( theTreatedObjects, aResList, aRefAltitude, "SetAltitudeObject" );
72
73   Handle(HYDROData_PolylineXY) aRefPolyline = GetPolyline();
74   setPythonReferenceObject( theTreatedObjects, aResList, aRefPolyline, "SetPolyline" );
75
76   aResList << QString( "" );
77
78   aResList << QString( "%1.Update();" ).arg( aZoneName );
79   aResList << QString( "" );
80
81   return aResList;
82 }
83
84 HYDROData_SequenceOfObjects HYDROData_ImmersibleZone::GetAllReferenceObjects() const
85 {
86   HYDROData_SequenceOfObjects aResSeq = HYDROData_NaturalObject::GetAllReferenceObjects();
87
88   Handle(HYDROData_PolylineXY) aRefPolyline = GetPolyline();
89   if ( !aRefPolyline.IsNull() )
90     aResSeq.Append( aRefPolyline );
91
92   return aResSeq;
93 }
94
95 TopoDS_Shape HYDROData_ImmersibleZone::GetTopShape() const
96 {
97   return getTopShape();
98 }
99
100 void HYDROData_ImmersibleZone::Update()
101 {
102   HYDROData_NaturalObject::Update();
103   
104   TopoDS_Shape aResShape = generateTopShape();
105   SetTopShape( aResShape );
106
107   createGroupObjects();
108 }
109
110 bool HYDROData_ImmersibleZone::IsHas2dPrs() const
111 {
112   return true;
113 }
114
115 TopoDS_Shape HYDROData_ImmersibleZone::generateTopShape() const
116 {
117   return generateTopShape( GetPolyline() );
118 }
119
120 TopoDS_Shape HYDROData_ImmersibleZone::generateTopShape( const Handle(HYDROData_PolylineXY)& aPolyline )
121 {
122   TopoDS_Face aResultFace = TopoDS_Face();
123
124   if( !aPolyline.IsNull() )
125   {
126     TopoDS_Shape aPolylineShape = aPolyline->GetShape();
127     TopTools_ListOfShape aWiresList;
128
129     if ( !aPolylineShape.IsNull() && 
130          aPolylineShape.ShapeType() == TopAbs_WIRE ) {
131       const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolylineShape );
132       if ( !aPolylineWire.IsNull() ) {
133         BRepBuilderAPI_MakeFace aMakeFace( aPolylineWire, Standard_True );
134         aMakeFace.Build();
135         if( aMakeFace.IsDone() ) {
136           aResultFace = aMakeFace.Face();
137         }
138       }
139     } else {
140       TopExp_Explorer anExp( aPolylineShape, TopAbs_WIRE );
141       for ( ; anExp.More(); anExp.Next() ) {
142         if(!anExp.Current().IsNull()) {
143           const TopoDS_Wire& aWire = TopoDS::Wire( anExp.Current() );
144           aWiresList.Append( aWire );
145         }
146       }
147       if(aWiresList.IsEmpty())
148          return aResultFace;
149
150       BRepAlgo_FaceRestrictor aFR;
151       TopoDS_Face aRefFace;      
152       TopoDS_Shape aS = aWiresList.First();
153       BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire(aWiresList.First()), Standard_True );
154       aMakeFace.Build();
155       if( aMakeFace.IsDone() ) {
156         aRefFace = aMakeFace.Face();
157       }
158       if(aRefFace.IsNull())
159         return aResultFace;
160
161       aFR.Init(aRefFace,Standard_False, Standard_True);
162       TopTools_ListIteratorOfListOfShape anIt( aWiresList );
163       for ( ; anIt.More(); anIt.Next() ) {
164         TopoDS_Wire& aWire = TopoDS::Wire( anIt.Value() );
165         if ( aWire.IsNull() ) 
166             continue;
167         aFR.Add(aWire);
168       }
169       aFR.Perform();
170       if (aFR.IsDone()) {
171         for (; aFR.More(); aFR.Next()) {
172           aResultFace = aFR.Current();
173           break;
174         }
175       }
176     }
177   }
178
179   if( aResultFace.IsNull() )
180     return aResultFace;
181
182   BRepCheck_Analyzer anAnalyzer( aResultFace );
183   if( anAnalyzer.IsValid() && aResultFace.ShapeType()==TopAbs_FACE )
184     return aResultFace;
185   else
186     return TopoDS_Face();
187 }
188
189 void HYDROData_ImmersibleZone::createGroupObjects()
190 {
191   TopoDS_Shape aZoneShape = GetTopShape();
192   
193   // Temporary solution while the restriction for polylines is not implemented
194   // and shape for zone can be compound and not face only
195   if ( !aZoneShape.IsNull() && aZoneShape.ShapeType() != TopAbs_FACE )
196   {
197     TopExp_Explorer aZoneFaceExp( aZoneShape, TopAbs_FACE );
198     if ( aZoneFaceExp.More() )
199       aZoneShape = aZoneFaceExp.Current(); // Take only first face into account
200   }
201
202   if ( aZoneShape.IsNull() || aZoneShape.ShapeType() != TopAbs_FACE )
203     return;
204
205   TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
206   
207   TopoDS_Wire aZoneOuterWire = ShapeAnalysis::OuterWire( aZoneFace );
208
209   // Create outer edges group
210   QString anOutWiresGroupName = GetName() + "_Outer";
211
212   Handle(HYDROData_ShapesGroup) anOutWiresGroup = createGroupObject();
213   anOutWiresGroup->SetName( anOutWiresGroupName );
214
215   TopTools_SequenceOfShape anOuterEdges;
216   HYDROData_ShapesTool::ExploreShapeToShapes( aZoneOuterWire, TopAbs_EDGE, anOuterEdges );
217   anOutWiresGroup->SetShapes( anOuterEdges );
218
219   int anInnerCounter = 1;
220   TopExp_Explorer aZoneFaceExp( aZoneFace, TopAbs_WIRE );
221   for ( ; aZoneFaceExp.More(); aZoneFaceExp.Next() )
222   {
223     TopoDS_Wire aZoneWire = TopoDS::Wire( aZoneFaceExp.Current() );
224     if ( aZoneWire.IsEqual( aZoneOuterWire ) )
225       continue; // Skip the outer wire
226
227     TopTools_SequenceOfShape anInnerEdges;
228     HYDROData_ShapesTool::ExploreShapeToShapes( aZoneWire, TopAbs_EDGE, anInnerEdges );
229     if ( anInnerEdges.IsEmpty() )
230       continue;
231
232     QString anInWiresGroupName = GetName() + "_Inner_" + QString::number( anInnerCounter++ );
233
234     Handle(HYDROData_ShapesGroup) anInWiresGroup = createGroupObject();
235     anInWiresGroup->SetName( anInWiresGroupName );
236
237     anInWiresGroup->SetShapes( anInnerEdges );
238   }  
239 }
240
241 TopoDS_Shape HYDROData_ImmersibleZone::GetShape3D() const
242 {
243   return getTopShape();
244 }
245
246 QColor HYDROData_ImmersibleZone::DefaultFillingColor()
247 {
248   return QColor( Qt::darkBlue );
249 }
250
251 QColor HYDROData_ImmersibleZone::DefaultBorderColor()
252 {
253   return QColor( Qt::transparent );
254 }
255
256 QColor HYDROData_ImmersibleZone::getDefaultFillingColor() const
257 {
258   return DefaultFillingColor();
259 }
260
261 QColor HYDROData_ImmersibleZone::getDefaultBorderColor() const
262 {
263   return DefaultBorderColor();
264 }
265
266 void HYDROData_ImmersibleZone::SetPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
267 {
268   SetReferenceObject( thePolyline, DataTag_Polyline );
269   SetToUpdate( true );
270 }
271
272 Handle(HYDROData_PolylineXY) HYDROData_ImmersibleZone::GetPolyline() const
273 {
274   return Handle(HYDROData_PolylineXY)::DownCast( 
275            GetReferenceObject( DataTag_Polyline ) );
276 }
277
278 void HYDROData_ImmersibleZone::RemovePolyline()
279 {
280   ClearReferenceObjects( DataTag_Polyline );
281   SetToUpdate( true );
282 }
283
284