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