Salome HOME
Issue #555 - Make a number of shifted/rotated copies - selected object does not appea...
[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     const bool theInfinite)
23 {
24   const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
25   const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
26
27   if (aStart.IsEqual(anEnd, Precision::Confusion()))
28     return std::shared_ptr<GeomAPI_Edge>();
29   if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
30     return std::shared_ptr<GeomAPI_Edge>();
31   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
32   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
33   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
34   if (theInfinite)
35     anEdge.Infinite(Standard_True);
36   aRes->setImpl(new TopoDS_Shape(anEdge));
37   return aRes;
38 }
39
40 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::cylinderAxis(
41     std::shared_ptr<GeomAPI_Shape> theCylindricalFace)
42 {
43   std::shared_ptr<GeomAPI_Edge> aResult;
44   const TopoDS_Shape& aShape = theCylindricalFace->impl<TopoDS_Shape>();
45   if (aShape.IsNull())
46     return aResult;
47   TopoDS_Face aFace = TopoDS::Face(aShape);
48   if (aFace.IsNull())
49     return aResult;
50   TopLoc_Location aLoc;
51   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
52   if (aSurf.IsNull())
53     return aResult;
54   Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
55   if (aCyl.IsNull())
56     return aResult;
57   gp_Ax1 anAxis = aCyl->Axis();
58   gp_Pnt aStart(anAxis.Location().Transformed(aLoc.Transformation()));
59   // edge length is 100, "-" because cylinder of extrusion has negative direction with the cylinder
60   gp_Pnt anEnd(anAxis.Location().XYZ() - anAxis.Direction().XYZ() * 100.);
61   anEnd.Transform(aLoc.Transformation());
62   
63   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
64   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
65   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
66   // an axis is an infinite object
67   anEdge.Infinite(Standard_True);
68   aRes->setImpl(new TopoDS_Shape(anEdge));
69   return aRes;
70 }
71
72 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
73     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
74     double theRadius)
75 {
76   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
77   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
78
79   gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
80
81   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
82   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
83   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
84   aRes->setImpl(new TopoDS_Shape(anEdge));
85   return aRes;
86 }
87
88 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
89     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
90     std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
91 {
92   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
93   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
94
95   double aRadius = theCenter->distance(theStartPoint);
96   gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
97
98   const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
99   const gp_Pnt& anEnd = theEndPoint->impl<gp_Pnt>();
100
101   BRepBuilderAPI_MakeEdge anEdgeBuilder;
102   if (aStart.IsEqual(anEnd, Precision::Confusion())
103       || gp_Pnt(0, 0, 0).IsEqual(anEnd, Precision::Confusion()))
104     anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle);
105   else
106     anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
107
108   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
109   anEdgeBuilder.Build();
110
111   if (anEdgeBuilder.IsDone())
112     aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
113   else
114     aRes = std::shared_ptr<GeomAPI_Edge>();
115   return aRes;
116 }