Python interface

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

Documentation of 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.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 smeshBuilder.Mesh documentation.

Class smeshstudytools.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.smeshBuilder:
    from salome.smesh import smeshBuilder
    smesh = smeshBuilder.New( salome.myStudy )
  2. Create a smeshBuilder.Mesh object:

    mesh = smesh.Mesh( geometry )
  3. Create and assign Basic meshing algorithms by calling corresponding methods of the mesh. If a sub-shape is provided as an argument, a Constructing sub-meshes is implicitly created on this sub-shape:
    regular1D = smeshBuilder.Mesh.Segment()
    mefisto   = smeshBuilder.Mesh.Triangle( smeshBuilder.MEFISTO )
    # use other triangle algorithm on a face -- a sub-mesh appears in the mesh
    netgen    = smeshBuilder.Mesh.Triangle( smeshBuilder.NETGEN_1D2D, face )
  4. Create and assign About Hypotheses by calling corresponding methods of algorithms:
    segLen10 = StdMeshersBuilder.StdMeshersBuilder_Segment.LocalLength( 10. )
    maxArea  = StdMeshersBuilder.StdMeshersBuilder_Segment.LocalLength( 100. )
    netgen.SetMaxSize( 20. )
    netgen.SetFineness( smeshBuilder.VeryCoarse )
  5. Computing the mesh 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:

3dmesh.py

# 3d mesh generation and mesh exploration

import salome
salome.salome_init()
from salome.geom import geomBuilder
geompy = geomBuilder.New(salome.myStudy)

import SMESH
from salome.smesh import smeshBuilder
smesh =  smeshBuilder.New(salome.myStudy)

###
# Geometry: an assembly of a box, a cylinder and a truncated cone
# to be meshed with tetrahedra
###

# Define values
name = "ex21_lamp" 
cote = 60 
section = 20 
size = 200 
radius_1 = 80 
radius_2 = 40 
height = 100 

# Build a box
box = geompy.MakeBox(-cote, -cote, -cote, +cote, +cote, +cote) 

# Build a cylinder
pt1 = geompy.MakeVertex(0, 0, cote/3) 
di1 = geompy.MakeVectorDXDYDZ(0, 0, 1) 
cyl = geompy.MakeCylinder(pt1, di1, section, size) 

# Build a truncated cone
pt2 = geompy.MakeVertex(0, 0, size) 
cone = geompy.MakeCone(pt2, di1, radius_1, radius_2, height) 

# Fuse
box_cyl = geompy.MakeFuse(box, cyl) 
piece = geompy.MakeFuse(box_cyl, cone) 

# Add to the study
geompy.addToStudy(piece, name) 

# Create a group of faces
faces_group = geompy.CreateGroup(piece, geompy.ShapeType["FACE"]) 
group_name = name + "_grp" 
geompy.addToStudy(faces_group, group_name) 
faces_group.SetName(group_name) 

# Add faces to the group
faces = geompy.SubShapeAllIDs(piece, geompy.ShapeType["FACE"]) 
geompy.UnionIDs(faces_group, faces) 

###
# Create a mesh
###

# Define a mesh on a geometry
tetra = smesh.Mesh(piece, name) 

# Define 1D algorithm and hypothesis
algo1d = tetra.Segment() 
algo1d.LocalLength(10) 

# Define 2D algorithm and hypothesis
algo2d = tetra.Triangle() 
algo2d.LengthFromEdges() 

# Define 3D algorithm and hypothesis
algo3d = tetra.Tetrahedron()
algo3d.MaxElementVolume(100) 

# Compute the mesh
tetra.Compute() 

# Create a mesh group of all triangles generated on geom faces present in faces_group
group = tetra.Group(faces_group)

###
# Explore the mesh
###

# Retrieve coordinates of nodes
coordStr = ""
for node in tetra.GetNodesId():
    x,y,z = tetra.GetNodeXYZ( node )
    coordStr += "%s (%s, %s, %s) " % ( node, x,y,z )
    pass

# Retrieve nodal connectivity of triangles
triaStr = ""
for tria in tetra.GetElementsByType( SMESH.FACE ):
    nodes = tetra.GetElemNodes( tria )
    triaStr += "%s (%s, %s, %s) " % ( tria, nodes[0], nodes[1], nodes[2] )

# Retrieve group contents
groupStr = ""
for group in tetra.GetGroups():
    ids   = group.GetIDs()
    name  = group.GetName()
    eType = group.GetType()
    groupStr += "'%s' %s: %s \n" % ( name, eType, ids )

../../../examples/3dmesh.py

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