How can I dynamically update a list of options in a Python code or Python result?

Pierre Thieffry
Pierre Thieffry Member, Moderator, Employee Posts: 107
25 Answers Second Anniversary 10 Comments 25 Likes
✭✭✭✭
edited June 2023 in Structures

I want a list of options to be actuated based on its content. Let say I am showing a list of coordinate systems to choose from. How can I make sure the list of options is the actual list of available CS?

Tagged:

Answers

  • Pierre Thieffry
    Pierre Thieffry Member, Moderator, Employee Posts: 107
    25 Answers Second Anniversary 10 Comments 25 Likes
    ✭✭✭✭
    Answer ✓

    Here's a solution, using 'GetOptionsCallback'. In the property provider, you define your option property like this:

    options_prop = group.AddProperty("Coordinate systems", Control.Options)
    options_prop.GetOptionsCallback= get_options_handler
    

    Then you add this definition to the main script:

    def get_options_handler(prop):
        opt = {}
        num_opt=1
        for comp in ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.CoordinateSystem):
            opt[num_opt] = comp.Name
            num_opt+=1
        return opt
    
  • James Derrick
    James Derrick Administrator, Employee Posts: 283
    Ancient Membership 100 Comments 100 Likes 25 Answers
    admin

    Can you add a code-snippet example of what you mean?

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 210
    100 Comments 25 Answers Second Anniversary 25 Likes
    ✭✭✭✭
    Answer ✓

    Building on the script Pierre T provided, I modified it further to remove the small caveat (when you delete the system that is currently picked in the Python code, it will not invalidate it and show a number instead of the CS name.) associated with the previous solution.

    In the property provider, you define your option property like this: In addition to using 'GetOptionsCallback', we would need to use 'IsValidCallback' as well.

    options_prop = group.AddProperty("Coordinate systems", Control.Options)
    options_prop.GetOptionsCallback= get_options_handler
    options_prop.IsValidCallback = dropDownValid
    

    Then you add these two function definitions to the main script:

    def get_options_handler(prop):
        global opt
        try:
            numOptions_beforeRefresh = len(opt)
        except:
            ExtAPI.Log.WriteMessage("Opt is not yet defined")
        opt = {}
        num_opt=1
        for comp in ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.CoordinateSystem):
            opt[num_opt] = comp.Name
            num_opt+=1
        if prop.Value != None:
            if str(prop.Value) > str(len(opt)):
                prop.Value = None
        if "numOptions_beforeRefresh" in locals():
            if numOptions_beforeRefresh > len(opt):
                prop.Value = None
                
        return opt
        
    def dropDownValid(prop):
        if prop.Value==None:
            return False
        elif prop.ValueString.isnumeric() == True:
            return False
        else:
            return True