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