Using Python to find free vertices inside, outside, on vol

Use this forum to share journal files/scripts.

Using Python to find free vertices inside, outside, on vol

Postby csimsoft Moderator » Thu Sep 08, 2016 9:40 am

Use Python to determine if a free vertex is inside, outside, or on a volume. (See ... erface.htm for more information about using Python in Trelis)

The example creates a brick and three vertices, one inside, one outside, and one on the volume.

cubit.cmd("bri x 10")
cubit.cmd("create vert 0 0 0")
cubit.cmd("create vert 5 0 0")
cubit.cmd("create vert 6 0 0")

my_vol = cubit.volume(1)
vert1 = cubit.vertex(9)
vert2 = cubit.vertex(10)
vert3 = cubit.vertex(11)

# We only expect one body to be in this, but note that it is a tuple
vol_bodies = my_vol.bodies()
my_body = vol_bodies[0]

print my_body.point_containment(vert1.coordinates())
print my_body.point_containment(vert2.coordinates())
print my_body.point_containment(vert3.coordinates())

A value of 0 means outside, 1 means inside, 2 means on the volume.
csimsoft Moderator
Posts: 264
Joined: Tue Feb 14, 2012 5:59 pm
Location: American Fork, UT

Re: Using Python to find free vertices inside, outside, on v

Postby omid_kh_m » Fri May 19, 2017 2:29 pm

Continuing on this example, below is a script that will find all the nodes that fall inside an interior, unmeshed volume within a larger, encompassing external volume and group the nodes as a Node Set. Similarly, the script will group all elements that fall inside or intersect with the internal volume into a Block.

Code: Select all
cubit.cmd("bri x 10")
cubit.cmd("bri x 5")
cubit.cmd("volume 1 scheme Tetmesh")
cubit.cmd("volume 1 size auto factor 5")
cubit.cmd("mesh vol 1")  # Note that only the volume 1 is meshed, not volume 2
# Get element inside the internal volume (3 here)
my_vol = cubit.volume(3)
vol_bodies = my_vol.bodies()
int_body = vol_bodies[0]
nodes_in_body = []
elements_in_body = []
vol1_elements = cubit.parse_cubit_list("tet", "in volume 1")
# Loop over all tets in the volume
for tet_id in vol1_elements:
  interior_tet = False
  tet_nodes = cubit.parse_cubit_list("node", "in tet "+str(tet_id))
  # Loop over the four nodes of the tet
  for node_id in tet_nodes:
    node_coord = cubit.get_nodal_coordinates(node_id)
    result = int_body.point_containment(node_coord)
    # 1: inside; 2: on the boudary
    if result == 1 or result == 2:
      interior_tet = True
      if node_id not in nodes_in_body:
  if interior_tet:

print "Nodes in Internal Body: {}".format(nodes_in_body)
print "Elements in Internal Body: {}".format(elements_in_body)
cubit.cmd("nodeset 1 add node " +  " ".join(str(e) for e in nodes_in_body))
cubit.cmd("block 1 add tet " +  " ".join(str(e) for e in elements_in_body))
Posts: 1
Joined: Tue Mar 28, 2017 12:01 pm

Return to Journal Files/Scripts

Who is online

Users browsing this forum: No registered users and 1 guest


Search / Mark

User Control Panel


Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest (based on users active over the past 5 minutes)
Most users ever online was 58 on Mon Jun 26, 2017 4:19 pm

Users browsing this forum: No registered users and 1 guest