Salome HOME
Merge from V4_1_0_maintainance branch (from tag mergeto_BR_QT4_Dev_08Jul08)
[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     if (fabs(anAngle) < Precision::Angular()) anAngle += 2*PI; // NPAL19665,19769
102     aTrsf.SetRotation(anAx1, anAngle);
103
104     //NPAL18620: performance problem: multiple locations are accumulated
105     //           in shape and need a great time to process
106     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
107     //aShape = aTransformation.Shape();
108     TopLoc_Location aLocOrig = anOriginal.Location();
109     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
110     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
111     aShape = anOriginal.Located(aLocRes);
112   }
113   else if (aType ==  ROTATE_THREE_POINTS || aType == ROTATE_THREE_POINTS_COPY) {
114     Handle(GEOM_Function) aCentPoint = RI.GetCentPoint();
115     Handle(GEOM_Function) aPoint1 = RI.GetPoint1();
116     Handle(GEOM_Function) aPoint2 = RI.GetPoint2();
117     if(aCentPoint.IsNull() || aPoint1.IsNull() || aPoint2.IsNull()) return 0;
118     TopoDS_Shape aCV = aCentPoint->GetValue();
119     TopoDS_Shape aV1 = aPoint1->GetValue();
120     TopoDS_Shape aV2 = aPoint2->GetValue();
121     if(aCV.IsNull() || aCV.ShapeType() != TopAbs_VERTEX) return 0;
122     if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
123     if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
124
125     aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
126     aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
127     aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
128
129     gp_Vec aVec1 (aCP, aP1);
130     gp_Vec aVec2 (aCP, aP2);
131     gp_Dir aDir (aVec1 ^ aVec2);
132     gp_Ax1 anAx1 (aCP, aDir);
133     Standard_Real anAngle = aVec1.Angle(aVec2);
134     if (fabs(anAngle) < Precision::Angular()) anAngle += 2*PI; // NPAL19665
135     aTrsf.SetRotation(anAx1, anAngle);
136     //NPAL18620: performance problem: multiple locations are accumulated
137     //           in shape and need a great time to process
138     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
139     //aShape = aTransformation.Shape();
140     TopLoc_Location aLocOrig = anOriginal.Location();
141     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
142     TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
143     aShape = anOriginal.Located(aLocRes);
144   }
145   else if (aType == ROTATE_1D) {
146     //Get direction
147     Handle(GEOM_Function) anAxis = RI.GetAxis();
148     if(anAxis.IsNull()) return 0;
149     TopoDS_Shape A = anAxis->GetValue();
150     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
151     TopoDS_Edge anEdge = TopoDS::Edge(A);
152
153     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
154     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
155     gp_Dir D(gp_Vec(aP1, aP2));
156
157     gp_Ax1 AX1(aP1, D);
158
159     Standard_Integer nbtimes = RI.GetNbIter1();
160     Standard_Real angle = 360.0/nbtimes;
161
162     TopoDS_Compound aCompound;
163     BRep_Builder B;
164     B.MakeCompound( aCompound );
165
166     TopLoc_Location aLocOrig = anOriginal.Location();
167     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
168
169     for (int i = 0; i < nbtimes; i++ ) {
170       if (i == 0) { // NPAL19665
171         B.Add(aCompound, anOriginal);
172       }
173       else {
174         aTrsf.SetRotation(AX1, i*angle*PI180);
175         TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
176         B.Add(aCompound, anOriginal.Located(aLocRes));
177       }
178       //NPAL18620: performance problem: multiple locations are accumulated
179       //           in shape and need a great time to process
180       //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
181       //B.Add(aCompound, aBRepTransformation.Shape());
182     }
183
184     aShape = aCompound;
185   }
186   else if (aType == ROTATE_2D) {
187     //Get direction
188     Handle(GEOM_Function) anAxis = RI.GetAxis();
189     if(anAxis.IsNull()) return 0;
190     TopoDS_Shape A = anAxis->GetValue();
191     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
192     TopoDS_Edge anEdge = TopoDS::Edge(A);
193     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
194     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
195     gp_Dir D(gp_Vec(aP1, aP2));
196
197     gp_Ax1 AX1(aP1, D);
198
199
200     gp_Trsf aTrsf1;
201     gp_Trsf aTrsf2;
202     gp_Pnt P1;
203     GProp_GProps System;
204
205     if (anOriginal.ShapeType() == TopAbs_VERTEX) {
206       P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
207     }
208     else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
209       BRepGProp::LinearProperties(anOriginal, System);
210       P1 = System.CentreOfMass();
211     }
212     else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
213       BRepGProp::SurfaceProperties(anOriginal, System);
214       P1 = System.CentreOfMass();
215     }
216     else {
217       BRepGProp::VolumeProperties(anOriginal, System);
218       P1 = System.CentreOfMass();
219     }
220
221     Handle(Geom_Line) Line = new Geom_Line(AX1);
222     GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
223     gp_Pnt P2 = aPrjTool.NearestPoint();
224
225     if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
226
227     gp_Vec Vec (P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
228     Vec.Normalize();
229
230     Standard_Integer nbtimes2 = RI.GetNbIter2();
231     Standard_Integer nbtimes1 = RI.GetNbIter1();
232     Standard_Real step = RI.GetStep();
233     Standard_Real ang = RI.GetAngle();
234
235     TopLoc_Location aLocOrig = anOriginal.Location();
236     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
237
238     gp_Vec aVec;
239     TopoDS_Compound aCompound;
240     BRep_Builder B;
241     B.MakeCompound( aCompound );
242
243     Standard_Real DX, DY, DZ;
244
245     for (int i = 0; i < nbtimes2; i++ ) {
246       DX = i * step * Vec.X();
247       DY = i * step * Vec.Y();
248       DZ = i * step * Vec.Z();
249       aVec.SetCoord( DX, DY, DZ );
250       aTrsf1.SetTranslation(aVec);
251
252       for (int j = 0; j < nbtimes1; j++ ) {
253         if (j == 0) { // NPAL19665
254           TopLoc_Location aLocRes (aTrsf1 * aTrsfOrig);
255           B.Add(aCompound, anOriginal.Located(aLocRes));
256         }
257         else {
258           aTrsf2.SetRotation(AX1, j*ang*PI180);
259           TopLoc_Location aLocRes (aTrsf2 * aTrsf1 * aTrsfOrig);
260           B.Add(aCompound, anOriginal.Located(aLocRes));
261         }
262         //NPAL18620: performance problem: multiple locations are accumulated
263         //           in shape and need a great time to process
264         //BRepBuilderAPI_Transform aBRepTrsf1 (anOriginal, aTrsf1, Standard_False);
265         //BRepBuilderAPI_Transform aBRepTrsf2 (aBRepTrsf1.Shape(), aTrsf2, Standard_False);
266         //B.Add(aCompound, aBRepTrsf2.Shape());
267       }
268     }
269
270     aShape = aCompound;
271   }
272   else return 0;
273
274
275   if (aShape.IsNull()) return 0;
276
277   aFunction->SetValue(aShape);
278
279   log.SetTouched(Label());
280
281   return 1;
282 }
283
284
285 //=======================================================================
286 //function :  GEOMImpl_RotateDriver_Type_
287 //purpose  :
288 //=======================================================================
289 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
290 {
291   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
292   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
293   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
294   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
295   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
296   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
297
298   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
299   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
300                                                          sizeof(GEOMImpl_RotateDriver),
301                                                          1,
302                                                          (Standard_Address)_Ancestors,
303                                                          (Standard_Address)NULL);
304
305   return _aType;
306 }
307
308 //=======================================================================
309 //function : DownCast
310 //purpose  :
311 //=======================================================================
312 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
313 {
314   Handle(GEOMImpl_RotateDriver) _anOtherObject;
315
316   if (!AnObject.IsNull()) {
317      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
318        _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
319      }
320   }
321
322   return _anOtherObject;
323 }