GlobalStack.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any, Dict, Optional
  4. from PyQt5.QtCore import pyqtProperty
  5. from UM.Decorators import override
  6. from UM.MimeTypeDatabase import MimeType, MimeTypeDatabase
  7. from UM.Settings.ContainerStack import ContainerStack
  8. from UM.Settings.SettingInstance import InstanceState
  9. from UM.Settings.ContainerRegistry import ContainerRegistry
  10. from UM.Settings.Interfaces import PropertyEvaluationContext
  11. from UM.Logger import Logger
  12. from . import Exceptions
  13. from .CuraContainerStack import CuraContainerStack
  14. ## Represents the Global or Machine stack and its related containers.
  15. #
  16. class GlobalStack(CuraContainerStack):
  17. def __init__(self, container_id: str, *args, **kwargs):
  18. super().__init__(container_id, *args, **kwargs)
  19. self.addMetaDataEntry("type", "machine") # For backward compatibility
  20. self._extruders = {}
  21. # This property is used to track which settings we are calculating the "resolve" for
  22. # and if so, to bypass the resolve to prevent an infinite recursion that would occur
  23. # if the resolve function tried to access the same property it is a resolve for.
  24. self._resolving_settings = set()
  25. ## Get the list of extruders of this stack.
  26. #
  27. # \return The extruders registered with this stack.
  28. @pyqtProperty("QVariantMap")
  29. def extruders(self) -> Dict[str, "ExtruderStack"]:
  30. return self._extruders
  31. @classmethod
  32. def getLoadingPriority(cls) -> int:
  33. return 2
  34. def getConfigurationTypeFromSerialized(self, serialized: str) -> Optional[str]:
  35. configuration_type = None
  36. try:
  37. parser = self._readAndValidateSerialized(serialized)
  38. configuration_type = parser["metadata"].get("type")
  39. if configuration_type == "machine":
  40. configuration_type = "machine_stack"
  41. except Exception as e:
  42. Logger.log("e", "Could not get configuration type: %s", e)
  43. return configuration_type
  44. ## Add an extruder to the list of extruders of this stack.
  45. #
  46. # \param extruder The extruder to add.
  47. #
  48. # \throws Exceptions.TooManyExtrudersError Raised when trying to add an extruder while we
  49. # already have the maximum number of extruders.
  50. def addExtruder(self, extruder: ContainerStack) -> None:
  51. extruder_count = self.getProperty("machine_extruder_count", "value")
  52. if extruder_count <= 1:
  53. Logger.log("i", "Not adding extruder[%s] to [%s] because it is a single-extrusion machine.",
  54. extruder.id, self.id)
  55. return
  56. position = extruder.getMetaDataEntry("position")
  57. if position is None:
  58. Logger.log("w", "No position defined for extruder {extruder}, cannot add it to stack {stack}", extruder = extruder.id, stack = self.id)
  59. return
  60. if any(item.getId() == extruder.id for item in self._extruders.values()):
  61. Logger.log("w", "Extruder [%s] has already been added to this stack [%s]", extruder.id, self._id)
  62. return
  63. self._extruders[position] = extruder
  64. Logger.log("i", "Extruder[%s] added to [%s] at position [%s]", extruder.id, self.id, position)
  65. ## Overridden from ContainerStack
  66. #
  67. # This will return the value of the specified property for the specified setting,
  68. # unless the property is "value" and that setting has a "resolve" function set.
  69. # When a resolve is set, it will instead try and execute the resolve first and
  70. # then fall back to the normal "value" property.
  71. #
  72. # \param key The setting key to get the property of.
  73. # \param property_name The property to get the value of.
  74. #
  75. # \return The value of the property for the specified setting, or None if not found.
  76. @override(ContainerStack)
  77. def getProperty(self, key: str, property_name: str, context: Optional[PropertyEvaluationContext] = None) -> Any:
  78. if not self.definition.findDefinitions(key = key):
  79. return None
  80. if context is None:
  81. context = PropertyEvaluationContext()
  82. context.pushContainer(self)
  83. # Handle the "resolve" property.
  84. if self._shouldResolve(key, property_name, context):
  85. self._resolving_settings.add(key)
  86. resolve = super().getProperty(key, "resolve", context)
  87. self._resolving_settings.remove(key)
  88. if resolve is not None:
  89. return resolve
  90. # Handle the "limit_to_extruder" property.
  91. limit_to_extruder = super().getProperty(key, "limit_to_extruder", context)
  92. if limit_to_extruder is not None:
  93. limit_to_extruder = str(limit_to_extruder)
  94. if limit_to_extruder is not None and limit_to_extruder != "-1" and limit_to_extruder in self._extruders:
  95. if super().getProperty(key, "settable_per_extruder", context):
  96. result = self._extruders[str(limit_to_extruder)].getProperty(key, property_name, context)
  97. if result is not None:
  98. context.popContainer()
  99. return result
  100. else:
  101. Logger.log("e", "Setting {setting} has limit_to_extruder but is not settable per extruder!", setting = key)
  102. result = super().getProperty(key, property_name, context)
  103. context.popContainer()
  104. return result
  105. ## Overridden from ContainerStack
  106. #
  107. # This will simply raise an exception since the Global stack cannot have a next stack.
  108. @override(ContainerStack)
  109. def setNextStack(self, next_stack: ContainerStack) -> None:
  110. raise Exceptions.InvalidOperationError("Global stack cannot have a next stack!")
  111. ## Gets the approximate filament diameter that the machine requires.
  112. #
  113. # The approximate material diameter is the material diameter rounded to
  114. # the nearest millimetre.
  115. #
  116. # If the machine has no requirement for the diameter, -1 is returned.
  117. #
  118. # \return The approximate filament diameter for the printer, as a string.
  119. @pyqtProperty(str)
  120. def approximateMaterialDiameter(self) -> str:
  121. material_diameter = self.definition.getProperty("material_diameter", "value")
  122. if material_diameter is None:
  123. return "-1"
  124. return str(round(float(material_diameter))) #Round, then convert back to string.
  125. # protected:
  126. # Determine whether or not we should try to get the "resolve" property instead of the
  127. # requested property.
  128. def _shouldResolve(self, key: str, property_name: str, context: Optional[PropertyEvaluationContext] = None) -> bool:
  129. if property_name is not "value":
  130. # Do not try to resolve anything but the "value" property
  131. return False
  132. if key in self._resolving_settings:
  133. # To prevent infinite recursion, if getProperty is called with the same key as
  134. # we are already trying to resolve, we should not try to resolve again. Since
  135. # this can happen multiple times when trying to resolve a value, we need to
  136. # track all settings that are being resolved.
  137. return False
  138. setting_state = super().getProperty(key, "state", context = context)
  139. if setting_state is not None and setting_state != InstanceState.Default:
  140. # When the user has explicitly set a value, we should ignore any resolve and
  141. # just return that value.
  142. return False
  143. return True
  144. ## private:
  145. global_stack_mime = MimeType(
  146. name = "application/x-cura-globalstack",
  147. comment = "Cura Global Stack",
  148. suffixes = ["global.cfg"]
  149. )
  150. MimeTypeDatabase.addMimeType(global_stack_mime)
  151. ContainerRegistry.addContainerTypeByName(GlobalStack, "global_stack", global_stack_mime.name)