1 // File: HYDROData_Pipes.cxx
2 // Created: 25.12.13 16:39:40
4 // Copyright: Open CASCADE 2013
6 #include <HYDROData_Pipes.h>
9 #include <TopoDS_Edge.hxx>
10 #include <TopoDS_Face.hxx>
11 #include <TopoDS_Iterator.hxx>
13 #include <BRepOffsetAPI_NormalProjection.hxx>
14 #include <BRepLib_MakeVertex.hxx>
15 #include <BRepLib_MakeEdge.hxx>
16 #include <BRepLib_MakeFace.hxx>
17 #include <BRepLib_MakeWire.hxx>
18 #include <BRepExtrema_ExtCC.hxx>
19 #include <BRepBuilderAPI_Transform.hxx>
20 #include <BRepOffsetAPI_MakePipeShell.hxx>
21 #include <BRep_Tool.hxx>
22 #include <Geom_Curve.hxx>
24 HYDROData_Canal3dAnd2d::HYDROData_Canal3dAnd2d(const TopoDS_Wire& Profile,
25 const TopoDS_Wire& Guideline)
26 : myProfile(Profile), myGuideline(Guideline)
28 TopExp::Vertices(myProfile, myLeftVertex, myRightVertex);
30 ProjectWireOntoXOY(myGuideline, myProjectedGuideline);
36 Standard_Boolean HYDROData_Canal3dAnd2d::ProjectWireOntoXOY(const TopoDS_Wire& aWire,
37 TopoDS_Wire& ProjectedWire)
39 gp_Pln XOY; //default plane
40 TopoDS_Face theXOYface = BRepLib_MakeFace(XOY);
42 BRepOffsetAPI_NormalProjection OrtProj(theXOYface);
44 //OrtProj.SetParams(...); ???
46 TopTools_ListOfShape Wires;
47 OrtProj.BuildWire(Wires);
49 if (Wires.Extent() != 1)
50 return Standard_False;
52 ProjectedWire = TopoDS::Wire(Wires.First());
56 TopoDS_Vertex HYDROData_Canal3dAnd2d::ProjectVertexOntoXOY(const TopoDS_Vertex& aVertex)
58 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
59 gp_Pnt aProjPoint(aPoint.X(), aPoint.Y(), 0.);
60 TopoDS_Vertex aProjVertex = BRepLib_MakeVertex(aProjPoint);
64 void HYDROData_Canal3dAnd2d::Make2dProfile()
66 TopoDS_Vertex ProjectedLeftVertex = ProjectVertexOntoXOY(myLeftVertex);
67 TopoDS_Vertex ProjectedRightVertex = ProjectVertexOntoXOY(myRightVertex);
68 TopoDS_Edge anEdge = BRepLib_MakeEdge(ProjectedLeftVertex, ProjectedRightVertex);
69 myProjectedProfile = BRepLib_MakeWire(anEdge);
72 void HYDROData_Canal3dAnd2d::SetMiddlePoint2d()
75 TopExp::Vertices(myProjectedProfile, V1, V2);
76 gp_Pnt Pnt1 = BRep_Tool::Pnt(V1);
77 gp_Pnt Pnt2 = BRep_Tool::Pnt(V2);
78 myMiddlePoint2d.SetXYZ(0.5*(Pnt1.XYZ() + Pnt2.XYZ()));
81 void HYDROData_Canal3dAnd2d::SetMiddlePoint3d()
83 gp_Lin MidLin(myMiddlePoint2d, gp::DZ());
84 TopoDS_Edge MidEdge = BRepLib_MakeEdge(MidLin);
85 TopoDS_Iterator itw(myProfile);
86 for (; itw.More(); itw.Next())
88 const TopoDS_Edge& anEdge = TopoDS::Edge(itw.Value());
89 BRepExtrema_ExtCC ExtremaEE(MidEdge, anEdge);
90 if (ExtremaEE.IsDone() && ExtremaEE.NbExt() != 0)
92 for (Standard_Integer i = 1; i <= ExtremaEE.NbExt(); i++)
94 if (ExtremaEE.SquareDistance(i) <= Precision::Confusion())
96 myMiddlePoint3d = ExtremaEE.PointOnE1(i);
104 TopoDS_Wire HYDROData_Canal3dAnd2d::SetTransformedProfile(const TopoDS_Wire& aProfile,
105 const TopoDS_Wire& aGuideline,
106 const gp_Pnt& aMiddlePoint)
108 TopoDS_Wire aTransformedProfile;
110 TopoDS_Iterator itw(myProjectedGuideline);
111 TopoDS_Edge FirstEdge = TopoDS::Edge(itw.Value());
112 Standard_Real fpar, lpar;
113 Handle(Geom_Curve) FirstCurve = BRep_Tool::Curve(FirstEdge, fpar, lpar);
116 FirstCurve->D1(fpar, StartPnt, StartVec);
118 TopoDS_Vertex FirstOnGuide, LastOnGuide;
119 TopExp::Vertices(aGuideline, FirstOnGuide, LastOnGuide);
120 gp_Pnt StartPointOnGuide = BRep_Tool::Pnt(FirstOnGuide);
122 gp_Trsf Translation, Rotation;
123 Translation.SetTranslation(aMiddlePoint, StartPointOnGuide);
124 aTransformedProfile =
125 TopoDS::Wire(BRepBuilderAPI_Transform(aProfile, Translation, Standard_True)); //copy
127 gp_Vec Vertical(0.,0.,1.);
128 TopoDS_Vertex LeftVertex, RightVertex;
129 TopExp::Vertices(aTransformedProfile, LeftVertex, RightVertex);
130 gp_Pnt LeftPoint = BRep_Tool::Pnt(LeftVertex);
131 gp_Pnt RightPoint = BRep_Tool::Pnt(RightVertex);
132 gp_Vec LeftToRight(LeftPoint, RightPoint);
133 gp_Vec NormalToProfile = Vertical ^ LeftToRight;
135 gp_Vec AxisOfRotation = NormalToProfile ^ StartVec;
136 if (AxisOfRotation.Magnitude() <= gp::Resolution())
138 if (Vertical * LeftToRight < 0.)
140 gp_Ax1 theVertical(StartPointOnGuide, gp::DZ());
141 Rotation.SetRotation(theVertical, M_PI);
146 gp_Ax1 theAxis(StartPointOnGuide, AxisOfRotation);
147 Standard_Real theAngle = NormalToProfile.AngleWithRef(StartVec, AxisOfRotation);
148 Rotation.SetRotation(theAxis, theAngle);
151 aTransformedProfile =
152 TopoDS::Wire(BRepBuilderAPI_Transform(aTransformedProfile, Rotation, Standard_True)); //copy
154 return aTransformedProfile;
157 Standard_Boolean HYDROData_Canal3dAnd2d::Create3dPresentation()
159 myTransformedProfile3d = SetTransformedProfile(myProfile, myGuideline, myMiddlePoint3d);
161 mySweep3d = new BRepOffsetAPI_MakePipeShell(myGuideline);
162 mySweep3d->SetMode(gp::DZ()); //optional
163 mySweep3d->Add(myTransformedProfile3d);
164 //mySweep3d->SetTransitionMode(BRepBuilderAPI_RightCorner); //optional
166 if (!mySweep3d->IsDone())
167 return Standard_False;
169 myPipe3d = mySweep3d->Shape();
170 return Standard_True;
173 Standard_Boolean HYDROData_Canal3dAnd2d::Create2dPresentation()
175 myTransformedProfile2d = SetTransformedProfile(myProjectedProfile, myProjectedGuideline, myMiddlePoint2d);
177 mySweep2d = new BRepOffsetAPI_MakePipeShell(myProjectedGuideline);
178 mySweep2d->SetMode(gp::DZ()); //optional
179 mySweep2d->Add(myTransformedProfile2d);
180 //mySweep2d->SetTransitionMode(BRepBuilderAPI_RightCorner); //optional
182 if (!mySweep2d->IsDone())
183 return Standard_False;
185 myPipe2d = mySweep2d->Shape();
186 myInlet = TopoDS::Wire(mySweep2d->FirstShape());
187 myOutlet = TopoDS::Wire(mySweep2d->LastShape());
188 TopoDS_Vertex V1, V2, V3, V4;
189 TopExp::Vertices(myTransformedProfile2d, V1, V2);
190 TopExp::Vertices(myInlet, V3, V4);
191 gp_Pnt P1 = BRep_Tool::Pnt(V1);
192 gp_Pnt P3 = BRep_Tool::Pnt(V3);
193 if (P1.IsEqual(P3, Precision::Confusion()))
194 { myLeftVertex2d = V3; myRightVertex2d = V4; }
196 { myLeftVertex2d = V4; myRightVertex2d = V3; }
198 return Standard_True;
201 TopoDS_Wire HYDROData_Canal3dAnd2d::GetBank(const TopoDS_Vertex& aFreeVertex)
205 TopTools_ListOfShape GeneratedShapes;
206 GeneratedShapes = mySweep2d->Generated(aFreeVertex);
208 MW.Add(GeneratedShapes);
214 TopoDS_Shape HYDROData_Canal3dAnd2d::Get3dPresentation()
219 TopoDS_Shape HYDROData_Canal3dAnd2d::Get2dPresentation()
224 TopoDS_Wire HYDROData_Canal3dAnd2d::GetInlet()
229 TopoDS_Wire HYDROData_Canal3dAnd2d::GetOutlet()
234 TopoDS_Wire HYDROData_Canal3dAnd2d::GetLeftBank()
236 return GetBank(myLeftVertex2d);
239 TopoDS_Wire HYDROData_Canal3dAnd2d::GetRightBank()
241 return GetBank(myRightVertex2d);