How to make total mass of a part as an Output parameter in Mechanical using Python Code object?

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 193
100 Comments 25 Answers Second Anniversary 25 Likes
✭✭✭✭
edited June 2023 in Structures

In ANSYS Mechanical, we can make the mass of all bodies as a parameter, but, if one wants mass of a multibody Part or an assembly, the Parameter option is not available.

Best Answer

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 193
    100 Comments 25 Answers Second Anniversary 25 Likes
    ✭✭✭✭
    edited September 18 Answer ✓

    Starting from v2021R2, we can use Python Code object to parametrize non-parametrizable properties in Mechanical, such as Mass (for example) shown in the question.

    Here are the steps:

    Step 1: RMB on Solution --> Insert --> Python Code, and set the, "Target Callback" in the Properties to "After Solve"

    Step 2: Python Code object has two tabs, "Script", "Property Provider". Script section is where we put our Mechanical script (python). Property Provider section is used to create custom properties (it can be float, text, drop down menu with options, geometry scoping etc)




    • Property Provider:
      • Replace the first function, def reload_props() in "Property Provider" tab of the Python Code window, with the below script and then, RMB on Python Code object --> Reload Properties. This should now show a custom output property called "Mass"
     def reload_props():
        this.PropertyProvider = None
        
        # comment the following return to allow the rest of the function definition to be executed and add properties
        #return
        
        """
        Some sample code is provided below that shows how to:
            1. Create an instance of the Provider. The Provider class is used to add custom properties to the details.
            2. Use the Provider instance to add custom properties.
            3. Configure those custom properties.
        """
        
        # Create the property instance
        provider = Provider()
        
        # Create a group named Group 1.
        group = provider.AddGroup("Outputs")
        
        # Create a property with control type Expression and a property with control type Double, and add it to the Group 1
        double_prop = group.AddProperty("Mass", Control.Double)
        double_prop.CanParameterize = True
        double_prop.ParameterType = ParameterType.Output
        
        # The valid range set here is used by the IsValid handler in the Provider class, please look at the class definition above.
        # If interested in the implementation, please look at the class definition below
        this.PropertyProvider = provider
    
    • Script:
      • Replace the entire script in the Script section of the Python code object with the below code
      • This script looks for a multibody part with a name, "Component1", extracts its Mass (from its Properties) and plugs it into the custom output property we created.
    def after_solve(this, analysis):# Do not edit this line
        """
        Called after solving the parent analysis.
        Keyword Arguments : 
            this -- the datamodel object instance of the python code object you are currently editing in the tree
            analysis -- Static Structural
        """
    
        # To access properties created using the Property Provider, please use the following command.
        # this.GetCustomPropertyByPath("your_property_group_name/your_property_name")
    
    
        # To access scoping properties use the following to access geometry scoping and named selection respectively:
        # this.GetCustomPropertyByPath("your_property_group_name/your_property_name/Geometry Selection")
        # this.GetCustomPropertyByPath("your_property_group_name/your_property_name/Named Selection")
    
    
        this.GetCustomPropertyByPath("Outputs/Mass").Value = DataModel.GetObjectsByName("Component1")[0].Mass.Value
    

    Step 3: RMB on Python Code --> Reload Properties and click on the check box to send this property as a parameter to WB (you can now see this under Parameter page in WB). One can now start the Design Point run to see the variation of output parameter for each Design Point.