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