1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include <Standard_Stream.hxx>
23 #include <GEOMImpl_RotateDriver.hxx>
24 #include <GEOMImpl_IRotate.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27 #include <gp_Trsf.hxx>
32 #include <BRepBuilderAPI_Transform.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Compound.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <BRep_Tool.hxx>
42 #include <BRep_Builder.hxx>
43 #include <GeomAPI_ProjectPointOnCurve.hxx>
44 #include <Geom_Line.hxx>
45 #include <GProp_GProps.hxx>
46 #include <BRepGProp.hxx>
47 #include <Precision.hxx>
49 //=======================================================================
52 //=======================================================================
53 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
55 static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
60 //=======================================================================
61 //function : GEOMImpl_RotateDriver
63 //=======================================================================
65 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver()
69 //=======================================================================
72 //=======================================================================
73 Standard_Integer GEOMImpl_RotateDriver::Execute(TFunction_Logbook& log) const
75 if (Label().IsNull()) return 0;
76 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
78 if (aFunction.IsNull()) return 0;
80 GEOMImpl_IRotate RI(aFunction);
83 Standard_Integer aType = aFunction->GetType();
84 Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
85 if (anOriginalFunction.IsNull()) return 0;
86 TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
87 if (anOriginal.IsNull()) return 0;
89 if (aType == ROTATE || aType == ROTATE_COPY) {
90 Handle(GEOM_Function) anAxis = RI.GetAxis();
91 if (anAxis.IsNull()) return 0;
92 TopoDS_Shape A = anAxis->GetValue();
93 if (A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
94 TopoDS_Edge anEdge = TopoDS::Edge(A);
96 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
97 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
98 gp_Dir aDir(gp_Vec(aP1, aP2));
99 gp_Ax1 anAx1(aP1, aDir);
100 Standard_Real anAngle = RI.GetAngle();
101 if (fabs(anAngle) < Precision::Angular()) anAngle += 2*PI; // NPAL19665,19769
102 aTrsf.SetRotation(anAx1, anAngle);
104 //NPAL18620: performance problem: multiple locations are accumulated
105 // in shape and need a great time to process
106 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
107 //aShape = aTransformation.Shape();
108 TopLoc_Location aLocOrig = anOriginal.Location();
109 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
110 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
111 aTrsfOrig.PreMultiply(aTrsf);
112 TopLoc_Location aLocRes (aTrsfOrig);
113 aShape = anOriginal.Located(aLocRes);
115 else if (aType == ROTATE_THREE_POINTS || aType == ROTATE_THREE_POINTS_COPY) {
116 Handle(GEOM_Function) aCentPoint = RI.GetCentPoint();
117 Handle(GEOM_Function) aPoint1 = RI.GetPoint1();
118 Handle(GEOM_Function) aPoint2 = RI.GetPoint2();
119 if(aCentPoint.IsNull() || aPoint1.IsNull() || aPoint2.IsNull()) return 0;
120 TopoDS_Shape aCV = aCentPoint->GetValue();
121 TopoDS_Shape aV1 = aPoint1->GetValue();
122 TopoDS_Shape aV2 = aPoint2->GetValue();
123 if(aCV.IsNull() || aCV.ShapeType() != TopAbs_VERTEX) return 0;
124 if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
125 if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
127 aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
128 aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
129 aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
131 gp_Vec aVec1 (aCP, aP1);
132 gp_Vec aVec2 (aCP, aP2);
133 gp_Dir aDir (aVec1 ^ aVec2);
134 gp_Ax1 anAx1 (aCP, aDir);
135 Standard_Real anAngle = aVec1.Angle(aVec2);
136 if (fabs(anAngle) < Precision::Angular()) anAngle += 2*PI; // NPAL19665
137 aTrsf.SetRotation(anAx1, anAngle);
138 //NPAL18620: performance problem: multiple locations are accumulated
139 // in shape and need a great time to process
140 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
141 //aShape = aTransformation.Shape();
142 TopLoc_Location aLocOrig = anOriginal.Location();
143 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
144 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
145 aTrsfOrig.PreMultiply(aTrsf);
146 TopLoc_Location aLocRes (aTrsfOrig);
147 aShape = anOriginal.Located(aLocRes);
149 else if (aType == ROTATE_1D) {
151 Handle(GEOM_Function) anAxis = RI.GetAxis();
152 if(anAxis.IsNull()) return 0;
153 TopoDS_Shape A = anAxis->GetValue();
154 if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
155 TopoDS_Edge anEdge = TopoDS::Edge(A);
157 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
158 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
159 gp_Dir D(gp_Vec(aP1, aP2));
163 Standard_Integer nbtimes = RI.GetNbIter1();
164 Standard_Real angle = 360.0/nbtimes;
166 TopoDS_Compound aCompound;
168 B.MakeCompound( aCompound );
170 TopLoc_Location aLocOrig = anOriginal.Location();
171 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
173 for (int i = 0; i < nbtimes; i++ ) {
174 if (i == 0) { // NPAL19665
175 B.Add(aCompound, anOriginal);
178 aTrsf.SetRotation(AX1, i*angle*PI180);
179 //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
180 gp_Trsf aTrsfNew (aTrsfOrig);
181 aTrsfNew.PreMultiply(aTrsf);
182 TopLoc_Location aLocRes (aTrsfNew);
183 B.Add(aCompound, anOriginal.Located(aLocRes));
185 //NPAL18620: performance problem: multiple locations are accumulated
186 // in shape and need a great time to process
187 //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
188 //B.Add(aCompound, aBRepTransformation.Shape());
193 else if (aType == ROTATE_2D) {
195 Handle(GEOM_Function) anAxis = RI.GetAxis();
196 if(anAxis.IsNull()) return 0;
197 TopoDS_Shape A = anAxis->GetValue();
198 if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
199 TopoDS_Edge anEdge = TopoDS::Edge(A);
200 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
201 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
202 gp_Dir D(gp_Vec(aP1, aP2));
212 if (anOriginal.ShapeType() == TopAbs_VERTEX) {
213 P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
215 else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
216 BRepGProp::LinearProperties(anOriginal, System);
217 P1 = System.CentreOfMass();
219 else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
220 BRepGProp::SurfaceProperties(anOriginal, System);
221 P1 = System.CentreOfMass();
224 BRepGProp::VolumeProperties(anOriginal, System);
225 P1 = System.CentreOfMass();
228 Handle(Geom_Line) Line = new Geom_Line(AX1);
229 GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
230 gp_Pnt P2 = aPrjTool.NearestPoint();
232 if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
234 gp_Vec Vec (P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
237 Standard_Integer nbtimes2 = RI.GetNbIter2();
238 Standard_Integer nbtimes1 = RI.GetNbIter1();
239 Standard_Real step = RI.GetStep();
240 Standard_Real ang = RI.GetAngle();
242 TopLoc_Location aLocOrig = anOriginal.Location();
243 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
246 TopoDS_Compound aCompound;
248 B.MakeCompound( aCompound );
250 Standard_Real DX, DY, DZ;
252 for (int i = 0; i < nbtimes2; i++ ) {
253 DX = i * step * Vec.X();
254 DY = i * step * Vec.Y();
255 DZ = i * step * Vec.Z();
256 aVec.SetCoord( DX, DY, DZ );
257 aTrsf1.SetTranslation(aVec);
259 for (int j = 0; j < nbtimes1; j++ ) {
260 if (j == 0) { // NPAL19665
261 TopLoc_Location aLocRes (aTrsf1 * aTrsfOrig);
262 B.Add(aCompound, anOriginal.Located(aLocRes));
265 aTrsf2.SetRotation(AX1, j*ang*PI180);
266 //TopLoc_Location aLocRes (aTrsf2 * aTrsf1 * aTrsfOrig); // gp_Trsf::Multiply() has a bug
267 gp_Trsf aTrsfNew (aTrsfOrig);
268 aTrsfNew.PreMultiply(aTrsf1);
269 aTrsfNew.PreMultiply(aTrsf2);
270 TopLoc_Location aLocRes (aTrsfNew);
271 B.Add(aCompound, anOriginal.Located(aLocRes));
273 //NPAL18620: performance problem: multiple locations are accumulated
274 // in shape and need a great time to process
275 //BRepBuilderAPI_Transform aBRepTrsf1 (anOriginal, aTrsf1, Standard_False);
276 //BRepBuilderAPI_Transform aBRepTrsf2 (aBRepTrsf1.Shape(), aTrsf2, Standard_False);
277 //B.Add(aCompound, aBRepTrsf2.Shape());
286 if (aShape.IsNull()) return 0;
288 aFunction->SetValue(aShape);
290 log.SetTouched(Label());
296 //=======================================================================
297 //function : GEOMImpl_RotateDriver_Type_
299 //=======================================================================
300 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
302 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
303 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
304 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
305 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
306 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
307 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
309 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
310 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
311 sizeof(GEOMImpl_RotateDriver),
313 (Standard_Address)_Ancestors,
314 (Standard_Address)NULL);
319 //=======================================================================
320 //function : DownCast
322 //=======================================================================
323 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
325 Handle(GEOMImpl_RotateDriver) _anOtherObject;
327 if (!AnObject.IsNull()) {
328 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
329 _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
333 return _anOtherObject;