-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsettings.py
342 lines (279 loc) · 15.5 KB
/
settings.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
import ast
import builtins
import json
import os
from collections import namedtuple, OrderedDict, defaultdict, Mapping
from configparser import ConfigParser
from pathlib import Path
from adsk.core import DropDownStyles, ValueInput
from .lib.appdirs import user_config_dir
fdmprinterfile = os.path.join(os.path.dirname(__file__), 'fdmprinter.def.json')
fdmextruderfile = os.path.join(os.path.dirname(__file__), 'fdmextruder.def.json')
useless_settings = {'machine_steps_per_mm_x', 'machine_steps_per_mm_y', 'machine_steps_per_mm_z',
'machine_steps_per_mm_e', 'machine_endstop_positive_direction_x',
'machine_endstop_positive_direction_y', 'machine_endstop_positive_direction_z',
'machine_show_variants'}
config_dir = user_config_dir('FusedCura', 'nraynaud')
machine_preference_file_path = os.path.join(config_dir, 'machine.ini')
visibility_preference_file_path = os.path.join(config_dir, 'visibility.ini')
general_configuration_file_path = os.path.join(config_dir, 'fusedcura.ini')
print('config_dir', config_dir)
print('machine_preference_file_path', machine_preference_file_path)
print('visibility_preference_file_path', visibility_preference_file_path)
print('general_configuration_file_path', general_configuration_file_path)
def get_extruder_file_path(index):
return os.path.join(config_dir, 'extuder%s.ini' % index)
_builtin_set = set(dir(builtins))
def get_dependencies(tree):
dependencies = set()
class DependencyHarvester(ast.NodeVisitor):
def visit_Name(self, node: ast.Name) -> None:
if node.id not in _builtin_set:
dependencies.add(node.id)
def visit_Str(self, node: ast.AST) -> None:
if node.s not in _builtin_set:
dependencies.add(node.s)
DependencyHarvester().visit(tree)
return dependencies
def setting_tree_to_dict_and_default(settings):
def get_settings_dict(node, dictionary):
for k, val in node.items():
val['id'] = k
if val['type'] != 'category':
dictionary[k] = val
if val.get('children'):
get_settings_dict(val['children'], dictionary)
setting_dict = dict()
dependant_dict = defaultdict(set)
get_settings_dict(settings, setting_dict)
for k, v in setting_dict.items():
v['dependants'] = set()
if v.get('value'):
tree = ast.parse(v.get('value'), "eval")
dependencies = get_dependencies(tree)
for dep in dependencies:
dependant_dict[dep].add(k)
v['compiled'] = compile(v.get('value'), '<string>' + k, 'eval')
v['dependencies'] = dependencies
for k in setting_dict.keys():
setting_dict[k]['dependants'] = dependant_dict[k]
defaults = {
k: setting_types[v['type']].from_str(v['default_value']) if setting_types.get(v['type']) and v.get(
'default_value') else v.get('default_value') for (k, v) in setting_dict.items()}
return setting_dict, defaults
def remove_categories(settings):
result = []
for k, v in settings.items():
if v['type'] != 'category':
result.append((k, v))
else:
result.extend(remove_categories(v['children']))
return result
def save_machine_config(changed_settings, setting_definitions):
config = ConfigParser()
config['machine'] = {k: setting_types[setting_definitions[k]['type']].to_str(v) for (k, v) in
changed_settings.items()}
os.makedirs(config_dir, exist_ok=True)
with open(machine_preference_file_path, 'w') as configfile:
config.write(configfile)
def read_machine_settings(global_settings_definitions, global_settings_defaults):
machine_settings = {}
try:
machine_config_parser = ConfigParser(interpolation=None, comment_prefixes=())
with open(machine_preference_file_path) as f:
machine_config_parser.read_file(f)
machine_config_parser = machine_config_parser['machine']
for (k, s) in machine_config_parser.items():
value = setting_types[global_settings_definitions[k]['type']].from_str(s)
collect_changed_setting_if_different_from_parent(k, value, [global_settings_defaults], machine_settings)
except OSError:
pass
return machine_settings
def save_extruder_config(index, changed_settings, setting_definitions):
config = ConfigParser()
config['extruder'] = {k: setting_types[setting_definitions[k]['type']].to_str(v) for (k, v) in
changed_settings.items()}
os.makedirs(config_dir, exist_ok=True)
with open(get_extruder_file_path(index), 'w') as configfile:
config.write(configfile)
def read_extruder_config(index, global_settings_definitions=None, global_settings_defaults=None):
if global_settings_definitions is None:
global_settings_definitions = setting_tree_to_dict_and_default(get_config(fdmextruderfile, useless_settings))[0]
extruder_settings = {}
try:
extruder_settings_parser = ConfigParser(interpolation=None, comment_prefixes=())
with open(get_extruder_file_path(index)) as f:
extruder_settings_parser.read_file(f)
extruder_settings_parser = extruder_settings_parser['extruder']
for (k, s) in extruder_settings_parser.items():
value = setting_types[global_settings_definitions[k]['type']].from_str(s)
if global_settings_defaults is not None:
collect_changed_setting_if_different_from_parent(k, value, [global_settings_defaults],
extruder_settings)
else:
extruder_settings[k] = value
except OSError:
pass
return extruder_settings
def save_visibility(visibilities):
config = ConfigParser()
config['visibilities'] = visibilities
os.makedirs(config_dir, exist_ok=True)
with open(visibility_preference_file_path, 'w') as configfile:
config.write(configfile)
def read_visibility():
visibility = {k: True for k in defaut_visible_settings}
visibility_config = ConfigParser(comment_prefixes=())
try:
with open(visibility_preference_file_path) as f:
visibility_config.read_file(f)
visibility.update({k: bool(v) for k, v in visibility_config['visibilities'].items()})
except OSError:
pass
return visibility
def save_configuration(configuration):
config = ConfigParser()
config['configuration'] = configuration
os.makedirs(config_dir, exist_ok=True)
with open(general_configuration_file_path, 'w') as configfile:
config.write(configfile)
def read_configuration():
configuration = ConfigParser(comment_prefixes=())
try:
with open(general_configuration_file_path) as f:
configuration.read_file(f)
return configuration['configuration']
except OSError:
pass
return None
def collect_changed_setting_if_different_from_parent(key, value, stack_of_dict, collection_dict):
for parent_dict in reversed(stack_of_dict):
if key in parent_dict:
parent_val = parent_dict[key]
if parent_val == value:
if key in collection_dict:
del collection_dict[key]
else:
collection_dict[key] = value
break
def find_setting_in_stack(key, stack_of_dict):
for parent_dict in reversed(stack_of_dict):
if key in parent_dict:
return parent_dict[key]
raise KeyError(key)
def get_config(file_name, useless_set=set()):
preferred_order = ['resolution', 'shell', 'infill', 'material', 'speed', 'cooling', 'support', 'travel',
'machine_settings', 'experimental', 'platform_adhesion']
file_content = Path(file_name).read_text()
loaded = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(file_content)['settings']
other_keys = [k for k in loaded.keys() if k not in set(preferred_order)]
re_ordered_dict = OrderedDict([(k, loaded[k]) for k in preferred_order + other_keys if k in loaded])
def filter_useless(node):
if node.get('children'):
filtered_children = OrderedDict(
[(k, filter_useless(v)) for k, v in node['children'].items() if k not in useless_set])
return {**{k: v for k, v in node.items() if k != 'children'}, 'children': filtered_children}
return node
return OrderedDict([(k, filter_useless(v)) for (k, v) in re_ordered_dict.items()])
def stacked_mapping(stack):
class CustomDict(dict):
def __getitem__(self, k):
return find_setting_in_stack(k, stack)
return CustomDict()
def computed_dict(definitions, locals_vars):
computed_properties = {k: v for k, v in definitions.items() if 'value' in v}
extra_functions = {
'resolveOrValue': lambda x: locals_vars[x],
# TODO: implement
'extruderValue': lambda i, var: locals_vars[var],
'extruderValues': lambda var: [locals_vars[var]],
'defaultExtruderPosition': lambda: 0
}
import math
eval_global = {
**globals(), **extra_functions, 'math': math
}
class ComputedDict(Mapping):
def __getitem__(_, k):
return eval(computed_properties[k]['compiled'], eval_global, locals_vars)
def __iter__(self):
return computed_properties.__iter__()
def __len__(self):
return len(computed_properties)
return ComputedDict()
def add_enum_input(key, node, _inputs, value):
new_input = _inputs.addDropDownCommandInput(key, node['label'], DropDownStyles.TextListDropDownStyle)
for (opk, op) in node['options'].items():
item = new_input.listItems.add(op, opk == value)
item.id = opk
return new_input
def set_enum_value(i, v, node):
items = [item for item in i.listItems if item.name == node['options'][v]]
items[0].isSelected = True
def set_normal_value(i, v, node):
i.value = v
SettingType = namedtuple('SettingType', ['to_input', 'from_input', 'to_str', 'from_str', 'set_value'])
setting_types = {
'bool': SettingType(
to_input=lambda k, desc, inp, v: inp.addBoolValueInput(k, desc['label'], True, '', bool(v)),
to_str=lambda v: str(v).lower(), from_str=bool, from_input=(lambda i, node: i.value),
set_value=set_normal_value),
'float': SettingType(
to_input=lambda k, desc, inp, v: inp.addValueInput(k, desc['label'], '',
ValueInput.createByReal(float(v) if v else 0), ),
to_str=str, from_str=float, from_input=lambda i, node: i.value, set_value=set_normal_value),
'int': SettingType(
to_input=lambda k, desc, inp, v: inp.addIntegerSpinnerCommandInput(k, desc['label'], 0, 300000, 1,
int(v) if v else 0),
to_str=str, from_str=int, from_input=lambda i, node: i.value, set_value=set_normal_value),
'str': SettingType(
to_input=lambda k, desc, inp, v: inp.addStringValueInput(k, desc['label'], v if v else ''),
to_str=lambda v: v, from_str=lambda v: v, from_input=lambda i, node: i.value, set_value=set_normal_value),
'enum': SettingType(to_input=add_enum_input, to_str=str, from_str=str,
from_input=lambda i, node:
[k for (k, v) in node['options'].items() if v == i.selectedItem.name][0],
set_value=set_enum_value),
'optional_extruder': SettingType(
to_input=lambda k, desc, inp, v: inp.addIntegerSpinnerCommandInput(k, desc['label'], -1, 16,
1, int(v) if v else 0),
to_str=str, from_str=int, from_input=lambda i, node: i.value, set_value=set_normal_value),
'extruder': SettingType(
to_input=lambda k, desc, inp, v: inp.addIntegerSpinnerCommandInput(k, desc['label'], 0, 16, 1,
int(v) if v else 0),
to_str=str, from_str=int, from_input=lambda i, node: i.value, set_value=set_normal_value),
'[int]': SettingType(
to_input=lambda k, desc, inp, v: inp.addStringValueInput(k, desc['label'], v if v else ''),
to_str=str, from_str=str, from_input=lambda i, node: i.value, set_value=set_normal_value),
}
defaut_visible_settings = {'layer_height', 'layer_height_0', 'line_width', 'wall_line_width', 'wall_line_width_0',
'wall_line_width_x', 'skin_line_width', 'infill_line_width',
'initial_layer_line_width_factor', 'wall_extruder_nr', 'wall_0_extruder_nr',
'wall_x_extruder_nr', 'wall_thickness', 'wall_line_count', 'top_bottom_extruder_nr',
'top_bottom_thickness', 'top_thickness', 'top_layers', 'bottom_thickness', 'bottom_layers',
'optimize_wall_printing_order', 'fill_perimeter_gaps', 'xy_offset', 'ironing_enabled',
'infill_extruder_nr', 'infill_sparse_density', 'infill_line_distance', 'infill_pattern',
'infill_overlap', 'infill_sparse_thickness', 'gradual_infill_steps',
'material_print_temperature', 'material_print_temperature_layer_0',
'material_initial_print_temperature', 'material_final_print_temperature',
'material_bed_temperature', 'material_bed_temperature_layer_0', 'retraction_enable',
'retract_at_layer_change', 'retraction_amount', 'retraction_speed',
'material_standby_temperature', 'speed_print', 'speed_infill', 'speed_wall', 'speed_wall_0',
'speed_wall_x', 'speed_topbottom', 'speed_support', 'speed_prime_tower', 'speed_travel',
'speed_layer_0', 'skirt_brim_speed', 'acceleration_enabled', 'jerk_enabled',
'retraction_combing', 'travel_avoid_other_parts', 'travel_avoid_supports',
'travel_avoid_distance', 'retraction_hop_enabled', 'retraction_hop_only_when_collides',
'retraction_hop', 'retraction_hop_after_extruder_switch', 'cool_fan_enabled',
'cool_fan_speed', 'cool_fan_speed_min', 'cool_fan_speed_max',
'cool_min_layer_time_fan_speed_max', 'cool_fan_speed_0', 'cool_fan_full_at_height',
'cool_fan_full_layer', 'cool_min_layer_time', 'cool_min_speed', 'cool_lift_head',
'support_enable', 'support_extruder_nr', 'support_infill_extruder_nr',
'support_extruder_nr_layer_0', 'support_interface_extruder_nr', 'support_type',
'support_angle', 'support_pattern', 'support_infill_rate', 'support_offset',
'support_infill_sparse_thickness', 'gradual_support_infill_steps',
'gradual_support_infill_step_height', 'support_interface_enable', 'support_roof_enable',
'support_bottom_enable', 'prime_blob_enable', 'adhesion_type', 'adhesion_extruder_nr',
'skirt_line_count', 'brim_width', 'brim_line_count', 'brim_outside_only',
'prime_tower_enable', 'prime_tower_position_x', 'prime_tower_position_y',
'prime_tower_purge_volume', 'print_sequence', 'magic_mesh_surface_mode', 'magic_spiralize',
'smooth_spiralized_contours', 'conical_overhang_enabled', 'support_conical_enabled',
'adaptive_layer_height_enabled'}