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 aTrsf.SetRotation(anAx1, anAngle);
103 //NPAL18620: performance problem: multiple locations are accumulated
104 // in shape and need a great time to process
105 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
106 //aShape = aTransformation.Shape();
107 TopLoc_Location aLocOrig = anOriginal.Location();
108 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
109 TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
110 aShape = anOriginal.Located(aLocRes);
112 else if (aType == ROTATE_THREE_POINTS || aType == ROTATE_THREE_POINTS_COPY) {
113 Handle(GEOM_Function) aCentPoint = RI.GetCentPoint();
114 Handle(GEOM_Function) aPoint1 = RI.GetPoint1();
115 Handle(GEOM_Function) aPoint2 = RI.GetPoint2();
116 if(aCentPoint.IsNull() || aPoint1.IsNull() || aPoint2.IsNull()) return 0;
117 TopoDS_Shape aCV = aCentPoint->GetValue();
118 TopoDS_Shape aV1 = aPoint1->GetValue();
119 TopoDS_Shape aV2 = aPoint2->GetValue();
120 if(aCV.IsNull() || aCV.ShapeType() != TopAbs_VERTEX) return 0;
121 if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
122 if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
124 aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
125 aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
126 aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
128 gp_Vec aVec1 (aCP, aP1);
129 gp_Vec aVec2 (aCP, aP2);
130 gp_Dir aDir (aVec1 ^ aVec2);
131 gp_Ax1 anAx1 (aCP, aDir);
132 Standard_Real anAngle = aVec1.Angle(aVec2);
133 if (fabs(anAngle) < Precision::Angular()) anAngle += 2*PI; // NPAL19665
134 aTrsf.SetRotation(anAx1, anAngle);
135 //NPAL18620: performance problem: multiple locations are accumulated
136 // in shape and need a great time to process
137 //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
138 //aShape = aTransformation.Shape();
139 TopLoc_Location aLocOrig = anOriginal.Location();
140 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
141 TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
142 aShape = anOriginal.Located(aLocRes);
144 else if (aType == ROTATE_1D) {
146 Handle(GEOM_Function) anAxis = RI.GetAxis();
147 if(anAxis.IsNull()) return 0;
148 TopoDS_Shape A = anAxis->GetValue();
149 if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
150 TopoDS_Edge anEdge = TopoDS::Edge(A);
152 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
153 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
154 gp_Dir D(gp_Vec(aP1, aP2));
158 Standard_Integer nbtimes = RI.GetNbIter1();
159 Standard_Real angle = 360.0/nbtimes;
161 TopoDS_Compound aCompound;
163 B.MakeCompound( aCompound );
165 TopLoc_Location aLocOrig = anOriginal.Location();
166 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
168 for (int i = 0; i < nbtimes; i++ ) {
169 if (i == 0) { // NPAL19665
170 B.Add(aCompound, anOriginal);
173 aTrsf.SetRotation(AX1, i*angle*PI180);
174 TopLoc_Location aLocRes (aTrsf * aTrsfOrig);
175 B.Add(aCompound, anOriginal.Located(aLocRes));
177 //NPAL18620: performance problem: multiple locations are accumulated
178 // in shape and need a great time to process
179 //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
180 //B.Add(aCompound, aBRepTransformation.Shape());
185 else if (aType == ROTATE_2D) {
187 Handle(GEOM_Function) anAxis = RI.GetAxis();
188 if(anAxis.IsNull()) return 0;
189 TopoDS_Shape A = anAxis->GetValue();
190 if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
191 TopoDS_Edge anEdge = TopoDS::Edge(A);
192 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
193 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
194 gp_Dir D(gp_Vec(aP1, aP2));
204 if (anOriginal.ShapeType() == TopAbs_VERTEX) {
205 P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
207 else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
208 BRepGProp::LinearProperties(anOriginal, System);
209 P1 = System.CentreOfMass();
211 else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
212 BRepGProp::SurfaceProperties(anOriginal, System);
213 P1 = System.CentreOfMass();
216 BRepGProp::VolumeProperties(anOriginal, System);
217 P1 = System.CentreOfMass();
220 Handle(Geom_Line) Line = new Geom_Line(AX1);
221 GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
222 gp_Pnt P2 = aPrjTool.NearestPoint();
224 if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
226 gp_Vec Vec (P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
229 Standard_Integer nbtimes2 = RI.GetNbIter2();
230 Standard_Integer nbtimes1 = RI.GetNbIter1();
231 Standard_Real step = RI.GetStep();
232 Standard_Real ang = RI.GetAngle();
234 TopLoc_Location aLocOrig = anOriginal.Location();
235 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
238 TopoDS_Compound aCompound;
240 B.MakeCompound( aCompound );
242 Standard_Real DX, DY, DZ;
245 for (int i = 0; i < nbtimes2; i++ ) {
246 DX = i * step * Vec.X();
247 DY = i * step * Vec.Y();
248 DZ = i * step * Vec.Z();
249 aVec.SetCoord( DX, DY, DZ );
250 aVec = i * step * Vec;
252 aTrsf1.SetTranslation(aVec);
254 for (int j = 0; j < nbtimes1; j++ ) {
255 aTrsf2.SetRotation(AX1, j*ang*PI180);
257 gp_Trsf aTrsf3 = aTrsf1 * aTrsfOrig;
258 gp_Trsf aTrsf4 = aTrsf2 * aTrsf3;
259 gp_Trsf aTrsf5 = aTrsf2 * aTrsf1 * aTrsfOrig;
261 gp_Trsf aTrsf6 = aTrsf2 * aTrsfOrig;
263 gp_Pnt aP1 = P1.Transformed(aTrsf6);
264 gp_Pnt aP2 = P1.Transformed(aTrsf4);
265 gp_Pnt aP3 = P1.Transformed(aTrsf3);
266 gp_Pnt aP4 = P1.Transformed(aTrsf3);
271 for (int i = 0; i < nbtimes2; i++ ) {
272 DX = i * step * Vec.X();
273 DY = i * step * Vec.Y();
274 DZ = i * step * Vec.Z();
275 aVec.SetCoord( DX, DY, DZ );
276 aTrsf1.SetTranslation(aVec);
278 for (int j = 0; j < nbtimes1; j++ ) {
279 if (j == 0) { // NPAL19665
280 TopLoc_Location aLocRes (aTrsf1 * aTrsfOrig);
281 B.Add(aCompound, anOriginal.Located(aLocRes));
284 aTrsf2.SetRotation(AX1, j*ang*PI180);
285 TopLoc_Location aLocRes (aTrsf2 * aTrsf1 * aTrsfOrig);
286 B.Add(aCompound, anOriginal.Located(aLocRes));
288 //NPAL18620: performance problem: multiple locations are accumulated
289 // in shape and need a great time to process
290 //BRepBuilderAPI_Transform aBRepTrsf1 (anOriginal, aTrsf1, Standard_False);
291 //BRepBuilderAPI_Transform aBRepTrsf2 (aBRepTrsf1.Shape(), aTrsf2, Standard_False);
292 //B.Add(aCompound, aBRepTrsf2.Shape());
301 if (aShape.IsNull()) return 0;
303 aFunction->SetValue(aShape);
305 log.SetTouched(Label());
311 //=======================================================================
312 //function : GEOMImpl_RotateDriver_Type_
314 //=======================================================================
315 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
317 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
318 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
319 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
320 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
321 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
322 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
324 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
325 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
326 sizeof(GEOMImpl_RotateDriver),
328 (Standard_Address)_Ancestors,
329 (Standard_Address)NULL);
334 //=======================================================================
335 //function : DownCast
337 //=======================================================================
338 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
340 Handle(GEOMImpl_RotateDriver) _anOtherObject;
342 if (!AnObject.IsNull()) {
343 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
344 _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
348 return _anOtherObject;