1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_Polyline3D.h"
21 #include "HYDROData_IAltitudeObject.h"
22 #include "HYDROData_Document.h"
23 #include "HYDROData_PolylineXY.h"
24 #include "HYDROData_Profile.h"
25 #include "HYDROData_ProfileUZ.h"
26 #include "HYDROData_Tool.h"
28 #include <gp_Pnt2d.hxx>
33 #include <TopoDS_Wire.hxx>
36 #include <QStringList>
38 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline3D,HYDROData_Object)
39 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
42 HYDROData_Polyline3D::HYDROData_Polyline3D()
47 HYDROData_Polyline3D::~HYDROData_Polyline3D()
51 QStringList HYDROData_Polyline3D::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
53 QStringList aResList = dumpObjectCreation( theTreatedObjects );
54 QString aPolylineName = GetObjPyName();
56 Handle(HYDROData_PolylineXY) aRefPolyline = GetPolylineXY();
57 setPythonReferenceObject( theTreatedObjects, aResList, aRefPolyline, "SetPolylineXY" );
59 Handle(HYDROData_ProfileUZ) aRefProfileUZ = GetProfileUZ();
60 if ( !aRefProfileUZ.IsNull() )
62 Handle(HYDROData_Profile) aProfile =
63 Handle(HYDROData_Profile)::DownCast( aRefProfileUZ->GetFatherObject() );
64 if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
66 QString aProfileName = aProfile->GetObjPyName();
67 if ( !aProfileName.isEmpty() )
69 aResList << QString( "%1.SetProfileUZ( %2.GetProfileUZ() );" )
70 .arg( aPolylineName ).arg( aProfileName );
76 Handle(HYDROData_IAltitudeObject) aRefBathymetry = GetAltitudeObject();
77 if ( !aRefBathymetry.IsNull() )
79 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
80 if ( !aChildProfileUZ.IsNull() )
82 Handle(HYDROData_Profile) aProfile =
83 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
84 if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
86 QString aProfileName = aProfile->GetObjPyName();
87 if ( !aProfileName.isEmpty() )
89 aResList << QString( "%1.SetChildProfileUZ( %2.GetProfileUZ() );" )
90 .arg( aPolylineName ).arg( aProfileName );
95 setPythonReferenceObject( theTreatedObjects, aResList, aRefBathymetry, "SetAltitudeObject" );
99 aResList << QString( "" );
100 aResList << QString( "%1.Update();" ).arg( aPolylineName );
101 aResList << QString( "" );
106 HYDROData_SequenceOfObjects HYDROData_Polyline3D::GetAllReferenceObjects() const
108 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
110 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
111 if ( !aPolylineXY.IsNull() )
112 aResSeq.Append( aPolylineXY );
114 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
115 if ( !aProfileUZ.IsNull() )
116 aResSeq.Append( aProfileUZ );
118 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
119 if ( !aChildProfileUZ.IsNull() )
120 aResSeq.Append( aChildProfileUZ );
125 TopoDS_Shape HYDROData_Polyline3D::GetTopShape() const
127 return getTopShape();
130 TopoDS_Shape HYDROData_Polyline3D::GetShape3D() const
135 void HYDROData_Polyline3D::Update()
137 HYDROData_Object::Update();
139 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
140 if ( aPolylineXY.IsNull() )
143 bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
144 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
145 HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
146 if ( aPolylinePoints.IsEmpty() )
149 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
151 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
152 if ( !anAltitude.IsNull() )
153 aProfileUZ = GetChildProfileUZ();
155 if ( aProfileUZ.IsNull() )
158 HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
159 if ( aProfilePoints.IsEmpty() )
162 const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
163 const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
165 const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
166 const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
168 double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
169 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
171 NCollection_Sequence<Polyline3DPoint> aResPoints;
173 // Add first point as is
174 aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
176 for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
178 const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
180 double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
182 double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
183 double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
185 Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
186 aResPoints.Append( aCompPoint );
189 // Add last point as is
190 aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
192 TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
193 SetTopShape( aResWire );
194 SetShape3D( aResWire );
197 QColor HYDROData_Polyline3D::DefaultFillingColor()
199 return QColor( Qt::transparent );
202 QColor HYDROData_Polyline3D::DefaultBorderColor()
204 return QColor( Qt::red );
207 QColor HYDROData_Polyline3D::getDefaultFillingColor() const
209 return DefaultFillingColor();
212 QColor HYDROData_Polyline3D::getDefaultBorderColor() const
214 return DefaultBorderColor();
217 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
218 const bool theIsUpdateProfile )
220 if ( thePolyline.IsNull() )
223 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
224 if ( IsEqual( aPrevPolyline, thePolyline ) )
227 SetReferenceObject( thePolyline, DataTag_PolylineXY );
229 // Update the child profile object
230 if ( theIsUpdateProfile )
231 updateChildProfilePoints();
233 // Indicate model of the need to update the polyline presentation
239 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
241 return Handle(HYDROData_PolylineXY)::DownCast(
242 GetReferenceObject( DataTag_PolylineXY ) );
245 void HYDROData_Polyline3D::RemovePolylineXY()
247 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
248 if ( aPrevPolyline.IsNull() )
251 ClearReferenceObjects( DataTag_PolylineXY );
253 // Indicate model of the need to update the polyline presentation
257 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
259 if ( theProfile.IsNull() )
262 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
263 if ( IsEqual( aPrevProfile, theProfile ) )
266 SetReferenceObject( theProfile, DataTag_ProfileUZ );
268 // Remove the bathymetry, because one altitude object can be presented at time
269 RemoveAltitudeObject();
271 // Indicate model of the need to update the polyline presentation
277 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
279 return Handle(HYDROData_ProfileUZ)::DownCast(
280 GetReferenceObject( DataTag_ProfileUZ ) );
283 void HYDROData_Polyline3D::RemoveProfileUZ()
285 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
286 if ( aPrevProfile.IsNull() )
289 ClearReferenceObjects( DataTag_ProfileUZ );
291 // Indicate model of the need to update the polyline presentation
295 bool HYDROData_Polyline3D::SetAltitudeObject(
296 const Handle(HYDROData_IAltitudeObject)& theAltitude )
298 Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
300 if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
303 if ( IsEqual( aPrevAltitude, theAltitude ) )
306 // Remove the u,z profile, because one altitude object can be presented at time
309 // Create the child profile object
310 updateChildProfilePoints();
316 void HYDROData_Polyline3D::RemoveAltitudeObject()
318 HYDROData_Object::RemoveAltitudeObject();
320 // Remove the child profile object
321 removeChildProfileUZ();
324 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
326 Handle(HYDROData_ProfileUZ) aProfileUZ =
327 Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
328 if ( !theIsCreate || !aProfileUZ.IsNull() )
331 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
332 if ( aDocument.IsNull() )
335 Handle(HYDROData_Profile) aProfile =
336 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
338 QString aProfilePref = GetName() + "_Profile";
339 QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
341 aProfile->SetName( aProfileName );
343 aProfileUZ = aProfile->GetProfileUZ();
345 HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
346 me->SetChildProfileUZ( aProfileUZ );
351 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
353 SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
356 HYDROData_IPolyline::PointsList generateProfileUZPoints(
357 const Handle(HYDROData_PolylineXY)& thePolyline,
358 const Handle(HYDROData_IAltitudeObject)& theAltitude )
360 HYDROData_IPolyline::PointsList aPointsList;
361 if ( thePolyline.IsNull() || theAltitude.IsNull() )
364 bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
365 HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
366 HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
367 if ( aPolylinePoints.IsEmpty() )
370 for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
372 const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
374 double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
375 double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
376 if( aPointDepth == theAltitude->GetInvalidAltitude() )
379 HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
380 aPointsList.Append( anAltitudePoint );
386 void HYDROData_Polyline3D::updateChildProfilePoints()
388 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
389 if ( anAltitude.IsNull() )
392 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
393 if ( aChildProfileUZ.IsNull() )
396 Handle(HYDROData_Profile) aProfile =
397 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
398 if ( aProfile.IsNull() )
401 HYDROData_IPolyline::PointsList aProfilePoints =
402 generateProfileUZPoints( GetPolylineXY(), anAltitude );
403 aProfile->SetParametricPoints( aProfilePoints );
408 void HYDROData_Polyline3D::removeChildProfileUZ()
410 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
411 if ( aChildProfileUZ.IsNull() )
414 ClearReferenceObjects( DataTag_ChildProfileUZ );
416 /* Uncomment if removing is requested
417 Handle(HYDROData_Profile) aProfile =
418 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
419 if ( !aProfile.IsNull() )