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