Salome HOME
new implementation for channels creation
[modules/hydro.git] / src / HYDROData / HYDROData_Channel.cxx
1
2 #include "HYDROData_Channel.h"
3
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Polyline3D.h"
6 #include "HYDROData_Profile.h"
7 #include "HYDROData_PolylineXY.h"
8 #include "HYDROData_Projection.h"
9 #include "HYDROData_ShapesGroup.h"
10 #include "HYDROData_ShapesTool.h"
11 #include "HYDROData_Pipes.h"
12
13 #include <BRepOffsetAPI_MakePipeShell.hxx>
14 #include <BRepOffsetAPI_MakePipe.hxx>
15 #include <BRepCheck_Analyzer.hxx>
16 #include <BRepTools.hxx>
17
18 #include <TopExp.hxx>
19
20 #include <TopoDS.hxx>
21 #include <TopoDS_Wire.hxx>
22 #include <TopoDS_Vertex.hxx>
23 #include <TopoDS_Face.hxx>
24
25 //#define DEB_CHANNEL 1
26 #ifdef DEB_CHANNEL
27 #include <BRepTools.hxx>
28 #endif
29
30 #include <QColor>
31 #include <QStringList>
32
33 #define PYTHON_CHANNEL_ID "KIND_CHANNEL"
34
35 IMPLEMENT_STANDARD_HANDLE(HYDROData_Channel,HYDROData_ArtificialObject)
36 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Channel,HYDROData_ArtificialObject)
37
38
39 HYDROData_Channel::HYDROData_Channel()
40 : HYDROData_ArtificialObject()
41 {
42 }
43
44 HYDROData_Channel::~HYDROData_Channel()
45 {
46 }
47
48 QStringList HYDROData_Channel::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
49 {
50   QStringList aResList;
51
52   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
53   if ( aDocument.IsNull() )
54     return aResList;
55
56   QString aDocName = aDocument->GetDocPyName();
57   QString aChannelName = GetName();
58
59   aResList << QString( "%1 = %2.CreateObject( %3 );" )
60               .arg( aChannelName ).arg( aDocName ).arg( PYTHON_CHANNEL_ID );
61   aResList << QString( "%1.SetName( \"%2\" );" )
62               .arg( aChannelName ).arg( aChannelName );
63   aResList << QString( "" );
64
65   // TODO
66
67   return aResList;
68 }
69
70 HYDROData_SequenceOfObjects HYDROData_Channel::GetAllReferenceObjects() const
71 {
72   HYDROData_SequenceOfObjects aResSeq = HYDROData_ArtificialObject::GetAllReferenceObjects();
73
74   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
75   if ( !aGuideLine.IsNull() )
76     aResSeq.Append( aGuideLine );
77
78   Handle(HYDROData_Profile) aProfile = GetProfile();
79   if ( !aProfile.IsNull() )
80     aResSeq.Append( aProfile );
81
82   return aResSeq;
83 }
84
85 TopoDS_Shape HYDROData_Channel::GetTopShape() const
86 {
87   return getTopShape();
88 }
89
90 TopoDS_Shape HYDROData_Channel::GetShape3D() const
91 {
92   return getShape3D();
93 }
94
95 void HYDROData_Channel::Update()
96 {
97   HYDROData_ArtificialObject::Update();
98
99   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
100   Handle(HYDROData_Profile) aProfile = GetProfile();
101   if ( aGuideLine.IsNull() || aProfile.IsNull() )
102     return;
103
104   // build 3d shape 
105   TopoDS_Wire aPathWire = TopoDS::Wire(aGuideLine->GetShape3D());
106   if(aPathWire.IsNull())
107     return;
108   TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetShape3D() );
109   if(aProfileWire.IsNull())
110     return;
111
112   //BRepTools::Write( aPathWire, "guideline.brep" );
113   //BRepTools::Write( aProfileWire, "profile.brep" );
114
115   HYDROData_Canal3dAnd2d aChannelConstructor( aProfileWire, aPathWire );
116   aChannelConstructor.Create3dPresentation();
117   aChannelConstructor.Create2dPresentation();
118   SetShape3D( aChannelConstructor.Get3dPresentation() );
119   SetTopShape( aChannelConstructor.Get2dPresentation() );
120
121
122   /*TODO: groups via new API
123   QString aLeftGroupName = GetName() + "_Left_Bank";
124
125   Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
126   aLeftGroup->SetName( aLeftGroupName );
127   aLeftGroup->SetShapes( aProjLeftEdges );
128
129   QString aRightGroupName = GetName() + "_Right_Bank";
130
131   Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
132   aRightGroup->SetName( aRightGroupName );
133   aRightGroup->SetShapes( aProjRightEdges );
134
135   QString anInGroupName = GetName() + "_Inlet";
136
137   Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
138   anInGroup->SetName( anInGroupName );
139   anInGroup->SetShapes( aProjInletEdges );
140
141   QString anOutGroupName = GetName() + "_Outlet";
142
143   Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
144   anOutGroup->SetName( anOutGroupName );
145   anOutGroup->SetShapes( aProjOutletEdges );
146   */
147 }
148
149 QColor HYDROData_Channel::DefaultFillingColor()
150 {
151   return QColor( Qt::blue );
152 }
153
154 QColor HYDROData_Channel::DefaultBorderColor()
155 {
156   return QColor( Qt::transparent );
157 }
158
159 QColor HYDROData_Channel::getDefaultFillingColor() const
160 {
161   return DefaultFillingColor();
162 }
163
164 QColor HYDROData_Channel::getDefaultBorderColor() const
165 {
166   return DefaultBorderColor();
167 }
168
169 bool HYDROData_Channel::SetGuideLine( const Handle(HYDROData_Polyline3D)& theGuideLine )
170 {
171   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
172
173   if ( theGuideLine.IsNull() )
174   {
175     RemoveGuideLine();
176     return !aPrevGuideLine.IsNull();
177   }
178
179   if ( IsEqual( aPrevGuideLine, theGuideLine ) )
180     return false;
181
182   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theGuideLine->GetTopShape() );
183   if ( aHydraulicWire.IsNull() )
184     return false; // The polyline must be a single wire
185
186   SetReferenceObject( theGuideLine, DataTag_GuideLine );
187
188   // Indicate model of the need to update the chanel presentation
189   SetToUpdate( true );
190
191   return true;
192 }
193
194 Handle(HYDROData_Polyline3D) HYDROData_Channel::GetGuideLine() const
195 {
196   return Handle(HYDROData_Polyline3D)::DownCast( 
197            GetReferenceObject( DataTag_GuideLine ) );
198 }
199
200 void HYDROData_Channel::RemoveGuideLine()
201 {
202   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
203   if ( aPrevGuideLine.IsNull() )
204     return;
205
206   ClearReferenceObjects( DataTag_GuideLine );
207
208   // Indicate model of the need to update the chanel presentation
209   SetToUpdate( true );
210 }
211
212 bool HYDROData_Channel::SetProfile( const Handle(HYDROData_Profile)& theProfile )
213 {
214   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
215
216   if ( theProfile.IsNull() )
217   {
218     RemoveProfile();
219     return !aPrevProfile.IsNull();
220   }
221
222   if ( IsEqual( aPrevProfile, theProfile ) )
223     return false;
224
225   SetReferenceObject( theProfile, DataTag_Profile );
226
227   // Indicate model of the need to update the chanel presentation
228   SetToUpdate( true );
229
230   return true;
231 }
232
233 Handle(HYDROData_Profile) HYDROData_Channel::GetProfile() const
234 {
235   return Handle(HYDROData_Profile)::DownCast( 
236            GetReferenceObject( DataTag_Profile ) );
237 }
238
239 void HYDROData_Channel::RemoveProfile()
240 {
241   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
242   if ( aPrevProfile.IsNull() )
243     return;
244
245   ClearReferenceObjects( DataTag_Profile );
246
247   // Indicate model of the need to update the chanel presentation
248   SetToUpdate( true );
249 }
250
251 ObjectKind HYDROData_Channel::getAltitudeObjectType() const
252 {
253   return KIND_OBSTACLE_ALTITUDE;
254 }
255
256 void HYDROData_Channel::findEdges( const TopTools_SequenceOfShape& theInShapes,
257                                    const TopTools_ListOfShape&     theEdges3D,
258                                    TopTools_SequenceOfShape&       theOutShapes ) const
259 {
260   theOutShapes.Clear();
261   if ( theEdges3D.IsEmpty() || theInShapes.IsEmpty() )
262     return;
263
264   const TopoDS_Shape& aFirstShape3D = theEdges3D.First();
265   const TopoDS_Shape& aLastShape3D = theEdges3D.Last();
266
267   TopoDS_Vertex aFirstVert, aLastVert, aDummyVert;
268   if ( !HYDROData_ShapesTool::Vertices( aFirstShape3D, aFirstVert, aDummyVert ) ||
269        !HYDROData_ShapesTool::Vertices( aLastShape3D, aDummyVert, aLastVert ) )
270     return;
271
272   bool isStarted = false;
273   for ( int i = 1, n = theInShapes.Length(); i <= n; ++i )
274   {
275     const TopoDS_Shape& anInShape = theInShapes.Value( i );
276
277     TopoDS_Vertex aShapeFirstVert, aShapeLastVert;
278     if ( !HYDROData_ShapesTool::Vertices( anInShape, aShapeFirstVert, aShapeLastVert ) )
279       continue;
280
281     if ( !isStarted )
282     {
283       isStarted = HYDROData_ShapesTool::IsVerticesEquals( aFirstVert, aShapeFirstVert, true );
284     }
285
286     if ( isStarted )
287     {
288       theOutShapes.Append( anInShape );
289
290       if ( HYDROData_ShapesTool::IsVerticesEquals( aLastVert, aShapeLastVert, true ) )
291         break;
292     }
293   }
294 }
295
296 void HYDROData_Channel::findEdges( const TopTools_SequenceOfShape& theProjShapes,
297                                    const TopoDS_Shape&             theEdge3D,
298                                    TopTools_SequenceOfShape&       theOutShapes ) const
299 {
300   TopTools_ListOfShape aTmpList;
301   aTmpList.Append( theEdge3D );
302   findEdges( theProjShapes, aTmpList, theOutShapes );
303 }
304