How do you group Geometry bodies in Mechanical using the text before the backslash ?

Options
LTC
LTC Member Posts: 2

I have brought my model across from space claim and want to regroup the bodies into folders using the name of the item before the backslash. I am trying to write the code to go through each body and if it doesn't have a folder with that name create a new folder if not added the body to the folder already generated. This is what I have so far

bodies = ExtAPI.DataModel.Project.Model.Geometry.GetChildren(DataModelObjectCategory.Body,True)

Create an empty dictionary to store the folders

folders = {}

Loop through the bodies and create a folder for each unique substring

for body in bodies:
# Get the body by its name
body_objects = ExtAPI.DataModel.GetObjectsByName(body.Name)

# Check if the list of bodies with the specified name is not empty
if body_objects:
    # Get the first body object with the given name
    body_object = body_objects[0]

    # Extract the substring (everything forward of the first backslash)
    substring = body.Name.split('\\', 1)

    # If a folder for this substring doesn't exist, create it
    if substring not in folders:
        folders[substring] = ExtAPI.DataModel.Tree.Group([body_object])
        folders[substring].Name = substring

    # Add the body to the corresponding folder
    folders[substring].Add(body_object)

Why are the list of objects unhashable ?

Answers

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 283
    First Anniversary First Comment 5 Likes Ansys Employee
    Options

    you use the name "body_object" for the loop but then refer to "body" in this line. This may be a bug.

    substring = body.Name.split('\', 1)