ExtruderStack.py 7.0 KB

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