Salome HOME
if a shape to be rotated is a vertex, and it is located ON the Revolution Axis, then...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_RevolutionDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_RevolutionDriver.hxx"
4 #include "GEOMImpl_IRevolution.hxx"
5 #include "GEOMImpl_Types.hxx"
6 #include "GEOM_Function.hxx"
7
8 #include <BRepPrimAPI_MakeRevol.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 <gp_Lin.hxx>
19 #include <gp_Dir.hxx>
20 #include <Precision.hxx>
21 #include <StdFail_NotDone.hxx>
22 #include <Standard_TypeMismatch.hxx>
23 #include <Standard_ConstructionError.hxx>
24
25 //=======================================================================
26 //function : GetID
27 //purpose  :
28 //======================================================================= 
29 const Standard_GUID& GEOMImpl_RevolutionDriver::GetID()
30 {
31   static Standard_GUID aRevolutionDriver("FF1BBB18-5D14-4df2-980B-3A668264EA16");
32   return aRevolutionDriver; 
33 }
34
35
36 //=======================================================================
37 //function : GEOMImpl_RevolutionDriver
38 //purpose  : 
39 //=======================================================================
40 GEOMImpl_RevolutionDriver::GEOMImpl_RevolutionDriver() 
41 {
42 }
43
44 //=======================================================================
45 //function : Execute
46 //purpose  :
47 //======================================================================= 
48 Standard_Integer GEOMImpl_RevolutionDriver::Execute(TFunction_Logbook& log) const
49 {
50   if (Label().IsNull()) return 0;    
51   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
52
53   GEOMImpl_IRevolution aCI (aFunction);
54   Standard_Integer aType = aFunction->GetType();
55
56   TopoDS_Shape aShape;
57
58   if (aType == REVOLUTION_BASE_AXIS_ANGLE) {
59     Handle(GEOM_Function) aRefBase = aCI.GetBase();
60     Handle(GEOM_Function) aRefAxis = aCI.GetAxis();
61     TopoDS_Shape aShapeBase = aRefBase->GetValue();
62     TopoDS_Shape aShapeAxis = aRefAxis->GetValue();
63     if (aShapeAxis.ShapeType() != TopAbs_EDGE) {
64       Standard_TypeMismatch::Raise("Revolution Axis must be an edge");
65     }
66
67     TopoDS_Edge anE = TopoDS::Edge(aShapeAxis);
68     TopoDS_Vertex V1, V2;
69     TopExp::Vertices(anE, V1, V2, Standard_True);
70     if (V1.IsNull() || V2.IsNull()) {
71       Standard_ConstructionError::Raise("Bad edge for the Revolution Axis given");
72     }
73
74     gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
75     if (aV.Magnitude() < Precision::Confusion()) {
76       Standard_ConstructionError::Raise
77         ("End vertices of edge, defining the Revolution Axis, are too close");
78     }
79
80     if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
81       gp_Lin aL(BRep_Tool::Pnt(V1), gp_Dir(aV));
82       Standard_Real d = aL.Distance(BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase)));
83       if (d < Precision::Confusion()) {
84         Standard_ConstructionError::Raise("Vertex to be rotated is too close to Revolution Axis");
85       }
86     }
87
88     gp_Ax1 anAxis (BRep_Tool::Pnt(V1), aV);
89     BRepPrimAPI_MakeRevol MR (aShapeBase, anAxis, aCI.GetAngle(), Standard_False);
90     if (!MR.IsDone()) MR.Build();
91     if (!MR.IsDone()) StdFail_NotDone::Raise("Revolution algorithm has failed");
92     aShape = MR.Shape();
93   } else {
94   }
95
96   if (aShape.IsNull()) return 0;
97   aFunction->SetValue(aShape);
98
99   log.SetTouched(Label()); 
100
101   return 1;    
102 }
103
104
105 //=======================================================================
106 //function :  GEOMImpl_RevolutionDriver_Type_
107 //purpose  :
108 //======================================================================= 
109 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RevolutionDriver_Type_()
110 {
111
112   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
113   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
114   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
115   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
116   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
117   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
118  
119
120   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
121   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RevolutionDriver",
122                                                          sizeof(GEOMImpl_RevolutionDriver),
123                                                          1,
124                                                          (Standard_Address)_Ancestors,
125                                                          (Standard_Address)NULL);
126
127   return _aType;
128 }
129
130 //=======================================================================
131 //function : DownCast
132 //purpose  :
133 //======================================================================= 
134 const Handle(GEOMImpl_RevolutionDriver) Handle(GEOMImpl_RevolutionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
135 {
136   Handle(GEOMImpl_RevolutionDriver) _anOtherObject;
137
138   if (!AnObject.IsNull()) {
139      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RevolutionDriver))) {
140        _anOtherObject = Handle(GEOMImpl_RevolutionDriver)((Handle(GEOMImpl_RevolutionDriver)&)AnObject);
141      }
142   }
143
144   return _anOtherObject ;
145 }