Created
February 24, 2016 00:29
-
-
Save arnaudin/9ea1793b11ca98d4432e to your computer and use it in GitHub Desktop.
Adds a button to toolbar panel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Author- | |
#Description-This is sample addin. | |
import adsk.core, adsk.fusion, traceback | |
commandIdOnPanel = 'demoCommandOnPanel' | |
# global set of event handlers to keep them referenced for the duration of the command | |
handlers = [] | |
def commandDefinitionById(id): | |
app = adsk.core.Application.get() | |
ui = app.userInterface | |
if not id: | |
ui.messageBox('commandDefinition id is not specified') | |
return None | |
commandDefinitions_ = ui.commandDefinitions | |
commandDefinition_ = commandDefinitions_.itemById(id) | |
return commandDefinition_ | |
def commandControlByIdForPanel(id): | |
app = adsk.core.Application.get() | |
ui = app.userInterface | |
if not id: | |
ui.messageBox('commandControl id is not specified') | |
return None | |
workspaces_ = ui.workspaces | |
modelingWorkspace_ = workspaces_.itemById('FusionSolidEnvironment') | |
toolbarPanels_ = modelingWorkspace_.toolbarPanels | |
toolbarPanel_ = toolbarPanels_.item(0) | |
toolbarControls_ = toolbarPanel_.controls | |
toolbarControl_ = toolbarControls_.itemById(id) | |
return toolbarControl_ | |
def destroyObject(uiObj, tobeDeleteObj): | |
if uiObj and tobeDeleteObj: | |
if tobeDeleteObj.isValid: | |
tobeDeleteObj.deleteMe() | |
else: | |
uiObj.messageBox('tobeDeleteObj is not a valid object') | |
def run(context): | |
ui = None | |
try: | |
app = adsk.core.Application.get() | |
ui = app.userInterface | |
commandName = 'Demo Button' | |
commandDescription = 'Demo Command' | |
commandResources = './resources' | |
iconResources = './resources' | |
class CommandExecuteHandler(adsk.core.CommandEventHandler): | |
def __init__(self): | |
super().__init__() | |
def notify(self, args): | |
try: | |
command = args.firingEvent.sender | |
ui.messageBox('command: {} executed successfully'.format(command.parentCommandDefinition.id)) | |
except: | |
if ui: | |
ui.messageBox('command executed failed: {}'.format(traceback.format_exc())) | |
class CommandCreatedEventHandlerPanel(adsk.core.CommandCreatedEventHandler): | |
def __init__(self): | |
super().__init__() | |
def notify(self, args): | |
try: | |
cmd = args.command | |
onExecute = CommandExecuteHandler() | |
cmd.execute.add(onExecute) | |
# keep the handler referenced beyond this function | |
handlers.append(onExecute) | |
commandInputs_ = cmd.commandInputs | |
commandInputs_.addBoolValueInput('boolvalueInput_', 'Bool', True) | |
ui.messageBox('Panel command created successfully') | |
except: | |
if ui: | |
ui.messageBox('Panel command created failed: {}'.format(traceback.format_exc())) | |
commandDefinitions_ = ui.commandDefinitions | |
# add a command on create panel in modeling workspace | |
workspaces_ = ui.workspaces | |
modelingWorkspace_ = workspaces_.itemById('FusionSolidEnvironment') | |
toolbarPanels_ = modelingWorkspace_.toolbarPanels | |
toolbarPanel_ = toolbarPanels_.item(0) # add the new command under the first panel | |
toolbarControlsPanel_ = toolbarPanel_.controls | |
toolbarControlPanel_ = toolbarControlsPanel_.itemById(commandIdOnPanel) | |
if not toolbarControlPanel_: | |
commandDefinitionPanel_ = commandDefinitions_.itemById(commandIdOnPanel) | |
if not commandDefinitionPanel_: | |
commandDefinitionPanel_ = commandDefinitions_.addButtonDefinition(commandIdOnPanel, commandName, commandDescription, commandResources) | |
onCommandCreated = CommandCreatedEventHandlerPanel() | |
commandDefinitionPanel_.commandCreated.add(onCommandCreated) | |
# keep the handler referenced beyond this function | |
handlers.append(onCommandCreated) | |
toolbarControlPanel_ = toolbarControlsPanel_.addCommand(commandDefinitionPanel_) | |
toolbarControlPanel_.isVisible = True | |
ui.messageBox('A demo command is successfully added to the create panel in modeling workspace') | |
except: | |
if ui: | |
ui.messageBox('AddIn Start Failed: {}'.format(traceback.format_exc())) | |
def stop(context): | |
ui = None | |
try: | |
app = adsk.core.Application.get() | |
ui = app.userInterface | |
objArrayPanel = [] | |
commandControlPanel_ = commandControlByIdForPanel(commandIdOnPanel) | |
if commandControlPanel_: | |
objArrayPanel.append(commandControlPanel_) | |
commandDefinitionPanel_ = commandDefinitionById(commandIdOnPanel) | |
if commandDefinitionPanel_: | |
objArrayPanel.append(commandDefinitionPanel_) | |
for obj in objArrayPanel: | |
destroyObject(ui, obj) | |
except: | |
if ui: | |
ui.messageBox('AddIn Stop Failed: {}'.format(traceback.format_exc())) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment