1 // Copyright (C) 2007-2010 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
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_RotateDriver.hxx>
26 #include <GEOMImpl_IRotate.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29 #include <gp_Trsf.hxx>
34 #include <BRepBuilderAPI_Transform.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Compound.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <BRep_Tool.hxx>
44 #include <BRep_Builder.hxx>
45 #include <GeomAPI_ProjectPointOnCurve.hxx>
46 #include <Geom_Line.hxx>
47 #include <GProp_GProps.hxx>
48 #include <BRepGProp.hxx>
49 #include <Precision.hxx>
51 //=======================================================================
54 //=======================================================================
55 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
57 static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
62 //=======================================================================
63 //function : GEOMImpl_RotateDriver
65 //=======================================================================
67 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver()
71 //=======================================================================
74 //=======================================================================
75 Standard_Integer GEOMImpl_RotateDriver::Execute(TFunction_Logbook& log) const
77 if (Label().IsNull()) return 0;
78 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
80 if (aFunction.IsNull()) return 0;
82 GEOMImpl_IRotate RI(aFunction);
85 Standard_Integer aType = aFunction->GetType();
86 Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
87 if (anOriginalFunction.IsNull()) return 0;
88 TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
89 if (anOriginal.IsNull()) return 0;
91 if (aType == ROTATE || aType == ROTATE_COPY) {
92 Handle(GEOM_Function) anAxis = RI.GetAxis();
93 if (anAxis.IsNull()) return 0;
94 TopoDS_Shape A = anAxis->GetValue();
95 if (A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
96 TopoDS_Edge anEdge = TopoDS::Edge(A);
98 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
99 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
100 gp_Dir aDir(gp_Vec(aP1, aP2));
101 gp_Ax1 anAx1(aP1, aDir);
102 Standard_Real anAngle = RI.GetAngle();
103 if (fabs(anAngle) < Precision::Angular()) anAngle += 2*PI; // NPAL19665,19769
104 aTrsf.SetRotation(anAx1, anAngle);
106 //NPAL18620: performance problem: multiple locations are accumulated
107 // in shape and need a great time to process
108 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
109 //aShape = aTransformation.Shape();
110 TopLoc_Location aLocOrig = anOriginal.Location();
111 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
112 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
113 aTrsfOrig.PreMultiply(aTrsf);
114 TopLoc_Location aLocRes (aTrsfOrig);
115 aShape = anOriginal.Located(aLocRes);
117 else if (aType == ROTATE_THREE_POINTS || aType == ROTATE_THREE_POINTS_COPY) {
118 Handle(GEOM_Function) aCentPoint = RI.GetCentPoint();
119 Handle(GEOM_Function) aPoint1 = RI.GetPoint1();
120 Handle(GEOM_Function) aPoint2 = RI.GetPoint2();
121 if(aCentPoint.IsNull() || aPoint1.IsNull() || aPoint2.IsNull()) return 0;
122 TopoDS_Shape aCV = aCentPoint->GetValue();
123 TopoDS_Shape aV1 = aPoint1->GetValue();
124 TopoDS_Shape aV2 = aPoint2->GetValue();
125 if(aCV.IsNull() || aCV.ShapeType() != TopAbs_VERTEX) return 0;
126 if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
127 if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
129 aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
130 aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
131 aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
133 gp_Vec aVec1 (aCP, aP1);
134 gp_Vec aVec2 (aCP, aP2);
135 gp_Dir aDir (aVec1 ^ aVec2);
136 gp_Ax1 anAx1 (aCP, aDir);
137 Standard_Real anAngle = aVec1.Angle(aVec2);
138 if (fabs(anAngle) < Precision::Angular()) anAngle += 2*PI; // NPAL19665
139 aTrsf.SetRotation(anAx1, anAngle);
140 //NPAL18620: performance problem: multiple locations are accumulated
141 // in shape and need a great time to process
142 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
143 //aShape = aTransformation.Shape();
144 TopLoc_Location aLocOrig = anOriginal.Location();
145 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
146 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
147 aTrsfOrig.PreMultiply(aTrsf);
148 TopLoc_Location aLocRes (aTrsfOrig);
149 aShape = anOriginal.Located(aLocRes);
151 else if (aType == ROTATE_1D) {
153 Handle(GEOM_Function) anAxis = RI.GetAxis();
154 if(anAxis.IsNull()) return 0;
155 TopoDS_Shape A = anAxis->GetValue();
156 if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
157 TopoDS_Edge anEdge = TopoDS::Edge(A);
159 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
160 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
161 gp_Dir D(gp_Vec(aP1, aP2));
165 Standard_Integer nbtimes = RI.GetNbIter1();
166 Standard_Real angle = 360.0/nbtimes;
168 TopoDS_Compound aCompound;
170 B.MakeCompound( aCompound );
172 TopLoc_Location aLocOrig = anOriginal.Location();
173 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
175 for (int i = 0; i < nbtimes; i++ ) {
176 if (i == 0) { // NPAL19665
177 B.Add(aCompound, anOriginal);
180 aTrsf.SetRotation(AX1, i*angle*PI180);
181 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
182 gp_Trsf aTrsfNew (aTrsfOrig);
183 aTrsfNew.PreMultiply(aTrsf);
184 TopLoc_Location aLocRes (aTrsfNew);
185 B.Add(aCompound, anOriginal.Located(aLocRes));
187 //NPAL18620: performance problem: multiple locations are accumulated
188 // in shape and need a great time to process
189 //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
190 //B.Add(aCompound, aBRepTransformation.Shape());
195 else if (aType == ROTATE_2D) {
197 Handle(GEOM_Function) anAxis = RI.GetAxis();
198 if(anAxis.IsNull()) return 0;
199 TopoDS_Shape A = anAxis->GetValue();
200 if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
201 TopoDS_Edge anEdge = TopoDS::Edge(A);
202 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
203 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
204 gp_Dir D(gp_Vec(aP1, aP2));
214 if (anOriginal.ShapeType() == TopAbs_VERTEX) {
215 P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
217 else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
218 BRepGProp::LinearProperties(anOriginal, System);
219 P1 = System.CentreOfMass();
221 else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
222 BRepGProp::SurfaceProperties(anOriginal, System);
223 P1 = System.CentreOfMass();
226 BRepGProp::VolumeProperties(anOriginal, System);
227 P1 = System.CentreOfMass();
230 Handle(Geom_Line) Line = new Geom_Line(AX1);
231 GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
232 gp_Pnt P2 = aPrjTool.NearestPoint();
234 if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
236 gp_Vec Vec (P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
239 Standard_Integer nbtimes2 = RI.GetNbIter2();
240 Standard_Integer nbtimes1 = RI.GetNbIter1();
241 Standard_Real step = RI.GetStep();
242 Standard_Real ang = RI.GetAngle();
244 TopLoc_Location aLocOrig = anOriginal.Location();
245 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
248 TopoDS_Compound aCompound;
250 B.MakeCompound( aCompound );
252 Standard_Real DX, DY, DZ;
254 for (int i = 0; i < nbtimes2; i++ ) {
255 DX = i * step * Vec.X();
256 DY = i * step * Vec.Y();
257 DZ = i * step * Vec.Z();
258 aVec.SetCoord( DX, DY, DZ );
259 aTrsf1.SetTranslation(aVec);
261 for (int j = 0; j < nbtimes1; j++ ) {
262 if (j == 0) { // NPAL19665
263 TopLoc_Location aLocRes (aTrsf1 * aTrsfOrig);
264 B.Add(aCompound, anOriginal.Located(aLocRes));
267 aTrsf2.SetRotation(AX1, j*ang*PI180);
268 //TopLoc_Location aLocRes (aTrsf2 * aTrsf1 * aTrsfOrig); // gp_Trsf::Multiply() has a bug
269 gp_Trsf aTrsfNew (aTrsfOrig);
270 aTrsfNew.PreMultiply(aTrsf1);
271 aTrsfNew.PreMultiply(aTrsf2);
272 TopLoc_Location aLocRes (aTrsfNew);
273 B.Add(aCompound, anOriginal.Located(aLocRes));
275 //NPAL18620: performance problem: multiple locations are accumulated
276 // in shape and need a great time to process
277 //BRepBuilderAPI_Transform aBRepTrsf1 (anOriginal, aTrsf1, Standard_False);
278 //BRepBuilderAPI_Transform aBRepTrsf2 (aBRepTrsf1.Shape(), aTrsf2, Standard_False);
279 //B.Add(aCompound, aBRepTrsf2.Shape());
288 if (aShape.IsNull()) return 0;
290 aFunction->SetValue(aShape);
292 log.SetTouched(Label());
298 //=======================================================================
299 //function : GEOMImpl_RotateDriver_Type_
301 //=======================================================================
302 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
304 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
305 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
306 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
307 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
308 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
309 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
311 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
312 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
313 sizeof(GEOMImpl_RotateDriver),
315 (Standard_Address)_Ancestors,
316 (Standard_Address)NULL);
321 //=======================================================================
322 //function : DownCast
324 //=======================================================================
325 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
327 Handle(GEOMImpl_RotateDriver) _anOtherObject;
329 if (!AnObject.IsNull()) {
330 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
331 _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
335 return _anOtherObject;