1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <Standard_Stream.hxx>
24 #include <GEOMImpl_RotateDriver.hxx>
25 #include <GEOMImpl_IRotate.hxx>
26 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
30 #include <GEOMUtils.hxx>
32 #include <BRepBuilderAPI_Transform.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRepGProp.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TopoDS_Edge.hxx>
45 #include <GeomAPI_ProjectPointOnCurve.hxx>
46 #include <Geom_Line.hxx>
47 #include <GProp_GProps.hxx>
49 #include <Precision.hxx>
51 #include <gp_Trsf.hxx>
57 //=======================================================================
60 //=======================================================================
61 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
63 static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
68 //=======================================================================
69 //function : GEOMImpl_RotateDriver
71 //=======================================================================
73 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver()
77 //=======================================================================
80 //=======================================================================
81 Standard_Integer GEOMImpl_RotateDriver::Execute(TFunction_Logbook& log) const
83 if (Label().IsNull()) return 0;
84 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
86 if (aFunction.IsNull()) return 0;
88 GEOMImpl_IRotate RI(aFunction);
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;
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));
105 gp_Ax1 anAx1 (aP1, aDir);
107 Standard_Real anAngle = RI.GetAngle();
108 if (fabs(anAngle) < Precision::Angular()) anAngle += 2.*M_PI; // NPAL19665,19769
109 aTrsf.SetRotation(anAx1, anAngle);
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);
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;
134 aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
135 aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
136 aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
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);
156 else if (aType == ROTATE_1D || aType == ROTATE_1D_STEP) {
158 gp_Pnt aP1 = gp::Origin();
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));
170 Standard_Integer nbtimes = RI.GetNbIter1();
171 Standard_Real angle = 360. / nbtimes;
172 if (aType == ROTATE_1D_STEP)
173 angle = RI.GetAngle();
175 TopoDS_Compound aCompound;
177 B.MakeCompound( aCompound );
179 TopLoc_Location aLocOrig = anOriginal.Location();
180 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
182 for (int i = 0; i < nbtimes; i++ ) {
183 if (i == 0) { // NPAL19665
184 B.Add(aCompound, anOriginal);
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));
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());
202 else if (aType == ROTATE_2D) {
204 gp_Pnt aP1 = gp::Origin();
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));
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
225 if (anOriginal.ShapeType() == TopAbs_VERTEX) {
226 P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
228 else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
229 BRepGProp::LinearProperties(anOriginal, System);
230 P1 = System.CentreOfMass();
232 else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
233 BRepGProp::SurfaceProperties(anOriginal, System);
234 P1 = System.CentreOfMass();
237 BRepGProp::VolumeProperties(anOriginal, System);
238 P1 = System.CentreOfMass();
241 Handle(Geom_Line) Line = new Geom_Line(AX1);
242 GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
243 gp_Pnt P2 = aPrjTool.NearestPoint();
245 if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
247 aDir2 = gp_XYZ(P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
249 // Attention: this abnormal action is done for good working of
250 // TransformLikeOther(), used by RestoreSubShapes functionality
257 Standard_Integer nbtimes2 = RI.GetNbIter2();
258 Standard_Integer nbtimes1 = RI.GetNbIter1();
259 Standard_Real step = RI.GetStep();
260 Standard_Real ang = RI.GetAngle();
262 TopLoc_Location aLocOrig = anOriginal.Location();
263 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
266 TopoDS_Compound aCompound;
268 B.MakeCompound( aCompound );
270 Standard_Real DX, DY, DZ;
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);
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));
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));
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());
306 if (aShape.IsNull()) return 0;
308 aFunction->SetValue(aShape);
310 log.SetTouched(Label());
316 //=======================================================================
317 //function : GEOMImpl_RotateDriver_Type_
319 //=======================================================================
320 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
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);
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),
333 (Standard_Address)_Ancestors,
334 (Standard_Address)NULL);
339 //=======================================================================
340 //function : DownCast
342 //=======================================================================
343 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
345 Handle(GEOMImpl_RotateDriver) _anOtherObject;
347 if (!AnObject.IsNull()) {
348 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
349 _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
353 return _anOtherObject;