-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFilteredExportSaveCopyAs.py
191 lines (150 loc) · 7.01 KB
/
FilteredExportSaveCopyAs.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
import adsk.core
import adsk.fusion
import traceback
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'
#
# 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
# export all components
for component in components:
# create a copy and save it
component.saveCopyAs(component.name, documentFolder, '', '')
# amend
processedComponents.append(component.name)
# return resulting lists
return FilteredExportResult(documentFolder.name, 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
for component in components:
if component.bRepBodies and not component.occurrences:
# export leave component (contains bodies but no other components)
component.saveCopyAs(component.name, documentFolder, '', '')
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(documentFolder.name, 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
for component in components:
component.saveCopyAs(component.name, documentFolder, '', '')
processedComponents.append(component.name)
# return resulting lists
return FilteredExportResult(documentFolder.name, processedComponents, '')
#
# Save copy as a collection of bodies
#
class FilteredExportSaveCopyAs(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