QualityManager.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any, Dict, List, Optional, TYPE_CHECKING
  4. from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
  5. from UM.Logger import Logger
  6. from UM.Util import parseBool
  7. from UM.Settings.InstanceContainer import InstanceContainer
  8. from UM.Decorators import deprecated
  9. import cura.CuraApplication
  10. from cura.Settings.ExtruderStack import ExtruderStack
  11. from cura.Machines.ContainerTree import ContainerTree # The implementation that replaces this manager, to keep the deprecated interface working.
  12. from .QualityChangesGroup import QualityChangesGroup
  13. from .QualityGroup import QualityGroup
  14. from .QualityNode import QualityNode
  15. if TYPE_CHECKING:
  16. from UM.Settings.Interfaces import DefinitionContainerInterface
  17. from cura.Settings.GlobalStack import GlobalStack
  18. from .QualityChangesGroup import QualityChangesGroup
  19. #
  20. # Similar to MaterialManager, QualityManager maintains a number of maps and trees for quality profile lookup.
  21. # The models GUI and QML use are now only dependent on the QualityManager. That means as long as the data in
  22. # QualityManager gets updated correctly, the GUI models should be updated correctly too, and the same goes for GUI.
  23. #
  24. # For now, updating the lookup maps and trees here is very simple: we discard the old data completely and recreate them
  25. # again. This means the update is exactly the same as initialization. There are performance concerns about this approach
  26. # but so far the creation of the tables and maps is very fast and there is no noticeable slowness, we keep it like this
  27. # because it's simple.
  28. #
  29. class QualityManager(QObject):
  30. __instance = None
  31. @classmethod
  32. @deprecated("Use the ContainerTree structure instead.", since = "4.3")
  33. def getInstance(cls) -> "QualityManager":
  34. if cls.__instance is None:
  35. cls.__instance = QualityManager()
  36. return cls.__instance
  37. qualitiesUpdated = pyqtSignal()
  38. def __init__(self, parent = None) -> None:
  39. super().__init__(parent)
  40. application = cura.CuraApplication.CuraApplication.getInstance()
  41. self._material_manager = application.getMaterialManager()
  42. self._container_registry = application.getContainerRegistry()
  43. self._empty_quality_container = application.empty_quality_container
  44. self._empty_quality_changes_container = application.empty_quality_changes_container
  45. # For quality lookup
  46. self._machine_nozzle_buildplate_material_quality_type_to_quality_dict = {} # type: Dict[str, QualityNode]
  47. # For quality_changes lookup
  48. self._machine_quality_type_to_quality_changes_dict = {} # type: Dict[str, QualityNode]
  49. self._default_machine_definition_id = "fdmprinter"
  50. self._container_registry.containerMetaDataChanged.connect(self._onContainerMetadataChanged)
  51. self._container_registry.containerAdded.connect(self._onContainerMetadataChanged)
  52. self._container_registry.containerRemoved.connect(self._onContainerMetadataChanged)
  53. def _onContainerMetadataChanged(self, container: InstanceContainer) -> None:
  54. self._onContainerChanged(container)
  55. def _onContainerChanged(self, container: InstanceContainer) -> None:
  56. container_type = container.getMetaDataEntry("type")
  57. if container_type not in ("quality", "quality_changes"):
  58. return
  59. # Returns a dict of "custom profile name" -> QualityChangesGroup
  60. def getQualityChangesGroups(self, machine: "GlobalStack") -> List[QualityChangesGroup]:
  61. variant_names = [extruder.variant.getName() for extruder in machine.extruders.values()]
  62. material_bases = [extruder.material.getMetaDataEntry("base_file") for extruder in machine.extruders.values()]
  63. extruder_enabled = [extruder.isEnabled for extruder in machine.extruders.values()]
  64. machine_node = ContainerTree.getInstance().machines[machine.definition.getId()]
  65. return machine_node.getQualityChangesGroups(variant_names, material_bases, extruder_enabled)
  66. ## Gets the quality groups for the current printer.
  67. #
  68. # Both available and unavailable quality groups will be included. Whether
  69. # a quality group is available can be known via the field
  70. # ``QualityGroup.is_available``. For more details, see QualityGroup.
  71. # \return A dictionary with quality types as keys and the quality groups
  72. # for those types as values.
  73. def getQualityGroups(self, global_stack: "GlobalStack") -> Dict[str, QualityGroup]:
  74. # Gather up the variant names and material base files for each extruder.
  75. variant_names = [extruder.variant.getName() for extruder in global_stack.extruders.values()]
  76. material_bases = [extruder.material.getMetaDataEntry("base_file") for extruder in global_stack.extruders.values()]
  77. extruder_enabled = [extruder.isEnabled for extruder in global_stack.extruders.values()]
  78. definition_id = global_stack.definition.getId()
  79. return ContainerTree.getInstance().machines[definition_id].getQualityGroups(variant_names, material_bases, extruder_enabled)
  80. def getQualityGroupsForMachineDefinition(self, machine: "GlobalStack") -> Dict[str, QualityGroup]:
  81. machine_definition_id = getMachineDefinitionIDForQualitySearch(machine.definition)
  82. # To find the quality container for the GlobalStack, check in the following fall-back manner:
  83. # (1) the machine-specific node
  84. # (2) the generic node
  85. machine_node = self._machine_nozzle_buildplate_material_quality_type_to_quality_dict.get(machine_definition_id)
  86. default_machine_node = self._machine_nozzle_buildplate_material_quality_type_to_quality_dict.get(
  87. self._default_machine_definition_id)
  88. nodes_to_check = [machine_node, default_machine_node]
  89. # Iterate over all quality_types in the machine node
  90. quality_group_dict = dict()
  91. for node in nodes_to_check:
  92. if node and node.quality_type:
  93. quality_group = QualityGroup(node.getMetaDataEntry("name", ""), node.quality_type)
  94. quality_group.setGlobalNode(node)
  95. quality_group_dict[node.quality_type] = quality_group
  96. return quality_group_dict
  97. ## Get the quality group for the preferred quality type for a certain
  98. # global stack.
  99. #
  100. # If the preferred quality type is not available, ``None`` will be
  101. # returned.
  102. # \param machine The global stack of the machine to get the preferred
  103. # quality group for.
  104. # \return The preferred quality group, or ``None`` if that is not
  105. # available.
  106. def getDefaultQualityType(self, machine: "GlobalStack") -> Optional[QualityGroup]:
  107. machine_node = ContainerTree.getInstance().machines[machine.definition.getId()]
  108. quality_groups = self.getQualityGroups(machine)
  109. result = quality_groups.get(machine_node.preferred_quality_type)
  110. if result is not None and result.is_available:
  111. return result
  112. return None # If preferred quality type is not available, leave it up for the caller.
  113. #
  114. # Methods for GUI
  115. #
  116. ## Deletes a custom profile. It will be gone forever.
  117. # \param quality_changes_group The quality changes group representing the
  118. # profile to delete.
  119. @pyqtSlot(QObject)
  120. def removeQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup") -> None:
  121. return cura.CuraApplication.CuraApplication.getInstance().getQualityManagementModel().removeQualityChangesGroup(quality_changes_group)
  122. ## Rename a custom profile.
  123. #
  124. # Because the names must be unique, the new name may not actually become
  125. # the name that was given. The actual name is returned by this function.
  126. # \param quality_changes_group The custom profile that must be renamed.
  127. # \param new_name The desired name for the profile.
  128. # \return The actual new name of the profile, after making the name
  129. # unique.
  130. @pyqtSlot(QObject, str, result = str)
  131. def renameQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup", new_name: str) -> str:
  132. return cura.CuraApplication.CuraApplication.getInstance().getQualityManagementModel().removeQualityChangesGroup(quality_changes_group, new_name)
  133. ## Duplicates a given quality profile OR quality changes profile.
  134. # \param new_name The desired name of the new profile. This will be made
  135. # unique, so it might end up with a different name.
  136. # \param quality_model_item The item of this model to duplicate, as
  137. # dictionary. See the descriptions of the roles of this list model.
  138. @pyqtSlot(str, "QVariantMap")
  139. def duplicateQualityChanges(self, quality_changes_name: str, quality_model_item: Dict[str, Any]) -> None:
  140. return cura.CuraApplication.CuraApplication.getInstance().getQualityManagementModel().duplicateQualityChanges(quality_changes_name, quality_model_item)
  141. ## Create quality changes containers from the user containers in the active
  142. # stacks.
  143. #
  144. # This will go through the global and extruder stacks and create
  145. # quality_changes containers from the user containers in each stack. These
  146. # then replace the quality_changes containers in the stack and clear the
  147. # user settings.
  148. # \param base_name The new name for the quality changes profile. The final
  149. # name of the profile might be different from this, because it needs to be
  150. # made unique.
  151. @pyqtSlot(str)
  152. def createQualityChanges(self, base_name: str) -> None:
  153. return cura.CuraApplication.CuraApplication.getInstance().getQualityManagementModel().createQualityChanges(base_name)
  154. #
  155. # Create a quality changes container with the given setup.
  156. #
  157. def _createQualityChanges(self, quality_type: str, new_name: str, machine: "GlobalStack",
  158. extruder_stack: Optional["ExtruderStack"]) -> "InstanceContainer":
  159. base_id = machine.definition.getId() if extruder_stack is None else extruder_stack.getId()
  160. new_id = base_id + "_" + new_name
  161. new_id = new_id.lower().replace(" ", "_")
  162. new_id = self._container_registry.uniqueName(new_id)
  163. # Create a new quality_changes container for the quality.
  164. quality_changes = InstanceContainer(new_id)
  165. quality_changes.setName(new_name)
  166. quality_changes.setMetaDataEntry("type", "quality_changes")
  167. quality_changes.setMetaDataEntry("quality_type", quality_type)
  168. # If we are creating a container for an extruder, ensure we add that to the container
  169. if extruder_stack is not None:
  170. quality_changes.setMetaDataEntry("position", extruder_stack.getMetaDataEntry("position"))
  171. # If the machine specifies qualities should be filtered, ensure we match the current criteria.
  172. machine_definition_id = getMachineDefinitionIDForQualitySearch(machine.definition)
  173. quality_changes.setDefinition(machine_definition_id)
  174. quality_changes.setMetaDataEntry("setting_version", cura.CuraApplication.CuraApplication.getInstance().SettingVersion)
  175. return quality_changes
  176. #
  177. # Gets the machine definition ID that can be used to search for Quality containers that are suitable for the given
  178. # machine. The rule is as follows:
  179. # 1. By default, the machine definition ID for quality container search will be "fdmprinter", which is the generic
  180. # machine.
  181. # 2. If a machine has its own machine quality (with "has_machine_quality = True"), we should use the given machine's
  182. # own machine definition ID for quality search.
  183. # Example: for an Ultimaker 3, the definition ID should be "ultimaker3".
  184. # 3. When condition (2) is met, AND the machine has "quality_definition" defined in its definition file, then the
  185. # definition ID specified in "quality_definition" should be used.
  186. # Example: for an Ultimaker 3 Extended, it has "quality_definition = ultimaker3". This means Ultimaker 3 Extended
  187. # shares the same set of qualities profiles as Ultimaker 3.
  188. #
  189. def getMachineDefinitionIDForQualitySearch(machine_definition: "DefinitionContainerInterface",
  190. default_definition_id: str = "fdmprinter") -> str:
  191. machine_definition_id = default_definition_id
  192. if parseBool(machine_definition.getMetaDataEntry("has_machine_quality", False)):
  193. # Only use the machine's own quality definition ID if this machine has machine quality.
  194. machine_definition_id = machine_definition.getMetaDataEntry("quality_definition")
  195. if machine_definition_id is None:
  196. machine_definition_id = machine_definition.getId()
  197. return machine_definition_id