How do you group Geometry bodies in Mechanical using the text before the backslash ?
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 unashable ?
Answers
-
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)
0 -
I am able to create the initial folders in the first part of the if statement but then the else statement needs to add the other geometry items with the same name before the back splash into the same folder I have rationalized the code but cant get the geometries to move into the named folders without creating more folders within folders. Is there an append or move attribute function that can be looped through ?
bodies = ExtAPI.DataModel.Project.Model.Geometry.GetChildren(DataModelObjectCategory.Body, True) folders={} group_name = [] for item in bodies: #bodies = ExtAPI.DataModel.GetObjectsByName(body.Name) components = item.Name.split("\\") folder_name = components[0] if folder_name not in folders: folders[folder_name]=ExtAPI.DataModel.Tree.Group([item]) folders[folder_name].Name = folder_name print("folder made", folder_name) else: #ExtAPI.DataModel.Project.Model.Geometry. #existing_bodies = list(folders[folder_name].Children) #more_bodies = ExtAPI.DataModel.Tree.Group(existing_bodies +[item]) #more_bodies.Name = folder_name name=DataModel.GetObjectsByName(folder_name) folder_name=name.Add(item) #print("object name",name)
0