Salome HOME
Updated copyright comment
[modules/geom.git] / src / GEOMImpl / GEOMImpl_RevolutionDriver.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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, or (at your option) any later version.
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
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_RevolutionDriver.hxx>
26
27 #include <GEOMImpl_IRevolution.hxx>
28 #include <GEOMImpl_Types.hxx>
29
30 #include <GEOM_Function.hxx>
31
32 #include <GEOMUtils.hxx>
33
34 #include <BRepPrimAPI_MakeRevol.hxx>
35 #include <BRepBuilderAPI_Transform.hxx>
36 #include <BRep_Tool.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopAbs.hxx>
42 #include <TopExp.hxx>
43 #include <gp_Trsf.hxx>
44 #include <gp_Pnt.hxx>
45 #include <gp_Lin.hxx>
46 #include <gp_Dir.hxx>
47 #include <Precision.hxx>
48 #include <StdFail_NotDone.hxx>
49 #include <Standard_TypeMismatch.hxx>
50 #include <Standard_ConstructionError.hxx>
51
52 //=======================================================================
53 //function : GetID
54 //purpose  :
55 //======================================================================= 
56 const Standard_GUID& GEOMImpl_RevolutionDriver::GetID()
57 {
58   static Standard_GUID aRevolutionDriver("FF1BBB18-5D14-4df2-980B-3A668264EA16");
59   return aRevolutionDriver; 
60 }
61
62
63 //=======================================================================
64 //function : GEOMImpl_RevolutionDriver
65 //purpose  : 
66 //=======================================================================
67 GEOMImpl_RevolutionDriver::GEOMImpl_RevolutionDriver() 
68 {
69 }
70
71 //=======================================================================
72 //function : Execute
73 //purpose  :
74 //======================================================================= 
75 Standard_Integer GEOMImpl_RevolutionDriver::Execute(Handle(TFunction_Logbook)& log) const
76 {
77   if (Label().IsNull()) return 0;    
78   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
79
80   GEOMImpl_IRevolution aCI (aFunction);
81   Standard_Integer aType = aFunction->GetType();
82
83   TopoDS_Shape aShape;
84
85   if (aType == REVOLUTION_BASE_AXIS_ANGLE || aType == REVOLUTION_BASE_AXIS_ANGLE_2WAYS) {
86     Handle(GEOM_Function) aRefBase = aCI.GetBase();
87     Handle(GEOM_Function) aRefAxis = aCI.GetAxis();
88     TopoDS_Shape aShapeBase = aRefBase->GetValue();
89     TopoDS_Shape aShapeAxis = aRefAxis->GetValue();
90     if (aShapeAxis.ShapeType() != TopAbs_EDGE) {
91       Standard_TypeMismatch::Raise("Revolution Axis must be an edge");
92     }
93
94     TopoDS_Edge anE = TopoDS::Edge(aShapeAxis);
95     TopoDS_Vertex V1, V2;
96     TopExp::Vertices(anE, V1, V2, Standard_True);
97     if (V1.IsNull() || V2.IsNull()) {
98       Standard_ConstructionError::Raise("Bad edge for the Revolution Axis given");
99     }
100
101     gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
102     if (aV.Magnitude() < Precision::Confusion()) {
103       Standard_ConstructionError::Raise
104         ("End vertices of edge, defining the Revolution Axis, are too close");
105     }
106
107     if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
108       gp_Lin aL(BRep_Tool::Pnt(V1), gp_Dir(aV));
109       Standard_Real d = aL.Distance(BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase)));
110       if (d < Precision::Confusion()) {
111         Standard_ConstructionError::Raise("Vertex to be rotated is too close to Revolution Axis");
112       }
113     }
114     double anAngle = aCI.GetAngle();
115     gp_Ax1 anAxis (BRep_Tool::Pnt(V1), aV);
116     if (aType == REVOLUTION_BASE_AXIS_ANGLE_2WAYS)
117       {
118         gp_Trsf aTrsf;
119         aTrsf.SetRotation(anAxis, ( -anAngle ));
120         BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
121         aShapeBase = aTransformation.Shape();
122         anAngle = anAngle * 2;
123       }
124     BRepPrimAPI_MakeRevol MR (aShapeBase, anAxis, anAngle, Standard_False);
125     if (!MR.IsDone()) MR.Build();
126     if (!MR.IsDone()) StdFail_NotDone::Raise("Revolution algorithm has failed");
127     aShape = MR.Shape();
128   } else {
129   }
130
131   if (aShape.IsNull()) return 0;
132
133   TopoDS_Shape aRes = GEOMUtils::CompsolidToCompound(aShape);
134   aFunction->SetValue(aRes);
135
136   log->SetTouched(Label());
137
138   return 1;
139 }
140
141 //================================================================================
142 /*!
143  * \brief Returns a name of creation operation and names and values of creation parameters
144  */
145 //================================================================================
146
147 bool GEOMImpl_RevolutionDriver::
148 GetCreationInformation(std::string&             theOperationName,
149                        std::vector<GEOM_Param>& theParams)
150 {
151   if (Label().IsNull()) return 0;
152   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
153
154   GEOMImpl_IRevolution aCI( function );
155   Standard_Integer aType = function->GetType();
156
157   theOperationName = "REVOLUTION";
158
159   switch ( aType ) {
160   case REVOLUTION_BASE_AXIS_ANGLE:
161   case REVOLUTION_BASE_AXIS_ANGLE_2WAYS:
162     AddParam( theParams, "Object", aCI.GetBase() );
163     AddParam( theParams, "Axis", aCI.GetAxis() );
164     AddParam( theParams, "Angle", aCI.GetAngle() );
165     AddParam( theParams, "Both Directions", aType == REVOLUTION_BASE_AXIS_ANGLE_2WAYS );
166     break;
167   default:
168     return false;
169   }
170   
171   return true;
172 }
173
174 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_RevolutionDriver,GEOM_BaseDriver)