QualityManager.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import TYPE_CHECKING, Optional, cast, Dict, List, Set
  4. from PyQt5.QtCore import QObject, QTimer, pyqtSignal, pyqtSlot
  5. from UM.ConfigurationErrorMessage import ConfigurationErrorMessage
  6. from UM.Logger import Logger
  7. from UM.Util import parseBool
  8. from UM.Settings.InstanceContainer import InstanceContainer
  9. from cura.Settings.ExtruderStack import ExtruderStack
  10. from .QualityGroup import QualityGroup
  11. from .QualityNode import QualityNode
  12. if TYPE_CHECKING:
  13. from UM.Settings.Interfaces import DefinitionContainerInterface
  14. from cura.Settings.GlobalStack import GlobalStack
  15. from .QualityChangesGroup import QualityChangesGroup
  16. from cura.CuraApplication import CuraApplication
  17. #
  18. # Similar to MaterialManager, QualityManager maintains a number of maps and trees for quality profile lookup.
  19. # The models GUI and QML use are now only dependent on the QualityManager. That means as long as the data in
  20. # QualityManager gets updated correctly, the GUI models should be updated correctly too, and the same goes for GUI.
  21. #
  22. # For now, updating the lookup maps and trees here is very simple: we discard the old data completely and recreate them
  23. # again. This means the update is exactly the same as initialization. There are performance concerns about this approach
  24. # but so far the creation of the tables and maps is very fast and there is no noticeable slowness, we keep it like this
  25. # because it's simple.
  26. #
  27. class QualityManager(QObject):
  28. qualitiesUpdated = pyqtSignal()
  29. def __init__(self, application: "CuraApplication", parent = None) -> None:
  30. super().__init__(parent)
  31. self._application = application
  32. self._material_manager = self._application.getMaterialManager()
  33. self._container_registry = self._application.getContainerRegistry()
  34. self._empty_quality_container = self._application.empty_quality_container
  35. self._empty_quality_changes_container = self._application.empty_quality_changes_container
  36. # For quality lookup
  37. self._machine_nozzle_buildplate_material_quality_type_to_quality_dict = {} # type: Dict[str, QualityNode]
  38. # For quality_changes lookup
  39. self._machine_quality_type_to_quality_changes_dict = {} # type: Dict[str, QualityNode]
  40. self._default_machine_definition_id = "fdmprinter"
  41. self._container_registry.containerMetaDataChanged.connect(self._onContainerMetadataChanged)
  42. self._container_registry.containerAdded.connect(self._onContainerMetadataChanged)
  43. self._container_registry.containerRemoved.connect(self._onContainerMetadataChanged)
  44. # When a custom quality gets added/imported, there can be more than one InstanceContainers. In those cases,
  45. # we don't want to react on every container/metadata changed signal. The timer here is to buffer it a bit so
  46. # we don't react too many time.
  47. self._update_timer = QTimer(self)
  48. self._update_timer.setInterval(300)
  49. self._update_timer.setSingleShot(True)
  50. self._update_timer.timeout.connect(self._updateMaps)
  51. def initialize(self) -> None:
  52. # Initialize the lookup tree for quality profiles with following structure:
  53. # <machine> -> <nozzle> -> <buildplate> -> <material>
  54. # <machine> -> <material>
  55. self._machine_nozzle_buildplate_material_quality_type_to_quality_dict = {} # for quality lookup
  56. self._machine_quality_type_to_quality_changes_dict = {} # for quality_changes lookup
  57. quality_metadata_list = self._container_registry.findContainersMetadata(type = "quality")
  58. for metadata in quality_metadata_list:
  59. if metadata["id"] == "empty_quality":
  60. continue
  61. definition_id = metadata["definition"]
  62. quality_type = metadata["quality_type"]
  63. root_material_id = metadata.get("material")
  64. nozzle_name = metadata.get("variant")
  65. buildplate_name = metadata.get("buildplate")
  66. is_global_quality = metadata.get("global_quality", False)
  67. is_global_quality = is_global_quality or (root_material_id is None and nozzle_name is None and buildplate_name is None)
  68. # Sanity check: material+variant and is_global_quality cannot be present at the same time
  69. if is_global_quality and (root_material_id or nozzle_name):
  70. ConfigurationErrorMessage.getInstance().addFaultyContainers(metadata["id"])
  71. continue
  72. if definition_id not in self._machine_nozzle_buildplate_material_quality_type_to_quality_dict:
  73. self._machine_nozzle_buildplate_material_quality_type_to_quality_dict[definition_id] = QualityNode()
  74. machine_node = cast(QualityNode, self._machine_nozzle_buildplate_material_quality_type_to_quality_dict[definition_id])
  75. if is_global_quality:
  76. # For global qualities, save data in the machine node
  77. machine_node.addQualityMetadata(quality_type, metadata)
  78. continue
  79. current_node = machine_node
  80. intermediate_node_info_list = [nozzle_name, buildplate_name, root_material_id]
  81. current_intermediate_node_info_idx = 0
  82. while current_intermediate_node_info_idx < len(intermediate_node_info_list):
  83. node_name = intermediate_node_info_list[current_intermediate_node_info_idx]
  84. if node_name is not None:
  85. # There is specific information, update the current node to go deeper so we can add this quality
  86. # at the most specific branch in the lookup tree.
  87. if node_name not in current_node.children_map:
  88. current_node.children_map[node_name] = QualityNode()
  89. current_node = cast(QualityNode, current_node.children_map[node_name])
  90. current_intermediate_node_info_idx += 1
  91. current_node.addQualityMetadata(quality_type, metadata)
  92. # Initialize the lookup tree for quality_changes profiles with following structure:
  93. # <machine> -> <quality_type> -> <name>
  94. quality_changes_metadata_list = self._container_registry.findContainersMetadata(type = "quality_changes")
  95. for metadata in quality_changes_metadata_list:
  96. if metadata["id"] == "empty_quality_changes":
  97. continue
  98. machine_definition_id = metadata["definition"]
  99. quality_type = metadata["quality_type"]
  100. if machine_definition_id not in self._machine_quality_type_to_quality_changes_dict:
  101. self._machine_quality_type_to_quality_changes_dict[machine_definition_id] = QualityNode()
  102. machine_node = self._machine_quality_type_to_quality_changes_dict[machine_definition_id]
  103. machine_node.addQualityChangesMetadata(quality_type, metadata)
  104. Logger.log("d", "Lookup tables updated.")
  105. self.qualitiesUpdated.emit()
  106. def _updateMaps(self) -> None:
  107. self.initialize()
  108. def _onContainerMetadataChanged(self, container: InstanceContainer) -> None:
  109. self._onContainerChanged(container)
  110. def _onContainerChanged(self, container: InstanceContainer) -> None:
  111. container_type = container.getMetaDataEntry("type")
  112. if container_type not in ("quality", "quality_changes"):
  113. return
  114. # update the cache table
  115. self._update_timer.start()
  116. # Updates the given quality groups' availabilities according to which extruders are being used/ enabled.
  117. def _updateQualityGroupsAvailability(self, machine: "GlobalStack", quality_group_list) -> None:
  118. used_extruders = set()
  119. for i in range(machine.getProperty("machine_extruder_count", "value")):
  120. if str(i) in machine.extruders and machine.extruders[str(i)].isEnabled:
  121. used_extruders.add(str(i))
  122. # Update the "is_available" flag for each quality group.
  123. for quality_group in quality_group_list:
  124. is_available = True
  125. if quality_group.node_for_global is None:
  126. is_available = False
  127. if is_available:
  128. for position in used_extruders:
  129. if position not in quality_group.nodes_for_extruders:
  130. is_available = False
  131. break
  132. quality_group.is_available = is_available
  133. # Returns a dict of "custom profile name" -> QualityChangesGroup
  134. def getQualityChangesGroups(self, machine: "GlobalStack") -> dict:
  135. machine_definition_id = getMachineDefinitionIDForQualitySearch(machine.definition)
  136. machine_node = self._machine_quality_type_to_quality_changes_dict.get(machine_definition_id)
  137. if not machine_node:
  138. Logger.log("i", "Cannot find node for machine def [%s] in QualityChanges lookup table", machine_definition_id)
  139. return dict()
  140. # Update availability for each QualityChangesGroup:
  141. # A custom profile is always available as long as the quality_type it's based on is available
  142. quality_group_dict = self.getQualityGroups(machine)
  143. available_quality_type_list = [qt for qt, qg in quality_group_dict.items() if qg.is_available]
  144. # Iterate over all quality_types in the machine node
  145. quality_changes_group_dict = dict()
  146. for quality_type, quality_changes_node in machine_node.quality_type_map.items():
  147. for quality_changes_name, quality_changes_group in quality_changes_node.children_map.items():
  148. quality_changes_group_dict[quality_changes_name] = quality_changes_group
  149. quality_changes_group.is_available = quality_type in available_quality_type_list
  150. return quality_changes_group_dict
  151. #
  152. # Gets all quality groups for the given machine. Both available and none available ones will be included.
  153. # It returns a dictionary with "quality_type"s as keys and "QualityGroup"s as values.
  154. # Whether a QualityGroup is available can be unknown via the field QualityGroup.is_available.
  155. # For more details, see QualityGroup.
  156. #
  157. def getQualityGroups(self, machine: "GlobalStack") -> Dict[str, QualityGroup]:
  158. machine_definition_id = getMachineDefinitionIDForQualitySearch(machine.definition)
  159. # To find the quality container for the GlobalStack, check in the following fall-back manner:
  160. # (1) the machine-specific node
  161. # (2) the generic node
  162. machine_node = self._machine_nozzle_buildplate_material_quality_type_to_quality_dict.get(machine_definition_id)
  163. # Check if this machine has specific quality profiles for its extruders, if so, when looking up extruder
  164. # qualities, we should not fall back to use the global qualities.
  165. has_extruder_specific_qualities = False
  166. if machine_node:
  167. if machine_node.children_map:
  168. has_extruder_specific_qualities = True
  169. default_machine_node = self._machine_nozzle_buildplate_material_quality_type_to_quality_dict.get(self._default_machine_definition_id)
  170. nodes_to_check = [] # type: List[QualityNode]
  171. if machine_node is not None:
  172. nodes_to_check.append(machine_node)
  173. if default_machine_node is not None:
  174. nodes_to_check.append(default_machine_node)
  175. # Iterate over all quality_types in the machine node
  176. quality_group_dict = {}
  177. for node in nodes_to_check:
  178. if node and node.quality_type_map:
  179. quality_node = list(node.quality_type_map.values())[0]
  180. is_global_quality = parseBool(quality_node.getMetaDataEntry("global_quality", False))
  181. if not is_global_quality:
  182. continue
  183. for quality_type, quality_node in node.quality_type_map.items():
  184. quality_group = QualityGroup(quality_node.getMetaDataEntry("name", ""), quality_type)
  185. quality_group.setGlobalNode(quality_node)
  186. quality_group_dict[quality_type] = quality_group
  187. break
  188. buildplate_name = machine.getBuildplateName()
  189. # Iterate over all extruders to find quality containers for each extruder
  190. for position, extruder in machine.extruders.items():
  191. nozzle_name = None
  192. if extruder.variant.getId() != "empty_variant":
  193. nozzle_name = extruder.variant.getName()
  194. # This is a list of root material IDs to use for searching for suitable quality profiles.
  195. # The root material IDs in this list are in prioritized order.
  196. root_material_id_list = []
  197. has_material = False # flag indicating whether this extruder has a material assigned
  198. root_material_id = None
  199. if extruder.material.getId() != "empty_material":
  200. has_material = True
  201. root_material_id = extruder.material.getMetaDataEntry("base_file")
  202. # Convert possible generic_pla_175 -> generic_pla
  203. root_material_id = self._material_manager.getRootMaterialIDWithoutDiameter(root_material_id)
  204. root_material_id_list.append(root_material_id)
  205. # Also try to get the fallback materials
  206. fallback_ids = self._material_manager.getFallBackMaterialIdsByMaterial(extruder.material)
  207. if fallback_ids:
  208. root_material_id_list.extend(fallback_ids)
  209. # Weed out duplicates while preserving the order.
  210. seen = set() # type: Set[str]
  211. root_material_id_list = [x for x in root_material_id_list if x not in seen and not seen.add(x)] # type: ignore
  212. # Here we construct a list of nodes we want to look for qualities with the highest priority first.
  213. # The use case is that, when we look for qualities for a machine, we first want to search in the following
  214. # order:
  215. # 1. machine-nozzle-buildplate-and-material-specific qualities if exist
  216. # 2. machine-nozzle-and-material-specific qualities if exist
  217. # 3. machine-nozzle-specific qualities if exist
  218. # 4. machine-material-specific qualities if exist
  219. # 5. machine-specific global qualities if exist, otherwise generic global qualities
  220. # NOTE: We DO NOT fail back to generic global qualities if machine-specific global qualities exist.
  221. # This is because when a machine defines its own global qualities such as Normal, Fine, etc.,
  222. # it is intended to maintain those specific qualities ONLY. If we still fail back to the generic
  223. # global qualities, there can be unimplemented quality types e.g. "coarse", and this is not
  224. # correct.
  225. # Each points above can be represented as a node in the lookup tree, so here we simply put those nodes into
  226. # the list with priorities as the order. Later, we just need to loop over each node in this list and fetch
  227. # qualities from there.
  228. node_info_list_0 = [nozzle_name, buildplate_name, root_material_id] # type: List[Optional[str]]
  229. nodes_to_check = []
  230. # This function tries to recursively find the deepest (the most specific) branch and add those nodes to
  231. # the search list in the order described above. So, by iterating over that search node list, we first look
  232. # in the more specific branches and then the less specific (generic) ones.
  233. def addNodesToCheck(node: Optional[QualityNode], nodes_to_check_list: List[QualityNode], node_info_list, node_info_idx: int) -> None:
  234. if node is None:
  235. return
  236. if node_info_idx < len(node_info_list):
  237. node_name = node_info_list[node_info_idx]
  238. if node_name is not None:
  239. current_node = node.getChildNode(node_name)
  240. if current_node is not None and has_material:
  241. addNodesToCheck(current_node, nodes_to_check_list, node_info_list, node_info_idx + 1)
  242. if has_material:
  243. for rmid in root_material_id_list:
  244. material_node = node.getChildNode(rmid)
  245. if material_node:
  246. nodes_to_check_list.append(material_node)
  247. break
  248. nodes_to_check_list.append(node)
  249. addNodesToCheck(machine_node, nodes_to_check, node_info_list_0, 0)
  250. # The last fall back will be the global qualities (either from the machine-specific node or the generic
  251. # node), but we only use one. For details see the overview comments above.
  252. if machine_node is not None and machine_node.quality_type_map:
  253. nodes_to_check += [machine_node]
  254. elif default_machine_node is not None:
  255. nodes_to_check += [default_machine_node]
  256. for node_idx, node in enumerate(nodes_to_check):
  257. if node and node.quality_type_map:
  258. if has_extruder_specific_qualities:
  259. # Only include variant qualities; skip non global qualities
  260. quality_node = list(node.quality_type_map.values())[0]
  261. is_global_quality = parseBool(quality_node.getMetaDataEntry("global_quality", False))
  262. if is_global_quality:
  263. continue
  264. for quality_type, quality_node in node.quality_type_map.items():
  265. if quality_type not in quality_group_dict:
  266. quality_group = QualityGroup(quality_node.getMetaDataEntry("name", ""), quality_type)
  267. quality_group_dict[quality_type] = quality_group
  268. quality_group = quality_group_dict[quality_type]
  269. if position not in quality_group.nodes_for_extruders:
  270. quality_group.setExtruderNode(position, quality_node)
  271. # If the machine has its own specific qualities, for extruders, it should skip the global qualities
  272. # and use the material/variant specific qualities.
  273. if has_extruder_specific_qualities:
  274. if node_idx == len(nodes_to_check) - 1:
  275. break
  276. # Update availabilities for each quality group
  277. self._updateQualityGroupsAvailability(machine, quality_group_dict.values())
  278. return quality_group_dict
  279. def getQualityGroupsForMachineDefinition(self, machine: "GlobalStack") -> Dict[str, QualityGroup]:
  280. machine_definition_id = getMachineDefinitionIDForQualitySearch(machine.definition)
  281. # To find the quality container for the GlobalStack, check in the following fall-back manner:
  282. # (1) the machine-specific node
  283. # (2) the generic node
  284. machine_node = self._machine_nozzle_buildplate_material_quality_type_to_quality_dict.get(machine_definition_id)
  285. default_machine_node = self._machine_nozzle_buildplate_material_quality_type_to_quality_dict.get(
  286. self._default_machine_definition_id)
  287. nodes_to_check = [machine_node, default_machine_node]
  288. # Iterate over all quality_types in the machine node
  289. quality_group_dict = dict()
  290. for node in nodes_to_check:
  291. if node and node.quality_type_map:
  292. for quality_type, quality_node in node.quality_type_map.items():
  293. quality_group = QualityGroup(quality_node.getMetaDataEntry("name", ""), quality_type)
  294. quality_group.setGlobalNode(quality_node)
  295. quality_group_dict[quality_type] = quality_group
  296. break
  297. return quality_group_dict
  298. def getDefaultQualityType(self, machine: "GlobalStack") -> Optional[QualityGroup]:
  299. preferred_quality_type = machine.definition.getMetaDataEntry("preferred_quality_type")
  300. quality_group_dict = self.getQualityGroups(machine)
  301. quality_group = quality_group_dict.get(preferred_quality_type)
  302. return quality_group
  303. #
  304. # Methods for GUI
  305. #
  306. #
  307. # Remove the given quality changes group.
  308. #
  309. @pyqtSlot(QObject)
  310. def removeQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup") -> None:
  311. Logger.log("i", "Removing quality changes group [%s]", quality_changes_group.name)
  312. removed_quality_changes_ids = set()
  313. for node in quality_changes_group.getAllNodes():
  314. container_id = node.getMetaDataEntry("id")
  315. self._container_registry.removeContainer(container_id)
  316. removed_quality_changes_ids.add(container_id)
  317. # Reset all machines that have activated this quality changes to empty.
  318. for global_stack in self._container_registry.findContainerStacks(type = "machine"):
  319. if global_stack.qualityChanges.getId() in removed_quality_changes_ids:
  320. global_stack.qualityChanges = self._empty_quality_changes_container
  321. for extruder_stack in self._container_registry.findContainerStacks(type = "extruder_train"):
  322. if extruder_stack.qualityChanges.getId() in removed_quality_changes_ids:
  323. extruder_stack.qualityChanges = self._empty_quality_changes_container
  324. #
  325. # Rename a set of quality changes containers. Returns the new name.
  326. #
  327. @pyqtSlot(QObject, str, result = str)
  328. def renameQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup", new_name: str) -> str:
  329. Logger.log("i", "Renaming QualityChangesGroup[%s] to [%s]", quality_changes_group.name, new_name)
  330. if new_name == quality_changes_group.name:
  331. Logger.log("i", "QualityChangesGroup name [%s] unchanged.", quality_changes_group.name)
  332. return new_name
  333. new_name = self._container_registry.uniqueName(new_name)
  334. for node in quality_changes_group.getAllNodes():
  335. container = node.getContainer()
  336. if container:
  337. container.setName(new_name)
  338. quality_changes_group.name = new_name
  339. self._application.getMachineManager().activeQualityChanged.emit()
  340. self._application.getMachineManager().activeQualityGroupChanged.emit()
  341. return new_name
  342. #
  343. # Duplicates the given quality.
  344. #
  345. @pyqtSlot(str, "QVariantMap")
  346. def duplicateQualityChanges(self, quality_changes_name: str, quality_model_item) -> None:
  347. global_stack = self._application.getGlobalContainerStack()
  348. if not global_stack:
  349. Logger.log("i", "No active global stack, cannot duplicate quality changes.")
  350. return
  351. quality_group = quality_model_item["quality_group"]
  352. quality_changes_group = quality_model_item["quality_changes_group"]
  353. if quality_changes_group is None:
  354. # create global quality changes only
  355. new_name = self._container_registry.uniqueName(quality_changes_name)
  356. new_quality_changes = self._createQualityChanges(quality_group.quality_type, new_name,
  357. global_stack, None)
  358. self._container_registry.addContainer(new_quality_changes)
  359. else:
  360. new_name = self._container_registry.uniqueName(quality_changes_name)
  361. for node in quality_changes_group.getAllNodes():
  362. container = node.getContainer()
  363. if not container:
  364. continue
  365. new_id = self._container_registry.uniqueName(container.getId())
  366. self._container_registry.addContainer(container.duplicate(new_id, new_name))
  367. ## Create quality changes containers from the user containers in the active stacks.
  368. #
  369. # This will go through the global and extruder stacks and create quality_changes containers from
  370. # the user containers in each stack. These then replace the quality_changes containers in the
  371. # stack and clear the user settings.
  372. @pyqtSlot(str)
  373. def createQualityChanges(self, base_name: str) -> None:
  374. machine_manager = self._application.getMachineManager()
  375. global_stack = machine_manager.activeMachine
  376. if not global_stack:
  377. return
  378. active_quality_name = machine_manager.activeQualityOrQualityChangesName
  379. if active_quality_name == "":
  380. Logger.log("w", "No quality container found in stack %s, cannot create profile", global_stack.getId())
  381. return
  382. machine_manager.blurSettings.emit()
  383. if base_name is None or base_name == "":
  384. base_name = active_quality_name
  385. unique_name = self._container_registry.uniqueName(base_name)
  386. # Go through the active stacks and create quality_changes containers from the user containers.
  387. stack_list = [global_stack] + list(global_stack.extruders.values())
  388. for stack in stack_list:
  389. user_container = stack.userChanges
  390. quality_container = stack.quality
  391. quality_changes_container = stack.qualityChanges
  392. if not quality_container or not quality_changes_container:
  393. Logger.log("w", "No quality or quality changes container found in stack %s, ignoring it", stack.getId())
  394. continue
  395. quality_type = quality_container.getMetaDataEntry("quality_type")
  396. extruder_stack = None
  397. if isinstance(stack, ExtruderStack):
  398. extruder_stack = stack
  399. new_changes = self._createQualityChanges(quality_type, unique_name, global_stack, extruder_stack)
  400. from cura.Settings.ContainerManager import ContainerManager
  401. ContainerManager.getInstance()._performMerge(new_changes, quality_changes_container, clear_settings = False)
  402. ContainerManager.getInstance()._performMerge(new_changes, user_container)
  403. self._container_registry.addContainer(new_changes)
  404. #
  405. # Create a quality changes container with the given setup.
  406. #
  407. def _createQualityChanges(self, quality_type: str, new_name: str, machine: "GlobalStack",
  408. extruder_stack: Optional["ExtruderStack"]) -> "InstanceContainer":
  409. base_id = machine.definition.getId() if extruder_stack is None else extruder_stack.getId()
  410. new_id = base_id + "_" + new_name
  411. new_id = new_id.lower().replace(" ", "_")
  412. new_id = self._container_registry.uniqueName(new_id)
  413. # Create a new quality_changes container for the quality.
  414. quality_changes = InstanceContainer(new_id)
  415. quality_changes.setName(new_name)
  416. quality_changes.setMetaDataEntry("type", "quality_changes")
  417. quality_changes.setMetaDataEntry("quality_type", quality_type)
  418. # If we are creating a container for an extruder, ensure we add that to the container
  419. if extruder_stack is not None:
  420. quality_changes.setMetaDataEntry("position", extruder_stack.getMetaDataEntry("position"))
  421. # If the machine specifies qualities should be filtered, ensure we match the current criteria.
  422. machine_definition_id = getMachineDefinitionIDForQualitySearch(machine.definition)
  423. quality_changes.setDefinition(machine_definition_id)
  424. quality_changes.setMetaDataEntry("setting_version", self._application.SettingVersion)
  425. return quality_changes
  426. #
  427. # Gets the machine definition ID that can be used to search for Quality containers that are suitable for the given
  428. # machine. The rule is as follows:
  429. # 1. By default, the machine definition ID for quality container search will be "fdmprinter", which is the generic
  430. # machine.
  431. # 2. If a machine has its own machine quality (with "has_machine_quality = True"), we should use the given machine's
  432. # own machine definition ID for quality search.
  433. # Example: for an Ultimaker 3, the definition ID should be "ultimaker3".
  434. # 3. When condition (2) is met, AND the machine has "quality_definition" defined in its definition file, then the
  435. # definition ID specified in "quality_definition" should be used.
  436. # Example: for an Ultimaker 3 Extended, it has "quality_definition = ultimaker3". This means Ultimaker 3 Extended
  437. # shares the same set of qualities profiles as Ultimaker 3.
  438. #
  439. def getMachineDefinitionIDForQualitySearch(machine_definition: "DefinitionContainerInterface",
  440. default_definition_id: str = "fdmprinter") -> str:
  441. machine_definition_id = default_definition_id
  442. if parseBool(machine_definition.getMetaDataEntry("has_machine_quality", False)):
  443. # Only use the machine's own quality definition ID if this machine has machine quality.
  444. machine_definition_id = machine_definition.getMetaDataEntry("quality_definition")
  445. if machine_definition_id is None:
  446. machine_definition_id = machine_definition.getId()
  447. return machine_definition_id