SettingOverrideDecorator.py 5.8 KB

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