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 <Bnd_Box.hxx>
32 #include <BRepBndLib.hxx>
34 #include <Geom_BSplineCurve.hxx>
36 #include <gp_Pnt2d.hxx>
40 #include <TColStd_Array1OfReal.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopoDS_Wire.hxx>
49 #include <TopExp_Explorer.hxx>
51 #include <TopTools_SequenceOfShape.hxx>
54 #include <QStringList>
56 IMPLEMENT_STANDARD_HANDLE(HYDROData_Polyline3D,HYDROData_Object)
57 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Polyline3D,HYDROData_Object)
60 HYDROData_Polyline3D::HYDROData_Polyline3D()
65 HYDROData_Polyline3D::~HYDROData_Polyline3D()
69 QStringList HYDROData_Polyline3D::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
71 QStringList aResList = dumpObjectCreation( theTreatedObjects );
72 QString aPolylineName = GetObjPyName();
74 Handle(HYDROData_PolylineXY) aRefPolyline = GetPolylineXY();
75 setPythonReferenceObject( theTreatedObjects, aResList, aRefPolyline, "SetPolylineXY" );
77 Handle(HYDROData_ProfileUZ) aRefProfileUZ = GetProfileUZ();
78 if ( !aRefProfileUZ.IsNull() )
80 Handle(HYDROData_Profile) aProfile =
81 Handle(HYDROData_Profile)::DownCast( aRefProfileUZ->GetFatherObject() );
82 if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
84 QString aProfileName = aProfile->GetObjPyName();
85 if ( !aProfileName.isEmpty() )
87 aResList << QString( "%1.SetProfileUZ( %2.GetProfileUZ() );" )
88 .arg( aPolylineName ).arg( aProfileName );
94 Handle(HYDROData_IAltitudeObject) aRefBathymetry = GetAltitudeObject();
95 if ( !aRefBathymetry.IsNull() )
97 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
98 if ( !aChildProfileUZ.IsNull() )
100 Handle(HYDROData_Profile) aProfile =
101 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
102 if ( checkObjectPythonDefinition( theTreatedObjects, aResList, aProfile ) )
104 QString aProfileName = aProfile->GetObjPyName();
105 if ( !aProfileName.isEmpty() )
107 aResList << QString( "%1.SetChildProfileUZ( %2.GetProfileUZ() );" )
108 .arg( aPolylineName ).arg( aProfileName );
113 setPythonReferenceObject( theTreatedObjects, aResList, aRefBathymetry, "SetAltitudeObject" );
117 aResList << QString( "" );
118 aResList << QString( "%1.Update();" ).arg( aPolylineName );
119 aResList << QString( "" );
124 HYDROData_SequenceOfObjects HYDROData_Polyline3D::GetAllReferenceObjects() const
126 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
128 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
129 if ( !aPolylineXY.IsNull() )
130 aResSeq.Append( aPolylineXY );
132 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
133 if ( !aProfileUZ.IsNull() )
134 aResSeq.Append( aProfileUZ );
136 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
137 if ( !aChildProfileUZ.IsNull() )
138 aResSeq.Append( aChildProfileUZ );
143 TopoDS_Shape HYDROData_Polyline3D::GetTopShape() const
145 return getTopShape();
148 TopoDS_Shape HYDROData_Polyline3D::GetShape3D() const
153 void HYDROData_Polyline3D::Update()
155 HYDROData_Object::Update();
157 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
158 if ( aPolylineXY.IsNull() )
161 bool anIsSectionClosed = aPolylineXY->IsClosedSection( 0 );
162 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
163 HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
164 if ( aPolylinePoints.IsEmpty() )
167 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
169 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
170 if ( !anAltitude.IsNull() )
171 aProfileUZ = GetChildProfileUZ();
173 if ( aProfileUZ.IsNull() )
176 HYDROData_IPolyline::PointsList aProfilePoints = aProfileUZ->GetPoints();
177 if ( aProfilePoints.IsEmpty() )
180 const HYDROData_IPolyline::Point& aFirstPoint = aPolylinePoints.First();
181 const HYDROData_IPolyline::Point& aLastPoint = aPolylinePoints.Last();
183 const HYDROData_IPolyline::Point& aFirstParPoint = aProfilePoints.First();
184 const HYDROData_IPolyline::Point& aLastParPoint = aProfilePoints.Last();
186 double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylinePoints.Size() - 1 );
187 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
189 NCollection_Sequence<Polyline3DPoint> aResPoints;
191 // Add first point as is
192 aResPoints.Append( Polyline3DPoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
194 for ( int i = 2, aNbPoints = aPolylinePoints.Size(); i < aNbPoints; ++i )
196 const HYDROData_IPolyline::Point& aPolylinePoint = aPolylinePoints.Value( i );
198 double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
200 double aParLen = ( aDistance / aPolylineCommonDist ) * aParCommonDist;
201 double aDepth = HYDROData_ProfileUZ::GetDepthFromDistance( aProfilePoints, aParLen );
203 Polyline3DPoint aCompPoint( aPolylinePoint.X(), aPolylinePoint.Y(), aDepth );
204 aResPoints.Append( aCompPoint );
207 // Add last point as is
208 aResPoints.Append( Polyline3DPoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
210 TopoDS_Wire aResWire = HYDROData_PolylineXY::BuildWire( aSectionType, anIsSectionClosed, aResPoints );
211 SetTopShape( aResWire );
212 SetShape3D( aResWire );
214 double Xmin=0, Xmax=0, Ymin=0, Ymax=0, Zmin=-9999, Zmax=-9999;
216 BRepBndLib::Add(aResWire, B);
217 B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
218 SetMiddleZ((Zmin+Zmax)/2.);
221 QColor HYDROData_Polyline3D::DefaultFillingColor()
223 return QColor( Qt::transparent );
226 QColor HYDROData_Polyline3D::DefaultBorderColor()
228 return QColor( Qt::red );
231 QColor HYDROData_Polyline3D::getDefaultFillingColor() const
233 return DefaultFillingColor();
236 QColor HYDROData_Polyline3D::getDefaultBorderColor() const
238 return DefaultBorderColor();
241 bool HYDROData_Polyline3D::SetPolylineXY( const Handle(HYDROData_PolylineXY)& thePolyline,
242 const bool theIsUpdateProfile )
244 if ( thePolyline.IsNull() )
247 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
248 if ( IsEqual( aPrevPolyline, thePolyline ) )
251 SetReferenceObject( thePolyline, DataTag_PolylineXY );
253 // Update the child profile object
254 if ( theIsUpdateProfile )
255 updateChildProfilePoints();
257 // Indicate model of the need to update the polyline presentation
263 Handle(HYDROData_PolylineXY) HYDROData_Polyline3D::GetPolylineXY() const
265 return Handle(HYDROData_PolylineXY)::DownCast(
266 GetReferenceObject( DataTag_PolylineXY ) );
269 void HYDROData_Polyline3D::RemovePolylineXY()
271 Handle(HYDROData_PolylineXY) aPrevPolyline = GetPolylineXY();
272 if ( aPrevPolyline.IsNull() )
275 ClearReferenceObjects( DataTag_PolylineXY );
277 // Indicate model of the need to update the polyline presentation
281 bool HYDROData_Polyline3D::SetProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
283 if ( theProfile.IsNull() )
286 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
287 if ( IsEqual( aPrevProfile, theProfile ) )
290 SetReferenceObject( theProfile, DataTag_ProfileUZ );
292 // Remove the bathymetry, because one altitude object can be presented at time
293 RemoveAltitudeObject();
295 // Indicate model of the need to update the polyline presentation
301 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetProfileUZ() const
303 return Handle(HYDROData_ProfileUZ)::DownCast(
304 GetReferenceObject( DataTag_ProfileUZ ) );
307 void HYDROData_Polyline3D::RemoveProfileUZ()
309 Handle(HYDROData_ProfileUZ) aPrevProfile = GetProfileUZ();
310 if ( aPrevProfile.IsNull() )
313 ClearReferenceObjects( DataTag_ProfileUZ );
315 // Indicate model of the need to update the polyline presentation
319 bool HYDROData_Polyline3D::SetAltitudeObject(
320 const Handle(HYDROData_IAltitudeObject)& theAltitude )
322 Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
324 if ( !HYDROData_Object::SetAltitudeObject( theAltitude ) )
327 if ( IsEqual( aPrevAltitude, theAltitude ) )
330 // Remove the u,z profile, because one altitude object can be presented at time
333 // Create the child profile object
334 updateChildProfilePoints();
340 void HYDROData_Polyline3D::RemoveAltitudeObject()
342 HYDROData_Object::RemoveAltitudeObject();
344 // Remove the child profile object
345 removeChildProfileUZ();
348 Handle(HYDROData_ProfileUZ) HYDROData_Polyline3D::GetChildProfileUZ( const bool theIsCreate ) const
350 Handle(HYDROData_ProfileUZ) aProfileUZ =
351 Handle(HYDROData_ProfileUZ)::DownCast( GetReferenceObject( DataTag_ChildProfileUZ ) );
352 if ( !theIsCreate || !aProfileUZ.IsNull() )
355 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
356 if ( aDocument.IsNull() )
359 Handle(HYDROData_Profile) aProfile =
360 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
362 QString aProfilePref = GetName() + "_Profile";
363 QString aProfileName = HYDROData_Tool::GenerateObjectName( aDocument, aProfilePref );
365 aProfile->SetName( aProfileName );
367 aProfileUZ = aProfile->GetProfileUZ();
369 HYDROData_Polyline3D* me = const_cast<HYDROData_Polyline3D*>( this ); // Temporary to be revised
370 me->SetChildProfileUZ( aProfileUZ );
375 void HYDROData_Polyline3D::SetChildProfileUZ( const Handle(HYDROData_ProfileUZ)& theProfile )
377 SetReferenceObject( theProfile, DataTag_ChildProfileUZ );
380 HYDROData_IPolyline::PointsList generateProfileUZPoints(
381 const Handle(HYDROData_PolylineXY)& thePolyline,
382 const Handle(HYDROData_IAltitudeObject)& theAltitude )
384 HYDROData_IPolyline::PointsList aPointsList;
385 if ( thePolyline.IsNull() || theAltitude.IsNull() )
388 bool anIsSectionClosed = thePolyline->IsClosedSection( 0 );
389 HYDROData_IPolyline::SectionType aSectionType = thePolyline->GetSectionType( 0 );
390 HYDROData_IPolyline::PointsList aPolylinePoints = thePolyline->GetPoints( 0 );
391 if ( aPolylinePoints.IsEmpty() )
394 for ( int i = 1, aNbPoints = aPolylinePoints.Size(); i <= aNbPoints; ++i )
396 const HYDROData_PolylineXY::Point& aSectPoint = aPolylinePoints.Value( i );
398 double aPointDistance = thePolyline->GetDistance( 0, i - 1 );
399 double aPointDepth = theAltitude->GetAltitudeForPoint( aSectPoint );
400 if( aPointDepth == theAltitude->GetInvalidAltitude() )
403 HYDROData_IPolyline::Point anAltitudePoint( aPointDistance, aPointDepth );
404 aPointsList.Append( anAltitudePoint );
410 void HYDROData_Polyline3D::updateChildProfilePoints()
412 Handle(HYDROData_IAltitudeObject) anAltitude = GetAltitudeObject();
413 if ( anAltitude.IsNull() )
416 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ();
417 if ( aChildProfileUZ.IsNull() )
420 Handle(HYDROData_Profile) aProfile =
421 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
422 if ( aProfile.IsNull() )
425 HYDROData_IPolyline::PointsList aProfilePoints =
426 generateProfileUZPoints( GetPolylineXY(), anAltitude );
427 aProfile->SetParametricPoints( aProfilePoints );
432 void HYDROData_Polyline3D::removeChildProfileUZ()
434 Handle(HYDROData_ProfileUZ) aChildProfileUZ = GetChildProfileUZ( false );
435 if ( aChildProfileUZ.IsNull() )
438 ClearReferenceObjects( DataTag_ChildProfileUZ );
440 /* Uncomment if removing is requested
441 Handle(HYDROData_Profile) aProfile =
442 Handle(HYDROData_Profile)::DownCast( aChildProfileUZ->GetFatherObject() );
443 if ( !aProfile.IsNull() )
448 HYDROData_Polyline3D::Polyline3DPoints HYDROData_Polyline3D::GetPoints() const
450 Polyline3DPoints aPoints;
452 Handle(HYDROData_PolylineXY) aPolylineXY = GetPolylineXY();
453 TopoDS_Wire aWire = TopoDS::Wire( GetShape3D() );
454 if ( aPolylineXY.IsNull() || aWire.IsNull() ) {
458 // Explode polyline on edges
459 TopTools_SequenceOfShape anEdges;
460 HYDROData_ShapesTool::ExploreShapeToShapes( aWire, TopAbs_EDGE, anEdges );
463 if ( !anEdges.IsEmpty() ) {
464 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
466 if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
467 // Get points from wire
468 /* Seems that intermediate vertices are duplicated
469 TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
470 for ( ; anExp.More(); anExp.Next() ) {
471 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
472 if ( !aVertex.IsNull() ) {
473 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
474 aPoints.Append( aPnt.XYZ() );
478 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
480 for ( ; anExp.More(); anExp.Next() ) {
481 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
482 if ( !anEdge.IsNull() ) {
483 TopoDS_Vertex aV1, aV2;
484 TopExp::Vertices( anEdge, aV1, aV2 );
486 gp_Pnt aPnt1 = BRep_Tool::Pnt( aV1 );
487 aPoints.Append( aPnt1.XYZ() );
490 gp_Pnt aPnt2 = BRep_Tool::Pnt( aV2 );
491 aPoints.Append( aPnt2.XYZ() );
497 // Get points from spline curve
498 Standard_Real aStart, anEnd;
499 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
500 Handle(Geom_BSplineCurve) aGeomSpline = Handle(Geom_BSplineCurve)::DownCast( aCurve );
502 if ( !aGeomSpline.IsNull() ) {
503 int aNbKnots = aGeomSpline->NbKnots();
505 TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
506 aGeomSpline->Knots( aSplineKnots );
508 for ( int i = 1; i <= aNbKnots; ++i ) {
509 const Standard_Real& aKnot = aSplineKnots.Value( i );
511 aGeomSpline->D0( aKnot, aPnt );
512 aPoints.Append( aPnt.XYZ() );