Extract mass per body through scripting

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

How can I extract mass of each body and each point mass in the model through Mechanical scripting?

Tagged:

Best Answer

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

    The following function can be used:

    def ExtractMassInfo():
        '''
        Extract masses for all bodies and point masses
        '''
        listExportMasses = []
        # looping through geometry to extract name and mass of unssuppressed bodies
        for part in ExtAPI.DataModel.Project.Model.Geometry.Children: 
            if part.Suppressed is False: 
                partType = part.GetType() 
                if partType == Ansys.ACT.Automation.Mechanical.Part: 
                    for body in part.Children: 
                        if body.Suppressed is False: 
                            bodyName = body.Name 
                            bodyMass = body.Mass 
                            bodyMassValue = round(bodyMass.Value, 5) 
                            bodyMassUnit = bodyMass.Unit
                            bodyCentroidX = body.CentroidX
                            bodyCentroidXValue = round(bodyCentroidX.Value, 5)
                            bodyCentroidXUnit = bodyCentroidX.Unit
                            bodyCentroidY = body.CentroidY
                            bodyCentroidYValue = round(bodyCentroidY.Value, 5)
                            bodyCentroidYUnit = bodyCentroidY.Unit
                            bodyCentroidZ = body.CentroidZ
                            bodyCentroidZValue = round(bodyCentroidZ.Value, 5)
                            bodyCentroidZUnit = bodyCentroidZ.Unit
                            listExportMasses.append([
                                str(bodyName),
                                str(bodyMassValue),
                                str(bodyMassUnit),
                                str(bodyCentroidXValue),
                                str(bodyCentroidXUnit),
                                str(bodyCentroidYValue),
                                str(bodyCentroidYUnit),
                                str(bodyCentroidZValue),
                                str(bodyCentroidZUnit)
                            ])
                elif partType == Ansys.ACT.Automation.Mechanical.PointMass:
                    massName = part.Name
                    massMass = part.Mass
                    massMassValue = round(massMass.Value, 5)
                    massMassUnit = massMass.Unit
                    massCentroidX = part.XCoordinate
                    massCentroidXValue = round(massCentroidX.Value, 5)
                    massCentroidXUnit = massCentroidX.Unit
                    massCentroidY = part.YCoordinate
                    massCentroidYValue = round(massCentroidY.Value, 5)
                    massCentroidYUnit = massCentroidY.Unit
                    massCentroidZ = part.ZCoordinate
                    massCentroidZValue = round(massCentroidZ.Value, 5)
                    massCentroidZUnit = massCentroidZ.Unit
                    listExportMasses.append([
                        str(massName),
                        str(massMassValue),
                        str(massMassUnit),
                        str(massCentroidXValue),
                        str(massCentroidXUnit),
                        str(massCentroidYValue),
                        str(massCentroidYUnit),
                        str(massCentroidZValue),
                        str(massCentroidZUnit)
                    ])
                else:
                    pass
        return listExportMasses
    

Answers

  • Huy.Truong
    Huy.Truong Member Posts: 1
    First Comment

    Hi Pernelle,
    I add this python code to Properties Provider. I set up the Target Callback as Get Post Commands. However, I can not get the mass

    Could you show me how to run the code? or Do I need to modify the Target Callback?

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

    Hi @Huy.Truong , the property provider tab is to modify the details view of the Python Code object (ie add inputs and/or outputs parameters). It doesn't get executed when the Python Code object is triggered. Code for the Python Code object should be entered in the "Script" tab. Please note that the function I provided above is to be executed from the console. Also, "Get Post Command" callback is used to send MAPDL code for postprocessing so it is not suited in your case. I'd recommend having a look at this section of the help, it would help you.