I have two watertight stl files that define surfaces for two sets of volumes. Each stl file actually has multiple volumes. I would like to import the stl file and label the set of volumes in each stl file, then create tetrahedral mesh.
My example python script is below.
import sys
sys.path.append("/opt/Coreform-Cubit-2025.3/bin")
import cubit
cubit.cmd("import stl '1.stl' feature_angle 90.00 merge")
cubit.cmd("vol all scheme tet")
cubit.cmd("mesh vol all")
cubit.cmd("export nastran 'mesh.bdf' mesh_only overwrite everything")
I am wondering what cubit comands to use for that.
Welcome @leshinka!
You’re close! To “label” things for exporting to a mesh file you’ll want to assign Exodus entities. For example:
import sys
sys.path.append("/opt/Coreform-Cubit-2025.3/bin")
import cubit
## Import the first file
cubit.cmd("import stl '1.stl' feature_angle 90.00 merge")
cubit.cmd("block 1 volume all")
## Import the second file
cubit.cmd("import stl '2.stl' feature_angle 90.00 merge")
# Relies on the default behavior of unique block assignments
cubit.cmd("block 2 volume all")
cubit.cmd("vol all scheme tetmesh")
cubit.cmd("mesh vol all")
cubit.cmd("export nastran 'mesh.bdf' mesh_only overwrite everything")
Another way you could do this, which doesn’t rely on the default block assignment rules in Cubit

is by grabbing the range of volume ids imported by each STL file:
import sys
sys.path.append("/opt/Coreform-Cubit-2025.3/bin")
import cubit
volume_id_range = [ 0, 0 ]
## Import the first file
volume_id_range[0] = cubit.get_last_id( "volume" ) + 1
cubit.cmd("import stl '1.stl' feature_angle 90.00 merge")
volume_id_range[1] = cubit.get_last_id( "volume" )
cubit.cmd(f"block 1 volume {volume_id_range[0]} to {volume_id_range[1]}")
## Import the second file
volume_id_range[0] = cubit.get_last_id( "volume" ) + 1
cubit.cmd("import stl '2.stl' feature_angle 90.00 merge")
volume_id_range[1] = cubit.get_last_id( "volume" )
cubit.cmd(f"block 2 volume {volume_id_range[0]} to {volume_id_range[1]}")
cubit.cmd("vol all scheme tetmesh")
cubit.cmd("mesh vol all")
cubit.cmd("export nastran 'mesh.bdf' mesh_only overwrite everything")
Which is a bit more amenable to automation (e.g., if you have many files you want to import):
import sys
import pathlib
import glob
sys.path.append( "/opt/Coreform-Cubit-2025.3/bin" )
import cubit
stl_files = glob.glob( "*.stl" )
volume_id_range = [ 0, 0 ]
for ( i, filename ) in enumerate( stl_files ):
block_name = pathlib.Path( filename ).stem
volume_id_range[0] = cubit.get_last_id( "volume" ) + 1
cubit.cmd(f"import stl '{filename}' feature_angle 90.00 merge" )
volume_id_range[1] = cubit.get_last_id( "volume" )
cubit.cmd(f"block {i} volume {volume_id_range[0]} to {volume_id_range[1]}" )
cubit.cmd( "vol all scheme tetmesh" )
cubit.cmd( "mesh vol all" )
cubit.cmd( "export nastran 'mesh.bdf' mesh_only overwrite everything" )
1 Like