# List of hex of thousands of cells in a mesh

Hello,

I am new in cubit, and I am trying to figure out how to extract the information of thousands of hex mesh cells. Specifically, I have a cylinder with 250,000 hex mesh cells, and I want to extract information about the cells, especially the centroids. I am currently using list hex ID through the GUI, but this is not efficient and very time-consuming, as it can’t print more than 60 IDs at the same time. Is there a way to extract this information faster and more efficiently and store it in a .txt file?

Thank you!

Hi @tsiaraf,

just use the python api for things like this.

Here is an example.

First we create some geometry and mesh it.

``````#!cubit
reset
create cylinder r 1 height 10
vol 1 size auto factor 6
mesh vol 1
``````

As the python api doesn’t have a function to get the centroid directly for a element, we will define us one.

``````#!python
def get_centroid(el_type,id):
node_ids = cubit.get_connectivity(el_type,id)
centroid = [0,0,0]
for id in node_ids:
coord = cubit.get_nodal_coordinates(id)
centroid[0] += coord[0]
centroid[1] += coord[1]
centroid[2] += coord[2]
centroid[0] = round(centroid[0]/len(node_ids),6)
centroid[1] = round(centroid[1]/len(node_ids),6)
centroid[2] = round(centroid[2]/len(node_ids),6)
return centroid
``````

Then we just need to query the hex ids for our volume

``````hex_ids = cubit.parse_cubit_list("hex","all")
``````

When we have the ids we just make a loop and write the info into a file.

``````file = open("example.txt", "w")
for id in hex_ids:
centroid = get_centroid("hex",id)
file.write(f"hex id: {id} centroid x: {centroid[0]} y: {centroid[1]} z: {centroid[2]} \n")
file.close()
``````

In your current working directory you should now find the example.txt with your requested data in it.

And here is the full input again, you just need to copy/paste and run it.

``````#!cubit
reset
create cylinder r 1 height 10
vol 1 size auto factor 6
mesh vol 1

#!python
def get_centroid(el_type,id):
node_ids = cubit.get_connectivity(el_type,id)
centroid = [0,0,0]
for id in node_ids:
coord = cubit.get_nodal_coordinates(id)
centroid[0] += coord[0]
centroid[1] += coord[1]
centroid[2] += coord[2]
centroid[0] = round(centroid[0]/len(node_ids),6)
centroid[1] = round(centroid[1]/len(node_ids),6)
centroid[2] = round(centroid[2]/len(node_ids),6)
return centroid

hex_ids = cubit.parse_cubit_list("hex","all")

file = open("example.txt", "w")
for id in hex_ids:
centroid = get_centroid("hex",id)
file.write(f"hex id: {id} centroid x: {centroid[0]} y: {centroid[1]} z: {centroid[2]} \n")
file.close()
``````

Great, thank you this is extremely helpful, I appreciate the detail in your response!