Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I am back butchering code again.
I'm trying to select an occurrence and then whilst in the command to display the name of that occurrence.
Here is a screen shot of what I'd like it to look like and the resulting name should come up with "Cartridge Upper Front:1" if referring to the image.
I'm totally stumped. I need the command to remain open, as I will perform a bunch of calculations in the command, but don't need to execute anything from the command. From what I have read, something needs to happen in the SelectionEventHandler, but I don't know what or how.
The offending code is below.
Any help would be greatly appreciated.
#Author- #Description- import adsk.core, adsk.fusion, traceback app = None ui = None commandId = 'occur' commandName = 'Select occurence - state occurence name' commandDescription = 'Select an occurence, then state name of occurence ' # Global set of event handlers to keep them referenced for the duration of the command handlers = [] class SelectionDestroy(adsk.core.CommandEventHandler): def __init__(self): super().__init__() def notify(self, args): try: # when the command is done, terminate the script # this will release all globals which will remove all event handlers adsk.terminate() except: if ui: ui.messageBox('Selection Destroy Class Failed:\n{}'.format(traceback.format_exc())) class MySelectionEventHandler(adsk.core.SelectionEventHandler): def __init__(self): super().__init__() def notify(self, args): eventArgs = adsk.core.SelectionEventArgs.cast(args) # Check which selection input the event is firing for. activeSelectionInput = eventArgs.firingEvent.activeInput if activeSelectionInput.id == '_partSelection': bodySelection = adsk.fusion.BRepBody.cast(activeSelectionInput.entity) resultName = bodySelection.name else: eventArgs.isSelectable = True class MainProgramHandler(adsk.core.CommandCreatedEventHandler): def __init__(self): super().__init__() def notify(self, args): try: cmd = args.command cmd.isRepeatable = False onDestroy = SelectionDestroy() cmd.destroy.add(onDestroy) # keep the handler referenced beyond this function handlers.append(onDestroy) # Define the inputs. inputs = cmd.commandInputs global commandId selectionInput = inputs.addSelectionInput(commandId + '_partSelection', 'Select', 'Select an occurance') selectionInput.addSelectionFilter('Occurrences') # Create readonly textbox input inputs.addTextBoxCommandInput(commandId + '_partName', 'Occurence Name', 'resultName', 1, True) except: if ui: ui.messageBox('Main Program Handler Class Failed:\n{}'.format(traceback.format_exc())) def run(context): ui = None try: global app app = adsk.core.Application.get() global ui ui = app.userInterface global commandId global commandName global commandDescription design = app.activeProduct if not design: ui.messageBox('No active Fusion design', 'No Design') return # Create command defintion cmdDef = ui.commandDefinitions.itemById(commandId) if not cmdDef: cmdDef = ui.commandDefinitions.addButtonDefinition(commandId, commandName, commandDescription) # Add command created event onCommandCreated = MainProgramHandler() cmdDef.commandCreated.add(onCommandCreated) # Keep the handler referenced beyond this function handlers.append(onCommandCreated) # Execute command cmdDef.execute() # Prevent this module from being terminate when the script returns, because we are waiting for event handlers to fire adsk.autoTerminate(False) except: if ui: ui.messageBox('Run Function Failed:\n{}'.format(traceback.format_exc()))
Solved! Go to Solution.