Salome HOME
beec64f3c3387041c585ace91401777cc74d91f3
[modules/geom.git] / src / GEOMImpl / GEOMImpl_RevolutionDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_RevolutionDriver.hxx>
24 #include <GEOMImpl_IRevolution.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRepPrimAPI_MakeRevol.hxx>
29 #include <BRep_Tool.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopAbs.hxx>
35 #include <TopExp.hxx>
36
37 #include <gp_Pnt.hxx>
38 #include <gp_Lin.hxx>
39 #include <gp_Dir.hxx>
40 #include <Precision.hxx>
41 #include <StdFail_NotDone.hxx>
42 #include <Standard_TypeMismatch.hxx>
43 #include <Standard_ConstructionError.hxx>
44
45 //=======================================================================
46 //function : GetID
47 //purpose  :
48 //======================================================================= 
49 const Standard_GUID& GEOMImpl_RevolutionDriver::GetID()
50 {
51   static Standard_GUID aRevolutionDriver("FF1BBB18-5D14-4df2-980B-3A668264EA16");
52   return aRevolutionDriver; 
53 }
54
55
56 //=======================================================================
57 //function : GEOMImpl_RevolutionDriver
58 //purpose  : 
59 //=======================================================================
60 GEOMImpl_RevolutionDriver::GEOMImpl_RevolutionDriver() 
61 {
62 }
63
64 //=======================================================================
65 //function : Execute
66 //purpose  :
67 //======================================================================= 
68 Standard_Integer GEOMImpl_RevolutionDriver::Execute(TFunction_Logbook& log) const
69 {
70   if (Label().IsNull()) return 0;    
71   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
72
73   GEOMImpl_IRevolution aCI (aFunction);
74   Standard_Integer aType = aFunction->GetType();
75
76   TopoDS_Shape aShape;
77
78   if (aType == REVOLUTION_BASE_AXIS_ANGLE) {
79     Handle(GEOM_Function) aRefBase = aCI.GetBase();
80     Handle(GEOM_Function) aRefAxis = aCI.GetAxis();
81     TopoDS_Shape aShapeBase = aRefBase->GetValue();
82     TopoDS_Shape aShapeAxis = aRefAxis->GetValue();
83     if (aShapeAxis.ShapeType() != TopAbs_EDGE) {
84       Standard_TypeMismatch::Raise("Revolution Axis must be an edge");
85     }
86
87     TopoDS_Edge anE = TopoDS::Edge(aShapeAxis);
88     TopoDS_Vertex V1, V2;
89     TopExp::Vertices(anE, V1, V2, Standard_True);
90     if (V1.IsNull() || V2.IsNull()) {
91       Standard_ConstructionError::Raise("Bad edge for the Revolution Axis given");
92     }
93
94     gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
95     if (aV.Magnitude() < Precision::Confusion()) {
96       Standard_ConstructionError::Raise
97         ("End vertices of edge, defining the Revolution Axis, are too close");
98     }
99
100     if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
101       gp_Lin aL(BRep_Tool::Pnt(V1), gp_Dir(aV));
102       Standard_Real d = aL.Distance(BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase)));
103       if (d < Precision::Confusion()) {
104         Standard_ConstructionError::Raise("Vertex to be rotated is too close to Revolution Axis");
105       }
106     }
107
108     gp_Ax1 anAxis (BRep_Tool::Pnt(V1), aV);
109     BRepPrimAPI_MakeRevol MR (aShapeBase, anAxis, aCI.GetAngle(), Standard_False);
110     if (!MR.IsDone()) MR.Build();
111     if (!MR.IsDone()) StdFail_NotDone::Raise("Revolution algorithm has failed");
112     aShape = MR.Shape();
113   } else {
114   }
115
116   if (aShape.IsNull()) return 0;
117   aFunction->SetValue(aShape);
118
119   log.SetTouched(Label()); 
120
121   return 1;    
122 }
123
124
125 //=======================================================================
126 //function :  GEOMImpl_RevolutionDriver_Type_
127 //purpose  :
128 //======================================================================= 
129 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RevolutionDriver_Type_()
130 {
131
132   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
133   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
134   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
135   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
136   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
137   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
138  
139
140   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
141   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RevolutionDriver",
142                                                          sizeof(GEOMImpl_RevolutionDriver),
143                                                          1,
144                                                          (Standard_Address)_Ancestors,
145                                                          (Standard_Address)NULL);
146
147   return _aType;
148 }
149
150 //=======================================================================
151 //function : DownCast
152 //purpose  :
153 //======================================================================= 
154 const Handle(GEOMImpl_RevolutionDriver) Handle(GEOMImpl_RevolutionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
155 {
156   Handle(GEOMImpl_RevolutionDriver) _anOtherObject;
157
158   if (!AnObject.IsNull()) {
159      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RevolutionDriver))) {
160        _anOtherObject = Handle(GEOMImpl_RevolutionDriver)((Handle(GEOMImpl_RevolutionDriver)&)AnObject);
161      }
162   }
163
164   return _anOtherObject ;
165 }