Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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.hxx>
12 #include <BRep_Tool.hxx>
13 #include <Geom_Plane.hxx>
14
15 #include <gp_Ax2.hxx>
16 #include <gp_Circ.hxx>
17
18 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::line(
19     std::shared_ptr<GeomAPI_Pnt> theStart, std::shared_ptr<GeomAPI_Pnt> theEnd)
20 {
21   const gp_Pnt& aStart = theStart->impl<gp_Pnt>();
22   const gp_Pnt& anEnd = theEnd->impl<gp_Pnt>();
23
24   if (aStart.IsEqual(anEnd, Precision::Confusion()))
25     return std::shared_ptr<GeomAPI_Edge>();
26   if (Abs(aStart.SquareDistance(anEnd)) > 1.e+100)
27     return std::shared_ptr<GeomAPI_Edge>();
28   BRepBuilderAPI_MakeEdge anEdgeBuilder(aStart, anEnd);
29   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
30   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
31   aRes->setImpl(new TopoDS_Shape(anEdge));
32   return aRes;
33 }
34
35 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircle(
36     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
37     double theRadius)
38 {
39   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
40   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
41
42   gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius);
43
44   BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle);
45   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
46   TopoDS_Edge anEdge = anEdgeBuilder.Edge();
47   aRes->setImpl(new TopoDS_Shape(anEdge));
48   return aRes;
49 }
50
51 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_EdgeBuilder::lineCircleArc(
52     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Pnt> theStartPoint,
53     std::shared_ptr<GeomAPI_Pnt> theEndPoint, std::shared_ptr<GeomAPI_Dir> theNormal)
54 {
55   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
56   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
57
58   double aRadius = theCenter->distance(theStartPoint);
59   gp_Circ aCircle(gp_Ax2(aCenter, aDir), aRadius);
60
61   const gp_Pnt& aStart = theStartPoint->impl<gp_Pnt>();
62   const gp_Pnt& anEnd = theEndPoint->impl<gp_Pnt>();
63
64   BRepBuilderAPI_MakeEdge anEdgeBuilder;
65   if (aStart.IsEqual(anEnd, Precision::Confusion())
66       || gp_Pnt(0, 0, 0).IsEqual(anEnd, Precision::Confusion()))
67     anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle);
68   else
69     anEdgeBuilder = BRepBuilderAPI_MakeEdge(aCircle, aStart, anEnd);
70
71   std::shared_ptr<GeomAPI_Edge> aRes(new GeomAPI_Edge);
72   anEdgeBuilder.Build();
73
74   if (anEdgeBuilder.IsDone())
75     aRes->setImpl(new TopoDS_Shape(anEdgeBuilder.Edge()));
76   else
77     aRes = std::shared_ptr<GeomAPI_Edge>();
78   return aRes;
79 }