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 email : webmaster.salome@opencascade.com<mailto: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 <Bnd_Box.hxx>
34 #include <BRepBndLib.hxx>
36 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
37 std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
39 const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
40 const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
42 if (aStart.IsEqual(anEnd, Precision::Confusion()))
43 return std::shared_ptr<GeomAPI_Edge>();
44 if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
45 return std::shared_ptr<GeomAPI_Edge>();
46 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
47 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
48 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
49 aRes->setImpl(new TopoDS_Shape(anEdge));
52 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
53 double theDX, double theDY, double theDZ)
56 const gp_Pnt& aStart = gp_Pnt(0, 0, 0);
57 const gp_Pnt& anEnd = gp_Pnt(theDX, theDY, theDZ);
59 if (aStart.IsEqual(anEnd, Precision::Confusion()))
60 return std::shared_ptr<GeomAPI_Edge>();
61 if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
62 return std::shared_ptr<GeomAPI_Edge>();
63 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
64 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
65 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
66 aRes->setImpl(new TopoDS_Shape(anEdge));
70 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
71 const std::shared_ptr<GeomAPI_Lin> theLin)
74 return std::shared_ptr<GeomAPI_Edge>();
77 const gp_Lin& aLin = theLin->impl<gp_Lin>();
78 BRepBuilderAPI_MakeEdge anEdgeBuilder(aLin);
79 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge());
80 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
81 aRes->setImpl(new TopoDS_Shape(anEdge));
85 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
86 std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
88 std::shared_ptr<GeomAPI_Edge> aResult;
89 const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
92 TopoDS_Face aFace = TopoDS::Face(aShape);
96 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
99 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
100 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
101 if (!aTrimmed.IsNull())
102 aSurf = aTrimmed->BasisSurface();
103 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
106 gp_Ax1 anAxis = aCyl->Axis();
107 // compute the start and the end points of the resulting edge by the bounding box of the face
108 // (vertices projected to the axis) plus 10%
110 BRepBndLib::Add(aFace, aFaceBnd);
111 gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
113 double aParamMin = 0, aParamMax = 0;
114 for(int aX = 0; aX < 2; aX++) {
115 for(int aY = 0; aY < 2; aY++) {
116 for(int aZ = 0; aZ < 2; aZ++) {
117 gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
118 aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
119 gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
120 double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
123 aParamMin = aProjParam;
124 aParamMax = aProjParam;
126 if (aParamMin > aProjParam)
127 aParamMin = aProjParam;
128 else if (aParamMax < aProjParam)
129 aParamMax = aProjParam;
135 double aDelta = aParamMax - aParamMin;
136 if (aDelta < 1.e-4) aDelta = 1.e-4;
137 aParamMin -= aDelta * 0.1;
138 aParamMax += aDelta * 0.1;
140 gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
141 gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
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)
177 if(!theCircle.get()) {
178 return std::shared_ptr<GeomAPI_Edge>();
181 const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
182 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
183 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge());
184 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
185 aRes->setImpl(new TopoDS_Shape(anEdge));
189 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
190 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
191 std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
193 std::shared_ptr<GeomAPI_Edge> aRes;
195 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
196 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
198 /// OCCT creates an edge on a circle with empty radius, but visualization
199 /// is not able to process it
200 if (theCenter->isEqual(theStartPoint))
203 double aRadius = theCenter->distance(theStartPoint);
204 gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
206 const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
207 const gp_Pnt& anEndInter = theEndPoint->impl<gp_Pnt>();
209 // project end point to a circle
210 gp_XYZ aEndDir = anEndInter.XYZ() - aCenter.XYZ();
211 gp_Pnt anEnd(aCenter.XYZ() + aEndDir.Normalized() * aRadius);
213 BRepBuilderAPI_MakeEdge anEdgeBuilder;
214 anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
216 anEdgeBuilder.Build();
218 if (anEdgeBuilder.IsDone()) {
219 aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
220 aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));