SettingOverrideDecorator.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import copy
  4. import uuid
  5. from UM.Scene.SceneNodeDecorator import SceneNodeDecorator
  6. from UM.Signal import Signal, signalemitter
  7. from UM.Settings.InstanceContainer import InstanceContainer
  8. from UM.Settings.ContainerRegistry import ContainerRegistry
  9. from UM.Logger import Logger
  10. from UM.Application import Application
  11. from cura.Settings.PerObjectContainerStack import PerObjectContainerStack
  12. from cura.Settings.ExtruderManager import ExtruderManager
  13. @signalemitter
  14. class SettingOverrideDecorator(SceneNodeDecorator):
  15. """A decorator that adds a container stack to a Node. This stack should be queried for all settings regarding
  16. the linked node. The Stack in question will refer to the global stack (so that settings that are not defined by
  17. this stack still resolve.
  18. """
  19. activeExtruderChanged = Signal()
  20. """Event indicating that the user selected a different extruder."""
  21. _non_printing_mesh_settings = {"anti_overhang_mesh", "infill_mesh", "cutting_mesh"}
  22. """Non-printing meshes
  23. If these settings are True for any mesh, the mesh does not need a convex hull,
  24. and is sent to the slicer regardless of whether it fits inside the build volume.
  25. Note that Support Mesh is not in here because it actually generates
  26. g-code in the volume of the mesh.
  27. """
  28. _non_thumbnail_visible_settings = {"anti_overhang_mesh", "infill_mesh", "cutting_mesh", "support_mesh"}
  29. def __init__(self):
  30. super().__init__()
  31. self._stack = PerObjectContainerStack(container_id = "per_object_stack_" + str(id(self)))
  32. self._stack.setDirty(False) # This stack does not need to be saved.
  33. user_container = InstanceContainer(container_id = self._generateUniqueName())
  34. user_container.setMetaDataEntry("type", "user")
  35. self._stack.userChanges = user_container
  36. self._extruder_stack = ExtruderManager.getInstance().getExtruderStack(0).getId()
  37. self._is_non_printing_mesh = False
  38. self._is_non_thumbnail_visible_mesh = False
  39. self._stack.propertyChanged.connect(self._onSettingChanged)
  40. Application.getInstance().getContainerRegistry().addContainer(self._stack)
  41. Application.getInstance().globalContainerStackChanged.connect(self._updateNextStack)
  42. self.activeExtruderChanged.connect(self._updateNextStack)
  43. self._updateNextStack()
  44. def _generateUniqueName(self):
  45. return "SettingOverrideInstanceContainer-%s" % uuid.uuid1()
  46. def __deepcopy__(self, memo):
  47. deep_copy = SettingOverrideDecorator()
  48. """Create a fresh decorator object"""
  49. instance_container = copy.deepcopy(self._stack.getContainer(0), memo)
  50. """Copy the instance"""
  51. # A unique name must be added, or replaceContainer will not replace it
  52. instance_container.setMetaDataEntry("id", self._generateUniqueName())
  53. ## Set the copied instance as the first (and only) instance container of the stack.
  54. deep_copy._stack.replaceContainer(0, instance_container)
  55. # Properly set the right extruder on the copy
  56. deep_copy.setActiveExtruder(self._extruder_stack)
  57. # use value from the stack because there can be a delay in signal triggering and "_is_non_printing_mesh"
  58. # has not been updated yet.
  59. deep_copy._is_non_printing_mesh = self._evaluateIsNonPrintingMesh()
  60. deep_copy._is_non_thumbnail_visible_mesh = self._evaluateIsNonThumbnailVisibleMesh()
  61. return deep_copy
  62. def getActiveExtruder(self):
  63. """Gets the currently active extruder to print this object with.
  64. :return: An extruder's container stack.
  65. """
  66. return self._extruder_stack
  67. def getActiveExtruderChangedSignal(self):
  68. """Gets the signal that emits if the active extruder changed.
  69. This can then be accessed via a decorator.
  70. """
  71. return self.activeExtruderChanged
  72. def getActiveExtruderPosition(self):
  73. """Gets the currently active extruders position
  74. :return: An extruder's position, or None if no position info is available.
  75. """
  76. # for support_meshes, always use the support_extruder
  77. if self.getStack().getProperty("support_mesh", "value"):
  78. global_container_stack = Application.getInstance().getGlobalContainerStack()
  79. if global_container_stack:
  80. return str(global_container_stack.getProperty("support_extruder_nr", "value"))
  81. containers = ContainerRegistry.getInstance().findContainers(id = self.getActiveExtruder())
  82. if containers:
  83. container_stack = containers[0]
  84. return container_stack.getMetaDataEntry("position", default=None)
  85. def isNonPrintingMesh(self):
  86. return self._is_non_printing_mesh
  87. def _evaluateIsNonPrintingMesh(self):
  88. return any(bool(self._stack.getProperty(setting, "value")) for setting in self._non_printing_mesh_settings)
  89. def isNonThumbnailVisibleMesh(self):
  90. return self._is_non_thumbnail_visible_mesh
  91. def _evaluateIsNonThumbnailVisibleMesh(self):
  92. return any(bool(self._stack.getProperty(setting, "value")) for setting in self._non_thumbnail_visible_settings)
  93. def _onSettingChanged(self, setting_key, property_name): # Reminder: 'property' is a built-in function
  94. # We're only interested in a few settings and only if it's value changed.
  95. if property_name == "value":
  96. # Trigger slice/need slicing if the value has changed.
  97. self._is_non_printing_mesh = self._evaluateIsNonPrintingMesh()
  98. self._is_non_thumbnail_visible_mesh = self._evaluateIsNonThumbnailVisibleMesh()
  99. Application.getInstance().getBackend().needsSlicing()
  100. Application.getInstance().getBackend().tickle()
  101. def _updateNextStack(self):
  102. """Makes sure that the stack upon which the container stack is placed is
  103. kept up to date.
  104. """
  105. if self._extruder_stack:
  106. extruder_stack = ContainerRegistry.getInstance().findContainerStacks(id = self._extruder_stack)
  107. if extruder_stack:
  108. if self._stack.getNextStack():
  109. old_extruder_stack_id = self._stack.getNextStack().getId()
  110. else:
  111. old_extruder_stack_id = ""
  112. self._stack.setNextStack(extruder_stack[0])
  113. # Trigger slice/need slicing if the extruder changed.
  114. if self._stack.getNextStack().getId() != old_extruder_stack_id:
  115. Application.getInstance().getBackend().needsSlicing()
  116. Application.getInstance().getBackend().tickle()
  117. else:
  118. Logger.log("e", "Extruder stack %s below per-object settings does not exist.", self._extruder_stack)
  119. else:
  120. self._stack.setNextStack(Application.getInstance().getGlobalContainerStack())
  121. def setActiveExtruder(self, extruder_stack_id):
  122. """Changes the extruder with which to print this node.
  123. :param extruder_stack_id: The new extruder stack to print with.
  124. """
  125. self._extruder_stack = extruder_stack_id
  126. self._updateNextStack()
  127. ExtruderManager.getInstance().resetSelectedObjectExtruders()
  128. self.activeExtruderChanged.emit()
  129. def getStack(self):
  130. return self._stack