QualityManager.py 29 KB

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