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