]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_Polyline3D.cxx
Salome HOME
The method GetObjPyName is introduced in the Entity.
[modules/hydro.git] / src / HYDROData / HYDROData_Polyline3D.cxx
1
2 #include "HYDROData_Polyline3D.h"
3
4 #include "HYDROData_IAltitudeObject.h"
5 #include "HYDROData_Document.h"
6 #include "HYDROData_PolylineXY.h"
7 #include "HYDROData_Profile.h"
8 #include "HYDROData_ProfileUZ.h"
9 #include "HYDROData_Tool.h"
10
11 #include <gp_Pnt2d.hxx>
12 #include <gp_XY.hxx>
13 #include <gp_XYZ.hxx>
14
15 #include <TopoDS.hxx>
16 #include <TopoDS_Wire.hxx>
17
18 #include <QColor>
19 #include <QStringList>
20
21 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline3D,HYDROData_Object)
22 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
23
24
25 HYDROData_Polyline3D::HYDROData_Polyline3D()
26 : HYDROData_Object()
27 {
28 }
29
30 HYDROData_Polyline3D::~HYDROData_Polyline3D()
31 {
32 }
33
34 QStringList HYDROData_Polyline3D::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
35 {
36   QStringList aResList = dumpObjectCreation( theTreatedObjects );
37   QString aName = GetObjPyName();
38
39   Handle(HYDROData_PolylineXY) aRefPolyline = GetPolylineXY();
40   setPythonReferenceObject( theTreatedObjects, aResList, aRefPolyline, "SetPolylineXY" );
41
42   Handle(HYDROData_ProfileUZ) aRefProfile = GetProfileUZ();
43   setPythonReferenceObject( theTreatedObjects, aResList, aRefProfile, "SetProfileUZ" );
44
45   Handle(HYDROData_IAltitudeObject) aRefBathymetry = GetAltitudeObject();
46   setPythonReferenceObject( theTreatedObjects, aResList, aRefBathymetry, "SetAltitudeObject" );
47
48   aResList << QString( "" );
49   aResList << QString( "%1.Update();" ).arg( aName );
50   aResList << QString( "" );
51
52   return aResList;
53 }
54
55 HYDROData_SequenceOfObjects HYDROData_Polyline3D::GetAllReferenceObjects() const
56 {
57   HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
58
59   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
60   if ( !aPolylineXY.IsNull() )
61     aResSeq.Append( aPolylineXY );
62
63   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
64   if ( !aProfileUZ.IsNull() )
65     aResSeq.Append( aProfileUZ );
66
67   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
68   if ( !aChildProfileUZ.IsNull() )
69     aResSeq.Append( aChildProfileUZ );
70
71   return aResSeq;
72 }
73
74 TopoDS_Shape HYDROData_Polyline3D::GetTopShape() const
75 {
76   return getTopShape();
77 }
78
79 TopoDS_Shape HYDROData_Polyline3D::GetShape3D() const
80 {
81   return getShape3D();
82 }
83
84 void HYDROData_Polyline3D::Update()
85 {
86   HYDROData_Object::Update();
87
88   Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
89   if ( aPolylineXY.IsNull() )
90     return;
91
92   bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
93   HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
94   HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
95   if ( aPolylinePoints.IsEmpty() )
96     return;
97
98   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
99
100   Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
101   if ( !anAltitude.IsNull() )
102     aProfileUZ = GetChildProfileUZ();
103
104   if ( aProfileUZ.IsNull() )
105     return;
106
107   HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
108   if ( aProfilePoints.IsEmpty() )
109     return;
110
111   const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
112   const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
113
114   const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
115   const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
116
117   double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
118   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
119
120   NCollection_Sequence<Polyline3DPoint> aResPoints;
121   
122   // Add first point as is
123   aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
124
125   for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
126   {
127     const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
128
129     double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
130
131     double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
132     double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
133
134     Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
135     aResPoints.Append( aCompPoint );
136   }
137
138   // Add last point as is
139   aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
140
141   TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
142   SetTopShape( aResWire );
143   SetShape3D( aResWire );
144
145
146 QColor HYDROData_Polyline3D::DefaultFillingColor()
147 {
148   return QColor( Qt::transparent );
149 }
150
151 QColor HYDROData_Polyline3D::DefaultBorderColor()
152 {
153   return QColor( Qt::red );
154 }
155
156 QColor HYDROData_Polyline3D::getDefaultFillingColor() const
157 {
158   return DefaultFillingColor();
159 }
160
161 QColor HYDROData_Polyline3D::getDefaultBorderColor() const
162 {
163   return DefaultBorderColor();
164 }
165
166 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
167                                           const bool                          theIsUpdateProfile )
168 {
169   if ( thePolyline.IsNull() )
170     return false;
171   
172   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
173   if ( IsEqual( aPrevPolyline, thePolyline ) )
174     return true;
175
176   SetReferenceObject( thePolyline, DataTag_PolylineXY );
177
178   // Update the child profile object 
179   if ( theIsUpdateProfile )
180     updateChildProfilePoints();
181
182   // Indicate model of the need to update the polyline presentation
183   SetToUpdate( true );
184
185   return true;
186 }
187
188 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
189 {
190   return Handle(HYDROData_PolylineXY)::DownCast( 
191            GetReferenceObject( DataTag_PolylineXY ) );
192 }
193
194 void HYDROData_Polyline3D::RemovePolylineXY()
195 {
196   Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
197   if ( aPrevPolyline.IsNull() )
198     return;
199
200   ClearReferenceObjects( DataTag_PolylineXY );
201
202   // Indicate model of the need to update the polyline presentation
203   SetToUpdate( true );
204 }
205
206 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
207 {
208   if ( theProfile.IsNull() )
209     return false;
210   
211   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
212   if ( IsEqual( aPrevProfile, theProfile ) )
213     return true;
214
215   SetReferenceObject( theProfile, DataTag_ProfileUZ );
216
217   // Remove the bathymetry, because one altitude object can be presented at time
218   RemoveAltitudeObject();
219
220   // Indicate model of the need to update the polyline presentation
221   SetToUpdate( true );
222
223   return true;
224 }
225
226 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
227 {
228   return Handle(HYDROData_ProfileUZ)::DownCast( 
229            GetReferenceObject( DataTag_ProfileUZ ) );
230 }
231
232 void HYDROData_Polyline3D::RemoveProfileUZ()
233 {
234   Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
235   if ( aPrevProfile.IsNull() )
236     return;
237
238   ClearReferenceObjects( DataTag_ProfileUZ );
239
240   // Indicate model of the need to update the polyline presentation
241   SetToUpdate( true );
242 }
243
244 bool HYDROData_Polyline3D::SetAltitudeObject( 
245   const Handle(HYDROData_IAltitudeObject)& theAltitude )
246 {
247   Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
248
249   if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
250     return false;
251
252   if ( IsEqual( aPrevAltitude, theAltitude ) )
253     return true;
254
255   // Remove the u,z profile, because one altitude object can be presented at time
256   RemoveProfileUZ();
257
258   // Create the child profile object 
259   updateChildProfilePoints();
260
261   return true;
262 }
263
264
265 void HYDROData_Polyline3D::RemoveAltitudeObject()
266 {
267   HYDROData_Object::RemoveAltitudeObject();
268
269   // Remove the child profile object 
270   removeChildProfileUZ();
271 }
272
273 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
274 {
275   Handle(HYDROData_ProfileUZ) aProfileUZ =
276     Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
277   if ( !theIsCreate || !aProfileUZ.IsNull() )
278     return aProfileUZ;
279
280   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
281   if ( aDocument.IsNull() )
282     return aProfileUZ;
283
284   Handle(HYDROData_Profile) aProfile =
285     Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
286   
287   QString aProfilePref = GetName() + "_Profile";
288   QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
289
290   aProfile->SetName( aProfileName );
291
292   aProfileUZ = aProfile->GetProfileUZ();
293
294   Handle(HYDROData_Polyline3D) me = this;
295   me->SetReferenceObject( aProfileUZ, DataTag_ChildProfileUZ );
296
297   return aProfileUZ;
298 }
299
300 HYDROData_IPolyline::PointsList generateProfileUZPoints(
301   const Handle(HYDROData_PolylineXY)&      thePolyline,
302   const Handle(HYDROData_IAltitudeObject)& theAltitude )
303 {
304   HYDROData_IPolyline::PointsList aPointsList;
305   if ( thePolyline.IsNull() || theAltitude.IsNull() )
306     return aPointsList;
307
308   bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
309   HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
310   HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
311   if ( aPolylinePoints.IsEmpty() )
312     return aPointsList;
313
314   for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
315   {
316     const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
317
318     double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
319     double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
320     if( aPointDepth == theAltitude->GetInvalidAltitude() )
321       aPointDepth = 0.0;
322
323     HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
324     aPointsList.Append( anAltitudePoint );
325   }
326
327   return aPointsList;
328 }
329
330 void HYDROData_Polyline3D::updateChildProfilePoints()
331 {
332   Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
333   if ( anAltitude.IsNull() )
334     return;
335
336   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
337   if ( aChildProfileUZ.IsNull() )
338     return;
339
340   Handle(HYDROData_Profile) aProfile = 
341     Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
342   if ( aProfile.IsNull() )
343     return;
344
345   HYDROData_IPolyline::PointsList aProfilePoints = 
346     generateProfileUZPoints( GetPolylineXY(), anAltitude );
347   aProfile->SetParametricPoints( aProfilePoints );
348
349   aProfile->Update();
350 }
351
352 void HYDROData_Polyline3D::removeChildProfileUZ()
353 {
354   Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
355   if ( aChildProfileUZ.IsNull() )
356     return;
357
358   ClearReferenceObjects( DataTag_ChildProfileUZ );
359
360   /* Uncomment if removing is requested
361   Handle(HYDROData_Profile) aProfile = 
362     Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
363   if ( !aProfile.IsNull() )
364     aProfile->Remove();
365   */
366 }
367
368