How to parametrize User Defined Membrane Offset in Mechanical?

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 176
100 Comments Photogenic Ansys Employee 5 Likes
✭✭✭✭

I would like to parametrize "Membrane Offset" highlighted below. This is not natively possible.

Best Answer

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 176
    100 Comments Photogenic Ansys Employee 5 Likes
    ✭✭✭✭
    Answer ✓

    One can use Python Code object inserted under the Surface body to parametrize the Membrane offset value.

    Here are the steps:

    1. RMB on the Surface Body --> Insert --> Python Code
    2. Change the "Target Callback" property to "After Object Changed"
    3. Paste the below script under the "Script" tab (Python code object has two tabs. Script tab to create an automation script, Property Provider tab to create custom properties to input data)

    def after_object_changed(this, object_changed, property_name):# Do not edit this line
        """
        Called after an object is changed.
        Keyword Arguments : 
            this -- the datamodel object instance of the python code object you are currently editing in the tree
            object_changed -- The object that was changed
            property_name -- The property that was changed
        """
    
    
        # 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")
    
        if object_changed == this:
            surfaceBody = this.Parent
            offsetVal = this.GetCustomPropertyByPath("Properties/Membrane Offset").Value
            unit = surfaceBody.OffsetX.Unit
            surfaceBody.OffsetX = Quantity(offsetVal,unit)
    
    1. Replace the function "reload_properties" with the below script under "Property Provider" tab. This step is to create a custom property under Python Code object properties, which can be parametrized. We then connect this number to the Offset value in Surface body using Mechanical scripting.

    def reload_props():
        this.PropertyProvider = None
    
        # Create the property instance
        provider = Provider()
    
        # Create a group named Group 1.
        group = provider.AddGroup("Properties")
    
        # 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("Membrane Offset", Control.Double)
    
        # Configure the double property to be parameterizable. As a default the property will be an input parameter.
        # However, by updating the ParameterType property, it can be configured to be a output parameter as well.
        double_prop.CanParameterize = True
    
        # Connects the provider instance back to the object by setting the PropertyProvider member on this, 'this' being the 
        # current instance of the Python Code object.
        this.PropertyProvider = provider
    
    1. RMB on Python Code --> Reload. This should show a new property under Python Code called "Membrane Offset". This can also be parametrized like any other Mechanial parameters.
    2. RMB on Python Code --> Connect
    3. Try changing the number here in Python Code properties and verify if its changing the Membrane offset in the Surface body and if you encounter any error messages in Mechanical (if yes, debugging of code is needed)
    4. Also go to WB --> Tools --> Options --> Mechanical --> "Reconnect Python Code Object when Mechanical is launched". This step needs to be done before starting design point studies.