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