1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <GeomAlgoAPI_EdgeBuilder.h>
23 #include <BRepBuilderAPI_MakeEdge.hxx>
24 #include <TopoDS_Edge.hxx>
25 #include <TopoDS_Face.hxx>
27 #include <BRep_Tool.hxx>
28 #include <Geom_Plane.hxx>
29 #include <Geom_CylindricalSurface.hxx>
30 #include <Geom_RectangularTrimmedSurface.hxx>
33 #include <gp_Circ.hxx>
34 #include <gp_Elips.hxx>
35 #include <Bnd_Box.hxx>
36 #include <BRepBndLib.hxx>
38 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
39 std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
41 const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
42 const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
44 if (aStart.IsEqual(anEnd, Precision::Confusion()))
45 return std::shared_ptr<GeomAPI_Edge>();
46 if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
47 return std::shared_ptr<GeomAPI_Edge>();
48 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
49 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
50 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
51 aRes->setImpl(new TopoDS_Shape(anEdge));
54 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
55 double theDX, double theDY, double theDZ)
58 const gp_Pnt& aStart = gp_Pnt(0, 0, 0);
59 const gp_Pnt& anEnd = gp_Pnt(theDX, theDY, theDZ);
61 if (aStart.IsEqual(anEnd, Precision::Confusion()))
62 return std::shared_ptr<GeomAPI_Edge>();
63 if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
64 return std::shared_ptr<GeomAPI_Edge>();
65 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
66 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
67 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
68 aRes->setImpl(new TopoDS_Shape(anEdge));
72 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
73 const std::shared_ptr<GeomAPI_Lin> theLin)
76 return std::shared_ptr<GeomAPI_Edge>();
79 const gp_Lin& aLin = theLin->impl<gp_Lin>();
80 BRepBuilderAPI_MakeEdge anEdgeBuilder(aLin);
81 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge());
82 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
83 aRes->setImpl(new TopoDS_Shape(anEdge));
87 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
88 std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
90 std::shared_ptr<GeomAPI_Edge> aResult;
91 const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
94 TopoDS_Face aFace = TopoDS::Face(aShape);
98 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
101 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
102 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
103 if (!aTrimmed.IsNull())
104 aSurf = aTrimmed->BasisSurface();
105 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
108 gp_Ax1 anAxis = aCyl->Axis();
109 // compute the start and the end points of the resulting edge by the bounding box of the face
110 // (vertices projected to the axis) plus 10%
112 BRepBndLib::Add(aFace, aFaceBnd);
113 gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
115 double aParamMin = 0, aParamMax = 0;
116 for(int aX = 0; aX < 2; aX++) {
117 for(int aY = 0; aY < 2; aY++) {
118 for(int aZ = 0; aZ < 2; aZ++) {
119 gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
120 aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
121 gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
122 double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
125 aParamMin = aProjParam;
126 aParamMax = aProjParam;
128 if (aParamMin > aProjParam)
129 aParamMin = aProjParam;
130 else if (aParamMax < aProjParam)
131 aParamMax = aProjParam;
137 double aDelta = aParamMax - aParamMin;
138 if (aDelta < 1.e-4) aDelta = 1.e-4;
139 aParamMin -= aDelta * 0.1;
140 aParamMax += aDelta * 0.1;
142 gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
143 gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
145 gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
146 // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
147 gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
148 anEnd.Transform(aLoc.Transformation());
151 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
152 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
153 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
154 // an axis is an infinite object
155 anEdge.Infinite(Standard_True);
156 aRes->setImpl(new TopoDS_Shape(anEdge));
160 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
161 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
164 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
165 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
167 gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
169 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
170 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
171 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
172 aRes->setImpl(new TopoDS_Shape(anEdge));
176 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
177 std::shared_ptr<GeomAPI_Circ> theCircle)
179 if(!theCircle.get()) {
180 return std::shared_ptr<GeomAPI_Edge>();
183 const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
184 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
185 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge());
186 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
187 aRes->setImpl(new TopoDS_Shape(anEdge));
191 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
192 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
193 std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
195 std::shared_ptr<GeomAPI_Edge> aRes;
197 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
198 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
200 /// OCCT creates an edge on a circle with empty radius, but visualization
201 /// is not able to process it
202 if (theCenter->isEqual(theStartPoint) || theCenter->isEqual(theEndPoint))
205 double aRadius = theCenter->distance(theStartPoint);
206 gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
208 const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
209 const gp_Pnt& anEndInter = theEndPoint->impl<gp_Pnt>();
211 // project end point to a circle
212 gp_XYZ aEndDir = anEndInter.XYZ() - aCenter.XYZ();
213 gp_Pnt anEnd(aCenter.XYZ() + aEndDir.Normalized() * aRadius);
215 BRepBuilderAPI_MakeEdge anEdgeBuilder;
216 anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
218 anEdgeBuilder.Build();
220 if (anEdgeBuilder.IsDone()) {
221 aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
222 aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
227 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipse(
228 const std::shared_ptr<GeomAPI_Pnt>& theCenter,
229 const std::shared_ptr<GeomAPI_Dir>& theNormal,
230 const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
231 const double theMajorRadius,
232 const double theMinorRadius)
234 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
235 const gp_Dir& aNormal = theNormal->impl<gp_Dir>();
236 const gp_Dir& aMajorAxis = theMajorAxis->impl<gp_Dir>();
238 gp_Elips anEllipse(gp_Ax2(aCenter, aNormal, aMajorAxis), theMajorRadius, theMinorRadius);
240 BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse);
241 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
242 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
243 aRes->setImpl(new TopoDS_Shape(anEdge));