Salome HOME
Issue #608: Usage of OCCT in interface -- Remove OCCT from *Export interfaces --...
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_EdgeBuilder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_EdgeBuilder.cpp
4 // Created:     23 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <GeomAlgoAPI_EdgeBuilder.h>
8 #include <gp_Pln.hxx>
9 #include <BRepBuilderAPI_MakeEdge.hxx>
10 #include <TopoDS_Edge.hxx>
11 #include <TopoDS_Face.hxx>
12 #include <TopoDS.hxx>
13 #include <BRep_Tool.hxx>
14 #include <Geom_Plane.hxx>
15 #include <Geom_CylindricalSurface.hxx>
16
17 #include <gp_Ax2.hxx>
18 #include <gp_Circ.hxx>
19
20 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
21     std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
22 {
23   const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
24   const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
25
26   if (aStart.IsEqual(anEnd, Precision::Confusion()))
27     return std::shared_ptr<GeomAPI_Edge>();
28   if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
29     return std::shared_ptr<GeomAPI_Edge>();
30   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
31   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
32   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
33   aRes->setImpl(new TopoDS_Shape(anEdge));
34   return aRes;
35 }
36
37 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
38     std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
39 {
40   std::shared_ptr<GeomAPI_Edge> aResult;
41   const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
42   if (aShape.IsNull())
43     return aResult;
44   TopoDS_Face aFace = TopoDS::Face(aShape);
45   if (aFace.IsNull())
46     return aResult;
47   TopLoc_Location aLoc;
48   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
49   if (aSurf.IsNull())
50     return aResult;
51   Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
52   if (aCyl.IsNull())
53     return aResult;
54   gp_Ax1 anAxis = aCyl->Axis();
55   gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
56   // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
57   gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
58   anEnd.Transform(aLoc.Transformation());
59   
60   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
61   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
62   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
63   // an axis is an infinite object
64   anEdge.Infinite(Standard_True);
65   aRes->setImpl(new TopoDS_Shape(anEdge));
66   return aRes;
67 }
68
69 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
70     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
71     double theRadius)
72 {
73   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
74   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
75
76   gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
77
78   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
79   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
80   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
81   aRes->setImpl(new TopoDS_Shape(anEdge));
82   return aRes;
83 }
84
85 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
86     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
87     std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
88 {
89   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
90   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
91
92   double aRadius = theCenter->distance(theStartPoint);
93   gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
94
95   const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
96   const gp_Pnt& anEnd = theEndPoint->impl<gp_Pnt>();
97
98   BRepBuilderAPI_MakeEdge anEdgeBuilder;
99   if (aStart.IsEqual(anEnd, Precision::Confusion())
100       || gp_Pnt(0, 0, 0).IsEqual(anEnd, Precision::Confusion()))
101     anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle);
102   else
103     anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
104
105   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
106   anEdgeBuilder.Build();
107
108   if (anEdgeBuilder.IsDone())
109     aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
110   else
111     aRes = std::shared_ptr<GeomAPI_Edge>();
112   return aRes;
113 }