SettingsExportModel.py 7.1 KB

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