using Dropdown menu items

using Dropdown menu items

MJK_Performance
Advocate Advocate
1,334 Views
5 Replies
Message 1 of 6

using Dropdown menu items

MJK_Performance
Advocate
Advocate

very new to API and Add ins, Im trying to add drop down menu items in an add in, that depending on what is selected, would change a user parameter. I have the menu working and the "else" writing to parameters. But I cant get the IF statement to work, mostly because I have no idea what i'm doing.

 dropDownCommandInput_ = commandInputs_.addDropDownCommandInput('dropdownCommandInput', _('Material'), adsk.core.DropDownStyles.LabeledIconDropDownStyle)
                    dropDownItems_ = dropDownCommandInput_.listItems
                    dropDownItems_.add(_('ALREC00229  0.5in_1in'), True)
                    dropDownItems_.add(_('ALREC00174  1in_2.5in'), False)
                    dropDownItems_.add(_('ALREC00290  1in_3in'), False)
                    if dropDownItems_ == 'ALREC00229  0.5in_1in':
                        userParams.itemByName('stockX').expression = '.5' 
                        userParams.itemByName('stockY').expression = '1'
                    else:
                     userParams.itemByName('stockX').expression = '5' 
                     userParams.itemByName('stockY').expression = '5'

 

0 Likes
Accepted solutions (1)
1,335 Views
5 Replies
Replies (5)
Message 2 of 6

JeromeBriot
Mentor
Mentor

Hello,

 

Try this :

 

if dropDownCommandInput_.selectedItem.name == 'ALREC00229  0.5in_1in':

 

0 Likes
Message 3 of 6

MJK_Performance
Advocate
Advocate

perfect! that works!, but now is there a way to make that selection false so another one can be true? 

 dropDownItems_.add(_('Select'), True)
                    dropDownItems_.add(_('ALREC00229  0.5in_1in'), False)
                    dropDownItems_.add(_('ALREC00174  1in_2.5in'), False)
                    dropDownItems_.add(_('ALREC00290  1in_3in'), False)
                    if dropDownCommandInput_.selectedItem.name == 'Select':
                        userParams.itemByName('stockZ').expression = '1' 
                        userParams.itemByName('stockX').expression = '1'
                        
                    if dropDownCommandInput_.selectedItem.name == 'ALREC00229  0.5in_1in':
                        userParams.itemByName('stockZ').expression = '.5' 
                        userParams.itemByName('stockX').expression = '1'
                    
0 Likes
Message 4 of 6

MJK_Performance
Advocate
Advocate

what I meant to say was... That seem to work great for the first one, but doesn't seem to work for any other selection

0 Likes
Message 5 of 6

JeromeBriot
Mentor
Mentor
Accepted solution

Hello,

 

Here is a basic example:

import adsk.core, adsk.fusion, adsk.cam, traceback

# Global variables to keep everything in scope
handlers = []
app = adsk.core.Application.get()
ui = app.userInterface

# Event handler for the commandCreated event.
class CommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:

            eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
            cmd = eventArgs.command
            inputs = cmd.commandInputs

            dropDownCommandInput = inputs.addDropDownCommandInput('dropdownCommandInput', 'Material', adsk.core.DropDownStyles.LabeledIconDropDownStyle)
            dropDownItems = dropDownCommandInput.listItems
            dropDownItems.add('ALREC00229  0.5in_1in', True)
            dropDownItems.add('ALREC00174  1in_2.5in', False)
            dropDownItems.add('ALREC00290  1in_3in', False)

            #Connect handler to inputChanged event
            onInputChanged = CommandInputChangedHandler()
            cmd.inputChanged.add(onInputChanged)
            handlers.append(onInputChanged)

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


# Event handler for the inputChanged event.
class CommandInputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
         try:

            eventArgs = adsk.core.InputChangedEventArgs.cast(args)

            changedInput = eventArgs.input

            if changedInput.id == 'dropdownCommandInput':

                ui.messageBox(changedInput.selectedItem.name)

                if changedInput.selectedItem.name == 'ALREC00229  0.5in_1in':

                    stockZ = .5
                    stockX = .5

#                    userParams.itemByName('stockZ').expression = '.5'
#                    userParams.itemByName('stockX').expression = '1'

                else:

                    stockX = 5
                    stockY = 5

#                    userParams.itemByName('stockX').expression = '5'
#                    userParams.itemByName('stockY').expression = '5'

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


def run(context):
    try:
        # Get the CommandDefinitions collection.
        cmdDefs = ui.commandDefinitions

        # Create a button command definition.
        buttonDefinition = cmdDefs.addButtonDefinition('TestDropdownId',
                                                   'Test Dropdown',
                                                   '',
                                                   'resources')

        # Get the ADD-INS panel in the model workspace.
        addInsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')

        # Add the button to the bottom of the panel.
        buttonCommand = addInsPanel.controls.addCommand(buttonDefinition)
        buttonCommand.isPromoted = True
        buttonCommand.isPromotedByDefault = True

        # Connect to the command created event.
        commandCreated = CommandCreatedEventHandler()
        buttonDefinition.commandCreated.add(commandCreated)
        handlers.append(commandCreated)

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


def stop(context):
    try:
        # Clean up the UI.
        cmdDef = ui.commandDefinitions.itemById('TestDropdownId')
        if cmdDef:
            cmdDef.deleteMe()

        addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        cntrl = addinsPanel.controls.itemById('TestDropdownId')
        if cntrl:
            cntrl.deleteMe()
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

You should read this carefully: Creating Custom Fusion 360 Commands

0 Likes
Message 6 of 6

MJK_Performance
Advocate
Advocate

AMAZING! thank you!

0 Likes