-
Notifications
You must be signed in to change notification settings - Fork 8
/
ExportCutlist.py
362 lines (278 loc) · 13.1 KB
/
ExportCutlist.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
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
#Author-Billy Keyes
#Description-Export a cut list of all bodies as a JSON file
import functools
import io
import traceback
from operator import attrgetter
from collections import namedtuple
import adsk.core
import adsk.fusion
from .lib.format import ALL_FORMATS, TableFormat, CSVFormat, get_format
from .lib.geometry.bodies import MinimalBody, get_minimal_body
GroupBy = namedtuple('GroupBy', ['dimensions', 'material'])
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 = []
# remember user options in between creations of the command
preferences = {
'ignoreHidden': True,
'ignoreExternal': False,
'groupBy': DEFAULT_GROUPBY,
'format': TableFormat.name,
'unit': DEFAULT_UNIT,
'axisAligned': False,
'tolerance': DEFAULT_TOLERANCE,
}
def report_errors(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
app = adsk.core.Application.get()
app.userInterface.messageBox('Failed:\n{}'.format(traceback.format_exc()))
return wrapper
class Dimensions:
tolerance = DEFAULT_TOLERANCE
@classmethod
def from_body(cls, body: MinimalBody):
bbox = body.boundingBox
x = bbox.maxPoint.x - bbox.minPoint.x
y = bbox.maxPoint.y - bbox.minPoint.y
z = bbox.maxPoint.z - bbox.minPoint.z
return cls(x, y, z)
def __init__(self, x, y, z):
ordered = tuple(sorted((x, y, z)))
self.length = ordered[2]
self.width = ordered[1]
self.height = ordered[0]
def __eq__(self, other):
if isinstance(other, Dimensions):
return (abs(self.length - other.length) < Dimensions.tolerance and
abs(self.width - other.width) < Dimensions.tolerance and
abs(self.height - other.height) < Dimensions.tolerance)
return NotImplemented
class CutListItem:
def __init__(self, body: MinimalBody, name: str):
self.names = [name]
self.dimensions = Dimensions.from_body(body)
self.material = body.material.name
@property
def count(self):
return len(self.names)
def matches(self, other, group: GroupBy):
if isinstance(other, CutListItem):
other_dimensions = other.dimensions
other_material = other.material
elif isinstance(other, MinimalBody):
other_dimensions = Dimensions.from_body(other)
other_material = other.material.name
else:
return False
if group.dimensions:
if self.dimensions != other_dimensions:
return False
if group.material and self.material != other_material:
return False
return True
return False
def add_instance(self, name):
self.names.append(name)
class CutList:
def __init__(self, group: GroupBy, ignorehidden=False, ignoreexternal=False, axisaligned=False, namesep='/'):
self.items: list[CutListItem] = []
self.group = group
self.ignorehidden = ignorehidden
self.ignoreexternal = ignoreexternal
self.axisaligned = axisaligned
self.namesep = namesep
def add_body(self, body: adsk.fusion.BRepBody, name: str):
if not body.isSolid:
return
if not body.isVisible and self.ignorehidden:
return
if self.axisaligned:
minimal_body = MinimalBody(body)
else:
minimal_body = get_minimal_body(body)
added = False
for item in self.items:
if item.matches(minimal_body, self.group):
item.add_instance(name)
added = True
break
if not added:
item = CutListItem(minimal_body, name)
self.items.append(item)
def add(self, obj, name=None):
if isinstance(obj, adsk.fusion.BRepBody):
self.add_body(obj, self._joinname(name, obj.name))
elif isinstance(obj, adsk.fusion.Occurrence):
if obj.isReferencedComponent and self.ignoreexternal:
return
for body in obj.bRepBodies:
self.add_body(body, self._joinname(name, obj.component.name, body.name))
for child in obj.childOccurrences:
self.add(child, self._joinname(name, obj.component.name))
elif isinstance(obj, adsk.fusion.Component):
for body in obj.bRepBodies:
self.add_body(body, self._joinname(name, obj.name, body.name))
for occ in obj.occurrences:
self.add(occ, self._joinname(name, obj.name))
else:
raise ValueError(f'Cannot add object with type: {obj.objectType}')
def sorted_items(self):
items = list(self.items)
items.sort(key=lambda i: attrgetter('length', 'width', 'height')(i.dimensions), reverse=True)
items.sort(key=attrgetter('count'), reverse=True)
items.sort(key=attrgetter('material'))
return items
def _joinname(self, *parts):
return self.namesep.join([p for p in parts if p])
class CutlistCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
@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 False
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
cmd = eventArgs.command
inputs = cmd.commandInputs
selectInput = inputs.addSelectionInput('selection', 'Selection', 'Select body or component')
selectInput.tooltip = 'Select bodies or components to export.'
selectInput.addSelectionFilter('SolidBodies')
selectInput.addSelectionFilter('Occurrences')
selectInput.setSelectionLimits(0)
hiddenInput = inputs.addBoolValueInput('hidden', 'Ignore hidden', True, '', preferences['ignoreHidden'])
hiddenInput.tooltip = 'If checked, hidden bodies are excluded from the cutlist.'
externalInput = inputs.addBoolValueInput('external', 'Ignore external', True, '', preferences['ignoreExternal'])
externalInput.tooltip = 'If checked, external components are excluded from the cutlist.'
formatInput = inputs.addDropDownCommandInput('format', 'Output format', adsk.core.DropDownStyles.LabeledIconDropDownStyle)
formatInput.tooltip = 'The output format of the cutlist.'
for fmt in ALL_FORMATS:
formatInput.listItems.add(fmt.name, preferences['format'] == fmt.name, '')
groupingGroup = inputs.addGroupCommandInput('grouping', 'Group By')
groupingGroup.isEnabledCheckBoxDisplayed = False
groupingGroup.isExpanded = True
dimensionsInput = groupingGroup.children.addBoolValueInput('group_dimensions', 'Dimensions', True, '', preferences['groupBy'].dimensions)
dimensionsInput.tooltip = 'If checked, group bodies by their dimensions.'
materialInput = groupingGroup.children.addBoolValueInput('group_material', 'Material', True, '', preferences['groupBy'].material)
materialInput.tooltip = 'If checked, group bodies by their material.'
materialInput.tooltipDescription = 'This option is only used when also grouping bodies by their dimensions.'
materialInput.isEnabled = dimensionsInput.value
advancedGroup = inputs.addGroupCommandInput('advanced', 'Advanced Options')
advancedGroup.isEnabledCheckBoxDisplayed = False
advancedGroup.isExpanded = False
unitInput = advancedGroup.children.addDropDownCommandInput('unit', 'Output unit', adsk.core.DropDownStyles.LabeledIconDropDownStyle)
unitInput.tooltip = 'Units for output dimensions'
for unit in ALL_UNITS:
unitInput.listItems.add(unit, preferences['unit'] == unit, '')
axisAlignedInput = advancedGroup.children.addBoolValueInput('axisaligned', 'Use axis-aligned boxes', True, '', preferences['axisAligned'])
axisAlignedInput.tooltip = 'If checked, use axis-algined bounding boxes.'
axisAlignedInput.tooltipDescription = 'This disables the rotation heuristic and assumes parts are already in the ideal orientation relative to the X, Y, and Z axes.'
toleranceInput = advancedGroup.children.addValueInput('tolerance', 'Tolerance', 'mm', adsk.core.ValueInput.createByReal(preferences['tolerance']))
toleranceInput.tooltip = 'The tolerance used when matching bounding box dimensions.'
onExecute = CutlistCommandExecuteHandler()
cmd.execute.add(onExecute)
handlers.append(onExecute)
onInputChanged = CutlistCommandInputChangedHandler()
cmd.inputChanged.add(onInputChanged)
handlers.append(onInputChanged)
class CutlistCommandInputChangedHandler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
eventArgs = adsk.core.InputChangedEventArgs.cast(args)
changedInput = eventArgs.input
if changedInput.id == 'group_dimensions':
inputs = eventArgs.firingEvent.sender.commandInputs
materialInput = inputs.itemById('group_material')
materialInput.isEnabled = changedInput.value
class CutlistCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
@report_errors
def notify(self, args):
eventArgs = adsk.core.CommandEventArgs.cast(args)
inputs = eventArgs.command.commandInputs
app = adsk.core.Application.get()
ui = app.userInterface
doc = app.activeDocument
design = adsk.fusion.Design.cast(app.activeProduct)
set_preferences_from_inputs(inputs)
if preferences['tolerance'] > 0:
Dimensions.tolerance = preferences['tolerance']
cutlist = CutList(
preferences['groupBy'],
ignorehidden=preferences['ignoreHidden'],
ignoreexternal=preferences['ignoreExternal'],
axisaligned=preferences['axisAligned'],
)
selectionInput = inputs.itemById('selection')
for i in range(selectionInput.selectionCount):
cutlist.add(selectionInput.selection(i).entity)
fmt_class = get_format(preferences['format'])
fmt = fmt_class(design.unitsManager, doc.name, units=preferences['unit'])
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_preferences_from_inputs(inputs: adsk.core.CommandInputs):
hiddenInput: adsk.core.BoolValueCommandInput = inputs.itemById('hidden')
externalInput: adsk.core.BoolValueCommandInput = inputs.itemById('external')
formatInput: adsk.core.DropDownCommandInput = inputs.itemById('format')
axisAlignedInput: adsk.core.BoolValueCommandInput = inputs.itemById('axisaligned')
toleranceInput: adsk.core.ValueCommandInput = inputs.itemById('tolerance')
unitInput: adsk.core.DropDownCommandInput = inputs.itemById('unit')
dimensionsInput: adsk.core.BoolValueCommandInput = inputs.itemById('group_dimensions')
materialInput: adsk.core.BoolValueCommandInput = inputs.itemById('group_material')
group = GroupBy(dimensions=dimensionsInput.value, material=materialInput.value)
preferences['ignoreHidden'] = hiddenInput.value
preferences['ignoreExternal'] = externalInput.value
preferences['groupBy'] = group
preferences['format'] = formatInput.selectedItem.name
preferences['axisAligned'] = axisAlignedInput.value
preferences['tolerance'] = toleranceInput.value
preferences['unit'] = unitInput.selectedItem.name
@report_errors
def run(context):
app = adsk.core.Application.get()
ui = app.userInterface
cmdDef = ui.commandDefinitions.addButtonDefinition(
COMMAND_ID, COMMAND_NAME,
'Export a cutlist file for the bodies in selected components',
'.//resources')
onCreate = CutlistCommandCreatedEventHandler()
cmdDef.commandCreated.add(onCreate)
handlers.append(onCreate)
makePanel = ui.allToolbarPanels.itemById('MakePanel')
makePanel.controls.addCommand(cmdDef)
@report_errors
def stop(context):
app = adsk.core.Application.get()
ui = app.userInterface
cmdDef = ui.commandDefinitions.itemById(COMMAND_ID)
if cmdDef:
cmdDef.deleteMe()
makePanel = ui.allToolbarPanels.itemById('MakePanel')
button = makePanel.controls.itemById(COMMAND_ID)
if button:
button.deleteMe()