-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanel_command.py
More file actions
134 lines (102 loc) · 5.16 KB
/
panel_command.py
File metadata and controls
134 lines (102 loc) · 5.16 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
import adsk.core
import adsk.fusion
import traceback
from .. import fusionAddInUtils as futil
from .panel_inputs import Inputs
from .panel_options import PanelOptions
from .panel_generate import generatePanelComponent
app = adsk.core.Application.get()
ui = app.userInterface
# CommandInputs Object
# https://help.autodesk.com/view/fusion360/ENU/?contextId=CommandInputs
#
# Command Inputs
# https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-8B9041D5-75CC-4515-B4BB-4CF2CD5BC359
#
# Creating Custom Fusion Commands
# https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-3922697A-7BF1-4799-9A5B-C8539DF57051
#
# Command Inputs API Sample
# https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-e5c4dbe8-ee48-11e4-9823-f8b156d7cd97
CMD_NAME = "Modular Synth Panel Generator"
CMD_Description = "Create a modular synth panel"
OPTIONS = PanelOptions("modular_synth_panel_generator.json")
INPUTS: Inputs
# Local list of event handlers used to maintain a reference so
# they are not released and garbage collected.
LOCAL_HANDLERS = []
def log(msg):
futil.log(f"[{CMD_NAME}] {str(msg)}")
def getErrorMessage(text="An unknown error occurred, please validate your inputs and try again"):
stackTrace = traceback.format_exc()
return f"{text}:<br>{stackTrace}"
# Named for easy importing into commandDialog/entry.py
def command_created(args: adsk.core.CommandCreatedEventArgs):
log("Command Created Event")
OPTIONS.restoreDefaults()
global INPUTS
INPUTS = Inputs(args.command.commandInputs, OPTIONS)
args.command.setDialogMinimumSize(400, 450)
args.command.setDialogSize(400, 450)
# Register event handlers
futil.add_handler(args.command.execute, onCommandExecute, local_handlers=LOCAL_HANDLERS)
futil.add_handler(args.command.executePreview, onCommandPreview, local_handlers=LOCAL_HANDLERS)
futil.add_handler(args.command.inputChanged, onCommandInputChanged, local_handlers=LOCAL_HANDLERS)
futil.add_handler(args.command.validateInputs, onCommandValidateInput, local_handlers=LOCAL_HANDLERS)
futil.add_handler(args.command.destroy, onCommandDestroy, local_handlers=LOCAL_HANDLERS)
# This event handler is called when the user clicks the OK button in the command dialog or
# is immediately called after the created event not command inputs were created for the dialog.
def onCommandExecute(args: adsk.core.CommandEventArgs):
log("Command Execute Event")
generatePanel(args)
# This event handler is called when the command needs to compute a new preview in the graphics window.
def onCommandPreview(args: adsk.core.CommandEventArgs):
log("Command Preview Event")
if INPUTS.isValid:
generatePanel(args)
else:
args.executeFailed = True
args.executeFailedMessage = "Some inputs are invalid, unable to generate preview"
# This event handler is called when the user changes anything in the command dialog
# allowing you to modify values of other inputs based on that change.
def onCommandInputChanged(args: adsk.core.InputChangedEventArgs):
changedInputId = args.input.id
log(f"Command Input Changed: {changedInputId}")
INPUTS.handleInputChange(changedInputId)
# This event handler is called when the user interacts with any of the inputs in the dialog
# which allows you to verify that all of the inputs are valid and enables the OK button.
def onCommandValidateInput(args: adsk.core.ValidateInputsEventArgs):
args.areInputsValid = INPUTS.isValid
log(f"Validate Input Event: isValid={INPUTS.isValid}")
if INPUTS.isValid:
INPUTS.updateOptionsFromInputs()
# This event handler is called when the command terminates.
def onCommandDestroy(args: adsk.core.CommandEventArgs):
log("Command Destroy Event")
global LOCAL_HANDLERS
LOCAL_HANDLERS = []
def generatePanel(args: adsk.core.CommandEventArgs):
try:
des = adsk.fusion.Design.cast(app.activeProduct)
if des.designType == 0:
args.executeFailed = True
args.executeFailedMessage = "Projects with direct modeling are not supported, please enable parametric modeling (timeline) to proceed."
return False
root = adsk.fusion.Component.cast(des.rootComponent)
componentName = "{} {} {} Panel".format(OPTIONS.formatName, OPTIONS.widthInHp, OPTIONS.widthUnitName)
# create new component
newCmpOcc = adsk.fusion.Occurrences.cast(root.occurrences).addNewComponent(adsk.core.Matrix3D.create())
newCmpOcc.component.name = componentName
newCmpOcc.activate()
panelComponent: adsk.fusion.Component = newCmpOcc.component
generatePanelComponent(panelComponent, OPTIONS)
# group features in timeline
count = panelComponent.sketches.count + panelComponent.features.count + panelComponent.constructionAxes.count + panelComponent.constructionPlanes.count
if count > 1:
panelGroup = des.timeline.timelineGroups.add(newCmpOcc.timelineObject.index, newCmpOcc.timelineObject.index + count)
panelGroup.name = componentName
except Exception as err:
args.executeFailed = True
args.executeFailedMessage = getErrorMessage()
log(f"Error occurred, {err}, {getErrorMessage()}")
return False