Salome HOME
DCQ:prepare 2.0.0
[modules/smesh.git] / src / SMESH_SWIG / SMESH_demo_hexa2.py
1 #
2 # Tetrahedrization of a geometry (box minus a inner cylinder).
3 # Hypothesis and algorithms for the mesh generation are not global:
4 # the mesh of some edges is thinner
5 #
6
7 import math
8 import salome
9 from salome import sg
10
11 import geompy
12
13 import SMESH
14 import smeshpy
15
16 # -----------------------------------------------------------------------------
17
18 geom = geompy.geom
19 myBuilder = geompy.myBuilder
20
21 ShapeTypeShell     = 3
22 ShapeTypeFace      = 4
23 ShapeTypeEdge      = 6
24
25 a = math.sqrt(2.)/4.
26 ma = - a
27 zero = 0.
28 un = 1.
29 mun= - un
30 demi = 1./2.
31
32 Orig = geom.MakePointStruct(zero,zero,zero)
33 P0 = geom.MakePointStruct(a,a,zero)
34 P1 = geom.MakePointStruct(zero,demi,zero)
35 P2 = geom.MakePointStruct(ma,a,zero)
36 P3 = geom.MakePointStruct(mun,un,zero)
37 P4 = geom.MakePointStruct(un,un,zero)
38 P5 = geom.MakePointStruct(zero,zero,un)
39
40 arc = geompy.MakeArc(P0,P1,P2)
41 e1 = geompy.MakeEdge(P2,P3)
42 e2 = geompy.MakeEdge(P3,P4)
43 e3 = geompy.MakeEdge(P4,P0)
44
45 list = []
46 list.append(arc._get_Name())
47 list.append(e1._get_Name())
48 list.append(e2._get_Name())
49 list.append(e3._get_Name())
50
51 wire = geompy.MakeWire(list)
52 face = geompy.MakeFace(wire,1)
53
54 dir = geompy.MakeVector(Orig,P5)
55 vol1 = geompy.MakePipe(dir,face)
56
57 angle = math.pi/2.
58 dir = geom.MakeAxisStruct(zero,zero,zero,zero,zero,un)
59 vol2 = geompy.MakeRotation(vol1,dir,angle)
60
61 vol3 = geompy.MakeRotation(vol2,dir,angle)
62
63 vol4 = geompy.MakeRotation(vol3,dir,angle)
64
65 list = []
66 list.append(vol1._get_Name())
67 list.append(vol2._get_Name())
68 list.append(vol3._get_Name())
69 list.append(vol4._get_Name())
70
71 volComp = geompy.MakeCompound(list)
72
73 tol3d = 1.e-3
74 vol = geom.MakeGlueFaces(volComp,tol3d)
75 idVol = geompy.addToStudy(vol,"volume")
76
77 print "Analysis of the final volume:"
78 subShellList=geompy.SubShapeAllSorted(vol,ShapeTypeShell)
79 subFaceList=geompy.SubShapeAllSorted(vol,ShapeTypeFace)
80 subEdgeList=geompy.SubShapeAllSorted(vol,ShapeTypeEdge)
81
82 print "number of Shells in the volume : ",len(subShellList)
83 print "number of Faces in the volume : ",len(subFaceList)
84 print "number of Edges in the volume : ",len(subEdgeList)
85
86 idSubEdge = []
87 for k in range(len(subEdgeList)):
88     idSubEdge.append(geompy.addToStudyInFather(vol,subEdgeList[k],"SubEdge"+str(k)))
89
90 edgeZ = []
91 edgeZ.append(subEdgeList[0])
92 edgeZ.append(subEdgeList[3])
93 edgeZ.append(subEdgeList[10])
94 edgeZ.append(subEdgeList[11])
95 edgeZ.append(subEdgeList[20])
96 edgeZ.append(subEdgeList[21])
97 edgeZ.append(subEdgeList[28])
98 edgeZ.append(subEdgeList[31])
99
100 idEdgeZ = []
101 for i in range(8):
102     idEdgeZ.append(geompy.addToStudyInFather(vol,edgeZ[i],"EdgeZ"+str(i+1)))
103
104 # ---- launch SMESH
105
106 smeshgui = salome.ImportComponentGUI("SMESH")
107 smeshgui.Init(salome.myStudyId)
108
109 gen=smeshpy.smeshpy()
110
111 # ---- create Hypothesis
112
113 print "-------------------------- create Hypothesis"
114
115 print "-------------------------- NumberOfSegments the global one"
116
117 numberOfSegments = 10
118
119 hypothesis=gen.CreateHypothesis("NumberOfSegments")
120 hypNbSeg=hypothesis._narrow(SMESH.SMESH_NumberOfSegments)
121 hypNbSeg.SetNumberOfSegments(numberOfSegments)
122 hypNbSegID = hypNbSeg.GetId()
123 print hypNbSeg.GetName()
124 print hypNbSegID
125 print hypNbSeg.GetNumberOfSegments()
126
127 idseg = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypNbSeg) )
128 smeshgui.SetName(idseg, "NumberOfSegments")
129
130 print "-------------------------- NumberOfSegments in the Z direction"
131
132 numberOfSegmentsZ = 40
133
134 hypothesis=gen.CreateHypothesis("NumberOfSegments")
135 hypNbSegZ=hypothesis._narrow(SMESH.SMESH_NumberOfSegments)
136 hypNbSegZ.SetNumberOfSegments(numberOfSegmentsZ)
137 hypNbSegZID = hypNbSegZ.GetId()
138 print hypNbSegZ.GetName()
139 print hypNbSegZID
140 print hypNbSegZ.GetNumberOfSegments()
141
142 idsegZ = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypNbSegZ) )
143 smeshgui.SetName(idsegZ, "NumberOfSegmentsZ")
144
145 # ---- create Algorithms
146
147 print "-------------------------- create Algorithms"
148
149 print "-------------------------- Regular_1D"
150
151 hypothesis=gen.CreateHypothesis("Regular_1D")
152 regular1D = hypothesis._narrow(SMESH.SMESH_Regular_1D)
153 regularID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(regular1D) )
154 smeshgui.SetName(regularID, "Wire Discretisation")
155
156 print "-------------------------- Quadrangle_2D"
157
158 hypothesis=gen.CreateHypothesis("Quadrangle_2D")
159 quad2D = hypothesis._narrow(SMESH.SMESH_Quadrangle_2D)
160 quadID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(quad2D) )
161 smeshgui.SetName(quadID, "Quadrangle_2D")
162
163 print "-------------------------- Hexa_3D"
164
165 hypothesis=gen.CreateHypothesis("Hexa_3D")
166 hexa3D = hypothesis._narrow(SMESH.SMESH_Hexa_3D)
167 hexaID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(hexa3D) )
168 smeshgui.SetName(hexaID, "Hexa_3D")
169
170 # ---- init a Mesh with the volume
171
172 mesh=gen.Init(idVol)
173 idmesh = smeshgui.AddNewMesh( salome.orb.object_to_string(mesh) )
174 smeshgui.SetName(idmesh, "meshVolume")
175 smeshgui.SetShape(idVol, idmesh)
176
177 # ---- add hypothesis to the volume
178
179 print "-------------------------- add hypothesis to the volume"
180
181 ret=mesh.AddHypothesis(vol,regular1D)
182 print ret
183 ret=mesh.AddHypothesis(vol,hypNbSeg)
184 print ret
185 ret=mesh.AddHypothesis(vol,quad2D)
186 print ret
187 ret=mesh.AddHypothesis(vol,hexa3D)
188 print ret
189
190 print "-------------------------- set algoritms"
191
192 smeshgui.SetAlgorithms( idmesh, regularID)
193 smeshgui.SetHypothesis( idmesh, idseg )
194 smeshgui.SetAlgorithms( idmesh, quadID )
195 smeshgui.SetAlgorithms( idmesh, hexaID )
196
197 for i in range(8):
198     print "-------------------------- add hypothesis to edge in the Z directions", (i+1)
199
200     subMeshEdgeZ = mesh.GetElementsOnShape(edgeZ[i])
201
202     retZ = mesh.AddHypothesis(edgeZ[i],hypNbSegZ)
203     print " add hyp Z ", retZ
204
205     idsmZ = smeshgui.AddSubMeshOnShape(
206         idmesh,idEdgeZ[i],salome.orb.object_to_string(subMeshEdgeZ),
207         ShapeTypeEdge)
208
209     smeshgui.SetName(idsmZ, "SubMeshEdgeZ_"+str(i+1))
210
211     smeshgui.SetHypothesis(idsmZ, idsegZ)
212
213 sg.updateObjBrowser(1)
214
215 print "-------------------------- compute the mesh of the volume"
216 ret=gen.Compute(mesh,idVol)
217 print ret
218 if ret != 0:
219     log=mesh.GetLog(0) # no erase trace
220     for linelog in log:
221         print linelog
222 else:
223     print "problem when Computing the mesh"
224
225 sg.updateObjBrowser(1)