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_Ellipse.h>
26 #include <BRepBuilderAPI_MakeEdge.hxx>
27 #include <TopoDS_Edge.hxx>
28 #include <TopoDS_Face.hxx>
30 #include <BRep_Tool.hxx>
31 #include <Geom_Plane.hxx>
32 #include <Geom_CylindricalSurface.hxx>
33 #include <Geom_RectangularTrimmedSurface.hxx>
36 #include <gp_Circ.hxx>
37 #include <gp_Elips.hxx>
38 #include <Bnd_Box.hxx>
39 #include <BRepBndLib.hxx>
41 static GeomEdgePtr createLine(const gp_Pnt& theStart, const gp_Pnt& theEnd)
44 if (!theStart.IsEqual(theEnd, Precision::Confusion()) &&
45 Abs(theStart.SquareDistance(theEnd)) < 1.e+100) {
46 BRepBuilderAPI_MakeEdge anEdgeBuilder(theStart, theEnd);
47 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
48 aRes = GeomEdgePtr(new GeomAPI_Edge);
49 aRes->setImpl(new TopoDS_Shape(anEdge));
54 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
55 std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
57 const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
58 const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
59 return createLine(aStart, anEnd);
61 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
62 double theDX, double theDY, double theDZ)
64 const gp_Pnt& aStart = gp_Pnt(0, 0, 0);
65 const gp_Pnt& anEnd = gp_Pnt(theDX, theDY, theDZ);
66 return createLine(aStart, anEnd);
69 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
70 const std::shared_ptr<GeomAPI_Lin> theLin)
74 const gp_Lin& aLin = theLin->impl<gp_Lin>();
75 BRepBuilderAPI_MakeEdge anEdgeBuilder(aLin);
76 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
77 aRes = GeomEdgePtr(new GeomAPI_Edge);
78 aRes->setImpl(new TopoDS_Shape(anEdge));
83 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
84 std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
86 std::shared_ptr<GeomAPI_Edge> aResult;
87 const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
90 TopoDS_Face aFace = TopoDS::Face(aShape);
94 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
97 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
98 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
99 if (!aTrimmed.IsNull())
100 aSurf = aTrimmed->BasisSurface();
101 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
104 gp_Ax1 anAxis = aCyl->Axis();
105 // compute the start and the end points of the resulting edge by the bounding box of the face
106 // (vertices projected to the axis) plus 10%
108 BRepBndLib::Add(aFace, aFaceBnd);
109 gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
111 double aParamMin = 0, aParamMax = 0;
112 for(int aX = 0; aX < 2; aX++) {
113 for(int aY = 0; aY < 2; aY++) {
114 for(int aZ = 0; aZ < 2; aZ++) {
115 gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
116 aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
117 gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
118 double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
121 aParamMin = aProjParam;
122 aParamMax = aProjParam;
124 if (aParamMin > aProjParam)
125 aParamMin = aProjParam;
126 else if (aParamMax < aProjParam)
127 aParamMax = aProjParam;
133 double aDelta = aParamMax - aParamMin;
134 if (aDelta < 1.e-4) aDelta = 1.e-4;
135 aParamMin -= aDelta * 0.1;
136 aParamMax += aDelta * 0.1;
138 gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
139 aStart.Transform(aLoc.Transformation());
140 gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
141 anEnd.Transform(aLoc.Transformation());
143 gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
144 // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
145 gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
146 anEnd.Transform(aLoc.Transformation());
149 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
150 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
151 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
152 // an axis is an infinite object
153 anEdge.Infinite(Standard_True);
154 aRes->setImpl(new TopoDS_Shape(anEdge));
158 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
159 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
162 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
163 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
165 gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
167 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
168 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
169 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
170 aRes->setImpl(new TopoDS_Shape(anEdge));
174 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
175 std::shared_ptr<GeomAPI_Circ> theCircle)
178 if (theCircle.get()) {
179 const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
180 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
181 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
182 aRes = GeomEdgePtr(new GeomAPI_Edge);
183 aRes->setImpl(new TopoDS_Shape(anEdge));
188 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
189 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
190 std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
192 std::shared_ptr<GeomAPI_Edge> aRes;
194 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
195 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
197 /// OCCT creates an edge on a circle with empty radius, but visualization
198 /// is not able to process it
199 if (theCenter->isEqual(theStartPoint) || theCenter->isEqual(theEndPoint))
202 double aRadius = theCenter->distance(theStartPoint);
203 gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
205 const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
206 const gp_Pnt& anEndInter = theEndPoint->impl<gp_Pnt>();
208 // project end point to a circle
209 gp_XYZ aEndDir = anEndInter.XYZ() - aCenter.XYZ();
210 gp_Pnt anEnd(aCenter.XYZ() + aEndDir.Normalized() * aRadius);
212 BRepBuilderAPI_MakeEdge anEdgeBuilder;
213 anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
215 anEdgeBuilder.Build();
217 if (anEdgeBuilder.IsDone()) {
218 aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
219 aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
224 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipse(
225 const std::shared_ptr<GeomAPI_Pnt>& theCenter,
226 const std::shared_ptr<GeomAPI_Dir>& theNormal,
227 const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
228 const double theMajorRadius,
229 const double theMinorRadius)
231 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
232 const gp_Dir& aNormal = theNormal->impl<gp_Dir>();
233 const gp_Dir& aMajorAxis = theMajorAxis->impl<gp_Dir>();
235 gp_Elips anEllipse(gp_Ax2(aCenter, aNormal, aMajorAxis), theMajorRadius, theMinorRadius);
237 BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse);
238 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
239 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
240 aRes->setImpl(new TopoDS_Shape(anEdge));
244 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipticArc(
245 const std::shared_ptr<GeomAPI_Pnt>& theCenter,
246 const std::shared_ptr<GeomAPI_Dir>& theNormal,
247 const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
248 const double theMajorRadius,
249 const double theMinorRadius,
250 const std::shared_ptr<GeomAPI_Pnt>& theStart,
251 const std::shared_ptr<GeomAPI_Pnt>& theEnd)
253 std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(theCenter, theNormal, theMajorAxis));
254 GeomAPI_Ellipse anEllipse(anAx2, theMajorRadius, theMinorRadius);
256 GeomPointPtr aStartPnt = anEllipse.project(theStart);
257 GeomPointPtr aEndPnt = anEllipse.project(theEnd);
259 double aStartParam, aEndParam;
260 anEllipse.parameter(aStartPnt, Precision::Confusion(), aStartParam);
261 anEllipse.parameter(aEndPnt, Precision::Confusion(), aEndParam);
263 BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse.impl<gp_Elips>(), aStartParam, aEndParam);
264 GeomEdgePtr aRes(new GeomAPI_Edge);
265 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
266 aRes->setImpl(new TopoDS_Shape(anEdge));