1 // Copyright (C) 2007-2010 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
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_EllipseDriver.hxx>
26 #include <GEOMImpl_IEllipse.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRep_Tool.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Vertex.hxx>
40 #include <gp_Elips.hxx>
42 //=======================================================================
45 //=======================================================================
46 const Standard_GUID& GEOMImpl_EllipseDriver::GetID()
48 static Standard_GUID aEllipseDriver("FF1BBB34-5D14-4df2-980B-3A668264EA16");
49 return aEllipseDriver;
53 //=======================================================================
54 //function : GEOMImpl_EllipseDriver
56 //=======================================================================
57 GEOMImpl_EllipseDriver::GEOMImpl_EllipseDriver()
61 //=======================================================================
64 //=======================================================================
65 Standard_Integer GEOMImpl_EllipseDriver::Execute(TFunction_Logbook& log) const
67 if (Label().IsNull()) return 0;
68 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
70 GEOMImpl_IEllipse aCI (aFunction);
71 Standard_Integer aType = aFunction->GetType();
75 if (aType == ELLIPSE_PNT_VEC_RR) {
77 gp_Pnt aP = gp::Origin();
78 Handle(GEOM_Function) aRefPoint = aCI.GetCenter();
79 if (!aRefPoint.IsNull()) {
80 TopoDS_Shape aShapePnt = aRefPoint->GetValue();
81 if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
82 Standard_ConstructionError::Raise
83 ("Ellipse creation aborted: invalid center argument, must be a point");
85 aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
89 Handle(GEOM_Function) aRefVector = aCI.GetVector();
90 if (!aRefVector.IsNull()) {
91 TopoDS_Shape aShapeVec = aRefVector->GetValue();
92 if (aShapeVec.ShapeType() != TopAbs_EDGE) {
93 Standard_ConstructionError::Raise
94 ("Ellipse creation aborted: invalid normal vector argument, must be a vector or an edge");
96 TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
98 TopExp::Vertices(anE, V1, V2, Standard_True);
99 if (!V1.IsNull() && !V2.IsNull()) {
100 aV = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
101 if (aV.Magnitude() < gp::Resolution()) {
102 Standard_ConstructionError::Raise
103 ("Ellipse creation aborted: normal vector of zero length is given");
109 gp_Ax2 anAxes (aP, aV);
112 Handle(GEOM_Function) aRefVectorMaj = aCI.GetVectorMajor();
113 if (!aRefVectorMaj.IsNull()) {
114 TopoDS_Shape aShapeVec = aRefVectorMaj->GetValue();
115 if (aShapeVec.ShapeType() != TopAbs_EDGE) {
116 Standard_ConstructionError::Raise
117 ("Ellipse creation aborted: invalid major axis vector argument, must be a vector or an edge");
119 TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
120 TopoDS_Vertex V1, V2;
121 TopExp::Vertices(anE, V1, V2, Standard_True);
122 if (!V1.IsNull() && !V2.IsNull()) {
123 gp_Vec aVM (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
124 if (aVM.Magnitude() < gp::Resolution()) {
125 Standard_ConstructionError::Raise
126 ("Ellipse creation aborted: major axis vector of zero length is given");
128 if (aV.IsParallel(aVM, Precision::Angular())) {
129 Standard_ConstructionError::Raise
130 ("Ellipse creation aborted: normal and major axis vectors are parallel");
132 // Axes defined with main axis vector
133 anAxes = gp_Ax2 (aP, aV, aVM);
138 gp_Elips anEll (anAxes, aCI.GetRMajor(), aCI.GetRMinor());
139 aShape = BRepBuilderAPI_MakeEdge(anEll).Edge();
144 if (aShape.IsNull()) return 0;
146 aFunction->SetValue(aShape);
148 log.SetTouched(Label());
154 //=======================================================================
155 //function : GEOMImpl_EllipseDriver_Type_
157 //=======================================================================
158 Standard_EXPORT Handle_Standard_Type& GEOMImpl_EllipseDriver_Type_()
161 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
162 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
163 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
164 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
165 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
166 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
169 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
170 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_EllipseDriver",
171 sizeof(GEOMImpl_EllipseDriver),
173 (Standard_Address)_Ancestors,
174 (Standard_Address)NULL);
179 //=======================================================================
180 //function : DownCast
182 //=======================================================================
183 const Handle(GEOMImpl_EllipseDriver) Handle(GEOMImpl_EllipseDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
185 Handle(GEOMImpl_EllipseDriver) _anOtherObject;
187 if (!AnObject.IsNull()) {
188 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_EllipseDriver))) {
189 _anOtherObject = Handle(GEOMImpl_EllipseDriver)((Handle(GEOMImpl_EllipseDriver)&)AnObject);
193 return _anOtherObject ;