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