QualityManager.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. # This collects a lot of quality and quality changes related code which was split between ContainerManager
  4. # and the MachineManager and really needs to usable from both.
  5. from typing import List, Optional, Dict, TYPE_CHECKING
  6. from UM.Application import Application
  7. from UM.Settings.ContainerRegistry import ContainerRegistry
  8. from UM.Settings.DefinitionContainer import DefinitionContainer
  9. from UM.Settings.InstanceContainer import InstanceContainer
  10. from cura.Settings.ExtruderManager import ExtruderManager
  11. if TYPE_CHECKING:
  12. from cura.Settings.GlobalStack import GlobalStack
  13. from cura.Settings.ExtruderStack import ExtruderStack
  14. from UM.Settings.DefinitionContainer import DefinitionContainerInterface
  15. class QualityManager:
  16. ## Get the singleton instance for this class.
  17. @classmethod
  18. def getInstance(cls) -> "QualityManager":
  19. # Note: Explicit use of class name to prevent issues with inheritance.
  20. if not QualityManager.__instance:
  21. QualityManager.__instance = cls()
  22. return QualityManager.__instance
  23. __instance = None # type: "QualityManager"
  24. ## Find a quality by name for a specific machine definition and materials.
  25. #
  26. # \param quality_name
  27. # \param machine_definition (Optional) \type{DefinitionContainerInterface} If nothing is
  28. # specified then the currently selected machine definition is used.
  29. # \param material_containers (Optional) \type{List[InstanceContainer]} If nothing is specified then
  30. # the current set of selected materials is used.
  31. # \return the matching quality container \type{InstanceContainer}
  32. def findQualityByName(self, quality_name: str, machine_definition: Optional["DefinitionContainerInterface"] = None, material_containers: List[InstanceContainer] = None) -> Optional[InstanceContainer]:
  33. criteria = {"type": "quality", "name": quality_name}
  34. result = self._getFilteredContainersForStack(machine_definition, material_containers, **criteria)
  35. # Fall back to using generic materials and qualities if nothing could be found.
  36. if not result and material_containers and len(material_containers) == 1:
  37. basic_materials = self._getBasicMaterials(material_containers[0])
  38. result = self._getFilteredContainersForStack(machine_definition, basic_materials, **criteria)
  39. return result[0] if result else None
  40. ## Find a quality changes container by name.
  41. #
  42. # \param quality_changes_name \type{str} the name of the quality changes container.
  43. # \param machine_definition (Optional) \type{DefinitionContainer} If nothing is
  44. # specified then the currently selected machine definition is used..
  45. # \return the matching quality changes containers \type{List[InstanceContainer]}
  46. def findQualityChangesByName(self, quality_changes_name: str, machine_definition: Optional["DefinitionContainerInterface"] = None):
  47. if not machine_definition:
  48. global_stack = Application.getGlobalContainerStack()
  49. if not global_stack:
  50. return [] #No stack, so no current definition could be found, so there are no quality changes either.
  51. machine_definition = global_stack.definition
  52. result = self.findAllQualityChangesForMachine(machine_definition)
  53. result = [quality_change for quality_change in result if quality_change.getName() == quality_changes_name]
  54. return result
  55. ## Fetch the list of available quality types for this combination of machine definition and materials.
  56. #
  57. # \param machine_definition \type{DefinitionContainer}
  58. # \param material_containers \type{List[InstanceContainer]}
  59. # \return \type{List[str]}
  60. def findAllQualityTypesForMachineAndMaterials(self, machine_definition: "DefinitionContainerInterface", material_containers: List[InstanceContainer]) -> List[str]:
  61. # Determine the common set of quality types which can be
  62. # applied to all of the materials for this machine.
  63. quality_type_dict = self.__fetchQualityTypeDictForMaterial(machine_definition, material_containers[0])
  64. common_quality_types = set(quality_type_dict.keys())
  65. for material_container in material_containers[1:]:
  66. next_quality_type_dict = self.__fetchQualityTypeDictForMaterial(machine_definition, material_container)
  67. common_quality_types.intersection_update(set(next_quality_type_dict.keys()))
  68. return list(common_quality_types)
  69. def findAllQualitiesForMachineAndMaterials(self, machine_definition: "DefinitionContainerInterface", material_containers: List[InstanceContainer]) -> List[InstanceContainer]:
  70. # Determine the common set of quality types which can be
  71. # applied to all of the materials for this machine.
  72. quality_type_dict = self.__fetchQualityTypeDictForMaterial(machine_definition, material_containers[0])
  73. qualities = set(quality_type_dict.values())
  74. for material_container in material_containers[1:]:
  75. next_quality_type_dict = self.__fetchQualityTypeDictForMaterial(machine_definition, material_container)
  76. qualities.update(set(next_quality_type_dict.values()))
  77. return list(qualities)
  78. ## Fetches a dict of quality types names to quality profiles for a combination of machine and material.
  79. #
  80. # \param machine_definition \type{DefinitionContainer} the machine definition.
  81. # \param material \type{InstanceContainer} the material.
  82. # \return \type{Dict[str, InstanceContainer]} the dict of suitable quality type names mapping to qualities.
  83. def __fetchQualityTypeDictForMaterial(self, machine_definition: "DefinitionContainerInterface", material: InstanceContainer) -> Dict[str, InstanceContainer]:
  84. qualities = self.findAllQualitiesForMachineMaterial(machine_definition, material)
  85. quality_type_dict = {}
  86. for quality in qualities:
  87. quality_type_dict[quality.getMetaDataEntry("quality_type")] = quality
  88. return quality_type_dict
  89. ## Find a quality container by quality type.
  90. #
  91. # \param quality_type \type{str} the name of the quality type to search for.
  92. # \param machine_definition (Optional) \type{InstanceContainer} If nothing is
  93. # specified then the currently selected machine definition is used.
  94. # \param material_containers (Optional) \type{List[InstanceContainer]} If nothing is specified then
  95. # the current set of selected materials is used.
  96. # \return the matching quality container \type{InstanceContainer}
  97. def findQualityByQualityType(self, quality_type: str, machine_definition: Optional["DefinitionContainerInterface"] = None, material_containers: List[InstanceContainer] = None, **kwargs) -> InstanceContainer:
  98. criteria = kwargs
  99. criteria["type"] = "quality"
  100. if quality_type:
  101. criteria["quality_type"] = quality_type
  102. result = self._getFilteredContainersForStack(machine_definition, material_containers, **criteria)
  103. # Fall back to using generic materials and qualities if nothing could be found.
  104. if not result and material_containers and len(material_containers) == 1:
  105. basic_materials = self._getBasicMaterials(material_containers[0])
  106. if basic_materials:
  107. result = self._getFilteredContainersForStack(machine_definition, basic_materials, **criteria)
  108. return result[0] if result else None
  109. ## Find all suitable qualities for a combination of machine and material.
  110. #
  111. # \param machine_definition \type{DefinitionContainer} the machine definition.
  112. # \param material_container \type{InstanceContainer} the material.
  113. # \return \type{List[InstanceContainer]} the list of suitable qualities.
  114. def findAllQualitiesForMachineMaterial(self, machine_definition: "DefinitionContainerInterface", material_container: InstanceContainer) -> List[InstanceContainer]:
  115. criteria = {"type": "quality"}
  116. result = self._getFilteredContainersForStack(machine_definition, [material_container], **criteria)
  117. if not result:
  118. basic_materials = self._getBasicMaterials(material_container)
  119. if basic_materials:
  120. result = self._getFilteredContainersForStack(machine_definition, basic_materials, **criteria)
  121. return result
  122. ## Find all quality changes for a machine.
  123. #
  124. # \param machine_definition \type{DefinitionContainer} the machine definition.
  125. # \return \type{List[InstanceContainer]} the list of quality changes
  126. def findAllQualityChangesForMachine(self, machine_definition: "DefinitionContainerInterface") -> List[InstanceContainer]:
  127. if machine_definition.getMetaDataEntry("has_machine_quality"):
  128. definition_id = machine_definition.getId()
  129. else:
  130. definition_id = "fdmprinter"
  131. filter_dict = { "type": "quality_changes", "definition": definition_id }
  132. quality_changes_list = ContainerRegistry.getInstance().findInstanceContainers(**filter_dict)
  133. return quality_changes_list
  134. def findAllExtruderDefinitionsForMachine(self, machine_definition: "DefinitionContainerInterface") -> List["DefinitionContainerInterface"]:
  135. filter_dict = { "machine": machine_definition.getId() }
  136. return ContainerRegistry.getInstance().findDefinitionContainers(**filter_dict)
  137. ## Find all quality changes for a given extruder.
  138. #
  139. # \param extruder_definition The extruder to find the quality changes for.
  140. # \return The list of quality changes for the given extruder.
  141. def findAllQualityChangesForExtruder(self, extruder_definition: "DefinitionContainerInterface") -> List[InstanceContainer]:
  142. filter_dict = {"type": "quality_changes", "extruder": extruder_definition.getId()}
  143. return ContainerRegistry.getInstance().findInstanceContainers(**filter_dict)
  144. ## Find all usable qualities for a machine and extruders.
  145. #
  146. # Finds all of the qualities for this combination of machine and extruders.
  147. # Only one quality per quality type is returned. i.e. if there are 2 qualities with quality_type=normal
  148. # then only one of then is returned (at random).
  149. #
  150. # \param global_container_stack \type{GlobalStack} the global machine definition
  151. # \param extruder_stacks \type{List[ExtruderStack]} the list of extruder stacks
  152. # \return \type{List[InstanceContainer]} the list of the matching qualities. The quality profiles
  153. # return come from the first extruder in the given list of extruders.
  154. def findAllUsableQualitiesForMachineAndExtruders(self, global_container_stack: "GlobalStack", extruder_stacks: List["ExtruderStack"]) -> List[InstanceContainer]:
  155. global_machine_definition = global_container_stack.getBottom()
  156. if extruder_stacks:
  157. # Multi-extruder machine detected.
  158. materials = [stack.material for stack in extruder_stacks]
  159. else:
  160. # Machine with one extruder.
  161. materials = [global_container_stack.material]
  162. quality_types = self.findAllQualityTypesForMachineAndMaterials(global_machine_definition, materials)
  163. # Map the list of quality_types to InstanceContainers
  164. qualities = self.findAllQualitiesForMachineMaterial(global_machine_definition, materials[0])
  165. quality_type_dict = {}
  166. for quality in qualities:
  167. quality_type_dict[quality.getMetaDataEntry("quality_type")] = quality
  168. return [quality_type_dict[quality_type] for quality_type in quality_types]
  169. ## Fetch more basic versions of a material.
  170. #
  171. # This tries to find a generic or basic version of the given material.
  172. # \param material_container \type{InstanceContainer} the material
  173. # \return \type{List[InstanceContainer]} a list of the basic materials or an empty list if one could not be found.
  174. def _getBasicMaterials(self, material_container: InstanceContainer):
  175. base_material = material_container.getMetaDataEntry("material")
  176. material_container_definition = material_container.getDefinition()
  177. if material_container_definition and material_container_definition.getMetaDataEntry("has_machine_quality"):
  178. definition_id = material_container.getDefinition().getMetaDataEntry("quality_definition", material_container.getDefinition().getId())
  179. else:
  180. definition_id = "fdmprinter"
  181. if base_material:
  182. # There is a basic material specified
  183. criteria = { "type": "material", "name": base_material, "definition": definition_id }
  184. containers = ContainerRegistry.getInstance().findInstanceContainers(**criteria)
  185. containers = [basic_material for basic_material in containers if
  186. basic_material.getMetaDataEntry("variant") == material_container.getMetaDataEntry(
  187. "variant")]
  188. return containers
  189. return []
  190. def _getFilteredContainers(self, **kwargs):
  191. return self._getFilteredContainersForStack(None, None, **kwargs)
  192. def _getFilteredContainersForStack(self, machine_definition: "DefinitionContainerInterface" = None, material_containers: List[InstanceContainer] = None, **kwargs):
  193. # Fill in any default values.
  194. if machine_definition is None:
  195. machine_definition = Application.getInstance().getGlobalContainerStack().getBottom()
  196. quality_definition_id = machine_definition.getMetaDataEntry("quality_definition")
  197. if quality_definition_id is not None:
  198. machine_definition = ContainerRegistry.getInstance().findDefinitionContainers(id=quality_definition_id)[0]
  199. # for convenience
  200. if material_containers is None:
  201. material_containers = []
  202. if not material_containers:
  203. active_stacks = ExtruderManager.getInstance().getActiveGlobalAndExtruderStacks()
  204. if active_stacks:
  205. material_containers = [stack.material for stack in active_stacks]
  206. criteria = kwargs
  207. filter_by_material = False
  208. machine_definition = self.getParentMachineDefinition(machine_definition)
  209. criteria["definition"] = machine_definition.getId()
  210. found_containers_with_machine_definition = ContainerRegistry.getInstance().findInstanceContainers(**criteria)
  211. whole_machine_definition = self.getWholeMachineDefinition(machine_definition)
  212. if whole_machine_definition.getMetaDataEntry("has_machine_quality"):
  213. definition_id = machine_definition.getMetaDataEntry("quality_definition", whole_machine_definition.getId())
  214. criteria["definition"] = definition_id
  215. filter_by_material = whole_machine_definition.getMetaDataEntry("has_materials")
  216. # only fall back to "fdmprinter" when there is no container for this machine
  217. elif not found_containers_with_machine_definition:
  218. criteria["definition"] = "fdmprinter"
  219. # Stick the material IDs in a set
  220. material_ids = set()
  221. for material_instance in material_containers:
  222. if material_instance is not None:
  223. # Add the parent material too.
  224. for basic_material in self._getBasicMaterials(material_instance):
  225. material_ids.add(basic_material.getId())
  226. material_ids.add(material_instance.getId())
  227. containers = ContainerRegistry.getInstance().findInstanceContainers(**criteria)
  228. result = []
  229. for container in containers:
  230. # If the machine specifies we should filter by material, exclude containers that do not match any active material.
  231. if filter_by_material and container.getMetaDataEntry("material") not in material_ids and "global_quality" not in kwargs:
  232. continue
  233. result.append(container)
  234. return result
  235. ## Get the parent machine definition of a machine definition.
  236. #
  237. # \param machine_definition \type{DefinitionContainer} This may be a normal machine definition or
  238. # an extruder definition.
  239. # \return \type{DefinitionContainer} the parent machine definition. If the given machine
  240. # definition doesn't have a parent then it is simply returned.
  241. def getParentMachineDefinition(self, machine_definition: "DefinitionContainerInterface") -> "DefinitionContainerInterface":
  242. container_registry = ContainerRegistry.getInstance()
  243. machine_entry = machine_definition.getMetaDataEntry("machine")
  244. if machine_entry is None:
  245. # We have a normal (whole) machine defintion
  246. quality_definition = machine_definition.getMetaDataEntry("quality_definition")
  247. if quality_definition is not None:
  248. parent_machine_definition = container_registry.findDefinitionContainers(id=quality_definition)[0]
  249. return self.getParentMachineDefinition(parent_machine_definition)
  250. else:
  251. return machine_definition
  252. else:
  253. # This looks like an extruder. Find the rest of the machine.
  254. whole_machine = container_registry.findDefinitionContainers(id=machine_entry)[0]
  255. parent_machine = self.getParentMachineDefinition(whole_machine)
  256. if whole_machine is parent_machine:
  257. # This extruder already belongs to a 'parent' machine def.
  258. return machine_definition
  259. else:
  260. # Look up the corresponding extruder definition in the parent machine definition.
  261. extruder_position = machine_definition.getMetaDataEntry("position")
  262. parent_extruder_id = parent_machine.getMetaDataEntry("machine_extruder_trains")[extruder_position]
  263. return container_registry.findDefinitionContainers(id=parent_extruder_id)[0]
  264. ## Get the whole/global machine definition from an extruder definition.
  265. #
  266. # \param machine_definition \type{DefinitionContainer} This may be a normal machine definition or
  267. # an extruder definition.
  268. # \return \type{DefinitionContainerInterface}
  269. def getWholeMachineDefinition(self, machine_definition: "DefinitionContainerInterface") -> "DefinitionContainerInterface":
  270. machine_entry = machine_definition.getMetaDataEntry("machine")
  271. if machine_entry is None:
  272. # This already is a 'global' machine definition.
  273. return machine_definition
  274. else:
  275. container_registry = ContainerRegistry.getInstance()
  276. whole_machine = container_registry.findDefinitionContainers(id=machine_entry)[0]
  277. return whole_machine