Salome HOME
f252716aa7556cba65c818998f389cc91d54ac4f
[modules/geom.git] / src / GEOMImpl / GEOMImpl_RotateDriver.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_RotateDriver.hxx>
24 #include <GEOMImpl_IRotate.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27 #include <gp_Trsf.hxx>
28 #include <gp_Pnt.hxx>
29 #include <gp_Vec.hxx>
30 #include <gp_Dir.hxx>
31 #include <gp_Ax1.hxx>
32 #include <BRepBuilderAPI_Transform.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Compound.hxx>
37 #include <TopAbs.hxx>
38 #include <TopExp.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <BRep_Tool.hxx>
42 #include <BRep_Builder.hxx>
43 #include <GeomAPI_ProjectPointOnCurve.hxx>
44 #include <Geom_Line.hxx>
45 #include <GProp_GProps.hxx>
46 #include <BRepGProp.hxx>
47 #include <Precision.hxx>
48
49 //=======================================================================
50 //function : GetID
51 //purpose  :
52 //======================================================================= 
53 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
54 {
55   static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
56   return aRotateDriver; 
57 }
58
59
60 //=======================================================================
61 //function : GEOMImpl_RotateDriver
62 //purpose  : 
63 //=======================================================================
64
65 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver() 
66 {
67 }
68
69 //=======================================================================
70 //function : Execute
71 //purpose  :
72 //======================================================================= 
73 Standard_Integer GEOMImpl_RotateDriver::Execute(TFunction_Logbook& log) const
74 {
75   if(Label().IsNull())  return 0;    
76   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
77
78   if(aFunction.IsNull()) return 0;
79
80   GEOMImpl_IRotate RI(aFunction);
81   gp_Trsf aTrsf;
82   Standard_Integer aType = aFunction->GetType();
83   Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
84   if(anOriginalFunction.IsNull()) return 0;
85   TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
86   if(anOriginal.IsNull()) return 0;
87
88   if(aType == ROTATE || aType == ROTATE_COPY) {
89     Handle(GEOM_Function) anAxis = RI.GetAxis();
90     if(anAxis.IsNull()) return 0;
91     TopoDS_Shape A = anAxis->GetValue();
92     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
93     TopoDS_Edge anEdge = TopoDS::Edge(A);
94
95     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
96     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
97     gp_Dir aDir(gp_Vec(aP1, aP2));
98     gp_Ax1 anAx1(aP1, aDir);
99     Standard_Real anAngle = RI.GetAngle();
100     aTrsf.SetRotation(anAx1, anAngle);
101     
102     BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
103     aShape = aTransformation.Shape();
104   }
105   else if(aType == ROTATE_1D) {
106     //Get direction
107     Handle(GEOM_Function) anAxis = RI.GetAxis();
108     if(anAxis.IsNull()) return 0;
109     TopoDS_Shape A = anAxis->GetValue();
110     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
111     TopoDS_Edge anEdge = TopoDS::Edge(A);
112
113     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
114     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
115     gp_Dir D(gp_Vec(aP1, aP2)) ;
116
117     gp_Ax1 AX1(aP1, D) ;
118
119     Standard_Integer nbtimes = RI.GetNbIter1();
120     Standard_Real angle = 360.0/nbtimes ;
121
122     TopoDS_Compound aCompound;
123     BRep_Builder B;
124     B.MakeCompound( aCompound );
125     
126     for (int i = 0; i < nbtimes; i++ ) {
127       aTrsf.SetRotation(AX1, i*angle*PI180) ;
128       BRepBuilderAPI_Transform myBRepTransformation(anOriginal, aTrsf, Standard_False) ;
129       B.Add( aCompound, myBRepTransformation.Shape() );
130     }
131
132     aShape = aCompound ;
133   }
134   else if(aType == ROTATE_2D) {
135     Standard_Real DX, DY, DZ ;
136
137     //Get direction
138     Handle(GEOM_Function) anAxis = RI.GetAxis();
139     if(anAxis.IsNull()) return 0;
140     TopoDS_Shape A = anAxis->GetValue();
141     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
142     TopoDS_Edge anEdge = TopoDS::Edge(A);
143     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
144     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
145     gp_Dir D(gp_Vec(aP1, aP2)) ;
146
147     gp_Ax1 AX1(aP1, D) ;
148
149
150     gp_Trsf theTransformation1 ;
151     gp_Trsf theTransformation2 ;
152     gp_Pnt P1 ;
153     GProp_GProps System ;
154     
155     if ( anOriginal.ShapeType() == TopAbs_VERTEX) {
156       P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
157     } 
158     else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
159       BRepGProp::LinearProperties(anOriginal, System);
160       P1 = System.CentreOfMass() ;
161     }
162     else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
163       BRepGProp::SurfaceProperties(anOriginal, System);
164       P1 = System.CentreOfMass() ;
165     }
166     else {
167       BRepGProp::VolumeProperties(anOriginal, System);
168       P1 = System.CentreOfMass() ;
169     }
170     
171     Handle(Geom_Line) Line = new Geom_Line(AX1);
172     GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line ) ;
173     gp_Pnt P2 = aPrjTool.NearestPoint();
174     
175     if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
176     
177     gp_Vec Vec(P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z()) ;
178     Vec.Normalize();
179     
180     Standard_Integer nbtimes2 = RI.GetNbIter2();
181     Standard_Integer nbtimes1 = RI.GetNbIter1();
182     Standard_Real step = RI.GetStep();
183     Standard_Real ang = RI.GetAngle();
184
185     gp_Vec myVec ;
186     TopoDS_Compound aCompound;
187     BRep_Builder B;
188     B.MakeCompound( aCompound );
189     for (int i = 0; i < nbtimes2; i++ ) {
190       for (int j = 0; j < nbtimes1; j++ ) {
191         DX = i * step * Vec.X() ;
192         DY = i * step * Vec.Y() ;
193         DZ = i * step * Vec.Z() ;
194         myVec.SetCoord( DX, DY, DZ ) ;
195         theTransformation1.SetTranslation(myVec) ;
196         theTransformation2.SetRotation(AX1, j*ang*PI180) ;
197         BRepBuilderAPI_Transform myBRepTransformation1(anOriginal, theTransformation1, Standard_False) ;
198         BRepBuilderAPI_Transform myBRepTransformation2(myBRepTransformation1.Shape(), theTransformation2, Standard_False) ;
199         B.Add( aCompound, myBRepTransformation2.Shape() );
200       }
201     }
202
203     aShape = aCompound;
204
205   }
206   else return 0;
207
208  
209
210
211   if (aShape.IsNull()) return 0;
212
213   aFunction->SetValue(aShape);
214
215   log.SetTouched(Label()); 
216
217   return 1;
218 }
219
220
221 //=======================================================================
222 //function :  GEOMImpl_RotateDriver_Type_
223 //purpose  :
224 //======================================================================= 
225 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
226 {
227
228   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
229   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
230   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
231   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
232   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
233   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
234  
235
236   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
237   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
238                                                          sizeof(GEOMImpl_RotateDriver),
239                                                          1,
240                                                          (Standard_Address)_Ancestors,
241                                                          (Standard_Address)NULL);
242
243   return _aType;
244 }
245
246 //=======================================================================
247 //function : DownCast
248 //purpose  :
249 //======================================================================= 
250
251 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
252 {
253   Handle(GEOMImpl_RotateDriver) _anOtherObject;
254
255   if (!AnObject.IsNull()) {
256      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
257        _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
258      }
259   }
260
261   return _anOtherObject ;
262 }
263
264