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 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 #include <Standard_Stream.hxx>
26 #include <GEOMImpl_RotateDriver.hxx>
27 #include <GEOMImpl_IRotate.hxx>
28 #include <GEOMImpl_Types.hxx>
29 #include <GEOM_Function.hxx>
30 #include <gp_Trsf.hxx>
35 #include <BRepBuilderAPI_Transform.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Compound.hxx>
42 #include <TopoDS_Vertex.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <BRep_Tool.hxx>
45 #include <BRep_Builder.hxx>
46 #include <GeomAPI_ProjectPointOnCurve.hxx>
47 #include <Geom_Line.hxx>
48 #include <GProp_GProps.hxx>
49 #include <BRepGProp.hxx>
50 #include <Precision.hxx>
52 //=======================================================================
55 //=======================================================================
56 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
58 static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
63 //=======================================================================
64 //function : GEOMImpl_RotateDriver
66 //=======================================================================
68 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver()
72 //=======================================================================
75 //=======================================================================
76 Standard_Integer GEOMImpl_RotateDriver::Execute(TFunction_Logbook& log) const
78 if (Label().IsNull()) return 0;
79 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
81 if (aFunction.IsNull()) return 0;
83 GEOMImpl_IRotate RI(aFunction);
86 Standard_Integer aType = aFunction->GetType();
87 Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
88 if (anOriginalFunction.IsNull()) return 0;
89 TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
90 if (anOriginal.IsNull()) return 0;
92 if (aType == ROTATE || aType == ROTATE_COPY) {
93 Handle(GEOM_Function) anAxis = RI.GetAxis();
94 if (anAxis.IsNull()) return 0;
95 TopoDS_Shape A = anAxis->GetValue();
96 if (A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
97 TopoDS_Edge anEdge = TopoDS::Edge(A);
99 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
100 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
101 gp_Dir aDir(gp_Vec(aP1, aP2));
102 gp_Ax1 anAx1(aP1, aDir);
103 Standard_Real anAngle = RI.GetAngle();
104 if (fabs(anAngle) < Precision::Angular()) anAngle += 2*PI; // NPAL19665,19769
105 aTrsf.SetRotation(anAx1, anAngle);
107 //NPAL18620: performance problem: multiple locations are accumulated
108 // in shape and need a great time to process
109 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
110 //aShape = aTransformation.Shape();
111 TopLoc_Location aLocOrig = anOriginal.Location();
112 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
113 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
114 aTrsfOrig.PreMultiply(aTrsf);
115 TopLoc_Location aLocRes (aTrsfOrig);
116 aShape = anOriginal.Located(aLocRes);
118 else if (aType == ROTATE_THREE_POINTS || aType == ROTATE_THREE_POINTS_COPY) {
119 Handle(GEOM_Function) aCentPoint = RI.GetCentPoint();
120 Handle(GEOM_Function) aPoint1 = RI.GetPoint1();
121 Handle(GEOM_Function) aPoint2 = RI.GetPoint2();
122 if(aCentPoint.IsNull() || aPoint1.IsNull() || aPoint2.IsNull()) return 0;
123 TopoDS_Shape aCV = aCentPoint->GetValue();
124 TopoDS_Shape aV1 = aPoint1->GetValue();
125 TopoDS_Shape aV2 = aPoint2->GetValue();
126 if(aCV.IsNull() || aCV.ShapeType() != TopAbs_VERTEX) return 0;
127 if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
128 if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
130 aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
131 aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
132 aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
134 gp_Vec aVec1 (aCP, aP1);
135 gp_Vec aVec2 (aCP, aP2);
136 gp_Dir aDir (aVec1 ^ aVec2);
137 gp_Ax1 anAx1 (aCP, aDir);
138 Standard_Real anAngle = aVec1.Angle(aVec2);
139 if (fabs(anAngle) < Precision::Angular()) anAngle += 2*PI; // NPAL19665
140 aTrsf.SetRotation(anAx1, anAngle);
141 //NPAL18620: performance problem: multiple locations are accumulated
142 // in shape and need a great time to process
143 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
144 //aShape = aTransformation.Shape();
145 TopLoc_Location aLocOrig = anOriginal.Location();
146 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
147 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
148 aTrsfOrig.PreMultiply(aTrsf);
149 TopLoc_Location aLocRes (aTrsfOrig);
150 aShape = anOriginal.Located(aLocRes);
152 else if (aType == ROTATE_1D) {
154 Handle(GEOM_Function) anAxis = RI.GetAxis();
155 if(anAxis.IsNull()) return 0;
156 TopoDS_Shape A = anAxis->GetValue();
157 if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
158 TopoDS_Edge anEdge = TopoDS::Edge(A);
160 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
161 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
162 gp_Dir D(gp_Vec(aP1, aP2));
166 Standard_Integer nbtimes = RI.GetNbIter1();
167 Standard_Real angle = 360.0/nbtimes;
169 TopoDS_Compound aCompound;
171 B.MakeCompound( aCompound );
173 TopLoc_Location aLocOrig = anOriginal.Location();
174 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
176 for (int i = 0; i < nbtimes; i++ ) {
177 if (i == 0) { // NPAL19665
178 B.Add(aCompound, anOriginal);
181 aTrsf.SetRotation(AX1, i*angle*PI180);
182 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
183 gp_Trsf aTrsfNew (aTrsfOrig);
184 aTrsfNew.PreMultiply(aTrsf);
185 TopLoc_Location aLocRes (aTrsfNew);
186 B.Add(aCompound, anOriginal.Located(aLocRes));
188 //NPAL18620: performance problem: multiple locations are accumulated
189 // in shape and need a great time to process
190 //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
191 //B.Add(aCompound, aBRepTransformation.Shape());
196 else if (aType == ROTATE_2D) {
198 Handle(GEOM_Function) anAxis = RI.GetAxis();
199 if(anAxis.IsNull()) return 0;
200 TopoDS_Shape A = anAxis->GetValue();
201 if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
202 TopoDS_Edge anEdge = TopoDS::Edge(A);
203 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
204 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
205 gp_Dir D(gp_Vec(aP1, aP2));
215 if (anOriginal.ShapeType() == TopAbs_VERTEX) {
216 P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
218 else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
219 BRepGProp::LinearProperties(anOriginal, System);
220 P1 = System.CentreOfMass();
222 else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
223 BRepGProp::SurfaceProperties(anOriginal, System);
224 P1 = System.CentreOfMass();
227 BRepGProp::VolumeProperties(anOriginal, System);
228 P1 = System.CentreOfMass();
231 Handle(Geom_Line) Line = new Geom_Line(AX1);
232 GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
233 gp_Pnt P2 = aPrjTool.NearestPoint();
235 if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
237 gp_Vec Vec (P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
240 Standard_Integer nbtimes2 = RI.GetNbIter2();
241 Standard_Integer nbtimes1 = RI.GetNbIter1();
242 Standard_Real step = RI.GetStep();
243 Standard_Real ang = RI.GetAngle();
245 TopLoc_Location aLocOrig = anOriginal.Location();
246 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
249 TopoDS_Compound aCompound;
251 B.MakeCompound( aCompound );
253 Standard_Real DX, DY, DZ;
255 for (int i = 0; i < nbtimes2; i++ ) {
256 DX = i * step * Vec.X();
257 DY = i * step * Vec.Y();
258 DZ = i * step * Vec.Z();
259 aVec.SetCoord( DX, DY, DZ );
260 aTrsf1.SetTranslation(aVec);
262 for (int j = 0; j < nbtimes1; j++ ) {
263 if (j == 0) { // NPAL19665
264 TopLoc_Location aLocRes (aTrsf1 * aTrsfOrig);
265 B.Add(aCompound, anOriginal.Located(aLocRes));
268 aTrsf2.SetRotation(AX1, j*ang*PI180);
269 //TopLoc_Location aLocRes (aTrsf2 * aTrsf1 * aTrsfOrig); // gp_Trsf::Multiply() has a bug
270 gp_Trsf aTrsfNew (aTrsfOrig);
271 aTrsfNew.PreMultiply(aTrsf1);
272 aTrsfNew.PreMultiply(aTrsf2);
273 TopLoc_Location aLocRes (aTrsfNew);
274 B.Add(aCompound, anOriginal.Located(aLocRes));
276 //NPAL18620: performance problem: multiple locations are accumulated
277 // in shape and need a great time to process
278 //BRepBuilderAPI_Transform aBRepTrsf1 (anOriginal, aTrsf1, Standard_False);
279 //BRepBuilderAPI_Transform aBRepTrsf2 (aBRepTrsf1.Shape(), aTrsf2, Standard_False);
280 //B.Add(aCompound, aBRepTrsf2.Shape());
289 if (aShape.IsNull()) return 0;
291 aFunction->SetValue(aShape);
293 log.SetTouched(Label());
299 //=======================================================================
300 //function : GEOMImpl_RotateDriver_Type_
302 //=======================================================================
303 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
305 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
306 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
307 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
308 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
309 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
310 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
312 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
313 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
314 sizeof(GEOMImpl_RotateDriver),
316 (Standard_Address)_Ancestors,
317 (Standard_Address)NULL);
322 //=======================================================================
323 //function : DownCast
325 //=======================================================================
326 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
328 Handle(GEOMImpl_RotateDriver) _anOtherObject;
330 if (!AnObject.IsNull()) {
331 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
332 _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
336 return _anOtherObject;