
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Apologies if this is a very basic question but I've read lots of forum posts and the API manual and still can't figure this out.
I'm trying to get my script to refresh/update each time the user makes a different selection from a drop-down list. I understand that I need to use the InputChangedEventHandler for this but I'm really struggling with logic and implementation.
Here is a very basic script that I have written to try and get to grips with this functionality. If I can get this example working then I'm sure I can implement this functionality in my actual script.
#Test to understand the InputChanged functionality
import adsk.core, adsk.fusion, adsk.cam, traceback, os #importing modules which contain functions that will need to be used later
_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)
# Global set of event handlers to keep them referenced for the duration of the command
_handlers = []
# Event handler for the inputChanged event.
class MyInputChangedHandler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
eventArgs = adsk.core.InputChangedEventArgs.cast(args)
userselecteditem = dropdownInput3.selectedItem.index
changedInput = eventArgs.input
# Event handler for the execute event.
class MyExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.InputChangedEventArgs.cast(args)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler for the destroy event.
class MyDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
adsk.terminate()
# Event handler for the commandCreated event.
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
#--------------------CREATE TABS------------------
inputs = adsk.core.CommandInputs.cast(eventArgs.command.commandInputs)
# Create a tab input.
tabCmdInput1 = inputs.addTabCommandInput('tab_1', 'Selection')
tab1ChildInputs = tabCmdInput1.children
# multiple tabs to be created in the future.
#--------------------CREATE TABLE WITH USER INSTRUCTIONS------------------
table = tab1ChildInputs.addTableCommandInput('instructionstable', 'Inputs', 1, '1')
table.minimumVisibleRows = 1
table.maximumVisibleRows = 3
table.columnSpacing = 1
table.rowSpacing = 0
table.tablePresentationStyle = adsk.core.TablePresentationStyles.itemBorderTablePresentationStyle
table.hasGrid = False
textboxInput = inputs.addTextBoxCommandInput('readonly_textBox', 'Text Box 1', '<div align="center">Make your selection below </div>', 2, True)
textboxInput.isReadOnly = True
textboxInput.numRows = 3
table.addCommandInput(textboxInput, 0, 0, False, False)
#--------------------------------------
#--------------------USER MAKES SELECTION------------------
dropdownInput3 = tab1ChildInputs.addDropDownCommandInput('dropdown3', ' What is your selection? ', adsk.core.DropDownStyles.LabeledIconDropDownStyle);
dropdown3Items = dropdownInput3.listItems
dropdown3Items.add('1. Item 1', True, '')
dropdown3Items.add('2. Item 2', False, '')
dropdown3Items.add('3. Item 3', False, '')
dropdown3Items.add('4. Item 4', False, '')
userselecteditem = dropdownInput3.selectedItem.index
#--------------------------------------
#--------------------CREATE TABLE WSHOWING SELECTION------------------
table = tab1ChildInputs.addTableCommandInput('selectiontable', 'Inputs', 1, '1')
table.minimumVisibleRows = 3
table.maximumVisibleRows = 3
table.columnSpacing = 1
table.rowSpacing = 10
table.tablePresentationStyle = adsk.core.TablePresentationStyles.itemBorderTablePresentationStyle
table.hasGrid = False
textboxInput = inputs.addTextBoxCommandInput('readonly_textBox', 'Text Box 1', 'your selection is '+str(userselecteditem), 2, True)
textboxInput.isReadOnly = True
textboxInput.numRows = 3
table.addCommandInput(textboxInput, 0, 0, False, False)
#-----------Subscribe to the various command events-------------
# Connect to command execute.
onExecute = MyExecuteHandler()
eventArgs.command.execute.add(onExecute)
_handlers.append(onExecute)
# Connect to input changed.
onInputChanged = MyInputChangedHandler()
eventArgs.command.inputChanged.add(onInputChanged)
_handlers.append(onInputChanged)
# Connect to the command terminate.
onDestroy = MyDestroyHandler()
eventArgs.command.destroy.add(onDestroy)
_handlers.append(onDestroy)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context):
try:
global _app, _ui
_app = adsk.core.Application.get()
_ui = _app.userInterface
# Create a command.
cmd = _ui.commandDefinitions.itemById('tableTest')
if cmd:
cmd.deleteMe()
cmd = _ui.commandDefinitions.addButtonDefinition('tableTest', 'User selection test', 'Table Test', '')
# Connect to the command create event.
onCommandCreated = MyCommandCreatedHandler()
cmd.commandCreated.add(onCommandCreated)
_handlers.append(onCommandCreated)
# Execute the command.
cmd.execute()
# Set this so the script continues to run.
adsk.autoTerminate(False)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
The script should just tell the user what selection they have made from the drop-down list. Apologies for tagging members but @ekinsb I'm sure this would be very easy for you to solve.
Thanks in advance.
Solved! Go to Solution.