Salome HOME
External edges for sketch: lines and circles
[modules/shaper.git] / src / GeomAPI / GeomAPI_Edge.cpp
1 // File:        GeomAPI_Edge.cpp
2 // Created:     24 Jul 2014
3 // Author:      Artem ZHIDKOV
4
5 #include<GeomAPI_Edge.h>
6 #include<GeomAPI_Pnt.h>
7 #include<GeomAPI_Circ.h>
8 #include<GeomAPI_Dir.h>
9
10 #include <TopoDS_Shape.hxx>
11 #include <TopoDS_Edge.hxx>
12 #include <BRep_Tool.hxx>
13 #include <Geom_Curve.hxx>
14 #include <Geom_Line.hxx>
15 #include <Geom_Circle.hxx>
16 #include <gp_Ax1.hxx>
17
18 GeomAPI_Edge::GeomAPI_Edge()
19   : GeomAPI_Shape()
20 {
21 }
22
23 GeomAPI_Edge::GeomAPI_Edge(const boost::shared_ptr<GeomAPI_Shape>& theShape)
24 {
25   if (!theShape->isNull() && theShape->isEdge()) {
26     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
27   }
28 }
29
30 bool GeomAPI_Edge::isLine() const
31 {
32   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
33   double aFirst, aLast;
34   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
35   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
36     return true;
37   return false;
38 }
39
40 bool GeomAPI_Edge::isCircle() const
41 {
42   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
43   double aFirst, aLast;
44   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
45   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)) && aCurve->IsClosed())
46     return true;
47   return false;
48 }
49
50 bool GeomAPI_Edge::isArc() const
51 {
52   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
53   double aFirst, aLast;
54   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
55   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)) && !aCurve->IsClosed())
56     return true;
57   return false;
58 }
59
60 boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
61 {
62   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
63   double aFirst, aLast;
64   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
65   gp_Pnt aPoint;
66   aCurve->D0(aFirst, aPoint);
67   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
68 }
69
70 boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
71 {
72   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
73   double aFirst, aLast;
74   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
75   gp_Pnt aPoint;
76   aCurve->D0(aLast, aPoint);
77   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
78 }
79
80 boost::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
81 {
82   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
83   double aFirst, aLast;
84   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
85   if (aCurve) {
86     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
87     if (aCirc) {
88       gp_Pnt aLoc = aCirc->Location();
89       boost::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
90       gp_Dir anAxis = aCirc->Axis().Direction();
91       boost::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
92       return boost::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
93     }
94   }
95   return boost::shared_ptr<GeomAPI_Circ>(); // not circle
96 }