Reording userParameters stored in input

Reording userParameters stored in input

systemx17
Enthusiast Enthusiast
606 Views
1 Reply
Message 1 of 2

Reording userParameters stored in input

systemx17
Enthusiast
Enthusiast

I have the attached code as an add in that takes all of the favourite userParameters and adds them to a group in an input display and uses an underscore to identify if the parameter is part of a group (eg. Test1_Parameter1 when Test1 is the group and Parameter1 is the parameter).

 

systemx17_0-1645662841816.png

 

This is working great but I would really like to order the groups as well as the parameters in alphabetical order. Currently the iteration is performed in the order that they were originally entered into the user parameters screen. By adding them to groups at least the groups are sorted, it helps a little. But I would like to get it so that for example Parameter3 shows up AFTER Parameter2 without having to go back, delete all of the parameters that come after my target position and recreate them from scratch.

 

In this example I added Taa_Parameter1, it shows that the grouping as well is added at the end because the new parameter was the last one evaluated by the script.

 

systemx17_1-1645663205001.png

 

Is it possible to reorder the userParameters list (sorry I am not sure if these are lists, or arrays or dictionaries so I am probably not using the correct terminology) by copying it to a new list and sorting it? Or by sorting the inputs after they are created? I am not sure how to do this so I am guessing at possibilities from my past experience, but that is where I am stuck.

 

Thanks for any help you can provide

 

 

 

__author__ = ''

from distutils.log import debug
import adsk.core
import adsk.fusion
import traceback
import re
import os

from .Fusion360Utilities.Fusion360Utilities import get_app_objects
from .Fusion360Utilities.Fusion360CommandBase import Fusion360CommandBase

def update_params(inputs):
    app_objects = get_app_objects()
    design = app_objects['design']
    units_manager = app_objects['units_manager']

    if inputs.count < 1:
        app_objects['ui'].messageBox('No parameters marked as favourite')
        return

    for param in design.userParameters:
        if(param is not None):
            if param.isFavorite == True:
                input_expression = inputs.itemById(str(hash(param.name))).value

                if units_manager.isValidExpression(input_expression, units_manager.defaultLengthUnits) or units_manager.isValidExpression(re.sub("deg", "", input_expression), units_manager.defaultLengthUnits):

                    param.expression = re.sub("deg", "", input_expression)

                else:
                    app_objects['ui'].messageBox("Invalid expression: \n" +
                                         param.name + '\n' +
                                         input_expression + '\n')

class ProParamCommand(Fusion360CommandBase):
    def on_execute(self, command, inputs, args, input_values):
        update_params(inputs)

    def on_create(self, command, inputs):
        app_objects = get_app_objects()
        design = app_objects['design']

        firstRun = True

        for param in design.userParameters:
            if param.isFavorite == True:
                if param.name.count('_') > 0:
                    if(not inputs.itemById(param.name.split('_')[0])):
                        inputGroup = inputs.addGroupCommandInput(param.name.split('_')[0], re.sub(r'((?<=[a-z])[A-Z]|(?<!\A)[A-Z](?=[a-z]))', r' \1', param.name.split('_')[0]))
                        inputGroup.isExpanded = False

                    input = inputs.itemById(param.name.split('_')[0]).children.addStringValueInput(str(hash(param.name)), re.sub(r'((?<=[a-z])[A-Z]|(?<!\A)[A-Z](?=[a-z]))', r' \1', param.name.split('_')[1]), param.expression)
                    input.isFullWidth = False
                    if len(param.comment) > 0:
                        input.tooltip = param.comment
                else:
                    if(firstRun):
                        genericGroup = inputs.addGroupCommandInput("Generic", "Generic")
                        genericGroup.isExpanded = False
                        firstRun = False
                    input = genericGroup.children.addStringValueInput(str(hash(param.name)), re.sub(r'((?<=[a-z])[A-Z]|(?<!\A)[A-Z](?=[a-z]))', r' \1', param.name), param.expression)
                    input.isFullWidth = False
                    if len(param.comment) > 0:
                        input.tooltip = param.comment

 

 

0 Likes
607 Views
1 Reply
Reply (1)
Message 2 of 2

BrianEkins
Mentor
Mentor

It's not possible to sort parameters or command inputs. The best solution will be to read the parameters and store them in a list and sort that list.  Here's some info about how to sort a list based on whatever criteria you choose.

 

https://stackoverflow.com/questions/33940432/how-to-sort-list-according-certain-criterion

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes