1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <Standard_Stream.hxx>
24 #include <GEOMImpl_EllipseDriver.hxx>
25 #include <GEOMImpl_IEllipse.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRep_Tool.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Vertex.hxx>
39 #include <gp_Elips.hxx>
41 //=======================================================================
44 //=======================================================================
45 const Standard_GUID& GEOMImpl_EllipseDriver::GetID()
47 static Standard_GUID aEllipseDriver("FF1BBB34-5D14-4df2-980B-3A668264EA16");
48 return aEllipseDriver;
52 //=======================================================================
53 //function : GEOMImpl_EllipseDriver
55 //=======================================================================
56 GEOMImpl_EllipseDriver::GEOMImpl_EllipseDriver()
60 //=======================================================================
63 //=======================================================================
64 Standard_Integer GEOMImpl_EllipseDriver::Execute(TFunction_Logbook& log) const
66 if (Label().IsNull()) return 0;
67 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
69 GEOMImpl_IEllipse aCI (aFunction);
70 Standard_Integer aType = aFunction->GetType();
74 if (aType == ELLIPSE_PNT_VEC_RR) {
76 gp_Pnt aP = gp::Origin();
77 Handle(GEOM_Function) aRefPoint = aCI.GetCenter();
78 if (!aRefPoint.IsNull()) {
79 TopoDS_Shape aShapePnt = aRefPoint->GetValue();
80 if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
81 Standard_ConstructionError::Raise
82 ("Ellipse creation aborted: invalid center argument, must be a point");
84 aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
88 Handle(GEOM_Function) aRefVector = aCI.GetVector();
89 if (!aRefVector.IsNull()) {
90 TopoDS_Shape aShapeVec = aRefVector->GetValue();
91 if (aShapeVec.ShapeType() != TopAbs_EDGE) {
92 Standard_ConstructionError::Raise
93 ("Ellipse creation aborted: invalid normal vector argument, must be a vector or an edge");
95 TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
97 TopExp::Vertices(anE, V1, V2, Standard_True);
98 if (!V1.IsNull() && !V2.IsNull()) {
99 aV = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
100 if (aV.Magnitude() < gp::Resolution()) {
101 Standard_ConstructionError::Raise
102 ("Ellipse creation aborted: normal vector of zero length is given");
108 gp_Ax2 anAxes (aP, aV);
111 Handle(GEOM_Function) aRefVectorMaj = aCI.GetVectorMajor();
112 if (!aRefVectorMaj.IsNull()) {
113 TopoDS_Shape aShapeVec = aRefVectorMaj->GetValue();
114 if (aShapeVec.ShapeType() != TopAbs_EDGE) {
115 Standard_ConstructionError::Raise
116 ("Ellipse creation aborted: invalid major axis vector argument, must be a vector or an edge");
118 TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
119 TopoDS_Vertex V1, V2;
120 TopExp::Vertices(anE, V1, V2, Standard_True);
121 if (!V1.IsNull() && !V2.IsNull()) {
122 gp_Vec aVM (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
123 if (aVM.Magnitude() < gp::Resolution()) {
124 Standard_ConstructionError::Raise
125 ("Ellipse creation aborted: major axis vector of zero length is given");
127 if (aV.IsParallel(aVM, Precision::Angular())) {
128 Standard_ConstructionError::Raise
129 ("Ellipse creation aborted: normal and major axis vectors are parallel");
131 // Axes defined with main axis vector
132 anAxes = gp_Ax2 (aP, aV, aVM);
137 gp_Elips anEll (anAxes, aCI.GetRMajor(), aCI.GetRMinor());
138 aShape = BRepBuilderAPI_MakeEdge(anEll).Edge();
143 if (aShape.IsNull()) return 0;
145 aFunction->SetValue(aShape);
147 log.SetTouched(Label());
153 //=======================================================================
154 //function : GEOMImpl_EllipseDriver_Type_
156 //=======================================================================
157 Standard_EXPORT Handle_Standard_Type& GEOMImpl_EllipseDriver_Type_()
160 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
161 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
162 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
163 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
164 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
165 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
168 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
169 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_EllipseDriver",
170 sizeof(GEOMImpl_EllipseDriver),
172 (Standard_Address)_Ancestors,
173 (Standard_Address)NULL);
178 //=======================================================================
179 //function : DownCast
181 //=======================================================================
182 const Handle(GEOMImpl_EllipseDriver) Handle(GEOMImpl_EllipseDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
184 Handle(GEOMImpl_EllipseDriver) _anOtherObject;
186 if (!AnObject.IsNull()) {
187 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_EllipseDriver))) {
188 _anOtherObject = Handle(GEOMImpl_EllipseDriver)((Handle(GEOMImpl_EllipseDriver)&)AnObject);
192 return _anOtherObject ;