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_ShapesTool.h"
27 #include "HYDROData_Tool.h"
29 #include <BRep_Tool.hxx>
31 #include <Geom_BSplineCurve.hxx>
33 #include <gp_Pnt2d.hxx>
37 #include <TColStd_Array1OfReal.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopoDS_Wire.hxx>
46 #include <TopExp_Explorer.hxx>
48 #include <TopTools_SequenceOfShape.hxx>
51 #include <QStringList>
53 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline3D,HYDROData_Object)
54 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
57 HYDROData_Polyline3D::HYDROData_Polyline3D()
58 : HYDROData_Object( Geom_3d )
62 HYDROData_Polyline3D::~HYDROData_Polyline3D()
66 QStringList HYDROData_Polyline3D::DumpToPython( const QString& thePyScriptPath,
67 MapOfTreatedObjects& theTreatedObjects ) const
69 QStringList aResList = dumpObjectCreation( theTreatedObjects );
70 QString aPolylineName = GetObjPyName();
72 Handle(HYDROData_PolylineXY) aRefPolyline = GetPolylineXY();
73 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefPolyline, "SetPolylineXY" );
75 Handle(HYDROData_ProfileUZ) aRefProfileUZ = GetProfileUZ();
76 if ( !aRefProfileUZ.IsNull() )
78 Handle(HYDROData_Profile) aProfile =
79 Handle(HYDROData_Profile)::DownCast( aRefProfileUZ->GetFatherObject() );
80 if ( checkObjectPythonDefinition( thePyScriptPath, theTreatedObjects, aResList, aProfile ) )
82 QString aProfileName = aProfile->GetObjPyName();
83 if ( !aProfileName.isEmpty() )
85 aResList << QString( "%1.SetProfileUZ( %2.GetProfileUZ() );" )
86 .arg( aPolylineName ).arg( aProfileName );
92 Handle(HYDROData_IAltitudeObject) aRefBathymetry = GetAltitudeObject();
93 if ( !aRefBathymetry.IsNull() )
95 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
96 if ( !aChildProfileUZ.IsNull() )
98 Handle(HYDROData_Profile) aProfile =
99 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
100 if ( checkObjectPythonDefinition( thePyScriptPath, theTreatedObjects, aResList, aProfile ) )
102 QString aProfileName = aProfile->GetObjPyName();
103 if ( !aProfileName.isEmpty() )
105 aResList << QString( "%1.SetChildProfileUZ( %2.GetProfileUZ() );" )
106 .arg( aPolylineName ).arg( aProfileName );
111 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefBathymetry, "SetAltitudeObject" );
115 aResList << QString( "" );
116 aResList << QString( "%1.Update();" ).arg( aPolylineName );
117 aResList << QString( "" );
122 HYDROData_SequenceOfObjects HYDROData_Polyline3D::GetAllReferenceObjects() const
124 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
126 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
127 if ( !aPolylineXY.IsNull() )
128 aResSeq.Append( aPolylineXY );
130 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
131 if ( !aProfileUZ.IsNull() )
132 aResSeq.Append( aProfileUZ );
134 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
135 if ( !aChildProfileUZ.IsNull() )
136 aResSeq.Append( aChildProfileUZ );
141 void HYDROData_Polyline3D::Update()
143 HYDROData_Object::Update();
145 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
146 if ( aPolylineXY.IsNull() )
149 bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
150 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
151 HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
152 if ( aPolylinePoints.IsEmpty() )
155 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
157 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
158 if ( !anAltitude.IsNull() )
159 aProfileUZ = GetChildProfileUZ();
161 if ( aProfileUZ.IsNull() )
164 HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
165 if ( aProfilePoints.IsEmpty() )
168 const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
169 const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
171 const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
172 const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
174 double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
175 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
177 NCollection_Sequence<Polyline3DPoint> aResPoints;
179 // Add first point as is
180 aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
182 for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
184 const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
186 double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
188 double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
189 double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
191 Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
192 aResPoints.Append( aCompPoint );
195 // Add last point as is
196 aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
198 TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
199 SetTopShape( aResWire );
200 SetShape3D( aResWire );
203 QColor HYDROData_Polyline3D::DefaultFillingColor() const
205 return QColor( Qt::transparent );
208 QColor HYDROData_Polyline3D::DefaultBorderColor() const
210 return QColor( Qt::red );
213 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
214 const bool theIsUpdateProfile )
216 if ( thePolyline.IsNull() )
219 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
220 if ( IsEqual( aPrevPolyline, thePolyline ) )
223 SetReferenceObject( thePolyline, DataTag_PolylineXY );
225 // Update the child profile object
226 if ( theIsUpdateProfile )
227 updateChildProfilePoints();
229 // Indicate model of the need to update the polyline presentation
235 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
237 return Handle(HYDROData_PolylineXY)::DownCast(
238 GetReferenceObject( DataTag_PolylineXY ) );
241 void HYDROData_Polyline3D::RemovePolylineXY()
243 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
244 if ( aPrevPolyline.IsNull() )
247 ClearReferenceObjects( DataTag_PolylineXY );
249 // Indicate model of the need to update the polyline presentation
253 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
255 if ( theProfile.IsNull() )
258 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
259 if ( IsEqual( aPrevProfile, theProfile ) )
262 SetReferenceObject( theProfile, DataTag_ProfileUZ );
264 // Remove the bathymetry, because one altitude object can be presented at time
265 RemoveAltitudeObject();
267 // Indicate model of the need to update the polyline presentation
273 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
275 return Handle(HYDROData_ProfileUZ)::DownCast(
276 GetReferenceObject( DataTag_ProfileUZ ) );
279 void HYDROData_Polyline3D::RemoveProfileUZ()
281 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
282 if ( aPrevProfile.IsNull() )
285 ClearReferenceObjects( DataTag_ProfileUZ );
287 // Indicate model of the need to update the polyline presentation
291 bool HYDROData_Polyline3D::SetAltitudeObject(
292 const Handle(HYDROData_IAltitudeObject)& theAltitude )
294 Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
296 if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
299 if ( IsEqual( aPrevAltitude, theAltitude ) )
302 // Remove the u,z profile, because one altitude object can be presented at time
305 // Create the child profile object
306 updateChildProfilePoints();
312 void HYDROData_Polyline3D::RemoveAltitudeObject()
314 HYDROData_Object::RemoveAltitudeObject();
316 // Remove the child profile object
317 removeChildProfileUZ();
320 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
322 Handle(HYDROData_ProfileUZ) aProfileUZ =
323 Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
324 if ( !theIsCreate || !aProfileUZ.IsNull() )
327 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
328 if ( aDocument.IsNull() )
331 Handle(HYDROData_Profile) aProfile =
332 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
334 QString aProfilePref = GetName() + "_Profile";
335 QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
337 aProfile->SetName( aProfileName );
339 aProfileUZ = aProfile->GetProfileUZ();
341 HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
342 me->SetChildProfileUZ( aProfileUZ );
347 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
349 SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
352 HYDROData_IPolyline::PointsList generateProfileUZPoints(
353 const Handle(HYDROData_PolylineXY)& thePolyline,
354 const Handle(HYDROData_IAltitudeObject)& theAltitude )
356 HYDROData_IPolyline::PointsList aPointsList;
357 if ( thePolyline.IsNull() || theAltitude.IsNull() )
360 bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
361 HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
362 HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
363 if ( aPolylinePoints.IsEmpty() )
366 for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
368 const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
370 double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
371 double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
372 if( aPointDepth == theAltitude->GetInvalidAltitude() )
375 HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
376 aPointsList.Append( anAltitudePoint );
382 void HYDROData_Polyline3D::updateChildProfilePoints()
384 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
385 if ( anAltitude.IsNull() )
388 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
389 if ( aChildProfileUZ.IsNull() )
392 Handle(HYDROData_Profile) aProfile =
393 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
394 if ( aProfile.IsNull() )
397 HYDROData_IPolyline::PointsList aProfilePoints =
398 generateProfileUZPoints( GetPolylineXY(), anAltitude );
399 aProfile->SetParametricPoints( aProfilePoints );
404 void HYDROData_Polyline3D::removeChildProfileUZ()
406 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
407 if ( aChildProfileUZ.IsNull() )
410 ClearReferenceObjects( DataTag_ChildProfileUZ );
412 /* Uncomment if removing is requested
413 Handle(HYDROData_Profile) aProfile =
414 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
415 if ( !aProfile.IsNull() )
420 HYDROData_Polyline3D::Polyline3DPoints HYDROData_Polyline3D::GetPoints() const
422 Polyline3DPoints aPoints;
424 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
425 TopoDS_Wire aWire = TopoDS::Wire( GetShape3D() );
426 if ( aPolylineXY.IsNull() || aWire.IsNull() ) {
430 // Explode polyline on edges
431 TopTools_SequenceOfShape anEdges;
432 HYDROData_ShapesTool::ExploreShapeToShapes( aWire, TopAbs_EDGE, anEdges );
435 if ( !anEdges.IsEmpty() ) {
436 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
438 if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
439 // Get points from wire
440 /* Seems that intermediate vertices are duplicated
441 TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
442 for ( ; anExp.More(); anExp.Next() ) {
443 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
444 if ( !aVertex.IsNull() ) {
445 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
446 aPoints.Append( aPnt.XYZ() );
450 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
452 for ( ; anExp.More(); anExp.Next() ) {
453 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
454 if ( !anEdge.IsNull() ) {
455 TopoDS_Vertex aV1, aV2;
456 TopExp::Vertices( anEdge, aV1, aV2 );
458 gp_Pnt aPnt1 = BRep_Tool::Pnt( aV1 );
459 aPoints.Append( aPnt1.XYZ() );
462 gp_Pnt aPnt2 = BRep_Tool::Pnt( aV2 );
463 aPoints.Append( aPnt2.XYZ() );
469 // Get points from spline curve
470 Standard_Real aStart, anEnd;
471 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
472 Handle(Geom_BSplineCurve) aGeomSpline = Handle(Geom_BSplineCurve)::DownCast( aCurve );
474 if ( !aGeomSpline.IsNull() ) {
475 int aNbKnots = aGeomSpline->NbKnots();
477 TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
478 aGeomSpline->Knots( aSplineKnots );
480 for ( int i = 1; i <= aNbKnots; ++i ) {
481 const Standard_Real& aKnot = aSplineKnots.Value( i );
483 aGeomSpline->D0( aKnot, aPnt );
484 aPoints.Append( aPnt.XYZ() );