-
Notifications
You must be signed in to change notification settings - Fork 1
/
Export.py
359 lines (311 loc) · 13.1 KB
/
Export.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
import os
import json
from adsk.core import *
import traceback
import tempfile
from shutil import copyfile
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), 'libs'))
import Foundation
USER_PARAM = 'exporter_stl'
EXPORT_COMMAND_KEY = 'ExportDesign'
SETTINGS_COMMAND_KEY = 'SettingsExportDesign'
keepHandlers = []
deleters = []
def find_entity_from_path(path, component, ui):
if len(path) > 1:
if component.objectType == 'adsk::fusion::Component':
child = component.occurrences.itemByName(path[0])
else:
child = component.childOccurrences.itemByName(path[0])
if child:
return find_entity_from_path(path[1:], child, ui)
elif len(path) == 1:
return component.bRepBodies.itemByName(path[0])
return None
def get_path_for_entity(entity):
chain = [entity.name]
obj = entity
while True:
obj = obj.assemblyContext
if obj:
chain.append(obj.name)
else:
break
return list(reversed(chain))
def select_output_file(ui):
dialog = ui.createFileDialog()
dialog.title = 'Export STL file'
dialog.filter = 'STL files (*.stl)'
dialog.initialFilename = 'out'
accessible = dialog.showSave()
if accessible == DialogResults.DialogOK:
return dialog.filename
def handle(handler, clazz):
class Handler(clazz):
def notify(self, args):
try:
handler(args)
except:
Application.get().userInterface.messageBox('Failed:\n{}'.format(traceback.format_exc()))
handler_instance = Handler()
keepHandlers.append(handler_instance)
return handler_instance
def get_export_list():
attributes = Application.get().activeProduct.findAttributes('nraynaud-Export', 'export')
result = []
for attr in attributes:
body = attr.parent
content = json.loads(attr.value)
if body:
for entry in content:
result.append({'body': body, 'file': entry['file']})
return result
def create_setting_panel(args):
app = Application.get()
ui = app.userInterface
design = app.activeProduct
cmd = Command.cast(args.command)
shadow_table = []
deletion_table = []
shadow_dict = {}
current_file = None
def select_row(new_row):
nonlocal current_file
set_detail_visibility(True)
body = new_row.get('body')
if body:
selection_input.addSelection(body)
file_input.text = str(new_row.get('file'))
current_file = new_row.get('file')
def save_if_you_can():
if selection_input.selectionCount and current_file is not None:
if table.selectedRow == -1:
row_index = table.rowCount
row_id = 'TableInput_string{}'.format(row_index)
body = selection_input.selection(0).entity
shadow_row = {'id': row_id, 'file': current_file, 'body': body, 'new': True}
shadow_table.append(shadow_row)
shadow_dict[row_id] = shadow_row
string_input = table.commandInputs.addStringValueInput(row_id, 'String', current_file)
string_input.isReadOnly = True
shadow_row['input'] = string_input
table.addCommandInput(string_input, row_index, 0)
table.selectedRow = row_index
else:
shadow_row = shadow_table[table.selectedRow]
# adding the old row to deletion list will remove the attribute from the element
deletion_table.append(shadow_row.copy())
shadow_row['file'] = current_file
shadow_row['body'] = selection_input.selection(0).entity
def set_detail_visibility(visible):
selection_input.clearSelection()
if visible:
selection_input.isVisible = True
file_input.isVisible = True
else:
selection_input.isVisible = False
file_input.isVisible = False
def handle_input_change(args):
nonlocal current_file
event_args = InputChangedEventArgs.cast(args)
cmd_input = event_args.input
if cmd_input.id == 'selection':
save_if_you_can()
if cmd_input.id == 'selectFile':
file_name = select_output_file(ui)
if file_name:
current_file = file_name
cmd_input.text = file_name
save_if_you_can()
if cmd_input.id == 'addExport':
table.selectedRow = -1
remove_export.isEnabled = False
current_file = None
file_input.text = 'select file'
set_detail_visibility(True)
if cmd_input.id == 'removeExport':
shadow_row = shadow_table[table.selectedRow]
deletion_table.append(shadow_row)
del shadow_table[table.selectedRow]
del shadow_dict[shadow_row['id']]
table.deleteRow(table.selectedRow)
table.selectedRow = -1
remove_export.isEnabled = False
current_file = None
file_input.text = 'select file'
set_detail_visibility(False)
else:
shadow_row = shadow_dict.get(cmd_input.id)
if shadow_row:
select_row(shadow_row)
remove_export.isEnabled = True
inputs = cmd.commandInputs
table = inputs.addTableCommandInput('sampleTable', 'Table', 2, '1:1')
add_export = inputs.addBoolValueInput('addExport', 'Add Export', False, '', True)
add_export.text = '+'
add_export.tooltip = 'Add an export'
table.addToolbarCommandInput(add_export)
remove_export = inputs.addBoolValueInput('removeExport', 'Remove Export', False, '', True)
remove_export.text = '-'
remove_export.tooltip = "Don't export this anymore"
remove_export.isEnabled = False
table.addToolbarCommandInput(remove_export)
cmd.okButtonText = 'Ok Export'
selection_input = inputs.addSelectionInput('selection', 'Body', 'Basic select command input')
selection_input.addSelectionFilter('Bodies')
# allow 0 selection so that it doesn't invalidate the entire command if empty
selection_input.setSelectionLimits(0, 1)
file_input = inputs.addBoolValueInput('selectFile', 'File', False, '', True)
file_input.text = 'select file'
set_detail_visibility(False)
def on_activation(args):
exports = get_export_list()
index = 0
for export in exports:
file_path = decode_bookmark(export['file'])
row_id = 'TableInput_string{}'.format(index)
shadow_row = {'id': row_id, 'file': file_path, 'body': export['body']}
shadow_table.append(shadow_row)
shadow_dict[shadow_row['id']] = shadow_row
string_input = table.commandInputs.addStringValueInput(row_id, 'String', file_path)
shadow_row['input'] = string_input
string_input.isReadOnly = True
table.addCommandInput(string_input, index, 0)
index += 1
def add_or_replace_attribute(element, new_value):
attr = element.attributes.itemByName('nraynaud-Export', 'export')
if attr:
previous_content = list(json.loads(attr.value))
if not next((e for e in previous_content if e['file'] == new_value['file']), None):
previous_content.append(new_value)
attr.value = json.dumps(previous_content)
else:
element.attributes.add('nraynaud-Export', 'export', str(json.dumps([new_value])))
def on_execution(args):
for row in deletion_table:
file_name = row['file']
body = row['body']
the_bytes = get_bookmark_bytes(file_name)
attr = body.attributes.itemByName('nraynaud-Export', 'export')
if attr:
previous_content = json.loads(attr.value)
left_content = [e for e in previous_content if e['file'] != the_bytes]
if len(left_content):
attr.value = json.dumps(left_content)
else:
attr.deleteMe()
for row in shadow_table:
file_name = row['file']
body = row['body']
the_bytes = get_bookmark_bytes(file_name)
need_save = True
if not the_bytes:
export_stl(design, body, file_name)
the_bytes = get_bookmark_bytes(file_name)
need_save = False
add_or_replace_attribute(body, {'file': the_bytes, 'type': 'stl'})
if need_save:
export_to_bookmark(the_bytes, design, body)
cmd.inputChanged.add(handle(handle_input_change, InputChangedEventHandler))
cmd.activate.add((handle(on_activation, CommandEventHandler)))
cmd.execute.add(handle(on_execution, CommandEventHandler))
def get_bookmark_bytes(file_name):
url = Foundation.NSURL.alloc().initFileURLWithPath_(file_name)
bookmark, error = url.bookmarkDataWithOptions_includingResourceValuesForKeys_relativeToURL_error_(
Foundation.NSURLBookmarkCreationWithSecurityScope,
None,
None,
None)
if error:
# that's the error code if the file is not on disk yet.
if error.code() == 260:
return None
raise Exception(error.localizedDescription())
# base64 encode the bookmark so it can be jsonified
return bookmark.base64EncodedStringWithOptions_(0)
def decode_bookmark(bookmark, access_action=None):
nsdata = Foundation.NSData.alloc().initWithBase64EncodedString_options_(bookmark, 0)
url, is_stale, error = Foundation.NSURL.URLByResolvingBookmarkData_options_relativeToURL_bookmarkDataIsStale_error_(
nsdata, Foundation.NSURLBookmarkResolutionWithSecurityScope, None, None, None)
if error:
raise Exception(error.localizedDescription())
file_path = url.path()
if access_action:
accessible = url.startAccessingSecurityScopedResource()
if accessible:
try:
return access_action(file_path)
finally:
url.stopAccessingSecurityScopedResource()
else:
return file_path
def export_to_bookmark(bookmark, design, body):
return decode_bookmark(bookmark, lambda file_path: export_stl(design, body, file_path))
def export_all_files(args):
exports = get_export_list()
design = Application.get().activeProduct
for export in exports:
export_to_bookmark(export['file'], design, export['body'])
def export_stl(design, body, file_path):
# using the temp dir, for some reasons exportManager tries to access the directory
# surrounding the output file
with tempfile.NamedTemporaryFile(suffix='.stl') as temp_file:
export_manager = design.exportManager
options = export_manager.createSTLExportOptions(body, temp_file.name)
options.sendToPrintUtility = False
options.meshRefinement = 0
export_manager.execute(options)
temp_file.seek(0)
copyfile(temp_file.name, file_path)
return
def run(context):
app = Application.get()
ui = app.userInterface
try:
cmd_defs = ui.commandDefinitions
export_command = ui.commandDefinitions.itemById(EXPORT_COMMAND_KEY)
if export_command:
export_command.deleteMe()
export_command = cmd_defs.addButtonDefinition(EXPORT_COMMAND_KEY, 'Export STL',
'Export the pre-determined STL file',
'exportIcon')
settings_command = ui.commandDefinitions.itemById(SETTINGS_COMMAND_KEY)
if settings_command:
settings_command.deleteMe()
settings_command = cmd_defs.addButtonDefinition(SETTINGS_COMMAND_KEY, 'Configure STL export',
'Configure STL export',
'configureExportIcon')
export_command.commandCreated.add(handle(export_all_files, CommandCreatedEventHandler))
settings_command.commandCreated.add(handle(create_setting_panel, CommandCreatedEventHandler))
deleters.append(
replace_existing_control(ui.allToolbarPanels.itemById('SolidMakePanel').controls, settings_command))
deleters.append(replace_existing_control(ui.toolbars.itemById('QAT').controls, export_command))
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def delete_control(controls, command_key):
control = controls.itemById(command_key)
if control:
control.deleteMe()
def replace_existing_control(controls, new_command):
delete_control(controls, new_command.id)
if new_command:
control = controls.addCommand(new_command)
return lambda: control.deleteMe()
return lambda: None
def stop(context):
ui = None
try:
app = Application.get()
ui = app.userInterface
button = ui.commandDefinitions.itemById(EXPORT_COMMAND_KEY)
if button:
button.deleteMe()
global keepHandlers
keepHandlers = []
for obj in deleters:
obj()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))