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"
30 #include "HYDROData_Tool.h"
32 #include <BRepBuilderAPI_MakeWire.hxx>
34 #include <BRepOffsetAPI_MakePipeShell.hxx>
35 #include <BRepOffsetAPI_MakePipe.hxx>
36 #include <BRepCheck_Analyzer.hxx>
38 #include <BRep_Tool.hxx>
40 #include <BRepBuilderAPI_Transform.hxx>
42 #include <BRepLib_MakeEdge.hxx>
43 #include <BRepLib_MakeWire.hxx>
45 #include <GeomAPI_ProjectPointOnCurve.hxx>
50 #include <TopExp_Explorer.hxx>
52 #include <TColgp_HArray1OfPnt.hxx>
53 #include <TColgp_Array1OfDir.hxx>
55 #include <TColStd_Array1OfReal.hxx>
57 #include <TopTools_HArray1OfShape.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_HANDLE(HYDROData_Channel,HYDROData_ArtificialObject)
75 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Channel,HYDROData_ArtificialObject)
78 HYDROData_Channel::HYDROData_Channel()
79 : HYDROData_ArtificialObject( Geom_3d )
83 HYDROData_Channel::~HYDROData_Channel()
87 QStringList HYDROData_Channel::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
89 QStringList aResList = dumpObjectCreation( theTreatedObjects );
90 QString aName = GetObjPyName();
92 Handle(HYDROData_Polyline3D) aRefGideLine = GetGuideLine();
93 setPythonReferenceObject( theTreatedObjects, aResList, aRefGideLine, "SetGuideLine" );
95 Handle(HYDROData_Profile) aRefProfile = GetProfile();
96 setPythonReferenceObject( theTreatedObjects, aResList, aRefProfile, "SetProfile" );
98 aResList << QString( "" );
99 aResList << QString( "%1.Update();" ).arg( aName );
100 aResList << QString( "" );
105 HYDROData_SequenceOfObjects HYDROData_Channel::GetAllReferenceObjects() const
107 HYDROData_SequenceOfObjects aResSeq = HYDROData_ArtificialObject::GetAllReferenceObjects();
109 Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
110 if ( !aGuideLine.IsNull() )
111 aResSeq.Append( aGuideLine );
113 Handle(HYDROData_Profile) aProfile = GetProfile();
114 if ( !aProfile.IsNull() )
115 aResSeq.Append( aProfile );
120 TopoDS_Shape HYDROData_Channel::GetTopShape() const
122 return getTopShape();
125 TopoDS_Shape HYDROData_Channel::GetShape3D() const
130 bool HYDROData_Channel::CreatePresentations( const Handle(HYDROData_Polyline3D)& theGuideLine,
131 const Handle(HYDROData_Profile)& theProfile,
132 PrsDefinition& thePrs )
134 // Check input parameters
135 if ( theGuideLine.IsNull() || theProfile.IsNull() ) {
139 TopoDS_Wire aPathWire = TopoDS::Wire( theGuideLine->GetShape3D() );
140 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetShape3D() );
141 if ( aPathWire.IsNull() || aProfileWire.IsNull() ) {
146 std::string brepName = "guideline_";
147 brepName += theGuideLine->GetName().toStdString();
149 BRepTools::Write( aPathWire, brepName.c_str() );
150 brepName = "profile_";
151 brepName += theGuideLine->GetName().toStdString();
153 BRepTools::Write( aProfileWire, brepName.c_str() );
157 Handle(HYDROData_PolylineXY) aPolylineXY = theGuideLine->GetPolylineXY();
158 if ( aPolylineXY.IsNull() ) {
163 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
164 HYDROData_IPolyline::PointsList aPolylinePoints = aPolylineXY->GetPoints( 0 );
165 int aNbPoints = aPolylinePoints.Length();
168 HYDROData_Polyline3D::Polyline3DPoints aPolylinePoints3D = theGuideLine->GetPoints();
169 int aNbPoints = aPolylinePoints3D.Length();
170 if ( aNbPoints < 2 ) {
174 // Get tangent in each point of the guide line ( 2D )
175 TColgp_Array1OfDir aTangents( 1, aNbPoints );
177 HYDROData_IPolyline::SectionType aSectionType = aPolylineXY->GetSectionType( 0 );
179 if ( aSectionType == HYDROData_IPolyline::SECTION_POLYLINE ) {
180 for ( int i = 1; i <= aNbPoints; ++i ) {
181 gp_XYZ aPnt = aPolylinePoints3D.Value( i );
185 aPrevPnt = aPolylinePoints3D.Value( i - 1 );
190 if ( i < aNbPoints ) {
191 gp_XYZ aNextPnt = aPolylinePoints3D.Value( i + 1 );
194 gp_Vec anEdgeVec( aPnt, aNextPnt );
199 gp_Vec aPrevVec( aPrevPnt, aPnt );
200 aDir = aPrevVec.Normalized() + anEdgeVec.Normalized();
203 aDir = gp_Vec( aPrevPnt, aPnt );
206 aTangents.SetValue( i, aDir );
209 // Get curve from the first edge ( 2D )
210 TopTools_SequenceOfShape anEdges;
211 HYDROData_ShapesTool::ExploreShapeToShapes( aPolylineXY->GetShape(), TopAbs_EDGE, anEdges );
212 Standard_Real aStart, anEnd;
214 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( TopoDS::Edge( anEdges.First() ), aStart, anEnd );
215 GeomAPI_ProjectPointOnCurve aProject;
218 for ( int i = 1; i <= aNbPoints; ++i ) {
219 gp_XYZ aPointToTest = aPolylinePoints3D.Value( i );
220 aPointToTest.SetZ( 0. );
222 aProject.Init( aPointToTest, aCurve );
223 Quantity_Parameter aParam = aProject.LowerDistanceParameter();
226 aCurve->D1( aParam, aPnt, aDir);
228 aTangents.SetValue( i, aDir );
232 // Get the profile middle point ( 3D )
233 gp_Pnt aMiddlePoint( theProfile->GetMiddlePoint( true ) );
235 // Translate the profile to each point on the guide line ( 3D )
236 Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, aNbPoints );
237 Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, aNbPoints );
238 Handle(TopTools_HArray1OfShape) anArrOfProfiles = new TopTools_HArray1OfShape( 1, aNbPoints );
240 for ( int i = 1; i <= aNbPoints; ++i ) {
241 // Get point on the guide line
242 gp_Pnt aPointOnGuide( aPolylinePoints3D.Value( i ) );
244 // Define translation and rotation:
245 gp_Trsf Translation, Rotation;
248 Translation.SetTranslation( aMiddlePoint, aPointOnGuide );
249 TopoDS_Wire aTransformedProfile =
250 TopoDS::Wire( BRepBuilderAPI_Transform( aProfileWire, Translation, Standard_True ) );
253 gp_Vec aVertical( 0., 0., 1. );
254 TopoDS_Vertex aLeftVertex, aRightVertex;
255 TopExp::Vertices( aTransformedProfile, aLeftVertex, aRightVertex );
256 gp_Pnt aLeftPoint = BRep_Tool::Pnt( aLeftVertex );
257 gp_Pnt aRightPoint = BRep_Tool::Pnt( aRightVertex );
258 gp_Vec aLeftToRight( aLeftPoint, aRightPoint);
259 gp_Vec NormalToProfile = aVertical ^ aLeftToRight;
261 gp_Vec aDir = aTangents.Value( i );
262 gp_Vec AxisOfRotation = NormalToProfile ^ aDir;
263 if (AxisOfRotation.Magnitude() <= gp::Resolution()) {
264 if ( aVertical * aLeftToRight < 0. ) {
265 gp_Ax1 theVertical(aPointOnGuide, gp::DZ() );
266 Rotation.SetRotation(theVertical, M_PI);
269 gp_Ax1 theAxis(aPointOnGuide, AxisOfRotation);
270 Standard_Real theAngle = NormalToProfile.AngleWithRef(aDir, AxisOfRotation);
271 Rotation.SetRotation(theAxis, theAngle);
274 aTransformedProfile = TopoDS::Wire(BRepBuilderAPI_Transform( aTransformedProfile, Rotation, Standard_True) );
276 // Get the first and the last points of the transformed profile
277 TopoDS_Vertex V1, V2;
278 TopExp::Vertices( aTransformedProfile, V1, V2 );
281 anArrayOfFPnt->SetValue( i, BRep_Tool::Pnt( V1 ) );
282 anArrayOfLPnt->SetValue( i, BRep_Tool::Pnt( V2 ) );
284 anArrOfProfiles->SetValue( i, aTransformedProfile );
287 // Create presentation
288 HYDROData_Stream::PrsDefinition aPrs;
289 Handle(TopTools_HArray1OfShape) anArrOf2DProfiles; // we don't need 2D profiles for channel/digue presentation
290 bool aRes = HYDROData_Stream::CreatePresentations( anArrayOfFPnt, anArrayOfLPnt,
291 anArrOfProfiles, anArrOf2DProfiles, aPrs );
293 thePrs.myPrs3D = aPrs.myPrs3D;
294 thePrs.myPrs2D = TopoDS::Face( aPrs.myPrs2D );
295 BRepBuilderAPI_MakeWire aMakeWire( aPrs.myLeftBank ) ;
296 thePrs.myLeftBank = aMakeWire.Wire();
297 aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myRightBank );
298 thePrs.myRightBank = aMakeWire.Wire();
299 aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myInlet );
300 thePrs.myInlet = aMakeWire.Wire();
301 aMakeWire = BRepBuilderAPI_MakeWire( aPrs.myOutlet );
302 thePrs.myOutlet = aMakeWire.Wire();
308 void HYDROData_Channel::Update()
310 HYDROData_ArtificialObject::Update();
312 Handle(HYDROData_Polyline3D) aGuideLine = GetGuideLine();
313 Handle(HYDROData_Profile) aProfile = GetProfile();
315 PrsDefinition aResultPrs;
316 if ( !CreatePresentations( aGuideLine, aProfile, aResultPrs ) )
319 SetShape3D( aResultPrs.myPrs3D );
320 SetTopShape( aResultPrs.myPrs2D );
322 // Create groups for channel
323 TopTools_SequenceOfShape aLeftBankEdges;
324 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myLeftBank, TopAbs_EDGE, aLeftBankEdges );
326 TopTools_SequenceOfShape aRightBankEdges;
327 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myRightBank, TopAbs_EDGE, aRightBankEdges );
329 TopTools_SequenceOfShape anInletEdges;
330 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myInlet, TopAbs_EDGE, anInletEdges );
332 TopTools_SequenceOfShape anOutletEdges;
333 HYDROData_ShapesTool::ExploreShapeToShapes( aResultPrs.myOutlet, TopAbs_EDGE, anOutletEdges );
335 QString aLeftGroupName = GetName() + "_Left_Bank";
337 Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
338 aLeftGroup->SetName( aLeftGroupName );
339 aLeftGroup->SetShapes( aLeftBankEdges );
341 QString aRightGroupName = GetName() + "_Right_Bank";
343 Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
344 aRightGroup->SetName( aRightGroupName );
345 aRightGroup->SetShapes( aRightBankEdges );
347 QString anInGroupName = GetName() + "_Inlet";
349 Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
350 anInGroup->SetName( anInGroupName );
351 anInGroup->SetShapes( anInletEdges );
353 QString anOutGroupName = GetName() + "_Outlet";
355 Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
356 anOutGroup->SetName( anOutGroupName );
357 anOutGroup->SetShapes( anOutletEdges );
360 bool HYDROData_Channel::IsHas2dPrs() const
365 QColor HYDROData_Channel::DefaultFillingColor()
367 return QColor( Qt::blue );
370 QColor HYDROData_Channel::DefaultBorderColor()
372 return QColor( Qt::transparent );
375 QColor HYDROData_Channel::getDefaultFillingColor() const
377 return DefaultFillingColor();
380 QColor HYDROData_Channel::getDefaultBorderColor() const
382 return DefaultBorderColor();
385 bool HYDROData_Channel::SetGuideLine( const Handle(HYDROData_Polyline3D)& theGuideLine )
387 Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
389 if ( theGuideLine.IsNull() )
392 return !aPrevGuideLine.IsNull();
395 if ( IsEqual( aPrevGuideLine, theGuideLine ) )
398 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theGuideLine->GetTopShape() );
399 if ( aHydraulicWire.IsNull() )
400 return false; // The polyline must be a single wire
402 SetReferenceObject( theGuideLine, DataTag_GuideLine );
404 // Indicate model of the need to update the chanel presentation
410 Handle(HYDROData_Polyline3D) HYDROData_Channel::GetGuideLine() const
412 return Handle(HYDROData_Polyline3D)::DownCast(
413 GetReferenceObject( DataTag_GuideLine ) );
416 void HYDROData_Channel::RemoveGuideLine()
418 Handle(HYDROData_Polyline3D) aPrevGuideLine = GetGuideLine();
419 if ( aPrevGuideLine.IsNull() )
422 ClearReferenceObjects( DataTag_GuideLine );
424 // Indicate model of the need to update the chanel presentation
428 bool HYDROData_Channel::SetProfile( const Handle(HYDROData_Profile)& theProfile )
430 Handle(HYDROData_Profile) aPrevProfile = GetProfile();
432 if ( theProfile.IsNull() )
435 return !aPrevProfile.IsNull();
438 if ( IsEqual( aPrevProfile, theProfile ) )
441 SetReferenceObject( theProfile, DataTag_Profile );
443 // Indicate model of the need to update the chanel presentation
449 Handle(HYDROData_Profile) HYDROData_Channel::GetProfile() const
451 return Handle(HYDROData_Profile)::DownCast(
452 GetReferenceObject( DataTag_Profile ) );
455 void HYDROData_Channel::RemoveProfile()
457 Handle(HYDROData_Profile) aPrevProfile = GetProfile();
458 if ( aPrevProfile.IsNull() )
461 ClearReferenceObjects( DataTag_Profile );
463 // Indicate model of the need to update the chanel presentation
467 ObjectKind HYDROData_Channel::getAltitudeObjectType() const
469 DEBTRACE("HYDROData_Channel::getAltitudeObjectType");
470 return KIND_CHANNEL_ALTITUDE;
471 //return KIND_STREAM_ALTITUDE;
474 TopoDS_Shape HYDROData_Channel::GetLeftShape() const
476 HYDROData_SequenceOfObjects aGroups = GetGroups();
477 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 1);
480 TopoDS_Shape HYDROData_Channel::GetRightShape() const
482 HYDROData_SequenceOfObjects aGroups = GetGroups();
483 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 2);