]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/GeomAPI_Edge.cpp
Salome HOME
Issue #1369: Added "SubShapes" feature.
[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_Pln.h>
9 #include<GeomAPI_Pnt.h>
10 #include<GeomAPI_Circ.h>
11 #include<GeomAPI_Dir.h>
12 #include<GeomAPI_Lin.h>
13
14 #include <TopoDS_Shape.hxx>
15 #include <TopoDS_Edge.hxx>
16 #include <TopoDS.hxx>
17 #include <BRep_Builder.hxx>
18 #include <BRep_Tool.hxx>
19 #include <Geom_Curve.hxx>
20 #include <Geom_Line.hxx>
21 #include <Geom_Circle.hxx>
22 #include <GeomAdaptor_Curve.hxx>
23 #include <gp_Ax1.hxx>
24 #include <gp_Pln.hxx>
25
26 GeomAPI_Edge::GeomAPI_Edge()
27 {
28   TopoDS_Edge* anEdge = new TopoDS_Edge;
29
30   BRep_Builder aBuilder;
31   aBuilder.MakeEdge(*anEdge);
32
33   setImpl(anEdge);
34 }
35
36 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
37 {
38   if (!theShape->isNull() && theShape->isEdge()) {
39     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
40   }
41 }
42
43 bool GeomAPI_Edge::isLine() const
44 {
45   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
46   double aFirst, aLast;
47   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
48   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
49     return true;
50   return false;
51 }
52
53 bool GeomAPI_Edge::isCircle() const
54 {
55   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
56   double aFirst, aLast;
57   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
58   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
59   {
60     // Check the difference of first and last parameters to be equal to the curve period
61     if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
62       return true;
63   }
64   return false;
65 }
66
67 bool GeomAPI_Edge::isArc() const
68 {
69   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
70   double aFirst, aLast;
71   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
72   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
73   {
74     // Check the difference of first and last parameters is not equal the curve period
75     if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
76       return true;
77   }
78   return false;
79 }
80
81 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
82 {
83   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
84   double aFirst, aLast;
85   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
86   gp_Pnt aPoint;
87   aCurve->D0(aFirst, aPoint);
88   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
89 }
90
91 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
92 {
93   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
94   double aFirst, aLast;
95   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
96   gp_Pnt aPoint;
97   aCurve->D0(aLast, aPoint);
98   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
99 }
100
101 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
102 {
103   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
104   double aFirst, aLast;
105   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
106   if (aCurve) {
107     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
108     if (aCirc) {
109       gp_Pnt aLoc = aCirc->Location();
110       std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
111       gp_Dir anAxis = aCirc->Axis().Direction();
112       std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
113       return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
114     }
115   }
116   return std::shared_ptr<GeomAPI_Circ>(); // not circle
117 }
118
119 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line()
120 {
121   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
122   double aFirst, aLast;
123   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
124   if (aCurve) {
125     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
126     if (aLine) {
127       gp_Pnt aStartPnt = aLine->Value(aFirst);
128       std::shared_ptr<GeomAPI_Pnt> aStart(
129           new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
130       gp_Pnt aEndPnt = aLine->Value(aLast);
131       std::shared_ptr<GeomAPI_Pnt> aEnd(
132           new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
133       return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
134     }
135   }
136   return std::shared_ptr<GeomAPI_Lin>(); // not circle
137 }
138
139
140 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
141 {
142   if (!theEdge.get() || ! theEdge->isEdge())
143     return false;
144   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
145   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
146
147   if (aMyShape.IsNull() || aInShape.IsNull())
148     return false;
149
150   if (aMyShape.ShapeType() != aInShape.ShapeType())
151     return false;
152
153   double aMyStart, aMyEnd;
154   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
155   double aInStart, aInEnd;
156   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
157
158   // Check that curves a the same type
159   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
160   GeomAdaptor_Curve aInAdaptor(aInCurve);
161   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
162     return false;
163
164   // Check that end point parameters are the same
165   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
166     return false;
167
168   // Check that end points are equal
169   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
170   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
171   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
172   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
173
174   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) || 
175     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
176     return false;
177
178   return true;
179 }
180
181 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
182 {
183   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
184   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
185 }
186
187 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
188 {
189   double aFirst, aLast;
190   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
191   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
192
193   double A, B, C, D;
194   thePlane->coefficients(A, B, C, D);
195   gp_Pln aPlane(A, B, C, D);
196
197   bool inPlane = false;
198   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
199     // check start and end points on the plane
200     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
201     gp_Pnt aLastPnt = aCurve->Value(aLast);
202     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
203               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
204   } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
205     // check the center on the plane and normals are collinear
206     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
207     gp_Pnt aCenter = aCirc->Location();
208     Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
209     inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
210               Abs(Abs(aDot) - 1.0) < Precision::Confusion();
211   } else {
212     // three points checking
213     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
214     gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
215     gp_Pnt aLastPnt = aCurve->Value(aLast);
216     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
217               aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
218               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
219   }
220   return inPlane;
221 }