Version: 8.3.0
 All Classes Namespaces Files Functions Variables Groups Pages
Python interface

Python API for SALOME Mesh module defines several classes that can be used for easy mesh creation and edition.

Documentation for SALOME Mesh module Python API is available in two forms:


With SALOME 7.2, the Python interface for Mesh has been slightly modified to offer new functionality.
You may have to modify your scripts generated with SALOME 6 or older versions.
Please see Modifing Mesh Python scripts from SALOME 6 and before.

Class smeshBuilder provides an interface to create and handle meshes. It can be used to create an empty mesh or to import mesh from the data file.

As soon as a mesh is created, it is possible to manage it via its own methods, described in class Mesh documentation.

Class SMeshStudyTools provides several methods to manipulate mesh objects in Salome study.

A usual workflow to generate a mesh on geometry is following:

  1. Create an instance of smeshBuilder:
          from salome.smesh import smeshBuilder
          smesh = smeshBuilder.New( salome.myStudy )
        
  2. Create a mesh object:
          mesh = smesh.Mesh( geometry ) 
        
  3. Create and assign algorithms by calling corresponding methods of the mesh. If a sub-shape is provided as an argument, a sub-mesh is implicitly created on this sub-shape:
          regular1D = mesh.Segment()
          mefisto   = mesh.Triangle( smeshBuilder.MEFISTO )
          # use other triangle algorithm on a face -- a sub-mesh appears in the mesh
          netgen    = mesh.Triangle( smeshBuilder.NETGEN_1D2D, face )
        
  4. Create and assign hypotheses by calling corresponding methods of algorithms:
          segLen10 = regular1D.LocalLength( 10. )
          maxArea  = mefisto.MaxElementArea( 100. )
          netgen.SetMaxSize( 20. )
          netgen.SetFineness( smeshBuilder.VeryCoarse )
        
  5. Compute the mesh (generate mesh nodes and elements):
          mesh.Compute()
        

An easiest way to start with Python scripting is to do something in GUI and then to get a corresponding Python script via File > Dump Study menu item. Don't forget that you can get all methods of any object in hand (e.g. a mesh group or a hypothesis) by calling dir() Python built-in function.

All methods of the Mesh Group can be found in Create a Standalone Group sample script.

An example below demonstrates usage of the Python API for 3d mesh generation and for retrieving information on mesh nodes and elements.

Example of 3d mesh generation:

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 

Download this script

Examples of Python scripts for Mesh operations are available by the following links: