-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFilteredExportStp.py
236 lines (180 loc) · 8.23 KB
/
FilteredExportStp.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
import adsk.core
import adsk.fusion
import traceback
import os.path
from .FilteredExportUtil import getComponents
from .FilteredExportUtil import renderResultMessage
from .FilteredExportUtil import FilteredExportResult
from .Fusion360Utilities.Fusion360Utilities import AppObjects
from .Fusion360Utilities.Fusion360CommandBase import Fusion360CommandBase
# Faked statics for easy code maintainance
S_CPY_SELECTION_LOOKUP='cpySelection'
S_CPY_FILTER_TYPE_LOOKUP = 'cpyDropDownFilterType'
S_CPY_FILTER_TYPET_TOP_LEVEL = 'Top level'
S_CPY_FILTER_TYPE_LEAVES = 'Leaves'
S_CPY_FILTER_TYPE_MIXED_LEAVES = 'Mixed leaves'
#
# get path via dialog
#
def getPath(appObjects):
# return value
exportPath = ''
# create dialog
folderDialog = appObjects.ui.createFolderDialog()
folderDialog.title = 'Export Folder'
# open dialog
dialogResult = folderDialog.showDialog()
# check if user finished the dialog by pressing okay
if dialogResult == adsk.core.DialogResults.DialogOK:
exportPath = str.format(folderDialog.folder)
else:
# user canceled the dialog
raise ValueError('No export path defined.')
# return formated path
return exportPath
#
# Export top level or selected components
#
def exportTopLevelMode(appObjects, input_values):
# get root component
rootComponent = appObjects.design.rootComponent
# get component list
components = []
# selection available?
if S_CPY_SELECTION_LOOKUP in input_values:
if input_values[S_CPY_SELECTION_LOOKUP][0] == rootComponent:
# process all components
components = getComponents(rootComponent.occurrences, components, False, False)
else:
# process selcted components
components = getComponents(input_values[S_CPY_SELECTION_LOOKUP], components, False, False)
else:
# process all components
components = getComponents(rootComponent.occurrences, components, False, False)
# list of processed file names
processedComponents = []
# get target folder
# documentFolder = appObjects.document.dataFile.parentFolder
# get export path
exportPath = getPath(appObjects)
# export all components
for component in components:
fullFileName = os.path.join(exportPath, component.name)
stpExportOptions = appObjects.export_manager.createSTEPExportOptions(fullFileName, component)
appObjects.export_manager.execute(stpExportOptions)
processedComponents.append(component.name)
# return resulting lists
return FilteredExportResult(exportPath, processedComponents, '')
#
# Export top level or selected components
#
def exportLeaveMode(appObjects, input_values):
# get root component
rootComponent = appObjects.design.rootComponent
# get component list
components = []
# selection available?
if S_CPY_SELECTION_LOOKUP in input_values:
# process selcted components
components = getComponents(input_values[S_CPY_SELECTION_LOOKUP], components, True, False)
else:
# process all components
components = getComponents(rootComponent.occurrences, components, True, False)
# list of processed and skipped file names
processedComponents = []
skippedComponents = []
# get target folder
# documentFolder = appObjects.document.dataFile.parentFolder
# get export path
exportPath = getPath(appObjects)
for component in components:
if component.bRepBodies and not component.occurrences:
# export leave component (contains bodies but no other components)
fullFileName = os.path.join(exportPath, component.name)
stpExportOptions = appObjects.export_manager.createSTEPExportOptions(fullFileName, component)
appObjects.export_manager.execute(stpExportOptions)
processedComponents.append(component.name)
elif component.bRepBodies and component.occurrencesByComponent:
# skip export because this component contains bodies and components
skippedComponents.append(component.name)
# return resulting lists
return FilteredExportResult(exportPath, processedComponents, skippedComponents)
#
# Export top mixed level or selected components
#
def exportMixedLeaveMode(appObjects, input_values):
# get root component
rootComponent = appObjects.design.rootComponent
# get component list
components = []
# selection available?
if S_CPY_SELECTION_LOOKUP in input_values:
# process selcted components
components = getComponents(input_values[S_CPY_SELECTION_LOOKUP], components, True, False)
else:
# process all components
components = getComponents(rootComponent.occurrences, components, True, False)
# list of processed and skipped file names
processedComponents = []
# get target folder
# documentFolder = appObjects.document.dataFile.parentFolder
# get export path
exportPath = getPath(appObjects)
for component in components:
fullFileName = os.path.join(exportPath, component.name)
stpExportOptions = appObjects.export_manager.createSTEPExportOptions(fullFileName, component)
appObjects.export_manager.execute(stpExportOptions)
processedComponents.append(component.name)
# return resulting lists
return FilteredExportResult(exportPath, processedComponents, '')
#
# Save copy as a collection of bodies
#
class FilteredExportStp(Fusion360CommandBase):
# Run when the user presses OK
def on_execute(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs, args, input_values):
try:
appObjects = AppObjects()
# no design? Nothing to do
if not appObjects.design:
raise ValueError('No active Fusion design', 'No Design')
# export components
exportResult = None
if input_values[S_CPY_FILTER_TYPE_LOOKUP] == S_CPY_FILTER_TYPET_TOP_LEVEL:
# export top level mode
exportResult = exportTopLevelMode(appObjects, input_values)
elif input_values[S_CPY_FILTER_TYPE_LOOKUP] == S_CPY_FILTER_TYPE_LEAVES:
# export leave mode
exportResult = exportLeaveMode(appObjects, input_values)
elif input_values[S_CPY_FILTER_TYPE_LOOKUP] == S_CPY_FILTER_TYPE_MIXED_LEAVES:
# export mixed leave mode
exportResult = exportMixedLeaveMode(appObjects, input_values)
# show result list
appObjects.ui.messageBox(renderResultMessage(exportResult))
except ValueError as e:
if appObjects.ui:
appObjects.ui.messageBox(str(e))
except:
if appObjects.ui:
appObjects.ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Run when the user selects your command icon from the Fusion 360 UI
def on_create(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs):
# Select objects to process
selectionCommand = inputs.addSelectionInput(S_CPY_SELECTION_LOOKUP, 'Select Components', '')
selectionCommand.setSelectionLimits(0)
selectionCommand.addSelectionFilter('Occurrences')
# Filter type (Top level, )
dropDownCpyFilterType = inputs.addDropDownCommandInput(S_CPY_FILTER_TYPE_LOOKUP, 'Filter Type', adsk.core.DropDownStyles.LabeledIconDropDownStyle);
dropDownCpyFilterTypeItems = dropDownCpyFilterType.listItems
dropDownCpyFilterTypeItems.add(S_CPY_FILTER_TYPET_TOP_LEVEL, True, '')
dropDownCpyFilterTypeItems.add(S_CPY_FILTER_TYPE_LEAVES, False, '')
dropDownCpyFilterTypeItems.add(S_CPY_FILTER_TYPE_MIXED_LEAVES, False, '')
# Run whenever a user makes any change to a value or selection in the addin UI
def on_preview(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs, args, input_values):
pass
# Run after the command is finished.
def on_destroy(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs, reason, input_values):
pass
# Run when any input is changed.
def on_input_changed(self, command: adsk.core.Command, inputs: adsk.core.CommandInputs, changed_input, input_values):
pass