-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcommands.py
138 lines (105 loc) · 4.46 KB
/
commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import adsk.core
import adsk.fusion
from . import ui
# Keep track of all currently running commands in a global set, so their
# 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
creation to destruction. The constructor of this class automatically adds
the instance to running_commands and the onDestroy event removes it again.
To use this class, inherit from it an override the events.
"""
def __init__(self, args):
running_commands.add(self)
def onCreate(self, args):
cmd = adsk.core.Command.cast(args.command)
self._inputChangedHandler = makeForwardingHandler(
adsk.core.InputChangedEventHandler, self.onInputChanged)
cmd.inputChanged.add(self._inputChangedHandler)
self._selectionHandler = makeForwardingHandler(
adsk.core.SelectionEventHandler, self.onSelectionEvent)
cmd.selectionEvent.add(self._selectionHandler)
self._validateHandler = makeForwardingHandler(
adsk.core.ValidateInputsEventHandler, self.onValidate)
cmd.validateInputs.add(self._validateHandler)
self._executeHandler = makeForwardingHandler(
adsk.core.CommandEventHandler, self.onExecute)
cmd.execute.add(self._executeHandler)
self._executePreviewHandler = makeForwardingHandler(
adsk.core.CommandEventHandler, self.onExecutePreview)
cmd.executePreview.add(self._executePreviewHandler)
self._destroyHandler = makeForwardingHandler(
adsk.core.CommandEventHandler, self.onDestroy)
cmd.destroy.add(self._destroyHandler)
def onCreated(self, args):
pass
def onInputChanged(self, args):
pass
def onSelectionEvent(self, args):
pass
def onValidate(self, args):
pass
def onExecute(self, args):
pass
def onExecutePreview(self, args):
pass
def onDestroy(self, args):
running_commands.remove(self)
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 __init__(self):
fusion = adsk.core.Application.get()
self.fusionUI = fusion.userInterface
# 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)
def _getCreateButtonID(self):
return self.COMMAND_ID + 'Create'
def _getCreateButtonName(self):
return self.FEATURE_NAME
def addToUI(self):
# If there are existing instances of the button, clean them up first.
try:
self.removeFromUI()
except:
pass
# 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)
# 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):
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()