Salome HOME
9f1e3035b68d9d1519ea1e2961b55acf24289d70
[modules/geom.git] / src / GEOMImpl / GEOMImpl_RotateDriver.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_RotateDriver.hxx>
26 #include <GEOMImpl_IRotate.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29 #include <gp_Trsf.hxx>
30 #include <gp_Pnt.hxx>
31 #include <gp_Vec.hxx>
32 #include <gp_Dir.hxx>
33 #include <gp_Ax1.hxx>
34 #include <BRepBuilderAPI_Transform.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Compound.hxx>
39 #include <TopAbs.hxx>
40 #include <TopExp.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <BRep_Tool.hxx>
44 #include <BRep_Builder.hxx>
45 #include <GeomAPI_ProjectPointOnCurve.hxx>
46 #include <Geom_Line.hxx>
47 #include <GProp_GProps.hxx>
48 #include <BRepGProp.hxx>
49 #include <Precision.hxx>
50
51 //=======================================================================
52 //function : GetID
53 //purpose  :
54 //=======================================================================
55 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
56 {
57   static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
58   return aRotateDriver;
59 }
60
61
62 //=======================================================================
63 //function : GEOMImpl_RotateDriver
64 //purpose  :
65 //=======================================================================
66
67 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver()
68 {
69 }
70
71 //=======================================================================
72 //function : Execute
73 //purpose  :
74 //=======================================================================
75 Standard_Integer GEOMImpl_RotateDriver::Execute(TFunction_Logbook& log) const
76 {
77   if (Label().IsNull()) return 0;
78   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
79
80   if (aFunction.IsNull()) return 0;
81
82   GEOMImpl_IRotate RI(aFunction);
83   gp_Trsf aTrsf;
84   gp_Pnt aCP, aP1, aP2;
85   Standard_Integer aType = aFunction->GetType();
86   Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
87   if (anOriginalFunction.IsNull()) return 0;
88   TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
89   if (anOriginal.IsNull()) return 0;
90
91   if (aType == ROTATE || aType == ROTATE_COPY) {
92     Handle(GEOM_Function) anAxis = RI.GetAxis();
93     if (anAxis.IsNull()) return 0;
94     TopoDS_Shape A = anAxis->GetValue();
95     if (A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
96     TopoDS_Edge anEdge = TopoDS::Edge(A);
97
98     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
99     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
100     gp_Dir aDir(gp_Vec(aP1, aP2));
101     gp_Ax1 anAx1(aP1, aDir);
102     Standard_Real anAngle = RI.GetAngle();
103     if (fabs(anAngle) < Precision::Angular()) anAngle += 2.*M_PI; // NPAL19665,19769
104     aTrsf.SetRotation(anAx1, anAngle);
105
106     //NPAL18620: performance problem: multiple locations are accumulated
107     //           in shape and need a great time to process
108     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
109     //aShape = aTransformation.Shape();
110     TopLoc_Location aLocOrig = anOriginal.Location();
111     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
112     //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
113     aTrsfOrig.PreMultiply(aTrsf);
114     TopLoc_Location aLocRes (aTrsfOrig);
115     aShape = anOriginal.Located(aLocRes);
116   }
117   else if (aType ==  ROTATE_THREE_POINTS || aType == ROTATE_THREE_POINTS_COPY) {
118     Handle(GEOM_Function) aCentPoint = RI.GetCentPoint();
119     Handle(GEOM_Function) aPoint1 = RI.GetPoint1();
120     Handle(GEOM_Function) aPoint2 = RI.GetPoint2();
121     if(aCentPoint.IsNull() || aPoint1.IsNull() || aPoint2.IsNull()) return 0;
122     TopoDS_Shape aCV = aCentPoint->GetValue();
123     TopoDS_Shape aV1 = aPoint1->GetValue();
124     TopoDS_Shape aV2 = aPoint2->GetValue();
125     if(aCV.IsNull() || aCV.ShapeType() != TopAbs_VERTEX) return 0;
126     if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
127     if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
128
129     aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
130     aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
131     aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
132
133     gp_Vec aVec1 (aCP, aP1);
134     gp_Vec aVec2 (aCP, aP2);
135     gp_Dir aDir (aVec1 ^ aVec2);
136     gp_Ax1 anAx1 (aCP, aDir);
137     Standard_Real anAngle = aVec1.Angle(aVec2);
138     if (fabs(anAngle) < Precision::Angular()) anAngle += 2.*M_PI; // NPAL19665
139     aTrsf.SetRotation(anAx1, anAngle);
140     //NPAL18620: performance problem: multiple locations are accumulated
141     //           in shape and need a great time to process
142     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
143     //aShape = aTransformation.Shape();
144     TopLoc_Location aLocOrig = anOriginal.Location();
145     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
146     //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
147     aTrsfOrig.PreMultiply(aTrsf);
148     TopLoc_Location aLocRes (aTrsfOrig);
149     aShape = anOriginal.Located(aLocRes);
150   }
151   else if (aType == ROTATE_1D) {
152     //Get direction
153     Handle(GEOM_Function) anAxis = RI.GetAxis();
154     if(anAxis.IsNull()) return 0;
155     TopoDS_Shape A = anAxis->GetValue();
156     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
157     TopoDS_Edge anEdge = TopoDS::Edge(A);
158
159     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
160     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
161     gp_Dir D(gp_Vec(aP1, aP2));
162
163     gp_Ax1 AX1(aP1, D);
164
165     Standard_Integer nbtimes = RI.GetNbIter1();
166     Standard_Real angle = 360.0/nbtimes;
167
168     TopoDS_Compound aCompound;
169     BRep_Builder B;
170     B.MakeCompound( aCompound );
171
172     TopLoc_Location aLocOrig = anOriginal.Location();
173     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
174
175     for (int i = 0; i < nbtimes; i++ ) {
176       if (i == 0) { // NPAL19665
177         B.Add(aCompound, anOriginal);
178       }
179       else {
180         aTrsf.SetRotation(AX1, i*angle*M_PI/180.);
181         //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
182         gp_Trsf aTrsfNew (aTrsfOrig);
183         aTrsfNew.PreMultiply(aTrsf);
184         TopLoc_Location aLocRes (aTrsfNew);
185         B.Add(aCompound, anOriginal.Located(aLocRes));
186       }
187       //NPAL18620: performance problem: multiple locations are accumulated
188       //           in shape and need a great time to process
189       //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
190       //B.Add(aCompound, aBRepTransformation.Shape());
191     }
192
193     aShape = aCompound;
194   }
195   else if (aType == ROTATE_2D) {
196     //Get direction
197     Handle(GEOM_Function) anAxis = RI.GetAxis();
198     if(anAxis.IsNull()) return 0;
199     TopoDS_Shape A = anAxis->GetValue();
200     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
201     TopoDS_Edge anEdge = TopoDS::Edge(A);
202     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
203     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
204     gp_Dir D(gp_Vec(aP1, aP2));
205
206     gp_Ax1 AX1(aP1, D);
207
208     gp_Trsf aTrsf1;
209     gp_Trsf aTrsf2;
210
211     gp_XYZ aDir2 = RI.GetDir2(); // can be set by previous execution
212     if (aDir2.Modulus() < gp::Resolution()) {
213       // Calculate direction as vector from the axis to the shape's center
214       gp_Pnt P1;
215       GProp_GProps System;
216
217       if (anOriginal.ShapeType() == TopAbs_VERTEX) {
218         P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
219       }
220       else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
221         BRepGProp::LinearProperties(anOriginal, System);
222         P1 = System.CentreOfMass();
223       }
224       else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
225         BRepGProp::SurfaceProperties(anOriginal, System);
226         P1 = System.CentreOfMass();
227       }
228       else {
229         BRepGProp::VolumeProperties(anOriginal, System);
230         P1 = System.CentreOfMass();
231       }
232
233       Handle(Geom_Line) Line = new Geom_Line(AX1);
234       GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
235       gp_Pnt P2 = aPrjTool.NearestPoint();
236
237       if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
238
239       aDir2 = gp_XYZ(P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
240
241       // Attention: this abnormal action is done for good working of
242       // TransformLikeOther(), used by RestoreSubShapes functionality
243       RI.SetDir2(aDir2);
244     }
245
246     gp_Vec Vec (aDir2);
247     Vec.Normalize();
248
249     Standard_Integer nbtimes2 = RI.GetNbIter2();
250     Standard_Integer nbtimes1 = RI.GetNbIter1();
251     Standard_Real step = RI.GetStep();
252     Standard_Real ang = RI.GetAngle();
253
254     TopLoc_Location aLocOrig = anOriginal.Location();
255     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
256
257     gp_Vec aVec;
258     TopoDS_Compound aCompound;
259     BRep_Builder B;
260     B.MakeCompound( aCompound );
261
262     Standard_Real DX, DY, DZ;
263
264     for (int i = 0; i < nbtimes2; i++ ) {
265       DX = i * step * Vec.X();
266       DY = i * step * Vec.Y();
267       DZ = i * step * Vec.Z();
268       aVec.SetCoord( DX, DY, DZ );
269       aTrsf1.SetTranslation(aVec);
270
271       for (int j = 0; j < nbtimes1; j++ ) {
272         if (j == 0) { // NPAL19665
273           TopLoc_Location aLocRes (aTrsf1 * aTrsfOrig);
274           B.Add(aCompound, anOriginal.Located(aLocRes));
275         }
276         else {
277           aTrsf2.SetRotation(AX1, j*ang*M_PI/180.);
278           //TopLoc_Location aLocRes (aTrsf2 * aTrsf1 * aTrsfOrig); // gp_Trsf::Multiply() has a bug
279           gp_Trsf aTrsfNew (aTrsfOrig);
280           aTrsfNew.PreMultiply(aTrsf1);
281           aTrsfNew.PreMultiply(aTrsf2);
282           TopLoc_Location aLocRes (aTrsfNew);
283           B.Add(aCompound, anOriginal.Located(aLocRes));
284         }
285         //NPAL18620: performance problem: multiple locations are accumulated
286         //           in shape and need a great time to process
287         //BRepBuilderAPI_Transform aBRepTrsf1 (anOriginal, aTrsf1, Standard_False);
288         //BRepBuilderAPI_Transform aBRepTrsf2 (aBRepTrsf1.Shape(), aTrsf2, Standard_False);
289         //B.Add(aCompound, aBRepTrsf2.Shape());
290       }
291     }
292
293     aShape = aCompound;
294   }
295   else return 0;
296
297
298   if (aShape.IsNull()) return 0;
299
300   aFunction->SetValue(aShape);
301
302   log.SetTouched(Label());
303
304   return 1;
305 }
306
307
308 //=======================================================================
309 //function :  GEOMImpl_RotateDriver_Type_
310 //purpose  :
311 //=======================================================================
312 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
313 {
314   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
315   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
316   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
317   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
318   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
319   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
320
321   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
322   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
323                                                          sizeof(GEOMImpl_RotateDriver),
324                                                          1,
325                                                          (Standard_Address)_Ancestors,
326                                                          (Standard_Address)NULL);
327
328   return _aType;
329 }
330
331 //=======================================================================
332 //function : DownCast
333 //purpose  :
334 //=======================================================================
335 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
336 {
337   Handle(GEOMImpl_RotateDriver) _anOtherObject;
338
339   if (!AnObject.IsNull()) {
340      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
341        _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
342      }
343   }
344
345   return _anOtherObject;
346 }