how i can add the input to the userparameter?

how i can add the input to the userparameter?

mohammadkhader698
Participant Participant
510 Views
2 Replies
Message 1 of 3

how i can add the input to the userparameter?

mohammadkhader698
Participant
Participant

I used 'addInputValue' to take the input from the user but i cant put it in userprameter when i put the value in messgebox the box appeared before i put the input so how i cant insert the input from addinputvalue to userparameter

0 Likes
511 Views
2 Replies
Replies (2)
Message 2 of 3

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

This is the way you update the parameter expression with the value from the command input:

DISTANCE_PARAMETER  = 'distance'
ID_VALUE_INPUT = 'idValueInput'   # <-- replace by the id of the valueInput

. . .

design = adsk.fusion.Design.cast(app.activeProduct)
valInp: adsk.core.ValueCommandInput = eventArgs.command.commandInputs.itemById(ID_VALUE_INPUT)

distParam = design.userParameters.itemByName(DISTANCE_PARAMETER)
if not distParam:
    # parameter doesn't exists -> lets creat it
    distVal = adsk.core.ValueInput.createByString(f'{valInp.value}')
    distParam = design.userParameters.add(DISTANCE_PARAMETER, distVal, 'mm', '')
else:
    # paremeter already exists -> lest update its expression attribute
    distParam.expression = f'{valInp.value}'

This code goes inside the commandExecuteHandler, in the notify method.

 

Note: please kept your questions in a single post; this could be helpful for others looking to solve questions like yours.

 

Regards,

Jorge

 

PD: likes and accepted solutions are welcome!!

Message 3 of 3

BrianEkins
Mentor
Mentor

I would recommend a couple of small changes to @Jorge_Jaramillo great post.

if not distParam:
    # parameter doesn't exists -> lets creat it
    distVal = adsk.core.ValueInput.createByString(valInp.expression)
    distParam = design.userParameters.add(DISTANCE_PARAMETER, distVal, design.unitsManager.defaultLengthUnits, '')
else:
    # paremeter already exists -> lest update its expression attribute
    distParam.expression = valInp.expression

 

When creating a new parameter, it is usually best to use the defaultLengthUnit property of the UnitsManager. This returns whatever the user has set as their length units for the design. For example, if they've chosen inches it will return "inch," and if they've chosen millimeters, it will return "mm", etc. 

 

I also changed it to use the expression property of the ValueInput instead of value. The expression property returns the string the user has entered in the ValueInput. The value property converts that string to a number that is always in centimeters, if the value represents a length. The big advantage of using the expression is you keep any equations the user has entered. For example, they could enter "d1/2" into the ValueInput. The expression property will return "d1/2" and this expression is then assigned to the parameter which knows how to evaluate the string to get the correct value.

 

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