SettingOverrideDecorator.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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, *, force_update = True):
  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._is_support_mesh = False
  40. self._is_cutting_mesh = False
  41. self._is_infill_mesh = False
  42. self._is_anti_overhang_mesh = False
  43. self._stack.propertyChanged.connect(self._onSettingChanged)
  44. Application.getInstance().getContainerRegistry().addContainer(self._stack)
  45. Application.getInstance().globalContainerStackChanged.connect(self._updateNextStack)
  46. self.activeExtruderChanged.connect(self._updateNextStack)
  47. if force_update:
  48. self._updateNextStack()
  49. def _generateUniqueName(self):
  50. return "SettingOverrideInstanceContainer-%s" % uuid.uuid1()
  51. def __deepcopy__(self, memo):
  52. deep_copy = SettingOverrideDecorator(force_update = False)
  53. """Create a fresh decorator object"""
  54. instance_container = copy.deepcopy(self._stack.getContainer(0), memo)
  55. """Copy the instance"""
  56. # A unique name must be added, or replaceContainer will not replace it
  57. instance_container.setMetaDataEntry("id", self._generateUniqueName())
  58. ## Set the copied instance as the first (and only) instance container of the stack.
  59. deep_copy._stack.replaceContainer(0, instance_container)
  60. # Properly set the right extruder on the copy
  61. deep_copy.setActiveExtruder(self._extruder_stack)
  62. return deep_copy
  63. def getActiveExtruder(self):
  64. """Gets the currently active extruder to print this object with.
  65. :return: An extruder's container stack.
  66. """
  67. return self._extruder_stack
  68. def getActiveExtruderChangedSignal(self):
  69. """Gets the signal that emits if the active extruder changed.
  70. This can then be accessed via a decorator.
  71. """
  72. return self.activeExtruderChanged
  73. def getActiveExtruderPosition(self):
  74. """Gets the currently active extruders position
  75. :return: An extruder's position, or None if no position info is available.
  76. """
  77. # for support_meshes, always use the support_extruder
  78. if self._is_support_mesh:
  79. global_container_stack = Application.getInstance().getGlobalContainerStack()
  80. if global_container_stack:
  81. return str(global_container_stack.getProperty("support_extruder_nr", "value"))
  82. containers = ContainerRegistry.getInstance().findContainers(id = self.getActiveExtruder())
  83. if containers:
  84. container_stack = containers[0]
  85. return container_stack.getMetaDataEntry("position", default=None)
  86. def isCuttingMesh(self):
  87. return self._is_cutting_mesh
  88. def isSupportMesh(self):
  89. return self._is_support_mesh
  90. def isInfillMesh(self):
  91. return self._is_infill_mesh
  92. def isAntiOverhangMesh(self):
  93. return self._is_anti_overhang_mesh
  94. def _evaluateAntiOverhangMesh(self):
  95. return bool(self._stack.userChanges.getProperty("anti_overhang_mesh", "value"))
  96. def _evaluateIsCuttingMesh(self):
  97. return bool(self._stack.userChanges.getProperty("cutting_mesh", "value"))
  98. def _evaluateIsSupportMesh(self):
  99. return bool(self._stack.userChanges.getProperty("support_mesh", "value"))
  100. def _evaluateInfillMesh(self):
  101. return bool(self._stack.userChanges.getProperty("infill_mesh", "value"))
  102. def isNonPrintingMesh(self):
  103. return self._is_non_printing_mesh
  104. def _evaluateIsNonPrintingMesh(self):
  105. return any(bool(self._stack.getProperty(setting, "value")) for setting in self._non_printing_mesh_settings)
  106. def isNonThumbnailVisibleMesh(self):
  107. return self._is_non_thumbnail_visible_mesh
  108. def _evaluateIsNonThumbnailVisibleMesh(self):
  109. return any(bool(self._stack.getProperty(setting, "value")) for setting in self._non_thumbnail_visible_settings)
  110. def _onSettingChanged(self, setting_key, property_name): # Reminder: 'property' is a built-in function
  111. # We're only interested in a few settings and only if it's value changed.
  112. if property_name == "value":
  113. # Trigger slice/need slicing if the value has changed.
  114. self._is_non_printing_mesh = self._evaluateIsNonPrintingMesh()
  115. self._is_non_thumbnail_visible_mesh = self._evaluateIsNonThumbnailVisibleMesh()
  116. if setting_key == "anti_overhang_mesh":
  117. self._is_anti_overhang_mesh = self._evaluateAntiOverhangMesh()
  118. elif setting_key == "support_mesh":
  119. self._is_support_mesh = self._evaluateIsSupportMesh()
  120. elif setting_key == "cutting_mesh":
  121. self._is_cutting_mesh = self._evaluateIsCuttingMesh()
  122. elif setting_key == "infill_mesh":
  123. self._is_infill_mesh = self._evaluateInfillMesh()
  124. Application.getInstance().getBackend().needsSlicing()
  125. Application.getInstance().getBackend().tickle()
  126. def _updateNextStack(self):
  127. """Makes sure that the stack upon which the container stack is placed is
  128. kept up to date.
  129. """
  130. if self._extruder_stack:
  131. extruder_stack = ContainerRegistry.getInstance().findContainerStacks(id = self._extruder_stack)
  132. if extruder_stack:
  133. if self._stack.getNextStack():
  134. old_extruder_stack_id = self._stack.getNextStack().getId()
  135. else:
  136. old_extruder_stack_id = ""
  137. self._stack.setNextStack(extruder_stack[0])
  138. # Trigger slice/need slicing if the extruder changed.
  139. if self._stack.getNextStack().getId() != old_extruder_stack_id:
  140. Application.getInstance().getBackend().needsSlicing()
  141. Application.getInstance().getBackend().tickle()
  142. else:
  143. Logger.log("e", "Extruder stack %s below per-object settings does not exist.", self._extruder_stack)
  144. else:
  145. self._stack.setNextStack(Application.getInstance().getGlobalContainerStack())
  146. def setActiveExtruder(self, extruder_stack_id):
  147. """Changes the extruder with which to print this node.
  148. :param extruder_stack_id: The new extruder stack to print with.
  149. """
  150. self._extruder_stack = extruder_stack_id
  151. self._updateNextStack()
  152. ExtruderManager.getInstance().resetSelectedObjectExtruders()
  153. self.activeExtruderChanged.emit()
  154. def getStack(self):
  155. return self._stack