Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / GeomAPI / GeomAPI_Curve.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include<GeomAPI_Curve.h>
21 #include<GeomAPI_Pnt.h>
22
23 #include <TopoDS_Shape.hxx>
24 #include <Geom_Circle.hxx>
25 #include <Geom_Curve.hxx>
26 #include <Geom_Ellipse.hxx>
27 #include <Geom_Line.hxx>
28 #include <Geom_TrimmedCurve.hxx>
29 #include <BRep_Tool.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <TopoDS.hxx>
32 #include <GeomAdaptor_Curve.hxx>
33
34 #define MY_CURVE (*(implPtr<Handle_Geom_Curve>()))
35
36 GeomAPI_Curve::GeomAPI_Curve()
37     : GeomAPI_Interface(new Handle_Geom_Curve()), myStart(0), myEnd(1)
38 {
39 }
40
41 GeomAPI_Curve::GeomAPI_Curve(const std::shared_ptr<GeomAPI_Shape>& theShape)
42   : GeomAPI_Interface(new Handle_Geom_Curve()) // initially it is null
43 {
44   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
45   TopoDS_Edge anEdge = TopoDS::Edge(aShape);
46   if (!anEdge.IsNull()) {
47     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, myStart, myEnd);
48     if (!aCurve.IsNull()) {
49       setImpl(new Handle(Geom_Curve)(aCurve));
50     }
51   }
52 }
53
54 bool GeomAPI_Curve::isNull() const
55 {
56   return MY_CURVE.IsNull() == Standard_True;
57 }
58
59 bool GeomAPI_Curve::isLine() const
60 {
61   return !isNull() && MY_CURVE->DynamicType() == STANDARD_TYPE(Geom_Line);
62 }
63
64 bool GeomAPI_Curve::isCircle() const
65 {
66   return !isNull() && MY_CURVE->DynamicType() == STANDARD_TYPE(Geom_Circle);
67 }
68
69 bool GeomAPI_Curve::isEllipse() const
70 {
71   return !isNull() && MY_CURVE->DynamicType() == STANDARD_TYPE(Geom_Ellipse);
72 }
73
74 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Curve::getPoint(double theParam)
75 {
76   GeomAdaptor_Curve aAdaptor(MY_CURVE, myStart, myEnd);
77   gp_Pnt aPnt = aAdaptor.Value(theParam);
78   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
79 }
80
81 bool GeomAPI_Curve::isEqual(const std::shared_ptr<GeomAPI_Curve>& theOther) const
82 {
83   return MY_CURVE == theOther->impl<Handle_Geom_Curve>();
84 }
85
86 bool GeomAPI_Curve::isTrimmed() const
87 {
88   return !isNull() && MY_CURVE->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve);
89 }
90
91 // unused in the unit tests for now
92 // LCOV_EXCL_START
93 GeomCurvePtr GeomAPI_Curve::basisCurve() const
94 {
95   Handle(Geom_Curve) aCurve = MY_CURVE;
96   if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
97     aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
98
99   GeomCurvePtr aNewCurve(new GeomAPI_Curve);
100   aNewCurve->setImpl(new Handle(Geom_Curve)(aCurve));
101   return aNewCurve;
102 }
103 // LCOV_EXCL_STOP
104
105 // ================================================================================================
106
107 bool GeomAPI_Curve::Comparator::operator()(const GeomCurvePtr& theCurve1,
108                                            const GeomCurvePtr& theCurve2) const
109 {
110   const Handle(Geom_Curve)& aCurve1 = theCurve1->impl<Handle_Geom_Curve>();
111   const Handle(Geom_Curve)& aCurve2 = theCurve2->impl<Handle_Geom_Curve>();
112   return aCurve1.get() < aCurve2.get();
113 }