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