Salome HOME
adding python scripts to illustrate the way to mesh a shape giving different hypothes...
[modules/smesh.git] / src / SMESH_SWIG / SMESH_box_hexa.py
1 #
2 # Hexahedrization of a simple box. Hypothesis and algorithms for
3 # the mesh generation are not global: the mesh of some edges is thinner
4 #
5
6 import salome
7 from salome import sg
8
9 import geompy
10
11 import SMESH
12 import smeshpy
13
14 # -----------------------------------------------------------------------------
15
16 geom = geompy.geom
17 myBuilder = geompy.myBuilder
18
19 ShapeTypeShell     = 3
20 ShapeTypeFace      = 4
21 ShapeTypeEdge      = 6
22
23 # ---- define a boxe
24
25 box = geompy.MakeBox(0., 0., 0., 1., 1., 1.)
26
27 idbox = geompy.addToStudy(box,"box")
28
29 print "Analysis of the geometry box :"
30 subShellList=geompy.SubShapeAllSorted(box,ShapeTypeShell)
31 subFaceList=geompy.SubShapeAllSorted(box,ShapeTypeFace)
32 subEdgeList=geompy.SubShapeAllSorted(box,ShapeTypeEdge)
33
34 print "number of Shells in box : ",len(subShellList)
35 print "number of Faces in box : ",len(subFaceList)
36 print "number of Edges in box : ",len(subEdgeList)
37
38 idSubEdge = []
39 for k in range(len(subEdgeList)):
40     idSubEdge.append(geompy.addToStudyInFather(box,subEdgeList[k],"SubEdge"+str(k)))
41
42 edgeX = []
43 edgeX.append(subEdgeList[4])
44 edgeX.append(subEdgeList[5])
45 edgeX.append(subEdgeList[6])
46 edgeX.append(subEdgeList[7])
47
48 edgeY = []
49 edgeY.append(subEdgeList[1])
50 edgeY.append(subEdgeList[2])
51 edgeY.append(subEdgeList[9])
52 edgeY.append(subEdgeList[10])
53
54 edgeZ = []
55 edgeZ.append(subEdgeList[0])
56 edgeZ.append(subEdgeList[3])
57 edgeZ.append(subEdgeList[8])
58 edgeZ.append(subEdgeList[11])
59
60 idEdgeX = []
61 idEdgeY = []
62 idEdgeZ = []
63 for i in range(4):
64     idEdgeX.append(geompy.addToStudyInFather(box,edgeX[i],"EdgeX"+str(i+1)))
65     idEdgeY.append(geompy.addToStudyInFather(box,edgeY[i],"EdgeY"+str(i+1)))
66     idEdgeZ.append(geompy.addToStudyInFather(box,edgeZ[i],"EdgeZ"+str(i+1)))
67
68 # ---- launch SMESH
69
70 smeshgui = salome.ImportComponentGUI("SMESH")
71 smeshgui.Init(salome.myStudyId)
72
73 gen=smeshpy.smeshpy()
74
75 # ---- create Hypothesis
76
77 print "-------------------------- create Hypothesis"
78
79 print "-------------------------- NumberOfSegments in X, Y, Z direction"
80
81 numberOfSegmentsX = 10
82
83 hyp1=gen.CreateHypothesis("NumberOfSegments")
84 hypNbSegX=hyp1._narrow(SMESH.SMESH_NumberOfSegments)
85 hypNbSegX.SetNumberOfSegments(numberOfSegmentsX)
86 hypNbSegXID = hypNbSegX.GetId()
87 print hypNbSegX.GetName()
88 print hypNbSegXID
89 print hypNbSegX.GetNumberOfSegments()
90
91 idsegX = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypNbSegX) )
92 smeshgui.SetName(idsegX, "NumberOfSegmentsX")
93
94 print ""
95
96 numberOfSegmentsY = 20
97
98 hyp1=gen.CreateHypothesis("NumberOfSegments")
99 hypNbSegY=hyp1._narrow(SMESH.SMESH_NumberOfSegments)
100 hypNbSegY.SetNumberOfSegments(numberOfSegmentsY)
101 hypNbSegYID = hypNbSegY.GetId()
102 print hypNbSegY.GetName()
103 print hypNbSegYID
104 print hypNbSegY.GetNumberOfSegments()
105
106 idsegY = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypNbSegY) )
107 smeshgui.SetName(idsegY, "NumberOfSegmentsY")
108
109 print ""
110
111 numberOfSegmentsZ = 40
112
113 hyp1=gen.CreateHypothesis("NumberOfSegments")
114 hypNbSegZ=hyp1._narrow(SMESH.SMESH_NumberOfSegments)
115 hypNbSegZ.SetNumberOfSegments(numberOfSegmentsZ)
116 hypNbSegZID = hypNbSegZ.GetId()
117 print hypNbSegZ.GetName()
118 print hypNbSegZID
119 print hypNbSegZ.GetNumberOfSegments()
120
121 idsegZ = smeshgui.AddNewHypothesis( salome.orb.object_to_string(hypNbSegZ) )
122 smeshgui.SetName(idsegZ, "NumberOfSegmentsZ")
123
124 # ---- create Algorithms
125
126 print "-------------------------- create Algorithms"
127
128 print "-------------------------- Regular_1D"
129
130 hypothesis=gen.CreateHypothesis("Regular_1D")
131 regular1D = hypothesis._narrow(SMESH.SMESH_Regular_1D)
132 regularID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(regular1D) )
133 smeshgui.SetName(regularID, "Wire Discretisation")
134
135 print "-------------------------- Quadrangle_2D"
136
137 hypothesis=gen.CreateHypothesis("Quadrangle_2D")
138 quad2D = hypothesis._narrow(SMESH.SMESH_Quadrangle_2D)
139 quadID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(quad2D) )
140 smeshgui.SetName(quadID, "Quadrangle_2D")
141
142 print "-------------------------- Hexa_3D"
143
144 hypothesis=gen.CreateHypothesis("Hexa_3D")
145 hexa3D = hypothesis._narrow(SMESH.SMESH_Hexa_3D)
146 hexaID = smeshgui.AddNewAlgorithms( salome.orb.object_to_string(hexa3D) )
147 smeshgui.SetName(hexaID, "Hexa_3D")
148
149 # ---- init a Mesh with the boxe
150
151 mesh=gen.Init(idbox)
152 idmesh = smeshgui.AddNewMesh( salome.orb.object_to_string(mesh) )
153 smeshgui.SetName(idmesh, "MeshBox")
154 smeshgui.SetShape(idbox, idmesh)
155
156 # ---- add hypothesis to the boxe
157
158 print "-------------------------- add hypothesis to the boxe"
159 print "                           the number of segments is globally set to"
160 print "                           NumberOfSegmentsX = ", numberOfSegmentsX
161
162 ret=mesh.AddHypothesis(box,regular1D)
163 print ret
164 ret=mesh.AddHypothesis(box,hypNbSegX)
165 print ret
166 ret=mesh.AddHypothesis(box,quad2D)
167 print ret
168 ret=mesh.AddHypothesis(box,hexa3D)
169 print ret
170
171 print "-------------------------- set algoritms"
172
173 smeshgui.SetAlgorithms( idmesh, regularID)
174 smeshgui.SetHypothesis( idmesh, idsegX )
175 smeshgui.SetAlgorithms( idmesh, quadID )
176 smeshgui.SetAlgorithms( idmesh, hexaID )
177
178 for i in range(4):
179     print "-------------------------- add hypothesis to edge in the Y and Z directions", (i+1)
180
181     subMeshEdgeY = mesh.GetElementsOnShape(edgeY[i])
182     subMeshEdgeZ = mesh.GetElementsOnShape(edgeZ[i])
183
184     retY = mesh.AddHypothesis(edgeY[i],hypNbSegY)
185     retZ = mesh.AddHypothesis(edgeZ[i],hypNbSegZ)
186     print " add hyp Y ", retY, " Z ", retZ
187
188     idsmY = smeshgui.AddSubMeshOnShape(
189         idmesh,idEdgeY[i],salome.orb.object_to_string(subMeshEdgeY),
190         ShapeTypeEdge)
191     idsmZ = smeshgui.AddSubMeshOnShape(
192         idmesh,idEdgeZ[i],salome.orb.object_to_string(subMeshEdgeZ),
193         ShapeTypeEdge)
194
195     smeshgui.SetName(idsmY, "SubMeshEdgeY_"+str(i+1))
196     smeshgui.SetName(idsmZ, "SubMeshEdgeZ_"+str(i+1))
197
198     smeshgui.SetHypothesis(idsmY, idsegY)
199     smeshgui.SetHypothesis(idsmZ, idsegZ)
200
201 sg.updateObjBrowser(1)
202
203 print "-------------------------- compute the mesh of the boxe"
204 ret=gen.Compute(mesh,idbox)
205 print ret
206 if ret != 0:
207     log=mesh.GetLog(0) # no erase trace
208     for linelog in log:
209         print linelog
210 else:
211     print "problem when Computing the mesh"
212
213 sg.updateObjBrowser(1)
214
215