-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnyMacro.py
More file actions
665 lines (500 loc) · 27.2 KB
/
AnyMacro.py
File metadata and controls
665 lines (500 loc) · 27.2 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#Author-ZXYNINE
#Description-Shows a menu that let you assign macros from last run commands.
# This file is part of AnyMacro, a Fusion 360 add-in for assigning
# macros from last run commands.
#
# Copyright (c) 2021 ZXYNINE
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import adsk.core, adsk.fusion, adsk.cam
from collections import deque
import os.path as path
# Import relative path to avoid namespace pollution
from .AddinLib import utils, events, manifest, error, settings, geometry, AppObjects
utils.ReImport_List(AppObjects, events, manifest, error, settings, geometry, utils)
NAME = 'AnyMacro'
VERSION = manifest.getVersion()
FILE_DIR = path.dirname(path.realpath(__file__))
VERSION_INFO = f'({NAME} v {VERSION})'
CMD_DESCRIPTION = 'Enables or disables the tracking of commands to create a macro.'
COMMAND_DATA = f'{CMD_DESCRIPTION}\n\n{VERSION_INFO}\n'
ENABLE_CMD_DEF_ID = 'zxynine_anyMacroList'
PANEL_ID = 'zxynine_anyMacroPanel'
NO_MACROS_ID = 'zxynine_anyMacroListEmpty'
TRACKING_DROPDOWN_ID = 'zxynine_anyMacroDropdown'
MACRO_DROPDOWN_ID = 'zxynine_anyMacroPremadeMacrosDropdown'
CONSECUTIVE_TOGGLE_ID = 'zxynine_anyMacroBlockConsecutive'
BUILD_MACRO_CMD_DEF_ID = 'zxynine_anyMacroBuildMacro'
CLEAR_RECORD_CMD_ID = 'zxynine_anyMacroClearRecord'
HALT_CMD_ID = 'zxynine_anyMacroHaltFire'
ADD_MACRO_CUSTOM_ID = 'AnyMacro_Add_Macro'
app_:adsk.core.Application = None
ui_:adsk.core.UserInterface = None
error_catcher_ = error.ErrorCatcher()
events_manager_ = events.EventsManager(error_catcher_)
add_macro_event:adsk.core.CustomEvent=None
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#Commands/Settings
MAX_TRACK = 10
MACRO_DATA_PATH = path.join(FILE_DIR,'macros')
MACRO_FILE_DATA_PATH = path.join(MACRO_DATA_PATH, 'SavedMacros.json')
allMacros = []
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#Simple functions
def exists(obj):return obj is not None
def getDelete(collection:adsk.core.CommandDefinitions,objId): utils.ifDelete(collection.itemById(objId))
def deleteAll(*objs): return all(map(utils.ifDelete,objs))
def UpdateButton(cmdDef: adsk.core.CommandDefinition,Title,Icon): cmdDef.resourceFolder = Icon; cmdDef.controlDefinition.name = Title
def FullPromote(cmdCtrl:adsk.core.CommandControl): cmdCtrl.isPromoted=cmdCtrl.isPromotedByDefault = True
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#Specific Functions
# Commands without icons cannot have shortcuts, so add one if needed.
# Maybe because the "Pin to" options in the same menu would fail? Creds to u/lf_1 on reddit.
def checkIcon(cmdDef:adsk.core.CommandDefinition, noIconPath:str = './resources/noicon'):
try:
if not cmdDef.resourceFolder: cmdDef.resourceFolder = noIconPath
except: cmdDef.resourceFolder = noIconPath
return cmdDef
def update_enable_text(curCount=0):
if curCount > MAX_TRACK: currentMacro.stopTracking()
if not CommandTracker.tracking_: text,icon = f'Start recording (Auto-stop after {MAX_TRACK} unique commands)', './resources/record'
else: text,icon = f'Stop recording (Auto-stop after {MAX_TRACK-curCount} more commands)', './resources/stop'
UpdateButton(enable_cmd.definition, text, icon)
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class ReferenceBase:
def __init__(self,cmdDef:adsk.core.CommandDefinition=None, cmdCtrl: 'adsk.core.CommandControl | adsk.core.DropDownControl'=None):
self.definition = cmdDef
self.control = cmdCtrl
self.id = cmdCtrl.id if exists(cmdCtrl) else cmdDef.id if exists(cmdDef) else None
def deleteMe(self): utils.ifDelete(self.control); self.definition=self.control=None
class CommandRef(ReferenceBase):
def __init__(self,parentControls:adsk.core.ToolbarControls,newId,newName,newIcon='./resources/noicon',newToolTip=''):
getDelete(ui_.commandDefinitions, newId)
cmdDef = ui_.commandDefinitions.addButtonDefinition(newId, newName, newToolTip, newIcon)
super().__init__(cmdDef, parentControls.addCommand(checkIcon(cmdDef)))
@property
def commandCreated(self): return self.definition.commandCreated
class DropdownRef(ReferenceBase):
def __init__(self,parentControls:adsk.core.ToolbarControls,newId,newName,newIcon='./resources/noicon',newToolTip=''):
getDelete(parentControls, newId)
cmdCtrl:adsk.core.DropDownControl = parentControls.addDropDown(newName, newIcon, newId)
self.dropdownControls = cmdCtrl.controls
super().__init__(None, cmdCtrl)
class ToggleRef(ReferenceBase):
def __init__(self,parentControls:adsk.core.ToolbarControls,newId,newName,startValue,newToolTip=''):
getDelete(ui_.commandDefinitions, newId)
cmdDef = ui_.commandDefinitions.addCheckBoxDefinition(newId, newName, newToolTip, startValue)
self.controlDefinition:adsk.core.CheckBoxControlDefinition = cmdDef.controlDefinition
super().__init__(cmdDef, parentControls.addCommand(cmdDef))
@property
def value(self):return self.controlDefinition.isChecked
# def MakeSeperator(parentControls:adsk.core.ToolbarControls):parentControls.addSeparator
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
panel_:adsk.core.ToolbarPanel = None
tracking_dropdown_:DropdownRef = None
macro_dropdown_:DropdownRef = None
macro_dropdown_empty:CommandRef = None
enable_cmd:CommandRef = None
build_macro_cmd:CommandRef = None
clear_record_cmd:CommandRef = None
consecutive_block_tgl:ToggleRef=None
halt_cmd_def:CommandRef = None
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class Macro:
@staticmethod
def fromJson(JsonObject: object):
MacroMethod = settings.fromJson(JsonObject, {dict:Macro.fromDict, list:Macro.fromList})
return MacroMethod(JsonObject)
@classmethod
def fromList(cls, macroList:list): return [cls.fromDict(dict) for dict in macroList]
@classmethod
def fromDict(cls, macroDict:dict):
MacroName = macroDict['name']
MacroId = macroDict['id']
executeList = macroDict['executeList']
parentControl:adsk.core.ToolbarControls = macro_dropdown_.control.controls
return Macro(executeList,parentControl,MacroId,MacroName,True)
@classmethod
def toList(cls): return [dict for dict in [cls.toDict(macro) for macro in allMacros] if dict]
def toDict(self):
if self.name == '': return None
macroDict = {}
macroDict['name']= self.name
macroDict['id']= self.id
macroDict['executeList']= self.executeList
return macroDict
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def __init__(self,CommandIdList:list,parentControls:adsk.core.ToolbarControls=None,macroId=None,macroName=None, isBuilt = False):
allMacros.append(self)
self.executeList = list(CommandIdList)
self.parentControls = parentControls or macro_dropdown_.dropdownControls
self.isBuilt = isBuilt
self.initialise()
self.updateIdentity(macroId,macroName)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def updateIdentity(self,MacroId=None,MacroName=None):
if MacroName is None:
MacroName, cancelled = ui_.inputBox('Enter macro name:','Naming Macro','')
if cancelled or MacroName == '': return False
self.name = MacroName
self.id = MacroId or f'AnyMacro_{utils.toIdentifier(MacroName)}'
self.updateCommands(self.parentControls)
return True
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def updateCommands(self, parentControls:adsk.core.ToolbarControls):
self.removeCommands()
self.parentControls = parentControls
self.Dropdown = DropdownRef(parentControls, f'{self.id}_group', self.name, './resources/anymacro')
self.Command = CommandRef(self.Dropdown.dropdownControls, self.id, self.name, './resources/anymacro')
self.Delete = CommandRef(self.Dropdown.dropdownControls, f'{self.id}_delete', f'Delete {self.name}', './resources/delete')
self.updateHandlers(self.executeList)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def updateHandlers(self,CommandIdList:list = None):
self.removeHandlers()
self.executeList = list(CommandIdList)
def MacroRemoveHandler(args:adsk.core.CommandCreatedEventArgs):
result = utils.MessagePromptCast(f'Are you sure you wish to delete the macro "{self.name}"?', 'Confirm Macro Deletion')
if result is not True: return #Cancels the deletion
self.removeAll()
if self.isBuilt: macrosToJson() #Updates the save file
else: currentMacro.clear() #Removes the history along with it
self.createInfo = events_manager_.add_handler(self.Command.commandCreated, getQueuedEvents(self.executeList))
self.removeInfo = events_manager_.add_handler(self.Delete.commandCreated, MacroRemoveHandler)
if self.isBuilt: macrosToJson()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def initialise(self):
self.Dropdown:DropdownRef=None
self.Command:CommandRef=None
self.Delete:CommandRef=None
self.createInfo = self.removeInfo = None
def removeCommands(self):
[cmd.deleteMe() for cmd in (self.Dropdown,self.Command,self.Delete) if exists(cmd)]
def removeHandlers(self):
[handler.remove() for handler in (self.createInfo,self.removeInfo) if exists(handler)]
def removeAll(self):
self.removeHandlers()
self.removeCommands()
allMacros.remove(self)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def getQueuedEvents(executeList:deque):
def initialCreate(args: adsk.core.CommandCreatedEventArgs):
currentCommand = None
commandOrder = deque(executeList)
def stopHandlers():
nonlocal currentCommand; currentCommand = None
startingInfo.remove(); terminatedInfo.remove()
def CmdStartingHandler(args:adsk.core.ApplicationCommandEventArgs):
if args.commandId == HALT_CMD_ID: return stopHandlers()
if args.commandId == commandOrder[0]:
nonlocal currentCommand; currentCommand = commandOrder.popleft()
if len(commandOrder) == 0: stopHandlers()
def CmdTerminatedHandler(args:adsk.core.ApplicationCommandEventArgs):
if args.commandId == currentCommand:
if len(commandOrder) == 0: stopHandlers()
else: utils.executeCommand(commandOrder[0])
startingInfo = events_manager_.add_handler(ui_.commandStarting, CmdStartingHandler)
terminatedInfo = events_manager_.add_handler(ui_.commandTerminated, CmdTerminatedHandler)
utils.executeCommand(commandOrder[0])
return initialCreate
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#TODO: Convert Command tracker into singleton, or try to merge with Macro class
class CommandTracker:
deleteID = 0
tracking_ = False
@classmethod
def toggle(cls,value):
cls.tracking_ = value
update_enable_text()
checkQueue()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def __init__(self,):
self.executeList: 'deque[ReferenceBase]' = deque()
self.cmdIds: dict = {}
self.currentMacro:Macro=None
self.currentSeperator:adsk.core.SeparatorControl = None
self.startTracking()
def startTracking(self):
self.lastID = ''
self.getHandler()
CommandTracker.toggle(True)
def stopTracking(self):
CommandTracker.toggle(False)
self.removeHandler()
if self.count == 0: return
getDelete(tracking_dropdown_.dropdownControls, 'DemoMacroSeperator')
self.currentSeperator = tracking_dropdown_.dropdownControls.addSeparator('DemoMacroSeperator')
self.currentMacro = Macro(self.cmdIds.values(), tracking_dropdown_.dropdownControls, 'TestMacroId', 'Test Macro')
def getHandler(self):
def command_starting_handler(args:adsk.core.ApplicationCommandEventArgs):
cmdDef = args.commandDefinition
if cmdDef.id in {enable_cmd.id,'SelectCommand'}:return # Skip ourselves/Select
elif cmdDef.id != self.lastID: self.log(cmdDef) #Commands start twice? Dont log that.
elif not consecutive_block_tgl.value: self.lastID = ''
self.starting_handler = events_manager_.add_handler(ui_.commandStarting, command_starting_handler)
def removeHandler(self): self.starting_handler.remove()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@property
def count(self):return len(self.executeList)
def log(self, cmdDef: adsk.core.CommandDefinition):
self.lastID = cmdDef.id
newId = f'{cmdDef.id}_Macro_Fragment_{CommandTracker.deleteID}'
CommandTracker.deleteID +=1
self.cmdIds[newId] = cmdDef.id
getDelete(ui_.commandDefinitions, newId)
newCmdDef = ui_.commandDefinitions.addButtonDefinition(newId, cmdDef.name,'Click to remove this command from the Macro', checkIcon(cmdDef).resourceFolder)
newCmdCtrl = tracking_dropdown_.dropdownControls.addCommand(newCmdDef)
def removeHandler(args: adsk.core.CommandCreatedEventArgs):
getDelete(tracking_dropdown_.dropdownControls, newCmdDef.id)
removeInfo.remove()
self.cmdIds.pop(newId)
self.currentMacro.updateHandlers(self.cmdIds.values())
removeInfo = events_manager_.add_handler(newCmdDef.commandCreated,removeHandler)
self.executeList.append(ReferenceBase(newCmdDef,newCmdCtrl))
update_enable_text(self.count)
def build(self):
if self.count == 0: return
self.currentMacro.isBuilt = True #Enables the remove handler to update the json
oldControls = self.currentMacro.parentControls
self.currentMacro.parentControls = macro_dropdown_.dropdownControls
if self.currentMacro.updateIdentity(): #Forces the macro to ask for a name and update
macrosToJson() #Saves all current macros
self.clear()
else:
self.currentMacro.isBuilt = False
self.currentMacro.parentControls = oldControls
def clear(self):
self.currentMacro = None
self.currentSeperator.deleteMe(); self.currentSeperator = None
self.executeList=[cmd.deleteMe() for cmd in self.executeList][:0]
self.cmdIds.clear()
checkQueue()
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
currentMacro : CommandTracker = None
def checkQueue(): #This determines whether the save/clear buttons are visible (only visible after recording)
viewValue = exists(currentMacro) and currentMacro.count > 0
build_macro_cmd.control.isVisible = viewValue
clear_record_cmd.control.isVisible = viewValue
def enable_cmd_def__created_handler(args: adsk.core.CommandCreatedEventArgs):
def enable_command_execute_handler(args:adsk.core.CommandEventArgs):
global currentMacro
if currentMacro is None: currentMacro = CommandTracker()
else: currentMacro.startTracking() if (not CommandTracker.tracking_) else currentMacro.stopTracking()
events_manager_.add_handler(args.command.execute, callback=enable_command_execute_handler)
def build_macro_handler(args:adsk.core.CommandCreatedEventArgs):
if exists(currentMacro): currentMacro.build()
def clear_record_handler(args:adsk.core.CommandCreatedEventArgs):
if exists(currentMacro):
currentMacro.currentMacro.removeAll()
currentMacro.clear()
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@error_catcher_
def run(context):
global app_, ui_, panel_
app_,ui_ = AppObjects.GetAppUI()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Add the command to the tab.
panels = ui_.allToolbarTabs.itemById('ToolsTab').toolbarPanels
getDelete(panels,PANEL_ID)
panel_ = panels.add(PANEL_ID, NAME)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
add_primary_commands(panel_)
update_enable_text()
checkQueue()
jsonToMacros() #Loads the saved macros
createAddMacroCustomEvent()
createBuiltInCommands()
@error_catcher_
def stop(context):
removeAddMacroCustomEvent()
removeBuiltInCommands()
events_manager_.clean_up()
deleteAll(tracking_dropdown_.control, macro_dropdown_.control, panel_)
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def add_primary_commands(parent:adsk.core.ToolbarPanel):
global tracking_dropdown_
tracking_dropdown_ = DropdownRef(parent.controls, TRACKING_DROPDOWN_ID, 'Macro Recorder', './resources/tracker')
add_record_dropdown(tracking_dropdown_.control.controls)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global macro_dropdown_
macro_dropdown_ = DropdownRef(parent.controls, MACRO_DROPDOWN_ID, 'Custom Macros', './resources/allmacros')
add_macro_dropdown(macro_dropdown_.control.controls)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global halt_cmd_def
halt_cmd_def = CommandRef(parent.controls, HALT_CMD_ID, 'Stop current Macro', './resources/noicon',
'Will instantly stop any macro that is running when this command is fired.')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global consecutive_block_tgl
consecutive_block_tgl = ToggleRef(parent.controls, CONSECUTIVE_TOGGLE_ID, 'Block Consecutive Fires', False)
def add_record_dropdown(parent:adsk.core.ToolbarControls):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Cannot get checkbox to play nicely (won't update without collapsing the menu and the default checkbox icon is not showing...). See checkbox-test branch.
global enable_cmd
enable_cmd = CommandRef(parent, ENABLE_CMD_DEF_ID, f'Loading...',newToolTip=COMMAND_DATA)
events_manager_.add_handler(event=enable_cmd.commandCreated, callback=enable_cmd_def__created_handler)
FullPromote(enable_cmd.control)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global clear_record_cmd
clear_record_cmd = CommandRef(parent, CLEAR_RECORD_CMD_ID, 'Reset Recording', './resources/repeat')
events_manager_.add_handler(event=clear_record_cmd.commandCreated, callback=clear_record_handler)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global build_macro_cmd
build_macro_cmd = CommandRef(parent, BUILD_MACRO_CMD_DEF_ID, 'Save Macro', './resources/save')
events_manager_.add_handler(event=build_macro_cmd.commandCreated, callback=build_macro_handler)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
getDelete(parent, f'{ENABLE_CMD_DEF_ID}_Seperator')
parent.addSeparator(f'{ENABLE_CMD_DEF_ID}_Seperator')#|||||||||||||||||||||||||||||||||||||||||||||||||||||||
def add_macro_dropdown(parent:adsk.core.ToolbarControls):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global macro_dropdown_empty
macro_dropdown_empty = CommandRef(parent, NO_MACROS_ID, 'All Custom Macros', './resources/noicon',
'Start by recording a new macro and saving it')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
getDelete(parent, f'{NO_MACROS_ID}_Seperator')
parent.addSeparator(f'{NO_MACROS_ID}_Seperator')#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# class ViewCubeIDs:
# Home = 'Tion_ViewCube_Home'
# Top = 'Tion_ViewCube_Top'
# Bottom = 'Tion_ViewCube_Bottom'
# Left = 'Tion_ViewCube_Left'
# Right = 'Tion_ViewCube_Right'
# Front = 'Tion_ViewCube_Front'
# Back = 'Tion_ViewCube_Back'
# def add_view_orientations(parent:adsk.core.ToolbarControls):
# build_macro_cmd = CommandRef(parent, ViewCubeIDs.Home, 'Home', './resources/noicon')
# events_manager_.add_handler(event=build_macro_cmd.commandCreated, callback=build_macro_handler)
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#json data
def jsonToMacros(): Macro.fromList(settings.readDataFromFile(MACRO_FILE_DATA_PATH,True))
def macrosToJson(): settings.writeDataToFile(MACRO_FILE_DATA_PATH,Macro.toList(),True)
# Custom event so other addins can create macros
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
add_macro_event_Handler:events.LinkedHandler = None
def createAddMacroCustomEvent():
global add_macro_event, add_macro_event_Handler
def AddMacroEventHandler(args:adsk.core.CustomEventArgs):
Macro.fromJson(args.additionalInfo)
add_macro_event = utils.CustomEvents.Create(ADD_MACRO_CUSTOM_ID)
add_macro_event_Handler = events_manager_.add_handler(add_macro_event,AddMacroEventHandler)
def removeAddMacroCustomEvent():
add_macro_event_Handler.remove()
utils.CustomEvents.Remove(ADD_MACRO_CUSTOM_ID)
class ViewCube:
class Direction:
@classmethod
def NewDir(cls, UpDown=0,LeftRight=0,FrontBack=0):
Yup,Zup = (LeftRight,UpDown,FrontBack), (LeftRight,FrontBack,UpDown)
negYup,negZup = [-val for val in Yup], [-val for val in Zup]
return cls(Yup,Zup), cls(negYup,negZup)
YAxisUp = adsk.core.DefaultModelingOrientations.YUpModelingOrientation
ZAxisUp = adsk.core.DefaultModelingOrientations.ZUpModelingOrientation
def GetCurrentOrientation(self): return app_.preferences.generalPreferences.defaultModelingOrientation
def __init__(self, Yup,Zup): self.direction = {self.YAxisUp:Yup, self.ZAxisUp:Zup}.get
def __get__(self,instance,owner):
return adsk.core.Vector3D.create(*self.direction(self.GetCurrentOrientation()))
def __set__(self,instance,value): return False
def CombinedView(*directions:adsk.core.Vector3D):
SUMVEC :adsk.core.Vector3D = adsk.core.Vector3D.create(0,0,0)
for dir in directions: SUMVEC.add(dir)
return SUMVEC if SUMVEC.length != 0 and SUMVEC.normalize() else ViewCube.Front
Top, Bottom= Direction.NewDir(UpDown=1)
Left, Right= Direction.NewDir(LeftRight=-1)
Front, Back= Direction.NewDir(FrontBack=-1)
def GetOrientationsUp(orientation:adsk.core.Vector3D):
def vectorTuple(vec:adsk.core.Vector3D):return vec.x,vec.y,vec.z
return {vectorTuple(ViewCube.Top):ViewCube.Front, vectorTuple(ViewCube.Bottom):ViewCube.Back}.get(vectorTuple(orientation), ViewCube.Top)
def TryViewOrientation(args, orientation=None, localView = True):
# ui_.messageBox(str(ViewCube.CombinedView(ViewCube.Front,ViewCube.Left).asArray()))
# ui_.messageBox(str(ViewCube.CombinedView(ViewCube.Front,ViewCube.Back).asArray()))
# ui_.messageBox(str(ViewCube.CombinedView(ViewCube.Front,ViewCube.Left, ViewCube.Top).asArray()))
camera = utils.camera.get()
eyeVector= utils.camera.viewDirection(camera)
orientation = ViewCube.Front
upDirection = ViewCube.GetOrientationsUp(orientation)
# ui_.messageBox(str(upDirection.asArray()))
orientation.scaleBy(eyeVector.length)
newEye = camera.target.copy()
newEye.translateBy(orientation)
camera.upVector = upDirection
camera.eye = newEye
utils.camera.updateCamera(camera)
utils.doEvents()
# ui_.messageBox(str((ViewCube.Front.asArray(),ViewCube.Top.asArray())))
# ui_.messageBox(str((str(utils.camera.get().eye.asArray()),str(utils.camera.get().target.asArray()), str(utils.camera.get().upVector.asArray()))))
#Built in commands for built in macros
#|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def reAssignCamera(cameraCopy:adsk.core.Camera):
utils.camera.updateCamera(cameraCopy,True)
ui_.activeSelections.clear()
def getLineDirection(prompt):
try: line = ui_.selectEntity(prompt,'LinearEdges,SketchLines,ConstructionLines')
except: return None
if line: return geometry.lines.getDirection(line.entity)
def viewCommandSetup(cmd: adsk.core.Command): cmd.isRepeatable=cmd.isExecutedWhenPreEmpted=False
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def alignViewHandler(args: adsk.core.CommandCreatedEventArgs):
viewCommandSetup(args.command)
lineDirection = getLineDirection('Please select a line represinting the "up" direction')
if not lineDirection: return
camera_copy = utils.camera.get()
upDirection = camera_copy.upVector.copy()
orintatedVector = geometry.vectors.project(upDirection,lineDirection,True)
camera_copy.upVector = orintatedVector
reAssignCamera(camera_copy)
def changeViewAxis(args: adsk.core.CommandCreatedEventArgs):
viewCommandSetup(args.command)
lineDirection = getLineDirection('Please select a line represinting the "forwards" direction')
if not lineDirection: return
camera_copy = utils.camera.get()
cameraDirection = utils.camera.viewDirection(camera_copy)
if cameraDirection.isPerpendicularTo(lineDirection):#Prevents perpendicular angles from failing
orintatedVector = geometry.vectors.normalOf(lineDirection)
if camera_copy.upVector.isParallelTo(lineDirection):
camera_copy.upVector = geometry.vectors.normalOf(cameraDirection)
else: orintatedVector = geometry.vectors.project(cameraDirection,lineDirection,True)
orintatedVector.scaleBy(cameraDirection.length)
newEye = camera_copy.target.asVector()
newEye.subtract(orintatedVector)
camera_copy.eye = newEye.asPoint()
reAssignCamera(camera_copy)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def createBuiltInCommands():
inspectPanel = ui_.allToolbarPanels.itemById('ToolsInspectPanel')
AlignView= CommandRef(inspectPanel.controls,
'zxynine_anymacro_BuiltinAlignView',
'Change Cameras Up',
'./resources/BuiltinIcons/CameraUp','')
events_manager_.add_handler(AlignView.commandCreated, alignViewHandler)
ChangeView= CommandRef(inspectPanel.controls,
'zxynine_anymacro_BuiltinChangeView',
'Change Cameras Forwards',
'./resources/BuiltinIcons/CameraForward','')
events_manager_.add_handler(ChangeView.commandCreated, changeViewAxis)
ChangeView= CommandRef(inspectPanel.controls,
'zxynine_anymacro_BuiltinChangeViewOrientation',
'Change Cameras View Orientation',
'./resources/save','')
events_manager_.add_handler(ChangeView.definition.commandCreated, TryViewOrientation)
def removeBuiltInCommands():
inspectPanel = ui_.allToolbarPanels.itemById('ToolsInspectPanel')
getDelete(inspectPanel.controls,'zxynine_anymacro_BuiltinAlignView')
getDelete(inspectPanel.controls,'zxynine_anymacro_BuiltinChangeView')
getDelete(inspectPanel.controls,'zxynine_anymacro_BuiltinChangeViewOrientation')
getDelete(ui_.commandDefinitions,'zxynine_anymacro_BuiltinChangeViewOrientation')
getDelete(ui_.commandDefinitions,'zxynine_anymacro_BuiltinAlignView')
getDelete(ui_.commandDefinitions,'zxynine_anymacro_BuiltinChangeView')