Salome HOME
bb60ed1236e49e67d6cee3f57ea38c22d34608a6
[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
9 #include <TopoDS.hxx>
10 #include <TopoDS_Wire.hxx>
11 #include <BRepOffsetAPI_MakePipeShell.hxx>
12 #include <BRepOffsetAPI_MakePipe.hxx>
13 #include <BRepCheck_Analyzer.hxx>
14 #include <TopoDS_Vertex.hxx>
15 #include <TopExp.hxx>
16 //#define DEB_CHANNEL 1
17 #ifdef DEB_CHANNEL
18 #include <BRepTools.hxx>
19 #endif
20
21 #include <QColor>
22 #include <QStringList>
23
24 #define PYTHON_CHANNEL_ID "KIND_CHANNEL"
25
26 IMPLEMENT_STANDARD_HANDLE(HYDROData_Channel,HYDROData_ArtificialObject)
27 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Channel,HYDROData_ArtificialObject)
28
29
30 HYDROData_Channel::HYDROData_Channel()
31 : HYDROData_ArtificialObject()
32 {
33 }
34
35 HYDROData_Channel::~HYDROData_Channel()
36 {
37 }
38
39 QStringList HYDROData_Channel::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
40 {
41   QStringList aResList;
42
43   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
44   if ( aDocument.IsNull() )
45     return aResList;
46
47   QString aDocName = aDocument->GetDocPyName();
48   QString aChannelName = GetName();
49
50   aResList << QString( "%1 = %2.CreateObject( %3 );" )
51               .arg( aChannelName ).arg( aDocName ).arg( PYTHON_CHANNEL_ID );
52   aResList << QString( "%1.SetName( \"%2\" );" )
53               .arg( aChannelName ).arg( aChannelName );
54   aResList << QString( "" );
55
56   // TODO
57
58   return aResList;
59 }
60
61 HYDROData_SequenceOfObjects HYDROData_Channel::GetAllReferenceObjects() const
62 {
63   HYDROData_SequenceOfObjects aResSeq = HYDROData_ArtificialObject::GetAllReferenceObjects();
64
65   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
66   if ( !aGuideLine.IsNull() )
67     aResSeq.Append( aGuideLine );
68
69   Handle(HYDROData_Profile) aProfile = GetProfile();
70   if ( !aProfile.IsNull() )
71     aResSeq.Append( aProfile );
72
73   return aResSeq;
74 }
75
76 TopoDS_Shape HYDROData_Channel::GetTopShape() const
77 {
78   return getTopShape();
79 }
80
81 TopoDS_Shape HYDROData_Channel::GetShape3D() const
82 {
83   return getShape3D();
84 }
85
86 void HYDROData_Channel::Update()
87 {
88   HYDROData_ArtificialObject::Update();
89
90   Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
91   Handle(HYDROData_Profile) aProfile = GetProfile();
92   if ( aGuideLine.IsNull() || aProfile.IsNull() )
93     return;
94
95   // build 3d shape 
96   TopoDS_Wire aPathWire = TopoDS::Wire(aGuideLine->GetShape3D()); 
97   if(aPathWire.IsNull())
98     return;
99   TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetShape3D() ); 
100   if(aProfileWire.IsNull())
101         return;
102
103   BRepOffsetAPI_MakePipeShell aMkSweep(aPathWire);
104   aMkSweep.Add(aProfileWire,Standard_True, Standard_True);
105   aMkSweep.SetTransitionMode(BRepBuilderAPI_RightCorner);
106   aMkSweep.SetMode(Standard_True);
107   aMkSweep.Build();
108   if(aMkSweep.IsDone())  { 
109         const TopoDS_Shape& aChannel = aMkSweep.Shape();
110     BRepCheck_Analyzer aCheck(aChannel);
111     if(aCheck.IsValid()) 
112         {
113       //BRepTools::Write(aChannel, "ChanV.brep");  
114       SetShape3D( aMkSweep.Shape());
115         } else {
116 #ifdef DEB_CHANNEL
117           cout <<"NOT VALID" <<endl;
118           BRepTools::Write(aChannel, "ChanNV.brep");  
119 #endif
120         }
121   }
122
123   // build 2d shape -- temporary solution!!
124   //aMkSweep.Generated() - it seems doesn't work
125   //TopoDS_Vertex aV1,aV2;
126   //TopExp::Vertices (aProfileWire, aV1, aV2);
127   //const TopTools_ListOfShape& aList1 = aMkSweep.Generated(aV1);
128   //const TopTools_ListOfShape& aList2 = aMkSweep.Generated(aV2);
129   //cout <<"List1 = " << aList1.Extent() <<endl;
130   //cout <<"List2 = " << aList2.Extent() <<endl;
131   TopoDS_Wire aPathWire2d = TopoDS::Wire(aGuideLine->GetPolylineXY()->GetShape()); 
132   if(aPathWire2d.IsNull())
133     return;
134   TopoDS_Wire aProfileWire2d = TopoDS::Wire( aProfile->GetTopShape() ); 
135   if(aProfileWire2d.IsNull())
136         return;
137   //BRepTools::Write(aPathWire2d, "PathWire2.brep");  
138   //BRepTools::Write(aProfileWire2d, "ProfileWire2.brep");  
139   //BRepOffsetAPI_MakePipeShell aMkSweep2d(aPathWire2d);
140   //aMkSweep2d.Add(aProfileWire2d,Standard_True, Standard_True);
141   //aMkSweep2d.SetTransitionMode(BRepBuilderAPI_RightCorner);
142   //aMkSweep2d.SetMode(Standard_True);
143   //aMkSweep2d.Build();
144    BRepOffsetAPI_MakePipe aMkSweep2d(aPathWire2d, aProfileWire2d);
145    aMkSweep2d.Build();
146   if(aMkSweep2d.IsDone())        { 
147         const TopoDS_Shape& aChannel2d = aMkSweep2d.Shape();
148     BRepCheck_Analyzer aCheck(aChannel2d);
149     if(aCheck.IsValid()) 
150         {
151       //BRepTools::Write(aChannel2d, "Chan2dV.brep");  
152       SetTopShape( aChannel2d);
153         }
154 #ifdef DEB_CHANNEL      
155         else {
156           cout <<"NOT VALID" <<endl;
157           BRepTools::Write(aChannel2d, "Chan2dNV.brep");  
158         }
159 #endif
160   }
161 }
162
163 QColor HYDROData_Channel::DefaultFillingColor()
164 {
165   return QColor( Qt::blue );
166 }
167
168 QColor HYDROData_Channel::DefaultBorderColor()
169 {
170   return QColor( Qt::transparent );
171 }
172
173 QColor HYDROData_Channel::getDefaultFillingColor() const
174 {
175   return DefaultFillingColor();
176 }
177
178 QColor HYDROData_Channel::getDefaultBorderColor() const
179 {
180   return DefaultBorderColor();
181 }
182
183 bool HYDROData_Channel::SetGuideLine( const Handle(HYDROData_Polyline3D)& theGuideLine )
184 {
185   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
186
187   if ( theGuideLine.IsNull() )
188   {
189     RemoveGuideLine();
190     return !aPrevGuideLine.IsNull();
191   }
192
193   if ( IsEqual( aPrevGuideLine, theGuideLine ) )
194     return false;
195
196   TopoDS_Wire aHydraulicWire = TopoDS::Wire( theGuideLine->GetTopShape() );
197   if ( aHydraulicWire.IsNull() )
198     return false; // The polyline must be a single wire
199
200   SetReferenceObject( theGuideLine, DataTag_GuideLine );
201
202   // Indicate model of the need to update the chanel presentation
203   SetToUpdate( true );
204
205   return true;
206 }
207
208 Handle(HYDROData_Polyline3D) HYDROData_Channel::GetGuideLine() const
209 {
210   return Handle(HYDROData_Polyline3D)::DownCast( 
211            GetReferenceObject( DataTag_GuideLine ) );
212 }
213
214 void HYDROData_Channel::RemoveGuideLine()
215 {
216   Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
217   if ( aPrevGuideLine.IsNull() )
218     return;
219
220   ClearReferenceObjects( DataTag_GuideLine );
221
222   // Indicate model of the need to update the chanel presentation
223   SetToUpdate( true );
224 }
225
226 bool HYDROData_Channel::SetProfile( const Handle(HYDROData_Profile)& theProfile )
227 {
228   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
229
230   if ( theProfile.IsNull() )
231   {
232     RemoveProfile();
233     return !aPrevProfile.IsNull();
234   }
235
236   if ( IsEqual( aPrevProfile, theProfile ) )
237     return false;
238
239   SetReferenceObject( theProfile, DataTag_Profile );
240
241   // Indicate model of the need to update the chanel presentation
242   SetToUpdate( true );
243
244   return true;
245 }
246
247 Handle(HYDROData_Profile) HYDROData_Channel::GetProfile() const
248 {
249   return Handle(HYDROData_Profile)::DownCast( 
250            GetReferenceObject( DataTag_Profile ) );
251 }
252
253 void HYDROData_Channel::RemoveProfile()
254 {
255   Handle(HYDROData_Profile) aPrevProfile = GetProfile();
256   if ( aPrevProfile.IsNull() )
257     return;
258
259   ClearReferenceObjects( DataTag_Profile );
260
261   // Indicate model of the need to update the chanel presentation
262   SetToUpdate( true );
263 }