Need help updating python result property upon changing another property.

Kev
Kev Member Posts: 41
10 Comments First Anniversary 5 Likes Name Dropper
**

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: 107
    25 Answers Second Anniversary 10 Comments 25 Likes
    ✭✭✭✭
    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
      #return
       
       
      # 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):
        on_property_changed(sender)
         
      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
        define_dpf_workflow(analysis)
    
    
    # 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
        mech_dpf.setExtAPI(ExtAPI)
        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)
        u.inputs.data_sources.Connect(dataSource)
        nrm.Connect(u)
        dpf_workflow = dpf.Workflow()
        dpf_workflow.Add(u)
        dpf_workflow.Add(nrm)
        # dpf_workflow.SetInputName(u, 0, 'time')
        # dpf_workflow.Connect('time', timeScop)
        dpf_workflow.SetOutputContour(nrm)
        dpf_workflow.Record('wf_id', False)
        this.WorkflowId = dpf_workflow.GetRecordedId()
    


Answers

  • 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".......>

    <Callbacks>

    <OnValidate> UpdateStrength</OnValidate>

    </Callbacks>

    </Property>

  • 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
    10 Comments First Anniversary 5 Likes 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?