1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_RotateDriver.hxx>
26 #include <GEOMImpl_IRotate.hxx>
27 #include <GEOMImpl_Types.hxx>
29 #include <GEOM_Function.hxx>
31 #include <GEOMUtils.hxx>
33 #include <BRepBuilderAPI_Transform.hxx>
34 #include <BRep_Builder.hxx>
35 #include <BRep_Tool.hxx>
36 #include <BRepGProp.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Vertex.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <TopoDS_Edge.hxx>
46 #include <GeomAPI_ProjectPointOnCurve.hxx>
47 #include <Geom_Line.hxx>
48 #include <GProp_GProps.hxx>
50 #include <Precision.hxx>
52 #include <gp_Trsf.hxx>
58 //=======================================================================
61 //=======================================================================
62 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
64 static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
69 //=======================================================================
70 //function : GEOMImpl_RotateDriver
72 //=======================================================================
74 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver()
78 //=======================================================================
81 //=======================================================================
82 Standard_Integer GEOMImpl_RotateDriver::Execute(LOGBOOK& log) const
84 if (Label().IsNull()) return 0;
85 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
87 if (aFunction.IsNull()) return 0;
89 GEOMImpl_IRotate RI(aFunction);
92 Standard_Integer aType = aFunction->GetType();
93 Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
94 if (anOriginalFunction.IsNull()) return 0;
95 TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
96 if (anOriginal.IsNull()) return 0;
98 if (aType == ROTATE || aType == ROTATE_COPY) {
99 Handle(GEOM_Function) anAxis = RI.GetAxis();
100 if (anAxis.IsNull()) return 0;
101 TopoDS_Shape A = anAxis->GetValue();
102 // do not take into account edge orientation (it is correct)
103 gp_Vec aV = GEOMUtils::GetVector(A, Standard_False);
104 TopoDS_Edge anEdge = TopoDS::Edge(A);
105 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
107 gp_Ax1 anAx1 (aP1, aDir);
109 Standard_Real anAngle = RI.GetAngle();
110 if (fabs(anAngle) < Precision::Angular()) anAngle += 2.*M_PI; // NPAL19665,19769
111 aTrsf.SetRotation(anAx1, anAngle);
113 //NPAL18620: performance problem: multiple locations are accumulated
114 // in shape and need a great time to process
115 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
116 //aShape = aTransformation.Shape();
117 TopLoc_Location aLocOrig = anOriginal.Location();
118 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
119 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
120 aTrsfOrig.PreMultiply(aTrsf);
121 TopLoc_Location aLocRes (aTrsfOrig);
122 aShape = anOriginal.Located(aLocRes);
124 else if (aType == ROTATE_THREE_POINTS || aType == ROTATE_THREE_POINTS_COPY) {
125 Handle(GEOM_Function) aCentPoint = RI.GetCentPoint();
126 Handle(GEOM_Function) aPoint1 = RI.GetPoint1();
127 Handle(GEOM_Function) aPoint2 = RI.GetPoint2();
128 if(aCentPoint.IsNull() || aPoint1.IsNull() || aPoint2.IsNull()) return 0;
129 TopoDS_Shape aCV = aCentPoint->GetValue();
130 TopoDS_Shape aV1 = aPoint1->GetValue();
131 TopoDS_Shape aV2 = aPoint2->GetValue();
132 if(aCV.IsNull() || aCV.ShapeType() != TopAbs_VERTEX) return 0;
133 if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
134 if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
136 aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
137 aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
138 aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
140 gp_Vec aVec1 (aCP, aP1);
141 gp_Vec aVec2 (aCP, aP2);
142 gp_Dir aDir (aVec1 ^ aVec2);
143 gp_Ax1 anAx1 (aCP, aDir);
144 Standard_Real anAngle = aVec1.Angle(aVec2);
145 if (fabs(anAngle) < Precision::Angular()) anAngle += 2.*M_PI; // NPAL19665
146 aTrsf.SetRotation(anAx1, anAngle);
147 //NPAL18620: performance problem: multiple locations are accumulated
148 // in shape and need a great time to process
149 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
150 //aShape = aTransformation.Shape();
151 TopLoc_Location aLocOrig = anOriginal.Location();
152 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
153 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
154 aTrsfOrig.PreMultiply(aTrsf);
155 TopLoc_Location aLocRes (aTrsfOrig);
156 aShape = anOriginal.Located(aLocRes);
158 else if (aType == ROTATE_1D || aType == ROTATE_1D_STEP) {
160 gp_Pnt aP1 = gp::Origin();
162 Handle(GEOM_Function) anAxis = RI.GetAxis();
163 if (!anAxis.IsNull()) {
164 TopoDS_Shape A = anAxis->GetValue();
165 // do not take into account edge orientation (it is correct)
166 gp_Vec aV = GEOMUtils::GetVector(A, Standard_False);
167 TopoDS_Edge anEdge = TopoDS::Edge(A);
168 aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
173 Standard_Integer nbtimes = RI.GetNbIter1();
174 Standard_Real angle = 360. / nbtimes;
175 if (aType == ROTATE_1D_STEP)
176 angle = RI.GetAngle();
178 TopoDS_Compound aCompound;
180 B.MakeCompound( aCompound );
182 TopLoc_Location aLocOrig = anOriginal.Location();
183 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
185 for (int i = 0; i < nbtimes; i++ ) {
186 if (i == 0) { // NPAL19665
187 B.Add(aCompound, anOriginal);
190 aTrsf.SetRotation(AX1, i * angle * M_PI / 180.);
191 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
192 gp_Trsf aTrsfNew (aTrsfOrig);
193 aTrsfNew.PreMultiply(aTrsf);
194 TopLoc_Location aLocRes (aTrsfNew);
195 B.Add(aCompound, anOriginal.Located(aLocRes));
197 //NPAL18620: performance problem: multiple locations are accumulated
198 // in shape and need a great time to process
199 //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
200 //B.Add(aCompound, aBRepTransformation.Shape());
205 else if (aType == ROTATE_2D) {
207 gp_Pnt aP1 = gp::Origin();
209 Handle(GEOM_Function) anAxis = RI.GetAxis();
210 if (!anAxis.IsNull()) {
211 TopoDS_Shape A = anAxis->GetValue();
212 // do not take into account edge orientation (it is correct)
213 gp_Vec aV = GEOMUtils::GetVector(A, Standard_False);
214 TopoDS_Edge anEdge = TopoDS::Edge(A);
215 aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
223 gp_XYZ aDir2 = RI.GetDir2(); // can be set by previous execution
224 if (aDir2.Modulus() < gp::Resolution()) {
225 // Calculate direction as vector from the axis to the shape's center
229 if (anOriginal.ShapeType() == TopAbs_VERTEX) {
230 P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
232 else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
233 BRepGProp::LinearProperties(anOriginal, System);
234 P1 = System.CentreOfMass();
236 else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
237 BRepGProp::SurfaceProperties(anOriginal, System);
238 P1 = System.CentreOfMass();
241 BRepGProp::VolumeProperties(anOriginal, System);
242 P1 = System.CentreOfMass();
245 Handle(Geom_Line) Line = new Geom_Line(AX1);
246 GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
247 gp_Pnt P2 = aPrjTool.NearestPoint();
249 if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
251 aDir2 = gp_XYZ(P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
253 // Attention: this abnormal action is done for good working of
254 // TransformLikeOther(), used by RestoreSubShapes functionality
261 Standard_Integer nbtimes2 = RI.GetNbIter2();
262 Standard_Integer nbtimes1 = RI.GetNbIter1();
263 Standard_Real step = RI.GetStep();
264 Standard_Real ang = RI.GetAngle();
266 TopLoc_Location aLocOrig = anOriginal.Location();
267 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
270 TopoDS_Compound aCompound;
272 B.MakeCompound( aCompound );
274 Standard_Real DX, DY, DZ;
276 for (int i = 0; i < nbtimes2; i++ ) {
277 DX = i * step * Vec.X();
278 DY = i * step * Vec.Y();
279 DZ = i * step * Vec.Z();
280 aVec.SetCoord( DX, DY, DZ );
281 aTrsf1.SetTranslation(aVec);
283 for (int j = 0; j < nbtimes1; j++ ) {
284 if (j == 0) { // NPAL19665
285 TopLoc_Location aLocRes (aTrsf1 * aTrsfOrig);
286 B.Add(aCompound, anOriginal.Located(aLocRes));
289 aTrsf2.SetRotation(AX1, j * ang * M_PI / 180.);
290 //TopLoc_Location aLocRes (aTrsf2 * aTrsf1 * aTrsfOrig); // gp_Trsf::Multiply() has a bug
291 gp_Trsf aTrsfNew (aTrsfOrig);
292 aTrsfNew.PreMultiply(aTrsf1);
293 aTrsfNew.PreMultiply(aTrsf2);
294 TopLoc_Location aLocRes (aTrsfNew);
295 B.Add(aCompound, anOriginal.Located(aLocRes));
297 //NPAL18620: performance problem: multiple locations are accumulated
298 // in shape and need a great time to process
299 //BRepBuilderAPI_Transform aBRepTrsf1 (anOriginal, aTrsf1, Standard_False);
300 //BRepBuilderAPI_Transform aBRepTrsf2 (aBRepTrsf1.Shape(), aTrsf2, Standard_False);
301 //B.Add(aCompound, aBRepTrsf2.Shape());
310 if (aShape.IsNull()) return 0;
312 aFunction->SetValue(aShape);
314 #if OCC_VERSION_MAJOR < 7
315 log.SetTouched(Label());
317 log->SetTouched(Label());
323 //================================================================================
325 * \brief Returns a name of creation operation and names and values of creation parameters
327 //================================================================================
329 bool GEOMImpl_RotateDriver::
330 GetCreationInformation(std::string& theOperationName,
331 std::vector<GEOM_Param>& theParams)
333 if (Label().IsNull()) return 0;
334 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
336 GEOMImpl_IRotate aCI( function );
337 Standard_Integer aType = function->GetType();
342 theOperationName = "ROTATION";
343 AddParam( theParams, "Object", aCI.GetOriginal() );
344 AddParam( theParams, "Axis", aCI.GetAxis() );
345 AddParam( theParams, "Angle", aCI.GetAngle() );
347 case ROTATE_THREE_POINTS:
348 case ROTATE_THREE_POINTS_COPY:
349 theOperationName = "ROTATION";
350 AddParam( theParams, "Object", aCI.GetOriginal() );
351 AddParam( theParams, "Central Point", aCI.GetCentPoint() );
352 AddParam( theParams, "Point 1", aCI.GetPoint1() );
353 AddParam( theParams, "Point 2", aCI.GetPoint2() );
356 theOperationName = "MUL_ROTATION";
357 AddParam( theParams, "Main Object", aCI.GetOriginal() );
358 AddParam( theParams, "Axis", aCI.GetAxis(), "DZ" );
359 AddParam( theParams, "Nb. Times", aCI.GetNbIter1() );
362 theOperationName = "MUL_ROTATION";
363 AddParam( theParams, "Main Object", aCI.GetOriginal() );
364 AddParam( theParams, "Axis", aCI.GetAxis(), "DZ" );
365 AddParam( theParams, "Angular step", aCI.GetAngle() );
366 AddParam( theParams, "Nb. Times", aCI.GetNbIter1() );
369 theOperationName = "MUL_ROTATION";
370 AddParam( theParams, "Main Object", aCI.GetOriginal() );
371 AddParam( theParams, "Axis", aCI.GetAxis(), "DZ" );
372 AddParam( theParams, "Angular step", aCI.GetAngle() );
373 AddParam( theParams, "Angular Nb. Times", aCI.GetNbIter1() );
374 AddParam( theParams, "Radial step", aCI.GetStep() );
375 AddParam( theParams, "Radial Nb. Times", aCI.GetNbIter2() );
384 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_RotateDriver,GEOM_BaseDriver);