Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/smesh.git] / doc / salome / gui / SMESH / input / tui_creating_meshes.doc
1 /*!
2
3 \page tui_creating_meshes_page Creating Meshes
4
5 \n First of all see \ref example_3d_mesh "Example of 3d mesh generation",
6  which is an example of good python script style for Mesh module.
7
8 <br>
9 <h2>Construction of a Mesh</h2>
10
11 \code
12 import geompy
13 import smesh
14
15 # create a box
16 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
17 idbox = geompy.addToStudy(box, "box")
18
19 # create a mesh
20 tetra = smesh.Mesh(box, "MeshBox")
21
22 algo1D = tetra.Segment()
23 algo1D.NumberOfSegments(7)
24
25 algo2D = tetra.Triangle()
26 algo2D.MaxElementArea(800.)
27
28 algo3D = tetra.Tetrahedron()
29 algo3D.MaxElementVolume(900.)
30
31 # compute the mesh
32 ret = tetra.Compute()
33 if ret == 0:
34     print "problem when computing the mesh"
35 else:
36     print "mesh computed"
37     pass
38 \endcode
39
40 <br>
41 \anchor tui_construction_submesh
42 <h2>Construction of a Submesh</h2>
43
44 \code
45 from geompy import *
46 import smesh
47
48 # create a box
49 box = MakeBoxDXDYDZ(10., 10., 10.)
50 addToStudy(box, "Box")
51
52 # select one edge of the box for definition of a local hypothesis
53 p5 = MakeVertex(5., 0., 0.)
54 EdgeX = GetEdgeNearPoint(box, p5)
55 addToStudyInFather(box, EdgeX, "Edge [0,0,0 - 10,0,0]")
56
57 # create a hexahedral mesh on the box
58 quadra = smesh.Mesh(box, "Box : quadrangle 2D mesh")
59
60 # create a regular 1D algorithm for the faces
61 algo1D = quadra.Segment()
62
63 # define "NumberOfSegments" hypothesis to cut
64 # all the edges in a fixed number of segments
65 algo1D.NumberOfSegments(4)
66
67 # create a quadrangle 2D algorithm for the faces
68 quadra.Quadrangle()
69
70 # construct a submesh on the edge with a local hypothesis
71 algo_local = quadra.Segment(EdgeX)
72
73 # define "Arithmetic1D" hypothesis to cut the edge in several segments with increasing arithmetic length
74 algo_local.Arithmetic1D(1, 4)
75
76 # define "Propagation" hypothesis that propagates all other hypotheses
77 # on all edges of the opposite side in case of quadrangular faces
78 algo_local.Propagation()
79
80 # compute the mesh
81 quadra.Compute()
82
83 \endcode
84
85 <br>
86 <h2>Change priority of submeshes in Mesh</h2>
87
88 \code
89 import salome
90 import geompy
91 import smesh
92 import SMESH
93
94 Box_1 = geompy.MakeBoxDXDYDZ(200, 200, 200)
95 [Face_1,Face_2,Face_3,Face_4,Face_5,Face_6] = geompy.SubShapeAllSorted(Box_1, geompy.ShapeType["FACE"])
96
97 # create Mesh object on Box shape
98 Mesh_1 = smesh.Mesh(Box_1)
99
100 # assign mesh algorithms
101 Regular_1D = Mesh_1.Segment()
102 Nb_Segments_1 = Regular_1D.NumberOfSegments(20)
103 Nb_Segments_1.SetDistrType( 0 )
104 MEFISTO_2D = Mesh_1.Triangle()
105 Max_Element_Area_1 = MEFISTO_2D.MaxElementArea(1200)
106 Tetrahedron = Mesh_1.Tetrahedron()
107 Max_Element_Volume_1 = Tetrahedron.MaxElementVolume(40000)
108
109 # create submesh and assign algorithms on Face_1
110 Regular_1D_1 = Mesh_1.Segment(geom=Face_1)
111 Nb_Segments_2 = Regular_1D_1.NumberOfSegments(4)
112 Nb_Segments_2.SetDistrType( 0 )
113 MEFISTO_2D_1 = Mesh_1.Triangle(algo=smesh.MEFISTO,geom=Face_1)
114 Length_From_Edges_2D = MEFISTO_2D_1.LengthFromEdges()
115 SubMesh_1 = MEFISTO_2D_1.GetSubMesh()
116
117 # create submesh and assign algorithms on Face_2
118 Regular_1D_2 = Mesh_1.Segment(geom=Face_2)
119 Nb_Segments_3 = Regular_1D_2.NumberOfSegments(8)
120 Nb_Segments_3.SetDistrType( 0 )
121 MEFISTO_2D_2 = Mesh_1.Triangle(algo=smesh.MEFISTO,geom=Face_2)
122 Length_From_Edges_2D_1 = MEFISTO_2D_2.LengthFromEdges()
123 SubMesh_2 = MEFISTO_2D_2.GetSubMesh()
124
125 # create submesh and assign algorithms on Face_3
126 Regular_1D_3 = Mesh_1.Segment(geom=Face_3)
127 Nb_Segments_4 = Regular_1D_3.NumberOfSegments(12)
128 Nb_Segments_4.SetDistrType( 0 )
129 MEFISTO_2D_3 = Mesh_1.Triangle(algo=smesh.MEFISTO,geom=Face_3)
130 Length_From_Edges_2D_2 = MEFISTO_2D_3.LengthFromEdges()
131 SubMesh_3 = MEFISTO_2D_3.GetSubMesh()
132
133 # check exisiting submesh priority order
134 [ [ SubMesh_1, SubMesh_3, SubMesh_2 ] ] = Mesh_1.GetMeshOrder()
135 # set new submesh order
136 isDone = Mesh_1.SetMeshOrder( [ [ SubMesh_1, SubMesh_2, SubMesh_3 ] ])
137 # compute mesh
138 isDone = Mesh_1.Compute()
139
140 # clear mesh result and compute with other submesh order
141 Mesh_1.Clear()
142 isDone = Mesh_1.SetMeshOrder( [ [ SubMesh_2, SubMesh_1, SubMesh_3 ] ])
143 isDone = Mesh_1.Compute()
144
145 \endcode
146
147 <br>
148 \anchor tui_editing_mesh
149 <h2>Editing of a mesh</h2>
150
151 \code
152 import geompy
153 import smesh
154
155 def PrintMeshInfo(theMesh):
156     aMesh = theMesh.GetMesh()
157     print "Information about mesh:"
158     print "Number of nodes       : ", aMesh.NbNodes()
159     print "Number of edges       : ", aMesh.NbEdges()
160     print "Number of faces       : ", aMesh.NbFaces()
161     print "Number of volumes     : ", aMesh.NbVolumes()
162     pass
163
164 # create a box
165 box = geompy.MakeBox(0., 0., 0., 20., 20., 20.)
166 geompy.addToStudy(box, "box")
167
168 # select one edge of the box for definition of a local hypothesis
169 subShapeList = geompy.SubShapeAll(box, geompy.ShapeType["EDGE"])
170 edge = subShapeList[0]
171 name = geompy.SubShapeName(edge, box)
172 geompy.addToStudyInFather(box, edge, name)
173
174 # create a mesh
175 tria = smesh.Mesh(box, "Mesh 2D")
176 algo1D = tria.Segment()
177 hyp1 = algo1D.NumberOfSegments(3)
178 algo2D = tria.Triangle()
179 hyp2 = algo2D.MaxElementArea(10.)
180
181 # create a sub-mesh
182 algo_local = tria.Segment(edge)
183 hyp3 = algo_local.Arithmetic1D(1, 6)
184 hyp4 = algo_local.Propagation()
185
186 # compute the mesh
187 tria.Compute()
188 PrintMeshInfo(tria)
189
190 # remove a local hypothesis
191 mesh = tria.GetMesh()
192 mesh.RemoveHypothesis(edge, hyp4)
193
194 # compute the mesh
195 tria.Compute()
196 PrintMeshInfo(tria)
197
198 # change the value of the 2D hypothesis
199 hyp2.SetMaxElementArea(2.)
200
201 # compute the mesh
202 tria.Compute()
203 PrintMeshInfo(tria)
204 \endcode
205
206 <br>
207 \anchor tui_export_mesh
208 <h2>Export of a Mesh</h2>
209
210 \code
211 import geompy
212 import smesh
213
214 # create a box
215 box = geompy.MakeBox(0., 0., 0., 100., 200., 300.)
216 idbox = geompy.addToStudy(box, "box")
217
218 # create a mesh
219 tetra = smesh.Mesh(box, "MeshBox")
220
221 algo1D = tetra.Segment()
222 algo1D.NumberOfSegments(7)
223
224 algo2D = tetra.Triangle()
225 algo2D.MaxElementArea(800.)
226
227 algo3D = tetra.Tetrahedron()
228 algo3D.MaxElementVolume(900.)
229
230 # compute the mesh
231 tetra.Compute()
232
233 # export the mesh in a MED file
234 tetra.ExportMED("/tmp/meshMED.med", 0)
235
236 # export a group in a MED file
237 face = geompy.SubShapeAll( box, geompy.ShapeType["FACE"])[0] # a box side
238 group = tetra.GroupOnGeom( face, "face group" ) # group of 2D elements on the <face>
239 tetra.ExportMED("/tmp/groupMED.med", meshPart=group)
240 \endcode
241
242 <br>
243 <h2>How to mesh a cylinder with hexahedrons?</h2>
244 Here you can see an example of python script, creating a hexahedral
245 mesh on a cylinder. And a picture below the source code of the script,
246 demonstrating the resulting mesh.
247 \include ex24_cylinder.py
248
249 \image html mesh_cylinder_hexa.png
250
251 <br>
252 \anchor tui_building_compound
253 <h2>Building a compound of meshes</h2>
254 \dontinclude SMESH_BuildCompound.py
255 \skipline import geompy
256 \until #end
257
258 <br>
259 \anchor tui_copy_mesh
260 <h2>Mesh Copying</h2>
261 \code
262 from smesh import *
263 SetCurrentStudy(salome.myStudy)
264
265 # make geometry of a box
266 box = geompy.MakeBoxDXDYDZ(100,100,100)
267 face = geompy.SubShapeAllSorted(box, geompy.ShapeType["FACE"])[0]
268
269 # generate 3D mesh
270 mesh = Mesh(box)
271 localAlgo = mesh.Triangle(face)
272 mesh.AutomaticHexahedralization()
273
274 # objects to copy
275 fGroup = mesh.GroupOnGeom( face, "2D on face")
276 nGroup = mesh.GroupOnGeom( face, "nodes on face", NODE)
277 subMesh = localAlgo.GetSubMesh()
278
279 # make a new mesh by copying different parts of the mesh
280
281 # 1. copy the whole mesh
282 newMesh = CopyMesh( mesh, "whole mesh copy")
283
284 # 2. copy a group of 2D elements along with groups
285 newMesh = CopyMesh( fGroup,  "face group copy with groups",toCopyGroups=True)
286
287 # 3. copy a group of nodes with preseving their ids
288 newMesh = CopyMesh( nGroup, "node group copy", toKeepIDs=True)
289
290 # 4. copy some faces
291 faceIds = fGroup.GetIDs()[-10:]
292 newMesh = CopyMesh( mesh.GetIDSource( faceIds, FACE ), "some faces copy")
293
294 # 5. copy some nodes
295 nodeIds = nGroup.GetIDs()[-10:]
296 newMesh = CopyMesh( mesh.GetIDSource( nodeIds, NODE), "some nodes copy")
297
298 # 6. copy a sub-mesh
299 newMesh = CopyMesh( subMesh, "submesh copy" )
300 \endcode
301
302 */