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