Salome HOME
bos #17015 [CEA 17008] Body fitting with Viscous Layers for CFD meshing
[modules/smesh.git] / doc / examples / mesh_3d.py
1 # 3d mesh generation and mesh exploration
2
3 import salome
4 salome.salome_init_without_session()
5
6 import SMESH
7 from salome.geom import geomBuilder
8 from salome.smesh import smeshBuilder
9
10 geom_builder = geomBuilder.New()
11 smesh_builder = smeshBuilder.New()
12
13 ###
14 # Geometry: an assembly of a box, a cylinder and a truncated cone
15 # to be meshed with tetrahedra
16 ###
17
18 # Define values
19 name = "ex21_lamp" 
20 cote = 60 
21 section = 20 
22 size = 200 
23 radius_1 = 80 
24 radius_2 = 40 
25 height = 100 
26
27 # Build a box
28 box = geom_builder.MakeBox(-cote, -cote, -cote, +cote, +cote, +cote) 
29
30 # Build a cylinder
31 pt1 = geom_builder.MakeVertex(0, 0, cote/3) 
32 di1 = geom_builder.MakeVectorDXDYDZ(0, 0, 1) 
33 cyl = geom_builder.MakeCylinder(pt1, di1, section, size) 
34
35 # Build a truncated cone
36 pt2 = geom_builder.MakeVertex(0, 0, size) 
37 cone = geom_builder.MakeCone(pt2, di1, radius_1, radius_2, height) 
38
39 # Fuse
40 box_cyl = geom_builder.MakeFuse(box, cyl) 
41 piece = geom_builder.MakeFuse(box_cyl, cone) 
42
43 # Add to the study
44 geom_builder.addToStudy(piece, name) 
45
46 # Create a group of faces
47 faces_group = geom_builder.CreateGroup(piece, geom_builder.ShapeType["FACE"]) 
48 group_name = name + "_grp" 
49 geom_builder.addToStudy(faces_group, group_name) 
50 faces_group.SetName(group_name) 
51
52 # Add faces to the group
53 faces = geom_builder.SubShapeAllIDs(piece, geom_builder.ShapeType["FACE"]) 
54 geom_builder.UnionIDs(faces_group, faces) 
55
56 ###
57 # Create a mesh
58 ###
59
60 # Define a mesh on a geometry
61 tetra = smesh_builder.Mesh(piece, name) 
62
63 # Define 1D algorithm and hypothesis
64 algo1d = tetra.Segment() 
65 algo1d.LocalLength(10) 
66
67 # Define 2D algorithm and hypothesis
68 algo2d = tetra.Triangle() 
69 algo2d.LengthFromEdges() 
70
71 # Define 3D algorithm and hypothesis
72 algo3d = tetra.Tetrahedron()
73 algo3d.MaxElementVolume(100) 
74
75 # Compute the mesh
76 if not tetra.Compute(): raise Exception("Error when computing Mesh")
77
78 # Create a mesh group of all triangles generated on geom faces present in faces_group
79 group = tetra.Group(faces_group)
80
81 ###
82 # Explore the mesh
83 ###
84
85 # Retrieve coordinates of nodes
86 coordStr = ""
87 for node in tetra.GetNodesId():
88     x,y,z = tetra.GetNodeXYZ( node )
89     coordStr += "%s (%s, %s, %s) " % ( node, x,y,z )
90     pass
91
92 # Retrieve nodal connectivity of triangles
93 triaStr = ""
94 for tria in tetra.GetElementsByType( SMESH.FACE ):
95     nodes = tetra.GetElemNodes( tria )
96     triaStr += "%s (%s, %s, %s) " % ( tria, nodes[0], nodes[1], nodes[2] )
97
98 # Retrieve group contents
99 groupStr = ""
100 for group in tetra.GetGroups():
101     ids   = group.GetIDs()
102     name  = group.GetName()
103     eType = group.GetType()
104     groupStr += "'%s' %s: %s \n" % ( name, eType, ids )