Brian,
Here is the link to the file: https://a360.co/3Ddw9Mf
And here is the script.
I had to re-write it from the original add-in to be just a script.
It was part of a Patrick Rainsberry APPER add-in.
import adsk.core, adsk.fusion, traceback
import math
# Globals
_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)
strResult = ''
_handlers = []
def run(context):
try:
global _app, _ui
_app = adsk.core.Application.get()
_ui = _app.userInterface
cmdDef = _ui.commandDefinitions.itemById('adskSpurGearPythonScript')
if not cmdDef:
# Create a command definition.
cmdDef = _ui.commandDefinitions.addButtonDefinition('adskSpurGearPythonScript', 'Spur Gear', 'Creates a spur gear component', 'Resources/SpurGear')
# Connect to the command created event.
onCommandCreated = SketchCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
_handlers.append(onCommandCreated)
# Execute the 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('Failed:\n{}'.format(traceback.format_exc()))
class SketchCommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandEventArgs.cast(args)
# 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('Failed:\n{}'.format(traceback.format_exc()))
# Event handler for the commandCreated event.
class SketchCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
cmd = eventArgs.command
cmd.isExecutedWhenPreEmpted = False
inputs = cmd.commandInputs
global _inputSelectSketch, activeComp, moldBaseComp, _inputErrMessage, _inputSelectFace, dropdown1Items, des
product = _app.activeProduct
des = adsk.fusion.Design.cast(product)
rootComp = des.rootComponent
moldBase = rootComp.occurrences.asList.item(0)
moldBaseComp = adsk.fusion.Component.cast(moldBase.component)
activeComp = des.activeComponent
# Verify that a Fusion design is active.
if not des:
_ui.messageBox('A Fusion design must be active when invoking this command.')
return()
cmd.okButtonText = ("Project the Sketch") # text in "OK" button
cmd.isExecutedWhenPreEmpted = False
# Create the command dialog
_inputSelectFace = inputs.addSelectionInput('face', 'Select Planar Face', 'Select a Planar Face\nfot the new Sketch Plane')
_inputSelectFace.addSelectionFilter(adsk.core.SelectionCommandInput.PlanarFaces)
_inputSelectFace.setSelectionLimits(1,1)
_inputSelectSketch = inputs.addDropDownCommandInput('sketch', 'Select a Sketch', adsk.core.DropDownStyles.TextListDropDownStyle)
_inputSelectSketch.isVisible = False
dropdown1Items = _inputSelectSketch.listItems
dropdown1Items.add(' ', True, '')
moldBaseSketches = moldBaseComp.sketches
for sketch in moldBaseSketches:
dropdown1Items.add(sketch.name, False, '')
_inputErrMessage = inputs.addTextBoxCommandInput('errMessage', '', '', 2, True)
_inputErrMessage.isFullWidth = True
# Connect to the command related events.
onExecute = SketchCommandExecuteHandler()
cmd.execute.add(onExecute)
_handlers.append(onExecute)
onInputChanged = SketchCommandInputChangedHandler()
cmd.inputChanged.add(onInputChanged)
_handlers.append(onInputChanged)
onValidateInputs = SketchCommandValidateInputsHandler()
cmd.validateInputs.add(onValidateInputs)
_handlers.append(onValidateInputs)
onDestroy = SketchCommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler for the execute event.
class SketchCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandEventArgs.cast(args)
global strResult, activeComp, moldBaseComp
sketches = activeComp.sketches
newSketch = sketches.addWithoutEdges(xyPlane)
newSketch.name = strResult
sketchPoints = moldBaseComp.sketches.itemByName(strResult).sketchPoints
[newSketch.project(p) for p in sketchPoints]
sketchCurves = moldBaseComp.sketches.itemByName(strResult).sketchCurves
[newSketch.project(c) for c in sketchCurves]
strResult = ''
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler for the inputChanged event.
class SketchCommandInputChangedHandler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.InputChangedEventArgs.cast(args)
changedInput = eventArgs.input
inputs :adsk.core.CommandInputs = eventArgs.inputs
global _inputSelectSketch, _inputErrMessage, strResult, xyPlane, clickPoint
if changedInput.id == 'face':
selection_input = inputs.itemById('face')
selection = selection_input.selection(0)
xyPlane = selection.entity
_inputSelectSketch.isVisible = True
elif changedInput.id == 'sketch':
strResult = inputs.itemById('sketch').selectedItem.name
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler for the validateInputs event.
class SketchCommandValidateInputsHandler(adsk.core.ValidateInputsEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.ValidateInputsEventArgs.cast(args)
if strResult != ' ':
return True
else:
return False
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Here is the error message to this script (same message as before, different line number)

Thank you for the help.
This is the last stepping stone for my add-in. (I think)
Brad Bylls