1 // Copyright (C) 2007-2008 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>
27 #include <GEOM_Function.hxx>
28 #include <gp_Trsf.hxx>
33 #include <BRepBuilderAPI_Transform.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Compound.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>
50 //=======================================================================
53 //=======================================================================
54 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
56 static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
61 //=======================================================================
62 //function : GEOMImpl_RotateDriver
64 //=======================================================================
66 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver()
70 //=======================================================================
73 //=======================================================================
74 Standard_Integer GEOMImpl_RotateDriver::Execute(TFunction_Logbook& log) const
76 if (Label().IsNull()) return 0;
77 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
79 if (aFunction.IsNull()) return 0;
81 GEOMImpl_IRotate RI(aFunction);
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;
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);
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);
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);
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;
128 aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
129 aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
130 aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
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);
150 else if (aType == ROTATE_1D) {
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);
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));
164 Standard_Integer nbtimes = RI.GetNbIter1();
165 Standard_Real angle = 360.0/nbtimes;
167 TopoDS_Compound aCompound;
169 B.MakeCompound( aCompound );
171 TopLoc_Location aLocOrig = anOriginal.Location();
172 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
174 for (int i = 0; i < nbtimes; i++ ) {
175 if (i == 0) { // NPAL19665
176 B.Add(aCompound, anOriginal);
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));
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());
194 else if (aType == ROTATE_2D) {
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));
213 if (anOriginal.ShapeType() == TopAbs_VERTEX) {
214 P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
216 else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
217 BRepGProp::LinearProperties(anOriginal, System);
218 P1 = System.CentreOfMass();
220 else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
221 BRepGProp::SurfaceProperties(anOriginal, System);
222 P1 = System.CentreOfMass();
225 BRepGProp::VolumeProperties(anOriginal, System);
226 P1 = System.CentreOfMass();
229 Handle(Geom_Line) Line = new Geom_Line(AX1);
230 GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
231 gp_Pnt P2 = aPrjTool.NearestPoint();
233 if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
235 gp_Vec Vec (P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
238 Standard_Integer nbtimes2 = RI.GetNbIter2();
239 Standard_Integer nbtimes1 = RI.GetNbIter1();
240 Standard_Real step = RI.GetStep();
241 Standard_Real ang = RI.GetAngle();
243 TopLoc_Location aLocOrig = anOriginal.Location();
244 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
247 TopoDS_Compound aCompound;
249 B.MakeCompound( aCompound );
251 Standard_Real DX, DY, DZ;
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);
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));
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));
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());
287 if (aShape.IsNull()) return 0;
289 aFunction->SetValue(aShape);
291 log.SetTouched(Label());
297 //=======================================================================
298 //function : GEOMImpl_RotateDriver_Type_
300 //=======================================================================
301 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
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);
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),
314 (Standard_Address)_Ancestors,
315 (Standard_Address)NULL);
320 //=======================================================================
321 //function : DownCast
323 //=======================================================================
324 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
326 Handle(GEOMImpl_RotateDriver) _anOtherObject;
328 if (!AnObject.IsNull()) {
329 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
330 _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
334 return _anOtherObject;