Salome HOME
17b93936cd25a6ea3cf65fb133c6bf3e97bda46b
[modules/geom.git] / src / GEOMImpl / GEOMImpl_TorusDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_TorusDriver.hxx"
4 #include "GEOMImpl_ITorus.hxx"
5 #include "GEOMImpl_Types.hxx"
6 #include "GEOM_Function.hxx"
7
8 #include <BRepPrimAPI_MakeTorus.hxx>
9 #include <BRep_Tool.hxx>
10 #include <TopoDS.hxx>
11 #include <TopoDS_Shape.hxx>
12 #include <TopoDS_Edge.hxx>
13 #include <TopoDS_Vertex.hxx>
14 #include <TopAbs.hxx>
15 #include <TopExp.hxx>
16
17 #include <gp_Pnt.hxx>
18 #include <Precision.hxx>
19 #include <StdFail_NotDone.hxx>
20 #include <Standard_TypeMismatch.hxx>
21 #include <Standard_ConstructionError.hxx>
22
23 //=======================================================================
24 //function : GetID
25 //purpose  :
26 //======================================================================= 
27 const Standard_GUID& GEOMImpl_TorusDriver::GetID()
28 {
29   static Standard_GUID aTorusDriver("FF1BBB12-5D14-4df2-980B-3A668264EA16");
30   return aTorusDriver; 
31 }
32
33
34 //=======================================================================
35 //function : GEOMImpl_TorusDriver
36 //purpose  : 
37 //=======================================================================
38 GEOMImpl_TorusDriver::GEOMImpl_TorusDriver() 
39 {
40 }
41
42 //=======================================================================
43 //function : Execute
44 //purpose  :
45 //======================================================================= 
46 Standard_Integer GEOMImpl_TorusDriver::Execute(TFunction_Logbook& log) const
47 {
48   if (Label().IsNull()) return 0;    
49   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
50
51   GEOMImpl_ITorus aCI (aFunction);
52   Standard_Integer aType = aFunction->GetType();
53
54   TopoDS_Shape aShape;
55
56   if (aType == TORUS_RR) {
57     aShape = BRepPrimAPI_MakeTorus(aCI.GetRMajor(), aCI.GetRMinor()).Shape();
58
59   } else if (aType == TORUS_PNT_VEC_RR) {
60     Handle(GEOM_Function) aRefPoint  = aCI.GetCenter();
61     Handle(GEOM_Function) aRefVector = aCI.GetVector();
62     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
63     TopoDS_Shape aShapeVec = aRefVector->GetValue();
64     if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
65       Standard_TypeMismatch::Raise("Torus Center must be a vertex");
66     }
67     if (aShapeVec.ShapeType() != TopAbs_EDGE) {
68       Standard_TypeMismatch::Raise("Torus Axis must be an edge");
69     }
70
71     gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
72     TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
73     TopoDS_Vertex V1, V2;
74     TopExp::Vertices(anE, V1, V2, Standard_True);
75     if (V1.IsNull() || V2.IsNull()) {
76       Standard_ConstructionError::Raise("Bad edge for the Torus Axis given");
77     }
78
79     gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
80     if (aV.Magnitude() < Precision::Confusion()) {
81       Standard_ConstructionError::Raise
82         ("End vertices of edge, defining the Torus Axis, are too close");
83     }
84
85     gp_Ax2 anAxes (aP, aV);
86     BRepPrimAPI_MakeTorus MT (anAxes, aCI.GetRMajor(), aCI.GetRMinor());
87     if (!MT.IsDone()) MT.Build();
88     if (!MT.IsDone()) StdFail_NotDone::Raise("Torus construction algorithm has failed");
89     aShape = MT.Shape();
90   } else {
91   }
92
93   if (aShape.IsNull()) return 0;
94   aFunction->SetValue(aShape);
95
96   log.SetTouched(Label()); 
97
98   return 1;    
99 }
100
101
102 //=======================================================================
103 //function :  GEOMImpl_TorusDriver_Type_
104 //purpose  :
105 //======================================================================= 
106 Standard_EXPORT Handle_Standard_Type& GEOMImpl_TorusDriver_Type_()
107 {
108
109   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
110   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
111   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
112   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
113   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
114   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
115  
116
117   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
118   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_TorusDriver",
119                                                          sizeof(GEOMImpl_TorusDriver),
120                                                          1,
121                                                          (Standard_Address)_Ancestors,
122                                                          (Standard_Address)NULL);
123
124   return _aType;
125 }
126
127 //=======================================================================
128 //function : DownCast
129 //purpose  :
130 //======================================================================= 
131 const Handle(GEOMImpl_TorusDriver) Handle(GEOMImpl_TorusDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
132 {
133   Handle(GEOMImpl_TorusDriver) _anOtherObject;
134
135   if (!AnObject.IsNull()) {
136      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_TorusDriver))) {
137        _anOtherObject = Handle(GEOMImpl_TorusDriver)((Handle(GEOMImpl_TorusDriver)&)AnObject);
138      }
139   }
140
141   return _anOtherObject ;
142 }