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