Skip to content

Commit

Permalink
restructure addIn class
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianPommerening committed Aug 7, 2021
1 parent 5e22441 commit 1370c5a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 62 deletions.
29 changes: 13 additions & 16 deletions FingerJoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def createCutFeature(parentComponent, targetBody, toolBodyFeature):
return cutFeature


class FingerJointCommand(commands.RunningCommandBase):
class CreateFingerJointCommand(commands.RunningCommandBase):
def __init__(self, args: adsk.core.CommandCreatedEventArgs):
super(FingerJointCommand, self).__init__(args)
super(CreateFingerJointCommand, self).__init__(args)
self.options = options.FingerJointOptions()
self.ui = ui.FingerJointUI(args.command.commandInputs, self.options)

Expand Down Expand Up @@ -95,22 +95,19 @@ def createCustomFeature(self, body0, body1, toolBody0, toolBody1):
createCutFeature(activeComponent, body1, tool1Feature)

def onDestroy(self, args: adsk.core.CommandEventArgs):
super(FingerJointCommand, self).onDestroy(args)
super(CreateFingerJointCommand, self).onDestroy(args)
if args.terminationReason == adsk.core.CommandTerminationReason.CompletedTerminationReason:
self.options.writeDefaults()


class FingerJointAddIn(object):
def __init__(self):
self.button = commands.CommandButton('FingerJointBtn', 'SolidModifyPanel', FingerJointCommand)

def start(self):
self.button.addToUI('Finger Joint',
'Creates a finger joint from the overlap of two bodies',
'resources/ui/command_button')

def stop(self):
self.button.removeFromUI()
class FingerJointAddIn(commands.AddIn):
COMMAND_ID = 'fpFingerJoints'
FEATURE_NAME = 'Finger Joint'
RESOURCE_FOLDER = 'resources/ui/command_button'
CREATE_TOOLTIP='Creates a finger joint from the overlap of two bodies'
EDIT_TOOLTIP='Edit finger joint'
PANEL_NAME='SolidModifyPanel'
RUNNING_CREATE_COMMAND_CLASS = CreateFingerJointCommand


def run(context):
Expand All @@ -119,12 +116,12 @@ def run(context):
if addIn is not None:
stop({'IsApplicationClosing': False})
addIn = FingerJointAddIn()
addIn.start()
addIn.addToUI()
except:
ui.reportError('Uncaught exception', True)


def stop(context):
global addIn
addIn.stop()
addIn.removeFromUI()
addIn = None
95 changes: 51 additions & 44 deletions commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
# callback handlers are not GC'd.
running_commands = set()

def makeForwardingHandler(handler_cls, callback):
class ForwardingHandler(handler_cls):
def __init__(self, callback):
super().__init__()
self.callback = callback

def notify(self, args):
try:
self.callback(args)
except:
ui.reportError('Callback method failed', True)
return ForwardingHandler(callback)


class RunningCommandBase(object):
"""
Base class to keep persistent data during the lifetime of a command from
Expand All @@ -18,6 +32,7 @@ class RunningCommandBase(object):
def __init__(self, args):
running_commands.add(self)

def onCreate(self, args):
cmd = adsk.core.Command.cast(args.command)

self._inputChangedHandler = makeForwardingHandler(
Expand Down Expand Up @@ -66,66 +81,58 @@ def onDestroy(self, args):
running_commands.remove(self)


class CommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self, runningCommandClass):
super().__init__()
self.runningCommandClass = runningCommandClass

def notify(self, args):
try:
running_command = self.runningCommandClass(args)
running_command.onCreated(args)
except:
ui.reportError('Command creation callback method failed', True)

class AddIn(object):
# Defaults that are None have to be overridden in derived classes.
COMMAND_ID = None
FEATURE_NAME = None
RESOURCE_FOLDER = None
CREATE_TOOLTIP=''
EDIT_TOOLTIP=''
PANEL_NAME=None
RUNNING_CREATE_COMMAND_CLASS = None

def makeForwardingHandler(handler_cls, callback):
class ForwardingHandler(handler_cls):
def __init__(self, callback):
super().__init__()
self.callback = callback
def __init__(self):
fusion = adsk.core.Application.get()
self.fusionUI = fusion.userInterface

def notify(self, args):
try:
self.callback(args)
except:
ui.reportError('Callback method failed', True)
return ForwardingHandler(callback)
# Add handler for creating the feature.
self._createHandler = makeForwardingHandler(
adsk.core.CommandCreatedEventHandler, self._onCreate)

def _onCreate(self, args):
running_command = self.RUNNING_CREATE_COMMAND_CLASS(args)
running_command.onCreate(args)

class CommandButton(object):
def __init__(self, commandID, panelName, commandDataClass):
self.commandID = commandID
self.panelName = panelName
def _getCreateButtonID(self):
return self.COMMAND_ID + 'Create'

fusion = adsk.core.Application.get()
self.fusionUI = fusion.userInterface
self.creationHandler = CommandCreatedHandler(commandDataClass)
def _getCreateButtonName(self):
return self.FEATURE_NAME

def addToUI(self, name, tooltip='', resourceFolder=''):
def addToUI(self):
# If there are existing instances of the button, clean them up first.
try:
self.removeFromUI()
except:
pass

# Create a command definition and button for it.
commandDefinition = self.fusionUI.commandDefinitions.addButtonDefinition(
self.commandID, name, tooltip, resourceFolder)
commandDefinition.commandCreated.add(self.creationHandler)
# Create a command for creating the feature.
createCommandDefinition = self.fusionUI.commandDefinitions.addButtonDefinition(
self._getCreateButtonID(), self._getCreateButtonName(), self.CREATE_TOOLTIP, self.RESOURCE_FOLDER)
createCommandDefinition.commandCreated.add(self._createHandler)

panel = self.fusionUI.allToolbarPanels.itemById(self.panelName)
buttonControl = panel.controls.addCommand(commandDefinition)

# Make the button available in the panel.
# Add a button to the UI.
panel = self.fusionUI.allToolbarPanels.itemById(self.PANEL_NAME)
buttonControl = panel.controls.addCommand(createCommandDefinition)
buttonControl.isPromotedByDefault = True
buttonControl.isPromoted = True

def removeFromUI(self):
commandDefinition = self.fusionUI.commandDefinitions.itemById(self.commandID)
if commandDefinition:
commandDefinition.deleteMe()
panel = self.fusionUI.allToolbarPanels.itemById(self.panelName)
buttonControl = panel.controls.itemById(self.commandID)
createCommandDefinition = self.fusionUI.commandDefinitions.itemById(self._getCreateButtonID())
if createCommandDefinition:
createCommandDefinition.deleteMe()

panel = self.fusionUI.allToolbarPanels.itemById(self.PANEL_NAME)
buttonControl = panel.controls.itemById(self._getCreateButtonID())
if buttonControl:
buttonControl.deleteMe()
4 changes: 2 additions & 2 deletions ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def setInputErrorMessage(self, msg):
# value is x afterwards (e.g., '' is turned into '<br />').
if msg:
formattedText = '<p style="color:red">{}</p>'.format(msg)
if self._inputErrorMessage.formattedText != formattedText:
self._inputErrorMessage.formattedText = formattedText
if self._inputErrorMessage.formattedText != formattedText:
self._inputErrorMessage.formattedText = formattedText
else:
if self._inputErrorMessage.text != '':
self._inputErrorMessage.text = ''
Expand Down

0 comments on commit 1370c5a

Please sign in to comment.