Salome HOME
Fix for bug Bug IPAL20288 (4x: CRASH after trying to build a sketch).
[modules/geom.git] / src / GEOMImpl / GEOMImpl_EllipseDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_EllipseDriver.hxx>
24 #include <GEOMImpl_IEllipse.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRepBuilderAPI_MakeEdge.hxx>
29 #include <BRep_Tool.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopAbs.hxx>
35 #include <TopExp.hxx>
36
37 #include <gp_Pnt.hxx>
38 #include <gp_Elips.hxx>
39
40 //=======================================================================
41 //function : GetID
42 //purpose  :
43 //======================================================================= 
44 const Standard_GUID& GEOMImpl_EllipseDriver::GetID()
45 {
46   static Standard_GUID aEllipseDriver("FF1BBB34-5D14-4df2-980B-3A668264EA16");
47   return aEllipseDriver; 
48 }
49
50
51 //=======================================================================
52 //function : GEOMImpl_EllipseDriver
53 //purpose  : 
54 //=======================================================================
55 GEOMImpl_EllipseDriver::GEOMImpl_EllipseDriver() 
56 {
57 }
58
59 //=======================================================================
60 //function : Execute
61 //purpose  :
62 //======================================================================= 
63 Standard_Integer GEOMImpl_EllipseDriver::Execute(TFunction_Logbook& log) const
64 {
65   if (Label().IsNull()) return 0;    
66   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
67
68   GEOMImpl_IEllipse aCI (aFunction);
69   Standard_Integer aType = aFunction->GetType();
70
71   TopoDS_Shape aShape;
72
73   if (aType == ELLIPSE_PNT_VEC_RR) {
74     // Center
75     gp_Pnt aP = gp::Origin();
76     Handle(GEOM_Function) aRefPoint = aCI.GetCenter();
77     if (!aRefPoint.IsNull()) {
78       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
79       if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
80         Standard_ConstructionError::Raise
81           ("Circle creation aborted: invalid center argument, must be a point");
82       }
83       aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
84     }
85     // Normal
86     gp_Vec aV = gp::DZ();
87     Handle(GEOM_Function) aRefVector = aCI.GetVector();
88     if (!aRefVector.IsNull()) {
89       TopoDS_Shape aShapeVec = aRefVector->GetValue();
90       if (aShapeVec.ShapeType() != TopAbs_EDGE) {
91         Standard_ConstructionError::Raise
92           ("Circle creation aborted: invalid vector argument, must be a vector or an edge");
93       }
94       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
95       TopoDS_Vertex V1, V2;
96       TopExp::Vertices(anE, V1, V2, Standard_True);
97       if (!V1.IsNull() && !V2.IsNull()) {
98         aV = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
99         if (aV.Magnitude() < gp::Resolution()) {
100           Standard_ConstructionError::Raise
101             ("Circle creation aborted: vector of zero length is given");
102         }
103       }
104     }
105     // Axes
106     gp_Ax2 anAxes (aP, aV);
107     // Ellipse
108     gp_Elips anEll (anAxes, aCI.GetRMajor(), aCI.GetRMinor());
109     aShape = BRepBuilderAPI_MakeEdge(anEll).Edge();
110   }
111   else {
112   }
113
114   if (aShape.IsNull()) return 0;
115
116   aFunction->SetValue(aShape);
117
118   log.SetTouched(Label()); 
119
120   return 1;    
121 }
122
123
124 //=======================================================================
125 //function :  GEOMImpl_EllipseDriver_Type_
126 //purpose  :
127 //======================================================================= 
128 Standard_EXPORT Handle_Standard_Type& GEOMImpl_EllipseDriver_Type_()
129 {
130
131   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
132   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
133   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
134   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
135   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
136   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
137  
138
139   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
140   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_EllipseDriver",
141                                                          sizeof(GEOMImpl_EllipseDriver),
142                                                          1,
143                                                          (Standard_Address)_Ancestors,
144                                                          (Standard_Address)NULL);
145
146   return _aType;
147 }
148
149 //=======================================================================
150 //function : DownCast
151 //purpose  :
152 //======================================================================= 
153 const Handle(GEOMImpl_EllipseDriver) Handle(GEOMImpl_EllipseDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
154 {
155   Handle(GEOMImpl_EllipseDriver) _anOtherObject;
156
157   if (!AnObject.IsNull()) {
158      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_EllipseDriver))) {
159        _anOtherObject = Handle(GEOMImpl_EllipseDriver)((Handle(GEOMImpl_EllipseDriver)&)AnObject);
160      }
161   }
162
163   return _anOtherObject ;
164 }