forked from aconz2/Fusion360Exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exporter.py
368 lines (293 loc) · 12 KB
/
Exporter.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
363
364
365
366
367
368
import adsk.core
import traceback
from pathlib import Path
from datetime import datetime
from typing import NamedTuple, List, Set
from enum import Enum
from dataclasses import dataclass
import hashlib
import re
log_file = None
log_fh = None
handlers = []
def log(*args):
print(*args, file=log_fh)
log_fh.flush()
def init_directory(name):
directory = Path(name)
directory.mkdir(exist_ok=True)
return directory
def init_logging(directory):
global log_file, log_fh
log_file = directory / '{:%Y_%m_%d_%H_%M}.txt'.format(datetime.now())
log_fh = open(log_file, 'w')
class Format(Enum):
F3D = 'f3d'
STEP = 'step'
STL = 'stl'
IGES = 'igs'
SAT = 'sat'
SMT = 'smt'
TMF = '3mf' # must start with letter
FormatFromName = {x.value: x for x in Format}
DEFAULT_SELECTED_FORMATS = {Format.TMF}
class Ctx(NamedTuple):
folder: Path
formats: List[Format]
app: adsk.core.Application
projects: Set[str]
unhide_all: bool
save_sketches: bool
def extend(self, other):
return self._replace(folder=self.folder / other)
class LazyDocument:
def __init__(self, ctx, file):
self._ctx = ctx
self._file = file
self._document = None
def open(self):
if self._document is not None:
return
log(f'Opening `{self._file.name}`')
self._document = self._ctx.app.documents.open(self._file)
self._document.activate()
if self._ctx.unhide_all:
unhide_all_in_document(self._document)
def close(self):
if self._document is None:
return
log(f'Closing {self._file.name}')
self._document.close(False) # don't save changes
@property
def design(self):
return design_from_document(self._document)
@property
def rootComponent(self):
return self.design.rootComponent
@dataclass
class Counter:
saved: int = 0
skipped: int = 0
errored: int = 0
def __add__(self, other):
return Counter(
self.saved + other.saved,
self.skipped + other.skipped,
self.errored + other.errored,
)
def __iadd__(self, other):
self.saved += other.saved
self.skipped += other.skipped
self.errored += other.errored
return self
def design_from_document(document: adsk.core.Document):
return adsk.fusion.FusionDocument.cast(document).design
def unhide_all_in_document(document: adsk.core.Document):
unhide_all_in_component(design_from_document(document).rootComponent)
def unhide_all_in_component(component):
component.isBodiesFolderLightBulbOn = True
component.isSketchFolderLightBulbOn = True
for brep in component.bRepBodies:
brep.isLightBulbOn = True
for body in component.meshBodies:
body.isLightBulbOn = True
# I find the name occurrences very confusing, but apparently that is what a sub-component is called
for occurrence in component.occurrences:
occurrence.isLightBulbOn = True
unhide_all_in_component(occurrence.component)
def sanitize_filename(name: str) -> str:
"""
Remove "bad" characters from a filename. Right now just punctuation that Windows doesn't like
If any chars are removed, we append _{hash} so that we don't accidentally clobber other files
since eg `Model 1/2` and `Model 1 2` would otherwise have the same name
"""
# this list of characters is just from trying to rename a file in Explorer (on Windows)
# I think the actual requirements are per fileystem and will be different on Mac
# I'm not sure how other unicode chars are handled
with_replacement = re.sub(r'[:\\/*?<>|]', ' ', name)
if name == with_replacement:
return name
log(f'filename `{name}` contained bad chars, replacing by `{with_replacement}`')
hash = hashlib.sha256(name.encode()).hexdigest()[:8]
return f'{with_replacement}_{hash}'
def export_filename(ctx: Ctx, format: Format, file):
sanitized = sanitize_filename(file.name)
name = f'{sanitized}_v{file.versionNumber}.{format.value}'
return ctx.folder / name
def export_sketches(ctx, component):
counter = Counter()
for sketch in component.sketches:
output_path = ctx.folder / f'{sanitize_filename(sketch.name)}.dxf'
if output_path.exists():
log(f'{output_path} already exists, skipping')
counter.skipped += 1
else:
log(f'Exporting sketch {sketch.name} in {component.name} to {output_path}')
try:
output_path.parent.mkdir(exist_ok=True, parents=True)
sketch.saveAsDXF(str(output_path))
counter.saved += 1
except Exception:
log(traceback.format_exc())
counter.errored += 1
for occurrence in component.occurrences:
counter += export_sketches(ctx.extend(sanitize_filename(occurrence.name)), occurrence.component)
return counter
def export_file(ctx: Ctx, format: Format, file, doc: LazyDocument) -> Counter:
output_path = export_filename(ctx, format, file)
if output_path.exists():
log(f'{output_path} already exists, skipping')
return Counter(skipped=1)
doc.open()
# I'm just taking this from here https://github.com/tapnair/apper/blob/master/apper/Fusion360Utilities.py
# is there a nicer way to do this??
design = doc.design
em = design.exportManager
output_path.parent.mkdir(exist_ok=True, parents=True)
# leaving this ugly, not sure what else there might be to handle per format
if format == Format.F3D:
options = em.createFusionArchiveExportOptions(str(output_path))
elif format == Format.STL:
options = em.createSTLExportOptions(design.rootComponent, str(output_path))
elif format == Format.TMF:
options = em.createC3MFExportOptions(design.rootComponent, str(output_path))
elif format == Format.STEP:
options = em.createSTEPExportOptions(str(output_path))
elif format == Format.IGES:
options = em.createIGESExportOptions(str(output_path))
elif format == Format.SAT:
options = em.createSATExportOptions(str(output_path))
elif format == Format.SMT:
options = em.createSMTExportOptions(str(output_path))
else:
raise Exception(f'Got unknown export format {format}')
em.execute(options)
log(f'Saved {output_path}')
return Counter(saved=1)
def visit_file(ctx: Ctx, file) -> Counter:
log(f'Visiting file {file.name} v{file.versionNumber} . {file.fileExtension}')
if file.fileExtension != 'f3d':
log(f'file {file.name} has extension {file.fileExtension} which is not currently handled, skipping')
return Counter(skipped=1)
doc = LazyDocument(ctx, file)
counter = Counter()
if ctx.save_sketches:
doc.open()
counter += export_sketches(ctx.extend(sanitize_filename(doc.rootComponent.name)), doc.rootComponent)
for format in ctx.formats:
try:
counter += export_file(ctx, format, file, doc)
except Exception:
counter.errored += 1
log(traceback.format_exc())
doc.close()
return counter
def visit_folder(ctx: Ctx, folder) -> Counter:
log(f'Visiting folder {folder.name}')
new_ctx = ctx.extend(sanitize_filename(folder.name))
counter = Counter()
for file in folder.dataFiles:
try:
counter += visit_file(new_ctx, file)
except Exception:
log(f'Got exception visiting file\n{traceback.format_exc()}')
counter.errored += 1
for sub_folder in folder.dataFolders:
counter += visit_folder(new_ctx, sub_folder)
return counter
def main(ctx: Ctx) -> Counter:
init_directory(ctx.folder)
init_logging(ctx.folder)
counter = Counter()
for project in ctx.app.data.dataProjects:
if project.name in ctx.projects:
counter += visit_folder(ctx, project.rootFolder)
return counter
class ExporterCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
def notify(self, args):
try:
cmd = args.command
cmd.setDialogInitialSize(600, 400)
# http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-C1BF7FBF-6D35-4490-984B-11EB26232EAD
cmd.isExecutedWhenPreEmpted = False
onExecute = ExporterCommandExecuteHandler()
cmd.execute.add(onExecute)
onDestroy = ExporterCommandDestroyHandler()
cmd.destroy.add(onDestroy)
handlers.append(onExecute)
handlers.append(onDestroy)
inputs = cmd.commandInputs
inputs.addStringValueInput('directory', 'Directory', str(Path.home() / 'Desktop/Fusion360Export'))
drop = inputs.addDropDownCommandInput('file_types', 'Export Types', adsk.core.DropDownStyles.CheckBoxDropDownStyle)
for format in Format:
drop.listItems.add(format.value, format in DEFAULT_SELECTED_FORMATS)
drop = inputs.addDropDownCommandInput('projects', 'Export Projects', adsk.core.DropDownStyles.CheckBoxDropDownStyle)
for project in adsk.core.Application.get().data.dataProjects:
drop.listItems.add(project.name, False)
inputs.addBoolValueInput('unhide_all', 'Unhide All Bodies', True, '', True)
inputs.addBoolValueInput('save_sketches', 'Save Sketches as DXF', True, '', False)
except:
adsk.core.Application.get().userInterface.messageBox(traceback.format_exc())
class ExporterCommandDestroyHandler(adsk.core.CommandEventHandler):
def notify(self, args):
try:
adsk.terminate()
except:
adsk.core.Application.get().userInterface.messageBox(traceback.format_exc())
# Dont use yield and don't copy list items, swig wants to delete things
def selected(inputs):
return [it.name for it in inputs if it.isSelected]
class ExporterCommandExecuteHandler(adsk.core.CommandEventHandler):
def notify(self, args):
try:
inputs = args.command.commandInputs
app = adsk.core.Application.get()
ui = app.userInterface
ctx = Ctx(
app = app,
folder = Path(inputs.itemById('directory').value),
formats = [FormatFromName[x] for x in selected(inputs.itemById('file_types').listItems)],
projects = set(selected(inputs.itemById('projects').listItems)),
unhide_all = inputs.itemById('unhide_all').value,
save_sketches = inputs.itemById('save_sketches').value,
)
counter = main(ctx)
ui.messageBox('\n'.join((
f'Saved {counter.saved} files',
f'Skipped {counter.skipped} files',
f'Encountered {counter.errored} errors',
f'Log file is at {log_file}'
)))
except:
tb = traceback.format_exc()
adsk.core.Application.get().userInterface.messageBox(f'Log file is at {log_file}\n{tb}')
if log_fh is not None:
log(f'Got top level exception\n{tb}')
finally:
if log_fh is not None:
log_fh.close()
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
cmd_defs = ui.commandDefinitions
CMD_DEF_ID = 'aconz2_Exporter'
cmd_def = cmd_defs.itemById(CMD_DEF_ID)
# This isn't how all the other demo scripts manage the lifecycle, but if we don't delete the old
# command then we get double inputs when we run a second time
if cmd_def:
cmd_def.deleteMe()
cmd_def = cmd_defs.addButtonDefinition(
CMD_DEF_ID,
'Export all the things',
'Tooltip',
)
cmd_created = ExporterCommandCreatedEventHandler()
cmd_def.commandCreated.add(cmd_created)
handlers.append(cmd_created)
cmd_def.execute()
adsk.autoTerminate(False)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))