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