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_Channel.h"
21 #include "HYDROData_Document.h"
22 #include "HYDROData_Polyline3D.h"
23 #include "HYDROData_Profile.h"
24 #include "HYDROData_PolylineXY.h"
25 #include "HYDROData_Projection.h"
26 #include "HYDROData_ShapesGroup.h"
27 #include "HYDROData_ShapesTool.h"
28 #include "HYDROData_Stream.h"
29 #include "HYDROData_Tool.h"
31 #include <BRepBuilderAPI_MakeWire.hxx>
33 #include <BRepOffsetAPI_MakePipeShell.hxx>
34 #include <BRepOffsetAPI_MakePipe.hxx>
35 #include <BRepCheck_Analyzer.hxx>
37 #include <BRep_Tool.hxx>
39 #include <BRepBuilderAPI_Transform.hxx>
41 #include <BRepLib_MakeEdge.hxx>
42 #include <BRepLib_MakeWire.hxx>
44 #include <GeomAPI_ProjectPointOnCurve.hxx>
49 #include <TopExp_Explorer.hxx>
51 #include <TColgp_HArray1OfPnt.hxx>
52 #include <TColgp_Array1OfDir.hxx>
54 #include <TColStd_Array1OfReal.hxx>
56 #include <TopTools_HArray1OfShape.hxx>
57 #include <TopTools_SequenceOfShape.hxx>
60 #include <TopoDS_Wire.hxx>
61 #include <TopoDS_Vertex.hxx>
65 #include <BRepTools.hxx>
69 #include <QStringList>
72 #include "HYDRO_trace.hxx"
74 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Channel,HYDROData_ArtificialObject)
77 HYDROData_Channel::HYDROData_Channel()
78 : HYDROData_ArtificialObject( Geom_3d )
82 HYDROData_Channel::~HYDROData_Channel()
86 QStringList HYDROData_Channel::DumpToPython( const QString& thePyScriptPath,
87 MapOfTreatedObjects& theTreatedObjects ) const
89 QStringList aResList = dumpObjectCreation( theTreatedObjects );
90 QString aName = GetObjPyName();
92 Handle(HYDROData_Polyline3D) aRefGideLine = GetGuideLine();
93 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefGideLine, "SetGuideLine" );
95 Handle(HYDROData_Profile) aRefProfile = GetProfile();
96 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aRefProfile, "SetProfile" );
98 aResList << QString( "%1.SetEquiDistance( %2 )" ).arg( aName ).arg( GetEquiDistance() );
100 aResList << QString( "" );
101 aResList << QString( "%1.Update()" ).arg( aName );
102 aResList << QString( "" );
107 HYDROData_SequenceOfObjects HYDROData_Channel::GetAllReferenceObjects() const
109 HYDROData_SequenceOfObjects aResSeq = HYDROData_ArtificialObject::GetAllReferenceObjects();
111 Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
112 if ( !aGuideLine.IsNull() )
113 aResSeq.Append( aGuideLine );
115 Handle(HYDROData_Profile) aProfile = GetProfile();
116 if ( !aProfile.IsNull() )
117 aResSeq.Append( aProfile );
122 bool HYDROData_Channel::CreatePresentations( const Handle(HYDROData_Polyline3D)& theGuideLine,
123 const Handle(HYDROData_Profile)& theProfile,
124 PrsDefinition& thePrs,
125 double theEquiDistance )
127 // Check input parameters
128 if ( theGuideLine.IsNull() || theProfile.IsNull() ) {
132 TopoDS_Wire aPathWire = TopoDS::Wire( theGuideLine->GetShape3D() );
133 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetShape3D() );
134 if ( aPathWire.IsNull() || aProfileWire.IsNull() ) {
139 std::string brepName = "guideline_";
140 brepName += theGuideLine->GetName().toStdString();
142 BRepTools::Write( aPathWire, brepName.c_str() );
143 brepName = "profile_";
144 brepName += theGuideLine->GetName().toStdString();
146 BRepTools::Write( aProfileWire, brepName.c_str() );
150 Handle(HYDROData_PolylineXY) aPolylineXY = theGuideLine->GetPolylineXY();
151 if ( aPolylineXY.IsNull() ) {
156 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
157 HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
158 int aNbPoints = aPolylinePoints.Length();
161 HYDROData_Polyline3D::Polyline3DPoints aPolylinePoints3D = theGuideLine->GetPoints( theEquiDistance );
162 int aNbPoints = aPolylinePoints3D.Length();
166 // Get tangent in each point of the guide line ( 2D )
167 TColgp_Array1OfDir aTangents( 1, aNbPoints );
169 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
171 if( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE )
173 for ( int i = 1; i <= aNbPoints; ++i ) {
174 gp_XYZ aPnt = aPolylinePoints3D.Value( i );
178 aPrevPnt = aPolylinePoints3D.Value( i - 1 );
183 if ( i < aNbPoints ) {
184 gp_XYZ aNextPnt = aPolylinePoints3D.Value( i + 1 );
187 gp_Vec anEdgeVec( aPnt, aNextPnt );
192 gp_Vec aPrevVec( aPrevPnt, aPnt );
193 aDir = aPrevVec.Normalized() + anEdgeVec.Normalized();
196 aDir = gp_Vec( aPrevPnt, aPnt );
199 aTangents.SetValue( i, aDir );
202 // Get curve from the first edge ( 2D )
203 TopTools_SequenceOfShape anEdges;
204 HYDROData_ShapesTool::ExploreShapeToShapes( aPolylineXY->GetShape(), TopAbs_EDGE, anEdges );
205 Standard_Real aStart, anEnd;
207 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
208 GeomAPI_ProjectPointOnCurve aProject;
211 for ( int i = 1; i <= aNbPoints; ++i ) {
212 gp_XYZ aPointToTest = aPolylinePoints3D.Value( i );
213 aPointToTest.SetZ( 0. );
215 aProject.Init( aPointToTest, aCurve );
216 Quantity_Parameter aParam = aProject.LowerDistanceParameter();
219 aCurve->D1( aParam, aPnt, aDir);
221 aTangents.SetValue( i, aDir );
225 // Get the profile middle point ( 3D )
226 gp_Pnt aMiddlePoint( theProfile->GetMiddlePoint( true ) );
228 // Translate the profile to each point on the guide line ( 3D )
229 Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, aNbPoints );
230 Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, aNbPoints );
231 Handle(TopTools_HArray1OfShape) anArrOfProfiles = new TopTools_HArray1OfShape( 1, aNbPoints );
233 for ( int i = 1; i <= aNbPoints; ++i ) {
234 // Get point on the guide line
235 gp_Pnt aPointOnGuide( aPolylinePoints3D.Value( i ) );
237 // Define translation and rotation:
238 gp_Trsf Translation, Rotation;
241 Translation.SetTranslation( aMiddlePoint, aPointOnGuide );
242 TopoDS_Wire aTransformedProfile =
243 TopoDS::Wire( BRepBuilderAPI_Transform( aProfileWire, Translation, Standard_True ) );
246 gp_Vec aVertical( 0., 0., 1. );
247 TopoDS_Vertex aLeftVertex, aRightVertex;
248 TopExp::Vertices( aTransformedProfile, aLeftVertex, aRightVertex );
249 gp_Pnt aLeftPoint = BRep_Tool::Pnt( aLeftVertex );
250 gp_Pnt aRightPoint = BRep_Tool::Pnt( aRightVertex );
251 gp_Vec aLeftToRight( aLeftPoint, aRightPoint);
252 gp_Vec NormalToProfile = aVertical ^ aLeftToRight;
254 gp_Vec aDir = aTangents.Value( i );
255 gp_Vec AxisOfRotation = NormalToProfile ^ aDir;
256 if (AxisOfRotation.Magnitude() <= gp::Resolution()) {
257 if ( aVertical * aLeftToRight < 0. ) {
258 gp_Ax1 theVertical(aPointOnGuide, gp::DZ() );
259 Rotation.SetRotation(theVertical, M_PI);
262 gp_Ax1 theAxis(aPointOnGuide, AxisOfRotation);
263 Standard_Real theAngle = NormalToProfile.AngleWithRef(aDir, AxisOfRotation);
264 Rotation.SetRotation(theAxis, theAngle);
267 aTransformedProfile = TopoDS::Wire(BRepBuilderAPI_Transform( aTransformedProfile, Rotation, Standard_True) );
269 // Get the first and the last points of the transformed profile
270 TopoDS_Vertex V1, V2;
271 TopExp::Vertices( aTransformedProfile, V1, V2 );
274 anArrayOfFPnt->SetValue( i, BRep_Tool::Pnt( V1 ) );
275 anArrayOfLPnt->SetValue( i, BRep_Tool::Pnt( V2 ) );
277 anArrOfProfiles->SetValue( i, aTransformedProfile );
280 // Create presentation
281 HYDROData_Stream::PrsDefinition aPrs;
282 Handle(TopTools_HArray1OfShape) anArrOf2DProfiles; // we don't need 2D profiles for channel/digue presentation
283 bool aRes = HYDROData_Stream::CreatePresentations( anArrayOfFPnt, anArrayOfLPnt,
284 anArrOfProfiles, anArrOf2DProfiles, aPrs );
286 thePrs.myPrs3D = aPrs.myPrs3D;
287 thePrs.myPrs2D = TopoDS::Face( aPrs.myPrs2D );
288 BRepBuilderAPI_MakeWire aMakeWire( aPrs.myLeftBank ) ;
289 thePrs.myLeftBank = aMakeWire.Wire();
290 aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myRightBank );
291 thePrs.myRightBank = aMakeWire.Wire();
292 aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myInlet );
293 thePrs.myInlet = aMakeWire.Wire();
294 aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myOutlet );
295 thePrs.myOutlet = aMakeWire.Wire();
301 void HYDROData_Channel::Update()
303 HYDROData_ArtificialObject::Update();
305 Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
306 Handle(HYDROData_Profile) aProfile = GetProfile();
308 PrsDefinition aResultPrs;
309 double anEquiDistance = GetEquiDistance();
310 if ( !CreatePresentations( aGuideLine, aProfile, aResultPrs, anEquiDistance ) )
313 SetShape3D( aResultPrs.myPrs3D );
314 SetTopShape( aResultPrs.myPrs2D );
316 // Create groups for channel
317 TopTools_SequenceOfShape aLeftBankEdges;
318 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myLeftBank, TopAbs_EDGE, aLeftBankEdges );
320 TopTools_SequenceOfShape aRightBankEdges;
321 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myRightBank, TopAbs_EDGE, aRightBankEdges );
323 TopTools_SequenceOfShape anInletEdges;
324 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myInlet, TopAbs_EDGE, anInletEdges );
326 TopTools_SequenceOfShape anOutletEdges;
327 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myOutlet, TopAbs_EDGE, anOutletEdges );
329 QString aLeftGroupName = GetName() + "_Left_Bank";
331 Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
332 aLeftGroup->SetName( aLeftGroupName );
333 aLeftGroup->SetShapes( aLeftBankEdges );
335 QString aRightGroupName = GetName() + "_Right_Bank";
337 Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
338 aRightGroup->SetName( aRightGroupName );
339 aRightGroup->SetShapes( aRightBankEdges );
341 QString anInGroupName = GetName() + "_Inlet";
343 Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
344 anInGroup->SetName( anInGroupName );
345 anInGroup->SetShapes( anInletEdges );
347 QString anOutGroupName = GetName() + "_Outlet";
349 Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
350 anOutGroup->SetName( anOutGroupName );
351 anOutGroup->SetShapes( anOutletEdges );
354 bool HYDROData_Channel::IsHas2dPrs() const
359 QColor HYDROData_Channel::DefaultFillingColor() const
361 return QColor( Qt::blue );
364 QColor HYDROData_Channel::DefaultBorderColor() const
366 return QColor( Qt::transparent );
369 bool HYDROData_Channel::SetGuideLine( const Handle(HYDROData_Polyline3D)& theGuideLine )
371 Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
373 if ( theGuideLine.IsNull() )
376 return !aPrevGuideLine.IsNull();
379 if ( IsEqual( aPrevGuideLine, theGuideLine ) )
382 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theGuideLine->GetTopShape() );
383 if ( aHydraulicWire.IsNull() )
384 return false; // The polyline must be a single wire
386 SetReferenceObject( theGuideLine, DataTag_GuideLine );
388 // Indicate model of the need to update the chanel presentation
394 Handle(HYDROData_Polyline3D) HYDROData_Channel::GetGuideLine() const
396 return Handle(HYDROData_Polyline3D)::DownCast(
397 GetReferenceObject( DataTag_GuideLine ) );
400 void HYDROData_Channel::RemoveGuideLine()
402 Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
403 if ( aPrevGuideLine.IsNull() )
406 ClearReferenceObjects( DataTag_GuideLine );
408 // Indicate model of the need to update the chanel presentation
412 bool HYDROData_Channel::SetProfile( const Handle(HYDROData_Profile)& theProfile )
414 Handle(HYDROData_Profile) aPrevProfile = GetProfile();
416 if ( theProfile.IsNull() )
419 return !aPrevProfile.IsNull();
422 if ( IsEqual( aPrevProfile, theProfile ) )
425 SetReferenceObject( theProfile, DataTag_Profile );
427 // Indicate model of the need to update the chanel presentation
433 Handle(HYDROData_Profile) HYDROData_Channel::GetProfile() const
435 return Handle(HYDROData_Profile)::DownCast(
436 GetReferenceObject( DataTag_Profile ) );
439 void HYDROData_Channel::RemoveProfile()
441 Handle(HYDROData_Profile) aPrevProfile = GetProfile();
442 if ( aPrevProfile.IsNull() )
445 ClearReferenceObjects( DataTag_Profile );
447 // Indicate model of the need to update the chanel presentation
451 ObjectKind HYDROData_Channel::getAltitudeObjectType() const
453 DEBTRACE("HYDROData_Channel::getAltitudeObjectType");
454 return KIND_CHANNEL_ALTITUDE;
455 //return KIND_STREAM_ALTITUDE;
458 TopoDS_Shape HYDROData_Channel::GetLeftShape() const
460 HYDROData_SequenceOfObjects aGroups = GetGroups();
461 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 1);
464 TopoDS_Shape HYDROData_Channel::GetRightShape() const
466 HYDROData_SequenceOfObjects aGroups = GetGroups();
467 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 2);
470 void HYDROData_Channel::SetEquiDistance( double theEquiDistance )
472 double anEquiDistance = theEquiDistance > 0 ? theEquiDistance : 1E-3;
473 SetDouble( DataTag_EquiDistance, theEquiDistance );
476 double HYDROData_Channel::GetEquiDistance() const
478 return GetDouble( DataTag_EquiDistance, 1.0 );