Salome HOME
4f656dd5e87d6c249ea25f5f25481b63005202d5
[modules/geom.git] / src / GEOMImpl / GEOMImpl_RotateDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_RotateDriver.hxx"
4 #include <GEOMImpl_IRotate.hxx>
5 #include <GEOMImpl_Types.hxx>
6 #include "GEOM_Function.hxx"
7 #include <gp_Trsf.hxx>
8 #include <gp_Pnt.hxx>
9 #include <gp_Vec.hxx>
10 #include <gp_Dir.hxx>
11 #include <gp_Ax1.hxx>
12 #include <BRepBuilderAPI_Transform.hxx>
13 #include <TopoDS.hxx>
14 #include <TopoDS_Vertex.hxx>
15 #include <TopoDS_Shape.hxx>
16 #include <TopoDS_Compound.hxx>
17 #include <TopAbs.hxx>
18 #include <TopExp.hxx>
19 #include <TopoDS_Vertex.hxx>
20 #include <TopoDS_Edge.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRep_Builder.hxx>
23 #include <GeomAPI_ProjectPointOnCurve.hxx>
24 #include <Geom_Line.hxx>
25 #include <GProp_GProps.hxx>
26 #include <BRepGProp.hxx>
27 #include <Precision.hxx>
28
29 //=======================================================================
30 //function : GetID
31 //purpose  :
32 //======================================================================= 
33 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
34 {
35   static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
36   return aRotateDriver; 
37 }
38
39
40 //=======================================================================
41 //function : GEOMImpl_RotateDriver
42 //purpose  : 
43 //=======================================================================
44
45 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver() 
46 {
47 }
48
49 //=======================================================================
50 //function : Execute
51 //purpose  :
52 //======================================================================= 
53 Standard_Integer GEOMImpl_RotateDriver::Execute(TFunction_Logbook& log) const
54 {
55   if(Label().IsNull())  return 0;    
56   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
57
58   if(aFunction.IsNull()) return 0;
59
60   GEOMImpl_IRotate RI(aFunction);
61   gp_Trsf aTrsf;
62   Standard_Integer aType = aFunction->GetType();
63   Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
64   if(anOriginalFunction.IsNull()) return 0;
65   TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
66   if(anOriginal.IsNull()) return 0;
67
68   if(aType == ROTATE || aType == ROTATE_COPY) {
69     Handle(GEOM_Function) anAxis = RI.GetAxis();
70     if(anAxis.IsNull()) return 0;
71     TopoDS_Shape A = anAxis->GetValue();
72     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
73     TopoDS_Edge anEdge = TopoDS::Edge(A);
74
75     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
76     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
77     gp_Dir aDir(gp_Vec(aP1, aP2));
78     gp_Ax1 anAx1(aP1, aDir);
79     Standard_Real anAngle = RI.GetAngle();
80     aTrsf.SetRotation(anAx1, anAngle);
81     
82     BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
83     aShape = aTransformation.Shape();
84   }
85   else if(aType == ROTATE_1D) {
86     //Get direction
87     Handle(GEOM_Function) anAxis = RI.GetAxis();
88     if(anAxis.IsNull()) return 0;
89     TopoDS_Shape A = anAxis->GetValue();
90     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
91     TopoDS_Edge anEdge = TopoDS::Edge(A);
92
93     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
94     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
95     gp_Dir D(gp_Vec(aP1, aP2)) ;
96
97     gp_Ax1 AX1(aP1, D) ;
98
99     Standard_Integer nbtimes = RI.GetNbIter1();
100     Standard_Real angle = 360.0/nbtimes ;
101
102     TopoDS_Compound aCompound;
103     BRep_Builder B;
104     B.MakeCompound( aCompound );
105     
106     for (int i = 0; i < nbtimes; i++ ) {
107       aTrsf.SetRotation(AX1, i*angle*PI180) ;
108       BRepBuilderAPI_Transform myBRepTransformation(anOriginal, aTrsf, Standard_False) ;
109       B.Add( aCompound, myBRepTransformation.Shape() );
110     }
111
112     aShape = aCompound ;
113   }
114   else if(aType == ROTATE_2D) {
115     Standard_Real DX, DY, DZ ;
116
117     //Get direction
118     Handle(GEOM_Function) anAxis = RI.GetAxis();
119     if(anAxis.IsNull()) return 0;
120     TopoDS_Shape A = anAxis->GetValue();
121     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
122     TopoDS_Edge anEdge = TopoDS::Edge(A);
123     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
124     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
125     gp_Dir D(gp_Vec(aP1, aP2)) ;
126
127     gp_Ax1 AX1(aP1, D) ;
128
129
130     gp_Trsf theTransformation1 ;
131     gp_Trsf theTransformation2 ;
132     gp_Pnt P1 ;
133     GProp_GProps System ;
134     
135     if ( anOriginal.ShapeType() == TopAbs_VERTEX) {
136       P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
137     } 
138     else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
139       BRepGProp::LinearProperties(anOriginal, System);
140       P1 = System.CentreOfMass() ;
141     }
142     else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
143       BRepGProp::SurfaceProperties(anOriginal, System);
144       P1 = System.CentreOfMass() ;
145     }
146     else {
147       BRepGProp::VolumeProperties(anOriginal, System);
148       P1 = System.CentreOfMass() ;
149     }
150     
151     Handle(Geom_Line) Line = new Geom_Line(AX1);
152     gp_Pnt P2 = GeomAPI_ProjectPointOnCurve( P1, Line ) ;
153     
154     if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
155     
156     gp_Vec Vec(P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z()) ;
157     Vec.Normalize();
158     
159     Standard_Integer nbtimes2 = RI.GetNbIter2();
160     Standard_Integer nbtimes1 = RI.GetNbIter1();
161     Standard_Real step = RI.GetStep();
162     Standard_Real ang = RI.GetAngle();
163
164     gp_Vec myVec ;
165     TopoDS_Compound aCompound;
166     BRep_Builder B;
167     B.MakeCompound( aCompound );
168     for (int i = 0; i < nbtimes2; i++ ) {
169       for (int j = 0; j < nbtimes1; j++ ) {
170         DX = i * step * Vec.X() ;
171         DY = i * step * Vec.Y() ;
172         DZ = i * step * Vec.Z() ;
173         myVec.SetCoord( DX, DY, DZ ) ;
174         theTransformation1.SetTranslation(myVec) ;
175         theTransformation2.SetRotation(AX1, j*ang*PI180) ;
176         BRepBuilderAPI_Transform myBRepTransformation1(anOriginal, theTransformation1, Standard_False) ;
177         BRepBuilderAPI_Transform myBRepTransformation2(myBRepTransformation1.Shape(), theTransformation2, Standard_False) ;
178         B.Add( aCompound, myBRepTransformation2.Shape() );
179       }
180     }
181
182     aShape = aCompound;
183
184   }
185   else return 0;
186
187  
188
189
190   if (aShape.IsNull()) return 0;
191
192   aFunction->SetValue(aShape);
193
194   log.SetTouched(Label()); 
195
196   return 1;
197 }
198
199
200 //=======================================================================
201 //function :  GEOMImpl_RotateDriver_Type_
202 //purpose  :
203 //======================================================================= 
204 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
205 {
206
207   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
208   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
209   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
210   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
211   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
212   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
213  
214
215   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
216   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
217                                                          sizeof(GEOMImpl_RotateDriver),
218                                                          1,
219                                                          (Standard_Address)_Ancestors,
220                                                          (Standard_Address)NULL);
221
222   return _aType;
223 }
224
225 //=======================================================================
226 //function : DownCast
227 //purpose  :
228 //======================================================================= 
229
230 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
231 {
232   Handle(GEOMImpl_RotateDriver) _anOtherObject;
233
234   if (!AnObject.IsNull()) {
235      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
236        _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
237      }
238   }
239
240   return _anOtherObject ;
241 }
242
243