import adsk.core, adsk.fusion, adsk.cam, traceback
tbPanel = None
handlers = []
# //////////////////////////////////////// EventHandlers ///////////////////////////////////////////////////
# Event handler for the commandCreated event.
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler😞
def __init__(self😞
super().__init__()
def notify(self, args😞
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
cmd = eventArgs.command
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('Event Create Handler OK')
# _______________________________ Connect to Events handler _______________________
# Connect to the command destroyed event.
onDestroy = MyCommandDestroyHandler()
cmd.destroy.add(onDestroy)
handlers.append(onDestroy)
# Connect to the Mouse Up event
onMouseMove = MyMouseMoveHandler()
cmd.mouseMove.add(onMouseMove)
handlers.append(onMouseMove)
# Event handler for the execute event.----------------------------------------------------------------
class MyCommandExecuteHandler(adsk.core.CommandEventHandler😞
def __init__(self😞
super().__init__()
def notify(self, args😞
# Code to react to the event.
eventArgs = adsk.core.CommandEventArgs.cast(args)
cmd = eventArgs.command
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('In command execute event handler.')
# Event MouseMoveHandler.----------------------------------------------------------------
class MyMouseMoveHandler(adsk.core.MouseEventHandler😞
def __init__(self😞
super().__init__()
def notify(self, args😞
eventArgs = adsk.core.MouseEventArgs.cast(args)
cmd = eventArgs.firingEvent.sender
inputs = cmd.commandInputs
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('In command Mouse Down event handler.')
#click
msg = 'Click on the Sketch'
sel = ui.selectEntity(msg,'SketchCurves')
if sel is None:
return
clickPoint = sel.point
#sketch
root = adsk.fusion.Component.cast(app.activeProduct.rootComponent)
skt = root.sketches.add(root.xYConstructionPlane)
skt.sketchPoints.add(clickPoint)
# Event handler that reacts to when the command is destroyed. This terminates the script.---------------
class MyCommandDestroyHandler(adsk.core.CommandEventHandler😞
def __init__(self😞
super().__init__()
def notify(self, args😞
try:
adsk.terminate()
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#/////////////////////////////////////////////// My Class ////////////////////////////////////////////////
def run(context😞
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
workSpace = ui.workspaces.itemById('FusionSolidEnvironment')
tbPanels = workSpace.toolbarPanels
# Add new tbPanel
MyPanel = tbPanels.itemById('MyPanel')
if not MyPanel:
MyPanel = tbPanels.add('MyPanel', 'My Panel', 'Select', False)
# Vérif si les commands existe
cmdDefButton = ui.commandDefinitions.itemById('cmdDefButton')
if not cmdDefButton :
#Create a button command definition.
cmdDefButton = ui.commandDefinitions.addButtonDefinition('cmdDefButton', 'MyButton','tooltip')
# Connect to the command created event.
commandCreated = MyCommandCreatedHandler()
cmdDefButton.commandCreated.add(commandCreated)
handlers.append(commandCreated)
# Add the buttons to the bottom of the panel.------------------------------
cntrlButton = MyPanel.controls.addCommand(cmdDefButton)
# Make the button available in the panel.
cntrlButton.isPromotedByDefault = True
cntrlButton.isPromoted = True
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# ---------------------------------------------------------------------------------
def stop(context😞
try:
app = adsk.core.Application.get()
ui = app.userInterface
ui.messageBox('STOP')
# Clean up the UI Remove the CommandDefinitions and buttons.
cmdDefButton = ui.commandDefinitions.itemById('cmdDefButton')
if cmdDefButton :
cmdDefButton.deleteMe()
MyPanel = ui.allToolbarPanels.itemById('MyPanel')
cntrlButton = MyPanel.controls.itemById('cmdDefButton')
if cntrlButton:
cntrlButton.deleteMe()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))