]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_DividedDiskDriver.cxx
Salome HOME
EDF 2281: Added the possibility to choose the orientation of the divided disk
[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   if (aType == DIVIDEDDISK_R_RATIO) {
90     
91     // Getting data
92     double R           = aData.GetR();
93     double Ratio       = aData.GetRatio();
94     int theOrientation = aData.GetOrientation();
95     
96     // Geometry
97     gp_Dir ZDir(0,0,1);
98     gp_Dir XDir(1,0,0);
99     gp_Pnt Orig(0,0,0);
100     
101     // Circle
102     gp_Ax1 Ax1(Orig,ZDir);
103     gp_Ax2 Ax(Orig,ZDir,XDir);
104     gp_Circ aCircle(Ax, R);
105     
106     // Points
107     gp_Pnt P1(0.01*Ratio*R,0,0);
108     gp_Pnt P2(R,0,0);
109     gp_Pnt P3    = P2.Rotated(Ax1,M_PI/6.0);
110     gp_Pnt P4(P1.X(), 
111               P1.X()/sqrt(3.0),0);
112     
113     //surfaces
114     gp_Ax2 anAx (gp::XOY());
115     Handle(Geom_Plane) aPlane = new Geom_Plane (anAx);
116     
117     // Topology
118     
119     // Vertices
120     TopoDS_Vertex O  = BRepBuilderAPI_MakeVertex(Orig);
121     TopoDS_Vertex V1_init = BRepBuilderAPI_MakeVertex(P1);
122     TopoDS_Vertex V2_init = BRepBuilderAPI_MakeVertex(P2);
123     TopoDS_Vertex V3 = BRepBuilderAPI_MakeVertex(P3);
124     TopoDS_Vertex V4 = BRepBuilderAPI_MakeVertex(P4);
125     
126     TopoDS_Vertex V1 = V1_init;
127     TopoDS_Vertex V2 = V2_init;
128     
129     //Rotation
130     gp_Trsf myTrsf;
131     myTrsf.SetRotation(Ax1, M_PI/3.0);
132     
133     BRepBuilderAPI_Transform xform(myTrsf);
134     xform.Perform(V1,Standard_True);
135     TopoDS_Vertex V1_60 = TopoDS::Vertex(xform.Shape()); 
136     xform.Perform(V2,Standard_True);
137     TopoDS_Vertex V2_60 = TopoDS::Vertex(xform.Shape());
138     
139     // Declaration of shapes (used in the loop) 
140     TopoDS_Edge E1, E2, E3, E4, E5, E6, E7, E8, E9;
141     TopoDS_Wire W1, W2, W3;
142     TopoDS_Face F1, F2, F3;   
143     TopoDS_Shell S;
144     
145     BRep_Builder aBuilder;
146     aBuilder.MakeShell(S);
147     
148     // Initialisation of edges
149     TopoDS_Edge E1_init = BRepBuilderAPI_MakeEdge(V1,TopoDS::Vertex(V2.Reversed()));
150     E1 = E1_init;
151     TopoDS_Edge E8_init = BRepBuilderAPI_MakeEdge(O,TopoDS::Vertex(V1.Reversed()));
152     E8 = E8_init;
153     
154     for (int i=1;i<=6;i++)
155     { 
156       // Edges
157       // for Face1
158       E2 = BRepBuilderAPI_MakeEdge(aCircle, V2, TopoDS::Vertex(V3.Reversed())); 
159       E3 = BRepBuilderAPI_MakeEdge(V3,TopoDS::Vertex(V4.Reversed()));
160       E4 = BRepBuilderAPI_MakeEdge(V4,TopoDS::Vertex(V1.Reversed()));
161        
162       // for Face2
163       if (i==6)
164       {
165         E5 = BRepBuilderAPI_MakeEdge(aCircle, V3, TopoDS::Vertex(V2_init.Reversed()));
166         E7 = BRepBuilderAPI_MakeEdge(V1_init,TopoDS::Vertex(V4.Reversed()));
167       }
168       else
169       {
170         E5 = BRepBuilderAPI_MakeEdge(aCircle, V3, TopoDS::Vertex(V2_60.Reversed()));
171         E7 = BRepBuilderAPI_MakeEdge(V1_60,TopoDS::Vertex(V4.Reversed()));
172       }    
173       E6 = BRepBuilderAPI_MakeEdge(V2_60,TopoDS::Vertex(V1_60.Reversed()));
174       
175       // for Face3
176       E9 = BRepBuilderAPI_MakeEdge(V1_60,TopoDS::Vertex(O.Reversed()));
177       
178       
179       // Wires
180       //Wire1
181       aBuilder.MakeWire(W1);
182       if (i==1)
183         aBuilder.Add(W1,E1);
184       else
185         aBuilder.Add(W1,TopoDS::Edge(E1.Reversed()));
186       aBuilder.Add(W1,E2);
187       aBuilder.Add(W1,E3);
188       aBuilder.Add(W1,E4);
189       
190       // Wire 2
191       aBuilder.MakeWire(W2);
192       aBuilder.Add(W2,TopoDS::Edge(E3.Reversed()));
193       aBuilder.Add(W2,E5);
194       if (i==6)
195         aBuilder.Add(W2,TopoDS::Edge(E1_init.Reversed()));
196       else
197         aBuilder.Add(W2,E6);
198       aBuilder.Add(W2,E7);
199       
200       // Wire3
201       aBuilder.MakeWire(W3);
202       if (i==1)
203         aBuilder.Add(W3,E8);
204       else 
205         aBuilder.Add(W3,TopoDS::Edge(E8.Reversed()));    
206       aBuilder.Add(W3,TopoDS::Edge(E4.Reversed()));
207       aBuilder.Add(W3,TopoDS::Edge(E7.Reversed()));
208       if (i==6)
209         aBuilder.Add(W3,TopoDS::Edge(E8_init.Reversed()));
210       else
211         aBuilder.Add(W3,E9);
212         
213       // Faces creation
214       F1 = BRepBuilderAPI_MakeFace(aPlane,W1);
215       F2 = BRepBuilderAPI_MakeFace(aPlane,W2);
216       F3 = BRepBuilderAPI_MakeFace(aPlane,W3);
217       
218       //Shell
219       aBuilder.Add(S, F1);
220       aBuilder.Add(S, F2);
221       aBuilder.Add(S, F3);
222            
223       // rotation
224       V1=V1_60;
225       V2=V2_60;
226       
227       xform.Perform(V1_60,Standard_True);
228       V1_60 = TopoDS::Vertex(xform.Shape());
229       xform.Perform(V2_60,Standard_True);
230       V2_60 = TopoDS::Vertex(xform.Shape());
231       xform.Perform(V3,Standard_True);
232       V3    = TopoDS::Vertex(xform.Shape());
233       xform.Perform(V4,Standard_True);
234       V4    = TopoDS::Vertex(xform.Shape());
235       
236       // "Increment" of edges
237       E1=E6;
238       E8=E9;         
239     }
240     
241     aShape = TransformShape(S, theOrientation);   
242   }
243   else {
244     // other construction modes here
245   }
246
247   if (aShape.IsNull()) return 0;
248
249   aFunction->SetValue(aShape);
250
251   log.SetTouched(Label());
252
253   return 1;
254 }
255
256 //=======================================================================
257 //function :  TrasformShape(int theOrientation)
258 //purpose  :  Perform shape transformation accordingly with specified
259 //            orientation
260 //=======================================================================
261 TopoDS_Shape GEOMImpl_DividedDiskDriver::TransformShape(TopoDS_Shape aShape, int theOrientation) const
262 {
263   gp_Dir N, Vx;
264   gp_Pnt theOrigin = gp::Origin();
265   
266   switch(theOrientation)
267   {
268     case 1:
269     {
270       N = gp::DZ();
271       Vx = gp::DX();
272       break;
273     }
274     case 2:
275     {
276       N = gp::DX();
277       Vx = gp::DY();
278       break;
279     }
280     case 3:
281     {
282       N = gp::DY();
283       Vx = gp::DZ();
284       break;
285     }
286   }
287     
288   gp_Ax3 aWPlane = gp_Ax3(theOrigin, N, Vx);
289   
290   gp_Trsf aTrans;
291   aTrans.SetTransformation(aWPlane);
292   aTrans.Invert();
293   BRepBuilderAPI_Transform aTransformation (aShape, aTrans, Standard_False);
294   return aTransformation.Shape();
295 }
296
297 //=======================================================================
298 //function :  GEOMImpl_DividedDiskDriver_Type_
299 //purpose  :
300 //=======================================================================
301 Standard_EXPORT Handle_Standard_Type& GEOMImpl_DividedDiskDriver_Type_()
302 {
303   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
304   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
305   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
306   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
307   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
308   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
309
310   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
311   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_DividedDiskDriver",
312                                                          sizeof(GEOMImpl_DividedDiskDriver),
313                                                          1,
314                                                          (Standard_Address)_Ancestors,
315                                                          (Standard_Address)NULL);
316   return _aType;
317 }
318
319 //=======================================================================
320 //function : DownCast
321 //purpose  :
322 //=======================================================================
323 const Handle(GEOMImpl_DividedDiskDriver) Handle(GEOMImpl_DividedDiskDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
324 {
325   Handle(GEOMImpl_DividedDiskDriver) _anOtherObject;
326
327   if (!AnObject.IsNull()) {
328      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_DividedDiskDriver))) {
329        _anOtherObject = Handle(GEOMImpl_DividedDiskDriver)((Handle(GEOMImpl_DividedDiskDriver)&)AnObject);
330      }
331   }
332
333   return _anOtherObject;
334 }