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