ExtruderStack.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # Copyright (c) 2024 UltiMaker
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any, Dict, TYPE_CHECKING, Optional
  4. from PyQt6.QtCore import pyqtProperty, pyqtSignal
  5. from UM.Decorators import CachedMemberFunctions, override
  6. from UM.MimeTypeDatabase import MimeType, MimeTypeDatabase
  7. from UM.Settings.ContainerStack import ContainerStack
  8. from UM.Settings.ContainerRegistry import ContainerRegistry
  9. from UM.Settings.Interfaces import ContainerInterface, PropertyEvaluationContext
  10. from UM.Util import parseBool
  11. from . import Exceptions
  12. from .CuraContainerStack import CuraContainerStack, _ContainerIndexes
  13. if TYPE_CHECKING:
  14. from cura.Settings.GlobalStack import GlobalStack
  15. class ExtruderStack(CuraContainerStack):
  16. """Represents an Extruder and its related containers."""
  17. def __init__(self, container_id: str) -> None:
  18. super().__init__(container_id)
  19. self.setMetaDataEntry("type", "extruder_train") # For backward compatibility
  20. self.propertiesChanged.connect(self._onPropertiesChanged)
  21. self.setDirty(False)
  22. enabledChanged = pyqtSignal()
  23. @override(ContainerStack)
  24. def setNextStack(self, stack: CuraContainerStack, connect_signals: bool = True) -> None:
  25. """Overridden from ContainerStack
  26. This will set the next stack and ensure that we register this stack as an extruder.
  27. """
  28. super().setNextStack(stack)
  29. stack.addExtruder(self)
  30. self.setMetaDataEntry("machine", stack.id)
  31. @override(ContainerStack)
  32. def getNextStack(self) -> Optional["GlobalStack"]:
  33. return super().getNextStack()
  34. @pyqtProperty(int, constant = True)
  35. def position(self) -> int:
  36. return int(self.getMetaDataEntry("position"))
  37. def setEnabled(self, enabled: bool) -> None:
  38. if self.getMetaDataEntry("enabled", True) == enabled: # No change.
  39. return # Don't emit a signal then.
  40. self.setMetaDataEntry("enabled", str(enabled))
  41. self.enabledChanged.emit()
  42. @pyqtProperty(bool, notify = enabledChanged)
  43. def isEnabled(self) -> bool:
  44. return parseBool(self.getMetaDataEntry("enabled", "True"))
  45. @classmethod
  46. def getLoadingPriority(cls) -> int:
  47. return 3
  48. compatibleMaterialDiameterChanged = pyqtSignal()
  49. def getCompatibleMaterialDiameter(self) -> float:
  50. """Return the filament diameter that the machine requires.
  51. If the machine has no requirement for the diameter, -1 is returned.
  52. :return: The filament diameter for the printer
  53. """
  54. context = PropertyEvaluationContext(self)
  55. context.context["evaluate_from_container_index"] = _ContainerIndexes.Variant
  56. return float(self.getProperty("material_diameter", "value", context = context))
  57. def setCompatibleMaterialDiameter(self, value: float) -> None:
  58. old_approximate_diameter = self.getApproximateMaterialDiameter()
  59. if self.getCompatibleMaterialDiameter() != value:
  60. CachedMemberFunctions.clearInstanceCache(self)
  61. self.definitionChanges.setProperty("material_diameter", "value", value)
  62. self.compatibleMaterialDiameterChanged.emit()
  63. # Emit approximate diameter changed signal if needed
  64. if old_approximate_diameter != self.getApproximateMaterialDiameter():
  65. self.approximateMaterialDiameterChanged.emit()
  66. compatibleMaterialDiameter = pyqtProperty(float, fset = setCompatibleMaterialDiameter,
  67. fget = getCompatibleMaterialDiameter,
  68. notify = compatibleMaterialDiameterChanged)
  69. approximateMaterialDiameterChanged = pyqtSignal()
  70. def getApproximateMaterialDiameter(self) -> float:
  71. """Return the approximate filament diameter that the machine requires.
  72. The approximate material diameter is the material diameter rounded to
  73. the nearest millimetre.
  74. If the machine has no requirement for the diameter, -1 is returned.
  75. :return: The approximate filament diameter for the printer
  76. """
  77. return round(self.getCompatibleMaterialDiameter())
  78. approximateMaterialDiameter = pyqtProperty(float, fget = getApproximateMaterialDiameter,
  79. notify = approximateMaterialDiameterChanged)
  80. @override(ContainerStack)
  81. def getProperty(self, key: str, property_name: str, context: Optional[PropertyEvaluationContext] = None) -> Any:
  82. """Overridden from ContainerStack
  83. It will perform a few extra checks when trying to get properties.
  84. The two extra checks it currently does is to ensure a next stack is set and to bypass
  85. the extruder when the property is not settable per extruder.
  86. :throws Exceptions.NoGlobalStackError Raised when trying to get a property from an extruder without
  87. having a next stack set.
  88. """
  89. if not self._next_stack:
  90. raise Exceptions.NoGlobalStackError("Extruder {id} is missing the next stack!".format(id = self.id))
  91. if context:
  92. context.pushContainer(self)
  93. if not super().getProperty(key, "settable_per_extruder", context):
  94. result = self.getNextStack().getProperty(key, property_name, context)
  95. if context:
  96. context.popContainer()
  97. return result
  98. if not context:
  99. context = PropertyEvaluationContext(self)
  100. if "extruder_position" not in context.context:
  101. context.context["extruder_position"] = super().getProperty(key, "limit_to_extruder", context)
  102. limit_to_extruder = context.context["extruder_position"]
  103. if limit_to_extruder is not None:
  104. limit_to_extruder = str(limit_to_extruder)
  105. if (limit_to_extruder is not None and limit_to_extruder != "-1") and self.getMetaDataEntry("position") != str(limit_to_extruder):
  106. try:
  107. result = self.getNextStack().extruderList[int(limit_to_extruder)].getProperty(key, property_name, context)
  108. if result is not None:
  109. if context:
  110. context.popContainer()
  111. return result
  112. except IndexError:
  113. pass
  114. result = super().getProperty(key, property_name, context)
  115. if context:
  116. context.popContainer()
  117. return result
  118. @override(CuraContainerStack)
  119. def _getMachineDefinition(self) -> ContainerInterface:
  120. if not self.getNextStack():
  121. raise Exceptions.NoGlobalStackError("Extruder {id} is missing the next stack!".format(id = self.id))
  122. return self.getNextStack()._getMachineDefinition()
  123. @override(CuraContainerStack)
  124. def deserialize(self, contents: str, file_name: Optional[str] = None) -> None:
  125. super().deserialize(contents, file_name)
  126. if "enabled" not in self.getMetaData():
  127. self.setMetaDataEntry("enabled", "True")
  128. def _onPropertiesChanged(self, key: str, properties: Dict[str, Any]) -> None:
  129. # When there is a setting that is not settable per extruder that depends on a value from a setting that is,
  130. # we do not always get properly informed that we should re-evaluate the setting. So make sure to indicate
  131. # something changed for those settings.
  132. if not self.getNextStack():
  133. return #There are no global settings to depend on.
  134. definitions = self.getNextStack().definition.findDefinitions(key = key)
  135. if definitions:
  136. has_global_dependencies = False
  137. for relation in definitions[0].relations:
  138. if not getattr(relation.target, "settable_per_extruder", True):
  139. has_global_dependencies = True
  140. break
  141. if has_global_dependencies:
  142. self.getNextStack().propertiesChanged.emit(key, properties)
  143. extruder_stack_mime = MimeType(
  144. name = "application/x-cura-extruderstack",
  145. comment = "Cura Extruder Stack",
  146. suffixes = ["extruder.cfg"]
  147. )
  148. MimeTypeDatabase.addMimeType(extruder_stack_mime)
  149. ContainerRegistry.addContainerTypeByName(ExtruderStack, "extruder_stack", extruder_stack_mime.name)