Extract mass per body through scripting

Member, Moderator, Employee Posts: 873
100 Answers 500 Comments 250 Likes Second Anniversary
✭✭✭✭
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

  • Member, Moderator, Employee Posts: 873
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭
    Answer ✓

    The following function can be used:

    1. def ExtractMassInfo():
    2. '''
    3. Extract masses for all bodies and point masses
    4. '''
    5. listExportMasses = []
    6. # looping through geometry to extract name and mass of unssuppressed bodies
    7. for part in ExtAPI.DataModel.Project.Model.Geometry.Children:
    8. if part.Suppressed is False:
    9. partType = part.GetType()
    10. if partType == Ansys.ACT.Automation.Mechanical.Part:
    11. for body in part.Children:
    12. if body.Suppressed is False:
    13. bodyName = body.Name
    14. bodyMass = body.Mass
    15. bodyMassValue = round(bodyMass.Value, 5)
    16. bodyMassUnit = bodyMass.Unit
    17. bodyCentroidX = body.CentroidX
    18. bodyCentroidXValue = round(bodyCentroidX.Value, 5)
    19. bodyCentroidXUnit = bodyCentroidX.Unit
    20. bodyCentroidY = body.CentroidY
    21. bodyCentroidYValue = round(bodyCentroidY.Value, 5)
    22. bodyCentroidYUnit = bodyCentroidY.Unit
    23. bodyCentroidZ = body.CentroidZ
    24. bodyCentroidZValue = round(bodyCentroidZ.Value, 5)
    25. bodyCentroidZUnit = bodyCentroidZ.Unit
    26. listExportMasses.append([
    27. str(bodyName),
    28. str(bodyMassValue),
    29. str(bodyMassUnit),
    30. str(bodyCentroidXValue),
    31. str(bodyCentroidXUnit),
    32. str(bodyCentroidYValue),
    33. str(bodyCentroidYUnit),
    34. str(bodyCentroidZValue),
    35. str(bodyCentroidZUnit)
    36. ])
    37. elif partType == Ansys.ACT.Automation.Mechanical.PointMass:
    38. massName = part.Name
    39. massMass = part.Mass
    40. massMassValue = round(massMass.Value, 5)
    41. massMassUnit = massMass.Unit
    42. massCentroidX = part.XCoordinate
    43. massCentroidXValue = round(massCentroidX.Value, 5)
    44. massCentroidXUnit = massCentroidX.Unit
    45. massCentroidY = part.YCoordinate
    46. massCentroidYValue = round(massCentroidY.Value, 5)
    47. massCentroidYUnit = massCentroidY.Unit
    48. massCentroidZ = part.ZCoordinate
    49. massCentroidZValue = round(massCentroidZ.Value, 5)
    50. massCentroidZUnit = massCentroidZ.Unit
    51. listExportMasses.append([
    52. str(massName),
    53. str(massMassValue),
    54. str(massMassUnit),
    55. str(massCentroidXValue),
    56. str(massCentroidXUnit),
    57. str(massCentroidYValue),
    58. str(massCentroidYUnit),
    59. str(massCentroidZValue),
    60. str(massCentroidZUnit)
    61. ])
    62. else:
    63. pass
    64. return listExportMasses

Answers

  • 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?

  • Member, Moderator, Employee Posts: 873
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭

    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.

Welcome!

It looks like you're new here. Sign in or register to get started.