1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAPI_Edge.cpp
4 // Created: 24 Jul 2014
5 // Author: Artem ZHIDKOV
7 #include<GeomAPI_Edge.h>
8 #include<GeomAPI_Pnt.h>
9 #include<GeomAPI_Circ.h>
10 #include<GeomAPI_Dir.h>
11 #include<GeomAPI_Lin.h>
13 #include <TopoDS_Shape.hxx>
14 #include <TopoDS_Edge.hxx>
16 #include <BRep_Tool.hxx>
17 #include <Geom_Curve.hxx>
18 #include <Geom_Line.hxx>
19 #include <Geom_Circle.hxx>
20 #include <GeomAdaptor_Curve.hxx>
23 GeomAPI_Edge::GeomAPI_Edge()
28 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
30 if (!theShape->isNull() && theShape->isEdge()) {
31 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
35 bool GeomAPI_Edge::isLine() const
37 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
39 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
40 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
45 bool GeomAPI_Edge::isCircle() const
47 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
49 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
50 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
52 // Check the difference of first and last parameters to be equal to the curve period
53 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
59 bool GeomAPI_Edge::isArc() const
61 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
63 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
64 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
66 // Check the difference of first and last parameters is not equal the curve period
67 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
73 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
75 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
77 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
79 aCurve->D0(aFirst, aPoint);
80 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
83 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
85 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
87 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
89 aCurve->D0(aLast, aPoint);
90 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
93 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
95 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
97 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
99 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
101 gp_Pnt aLoc = aCirc->Location();
102 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
103 gp_Dir anAxis = aCirc->Axis().Direction();
104 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
105 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
108 return std::shared_ptr<GeomAPI_Circ>(); // not circle
111 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line()
113 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
114 double aFirst, aLast;
115 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
117 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
119 gp_Pnt aStartPnt = aLine->Value(aFirst);
120 std::shared_ptr<GeomAPI_Pnt> aStart(
121 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
122 gp_Pnt aEndPnt = aLine->Value(aLast);
123 std::shared_ptr<GeomAPI_Pnt> aEnd(
124 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
125 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
128 return std::shared_ptr<GeomAPI_Lin>(); // not circle
132 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
134 if (!theEdge.get() || ! theEdge->isEdge())
136 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
137 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
139 if (aMyShape.ShapeType() != aInShape.ShapeType())
142 double aMyStart, aMyEnd;
143 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
144 double aInStart, aInEnd;
145 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
147 // Check that curves a the same type
148 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
149 GeomAdaptor_Curve aInAdaptor(aInCurve);
150 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
153 // Check that end point parameters are the same
154 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
157 // Check that end points are equal
158 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
159 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
160 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
161 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
163 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
164 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
170 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
172 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
173 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);