-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAddInTemplateCommand.py
More file actions
178 lines (127 loc) · 5.63 KB
/
AddInTemplateCommand.py
File metadata and controls
178 lines (127 loc) · 5.63 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#Author-Author
#Description-Description
import adsk.core, adsk.fusion, adsk.cam, traceback
# Global set of event handlers to keep them referenced for the duration of the command
_handlers = []
# TODO: Chage to unique values
COMMAND_ID = "commandId"
COMMAND_NAME = "Human readable Name"
COMMAND_TOOLTIP = "Short descripting"
# TODO: Place command in correct location(s). See toolbarPanelNames.txt for panel names
TOOLBAR_PANELS = ["SolidModifyPanel", "SolidCreatePanel"]
# Fires when the CommandDefinition gets executed.
# Responsible for adding commandInputs to the command &
# registering the other command handlers.
class CommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# Get the command that was created.
cmd = adsk.core.Command.cast(args.command)
# Some common EventHandlers
# For more Handlers and Info go to:
# http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-3922697A-7BF1-4799-9A5B-C8539DF57051
# Registers the CommandDestryHandler
onExecute = CommandExecuteHandler()
cmd.execute.add(onExecute)
_handlers.append(onExecute)
# Registers the CommandExecutePreviewHandler
onExecutePreview = CommandExecutePreviewHandler()
cmd.executePreview.add(onExecutePreview)
_handlers.append(onExecutePreview)
# Registers the CommandInputChangedHandler
onInputChanged = CommandInputChangedHandler()
cmd.inputChanged.add(onInputChanged)
_handlers.append(onInputChanged)
# Registers the CommandDestryHandler
onDestroy = CommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)
# Get the CommandInputs collection associated with the command.
inputs = cmd.commandInputs
# TODO: Add CommandInputs here
inputs.addTextBoxCommandInput("id","Name","Hello World!", 1, False)
except:
print(traceback.format_exc())
#Fires when the User executes the Command
#Responsible for doing the changes to the document
class CommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
print("execute")
# TODO: Add Command Execution Stuf Here
pass
except:
print(traceback.format_exc())
# Fires when the Command is being created or when Inputs are being changed
# Responsible for generating a preview of the output.
# Changes done here are temporary and will be cleaned up automatically.
class CommandExecutePreviewHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandEventArgs.cast(args)
# TODO: Add Command Execution Preview Stuff Here
# If set to True Fusion will use the last preview instead of calling
# the ExecuteHandler when the user executes the Command.
# If the preview is identical to the actual executing this saves recomputation
eventArgs.isValidResult = False
except:
print(traceback.format_exc())
# Fires when CommandInputs are changed
# Responsible for dynamically updating other Command Inputs
class CommandInputChangedHandler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# TODO: Add Input Changed Stuff Here
pass
except:
print(traceback.format_exc())
# Fires when the Command gets Destroyed regardless of success
# Responsible for cleaning up
class CommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# TODO: Add Destroy stuff
pass
except:
print(traceback.format_exc())
def run(context):
try:
app = adsk.core.Application.get()
ui = app.userInterface
commandDefinitions = ui.commandDefinitions
#check the command exists or not
cmdDef = commandDefinitions.itemById(COMMAND_ID)
if not cmdDef:
cmdDef = commandDefinitions.addButtonDefinition(COMMAND_ID, COMMAND_NAME,
COMMAND_TOOLTIP, '')
#Adds the commandDefinition to the toolbar
for panel in TOOLBAR_PANELS:
ui.allToolbarPanels.itemById(panel).controls.addCommand(cmdDef)
onCommandCreated = CommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
_handlers.append(onCommandCreated)
except:
print(traceback.format_exc())
def stop(context):
try:
app = adsk.core.Application.get()
ui = app.userInterface
#Removes the commandDefinition from the toolbar
for panel in TOOLBAR_PANELS:
p = ui.allToolbarPanels.itemById(panel).controls.itemById(COMMAND_ID)
if p:
p.deleteMe()
#Deletes the commandDefinition
ui.commandDefinitions.itemById(COMMAND_ID).deleteMe()
except:
print(traceback.format_exc())