MachineNode.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Dict, List
  4. from UM.Logger import Logger
  5. from UM.Signal import Signal
  6. from UM.Util import parseBool
  7. from UM.Settings.ContainerRegistry import ContainerRegistry # To find all the variants for this machine.
  8. from cura.Machines.ContainerNode import ContainerNode
  9. from cura.Machines.QualityChangesGroup import QualityChangesGroup # To construct groups of quality changes profiles that belong together.
  10. from cura.Machines.QualityGroup import QualityGroup # To construct groups of quality profiles that belong together.
  11. from cura.Machines.QualityNode import QualityNode
  12. from cura.Machines.VariantNode import VariantNode
  13. ## This class represents a machine in the container tree.
  14. #
  15. # The subnodes of these nodes are variants.
  16. class MachineNode(ContainerNode):
  17. def __init__(self, container_id: str) -> None:
  18. super().__init__(container_id)
  19. self.variants = {} # type: Dict[str, VariantNode] # Mapping variant names to their nodes.
  20. self.global_qualities = {} # type: Dict[str, QualityNode] # Mapping quality types to the global quality for those types.
  21. self.materialsChanged = Signal() # Emitted when one of the materials underneath this machine has been changed.
  22. container_registry = ContainerRegistry.getInstance()
  23. try:
  24. my_metadata = container_registry.findContainersMetadata(id = container_id)[0]
  25. except IndexError:
  26. Logger.log("Unable to find metadata for container %s", container_id)
  27. my_metadata = {}
  28. # Some of the metadata is cached upon construction here.
  29. # ONLY DO THAT FOR METADATA THAT DOESN'T CHANGE DURING RUNTIME!
  30. # Otherwise you need to keep it up-to-date during runtime.
  31. self.has_materials = parseBool(my_metadata.get("has_materials", "true"))
  32. self.has_variants = parseBool(my_metadata.get("has_variants", "false"))
  33. self.has_machine_quality = parseBool(my_metadata.get("has_machine_quality", "false"))
  34. self.quality_definition = my_metadata.get("quality_definition", container_id) if self.has_machine_quality else "fdmprinter"
  35. self.exclude_materials = my_metadata.get("exclude_materials", [])
  36. self.preferred_variant_name = my_metadata.get("preferred_variant_name", "")
  37. self.preferred_material = my_metadata.get("preferred_material", "")
  38. self.preferred_quality_type = my_metadata.get("preferred_quality_type", "")
  39. self._loadAll()
  40. ## Get the available quality groups for this machine.
  41. #
  42. # This returns all quality groups, regardless of whether they are
  43. # available to the combination of extruders or not. On the resulting
  44. # quality groups, the is_available property is set to indicate whether the
  45. # quality group can be selected according to the combination of extruders
  46. # in the parameters.
  47. # \param variant_names The names of the variants loaded in each extruder.
  48. # \param material_bases The base file names of the materials loaded in
  49. # each extruder.
  50. # \param extruder_enabled Whether or not the extruders are enabled. This
  51. # allows the function to set the is_available properly.
  52. # \return For each available quality type, a QualityGroup instance.
  53. def getQualityGroups(self, variant_names: List[str], material_bases: List[str], extruder_enabled: List[bool]) -> Dict[str, QualityGroup]:
  54. if len(variant_names) != len(material_bases) or len(variant_names) != len(extruder_enabled):
  55. Logger.log("e", "The number of extruders in the list of variants (" + str(len(variant_names)) + ") is not equal to the number of extruders in the list of materials (" + str(len(material_bases)) + ") or the list of enabled extruders (" + str(len(extruder_enabled)) + ").")
  56. return {}
  57. # For each extruder, find which quality profiles are available. Later we'll intersect the quality types.
  58. qualities_per_type_per_extruder = [{}] * len(variant_names) # type: List[Dict[str, QualityNode]]
  59. for extruder_nr, variant_name in enumerate(variant_names):
  60. if not extruder_enabled[extruder_nr]:
  61. continue # No qualities are available in this extruder. It'll get skipped when calculating the available quality types.
  62. material_base = material_bases[extruder_nr]
  63. if variant_name not in self.variants or material_base not in self.variants[variant_name].materials:
  64. # The printer has no variant/material-specific quality profiles. Use the global quality profiles.
  65. qualities_per_type_per_extruder[extruder_nr] = self.global_qualities
  66. else:
  67. # Use the actually specialised quality profiles.
  68. qualities_per_type_per_extruder[extruder_nr] = {node.getMetaDataEntry("quality_type"): node for node in self.variants[variant_name].materials[material_base].qualities.values()}
  69. # Create the quality group for each available type.
  70. quality_groups = {}
  71. for quality_type, global_quality_node in self.global_qualities.items():
  72. if not global_quality_node.container:
  73. Logger.log("w", "Node {0} doesn't have a container.".format(global_quality_node.container_id))
  74. continue
  75. # CURA-6599
  76. # Same as QualityChangesGroup.
  77. # For some reason, QML will get null or fail to convert type for MachineManager.activeQualityChangesGroup() to
  78. # a QObject. Setting the object ownership to QQmlEngine.CppOwnership doesn't work, but setting the object
  79. # parent to application seems to work.
  80. from cura.CuraApplication import CuraApplication
  81. quality_groups[quality_type] = QualityGroup(name = global_quality_node.container.getMetaDataEntry("name", "Unnamed profile"),
  82. quality_type = quality_type,
  83. parent = CuraApplication.getInstance())
  84. quality_groups[quality_type].node_for_global = global_quality_node
  85. for extruder, qualities_per_type in enumerate(qualities_per_type_per_extruder):
  86. if quality_type in qualities_per_type:
  87. quality_groups[quality_type].nodes_for_extruders[extruder] = qualities_per_type[quality_type]
  88. available_quality_types = set(quality_groups.keys())
  89. for extruder_nr, qualities_per_type in enumerate(qualities_per_type_per_extruder):
  90. if not extruder_enabled[extruder_nr]:
  91. continue
  92. available_quality_types.intersection_update(qualities_per_type.keys())
  93. for quality_type in available_quality_types:
  94. quality_groups[quality_type].is_available = True
  95. return quality_groups
  96. ## Returns all of the quality changes groups available to this printer.
  97. #
  98. # The quality changes groups store which quality type and intent category
  99. # they were made for, but not which material and nozzle. Instead for the
  100. # quality type and intent category, the quality changes will always be
  101. # available but change the quality type and intent category when
  102. # activated.
  103. #
  104. # The quality changes group does depend on the printer: Which quality
  105. # definition is used.
  106. #
  107. # The quality changes groups that are available do depend on the quality
  108. # types that are available, so it must still be known which extruders are
  109. # enabled and which materials and variants are loaded in them. This allows
  110. # setting the correct is_available flag.
  111. # \param variant_names The names of the variants loaded in each extruder.
  112. # \param material_bases The base file names of the materials loaded in
  113. # each extruder.
  114. # \param extruder_enabled For each extruder whether or not they are
  115. # enabled.
  116. # \return List of all quality changes groups for the printer.
  117. def getQualityChangesGroups(self, variant_names: List[str], material_bases: List[str], extruder_enabled: List[bool]) -> List[QualityChangesGroup]:
  118. machine_quality_changes = ContainerRegistry.getInstance().findContainersMetadata(type = "quality_changes", definition = self.quality_definition) # All quality changes for each extruder.
  119. groups_by_name = {} #type: Dict[str, QualityChangesGroup] # Group quality changes profiles by their display name. The display name must be unique for quality changes. This finds profiles that belong together in a group.
  120. for quality_changes in machine_quality_changes:
  121. name = quality_changes["name"]
  122. if name not in groups_by_name:
  123. # CURA-6599
  124. # For some reason, QML will get null or fail to convert type for MachineManager.activeQualityChangesGroup() to
  125. # a QObject. Setting the object ownership to QQmlEngine.CppOwnership doesn't work, but setting the object
  126. # parent to application seems to work.
  127. from cura.CuraApplication import CuraApplication
  128. groups_by_name[name] = QualityChangesGroup(name, quality_type = quality_changes["quality_type"],
  129. intent_category = quality_changes.get("intent_category", "default"),
  130. parent = CuraApplication.getInstance())
  131. elif groups_by_name[name].intent_category == "default": # Intent category should be stored as "default" if everything is default or as the intent if any of the extruder have an actual intent.
  132. groups_by_name[name].intent_category = quality_changes.get("intent_category", "default")
  133. if "position" in quality_changes: # An extruder profile.
  134. groups_by_name[name].metadata_per_extruder[int(quality_changes["position"])] = quality_changes
  135. else: # Global profile.
  136. groups_by_name[name].metadata_for_global = quality_changes
  137. quality_groups = self.getQualityGroups(variant_names, material_bases, extruder_enabled)
  138. for quality_changes_group in groups_by_name.values():
  139. if quality_changes_group.quality_type not in quality_groups:
  140. quality_changes_group.is_available = False
  141. else:
  142. # Quality changes group is available iff the quality group it depends on is available. Irrespective of whether the intent category is available.
  143. quality_changes_group.is_available = quality_groups[quality_changes_group.quality_type].is_available
  144. return list(groups_by_name.values())
  145. ## Gets the preferred global quality node, going by the preferred quality
  146. # type.
  147. #
  148. # If the preferred global quality is not in there, an arbitrary global
  149. # quality is taken.
  150. # If there are no global qualities, an empty quality is returned.
  151. def preferredGlobalQuality(self) -> "QualityNode":
  152. return self.global_qualities.get(self.preferred_quality_type, next(iter(self.global_qualities.values())))
  153. ## (Re)loads all variants under this printer.
  154. def _loadAll(self):
  155. container_registry = ContainerRegistry.getInstance()
  156. if not self.has_variants:
  157. self.variants["empty"] = VariantNode("empty_variant", machine = self)
  158. else:
  159. # Find all the variants for this definition ID.
  160. variants = container_registry.findInstanceContainersMetadata(type = "variant", definition = self.container_id, hardware_type = "nozzle")
  161. for variant in variants:
  162. variant_name = variant["name"]
  163. if variant_name not in self.variants:
  164. self.variants[variant_name] = VariantNode(variant["id"], machine = self)
  165. self.variants[variant_name].materialsChanged.connect(self.materialsChanged)
  166. if not self.variants:
  167. self.variants["empty"] = VariantNode("empty_variant", machine = self)
  168. # Find the global qualities for this printer.
  169. global_qualities = container_registry.findInstanceContainersMetadata(type = "quality", definition = self.quality_definition, global_quality = "True") # First try specific to this printer.
  170. if len(global_qualities) == 0: # This printer doesn't override the global qualities.
  171. global_qualities = container_registry.findInstanceContainersMetadata(type = "quality", definition = "fdmprinter", global_quality = "True") # Otherwise pick the global global qualities.
  172. for global_quality in global_qualities:
  173. self.global_qualities[global_quality["quality_type"]] = QualityNode(global_quality["id"], parent = self)