Assign material in Mechanical through scripting

Options
Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 827
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
edited June 2023 in Structures

Using ACT, how can I select a body named selection and apply a material assignment to this named selection ?

Tagged:

Best Answer

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 827
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓
    Options

    The following code can be used. Please note the body named selection and the material must already be defined previous to using this code.

        ExtAPI.SelectionManager.ClearSelection()
        ns = ExtAPI.DataModel.GetObjectsByName("MyBodyNamedSelection")
        ma = ExtAPI.DataModel.Project.Model.Materials.AddMaterialAssignment()
        ma.Material = "MyMaterial"
        ma.Location = ns[0]
    

Answers

  • CollinsJnr
    CollinsJnr Member Posts: 17
    First Comment Name Dropper
    Options

    Hey @Pernelle Marone-Hitz

    Please I have a question

    This is a recording in Ansys Mechanical script for material assignment (similar to what you have)

    region Details View Action

    selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    selection.Ids = [4]
    material_assignment_37.Location = selection

    endregion

    region UI Action

    with Transaction(True):
    material_assignment_37.Material = "732b2fff-a359-4770-aaa8-e6a1c0aadfa9"

    endregion

    My question are:

    1. What is "732b2fff-a359-4770-aaa8-e6a1c0aadfa9". It seems like a material identifier, but it keeps changing each time I run a new instant of mechanical.

    2. How do I find it in each new mechanical instance? (I understand this identifier appears in the ds.dat file but at the start of the project, the ds.dat file is unavailable so I can't get it.)

    Thank you in advance

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 827
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Options

    Hi @CollinsJnr , the number you are getting is some reference to the material internal ids. As you have experienced, it is not robust to use it. Just use the name of the material, it should work and be robust.