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