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 static GeomEdgePtr createLine(const gp_Pnt& theStart, const gp_Pnt& theEnd)
41 if (!theStart.IsEqual(theEnd, Precision::Confusion()) &&
42 Abs(theStart.SquareDistance(theEnd)) < 1.e+100) {
43 BRepBuilderAPI_MakeEdge anEdgeBuilder(theStart, theEnd);
44 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
45 aRes = GeomEdgePtr(new GeomAPI_Edge);
46 aRes->setImpl(new TopoDS_Shape(anEdge));
51 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
52 std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
54 const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
55 const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
56 return createLine(aStart, anEnd);
58 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
59 double theDX, double theDY, double theDZ)
61 const gp_Pnt& aStart = gp_Pnt(0, 0, 0);
62 const gp_Pnt& anEnd = gp_Pnt(theDX, theDY, theDZ);
63 return createLine(aStart, anEnd);
66 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
67 const std::shared_ptr<GeomAPI_Lin> theLin)
71 const gp_Lin& aLin = theLin->impl<gp_Lin>();
72 BRepBuilderAPI_MakeEdge anEdgeBuilder(aLin);
73 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
74 aRes = GeomEdgePtr(new GeomAPI_Edge);
75 aRes->setImpl(new TopoDS_Shape(anEdge));
80 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
81 std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
83 std::shared_ptr<GeomAPI_Edge> aResult;
84 const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
87 TopoDS_Face aFace = TopoDS::Face(aShape);
91 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
94 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
95 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
96 if (!aTrimmed.IsNull())
97 aSurf = aTrimmed->BasisSurface();
98 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
101 gp_Ax1 anAxis = aCyl->Axis();
102 // compute the start and the end points of the resulting edge by the bounding box of the face
103 // (vertices projected to the axis) plus 10%
105 BRepBndLib::Add(aFace, aFaceBnd);
106 gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
108 double aParamMin = 0, aParamMax = 0;
109 for(int aX = 0; aX < 2; aX++) {
110 for(int aY = 0; aY < 2; aY++) {
111 for(int aZ = 0; aZ < 2; aZ++) {
112 gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
113 aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
114 gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
115 double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
118 aParamMin = aProjParam;
119 aParamMax = aProjParam;
121 if (aParamMin > aProjParam)
122 aParamMin = aProjParam;
123 else if (aParamMax < aProjParam)
124 aParamMax = aProjParam;
130 double aDelta = aParamMax - aParamMin;
131 if (aDelta < 1.e-4) aDelta = 1.e-4;
132 aParamMin -= aDelta * 0.1;
133 aParamMax += aDelta * 0.1;
135 gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
136 gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
138 gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
139 // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
140 gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
141 anEnd.Transform(aLoc.Transformation());
144 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
145 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
146 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
147 // an axis is an infinite object
148 anEdge.Infinite(Standard_True);
149 aRes->setImpl(new TopoDS_Shape(anEdge));
153 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
154 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
157 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
158 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
160 gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
162 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
163 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
164 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
165 aRes->setImpl(new TopoDS_Shape(anEdge));
169 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
170 std::shared_ptr<GeomAPI_Circ> theCircle)
173 if (theCircle.get()) {
174 const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
175 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
176 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
177 aRes = GeomEdgePtr(new GeomAPI_Edge);
178 aRes->setImpl(new TopoDS_Shape(anEdge));
183 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
184 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
185 std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
187 std::shared_ptr<GeomAPI_Edge> aRes;
189 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
190 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
192 /// OCCT creates an edge on a circle with empty radius, but visualization
193 /// is not able to process it
194 if (theCenter->isEqual(theStartPoint) || theCenter->isEqual(theEndPoint))
197 double aRadius = theCenter->distance(theStartPoint);
198 gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
200 const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
201 const gp_Pnt& anEndInter = theEndPoint->impl<gp_Pnt>();
203 // project end point to a circle
204 gp_XYZ aEndDir = anEndInter.XYZ() - aCenter.XYZ();
205 gp_Pnt anEnd(aCenter.XYZ() + aEndDir.Normalized() * aRadius);
207 BRepBuilderAPI_MakeEdge anEdgeBuilder;
208 anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
210 anEdgeBuilder.Build();
212 if (anEdgeBuilder.IsDone()) {
213 aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
214 aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
219 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::ellipse(
220 const std::shared_ptr<GeomAPI_Pnt>& theCenter,
221 const std::shared_ptr<GeomAPI_Dir>& theNormal,
222 const std::shared_ptr<GeomAPI_Dir>& theMajorAxis,
223 const double theMajorRadius,
224 const double theMinorRadius)
226 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
227 const gp_Dir& aNormal = theNormal->impl<gp_Dir>();
228 const gp_Dir& aMajorAxis = theMajorAxis->impl<gp_Dir>();
230 gp_Elips anEllipse(gp_Ax2(aCenter, aNormal, aMajorAxis), theMajorRadius, theMinorRadius);
232 BRepBuilderAPI_MakeEdge anEdgeBuilder(anEllipse);
233 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
234 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
235 aRes->setImpl(new TopoDS_Shape(anEdge));