Salome HOME
NPAL17269: Performance pb. when creating a group with GUI.
[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   gp_Pnt aCP, aP1, aP2;
83   Standard_Integer aType = aFunction->GetType();
84   Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
85   if(anOriginalFunction.IsNull()) return 0;
86   TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
87   if(anOriginal.IsNull()) return 0;
88
89   if(aType == ROTATE || aType == ROTATE_COPY) {
90     Handle(GEOM_Function) anAxis = RI.GetAxis();
91     if(anAxis.IsNull()) return 0;
92     TopoDS_Shape A = anAxis->GetValue();
93     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
94     TopoDS_Edge anEdge = TopoDS::Edge(A);
95
96     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
97     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
98     gp_Dir aDir(gp_Vec(aP1, aP2));
99     gp_Ax1 anAx1(aP1, aDir);
100     Standard_Real anAngle = RI.GetAngle();
101     aTrsf.SetRotation(anAx1, anAngle);
102     
103     BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
104     aShape = aTransformation.Shape();
105   }
106   else if(aType ==  ROTATE_THREE_POINTS || aType == ROTATE_THREE_POINTS_COPY) {
107     Handle(GEOM_Function) aCentPoint = RI.GetCentPoint();
108     Handle(GEOM_Function) aPoint1 = RI.GetPoint1();
109     Handle(GEOM_Function) aPoint2 = RI.GetPoint2();
110     if(aCentPoint.IsNull() || aPoint1.IsNull() || aPoint2.IsNull()) return 0;
111     TopoDS_Shape aCV = aCentPoint->GetValue();
112     TopoDS_Shape aV1 = aPoint1->GetValue();
113     TopoDS_Shape aV2 = aPoint2->GetValue();
114     if(aCV.IsNull() || aCV.ShapeType() != TopAbs_VERTEX) return 0;
115     if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
116     if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
117
118     aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
119     aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
120     aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
121
122     gp_Vec aVec1(aCP, aP1);
123     gp_Vec aVec2(aCP, aP2);
124     gp_Dir aDir(aVec1 ^ aVec2);
125     gp_Ax1 anAx1(aCP, aDir);
126     Standard_Real anAngle = aVec1.Angle(aVec2);
127     aTrsf.SetRotation(anAx1, anAngle);
128     BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
129     aShape = aTransformation.Shape();
130   }
131   else if(aType == ROTATE_1D) {
132     //Get direction
133     Handle(GEOM_Function) anAxis = RI.GetAxis();
134     if(anAxis.IsNull()) return 0;
135     TopoDS_Shape A = anAxis->GetValue();
136     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
137     TopoDS_Edge anEdge = TopoDS::Edge(A);
138
139     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
140     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
141     gp_Dir D(gp_Vec(aP1, aP2)) ;
142
143     gp_Ax1 AX1(aP1, D) ;
144
145     Standard_Integer nbtimes = RI.GetNbIter1();
146     Standard_Real angle = 360.0/nbtimes ;
147
148     TopoDS_Compound aCompound;
149     BRep_Builder B;
150     B.MakeCompound( aCompound );
151     
152     for (int i = 0; i < nbtimes; i++ ) {
153       aTrsf.SetRotation(AX1, i*angle*PI180) ;
154       BRepBuilderAPI_Transform myBRepTransformation(anOriginal, aTrsf, Standard_False) ;
155       B.Add( aCompound, myBRepTransformation.Shape() );
156     }
157
158     aShape = aCompound ;
159   }
160   else if(aType == ROTATE_2D) {
161     Standard_Real DX, DY, DZ ;
162
163     //Get direction
164     Handle(GEOM_Function) anAxis = RI.GetAxis();
165     if(anAxis.IsNull()) return 0;
166     TopoDS_Shape A = anAxis->GetValue();
167     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
168     TopoDS_Edge anEdge = TopoDS::Edge(A);
169     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
170     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
171     gp_Dir D(gp_Vec(aP1, aP2)) ;
172
173     gp_Ax1 AX1(aP1, D) ;
174
175
176     gp_Trsf theTransformation1 ;
177     gp_Trsf theTransformation2 ;
178     gp_Pnt P1 ;
179     GProp_GProps System ;
180     
181     if ( anOriginal.ShapeType() == TopAbs_VERTEX) {
182       P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
183     } 
184     else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
185       BRepGProp::LinearProperties(anOriginal, System);
186       P1 = System.CentreOfMass() ;
187     }
188     else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
189       BRepGProp::SurfaceProperties(anOriginal, System);
190       P1 = System.CentreOfMass() ;
191     }
192     else {
193       BRepGProp::VolumeProperties(anOriginal, System);
194       P1 = System.CentreOfMass() ;
195     }
196     
197     Handle(Geom_Line) Line = new Geom_Line(AX1);
198     GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line ) ;
199     gp_Pnt P2 = aPrjTool.NearestPoint();
200     
201     if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
202     
203     gp_Vec Vec(P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z()) ;
204     Vec.Normalize();
205     
206     Standard_Integer nbtimes2 = RI.GetNbIter2();
207     Standard_Integer nbtimes1 = RI.GetNbIter1();
208     Standard_Real step = RI.GetStep();
209     Standard_Real ang = RI.GetAngle();
210
211     gp_Vec myVec ;
212     TopoDS_Compound aCompound;
213     BRep_Builder B;
214     B.MakeCompound( aCompound );
215     for (int i = 0; i < nbtimes2; i++ ) {
216       for (int j = 0; j < nbtimes1; j++ ) {
217         DX = i * step * Vec.X() ;
218         DY = i * step * Vec.Y() ;
219         DZ = i * step * Vec.Z() ;
220         myVec.SetCoord( DX, DY, DZ ) ;
221         theTransformation1.SetTranslation(myVec) ;
222         theTransformation2.SetRotation(AX1, j*ang*PI180) ;
223         BRepBuilderAPI_Transform myBRepTransformation1(anOriginal, theTransformation1, Standard_False) ;
224         BRepBuilderAPI_Transform myBRepTransformation2(myBRepTransformation1.Shape(), theTransformation2, Standard_False) ;
225         B.Add( aCompound, myBRepTransformation2.Shape() );
226       }
227     }
228
229     aShape = aCompound;
230
231   }
232   else return 0;
233
234  
235
236
237   if (aShape.IsNull()) return 0;
238
239   aFunction->SetValue(aShape);
240
241   log.SetTouched(Label()); 
242
243   return 1;
244 }
245
246
247 //=======================================================================
248 //function :  GEOMImpl_RotateDriver_Type_
249 //purpose  :
250 //======================================================================= 
251 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
252 {
253
254   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
255   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
256   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
257   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
258   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
259   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
260  
261
262   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
263   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
264                                                          sizeof(GEOMImpl_RotateDriver),
265                                                          1,
266                                                          (Standard_Address)_Ancestors,
267                                                          (Standard_Address)NULL);
268
269   return _aType;
270 }
271
272 //=======================================================================
273 //function : DownCast
274 //purpose  :
275 //======================================================================= 
276
277 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
278 {
279   Handle(GEOMImpl_RotateDriver) _anOtherObject;
280
281   if (!AnObject.IsNull()) {
282      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
283        _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
284      }
285   }
286
287   return _anOtherObject ;
288 }
289
290