]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_CylinderDriver.cxx
Salome HOME
Add an Angle option in the cylinder primitive in order to build portion of cylinders...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_CylinderDriver.cxx
1 // Copyright (C) 2007-2014  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_CylinderDriver.hxx>
26 #include <GEOMImpl_ICylinder.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
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 <gp_Pnt.hxx>
43 #include <gp.hxx>
44
45 //=======================================================================
46 //function : GetID
47 //purpose  :
48 //=======================================================================
49 const Standard_GUID& GEOMImpl_CylinderDriver::GetID()
50 {
51   static Standard_GUID aCylinderDriver("FF1BBB14-5D14-4df2-980B-3A668264EA16");
52   return aCylinderDriver;
53 }
54
55
56 //=======================================================================
57 //function : GEOMImpl_CylinderDriver
58 //purpose  :
59 //=======================================================================
60 GEOMImpl_CylinderDriver::GEOMImpl_CylinderDriver()
61 {
62 }
63
64 //=======================================================================
65 //function : Execute
66 //purpose  :
67 //=======================================================================
68 Standard_Integer GEOMImpl_CylinderDriver::Execute(TFunction_Logbook& log) const
69 {
70   if (Label().IsNull()) return 0;
71   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
72
73   GEOMImpl_ICylinder aCI (aFunction);
74   Standard_Integer aType = aFunction->GetType();
75
76   gp_Pnt aP;
77   gp_Vec aV;
78   
79   TopoDS_Shape aShape;
80
81   if (aType == CYLINDER_R_H) {
82     aP = gp::Origin();
83     aV = gp::DZ();
84   }
85   else if (aType == CYLINDER_PNT_VEC_R_H) {
86     Handle(GEOM_Function) aRefPoint  = aCI.GetPoint();
87     Handle(GEOM_Function) aRefVector = aCI.GetVector();
88     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
89     TopoDS_Shape aShapeVec = aRefVector->GetValue();
90     if (aShapePnt.IsNull() || aShapeVec.IsNull()) {
91       Standard_NullObject::Raise("Cylinder creation aborted: point or vector is not defined");
92     }
93     if (aShapePnt.ShapeType() != TopAbs_VERTEX ||
94         aShapeVec.ShapeType() != TopAbs_EDGE) {
95       Standard_TypeMismatch::Raise("Cylinder creation aborted: point or vector shapes has wrong type");
96     }
97
98     aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
99
100     TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
101     TopoDS_Vertex V1, V2;
102     TopExp::Vertices(anE, V1, V2, Standard_True);
103     if (V1.IsNull() || V2.IsNull()) {
104       Standard_NullObject::Raise("Cylinder creation aborted: vector is not defined");
105     }
106     aV = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
107   }
108   else {
109     return 0;
110   }
111
112   if (aCI.GetH() < 0.0) aV.Reverse();
113   gp_Ax2 anAxes (aP, aV);
114   aShape = BRepPrimAPI_MakeCylinder(anAxes, aCI.GetR(), Abs(aCI.GetH())).Shape();
115   if(aCI.GetA() < 360. && aCI.GetA()> 0.){
116     BRepPrimAPI_MakeCylinder MC(anAxes, aCI.GetR(), Abs(aCI.GetH()), aCI.GetA()*M_PI/180.);
117     MC.Build();
118     if (!MC.IsDone()) {
119       StdFail_NotDone::Raise("Cylinder can't be computed from the given parameters");
120     }
121     aShape = MC.Shape();        
122   }    
123   if (aShape.IsNull()) return 0;
124
125   aFunction->SetValue(aShape);
126
127   log.SetTouched(Label());
128
129   return 1;
130 }
131
132 //================================================================================
133 /*!
134  * \brief Returns a name of creation operation and names and values of creation parameters
135  */
136 //================================================================================
137
138 bool GEOMImpl_CylinderDriver::
139 GetCreationInformation(std::string&             theOperationName,
140                        std::vector<GEOM_Param>& theParams)
141 {
142   if (Label().IsNull()) return 0;
143   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
144
145   GEOMImpl_ICylinder aCI( function );
146   Standard_Integer aType = function->GetType();
147
148   theOperationName = "CYLINDER";
149
150   switch ( aType ) {
151   case CYLINDER_R_H:
152     AddParam( theParams, "Radius", aCI.GetR() );
153     AddParam( theParams, "Height", aCI.GetH() );
154     break;
155   case CONE_PNT_VEC_R1_R2_H:
156     AddParam( theParams, "Base Point", aCI.GetPoint() );
157     AddParam( theParams, "Vector", aCI.GetVector() );
158     AddParam( theParams, "Radius", aCI.GetR() );
159     AddParam( theParams, "Height", aCI.GetH() );
160     break;
161   default:
162     return false;
163   }
164   
165   return true;
166 }
167
168 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_CylinderDriver,GEOM_BaseDriver);
169
170 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_CylinderDriver,GEOM_BaseDriver);