Community
Hello everyone,
I'm pretty new to the Fusion API so bear with me. I've found an add-in on how to create a new panel but I'm not sure how to attach my script as a button to it. I know that there is a Command property but I am lost on how to implement it. You can find the files for the panel add-in and the script I want to execute below.
Thank you,
Zach
#Panel Button Code import adsk.core, adsk.fusion, adsk.cam, traceback tbPanel = None def run(context): ui = None try: app = adsk.core.Application.get() ui = app.userInterface workSpace = ui.workspaces.itemById('FusionSolidEnvironment') tbPanels = workSpace.toolbarPanels global tbPanel tbPanel = tbPanels.itemById('NewPanel') if tbPanel: tbPanel.deleteMe() tbPanel = tbPanels.add('NewPanel', 'New Panel', 'SelectPanel', False) # Empty panel can't be displayed. Add a command to the panel cmdDef = ui.commandDefinitions.itemById('NewCommand') if cmdDef: cmdDef.deleteMe() cmdDef = ui.commandDefinitions.addButtonDefinition('NewCommand', 'Dimensions', 'Demo for new command') tbPanel.controls.addCommand(cmdDef) ui.messageBox('Hello addin') except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) def stop(context): ui = None try: app = adsk.core.Application.get() ui = app.userInterface if tbPanel: tbPanel.deleteMe() ui.messageBox('Stop addin') except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#Function I want to implement into button import adsk.core, adsk.fusion, adsk.cam, traceback def run(context): ui = None try: app = adsk.core.Application.get() ui = app.userInterface design = app.activeProduct if not design: ui.messageBox('No active Fusion design', 'No Design') return # Get the root component of the active design. rootComp = design.rootComponent #box = rootComp.bRepBody.boundingBox # Iterate over any bodies in the root component. totalVolume = 0 for j in range(0, rootComp.bRepBodies.count): body = rootComp.bRepBodies.item(j) # Get the volume of the current body and add it to the total. totalVolume += body.volume length = body.boundingBox.maxPoint.x - body.boundingBox.minPoint.x width = body.boundingBox.maxPoint.y - body.boundingBox.minPoint.y height = body.boundingBox.maxPoint.z - body.boundingBox.minPoint.z # Iterate through all of the occurrences in the assembly. for i in range(0, rootComp.allOccurrences.count): occ = rootComp.allOccurrences.item(i) # Get the associated component. comp = occ.component # Iterate over all of the bodies within the component. for j in range(0, comp.bRepBodies.count): body = comp.bRepBodies.item(j) # Get the volume of the current body and add it to the total. totalVolume += body.volume length = body.boundingBox.maxPoint.x - body.boundingBox.minPoint.x width = body.boundingBox.maxPoint.y - body.boundingBox.minPoint.y height = body.boundingBox.maxPoint.z - body.boundingBox.minPoint.z # Format a string to display the volume using the default distance units. resultLength = design.unitsManager.formatInternalValue(length, design.unitsManager.defaultLengthUnits, True) resultWidth = design.unitsManager.formatInternalValue(width, design.unitsManager.defaultLengthUnits, True) resultHeight = design.unitsManager.formatInternalValue(height, design.unitsManager.defaultLengthUnits, True) ui.messageBox('The length of the part is: ' + resultLength + "\n" + "The width of the part is: " + resultWidth + "\n" + "The height of the part is: " + resultHeight) except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Hello,
I merged your codes into one addin as below.
Thanks,
Marshal
import adsk.core, adsk.fusion, adsk.cam, traceback ui = None tbPanel = None handlers = [] class CommandExecuteHandler(adsk.core.CommandEventHandler): def __init__(self): super().__init__() def notify(self, args): try: app = adsk.core.Application.get() ui = app.userInterface design = app.activeProduct if not design: ui.messageBox('No active Fusion design', 'No Design') return # Get the root component of the active design. rootComp = design.rootComponent #box = rootComp.bRepBody.boundingBox # Iterate over any bodies in the root component. totalVolume = 0 for j in range(0, rootComp.bRepBodies.count): body = rootComp.bRepBodies.item(j) # Get the volume of the current body and add it to the total. totalVolume += body.volume length = body.boundingBox.maxPoint.x - body.boundingBox.minPoint.x width = body.boundingBox.maxPoint.y - body.boundingBox.minPoint.y height = body.boundingBox.maxPoint.z - body.boundingBox.minPoint.z # Iterate through all of the occurrences in the assembly. for i in range(0, rootComp.allOccurrences.count): occ = rootComp.allOccurrences.item(i) # Get the associated component. comp = occ.component # Iterate over all of the bodies within the component. for j in range(0, comp.bRepBodies.count): body = comp.bRepBodies.item(j) # Get the volume of the current body and add it to the total. totalVolume += body.volume length = body.boundingBox.maxPoint.x - body.boundingBox.minPoint.x width = body.boundingBox.maxPoint.y - body.boundingBox.minPoint.y height = body.boundingBox.maxPoint.z - body.boundingBox.minPoint.z # Format a string to display the volume using the default distance units. resultLength = design.unitsManager.formatInternalValue(length, design.unitsManager.defaultLengthUnits, True) resultWidth = design.unitsManager.formatInternalValue(width, design.unitsManager.defaultLengthUnits, True) resultHeight = design.unitsManager.formatInternalValue(height, design.unitsManager.defaultLengthUnits, True) ui.messageBox('The length of the part is: ' + resultLength + "\n" + "The width of the part is: " + resultWidth + "\n" + "The height of the part is: " + resultHeight) except: if ui: ui.messageBox(('command executed failed: {}').format(traceback.format_exc())) class CommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler): def __init__(self): super().__init__() def notify(self, args): try: cmd = args.command onExecute = CommandExecuteHandler() cmd.execute.add(onExecute) handlers.append(onExecute) except: if ui: ui.messageBox(('Panel command created failed: {}').format(traceback.format_exc())) def run(context): try: app = adsk.core.Application.get() global ui ui = app.userInterface workSpace = ui.workspaces.itemById('FusionSolidEnvironment') tbPanels = workSpace.toolbarPanels global tbPanel tbPanel = tbPanels.itemById('NewPanel') if tbPanel: tbPanel.deleteMe() tbPanel = tbPanels.add('NewPanel', 'New Panel', 'SelectPanel', False) # Empty panel can't be displayed. Add a command to the panel cmdDef = ui.commandDefinitions.itemById('NewCommand') if cmdDef: cmdDef.deleteMe() cmdDef = ui.commandDefinitions.addButtonDefinition('NewCommand', 'Dimensions', 'Demo for new command') tbPanel.controls.addCommand(cmdDef) onCreated = CommandCreatedEventHandler() cmdDef.commandCreated.add(onCreated) handlers.append(onCreated) ui.messageBox('Hello addin') except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) def stop(context): try: if tbPanel: tbPanel.deleteMe() ui.messageBox('Stop addin') except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Can't find what you're looking for? Ask the community or share your knowledge.