150 lines
6.2 KiB
Python
150 lines
6.2 KiB
Python
import json
|
|
import adsk.core
|
|
import os
|
|
from ...lib import fusionAddInUtils as futil
|
|
from ... import config
|
|
|
|
app = adsk.core.Application.get()
|
|
ui = app.userInterface
|
|
|
|
# TODO ********************* Change these names *********************
|
|
CMD_ID = f'{config.COMPANY_NAME}_{config.ADDIN_NAME}_palette_send'
|
|
CMD_NAME = 'Send to Palette'
|
|
CMD_Description = 'Send some information to the palette'
|
|
IS_PROMOTED = False
|
|
|
|
# Using "global" variables by referencing values from /config.py
|
|
PALETTE_ID = config.sample_palette_id
|
|
|
|
# TODO *** Define the location where the command button will be created. ***
|
|
# This is done by specifying the workspace, the tab, and the panel, and the
|
|
# command it will be inserted beside. Not providing the command to position it
|
|
# will insert it at the end.
|
|
WORKSPACE_ID = 'FusionSolidEnvironment'
|
|
PANEL_ID = 'SolidScriptsAddinsPanel'
|
|
COMMAND_BESIDE_ID = 'ScriptsManagerCommand'
|
|
|
|
# Resource location for command icons, here we assume a sub folder in this directory named "resources".
|
|
ICON_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resources', '')
|
|
|
|
# Local list of event handlers used to maintain a reference so
|
|
# they are not released and garbage collected.
|
|
local_handlers = []
|
|
|
|
|
|
# Executed when add-in is run.
|
|
def start():
|
|
# Create a command Definition.
|
|
cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)
|
|
|
|
# Add command created handler. The function passed here will be executed when the command is executed.
|
|
futil.add_handler(cmd_def.commandCreated, command_created)
|
|
|
|
# ******** Add a button into the UI so the user can run the command. ********
|
|
# Get the target workspace the button will be created in.
|
|
workspace = ui.workspaces.itemById(WORKSPACE_ID)
|
|
|
|
# Get the panel the button will be created in.
|
|
panel = workspace.toolbarPanels.itemById(PANEL_ID)
|
|
|
|
# Create the button command control in the UI after the specified existing command.
|
|
control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)
|
|
|
|
# Specify if the command is promoted to the main toolbar.
|
|
control.isPromoted = IS_PROMOTED
|
|
|
|
|
|
# Executed when add-in is stopped.
|
|
def stop():
|
|
# Get the various UI elements for this command
|
|
workspace = ui.workspaces.itemById(WORKSPACE_ID)
|
|
panel = workspace.toolbarPanels.itemById(PANEL_ID)
|
|
command_control = panel.controls.itemById(CMD_ID)
|
|
command_definition = ui.commandDefinitions.itemById(CMD_ID)
|
|
|
|
# Delete the button command control
|
|
if command_control:
|
|
command_control.deleteMe()
|
|
|
|
# Delete the command definition
|
|
if command_definition:
|
|
command_definition.deleteMe()
|
|
|
|
|
|
# Event handler that is called when the user clicks the command button in the UI.
|
|
# To have a dialog, you create the desired command inputs here. If you don't need
|
|
# a dialog, don't create any inputs and the execute event will be immediately fired.
|
|
# You also need to connect to any command related events here.
|
|
def command_created(args: adsk.core.CommandCreatedEventArgs):
|
|
# General logging for debug.
|
|
futil.log(f'{CMD_NAME} Command Created Event')
|
|
|
|
# TODO Create the event handlers you will need for this instance of the command
|
|
futil.add_handler(args.command.execute, command_execute, local_handlers=local_handlers)
|
|
futil.add_handler(args.command.inputChanged, command_input_changed, local_handlers=local_handlers)
|
|
futil.add_handler(args.command.executePreview, command_preview, local_handlers=local_handlers)
|
|
futil.add_handler(args.command.destroy, command_destroy, local_handlers=local_handlers)
|
|
|
|
# Create the user interface for your command by adding different inputs to the CommandInputs object
|
|
# https://help.autodesk.com/view/fusion360/ENU/?contextId=CommandInputs
|
|
inputs = args.command.commandInputs
|
|
|
|
# TODO ******************************** Define your UI Here ********************************
|
|
|
|
# Simple text input box
|
|
inputs.addTextBoxCommandInput('text_input', 'Text Message', 'Enter some text', 1, False)
|
|
|
|
# To create a numerical input with units, we need to get the current units and create a "ValueInput"
|
|
# https://help.autodesk.com/view/fusion360/ENU/?contextId=ValueInput
|
|
users_current_units = app.activeProduct.unitsManager.defaultLengthUnits
|
|
default_value = adsk.core.ValueInput.createByString(f'1 {users_current_units}')
|
|
inputs.addValueInput('value_input', 'Value Message', users_current_units, default_value)
|
|
|
|
|
|
# This function will be called when the user hits the OK button in the command dialog
|
|
def command_execute(args: adsk.core.CommandEventArgs):
|
|
# General logging for debug
|
|
futil.log(f'{CMD_NAME} Command Execute Event')
|
|
|
|
inputs = args.command.commandInputs
|
|
|
|
# TODO ******************************** Your code here ********************************
|
|
|
|
# Get a reference to your command's inputs
|
|
text_input: adsk.core.TextBoxCommandInput = inputs.itemById('text_input')
|
|
value_input: adsk.core.ValueCommandInput = inputs.itemById('value_input')
|
|
|
|
# Construct a message
|
|
message_action = 'updateMessage'
|
|
message_data = {
|
|
'myValue': f'{value_input.value} cm',
|
|
'myExpression': value_input.expression,
|
|
'myText': text_input.formattedText
|
|
}
|
|
# JSON strings are a useful way to translate between javascript objects and python dictionaries
|
|
message_json = json.dumps(message_data)
|
|
|
|
# Get a reference to the palette and send the message to the palette javascript
|
|
palette = ui.palettes.itemById(PALETTE_ID)
|
|
palette.sendInfoToHTML(message_action, message_json)
|
|
|
|
|
|
# This function will be called when the command needs to compute a new preview in the graphics window
|
|
def command_preview(args: adsk.core.CommandEventArgs):
|
|
inputs = args.command.commandInputs
|
|
futil.log(f'{CMD_NAME} Command Preview Event')
|
|
|
|
|
|
# This function will be called when the user changes anything in the command dialog
|
|
def command_input_changed(args: adsk.core.InputChangedEventArgs):
|
|
changed_input = args.input
|
|
inputs = args.inputs
|
|
futil.log(f'{CMD_NAME} Input Changed Event fired from a change to {changed_input.id}')
|
|
|
|
|
|
# This event handler is called when the command terminates.
|
|
def command_destroy(args: adsk.core.CommandEventArgs):
|
|
global local_handlers
|
|
local_handlers = []
|
|
futil.log(f'{CMD_NAME} Command Destroy Event')
|