ExtruderStack.py 7.9 KB

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