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