How can I access the bodies in Mechanical through scripting?

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
100 Answers 500 Comments 250 Likes First Anniversary
✭✭✭✭
edited June 2023 in Structures

What are the different methods to access the bodies in Mechanical through scripting?

Tagged:

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭
    Answer ✓

    There are several methods depending on what is meant by "bodies". The first method uses the automation API to get the children inside the geometry branch of Mechanical:

    for part in ExtAPI.DataModel.Project.Model.Geometry.Children:
        for body in part.Children:
            print body.Name
    

    As the tree structure is changed if the Mechanical model uses model assembly, another possibility is to use the type of the entities in the tree as a filter to select the bodies:

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

    The two methods above explain how to get to the bodies in the Mechanical tree (as children of the geometry branch). This is different to accessing the actual geometric bodies. To do this, one should use GeoData:

        for Assembly in ExtAPI.DataModel.GeoData.Assemblies:
            for Part in Assembly.Parts:
                for Body in Part.Bodies:
                    for Surface in Body.Faces:
                        print(Surface.Area)