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