QualityManager.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. # This determines if we should only get the global qualities for the global stack and skip the global qualities for the extruder stacks
  160. has_machine_specific_qualities = machine.getHasMachineQuality()
  161. # To find the quality container for the GlobalStack, check in the following fall-back manner:
  162. # (1) the machine-specific node
  163. # (2) the generic node
  164. machine_node = self._machine_nozzle_buildplate_material_quality_type_to_quality_dict.get(machine_definition_id)
  165. # Check if this machine has specific quality profiles for its extruders, if so, when looking up extruder
  166. # qualities, we should not fall back to use the global qualities.
  167. has_extruder_specific_qualities = False
  168. if machine_node:
  169. if machine_node.children_map:
  170. has_extruder_specific_qualities = True
  171. default_machine_node = self._machine_nozzle_buildplate_material_quality_type_to_quality_dict.get(self._default_machine_definition_id)
  172. nodes_to_check = [] # type: List[QualityNode]
  173. if machine_node is not None:
  174. nodes_to_check.append(machine_node)
  175. if default_machine_node is not None:
  176. nodes_to_check.append(default_machine_node)
  177. # Iterate over all quality_types in the machine node
  178. quality_group_dict = {}
  179. for node in nodes_to_check:
  180. if node and node.quality_type_map:
  181. quality_node = list(node.quality_type_map.values())[0]
  182. is_global_quality = parseBool(quality_node.getMetaDataEntry("global_quality", False))
  183. if not is_global_quality:
  184. continue
  185. for quality_type, quality_node in node.quality_type_map.items():
  186. quality_group = QualityGroup(quality_node.getMetaDataEntry("name", ""), quality_type)
  187. quality_group.setGlobalNode(quality_node)
  188. quality_group_dict[quality_type] = quality_group
  189. break
  190. buildplate_name = machine.getBuildplateName()
  191. # Iterate over all extruders to find quality containers for each extruder
  192. for position, extruder in machine.extruders.items():
  193. nozzle_name = None
  194. if extruder.variant.getId() != "empty_variant":
  195. nozzle_name = extruder.variant.getName()
  196. # This is a list of root material IDs to use for searching for suitable quality profiles.
  197. # The root material IDs in this list are in prioritized order.
  198. root_material_id_list = []
  199. has_material = False # flag indicating whether this extruder has a material assigned
  200. root_material_id = None
  201. if extruder.material.getId() != "empty_material":
  202. has_material = True
  203. root_material_id = extruder.material.getMetaDataEntry("base_file")
  204. # Convert possible generic_pla_175 -> generic_pla
  205. root_material_id = self._material_manager.getRootMaterialIDWithoutDiameter(root_material_id)
  206. root_material_id_list.append(root_material_id)
  207. # Also try to get the fallback materials
  208. fallback_ids = self._material_manager.getFallBackMaterialIdsByMaterial(extruder.material)
  209. if fallback_ids:
  210. root_material_id_list.extend(fallback_ids)
  211. # Weed out duplicates while preserving the order.
  212. seen = set() # type: Set[str]
  213. root_material_id_list = [x for x in root_material_id_list if x not in seen and not seen.add(x)] # type: ignore
  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.setExtruderNode(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.setGlobalNode(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 = self._application.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: "DefinitionContainerInterface",
  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