How to get volume name using python (surf, etc)?

Hi,

In Assemblies, the CAD shows different parts with different names; webcuts increase (even drastically) the total number of volumes:

  • after some basic tests, i understood volumes are added into blocks prior to export the mesh (1 block per original CAD part)
  • I would like to use python to automate the whole process whatever are the names and the number of volumes
  • I had a look to the python API docs/methods and so on, but i probably missed something

Step 1 : How to retrieve the volume name to build a list of all vol names?
“list names” is promissing but it returns a bool?

Thanks for the support

Paul

vol_id = cubit.parse_cubit_list('volume', 'all')
type(vol_id) # ok to retrieve the vol id

var = cubit.cmd('list names volume')
type(var) #bool not a tuple nor a list

Hi @paul18fr,
i think the method you are searching for is cubit.volume(id).entity_name()

Here is an example:

#!cubit
reset
create brick x 1
webcut volume all with plane xplane offset 0 
webcut volume all with plane yplane offset 0 
webcut volume all with plane zplane offset 0 

volume 1 rename "Name_1"
volume 2 rename "Name_2"
volume 3 rename "Name_3"
volume 4 rename "Name_4"
volume 5 rename "Name_5"
volume 6 rename "Name_6"
volume 7 rename "Name_7"
volume 8 rename "Name_8"

#!python
vol_ids = cubit.parse_cubit_list('volume', 'all')

list_names = []
for id in vol_ids:
 vol = cubit.volume(id)
 list_names.append(vol.entity_name ())

for name in list_names:
 print(name)

This will output the names to the command line.
image

Cubit frequently provides multiple methods to do these kinds of things. Once you have the volume id you can also use
name = cubit.get_entity_name(“volume”, id)
This is documented in Coreform Cubit User Documentation, Method Based Python.
Search for get_entity_name.

Norbert’s method is documented in Coreform Cubit User Documentation, Object Based Python. Search for entity_name.

Karl

Ok, thanks for all the informations and links.

The following first basic trial works as expected. Note the “parts_name” list has been manually implemented for now (i guess original names do not include any ‘@’)

Thanks again

parts_name = ['XX', 'YY', 'ZZ', 'MM', 'NN']
vol_ids = cubit.parse_cubit_list('volume', 'all')

vol_names = []

for id in vol_ids:
    name = cubit.get_entity_name("volume", id)
    vol_names.append([name, id])
    
i = 1
for part in parts_name:
    
    for vol, id in vol_names:
        if part in vol: cubit.cmd(f'block {i} add volume {id}')
        
    cubit.cmd(f'block {i} name "{part}"')
    cubit.cmd(f'block {i} element type hex20')
    i += 1
    
    
# cubit.cmd(f"save cub5 '{os.path.join(path, file)}'")