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_Pipes.h"
29 #include "HYDROData_Stream.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>
59 #include <TopoDS_Wire.hxx>
60 #include <TopoDS_Vertex.hxx>
64 #include <BRepTools.hxx>
68 #include <QStringList>
71 #include "HYDRO_trace.hxx"
73 IMPLEMENT_STANDARD_HANDLE(HYDROData_Channel,HYDROData_ArtificialObject)
74 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Channel,HYDROData_ArtificialObject)
77 HYDROData_Channel::HYDROData_Channel()
78 : HYDROData_ArtificialObject()
82 HYDROData_Channel::~HYDROData_Channel()
86 QStringList HYDROData_Channel::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
88 QStringList aResList = dumpObjectCreation( theTreatedObjects );
89 QString aName = GetObjPyName();
91 Handle(HYDROData_Polyline3D) aRefGideLine = GetGuideLine();
92 setPythonReferenceObject( theTreatedObjects, aResList, aRefGideLine, "SetGuideLine" );
94 Handle(HYDROData_Profile) aRefProfile = GetProfile();
95 setPythonReferenceObject( theTreatedObjects, aResList, aRefProfile, "SetProfile" );
97 aResList << QString( "" );
98 aResList << QString( "%1.Update();" ).arg( aName );
99 aResList << QString( "" );
104 HYDROData_SequenceOfObjects HYDROData_Channel::GetAllReferenceObjects() const
106 HYDROData_SequenceOfObjects aResSeq = HYDROData_ArtificialObject::GetAllReferenceObjects();
108 Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
109 if ( !aGuideLine.IsNull() )
110 aResSeq.Append( aGuideLine );
112 Handle(HYDROData_Profile) aProfile = GetProfile();
113 if ( !aProfile.IsNull() )
114 aResSeq.Append( aProfile );
119 TopoDS_Shape HYDROData_Channel::GetTopShape() const
121 return getTopShape();
124 TopoDS_Shape HYDROData_Channel::GetShape3D() const
129 bool HYDROData_Channel::CreatePresentations( const Handle(HYDROData_Polyline3D)& theGuideLine,
130 const Handle(HYDROData_Profile)& theProfile,
131 PrsDefinition& thePrs )
133 // Check input parameters
134 if ( theGuideLine.IsNull() || theProfile.IsNull() ) {
138 TopoDS_Wire aPathWire = TopoDS::Wire( theGuideLine->GetShape3D() );
139 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetShape3D() );
140 if ( aPathWire.IsNull() || aProfileWire.IsNull() ) {
145 std::string brepName = "guideline_";
146 brepName += theGuideLine->GetName().toStdString();
148 BRepTools::Write( aPathWire, brepName.c_str() );
149 brepName = "profile_";
150 brepName += theGuideLine->GetName().toStdString();
152 BRepTools::Write( aProfileWire, brepName.c_str() );
156 Handle(HYDROData_PolylineXY) aPolylineXY = theGuideLine->GetPolylineXY();
157 if ( aPolylineXY.IsNull() ) {
162 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
163 HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
164 int aNbPoints = aPolylinePoints.Length();
167 HYDROData_Polyline3D::Polyline3DPoints aPolylinePoints3D = theGuideLine->GetPoints();
168 int aNbPoints = aPolylinePoints3D.Length();
169 if ( aNbPoints < 2 ) {
173 // Get tangent in each point of the guide line ( 2D )
174 TColgp_Array1OfDir aTangents( 1, aNbPoints );
176 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
178 if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
179 for ( int i = 1; i <= aNbPoints; ++i ) {
180 gp_XYZ aPnt = aPolylinePoints3D.Value( i );
184 aPrevPnt = aPolylinePoints3D.Value( i - 1 );
189 if ( i < aNbPoints ) {
190 gp_XYZ aNextPnt = aPolylinePoints3D.Value( i + 1 );
193 gp_Vec anEdgeVec( aPnt, aNextPnt );
198 gp_Vec aPrevVec( aPrevPnt, aPnt );
199 aDir = aPrevVec.Normalized() + anEdgeVec.Normalized();
202 aDir = gp_Vec( aPrevPnt, aPnt );
205 aTangents.SetValue( i, aDir );
208 // Get curve from the first edge ( 2D )
209 TopTools_SequenceOfShape anEdges;
210 HYDROData_ShapesTool::ExploreShapeToShapes( aPolylineXY->GetShape(), TopAbs_EDGE, anEdges );
211 Standard_Real aStart, anEnd;
213 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
214 GeomAPI_ProjectPointOnCurve aProject;
217 for ( int i = 1; i <= aNbPoints; ++i ) {
218 gp_XYZ aPointToTest = aPolylinePoints3D.Value( i );
219 aPointToTest.SetZ( 0. );
221 aProject.Init( aPointToTest, aCurve );
222 Quantity_Parameter aParam = aProject.LowerDistanceParameter();
225 aCurve->D1( aParam, aPnt, aDir);
227 aTangents.SetValue( i, aDir );
231 // Get the profile middle point ( 3D )
232 gp_Pnt aMiddlePoint( theProfile->GetMiddlePoint() );
234 // Translate the profile to each point on the guide line ( 3D )
235 Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, aNbPoints );
236 Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, aNbPoints );
237 Handle(TopTools_HArray1OfShape) anArrOfProfiles = new TopTools_HArray1OfShape( 1, aNbPoints );
239 for ( int i = 1; i <= aNbPoints; ++i ) {
240 // Get point on the guide line
241 gp_Pnt aPointOnGuide( aPolylinePoints3D.Value( i ) );
243 // Define translation and rotation:
244 gp_Trsf Translation, Rotation;
247 Translation.SetTranslation( aMiddlePoint, aPointOnGuide );
248 TopoDS_Wire aTransformedProfile =
249 TopoDS::Wire( BRepBuilderAPI_Transform( aProfileWire, Translation, Standard_True ) );
252 gp_Vec aVertical( 0., 0., 1. );
253 TopoDS_Vertex aLeftVertex, aRightVertex;
254 TopExp::Vertices( aTransformedProfile, aLeftVertex, aRightVertex );
255 gp_Pnt aLeftPoint = BRep_Tool::Pnt( aLeftVertex );
256 gp_Pnt aRightPoint = BRep_Tool::Pnt( aRightVertex );
257 gp_Vec aLeftToRight( aLeftPoint, aRightPoint);
258 gp_Vec NormalToProfile = aVertical ^ aLeftToRight;
260 gp_Vec aDir = aTangents.Value( i );
261 gp_Vec AxisOfRotation = NormalToProfile ^ aDir;
262 if (AxisOfRotation.Magnitude() <= gp::Resolution()) {
263 if ( aVertical * aLeftToRight < 0. ) {
264 gp_Ax1 theVertical(aPointOnGuide, gp::DZ() );
265 Rotation.SetRotation(theVertical, M_PI);
268 gp_Ax1 theAxis(aPointOnGuide, AxisOfRotation);
269 Standard_Real theAngle = NormalToProfile.AngleWithRef(aDir, AxisOfRotation);
270 Rotation.SetRotation(theAxis, theAngle);
273 aTransformedProfile = TopoDS::Wire(BRepBuilderAPI_Transform( aTransformedProfile, Rotation, Standard_True) );
275 // Get the first and the last points of the transformed profile
276 TopoDS_Vertex V1, V2;
277 TopExp::Vertices( aTransformedProfile, V1, V2 );
280 anArrayOfFPnt->SetValue( i, BRep_Tool::Pnt( V1 ) );
281 anArrayOfLPnt->SetValue( i, BRep_Tool::Pnt( V2 ) );
283 anArrOfProfiles->SetValue( i, aTransformedProfile );
286 // Create presentation
287 HYDROData_Stream::PrsDefinition aPrs;
288 Handle(TopTools_HArray1OfShape) anArrOf2DProfiles; // we don't need 2D profiles for channel/digue presentation
289 bool aRes = HYDROData_Stream::CreatePresentations( anArrayOfFPnt, anArrayOfLPnt,
290 anArrOfProfiles, anArrOf2DProfiles, aPrs );
292 thePrs.myPrs3D = aPrs.myPrs3D;
293 thePrs.myPrs2D = TopoDS::Face( aPrs.myPrs2D );
294 BRepBuilderAPI_MakeWire aMakeWire( aPrs.myLeftBank ) ;
295 thePrs.myLeftBank = aMakeWire.Wire();
296 aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myRightBank );
297 thePrs.myRightBank = aMakeWire.Wire();
298 aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myInlet );
299 thePrs.myInlet = aMakeWire.Wire();
300 aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myOutlet );
301 thePrs.myOutlet = aMakeWire.Wire();
307 void HYDROData_Channel::Update()
309 HYDROData_ArtificialObject::Update();
311 Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
312 Handle(HYDROData_Profile) aProfile = GetProfile();
314 PrsDefinition aResultPrs;
315 if ( !CreatePresentations( aGuideLine, aProfile, aResultPrs ) )
318 SetShape3D( aResultPrs.myPrs3D );
319 SetTopShape( aResultPrs.myPrs2D );
321 // Create groups for channel
322 TopTools_SequenceOfShape aLeftBankEdges;
323 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myLeftBank, TopAbs_EDGE, aLeftBankEdges );
325 TopTools_SequenceOfShape aRightBankEdges;
326 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myRightBank, TopAbs_EDGE, aRightBankEdges );
328 TopTools_SequenceOfShape anInletEdges;
329 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myInlet, TopAbs_EDGE, anInletEdges );
331 TopTools_SequenceOfShape anOutletEdges;
332 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myOutlet, TopAbs_EDGE, anOutletEdges );
334 QString aLeftGroupName = GetName() + "_Left_Bank";
336 Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
337 aLeftGroup->SetName( aLeftGroupName );
338 aLeftGroup->SetShapes( aLeftBankEdges );
340 QString aRightGroupName = GetName() + "_Right_Bank";
342 Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
343 aRightGroup->SetName( aRightGroupName );
344 aRightGroup->SetShapes( aRightBankEdges );
346 QString anInGroupName = GetName() + "_Inlet";
348 Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
349 anInGroup->SetName( anInGroupName );
350 anInGroup->SetShapes( anInletEdges );
352 QString anOutGroupName = GetName() + "_Outlet";
354 Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
355 anOutGroup->SetName( anOutGroupName );
356 anOutGroup->SetShapes( anOutletEdges );
359 bool HYDROData_Channel::IsHas2dPrs() const
364 QColor HYDROData_Channel::DefaultFillingColor()
366 return QColor( Qt::blue );
369 QColor HYDROData_Channel::DefaultBorderColor()
371 return QColor( Qt::transparent );
374 QColor HYDROData_Channel::getDefaultFillingColor() const
376 return DefaultFillingColor();
379 QColor HYDROData_Channel::getDefaultBorderColor() const
381 return DefaultBorderColor();
384 bool HYDROData_Channel::SetGuideLine( const Handle(HYDROData_Polyline3D)& theGuideLine )
386 Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
388 if ( theGuideLine.IsNull() )
391 return !aPrevGuideLine.IsNull();
394 if ( IsEqual( aPrevGuideLine, theGuideLine ) )
397 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theGuideLine->GetTopShape() );
398 if ( aHydraulicWire.IsNull() )
399 return false; // The polyline must be a single wire
401 SetReferenceObject( theGuideLine, DataTag_GuideLine );
403 // Indicate model of the need to update the chanel presentation
409 Handle(HYDROData_Polyline3D) HYDROData_Channel::GetGuideLine() const
411 return Handle(HYDROData_Polyline3D)::DownCast(
412 GetReferenceObject( DataTag_GuideLine ) );
415 void HYDROData_Channel::RemoveGuideLine()
417 Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
418 if ( aPrevGuideLine.IsNull() )
421 ClearReferenceObjects( DataTag_GuideLine );
423 // Indicate model of the need to update the chanel presentation
427 bool HYDROData_Channel::SetProfile( const Handle(HYDROData_Profile)& theProfile )
429 Handle(HYDROData_Profile) aPrevProfile = GetProfile();
431 if ( theProfile.IsNull() )
434 return !aPrevProfile.IsNull();
437 if ( IsEqual( aPrevProfile, theProfile ) )
440 SetReferenceObject( theProfile, DataTag_Profile );
442 // Indicate model of the need to update the chanel presentation
448 Handle(HYDROData_Profile) HYDROData_Channel::GetProfile() const
450 return Handle(HYDROData_Profile)::DownCast(
451 GetReferenceObject( DataTag_Profile ) );
454 void HYDROData_Channel::RemoveProfile()
456 Handle(HYDROData_Profile) aPrevProfile = GetProfile();
457 if ( aPrevProfile.IsNull() )
460 ClearReferenceObjects( DataTag_Profile );
462 // Indicate model of the need to update the chanel presentation
466 ObjectKind HYDROData_Channel::getAltitudeObjectType() const
468 DEBTRACE("HYDROData_Channel::getAltitudeObjectType");
469 return KIND_CHANNEL_ALTITUDE;