1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_EdgeBuilder.cpp
4 // Created: 23 Apr 2014
5 // Author: Mikhail PONIKAROV
7 #include <GeomAlgoAPI_EdgeBuilder.h>
9 #include <BRepBuilderAPI_MakeEdge.hxx>
10 #include <TopoDS_Edge.hxx>
11 #include <TopoDS_Face.hxx>
13 #include <BRep_Tool.hxx>
14 #include <Geom_Plane.hxx>
15 #include <Geom_CylindricalSurface.hxx>
16 #include <Geom_RectangularTrimmedSurface.hxx>
19 #include <gp_Circ.hxx>
20 #include <Bnd_Box.hxx>
21 #include <BRepBndLib.hxx>
23 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
24 std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
26 const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
27 const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
29 if (aStart.IsEqual(anEnd, Precision::Confusion()))
30 return std::shared_ptr<GeomAPI_Edge>();
31 if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
32 return std::shared_ptr<GeomAPI_Edge>();
33 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
34 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
35 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
36 aRes->setImpl(new TopoDS_Shape(anEdge));
39 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
40 double theDX, double theDY, double theDZ)
43 const gp_Pnt& aStart = gp_Pnt(0, 0, 0);
44 const gp_Pnt& anEnd = gp_Pnt(theDX, theDY, theDZ);
46 if (aStart.IsEqual(anEnd, Precision::Confusion()))
47 return std::shared_ptr<GeomAPI_Edge>();
48 if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
49 return std::shared_ptr<GeomAPI_Edge>();
50 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
51 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
52 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
53 aRes->setImpl(new TopoDS_Shape(anEdge));
57 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
58 const std::shared_ptr<GeomAPI_Lin> theLin)
61 return std::shared_ptr<GeomAPI_Edge>();
64 const gp_Lin& aLin = theLin->impl<gp_Lin>();
65 BRepBuilderAPI_MakeEdge anEdgeBuilder(aLin);
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::cylinderAxis(
73 std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
75 std::shared_ptr<GeomAPI_Edge> aResult;
76 const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
79 TopoDS_Face aFace = TopoDS::Face(aShape);
83 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
86 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
87 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
88 if (!aTrimmed.IsNull())
89 aSurf = aTrimmed->BasisSurface();
90 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
93 gp_Ax1 anAxis = aCyl->Axis();
94 // compute the start and the end points of the resulting edge by the bounding box of the face
95 // (vertices projected to the axis) plus 10%
97 BRepBndLib::Add(aFace, aFaceBnd);
98 gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
100 double aParamMin = 0, aParamMax = 0;
101 for(int aX = 0; aX < 2; aX++) {
102 for(int aY = 0; aY < 2; aY++) {
103 for(int aZ = 0; aZ < 2; aZ++) {
104 gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
105 aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
106 gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
107 double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
110 aParamMin = aProjParam;
111 aParamMax = aProjParam;
113 if (aParamMin > aProjParam)
114 aParamMin = aProjParam;
115 else if (aParamMax < aProjParam)
116 aParamMax = aProjParam;
122 double aDelta = aParamMax - aParamMin;
123 if (aDelta < 1.e-4) aDelta = 1.e-4;
124 aParamMin -= aDelta * 0.1;
125 aParamMax += aDelta * 0.1;
127 gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
128 gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
130 gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
131 // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
132 gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
133 anEnd.Transform(aLoc.Transformation());
136 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
137 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
138 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
139 // an axis is an infinite object
140 anEdge.Infinite(Standard_True);
141 aRes->setImpl(new TopoDS_Shape(anEdge));
145 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
146 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
149 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
150 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
152 gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
154 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
155 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
156 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
157 aRes->setImpl(new TopoDS_Shape(anEdge));
161 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
162 std::shared_ptr<GeomAPI_Circ> theCircle)
164 if(!theCircle.get()) {
165 return std::shared_ptr<GeomAPI_Edge>();
168 const gp_Circ& aCirc = theCircle->impl<gp_Circ>();
169 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCirc);
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::lineCircleArc(
177 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
178 std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
180 std::shared_ptr<GeomAPI_Edge> aRes;
182 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
183 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
185 /// OCCT creates an edge on a circle with empty radius, but visualization
186 /// is not able to process it
187 if (theCenter->isEqual(theStartPoint))
190 double aRadius = theCenter->distance(theStartPoint);
191 gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
193 const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
194 const gp_Pnt& anEndInter = theEndPoint->impl<gp_Pnt>();
196 // project end point to a circle
197 gp_XYZ aEndDir = anEndInter.XYZ() - aCenter.XYZ();
198 gp_Pnt anEnd(aCenter.XYZ() + aEndDir.Normalized() * aRadius);
200 BRepBuilderAPI_MakeEdge anEdgeBuilder;
201 anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
203 anEdgeBuilder.Build();
205 if (anEdgeBuilder.IsDone()) {
206 aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
207 aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));