SettingOverrideDecorator.py 6.2 KB

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