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