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