Hi @ebunn3 ,
I have made a short sample which should work:
import adsk.core, adsk.fusion, adsk.cam, traceback
import random
class addBody_commandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
#Change to direct design type
# Note: If using parametric design type, base feature should be added
# base = rootComponent.baseFeatures.add()
# base.startEdit() ----> base.finishEdit()
design = adsk.fusion.Design.cast(_app.activeProduct)
design.designType = adsk.fusion.DesignTypes.DirectDesignType
command = adsk.core.CommandCreatedEventArgs.cast(args).command
inputs = command.commandInputs
#Add a button to add body
inputs.addBoolValueInput('addBody_button', 'Add', False, '', False)
#Connect to events
addBody_inputChanged = addBody_inputChangedHandler()
command.inputChanged.add(addBody_inputChanged)
_handlers.append(addBody_inputChanged)
addBody_executePreview = addBody_executePreviewHandler()
command.executePreview.add(addBody_executePreview)
_handlers.append(addBody_executePreview)
addBody_execute = addBody_executeHandler()
command.execute.add(addBody_execute)
_handlers.append(addBody_execute)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class addBody_inputChangedHandler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
global _temp_body, _temp_body_origin
ipt = adsk.core.InputChangedEventArgs.cast(args).input
if ipt.id == 'addBody_button':
#Decide origin for new body
while True:
#Get a random point
new_coor = [random.randint(-10, 10), random.randint(-10, 10), random.randint(-10, 10)]
if not new_coor in _temp_body_origin:
#If not exist, create a point
new_origin = adsk.core.Point3D.create(new_coor[0], new_coor[1], new_coor[2])
_temp_body_origin
break
#Create a 0.5-side cube
length_dir = adsk.core.Vector3D.create(1, 0, 0)
width_dir = adsk.core.Vector3D.create(0, 1, 0)
length = 0.5
width = 0.5
height = 0.5
#Create oriented bouding box
obb = adsk.core.OrientedBoundingBox3D.create(new_origin, length_dir, width_dir, length, width, height)
#Add to new temporary bRepBody
tempBR_manager = adsk.fusion.TemporaryBRepManager.get()
temp_body = tempBR_manager.createBox(obb)
_temp_body.append(temp_body)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class addBody_executePreviewHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
global _temp_body
root_comp = adsk.fusion.Design.cast(_app.activeProduct).rootComponent
cgg = root_comp.customGraphicsGroups.add()
for temp_body in _temp_body:
cg_body = cgg.addBRepBody(temp_body)
#cg_body.color = //color #Add color if desired
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class addBody_executeHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
global _temp_body
#Add bodies under rootComponent
root_comp = adsk.fusion.Design.cast(_app.activeProduct).rootComponent
for temp_body in _temp_body:
root_comp.bRepBodies.add(temp_body)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
_app = adsk.core.Application.get()
_ui = _app.userInterface
_handlers = []
_temp_body = []; _temp_body_origin = []
def run(context):
try:
#Get command definitions & design workspace
cmd_def = _ui.commandDefinitions
des_wp = _ui.workspaces.itemById('FusionSolidEnvironment')
#Get tools tab
tools_tab = des_wp.toolbarTabs.itemById('ToolsTab')
#Get add-in panel
addin_panel = tools_tab.toolbarPanels.itemById('SolidScriptsAddinsPanel')
#Add command definition and control
addBody_cmdDef = cmd_def.itemById('AddBody_cmd')
if not addBody_cmdDef:
addBody_cmdDef = cmd_def.addButtonDefinition('AddBody_cmd', 'Add body', 'Adds body(bodies) to root component', './resources/addBody')
addBody_ctrl = addin_panel.controls.itemById('AddBody_cmd')
if not addBody_ctrl:
addBody_ctrl = addin_panel.controls.addCommand(addBody_cmdDef)
#Set visibility of control
addBody_ctrl.isVisible = True; addBody_ctrl.isPromoted = True; addBody_ctrl.isPromotedByDefault = True
#Connect to command created event
addBody_commandCreated = addBody_commandCreatedEventHandler()
addBody_cmdDef.commandCreated.add(addBody_commandCreated)
_handlers.append(addBody_commandCreated)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
You will probably need to modify it to suit your case better. You need to put your own icon in the './resources/addBody' folder.
Unfortunately, although I personally do not like using global variables, it seems like using global variables is a MUST to pass the data around. In this code, most of the work is done in inputChangedHandler, where a new temporary body is created. The executePreviewHandler shows the temporary body. The preview handler do not have to 'respond' to something (button click), instead, it constantly refreshes itself (it always listens to everything). Finally, the executeHandler adds the body.