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/
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);
82 Standard_Integer aType = aFunction->GetType();
83 Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
84 if(anOriginalFunction.IsNull()) return 0;
85 TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
86 if(anOriginal.IsNull()) return 0;
88 if(aType == ROTATE || aType == ROTATE_COPY) {
89 Handle(GEOM_Function) anAxis = RI.GetAxis();
90 if(anAxis.IsNull()) return 0;
91 TopoDS_Shape A = anAxis->GetValue();
92 if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
93 TopoDS_Edge anEdge = TopoDS::Edge(A);
95 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
96 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
97 gp_Dir aDir(gp_Vec(aP1, aP2));
98 gp_Ax1 anAx1(aP1, aDir);
99 Standard_Real anAngle = RI.GetAngle();
100 aTrsf.SetRotation(anAx1, anAngle);
102 BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
103 aShape = aTransformation.Shape();
105 else if(aType == ROTATE_1D) {
107 Handle(GEOM_Function) anAxis = RI.GetAxis();
108 if(anAxis.IsNull()) return 0;
109 TopoDS_Shape A = anAxis->GetValue();
110 if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
111 TopoDS_Edge anEdge = TopoDS::Edge(A);
113 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
114 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
115 gp_Dir D(gp_Vec(aP1, aP2)) ;
119 Standard_Integer nbtimes = RI.GetNbIter1();
120 Standard_Real angle = 360.0/nbtimes ;
122 TopoDS_Compound aCompound;
124 B.MakeCompound( aCompound );
126 for (int i = 0; i < nbtimes; i++ ) {
127 aTrsf.SetRotation(AX1, i*angle*PI180) ;
128 BRepBuilderAPI_Transform myBRepTransformation(anOriginal, aTrsf, Standard_False) ;
129 B.Add( aCompound, myBRepTransformation.Shape() );
134 else if(aType == ROTATE_2D) {
135 Standard_Real DX, DY, DZ ;
138 Handle(GEOM_Function) anAxis = RI.GetAxis();
139 if(anAxis.IsNull()) return 0;
140 TopoDS_Shape A = anAxis->GetValue();
141 if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
142 TopoDS_Edge anEdge = TopoDS::Edge(A);
143 gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
144 gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
145 gp_Dir D(gp_Vec(aP1, aP2)) ;
150 gp_Trsf theTransformation1 ;
151 gp_Trsf theTransformation2 ;
153 GProp_GProps System ;
155 if ( anOriginal.ShapeType() == TopAbs_VERTEX) {
156 P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
158 else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
159 BRepGProp::LinearProperties(anOriginal, System);
160 P1 = System.CentreOfMass() ;
162 else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
163 BRepGProp::SurfaceProperties(anOriginal, System);
164 P1 = System.CentreOfMass() ;
167 BRepGProp::VolumeProperties(anOriginal, System);
168 P1 = System.CentreOfMass() ;
171 Handle(Geom_Line) Line = new Geom_Line(AX1);
172 GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line ) ;
173 gp_Pnt P2 = aPrjTool.NearestPoint();
175 if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
177 gp_Vec Vec(P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z()) ;
180 Standard_Integer nbtimes2 = RI.GetNbIter2();
181 Standard_Integer nbtimes1 = RI.GetNbIter1();
182 Standard_Real step = RI.GetStep();
183 Standard_Real ang = RI.GetAngle();
186 TopoDS_Compound aCompound;
188 B.MakeCompound( aCompound );
189 for (int i = 0; i < nbtimes2; i++ ) {
190 for (int j = 0; j < nbtimes1; j++ ) {
191 DX = i * step * Vec.X() ;
192 DY = i * step * Vec.Y() ;
193 DZ = i * step * Vec.Z() ;
194 myVec.SetCoord( DX, DY, DZ ) ;
195 theTransformation1.SetTranslation(myVec) ;
196 theTransformation2.SetRotation(AX1, j*ang*PI180) ;
197 BRepBuilderAPI_Transform myBRepTransformation1(anOriginal, theTransformation1, Standard_False) ;
198 BRepBuilderAPI_Transform myBRepTransformation2(myBRepTransformation1.Shape(), theTransformation2, Standard_False) ;
199 B.Add( aCompound, myBRepTransformation2.Shape() );
211 if (aShape.IsNull()) return 0;
213 aFunction->SetValue(aShape);
215 log.SetTouched(Label());
221 //=======================================================================
222 //function : GEOMImpl_RotateDriver_Type_
224 //=======================================================================
225 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
228 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
229 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
230 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
231 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
232 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
233 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
236 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
237 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
238 sizeof(GEOMImpl_RotateDriver),
240 (Standard_Address)_Ancestors,
241 (Standard_Address)NULL);
246 //=======================================================================
247 //function : DownCast
249 //=======================================================================
251 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
253 Handle(GEOMImpl_RotateDriver) _anOtherObject;
255 if (!AnObject.IsNull()) {
256 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
257 _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
261 return _anOtherObject ;