SettingsExportModel.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (c) 2024 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from dataclasses import asdict
  4. from typing import Optional, cast, List, Dict, Pattern, Set
  5. from PyQt6.QtCore import QObject, pyqtProperty
  6. from UM.Settings.SettingDefinition import SettingDefinition
  7. from UM.Settings.InstanceContainer import InstanceContainer
  8. from UM.Settings.SettingFunction import SettingFunction
  9. from cura.CuraApplication import CuraApplication
  10. from cura.Settings.ExtruderManager import ExtruderManager
  11. from cura.Settings.GlobalStack import GlobalStack
  12. from .SettingsExportGroup import SettingsExportGroup
  13. from .SettingExport import SettingExport
  14. class SettingsExportModel(QObject):
  15. EXPORTABLE_SETTINGS = {'infill_sparse_density',
  16. 'adhesion_type',
  17. 'support_enable',
  18. 'infill_pattern',
  19. 'support_type',
  20. 'support_structure',
  21. 'support_angle',
  22. 'support_infill_rate',
  23. 'ironing_enabled',
  24. 'fill_outline_gaps',
  25. 'coasting_enable',
  26. 'skin_monotonic',
  27. 'z_seam_position',
  28. 'infill_before_walls',
  29. 'ironing_only_highest_layer',
  30. 'xy_offset',
  31. 'adaptive_layer_height_enabled',
  32. 'brim_gap',
  33. 'support_offset',
  34. 'brim_outside_only',
  35. 'magic_spiralize',
  36. 'slicing_tolerance',
  37. 'outer_inset_first',
  38. 'magic_fuzzy_skin_outside_only',
  39. 'conical_overhang_enabled',
  40. 'min_infill_area',
  41. 'small_hole_max_size',
  42. 'magic_mesh_surface_mode',
  43. 'carve_multiple_volumes',
  44. 'meshfix_union_all_remove_holes',
  45. 'support_tree_rest_preference',
  46. 'small_feature_max_length',
  47. 'draft_shield_enabled',
  48. 'brim_smart_ordering',
  49. 'ooze_shield_enabled',
  50. 'bottom_skin_preshrink',
  51. 'skin_edge_support_thickness',
  52. 'alternate_carve_order',
  53. 'top_skin_preshrink',
  54. 'interlocking_enable'}
  55. PER_MODEL_EXPORTABLE_SETTINGS_KEYS = {"anti_overhang_mesh",
  56. "infill_mesh",
  57. "cutting_mesh",
  58. "support_mesh"}
  59. def __init__(self, parent=None):
  60. super().__init__(parent)
  61. self._settings_groups = []
  62. application = CuraApplication.getInstance()
  63. self._appendGlobalSettings(application)
  64. self._appendExtruderSettings(application)
  65. self._appendModelSettings(application)
  66. def _appendGlobalSettings(self, application):
  67. global_stack = application.getGlobalContainerStack()
  68. self._settings_groups.append(SettingsExportGroup(
  69. global_stack, "Global settings", SettingsExportGroup.Category.Global, self._exportSettings(global_stack)))
  70. def _appendExtruderSettings(self, application):
  71. extruders_stacks = ExtruderManager.getInstance().getUsedExtruderStacks()
  72. for extruder_stack in extruders_stacks:
  73. color = extruder_stack.material.getMetaDataEntry("color_code") if extruder_stack.material else ""
  74. self._settings_groups.append(SettingsExportGroup(
  75. extruder_stack, "Extruder settings", SettingsExportGroup.Category.Extruder,
  76. self._exportSettings(extruder_stack), extruder_index=extruder_stack.position, extruder_color=color))
  77. def _appendModelSettings(self, application):
  78. scene = application.getController().getScene()
  79. for scene_node in scene.getRoot().getChildren():
  80. self._appendNodeSettings(scene_node, "Model settings", SettingsExportGroup.Category.Model)
  81. def _appendNodeSettings(self, node, title_prefix, category):
  82. stack = node.callDecoration("getStack")
  83. if stack:
  84. self._settings_groups.append(SettingsExportGroup(
  85. stack, f"{title_prefix}", category, self._exportSettings(stack), node.getName()))
  86. for child in node.getChildren():
  87. self._appendNodeSettings(child, f"Children of {node.getName()}", SettingsExportGroup.Category.Model)
  88. @pyqtProperty(list, constant=True)
  89. def settingsGroups(self) -> List[SettingsExportGroup]:
  90. return self._settings_groups
  91. @staticmethod
  92. def _exportSettings(settings_stack):
  93. user_settings_container = settings_stack.userChanges
  94. user_keys = user_settings_container.getAllKeys()
  95. exportable_settings = SettingsExportModel.EXPORTABLE_SETTINGS
  96. settings_export = []
  97. # Check whether any of the user keys exist in PER_MODEL_EXPORTABLE_SETTINGS_KEYS
  98. is_exportable = any(key in SettingsExportModel.PER_MODEL_EXPORTABLE_SETTINGS_KEYS for key in user_keys)
  99. for setting_to_export in user_keys:
  100. label = settings_stack.getProperty(setting_to_export, "label")
  101. value = settings_stack.getProperty(setting_to_export, "value")
  102. unit = settings_stack.getProperty(setting_to_export, "unit")
  103. setting_type = settings_stack.getProperty(setting_to_export, "type")
  104. if setting_type is not None:
  105. value = f"{str(SettingDefinition.settingValueToString(setting_type, value))} {unit}"
  106. else:
  107. value = str(value)
  108. settings_export.append(SettingExport(setting_to_export,
  109. label,
  110. value,
  111. is_exportable or setting_to_export in exportable_settings))
  112. return settings_export