ExtruderStack.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 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. self.definitionChanges.setProperty("material_diameter", "value", value)
  63. self.compatibleMaterialDiameterChanged.emit()
  64. # Emit approximate diameter changed signal if needed
  65. if old_approximate_diameter != self.getApproximateMaterialDiameter():
  66. self.approximateMaterialDiameterChanged.emit()
  67. compatibleMaterialDiameter = pyqtProperty(float, fset = setCompatibleMaterialDiameter,
  68. fget = getCompatibleMaterialDiameter,
  69. notify = compatibleMaterialDiameterChanged)
  70. approximateMaterialDiameterChanged = pyqtSignal()
  71. def getApproximateMaterialDiameter(self) -> float:
  72. """Return the approximate filament diameter that the machine requires.
  73. The approximate material diameter is the material diameter rounded to
  74. the nearest millimetre.
  75. If the machine has no requirement for the diameter, -1 is returned.
  76. :return: The approximate filament diameter for the printer
  77. """
  78. return round(self.getCompatibleMaterialDiameter())
  79. approximateMaterialDiameter = pyqtProperty(float, fget = getApproximateMaterialDiameter,
  80. notify = approximateMaterialDiameterChanged)
  81. @override(ContainerStack)
  82. def getProperty(self, key: str, property_name: str, context: Optional[PropertyEvaluationContext] = None) -> Any:
  83. """Overridden from ContainerStack
  84. It will perform a few extra checks when trying to get properties.
  85. The two extra checks it currently does is to ensure a next stack is set and to bypass
  86. the extruder when the property is not settable per extruder.
  87. :throws Exceptions.NoGlobalStackError Raised when trying to get a property from an extruder without
  88. having a next stack set.
  89. """
  90. if not self._next_stack:
  91. raise Exceptions.NoGlobalStackError("Extruder {id} is missing the next stack!".format(id = self.id))
  92. if context:
  93. context.pushContainer(self)
  94. if not super().getProperty(key, "settable_per_extruder", context):
  95. result = self.getNextStack().getProperty(key, property_name, context)
  96. if context:
  97. context.popContainer()
  98. return result
  99. limit_to_extruder = super().getProperty(key, "limit_to_extruder", context)
  100. if limit_to_extruder is not None:
  101. if limit_to_extruder == -1:
  102. limit_to_extruder = int(cura.CuraApplication.CuraApplication.getInstance().getMachineManager().defaultExtruderPosition)
  103. limit_to_extruder = str(limit_to_extruder)
  104. if (limit_to_extruder is not None and limit_to_extruder != "-1") and self.getMetaDataEntry("position") != str(limit_to_extruder):
  105. try:
  106. result = self.getNextStack().extruderList[int(limit_to_extruder)].getProperty(key, property_name, context)
  107. if result is not None:
  108. if context:
  109. context.popContainer()
  110. return result
  111. except IndexError:
  112. pass
  113. result = super().getProperty(key, property_name, context)
  114. if context:
  115. context.popContainer()
  116. return result
  117. @override(CuraContainerStack)
  118. def _getMachineDefinition(self) -> ContainerInterface:
  119. if not self.getNextStack():
  120. raise Exceptions.NoGlobalStackError("Extruder {id} is missing the next stack!".format(id = self.id))
  121. return self.getNextStack()._getMachineDefinition()
  122. @override(CuraContainerStack)
  123. def deserialize(self, contents: str, file_name: Optional[str] = None) -> None:
  124. super().deserialize(contents, file_name)
  125. if "enabled" not in self.getMetaData():
  126. self.setMetaDataEntry("enabled", "True")
  127. def _onPropertiesChanged(self, key: str, properties: Dict[str, Any]) -> None:
  128. # When there is a setting that is not settable per extruder that depends on a value from a setting that is,
  129. # we do not always get properly informed that we should re-evaluate the setting. So make sure to indicate
  130. # something changed for those settings.
  131. if not self.getNextStack():
  132. return #There are no global settings to depend on.
  133. definitions = self.getNextStack().definition.findDefinitions(key = key)
  134. if definitions:
  135. has_global_dependencies = False
  136. for relation in definitions[0].relations:
  137. if not getattr(relation.target, "settable_per_extruder", True):
  138. has_global_dependencies = True
  139. break
  140. if has_global_dependencies:
  141. self.getNextStack().propertiesChanged.emit(key, properties)
  142. extruder_stack_mime = MimeType(
  143. name = "application/x-cura-extruderstack",
  144. comment = "Cura Extruder Stack",
  145. suffixes = ["extruder.cfg"]
  146. )
  147. MimeTypeDatabase.addMimeType(extruder_stack_mime)
  148. ContainerRegistry.addContainerTypeByName(ExtruderStack, "extruder_stack", extruder_stack_mime.name)