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 <BRepBuilderAPI_MakeEdge.hxx>
23 #include <TopoDS_Edge.hxx>
24 #include <TopoDS_Face.hxx>
26 #include <BRep_Tool.hxx>
27 #include <Geom_Plane.hxx>
28 #include <Geom_CylindricalSurface.hxx>
29 #include <Geom_RectangularTrimmedSurface.hxx>
32 #include <gp_Circ.hxx>
33 #include <gp_Elips.hxx>
34 #include <Bnd_Box.hxx>
35 #include <BRepBndLib.hxx>
37 static GeomEdgePtr createLine(const gp_Pnt& theStart, const gp_Pnt& theEnd)
40 if (!theStart.IsEqual(theEnd, Precision::Confusion()) &&
41 Abs(theStart.SquareDistance(theEnd)) < 1.e+100) {
42 BRepBuilderAPI_MakeEdge anEdgeBuilder(theStart, theEnd);
43 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
44 aRes = GeomEdgePtr(new GeomAPI_Edge);
45 aRes->setImpl(new TopoDS_Shape(anEdge));
50 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
51 std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
53 const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
54 const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
55 return createLine(aStart, anEnd);
57 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
58 double theDX, double theDY, double theDZ)
60 const gp_Pnt& aStart = gp_Pnt(0, 0, 0);
61 const gp_Pnt& anEnd = gp_Pnt(theDX, theDY, theDZ);
62 return createLine(aStart, anEnd);
65 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
66 const std::shared_ptr<GeomAPI_Lin> theLin)
70 const gp_Lin& aLin = theLin->impl<gp_Lin>();
71 BRepBuilderAPI_MakeEdge anEdgeBuilder(aLin);
72 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
73 aRes = GeomEdgePtr(new GeomAPI_Edge);
74 aRes->setImpl(new TopoDS_Shape(anEdge));
79 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
80 std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
82 std::shared_ptr<GeomAPI_Edge> aResult;
83 const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
86 TopoDS_Face aFace = TopoDS::Face(aShape);
90 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
93 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
94 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
95 if (!aTrimmed.IsNull())
96 aSurf = aTrimmed->BasisSurface();
97 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
100 gp_Ax1 anAxis = aCyl->Axis();
101 // compute the start and the end points of the resulting edge by the bounding box of the face
102 // (vertices projected to the axis) plus 10%
104 BRepBndLib::Add(aFace, aFaceBnd);
105 gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
107 double aParamMin = 0, aParamMax = 0;
108 for(int aX = 0; aX < 2; aX++) {
109 for(int aY = 0; aY < 2; aY++) {
110 for(int aZ = 0; aZ < 2; aZ++) {
111 gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
112 aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
113 gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
114 double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
117 aParamMin = aProjParam;
118 aParamMax = aProjParam;
120 if (aParamMin > aProjParam)
121 aParamMin = aProjParam;
122 else if (aParamMax < aProjParam)
123 aParamMax = aProjParam;
129 double aDelta = aParamMax - aParamMin;
130 if (aDelta < 1.e-4) aDelta = 1.e-4;
131 aParamMin -= aDelta * 0.1;
132 aParamMax += aDelta * 0.1;
134 gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
135 aStart.Transform(aLoc.Transformation());
136 gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
137 anEnd.Transform(aLoc.Transformation());
139 gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
140 // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
141 gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
142 anEnd.Transform(aLoc.Transformation());
145 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
146 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
147 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
148 // an axis is an infinite object
149 anEdge.Infinite(Standard_True);
150 aRes->setImpl(new TopoDS_Shape(anEdge));
154 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
155 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
158 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
159 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
161 gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
163 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
164 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
165 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
166 aRes->setImpl(new TopoDS_Shape(anEdge));
170 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
171 std::shared_ptr<GeomAPI_Circ> theCircle)
174 if (theCircle.get()) {
175 const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
176 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
177 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
178 aRes = GeomEdgePtr(new GeomAPI_Edge);
179 aRes->setImpl(new TopoDS_Shape(anEdge));
184 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
185 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
186 std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
188 std::shared_ptr<GeomAPI_Edge> aRes;
190 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
191 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
193 /// OCCT creates an edge on a circle with empty radius, but visualization
194 /// is not able to process it
195 if (theCenter->isEqual(theStartPoint) || theCenter->isEqual(theEndPoint))
198 double aRadius = theCenter->distance(theStartPoint);
199 gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
201 const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
202 const gp_Pnt& anEndInter = theEndPoint->impl<gp_Pnt>();
204 // project end point to a circle
205 gp_XYZ aEndDir = anEndInter.XYZ() - aCenter.XYZ();
206 gp_Pnt anEnd(aCenter.XYZ() + aEndDir.Normalized() * aRadius);
208 BRepBuilderAPI_MakeEdge anEdgeBuilder;
209 anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
211 anEdgeBuilder.Build();
213 if (anEdgeBuilder.IsDone()) {
214 aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
215 aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
220 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipse(
221 const std::shared_ptr<GeomAPI_Pnt>& theCenter,
222 const std::shared_ptr<GeomAPI_Dir>& theNormal,
223 const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
224 const double theMajorRadius,
225 const double theMinorRadius)
227 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
228 const gp_Dir& aNormal = theNormal->impl<gp_Dir>();
229 const gp_Dir& aMajorAxis = theMajorAxis->impl<gp_Dir>();
231 gp_Elips anEllipse(gp_Ax2(aCenter, aNormal, aMajorAxis), theMajorRadius, theMinorRadius);
233 BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse);
234 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
235 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
236 aRes->setImpl(new TopoDS_Shape(anEdge));