Salome HOME
Fix for issue #461
[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   aRes->setImpl(new TopoDS_Shape(anEdge));
64   return aRes;
65 }
66
67 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
68     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
69     double theRadius)
70 {
71   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
72   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
73
74   gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
75
76   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
77   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
78   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
79   aRes->setImpl(new TopoDS_Shape(anEdge));
80   return aRes;
81 }
82
83 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
84     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
85     std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
86 {
87   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
88   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
89
90   double aRadius = theCenter->distance(theStartPoint);
91   gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
92
93   const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
94   const gp_Pnt& anEnd = theEndPoint->impl<gp_Pnt>();
95
96   BRepBuilderAPI_MakeEdge anEdgeBuilder;
97   if (aStart.IsEqual(anEnd, Precision::Confusion())
98       || gp_Pnt(0, 0, 0).IsEqual(anEnd, Precision::Confusion()))
99     anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle);
100   else
101     anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
102
103   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
104   anEdgeBuilder.Build();
105
106   if (anEdgeBuilder.IsDone())
107     aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
108   else
109     aRes = std::shared_ptr<GeomAPI_Edge>();
110   return aRes;
111 }