Conditional user parameters

Conditional user parameters

brad.bylls
Collaborator Collaborator
2,691 Views
5 Replies
Message 1 of 6

Conditional user parameters

brad.bylls
Collaborator
Collaborator

Can you use conditional statements in User parameters?

e.g. If param1 > param2 then param3 = x

Brad Bylls
0 Likes
Accepted solutions (1)
2,692 Views
5 Replies
Replies (5)
Message 2 of 6

kandennti
Mentor
Mentor

Hi @brad.bylls .

 

In Fusion360, I don't think you can use conditional branches like "IF" for parameters.

If you really want to do it, you can use the add-in event.

# FusionAPI python Addin 
import adsk.core, adsk.fusion, traceback
_handlers = []

_targetParameterName1 = 'param1'
_targetParameterName2 = 'param2'
_resultParameterName = 'param3'

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        app.log('start addin')
        ui  = app.userInterface

        onCommandTerminated = MyCommandTerminatedHandler()
        ui.commandTerminated.add(onCommandTerminated)
        _handlers.append(onCommandTerminated)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def stop(context):
    ui = None
    try:
        adsk.core.Application.get().log('stop addin')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


class MyCommandTerminatedHandler(adsk.core.ApplicationCommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        app: adsk.core.Application = adsk.core.Application.get()
        prms: adsk.fusion.ParameterList = app.activeProduct.allParameters
        
        param1: adsk.fusion.Parameter = prms.itemByName(_targetParameterName1)
        param2: adsk.fusion.Parameter = prms.itemByName(_targetParameterName2)
        param3: adsk.fusion.Parameter = prms.itemByName(_resultParameterName)

        if (not param1) or (not param2) or (not param3):
            return

        if param1.value > param2.value:
            param3.value = param1.value
        else:
            param3.value = param2.value

 

However, it is very cumbersome.

 

0 Likes
Message 3 of 6

BrianEkins
Mentor
Mentor
Accepted solution

It's not exactly an if, but you can use the min and max functions in a parameter expression.  For example, the value of a parameter can be "min(param1; param2)".  Notice the semi-colon between them instead of a comma.  Here's the documentation for all of the functions supported in a parameter expression.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-76272551-3275-46C4-AE4D-10D58B408C20

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

brad.bylls
Collaborator
Collaborator

Thanks Brian.

I might be able to work something out with that.

Brad Bylls
0 Likes
Message 5 of 6

brad.bylls
Collaborator
Collaborator

Thanks for the reply.

Brad Bylls
0 Likes
Message 6 of 6

Bowen.Christopher58NJL
Explorer
Explorer

Here is a link to an Addin that evaluates conditional parameters: 

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/new-script-evaluate-logical-operators-and-...