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 Modifying 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()
  2. Create a smeshBuilder.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()
    quadrangle = mesh.Quadrangle()
    # use other 2D algorithm on a face -- a sub-mesh appears in the mesh
    triangle   = mesh.Triangle( face )
  4. Create and assign hypotheses by calling corresponding methods of algorithms:
    regular1D.LocalLength( 10. )
    triangle.SetMaxSize( 20. )
    triangle.SetFineness( smeshBuilder.VeryCoarse )
  5. Compute the mesh (generate mesh nodes and elements):

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 basic information on mesh nodes, elements and groups.

Example of 3d mesh generation:

# 3d mesh generation and mesh exploration

import salome

import SMESH
from salome.geom import geomBuilder
from salome.smesh import smeshBuilder

geom_builder = geomBuilder.New()
smesh_builder = smeshBuilder.New()

# 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 = geom_builder.MakeBox(-cote, -cote, -cote, +cote, +cote, +cote) 

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

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

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

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

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

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

# Create a mesh

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

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

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

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

# Compute the mesh
if not tetra.Compute(): raise Exception("Error when computing Mesh")

# 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 )

# 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 )

Download this script

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