Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Drop down in panel drop down

3 REPLIES 3
Reply
Message 1 of 4
brad.bylls
631 Views, 3 Replies

Drop down in panel drop down

I've added a drop down item to the Addins drop down list.

What I can't figure out is how to get them to work when I click on them.

I just want to click on them which will then run another script.

 

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

def run(context😞
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
            
        addins = adsk.core.ToolbarPanel.cast(ui.allToolbarPanels.itemById("SolidScriptsAddinsPanel"))
        dropDown = addins.controls.addDropDown("DropDown""./Resources")
        cmd0 = ui.commandDefinitions.addButtonDefinition("cmd0""SHCS""Create a SHCS""./Resources")
        cmd1 = ui.commandDefinitions.addButtonDefinition("cmd1""FHSCS""Create a FHSCS""./Resources")
        dropDown.controls.addCommand(cmd0)
        dropDown.controls.addCommand(cmd1)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Brad Bylls
3 REPLIES 3
Message 2 of 4
JeromeBriot
in reply to: brad.bylls

Hello,

 

You should carefully read this part of the API documentation: Fusion 360 Commands

 

Message 3 of 4
brad.bylls
in reply to: JeromeBriot

After looking at the documentation link, this is what I came up with. Doesn't work.

I'm hoping it is just the execFile('SHCS') part that is not working.

Can't find how to run another script from a script.

 

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

handlers = []

def run(context😞
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
            
        addins = adsk.core.ToolbarPanel.cast(ui.allToolbarPanels.itemById("SolidScriptsAddinsPanel"))
        dropDown = addins.controls.addDropDown("DropDown""./Resources")
        shcsButton = ui.commandDefinitions.addButtonDefinition("shcs""SHCS""Create a SHCS""./Resources")
        fhscsButton = ui.commandDefinitions.addButtonDefinition("fhscs""FHSCS""Create a FHSCS""./Resources")
        
        # Connect to the command created event.
        shcsCreated = ShcsCreatedEventHandler()
        shcsButton.commandCreated.add(shcsCreated)
        handlers.append(shcsCreated)
        
        fhscsCreated = FhscsCreatedEventHandler()
        fhscsButton.commandCreated.add(fhscsCreated)
        handlers.append(fhscsCreated)
        
        # Get the ADD-INS panel in the model workspace. 
        addInsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        
        # Add the button to the bottom of the panel.
        dropDown.controls.addCommand(shcsButton)
        dropDown.controls.addCommand(fhscsButton)

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

# Event handler for the shcsCreated event.
class ShcsCreatedEventHandler(adsk.core.CommandCreatedEventHandler😞
    def __init__(self😞
        super().__init__()
    def notify(selfargs😞
        eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
        cmd = eventArgs.command

        # Connect to the execute event.
        onExecute = ShcsExecuteHandler()
        cmd.execute.add(onExecute)
        handlers.append(onExecute)


# Event handler for the shcs execute event.
class ShcsExecuteHandler(adsk.core.CommandEventHandler😞
    def __init__(self😞
        super().__init__()
    def notify(selfargs😞
        eventArgs = adsk.core.CommandEventArgs.cast(args)

        # Code to react to the event.
        app = adsk.core.Application.get()
        ui  = app.userInterface
        execFile('SHCS.py')

# Event handler for the fhscsCreated event.
class FhscsCreatedEventHandler(adsk.core.CommandCreatedEventHandler😞
    def __init__(self😞
        super().__init__()
    def notify(selfargs😞
        eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
        cmd = eventArgs.command

        # Connect to the execute event.
        onExecute = FhscsExecuteHandler()
        cmd.execute.add(onExecute)
        handlers.append(onExecute)

# Event handler for the fhscs execute event.
class FhscsExecuteHandler(adsk.core.CommandEventHandler😞
    def __init__(self😞
        super().__init__()
    def notify(selfargs😞
        eventArgs = adsk.core.CommandEventArgs.cast(args)

        # Code to react to the event.
        app = adsk.core.Application.get()
        ui  = app.userInterface
        execFile('FHSCS.py')

def stop(context😞
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        # Clean up the UI.
        cmdDef0 = ui.commandDefinitions.itemById('shcs')
        if cmdDef0:
            cmdDef0.deleteMe()
            
        addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        cntrl0 = addinsPanel.controls.itemById('shcs')
        if cntrl0:
            cntrl0.deleteMe()

        cmdDef1 = ui.commandDefinitions.itemById('fhscs')
        if cmdDef1:
            cmdDef1.deleteMe()
            
        addinsPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel')
        cntrl1 = addinsPanel.controls.itemById('fhscs')
        if cntrl1:
            cntrl1.deleteMe()

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) 
Brad Bylls
Message 4 of 4
JeromeBriot
in reply to: brad.bylls

Hello,

 

To test the menus, replace the execFile commands with something like ui.messageBox('Test')

 


@brad.bylls wrote:

Can't find how to run another script from a script.

Maybe:

subprocess.run([sys.executable, 'FHSCS'])

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report