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));
40 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
41 std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
43 std::shared_ptr<GeomAPI_Edge> aResult;
44 const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
47 TopoDS_Face aFace = TopoDS::Face(aShape);
51 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
54 Handle(Geom_RectangularTrimmedSurface) aTrimmed =
55 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
56 if (!aTrimmed.IsNull())
57 aSurf = aTrimmed->BasisSurface();
58 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
61 gp_Ax1 anAxis = aCyl->Axis();
62 // compute the start and the end points of the resulting edge by the bounding box of the face
63 // (vertices projected to the axis) plus 10%
65 BRepBndLib::Add(aFace, aFaceBnd);
66 gp_Pnt aBoxMin(aFaceBnd.CornerMin()), aBoxMax(aFaceBnd.CornerMax());
68 double aParamMin = 0, aParamMax = 0;
69 for(int aX = 0; aX < 2; aX++) {
70 for(int aY = 0; aY < 2; aY++) {
71 for(int aZ = 0; aZ < 2; aZ++) {
72 gp_XYZ aBoxVertex(aX == 0 ? aBoxMin.X() : aBoxMax.X(),
73 aY == 0 ? aBoxMin.Y() : aBoxMax.Y(), aZ == 0 ? aBoxMin.Z() : aBoxMax.Z());
74 gp_XYZ aVec(aBoxVertex - anAxis.Location().XYZ());
75 double aProjParam = aVec.Dot(anAxis.Direction().XYZ());
78 aParamMin = aProjParam;
79 aParamMax = aProjParam;
81 if (aParamMin > aProjParam)
82 aParamMin = aProjParam;
83 else if (aParamMax < aProjParam)
84 aParamMax = aProjParam;
90 double aDelta = aParamMax - aParamMin;
91 if (aDelta < 1.e-4) aDelta = 1.e-4;
92 aParamMin -= aDelta * 0.1;
93 aParamMax += aDelta * 0.1;
95 gp_Pnt aStart(aParamMin * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
96 gp_Pnt anEnd(aParamMax * anAxis.Direction().XYZ() + anAxis.Location().XYZ());
98 gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
99 // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
100 gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
101 anEnd.Transform(aLoc.Transformation());
104 BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
105 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
106 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
107 // an axis is an infinite object
108 anEdge.Infinite(Standard_True);
109 aRes->setImpl(new TopoDS_Shape(anEdge));
113 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
114 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
117 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
118 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
120 gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
122 BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
123 std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
124 TopoDS_Edge anEdge = anEdgeBuilder.Edge();
125 aRes->setImpl(new TopoDS_Shape(anEdge));
129 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
130 std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
131 std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
133 std::shared_ptr<GeomAPI_Edge> aRes;
135 const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
136 const gp_Dir& aDir = theNormal->impl<gp_Dir>();
138 /// OCCT creates an edge on a circle with empty radius, but visualization
139 /// is not able to process it
140 if (theCenter->isEqual(theStartPoint))
143 double aRadius = theCenter->distance(theStartPoint);
144 gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
146 const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
147 const gp_Pnt& anEnd = theEndPoint->impl<gp_Pnt>();
149 BRepBuilderAPI_MakeEdge anEdgeBuilder;
150 anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
152 anEdgeBuilder.Build();
154 if (anEdgeBuilder.IsDone()) {
155 aRes = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge);
156 aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));