addDropDown control to ToolbarPanel

sgraves
Enthusiast

addDropDown control to ToolbarPanel

sgraves
Enthusiast
Enthusiast

I have been trying to find documentation on adding a dropdown control to a ToolbarPanel.   The API user manual teases by showing examples without code.  Searching shows numerous examples of adding a button, but I haven't any for the dropdown.

 

The user manual implies that every control needs a command definition and it implies that the ListDefinition is the one for a dropdown.  But the addDropDown method is not like the addCommand method.  It does not have a parameter for a command definition.

 

Below is my code that doesn't work.  What is the right way to do it?

 

# -*- coding: utf-8 -*-
"""
Created on Fri Jun  9 17:44:24 2017

Gear Generator
@author: Steve Graves
"""
import adsk.core, adsk.fusion, adsk.cam, traceback
import math

# Globals
_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)
_units = ''

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface

        createPanel = _ui.allToolbarPanels.itemById('SolidCreatePanel')
        dropCmdDefGear = _ui.commandDefinitions.addListDefinition('smgDropGearGenList', 'Gear Generator', adsk.core.ListControlDisplayTypes.StandardListType)

        dropGear = createPanel.controls.addDropDown('Gear Generator','', 'smgDropGearGen') #Resources/GearGen

        cmdGearPlanetDef = _ui.commandDefinitions.addButtonDefinition('smgCmdGearGenPlanet', 'Planetary Gears', 'Creates a planetary gear component', '')   #Resources/GearGen     
        planetGearButton = dropGear.controls.addCommand(cmdGearPlanetDef)
        
        if context['IsApplicationStartup'] == False:
            _ui.messageBox('The "Gear Generator" commands have been added\nto the CREATE panel of the MODEL workspace.')
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def stop(context):
    try:
        createPanel = _ui.allToolbarPanels.itemById('SolidCreatePanel')
        planetGearButton = createPanel.controls.itemById('smgCmdGearGenPlanet')       
        if planetGearButton:
            planetGearButton.deleteMe()
        
        cmdDef = _ui.commandDefinitions.itemById('smgCmdGearGenPlanet')
        if cmdDef:
            cmdDef.deleteMe()
            
        cmdDef = _ui.commandDefinitions.itemById('smgDropGearGenList')
        if cmdDef:
            cmdDef.deleteMe()
            
            
        dropCtl = createPanel.controls.itemById('smgDropGearGen')
        if dropCtl:
            dropCtl.deleteMe()

    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Reply
Accepted solutions (1)
700 Views
7 Replies
Replies (7)

sgraves
Enthusiast
Enthusiast
Accepted solution

I have my solution.  I have a VPN (Virtual Private Network) and I have been keeping the files I work on in folders on the VPN. That way I am working on the same file whatever I am.  It had appeared that this was allowed because my scripts were interacting with Fusion.  I was exploring the palette sample code and found that it didn't work from a folder on the VPN.  When I followed the instructions exactly and created an add-in from Fusion, it worked.  Turns out the same is true for my Gear Generator add-in.  The code below now works.

 

I still have a question about the ListDefinition.  It appears that I don't need it here.  The User Manual is either misleading or incomplete when it discusses the DropDown.  I would bet there is a use for ListDefinition or that it is an obsolete object.  BTW, the documentation shows a property called listItems that doesn't appear to be available on the actual object.

 

Can someone fill us in on the uses for ListDefinition ?

 

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

# Globals
_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)
_units = ''

_handlers = []

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface

        createPanel = _ui.allToolbarPanels.itemById('SolidCreatePanel')
        cmdGearPlanetDef = _ui.commandDefinitions.addButtonDefinition('smgCmdGearGenPlanet', 'Planetary Gears', 'Creates a planetary gear component', '')
        cmdGearTwoShafts = _ui.commandDefinitions.addButtonDefinition('smgCmdGearGen2Shaft', 'Gear Pair on 2 axises', 'Creates a pair of gears between two axises', '')

        dropGear = createPanel.controls.addDropDown('Gear Generator','resources/GearGen', 'smgDropGearGen') #Resources/GearGen
            
        planetGearButton = dropGear.controls.addCommand(cmdGearPlanetDef)
        twoAxisGearButton = dropGear.controls.addCommand(cmdGearTwoShafts)
                
        
        if context['IsApplicationStartup'] == False:
            _ui.messageBox('The "Gear Generator" commands have been added\nto the CREATE panel of the MODEL workspace.')
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def stop(context):
    try:
        createPanel = _ui.allToolbarPanels.itemById('SolidCreatePanel')
            
        dropCtl = createPanel.controls.itemById('smgDropGearGen')
        if dropCtl:
            planetGearButton = dropCtl.controls.itemById('smgCmdGearGenPlanet')       
            if planetGearButton:
                planetGearButton.deleteMe()
                print("planetGearButton deleted")
                
            twoAxisGearButton = dropCtl.controls.itemById('smgCmdGearGen2Shaft')       
            if twoAxisGearButton:
                twoAxisGearButton.deleteMe()
                print("twoAxisGearButton deleted")
                
            dropCtl.deleteMe()
            print("smgDropGearGen DropControl deleted")
        
        cmdDef = _ui.commandDefinitions.itemById('smgCmdGearGenPlanet')
        if cmdDef:
            cmdDef.deleteMe()
            print("smgCmdGearGenPlanet cmddef deleted")            
        
        cmdDef = _ui.commandDefinitions.itemById('smgCmdGearGen2Shaft')
        if cmdDef:
            cmdDef.deleteMe()
            print("smgCmdGearGen2Shaft cmddef deleted")            

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

marshaltu
Autodesk
Autodesk

Hello,

 

Please refer to the following piece of codes how to create a drop-down(list) command definition. They are from our sample "AddInSample" in "Scripts and Add-In" dialog and I did some modification(marked as red).

 

Thanks,

Marshal

 

 

        # add a command button on Quick Access Toolbar
        toolbars_ = ui.toolbars
        toolbarQAT_ = toolbars_.itemById('QAT')
        toolbarControlsQAT_ = toolbarQAT_.controls
        toolbarControlQAT_ = toolbarControlsQAT_.itemById(commandIdOnQAT)
        if not toolbarControlQAT_:
            commandDefinitionQAT_ = commandDefinitions_.itemById(commandIdOnQAT)
            if not commandDefinitionQAT_:
                commandDefinitionQAT_ = commandDefinitions_.addListDefinition(commandIdOnQAT, commandName, adsk.core.ListControlDisplayTypes.CheckBoxListType)
                listCtlDef = adsk.core.ListControlDefinition.cast(commandDefinitionQAT_.controlDefinition)
                listCtlDef.listItems.add('MyItem1', True)
                listCtlDef.listItems.add('MyItem2', False)
                listCtlDef.listItems.add('MyItem3', False)
            onCommandCreated = CommandCreatedEventHandlerQAT()
            commandDefinitionQAT_.commandCreated.add(onCommandCreated)
            # keep the handler referenced beyond this function
            handlers.append(onCommandCreated)
            toolbarControlQAT_ = toolbarControlsQAT_.addCommand(commandDefinitionQAT_)
            toolbarControlQAT_.isVisible = True
            ui.messageBox(_('A demo command button is successfully added to the Quick Access Toolbar'))

 



Marshal Tu
Fusion Developer
>
0 Likes

sgraves
Enthusiast
Enthusiast

Marshall,

Thank you for trying to document the ListDefinition object.  I can't get the code to work.  I do see a little flash on the QAT, but no menu shows up.  I now see that there is a different object called ListControlDefinition.  It seems like the ButtonDefinition and the ListDefinition do not follow the same pattern.  Fortunately, that doesn't seem to happen very often in the API.  It does seem that once one gets the API pattern i.e collections, input objects, etc. the concept can be applied in multiple areas.

 

In any case, I have my solution.  At this point I don't see any value in the ListDefintion, so I am not planning on spending time figuring out what is going wrong with the code.  I don't plan on marking this as a solution because it would be misleading to the users that follow.

 

And actually, the problem was really operator error.  I didn't have the proper debug environment.

0 Likes

marshaltu
Autodesk
Autodesk

Hello,

 

I would like to attach whole sample in the thread so that developers can refer to in case they are interested at using CommandDefinitions.addListDefinition and ListControlDefinition.  

 

Thanks,

Marshal

 

-------------------------

CommandDefinitions.addListDefinition Method

Parent Object: CommandDefinitions

Description

Creates a new command definition that can be used to create a list of check boxes, radio buttons, or text with an icon within a pop-up.

When the list is of check boxes any combinations of items in the list can be checked. The drop-down also remains displayed allowing the user to check and uncheck multiple items however a CommandCreated event is fired for every change.

When the list is of radio buttons or a list of text items, only one item in the list can be selected at a time. When an item is selected the drop-down is immediately dismissed.

The items in the list and their initial state are defined using functionality on the associated ListControlDefinition, which is accessible through the returned CommandDefinition.

 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-558e25b3-933f-4721-a674-711acd91e331



Marshal Tu
Fusion Developer
>
0 Likes

sgraves
Enthusiast
Enthusiast

Marshall,

Thank you, very good idea.

 

I have a quandary, the title of this post refers to the addDropDown function which doesn't appear to use the ListControlDefinition.  For me it is the way to go for adding drop down menus to toolbars.  It is simpler and it definitely works.  But the ListControlDefinition appears to have a place where it needs to be used.  Your post needs to get a little more exposure.  But if I accept your post as the solution, it sends the message that the ListControlDefinition is the way to go when adding a dropdown to a toolbar.

 

Should I make another post with a different title that you can put your post on as the solution?

0 Likes

marshaltu
Autodesk
Autodesk

Hello,

 

Thank you for the suggestion. Starting a new post is good idea. I would like to update our sample "AddInSample" and make sure developers can find out how to use "ListControlDefinition" from our help document. I will create a task to track that in our backlog.

 

Thanks,

Marshal



Marshal Tu
Fusion Developer
>
0 Likes

sgraves
Enthusiast
Enthusiast

Marshall,

Sorry for the delay.  I made a little trip to the hospital.  I had a stent last year and I thought it was happening again.  Turns out my heart is ok and they think my meds are wrong.  We will see.  In any case, I will make that new post now.

 

Steve

0 Likes