ExtruderStack.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 UM.Decorators import override
  5. from UM.MimeTypeDatabase import MimeType, MimeTypeDatabase
  6. from UM.Settings.ContainerStack import ContainerStack
  7. from UM.Settings.ContainerRegistry import ContainerRegistry
  8. from UM.Settings.Interfaces import ContainerInterface, PropertyEvaluationContext
  9. from UM.Settings.SettingInstance import SettingInstance
  10. from . import Exceptions
  11. from .CuraContainerStack import CuraContainerStack
  12. from .ExtruderManager import ExtruderManager
  13. if TYPE_CHECKING:
  14. from cura.Settings.GlobalStack import GlobalStack
  15. ## Represents an Extruder and its related containers.
  16. #
  17. #
  18. class ExtruderStack(CuraContainerStack):
  19. def __init__(self, container_id: str, *args, **kwargs):
  20. super().__init__(container_id, *args, **kwargs)
  21. self.addMetaDataEntry("type", "extruder_train") # For backward compatibility
  22. self.propertiesChanged.connect(self._onPropertiesChanged)
  23. ## Overridden from ContainerStack
  24. #
  25. # This will set the next stack and ensure that we register this stack as an extruder.
  26. @override(ContainerStack)
  27. def setNextStack(self, stack: ContainerStack) -> None:
  28. super().setNextStack(stack)
  29. stack.addExtruder(self)
  30. self.addMetaDataEntry("machine", stack.id)
  31. # For backward compatibility: Register the extruder with the Extruder Manager
  32. ExtruderManager.getInstance().registerExtruder(self, stack.id)
  33. # Now each machine will have at least one extruder stack. If this is the first extruder, the extruder-specific
  34. # settings such as nozzle size and material diameter should be moved from the machine's definition_changes to
  35. # the this extruder's definition_changes.
  36. #
  37. # We do this here because it is tooooo expansive to do it in the version upgrade: During the version upgrade,
  38. # when we are upgrading a definition_changes container file, there is NO guarantee that other files such as
  39. # machine an extruder stack files are upgraded before this, so we cannot read those files assuming they are in
  40. # the latest format.
  41. #
  42. # MORE:
  43. # For single-extrusion machines, nozzle size is saved in the global stack, so the nozzle size value should be
  44. # carried to the first extruder.
  45. # For material diameter, it was supposed to be applied to all extruders, so its value should be copied to all
  46. # extruders.
  47. #
  48. keys_to_copy = ["material_diameter"] # material diameter will be copied to all extruders
  49. if self.getMetaDataEntry("position") == "0":
  50. keys_to_copy.append("machine_nozzle_size")
  51. for key in keys_to_copy:
  52. # Only copy the value when this extruder doesn't have the value.
  53. if self.definitionChanges.hasProperty(key, "value"):
  54. continue
  55. setting_value = stack.definitionChanges.getProperty(key, "value")
  56. if setting_value is None:
  57. continue
  58. setting_definition = stack.getSettingDefinition(key)
  59. new_instance = SettingInstance(setting_definition, self.definitionChanges)
  60. new_instance.setProperty("value", setting_value)
  61. new_instance.resetState() # Ensure that the state is not seen as a user state.
  62. self.definitionChanges.addInstance(new_instance)
  63. self.definitionChanges.setDirty(True)
  64. # NOTE: We cannot remove the setting from the global stack's definition changes container because for
  65. # material diameter, it needs to be applied to all extruders, but here we don't know how many extruders
  66. # a machine actually has and how many extruders has already been loaded for that machine, so we have to
  67. # keep this setting for any remaining extruders that haven't been loaded yet.
  68. #
  69. # Those settings will be removed in ExtruderManager which knows all those info.
  70. @override(ContainerStack)
  71. def getNextStack(self) -> Optional["GlobalStack"]:
  72. return super().getNextStack()
  73. @classmethod
  74. def getLoadingPriority(cls) -> int:
  75. return 3
  76. ## Overridden from ContainerStack
  77. #
  78. # It will perform a few extra checks when trying to get properties.
  79. #
  80. # The two extra checks it currently does is to ensure a next stack is set and to bypass
  81. # the extruder when the property is not settable per extruder.
  82. #
  83. # \throws Exceptions.NoGlobalStackError Raised when trying to get a property from an extruder without
  84. # having a next stack set.
  85. @override(ContainerStack)
  86. def getProperty(self, key: str, property_name: str, context: Optional[PropertyEvaluationContext] = None) -> Any:
  87. if not self._next_stack:
  88. raise Exceptions.NoGlobalStackError("Extruder {id} is missing the next stack!".format(id = self.id))
  89. if context is None:
  90. context = PropertyEvaluationContext()
  91. context.pushContainer(self)
  92. if not super().getProperty(key, "settable_per_extruder", context):
  93. result = self.getNextStack().getProperty(key, property_name, context)
  94. context.popContainer()
  95. return result
  96. limit_to_extruder = super().getProperty(key, "limit_to_extruder", context)
  97. if limit_to_extruder is not None:
  98. limit_to_extruder = str(limit_to_extruder)
  99. if (limit_to_extruder is not None and limit_to_extruder != "-1") and self.getMetaDataEntry("position") != str(limit_to_extruder):
  100. if str(limit_to_extruder) in self.getNextStack().extruders:
  101. result = self.getNextStack().extruders[str(limit_to_extruder)].getProperty(key, property_name, context)
  102. if result is not None:
  103. context.popContainer()
  104. return result
  105. result = super().getProperty(key, property_name, context)
  106. context.popContainer()
  107. return result
  108. @override(CuraContainerStack)
  109. def _getMachineDefinition(self) -> ContainerInterface:
  110. if not self.getNextStack():
  111. raise Exceptions.NoGlobalStackError("Extruder {id} is missing the next stack!".format(id = self.id))
  112. return self.getNextStack()._getMachineDefinition()
  113. @override(CuraContainerStack)
  114. def deserialize(self, contents: str, file_name: Optional[str] = None) -> None:
  115. super().deserialize(contents, file_name)
  116. stacks = ContainerRegistry.getInstance().findContainerStacks(id=self.getMetaDataEntry("machine", ""))
  117. if stacks:
  118. self.setNextStack(stacks[0])
  119. def _onPropertiesChanged(self, key, properties):
  120. # When there is a setting that is not settable per extruder that depends on a value from a setting that is,
  121. # we do not always get properly informed that we should re-evaluate the setting. So make sure to indicate
  122. # something changed for those settings.
  123. if not self.getNextStack():
  124. return #There are no global settings to depend on.
  125. definitions = self.getNextStack().definition.findDefinitions(key = key)
  126. if definitions:
  127. has_global_dependencies = False
  128. for relation in definitions[0].relations:
  129. if not getattr(relation.target, "settable_per_extruder", True):
  130. has_global_dependencies = True
  131. break
  132. if has_global_dependencies:
  133. self.getNextStack().propertiesChanged.emit(key, properties)
  134. def findDefaultVariant(self):
  135. # The default variant is defined in the machine stack and/or definition, so use the machine stack to find
  136. # the default variant.
  137. return self.getNextStack().findDefaultVariant()
  138. extruder_stack_mime = MimeType(
  139. name = "application/x-cura-extruderstack",
  140. comment = "Cura Extruder Stack",
  141. suffixes = ["extruder.cfg"]
  142. )
  143. MimeTypeDatabase.addMimeType(extruder_stack_mime)
  144. ContainerRegistry.addContainerTypeByName(ExtruderStack, "extruder_stack", extruder_stack_mime.name)