Need help updating python result property upon changing another property.


Hi again

Was wondering if someone would know how to setup a python result property that updates another property upon change.

For example, when I change the Weaker plate material field, I would like the weld strength to be updated with it to what ever value I have associated to the plate.

You can assume that I am using options for these two fields like so:

I would just need an example or a nudge in the right direction with this. I remember in VBA forms, you could specify what happens in case of change to a field or click on the form or …. I was hoping there was a similar trick here as well.

Let me know if this can be done. Thanks guys 😊

Best Answer

  • Pierre Thieffry
    Pierre Thieffry Member, Moderator, Employee Posts: 103
    First Anniversary Ansys Employee Solution Developer Community of Practice Member Photogenic
    edited May 2023 Answer ✓

    @Kev YOu will need to add OnAfterPropertyChanged callback to the list that will drive the other and then define what the changes do in the main script.

    So, the property provider code will look something like this:

    def reload_props():
      this.PropertyProvider = None
      # comment the following return to allow the rest of the function definition to be executed and add properties
      # Create the property instance
      provider = Provider()
      # Create a new group named Group 2.
      group_two = provider.AddGroup("Group 2")
      # Add an options property to the second group
      options_prop = group_two.AddProperty("Choice 1", Control.Options)
      # Add a couple options to the options property.
      options_prop.Options = {1 : "Value 1", 2 : "Value 2"}
        # Add an options property to the second group
      options_prop2 = group_two.AddProperty("Choice 2", Control.Options)
      # Add a couple options to the options property.
      options_prop2.Options = {1 : "Value A", 2 : "Value B", 3 : "Value C"}
      def property_changed_handler(sender, args):
      options_prop.OnAfterPropertyChanged += property_changed_handler
      # 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

    And the script of the Python result will have the "on_property_changed' function defined with how your lists should behave:

    def post_started(sender, analysis):# Do not edit this line
    # Uncomment this function to enable retrieving results from the table/chart
    # def table_retrieve_result(value):# Do not edit this line
        # import mech_dpf
        # import Ans.DataProcessing as dpf
        # wf = dpf.Workflow(this.WorkflowId)
        # wf.Connect('contour_selector', value)
        # this.Evaluate()
    def on_property_changed(prop):
        if prop.Value == "1":
            this.GetCustomPropertyByPath("Group 2/Choice 2").Value="3"
        if prop.Value == "2":
            this.GetCustomPropertyByPath("Group 2/Choice 2").Value="1"
    def define_dpf_workflow(analysis):
        import mech_dpf
        import Ans.DataProcessing as dpf
        dataSource = dpf.DataSources(analysis.ResultFileName)
        u = dpf.operators.result.displacement()
        nrm = dpf.operators.math.norm_fc()
        # timeScop = dpf.Scoping()
        # timeScop.Ids = [1]
        # u.inputs.time_scoping.Connect(timeScop)
        dpf_workflow = dpf.Workflow()
        # dpf_workflow.SetInputName(u, 0, 'time')
        # dpf_workflow.Connect('time', timeScop)
        dpf_workflow.Record('wf_id', False)
        this.WorkflowId = dpf_workflow.GetRecordedId()


  • ChrisC
    ChrisC Member Posts: 11
    First Anniversary First Comment

    For the Weaker Plate Material <property> I think you want to add a callback for for <OnValidate> . So something like below. Basically one the drop down selection has been validated, it will run the Python Function UpdateStrength, which can then pull the Weaker Plate Material value, and update the Weld Strength appropriately. The UpdateStrength should get passed two variables, one for the object, and one for the property. So you should have everything you need to pull the Weaker Plate material specified, then pass back the Weld strength value.

    <Property....caption="Weaker Plate Material".......>


    <OnValidate> UpdateStrength</OnValidate>



  • ChrisC
    ChrisC Member Posts: 11
    First Anniversary First Comment

    Perhaps I misunderstood your question. The solution above will work for an ACT Extension and associated XML file. I'm not sure about python result object.

  • Kev
    Kev Member Posts: 41
    First Anniversary 5 Likes First Comment Name Dropper

    Thanks @Pierre Thieffry. That did the trick.

    Where can i find the documentations on Python results functions ? my google search on "on_property_changed" doe snot return much?