How can we create named selections containing the parts that have the same material

Erik Kostson
Erik Kostson Member, Employee Posts: 87
First Answer Name Dropper First Anniversary Ansys Employee
edited December 2023 in General Language Questions

We have an assembly of parts that have different material assignments. How can we create named selections where every named selection (ns) contains only parts with the same material assignment. So say one ns will have all parts with a Structural Steel assignment, while the another ns will have all parts with a Copper material assignment etc.

Comments

  • Erik Kostson
    Erik Kostson Member, Employee Posts: 87
    First Answer Name Dropper First Anniversary Ansys Employee
    edited December 2023

    The sample code below shows one possible way of doing this via mechanical scripting:

    model = ExtAPI.DataModel.Project.Model
    materials=model.Materials.Children
    
    Parts = model.Geometry.GetChildren(DataModelObjectCategory.Part,True)
    selId = []
    
    SlMn = ExtAPI.SelectionManager
    SlMn.ClearSelection()
    Sel = SlMn.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    
    for mat in materials:
        for Part in Parts:
            PName = Part.Name
            print(PName)
            for Body in Part.Children:
                Bmatname = Body.Material
                if mat.Name == Bmatname:
                    BId = Body.GetGeoBody().Id
                    selId.append(BId)
        Sel.Ids=selId
        ns=model.AddNamedSelection()
        ns.Location=Sel
        ns.Name=mat.Name
        selId=[]
        SlMn.ClearSelection()
    
    
    
This discussion has been closed.