Salome HOME
New feature "Placement" was implemented
[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 <TopoDS.hxx>
13 #include <BRep_Tool.hxx>
14 #include <Geom_Curve.hxx>
15 #include <Geom_Line.hxx>
16 #include <Geom_Circle.hxx>
17 #include <GeomAdaptor_Curve.hxx>
18 #include <gp_Ax1.hxx>
19
20 GeomAPI_Edge::GeomAPI_Edge()
21   : GeomAPI_Shape()
22 {
23 }
24
25 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
26 {
27   if (!theShape->isNull() && theShape->isEdge()) {
28     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
29   }
30 }
31
32 bool GeomAPI_Edge::isLine() const
33 {
34   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
35   double aFirst, aLast;
36   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
37   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
38     return true;
39   return false;
40 }
41
42 bool GeomAPI_Edge::isCircle() const
43 {
44   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
45   double aFirst, aLast;
46   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
47   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)) && aCurve->IsClosed())
48     return true;
49   return false;
50 }
51
52 bool GeomAPI_Edge::isArc() const
53 {
54   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
55   double aFirst, aLast;
56   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
57   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)) && !aCurve->IsClosed())
58     return true;
59   return false;
60 }
61
62 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
63 {
64   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
65   double aFirst, aLast;
66   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
67   gp_Pnt aPoint;
68   aCurve->D0(aFirst, aPoint);
69   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
70 }
71
72 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
73 {
74   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
75   double aFirst, aLast;
76   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
77   gp_Pnt aPoint;
78   aCurve->D0(aLast, aPoint);
79   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
80 }
81
82 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
83 {
84   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
85   double aFirst, aLast;
86   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
87   if (aCurve) {
88     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
89     if (aCirc) {
90       gp_Pnt aLoc = aCirc->Location();
91       std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
92       gp_Dir anAxis = aCirc->Axis().Direction();
93       std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
94       return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
95     }
96   }
97   return std::shared_ptr<GeomAPI_Circ>(); // not circle
98 }
99
100
101 bool GeomAPI_Edge::isEqual(std::shared_ptr<GeomAPI_Shape> theEdge)
102 {
103   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
104   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
105   
106   double aMyStart, aMyEnd;
107   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
108   double aInStart, aInEnd;
109   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
110
111   // Check that curves a the same type
112   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
113   GeomAdaptor_Curve aInAdaptor(aInCurve);
114   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
115     return false;
116
117   // Check that end point parameters are the same
118   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
119     return false;
120
121   // Check that end points are equal
122   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
123   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
124   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
125   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
126
127   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) || 
128     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
129     return false;
130
131   return true;
132 }