-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathExportCutlist.py
More file actions
268 lines (197 loc) · 11.7 KB
/
ExportCutlist.py
File metadata and controls
268 lines (197 loc) · 11.7 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
#Author-Billy Keyes
#Description-Export a cut list of all bodies as a JSON file
# Fusion addins by convention use CamelCase for the root module name. Disable
# the invalid-name check only for the module name, then re-enable it for the
# names within the module.
#
# pylint: disable=invalid-name
# pylint: enable=invalid-name
import functools
import io
import traceback
from dataclasses import dataclass, field
import adsk.core
import adsk.fusion
from .lib.format import ALL_FORMATS, FormatOptions, TableFormat, CSVFormat, get_format
from .lib.cutlist import GroupBy, CutList, CutListOptions
COMMAND_ID = 'ExportCutlistCommand'
COMMAND_NAME = 'Export Cutlist'
DEFAULT_TOLERANCE = 1e-04
DEFAULT_GROUPBY = GroupBy(dimensions=True, material=True)
DEFAULT_UNIT = 'auto'
ALL_UNITS = [
'auto', 'in', 'ft', 'mm', 'cm', 'm'
]
# Required to keep handlers in scope
handlers = []
@dataclass
class Options:
cutlist_options: CutListOptions = field(default_factory=CutListOptions)
format: str = TableFormat.name
format_options: FormatOptions = field(default_factory=FormatOptions)
# Remember user options in between creations of the command
user_options = Options()
def report_errors(func):
"""Decorator that catches any exception thrown by the function and displays it in the UI.
For use only on top-level functions called by Fusion that do not return values.
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except: # pylint: disable=bare-except
app = adsk.core.Application.get()
app.userInterface.messageBox(f'Failed:\n{traceback.format_exc()}')
return wrapper
class CutlistCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
@report_errors
def notify(self, args):
app = adsk.core.Application.get()
design = adsk.fusion.Design.cast(app.activeProduct)
if not design:
app.userInterface.messageBox('A design must be active for this command.', COMMAND_NAME)
return
event_args = adsk.core.CommandCreatedEventArgs.cast(args)
cmd = event_args.command
inputs = cmd.commandInputs
select_input = inputs.addSelectionInput('selection', 'Selection', 'Select body or component')
select_input.tooltip = 'Select bodies or components to export.'
select_input.addSelectionFilter('SolidBodies')
select_input.addSelectionFilter('Occurrences')
select_input.setSelectionLimits(0)
hidden_input = inputs.addBoolValueInput('hidden', 'Ignore hidden', True, '', user_options.cutlist_options.ignore_hidden)
hidden_input.tooltip = 'If checked, hidden bodies are excluded from the cutlist.'
external_input = inputs.addBoolValueInput('external', 'Ignore external', True, '', user_options.cutlist_options.ignore_external)
external_input.tooltip = 'If checked, external components are excluded from the cutlist.'
format_group = inputs.addGroupCommandInput('format_group', 'Format')
format_group.isEnabledCheckBoxDisplayed = False
format_group.isExpanded = True
format_input = format_group.children.addDropDownCommandInput('format', 'Output format', adsk.core.DropDownStyles.LabeledIconDropDownStyle)
format_input.tooltip = 'The output format of the cutlist.'
for fmt in ALL_FORMATS:
format_input.listItems.add(fmt.name, user_options.format == fmt.name, '')
short_names_input = format_group.children.addBoolValueInput('short_names', 'Use short names', True, '', user_options.format_options.short_names)
short_names_input.tooltip = 'If checked, do not include the parents of a body or component in its name.'
component_names_input = format_group.children.addBoolValueInput('component_names', 'Use component names', True, '', user_options.format_options.component_names)
component_names_input.tooltip = 'If checked, use component names instead of body names.'
unique_names_input = format_group.children.addBoolValueInput('unique_names', 'Only output unique names', True, '', user_options.format_options.unique_names)
unique_names_input.tooltip = 'If checked, only include the unique names associated with each cutlist item.'
remove_numeric_input = format_group.children.addBoolValueInput('remove_numeric', 'Remove numeric suffixes', True, '', user_options.format_options.remove_numeric_suffixes)
remove_numeric_input.tooltip = 'If checked, remove common numeric suffixes from names.'
include_material_input = format_group.children.addBoolValueInput('include_material', 'Include material details', True, '', user_options.format_options.include_material)
include_material_input.tooltip = 'If checked, include material details in the output'
unit_input = format_group.children.addDropDownCommandInput('unit', 'Output units', adsk.core.DropDownStyles.LabeledIconDropDownStyle)
unit_input.tooltip = 'Units for output dimensions'
for units in ALL_UNITS:
unit_input.listItems.add(units, user_options.format_options.units == units, '')
group_by_group = inputs.addGroupCommandInput('group_by_group', 'Group By')
group_by_group.isEnabledCheckBoxDisplayed = False
group_by_group.isExpanded = True
dimensions_input = group_by_group.children.addBoolValueInput('group_dimensions', 'Dimensions', True, '', user_options.cutlist_options.group_by.dimensions)
dimensions_input.tooltip = 'If checked, group bodies by their dimensions.'
material_input = group_by_group.children.addBoolValueInput('group_material', 'Material', True, '', user_options.cutlist_options.group_by.material)
material_input.tooltip = 'If checked, group bodies by their material.'
material_input.tooltipDescription = 'This option is only used when also grouping bodies by their dimensions.'
material_input.isEnabled = dimensions_input.value
advanced_group = inputs.addGroupCommandInput('advanced', 'Advanced Options')
advanced_group.isEnabledCheckBoxDisplayed = False
advanced_group.isExpanded = False
axis_aligned_input = advanced_group.children.addBoolValueInput('axisaligned', 'Use axis-aligned boxes', True, '', user_options.cutlist_options.axis_aligned)
axis_aligned_input.tooltip = 'If checked, use axis-algined bounding boxes.'
axis_aligned_input.tooltipDescription = 'This disables the rotation heuristic and assumes parts are already in the ideal orientation relative to the X, Y, and Z axes.'
tolerance_input = advanced_group.children.addValueInput('tolerance', 'Tolerance', 'mm', adsk.core.ValueInput.createByReal(user_options.cutlist_options.tolerance))
tolerance_input.tooltip = 'The tolerance used when matching bounding box dimensions.'
execute_handler = CutlistCommandExecuteHandler()
cmd.execute.add(execute_handler)
handlers.append(execute_handler)
input_handler = CutlistCommandInputChangedHandler()
cmd.inputChanged.add(input_handler)
handlers.append(input_handler)
class CutlistCommandInputChangedHandler(adsk.core.InputChangedEventHandler):
def notify(self, args):
event_args = adsk.core.InputChangedEventArgs.cast(args)
changed_input = event_args.input
if changed_input.id == 'group_dimensions':
inputs = event_args.firingEvent.sender.commandInputs
material_input = inputs.itemById('group_material')
material_input.isEnabled = changed_input.value
class CutlistCommandExecuteHandler(adsk.core.CommandEventHandler):
@report_errors
def notify(self, args):
event_args = adsk.core.CommandEventArgs.cast(args)
inputs = event_args.command.commandInputs
app = adsk.core.Application.get()
ui = app.userInterface
doc = app.activeDocument
design = adsk.fusion.Design.cast(app.activeProduct)
set_options_from_inputs(inputs)
selection = []
selection_input = inputs.itemById('selection')
for i in range(selection_input.selectionCount):
selection.append(selection_input.selection(i).entity)
cutlist = CutList(user_options.cutlist_options)
cutlist.add(design.rootComponent, selection)
fmt_class = get_format(user_options.format)
fmt = fmt_class(design.unitsManager, doc.name, user_options.format_options)
dlg = ui.createFileDialog()
dlg.title = 'Save Cutlist'
dlg.filter = fmt.filefilter.filter_str
dlg.initialFilename = fmt.filename
if dlg.showSave() != adsk.core.DialogResults.DialogOK:
return
filename = dlg.filename
newline = '' if isinstance(fmt, CSVFormat) else None
with io.open(filename, 'w', newline=newline, encoding='utf-8') as f:
f.write(fmt.format(cutlist))
ui.messageBox(f'Export complete: {filename}', COMMAND_NAME)
def set_options_from_inputs(inputs: adsk.core.CommandInputs):
hidden_input: adsk.core.BoolValueCommandInput = inputs.itemById('hidden')
external_input: adsk.core.BoolValueCommandInput = inputs.itemById('external')
format_input: adsk.core.DropDownCommandInput = inputs.itemById('format')
short_names_input: adsk.core.BoolValueCommandInput = inputs.itemById('short_names')
component_names_input: adsk.core.BoolValueCommandInput = inputs.itemById('component_names')
unique_names_input: adsk.core.BoolValueCommandInput = inputs.itemById('unique_names')
remove_numeric_input: adsk.core.BoolValueCommandInput = inputs.itemById('remove_numeric')
include_material_input: adsk.core.BoolValueCommandInput = inputs.itemById('include_material')
unit_input: adsk.core.DropDownCommandInput = inputs.itemById('unit')
dimensions_input: adsk.core.BoolValueCommandInput = inputs.itemById('group_dimensions')
material_input: adsk.core.BoolValueCommandInput = inputs.itemById('group_material')
group_by = GroupBy(dimensions=dimensions_input.value, material=material_input.value)
axis_aligned_input: adsk.core.BoolValueCommandInput = inputs.itemById('axisaligned')
tolerance_input: adsk.core.ValueCommandInput = inputs.itemById('tolerance')
user_options.cutlist_options.ignore_hidden = hidden_input.value
user_options.cutlist_options.ignore_external = external_input.value
user_options.cutlist_options.group_by = group_by
user_options.cutlist_options.axis_aligned = axis_aligned_input.value
user_options.cutlist_options.tolerance = tolerance_input.value
user_options.format = format_input.selectedItem.name
user_options.format_options.short_names = short_names_input.value
user_options.format_options.component_names = component_names_input.value
user_options.format_options.unique_names = unique_names_input.value
user_options.format_options.remove_numeric_suffixes = remove_numeric_input.value
user_options.format_options.include_material = include_material_input.value
user_options.format_options.units = unit_input.selectedItem.name
@report_errors
def run(_context: dict):
app = adsk.core.Application.get()
ui = app.userInterface
cmd = ui.commandDefinitions.addButtonDefinition(
COMMAND_ID, COMMAND_NAME,
'Export a cutlist file for the bodies in selected components',
'.//resources')
create_handler = CutlistCommandCreatedEventHandler()
cmd.commandCreated.add(create_handler)
handlers.append(create_handler)
panel = ui.allToolbarPanels.itemById('MakePanel')
panel.controls.addCommand(cmd)
@report_errors
def stop(_context: dict):
app = adsk.core.Application.get()
ui = app.userInterface
cmd = ui.commandDefinitions.itemById(COMMAND_ID)
if cmd:
cmd.deleteMe()
panel = ui.allToolbarPanels.itemById('MakePanel')
button = panel.controls.itemById(COMMAND_ID)
if button:
button.deleteMe()