1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <GeomAlgoAPI_EdgeBuilder.h>
22 #include <GeomAPI_Ax2.h>
23 #include <GeomAPI_Ax3.h>
24 #include <GeomAPI_BSpline2d.h>
25 #include <GeomAPI_Ellipse.h>
26 #include <GeomAPI_Pnt2d.h>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <TopoDS_Face.hxx>
33 #include <BRep_Tool.hxx>
34 #include <Geom2d_BSplineCurve.hxx>
35 #include <Geom_Plane.hxx>
36 #include <Geom_CylindricalSurface.hxx>
37 #include <Geom_RectangularTrimmedSurface.hxx>
38 #include <GeomLib.hxx>
41 #include <gp_Circ.hxx>
42 #include <gp_Elips.hxx>
43 #include <Bnd_Box.hxx>
44 #include <BRepBndLib.hxx>
46 static GeomEdgePtr createLine(const gp_Pnt& theStart, const gp_Pnt& theEnd)
49 if (!theStart.IsEqual(theEnd, Precision::Confusion()) &&
50 Abs(theStart.SquareDistance(theEnd)) < 1.e+100) {
51 BRepBuilderAPI_MakeEdge anEdgeBuilder(theStart, theEnd);
52 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
53 aRes = GeomEdgePtr(new GeomAPI_Edge);
54 aRes->setImpl(new TopoDS_Shape(anEdge));
59 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
60 std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
62 const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
63 const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
64 return createLine(aStart, anEnd);
66 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
67 double theDX, double theDY, double theDZ)
69 const gp_Pnt& aStart = gp_Pnt(0, 0, 0);
70 const gp_Pnt& anEnd = gp_Pnt(theDX, theDY, theDZ);
71 return createLine(aStart, anEnd);
74 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
75 const std::shared_ptr<GeomAPI_Lin> theLin)
79 const gp_Lin& aLin = theLin->impl<gp_Lin>();
80 BRepBuilderAPI_MakeEdge anEdgeBuilder(aLin);
81 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
82 aRes = GeomEdgePtr(new GeomAPI_Edge);
83 aRes->setImpl(new TopoDS_Shape(anEdge));
88 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
89 std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
91 std::shared_ptr<GeomAPI_Edge> aResult;
92 const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
95 TopoDS_Face aFace = TopoDS::Face(aShape);
99 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
102 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
103 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
104 if (!aTrimmed.IsNull())
105 aSurf = aTrimmed->BasisSurface();
106 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
109 gp_Ax1 anAxis = aCyl->Axis();
110 // compute the start and the end points of the resulting edge by the bounding box of the face
111 // (vertices projected to the axis) plus 10%
113 BRepBndLib::Add(aFace, aFaceBnd);
114 gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
116 double aParamMin = 0, aParamMax = 0;
117 for(int aX = 0; aX < 2; aX++) {
118 for(int aY = 0; aY < 2; aY++) {
119 for(int aZ = 0; aZ < 2; aZ++) {
120 gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
121 aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
122 gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
123 double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
126 aParamMin = aProjParam;
127 aParamMax = aProjParam;
129 if (aParamMin > aProjParam)
130 aParamMin = aProjParam;
131 else if (aParamMax < aProjParam)
132 aParamMax = aProjParam;
138 double aDelta = aParamMax - aParamMin;
139 if (aDelta < 1.e-4) aDelta = 1.e-4;
140 aParamMin -= aDelta * 0.1;
141 aParamMax += aDelta * 0.1;
143 gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
144 aStart.Transform(aLoc.Transformation());
145 gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
146 anEnd.Transform(aLoc.Transformation());
148 gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
149 // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
150 gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
151 anEnd.Transform(aLoc.Transformation());
154 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
155 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
156 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
157 // an axis is an infinite object
158 anEdge.Infinite(Standard_True);
159 aRes->setImpl(new TopoDS_Shape(anEdge));
163 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
164 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
167 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
168 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
170 gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
172 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
173 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
174 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
175 aRes->setImpl(new TopoDS_Shape(anEdge));
179 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
180 std::shared_ptr<GeomAPI_Circ> theCircle)
183 if (theCircle.get()) {
184 const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
185 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
186 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
187 aRes = GeomEdgePtr(new GeomAPI_Edge);
188 aRes->setImpl(new TopoDS_Shape(anEdge));
193 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
194 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
195 std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
197 std::shared_ptr<GeomAPI_Edge> aRes;
199 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
200 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
202 /// OCCT creates an edge on a circle with empty radius, but visualization
203 /// is not able to process it
204 if (theCenter->isEqual(theStartPoint) || theCenter->isEqual(theEndPoint))
207 double aRadius = theCenter->distance(theStartPoint);
208 gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
210 const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
211 const gp_Pnt& anEndInter = theEndPoint->impl<gp_Pnt>();
213 // project end point to a circle
214 gp_XYZ aEndDir = anEndInter.XYZ() - aCenter.XYZ();
215 gp_Pnt anEnd(aCenter.XYZ() + aEndDir.Normalized() * aRadius);
217 BRepBuilderAPI_MakeEdge anEdgeBuilder;
218 anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
220 anEdgeBuilder.Build();
222 if (anEdgeBuilder.IsDone()) {
223 aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
224 aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
229 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipse(
230 const std::shared_ptr<GeomAPI_Pnt>& theCenter,
231 const std::shared_ptr<GeomAPI_Dir>& theNormal,
232 const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
233 const double theMajorRadius,
234 const double theMinorRadius)
236 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
237 const gp_Dir& aNormal = theNormal->impl<gp_Dir>();
238 const gp_Dir& aMajorAxis = theMajorAxis->impl<gp_Dir>();
240 gp_Elips anEllipse(gp_Ax2(aCenter, aNormal, aMajorAxis), theMajorRadius, theMinorRadius);
242 BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse);
243 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
244 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
245 aRes->setImpl(new TopoDS_Shape(anEdge));
249 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipticArc(
250 const std::shared_ptr<GeomAPI_Pnt>& theCenter,
251 const std::shared_ptr<GeomAPI_Dir>& theNormal,
252 const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
253 const double theMajorRadius,
254 const double theMinorRadius,
255 const std::shared_ptr<GeomAPI_Pnt>& theStart,
256 const std::shared_ptr<GeomAPI_Pnt>& theEnd)
258 std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(theCenter, theNormal, theMajorAxis));
259 GeomAPI_Ellipse anEllipse(anAx2, theMajorRadius, theMinorRadius);
261 GeomPointPtr aStartPnt = anEllipse.project(theStart);
262 GeomPointPtr aEndPnt = anEllipse.project(theEnd);
264 double aStartParam, aEndParam;
265 anEllipse.parameter(aStartPnt, Precision::Confusion(), aStartParam);
266 anEllipse.parameter(aEndPnt, Precision::Confusion(), aEndParam);
268 BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse.impl<gp_Elips>(), aStartParam, aEndParam);
269 GeomEdgePtr aRes(new GeomAPI_Edge);
270 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
271 aRes->setImpl(new TopoDS_Shape(anEdge));
275 GeomEdgePtr GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(
276 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
277 const std::list<GeomPnt2dPtr>& thePoles,
278 const std::list<double>& theWeights,
279 const std::list<double>& theKnots,
280 const std::list<int>& theMults,
282 const bool thePeriodic)
284 std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve(
285 new GeomAPI_BSpline2d(theDegree, thePoles, theWeights, theKnots, theMults, thePeriodic));
286 return bsplineOnPlane(thePlane, aBSplineCurve);
289 GeomEdgePtr GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(
290 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
291 const std::shared_ptr<GeomAPI_BSpline2d>& theCurve)
293 Handle(Geom_Curve) aCurve3D = GeomLib::To3d(thePlane->impl<gp_Ax3>().Ax2(),
294 theCurve->impl<Handle_Geom2d_BSplineCurve>());
296 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCurve3D);
297 GeomEdgePtr aRes(new GeomAPI_Edge);
298 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
299 aRes->setImpl(new TopoDS_Shape(anEdge));