Salome HOME
a84948554a5ee4a0d0279b67c6e99b3cc850de75
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ConeDriver.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_ConeDriver.hxx>
25 #include <GEOMImpl_ICone.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <BRepPrimAPI_MakeCone.hxx>
30 #include <BRepPrimAPI_MakeCylinder.hxx>
31 #include <BRep_Tool.hxx>
32
33 #include <TopAbs.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopExp.hxx>
38
39 #include <Standard_TypeMismatch.hxx>
40 #include <Standard_NullObject.hxx>
41 #include <StdFail_NotDone.hxx>
42 #include <Precision.hxx>
43 #include <gp_Pnt.hxx>
44 #include <gp.hxx>
45
46 //=======================================================================
47 //function : GetID
48 //purpose  :
49 //=======================================================================
50 const Standard_GUID& GEOMImpl_ConeDriver::GetID()
51 {
52   static Standard_GUID aConeDriver("FF1BBB15-5D14-4df2-980B-3A668264EA16");
53   return aConeDriver;
54 }
55
56
57 //=======================================================================
58 //function : GEOMImpl_ConeDriver
59 //purpose  :
60 //=======================================================================
61 GEOMImpl_ConeDriver::GEOMImpl_ConeDriver()
62 {
63 }
64
65 //=======================================================================
66 //function : Execute
67 //purpose  :
68 //=======================================================================
69 Standard_Integer GEOMImpl_ConeDriver::Execute(TFunction_Logbook& log) const
70 {
71   if (Label().IsNull()) return 0;
72   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
73
74   GEOMImpl_ICone aCI (aFunction);
75   Standard_Integer aType = aFunction->GetType();
76
77   gp_Pnt aP;
78   gp_Vec aV;
79
80   Standard_Real aR1 = aCI.GetR1();
81   Standard_Real aR2 = aCI.GetR2();
82
83   if (aType == CONE_R1_R2_H) {
84     aP = gp::Origin();
85     aV = gp::DZ();
86
87   } else if (aType == CONE_PNT_VEC_R1_R2_H) {
88     Handle(GEOM_Function) aRefPoint  = aCI.GetPoint();
89     Handle(GEOM_Function) aRefVector = aCI.GetVector();
90     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
91     TopoDS_Shape aShapeVec = aRefVector->GetValue();
92     if (aShapePnt.IsNull() || aShapeVec.IsNull()) {
93       Standard_NullObject::Raise
94         ("Cone creation aborted: point or vector is not defined");
95     }
96     if (aShapePnt.ShapeType() != TopAbs_VERTEX ||
97         aShapeVec.ShapeType() != TopAbs_EDGE) {
98       Standard_TypeMismatch::Raise
99         ("Cone creation aborted: point or vector shapes has wrong type");
100     }
101
102     aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
103
104     TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
105     TopoDS_Vertex V1, V2;
106     TopExp::Vertices(anE, V1, V2, Standard_True);
107     if (V1.IsNull() || V2.IsNull()) {
108       Standard_NullObject::Raise
109         ("Cylinder creation aborted: vector is not defined");
110     }
111     aV = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
112
113   } else {
114     return 0;
115   }
116
117   if (aCI.GetH() < 0.0) aV.Reverse();
118   gp_Ax2 anAxes (aP, aV);
119
120   TopoDS_Shape aShape;
121   // Cone does not work if same radius
122   if (fabs(aR1 - aR2) <= Precision::Confusion()) {
123     BRepPrimAPI_MakeCylinder MC (anAxes, (aR1 + aR2)/2.0, Abs(aCI.GetH()));
124     MC.Build();
125     if (!MC.IsDone()) {
126       StdFail_NotDone::Raise("Cylinder can't be computed from the given parameters");
127     }
128     aShape = MC.Shape();
129   } else {
130     BRepPrimAPI_MakeCone MC (anAxes, aCI.GetR1(), aCI.GetR2(), Abs(aCI.GetH()));
131     MC.Build();
132     if (!MC.IsDone()) {
133       StdFail_NotDone::Raise("Cylinder can't be computed from the given parameters");
134     }
135     aShape = MC.Shape();
136   }
137   if (aShape.IsNull()) return 0;
138
139   log.SetTouched(Label());
140
141   aFunction->SetValue(aShape);
142   return 1;
143 }
144
145
146 //=======================================================================
147 //function :  GEOMImpl_ConeDriver_Type_
148 //purpose  :
149 //=======================================================================
150 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ConeDriver_Type_()
151 {
152
153   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
154   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
155   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
156   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
157   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
158   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
159
160
161   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
162   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ConeDriver",
163                                                          sizeof(GEOMImpl_ConeDriver),
164                                                          1,
165                                                          (Standard_Address)_Ancestors,
166                                                          (Standard_Address)NULL);
167
168   return _aType;
169 }
170
171 //=======================================================================
172 //function : DownCast
173 //purpose  :
174 //=======================================================================
175 const Handle(GEOMImpl_ConeDriver) Handle(GEOMImpl_ConeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
176 {
177   Handle(GEOMImpl_ConeDriver) _anOtherObject;
178
179   if (!AnObject.IsNull()) {
180      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ConeDriver))) {
181        _anOtherObject = Handle(GEOMImpl_ConeDriver)((Handle(GEOMImpl_ConeDriver)&)AnObject);
182      }
183   }
184
185   return _anOtherObject ;
186 }