SettingOverrideDecorator.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. _non_thumbnail_visible_settings = {"anti_overhang_mesh", "infill_mesh", "cutting_mesh", "support_mesh"}
  28. def __init__(self):
  29. super().__init__()
  30. self._stack = PerObjectContainerStack(container_id = "per_object_stack_" + str(id(self)))
  31. self._stack.setDirty(False) # This stack does not need to be saved.
  32. user_container = InstanceContainer(container_id = self._generateUniqueName())
  33. user_container.setMetaDataEntry("type", "user")
  34. self._stack.userChanges = user_container
  35. self._extruder_stack = ExtruderManager.getInstance().getExtruderStack(0).getId()
  36. self._is_non_printing_mesh = False
  37. self._is_non_thumbnail_visible_mesh = False
  38. self._stack.propertyChanged.connect(self._onSettingChanged)
  39. Application.getInstance().getContainerRegistry().addContainer(self._stack)
  40. Application.getInstance().globalContainerStackChanged.connect(self._updateNextStack)
  41. self.activeExtruderChanged.connect(self._updateNextStack)
  42. self._updateNextStack()
  43. def _generateUniqueName(self):
  44. return "SettingOverrideInstanceContainer-%s" % uuid.uuid1()
  45. def __deepcopy__(self, memo):
  46. ## Create a fresh decorator object
  47. deep_copy = SettingOverrideDecorator()
  48. ## Copy the instance
  49. instance_container = copy.deepcopy(self._stack.getContainer(0), memo)
  50. # A unique name must be added, or replaceContainer will not replace it
  51. instance_container.setMetaDataEntry("id", self._generateUniqueName())
  52. ## Set the copied instance as the first (and only) instance container of the stack.
  53. deep_copy._stack.replaceContainer(0, instance_container)
  54. # Properly set the right extruder on the copy
  55. deep_copy.setActiveExtruder(self._extruder_stack)
  56. # use value from the stack because there can be a delay in signal triggering and "_is_non_printing_mesh"
  57. # has not been updated yet.
  58. deep_copy._is_non_printing_mesh = self._evaluateIsNonPrintingMesh()
  59. deep_copy._is_non_thumbnail_visible_mesh = self._evaluateIsNonThumbnailVisibleMesh()
  60. return deep_copy
  61. ## Gets the currently active extruder to print this object with.
  62. #
  63. # \return An extruder's container stack.
  64. def getActiveExtruder(self):
  65. return self._extruder_stack
  66. ## Gets the signal that emits if the active extruder changed.
  67. #
  68. # This can then be accessed via a decorator.
  69. def getActiveExtruderChangedSignal(self):
  70. return self.activeExtruderChanged
  71. ## Gets the currently active extruders position
  72. #
  73. # \return An extruder's position, or None if no position info is available.
  74. def getActiveExtruderPosition(self):
  75. containers = ContainerRegistry.getInstance().findContainers(id = self.getActiveExtruder())
  76. if containers:
  77. container_stack = containers[0]
  78. return container_stack.getMetaDataEntry("position", default=None)
  79. def isNonPrintingMesh(self):
  80. return self._is_non_printing_mesh
  81. def _evaluateIsNonPrintingMesh(self):
  82. return any(bool(self._stack.getProperty(setting, "value")) for setting in self._non_printing_mesh_settings)
  83. def isNonThumbnailVisibleMesh(self):
  84. return self._is_non_thumbnail_visible_mesh
  85. def _evaluateIsNonThumbnailVisibleMesh(self):
  86. return any(bool(self._stack.getProperty(setting, "value")) for setting in self._non_thumbnail_visible_settings)
  87. def _onSettingChanged(self, setting_key, property_name): # Reminder: 'property' is a built-in function
  88. # We're only interested in a few settings and only if it's value changed.
  89. if property_name == "value":
  90. # Trigger slice/need slicing if the value has changed.
  91. self._is_non_printing_mesh = self._evaluateIsNonPrintingMesh()
  92. self._is_non_thumbnail_visible_mesh = self._evaluateIsNonThumbnailVisibleMesh()
  93. Application.getInstance().getBackend().needsSlicing()
  94. Application.getInstance().getBackend().tickle()
  95. ## Makes sure that the stack upon which the container stack is placed is
  96. # kept up to date.
  97. def _updateNextStack(self):
  98. if self._extruder_stack:
  99. extruder_stack = ContainerRegistry.getInstance().findContainerStacks(id = self._extruder_stack)
  100. if extruder_stack:
  101. if self._stack.getNextStack():
  102. old_extruder_stack_id = self._stack.getNextStack().getId()
  103. else:
  104. old_extruder_stack_id = ""
  105. self._stack.setNextStack(extruder_stack[0])
  106. # Trigger slice/need slicing if the extruder changed.
  107. if self._stack.getNextStack().getId() != old_extruder_stack_id:
  108. Application.getInstance().getBackend().needsSlicing()
  109. Application.getInstance().getBackend().tickle()
  110. else:
  111. Logger.log("e", "Extruder stack %s below per-object settings does not exist.", self._extruder_stack)
  112. else:
  113. self._stack.setNextStack(Application.getInstance().getGlobalContainerStack())
  114. ## Changes the extruder with which to print this node.
  115. #
  116. # \param extruder_stack_id The new extruder stack to print with.
  117. def setActiveExtruder(self, extruder_stack_id):
  118. self._extruder_stack = extruder_stack_id
  119. self._updateNextStack()
  120. ExtruderManager.getInstance().resetSelectedObjectExtruders()
  121. self.activeExtruderChanged.emit()
  122. def getStack(self):
  123. return self._stack