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