]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_DividedDiskDriver.cxx
Salome HOME
EDF 2281 : code refactoring and first steps for a second constructor of the divided...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_DividedDiskDriver.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_DividedDiskDriver.hxx>
25 #include <GEOMImpl_IDividedDisk.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 // OCCT includes
30 #include <gp_Pnt.hxx>
31 #include <gp_Dir.hxx>
32 #include <gp_Lin.hxx>
33 #include <gp_Circ.hxx>
34 #include <gp_Ax1.hxx>
35 #include <gp_Ax2.hxx>
36
37 #include <BRep_Builder.hxx>
38 #include <BRepBuilderAPI_MakeFace.hxx>
39 #include <BRepBuilderAPI_MakeEdge.hxx>
40 #include <BRepBuilderAPI_MakeVertex.hxx>
41 #include <BRepBuilderAPI_MakeWire.hxx>
42 #include <BRepBuilderAPI_Transform.hxx>
43
44 #include <Geom_Plane.hxx>
45
46 #include <TopoDS.hxx>
47 #include <TopoDS_Shape.hxx>
48 #include <TopoDS_Edge.hxx>
49
50 #include <TFunction_Logbook.hxx>
51 #include <StdFail_NotDone.hxx>
52
53 #include <utilities.h>
54
55 //@@ include required header files here @@//
56
57 //=======================================================================
58 //function : GetID
59 //purpose  :
60 //=======================================================================
61 const Standard_GUID& GEOMImpl_DividedDiskDriver::GetID()
62 {
63   static Standard_GUID aGUID("0b01da9a-c5da-11e1-8d80-78e7d1879630");
64   return aGUID;
65 }
66
67 //=======================================================================
68 //function : GEOMImpl_DividedDiskDriver
69 //purpose  :
70 //=======================================================================
71 GEOMImpl_DividedDiskDriver::GEOMImpl_DividedDiskDriver()
72 {
73 }
74
75 //=======================================================================
76 //function : Execute
77 //purpose  :
78 //=======================================================================
79 Standard_Integer GEOMImpl_DividedDiskDriver::Execute(TFunction_Logbook& log) const
80 {
81   if (Label().IsNull()) return 0;
82   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
83
84   GEOMImpl_IDividedDisk aData (aFunction);
85   Standard_Integer aType = aFunction->GetType();
86
87   TopoDS_Shape aShape;
88
89   // Getting data
90   double R           = aData.GetR();
91   double Ratio       = aData.GetRatio();
92   
93   // Build reference disk (in the global coordinate system)
94   TopoDS_Shell S = MakeDisk( R, Ratio );
95   
96   if (aType == DIVIDEDDISK_R_RATIO) { 
97     int theOrientation = aData.GetOrientation();        
98     aShape = TransformShape(S, theOrientation);   
99   }
100   else if (aType == DIVIDEDDISK_R_VECTOR_PNT){
101     // other construction modes here
102     gp_Pnt P = gp::Origin();
103     gp_Dir V = gp::DZ();
104     aShape = TransformShape(S, P, V);  
105   }
106
107   if (aShape.IsNull()) return 0;
108
109   aFunction->SetValue(aShape);
110
111   log.SetTouched(Label());
112
113   return 1;
114 }
115
116
117 //=======================================================================
118 //function : MakeDisk
119 //purpose  :
120 //=======================================================================
121 TopoDS_Shell GEOMImpl_DividedDiskDriver::MakeDisk(double R, double Ratio) const
122 {
123   // Geometry
124   gp_Dir ZDir(0,0,1);
125   gp_Dir XDir(1,0,0);
126   gp_Pnt Orig(0,0,0);
127   
128   // Circle
129   gp_Ax1 Ax1(Orig,ZDir);
130   gp_Ax2 Ax(Orig,ZDir,XDir);
131   gp_Circ aCircle(Ax, R);
132   
133   // Points
134   gp_Pnt P1(0.01*Ratio*R,0,0);
135   gp_Pnt P2(R,0,0);
136   gp_Pnt P3 = P2.Rotated(Ax1,M_PI/6.0);
137   gp_Pnt P4(P1.X(), 
138             P1.X()/sqrt(3.0),0);
139   
140   //surfaces
141   gp_Ax2 anAx (gp::XOY());
142   Handle(Geom_Plane) aPlane = new Geom_Plane (anAx);
143   
144   // Topology
145   
146   // Vertices
147   TopoDS_Vertex O  = BRepBuilderAPI_MakeVertex(Orig);
148   TopoDS_Vertex V1_init = BRepBuilderAPI_MakeVertex(P1);
149   TopoDS_Vertex V2_init = BRepBuilderAPI_MakeVertex(P2);
150   TopoDS_Vertex V3 = BRepBuilderAPI_MakeVertex(P3);
151   TopoDS_Vertex V4 = BRepBuilderAPI_MakeVertex(P4);
152   
153   TopoDS_Vertex V1 = V1_init;
154   TopoDS_Vertex V2 = V2_init;
155   
156   //Rotation
157   gp_Trsf myTrsf;
158   myTrsf.SetRotation(Ax1, M_PI/3.0);
159   
160   BRepBuilderAPI_Transform xform(myTrsf);
161   xform.Perform(V1,Standard_True);
162   TopoDS_Vertex V1_60 = TopoDS::Vertex(xform.Shape()); 
163   xform.Perform(V2,Standard_True);
164   TopoDS_Vertex V2_60 = TopoDS::Vertex(xform.Shape());
165   
166   // Declaration of shapes (used in the loop) 
167   TopoDS_Edge E1, E2, E3, E4, E5, E6, E7, E8, E9;
168   TopoDS_Wire W1, W2, W3;
169   TopoDS_Face F1, F2, F3;   
170   TopoDS_Shell S;
171   
172   BRep_Builder aBuilder;
173   aBuilder.MakeShell(S);
174   
175   // Initialisation of edges
176   TopoDS_Edge E1_init = BRepBuilderAPI_MakeEdge(V1,TopoDS::Vertex(V2.Reversed()));
177   E1 = E1_init;
178   TopoDS_Edge E8_init = BRepBuilderAPI_MakeEdge(O,TopoDS::Vertex(V1.Reversed()));
179   E8 = E8_init;
180   
181   for (int i=1;i<=6;i++)
182   { 
183     // Edges
184     // for Face1
185     E2 = BRepBuilderAPI_MakeEdge(aCircle, V2, TopoDS::Vertex(V3.Reversed())); 
186     E3 = BRepBuilderAPI_MakeEdge(V3,TopoDS::Vertex(V4.Reversed()));
187     E4 = BRepBuilderAPI_MakeEdge(V4,TopoDS::Vertex(V1.Reversed()));
188       
189     // for Face2
190     if (i==6)
191     {
192       E5 = BRepBuilderAPI_MakeEdge(aCircle, V3, TopoDS::Vertex(V2_init.Reversed()));
193       E7 = BRepBuilderAPI_MakeEdge(V1_init,TopoDS::Vertex(V4.Reversed()));
194     }
195     else
196     {
197       E5 = BRepBuilderAPI_MakeEdge(aCircle, V3, TopoDS::Vertex(V2_60.Reversed()));
198       E7 = BRepBuilderAPI_MakeEdge(V1_60,TopoDS::Vertex(V4.Reversed()));
199     }    
200     E6 = BRepBuilderAPI_MakeEdge(V2_60,TopoDS::Vertex(V1_60.Reversed()));
201     
202     // for Face3
203     E9 = BRepBuilderAPI_MakeEdge(V1_60,TopoDS::Vertex(O.Reversed()));
204     
205     
206     // Wires
207     //Wire1
208     aBuilder.MakeWire(W1);
209     if (i==1)
210       aBuilder.Add(W1,E1);
211     else
212       aBuilder.Add(W1,TopoDS::Edge(E1.Reversed()));
213     aBuilder.Add(W1,E2);
214     aBuilder.Add(W1,E3);
215     aBuilder.Add(W1,E4);
216     
217     // Wire 2
218     aBuilder.MakeWire(W2);
219     aBuilder.Add(W2,TopoDS::Edge(E3.Reversed()));
220     aBuilder.Add(W2,E5);
221     if (i==6)
222       aBuilder.Add(W2,TopoDS::Edge(E1_init.Reversed()));
223     else
224       aBuilder.Add(W2,E6);
225     aBuilder.Add(W2,E7);
226     
227     // Wire3
228     aBuilder.MakeWire(W3);
229     if (i==1)
230       aBuilder.Add(W3,E8);
231     else 
232       aBuilder.Add(W3,TopoDS::Edge(E8.Reversed()));    
233     aBuilder.Add(W3,TopoDS::Edge(E4.Reversed()));
234     aBuilder.Add(W3,TopoDS::Edge(E7.Reversed()));
235     if (i==6)
236       aBuilder.Add(W3,TopoDS::Edge(E8_init.Reversed()));
237     else
238       aBuilder.Add(W3,E9);
239       
240     // Faces creation
241     F1 = BRepBuilderAPI_MakeFace(aPlane,W1);
242     F2 = BRepBuilderAPI_MakeFace(aPlane,W2);
243     F3 = BRepBuilderAPI_MakeFace(aPlane,W3);
244     
245     //Shell
246     aBuilder.Add(S, F1);
247     aBuilder.Add(S, F2);
248     aBuilder.Add(S, F3);
249           
250     // rotation
251     V1=V1_60;
252     V2=V2_60;
253     
254     xform.Perform(V1_60,Standard_True);
255     V1_60 = TopoDS::Vertex(xform.Shape());
256     xform.Perform(V2_60,Standard_True);
257     V2_60 = TopoDS::Vertex(xform.Shape());
258     xform.Perform(V3,Standard_True);
259     V3    = TopoDS::Vertex(xform.Shape());
260     xform.Perform(V4,Standard_True);
261     V4    = TopoDS::Vertex(xform.Shape());
262     
263     // "Increment" of edges
264     E1=E6;
265     E8=E9;         
266   }
267   
268   return S;
269 }
270
271 //=======================================================================
272 //function :  TrasformShape(TopoDS_Shape aShape,int theOrientation)
273 //purpose  :  Perform shape transformation accordingly with specified
274 //            orientation
275 //=======================================================================
276 TopoDS_Shape GEOMImpl_DividedDiskDriver::TransformShape(TopoDS_Shape theShape, int theOrientation) const
277 {
278   gp_Dir N, Vx;
279   gp_Pnt theOrigin = gp::Origin();
280   
281   switch(theOrientation)
282   {
283     case 1:
284     {
285       N = gp::DZ();
286       Vx = gp::DX();
287       break;
288     }
289     case 2:
290     {
291       N = gp::DX();
292       Vx = gp::DY();
293       break;
294     }
295     case 3:
296     {
297       N = gp::DY();
298       Vx = gp::DZ();
299       break;
300     }
301   }
302     
303   gp_Ax3 aWPlane = gp_Ax3(theOrigin, N, Vx);
304   
305   return WPlaneTransform(theShape, aWPlane);
306 }
307
308 //=======================================================================
309 //function :  TrasformShape(TopoDS_Shape aShape, gp_Dir V, gp_Pnt P)
310 //purpose  :  Perform shape transformation accordingly with specified
311 //            pnt and direction
312 //=======================================================================
313 TopoDS_Shape GEOMImpl_DividedDiskDriver::TransformShape(TopoDS_Shape theShape, gp_Pnt P, gp_Dir V) const
314 {
315   gp_Ax3 aWPlane( P, V );
316   return WPlaneTransform(theShape, aWPlane);
317 }
318
319 //=======================================================================
320 //function :  WPlaneTransform
321 //purpose  :  Perform shape transformation accordingly with the given 
322 //            Working Plane  
323 //=======================================================================
324 TopoDS_Shape GEOMImpl_DividedDiskDriver::WPlaneTransform(TopoDS_Shape theShape, gp_Ax3 theWPlane) const
325 {
326   gp_Trsf aTrans;
327   aTrans.SetTransformation(theWPlane);
328   aTrans.Invert();
329   BRepBuilderAPI_Transform aTransformation (theShape, aTrans, Standard_False);
330   return aTransformation.Shape();
331 }
332
333 //=======================================================================
334 //function :  GEOMImpl_DividedDiskDriver_Type_
335 //purpose  :
336 //=======================================================================
337 Standard_EXPORT Handle_Standard_Type& GEOMImpl_DividedDiskDriver_Type_()
338 {
339   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
340   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
341   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
342   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
343   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
344   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
345
346   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
347   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_DividedDiskDriver",
348                                                          sizeof(GEOMImpl_DividedDiskDriver),
349                                                          1,
350                                                          (Standard_Address)_Ancestors,
351                                                          (Standard_Address)NULL);
352   return _aType;
353 }
354
355 //=======================================================================
356 //function : DownCast
357 //purpose  :
358 //=======================================================================
359 const Handle(GEOMImpl_DividedDiskDriver) Handle(GEOMImpl_DividedDiskDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
360 {
361   Handle(GEOMImpl_DividedDiskDriver) _anOtherObject;
362
363   if (!AnObject.IsNull()) {
364      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_DividedDiskDriver))) {
365        _anOtherObject = Handle(GEOMImpl_DividedDiskDriver)((Handle(GEOMImpl_DividedDiskDriver)&)AnObject);
366      }
367   }
368
369   return _anOtherObject;
370 }