PerObjectSettingsTool.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Logger import Logger
  4. from UM.Tool import Tool
  5. from UM.Scene.Selection import Selection
  6. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  7. from UM.Application import Application
  8. from cura.Settings.SettingOverrideDecorator import SettingOverrideDecorator
  9. from cura.Settings.ExtruderManager import ExtruderManager
  10. from UM.Settings.SettingInstance import SettingInstance
  11. from UM.Event import Event
  12. class PerObjectSettingsTool(Tool):
  13. """This tool allows the user to add & change settings per node in the scene.
  14. The settings per object are kept in a ContainerStack, which is linked to a node by decorator.
  15. """
  16. def __init__(self):
  17. super().__init__()
  18. self._model = None
  19. self.setExposedProperties("SelectedObjectId", "ContainerID", "SelectedActiveExtruder", "MeshType")
  20. self._multi_extrusion = False
  21. self._single_model_selected = False
  22. self.visibility_handler = None
  23. Selection.selectionChanged.connect(self.propertyChanged)
  24. Application.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerChanged)
  25. self._onGlobalContainerChanged()
  26. Selection.selectionChanged.connect(self._updateEnabled)
  27. def event(self, event):
  28. super().event(event)
  29. if event.type == Event.MousePressEvent and self._controller.getToolsEnabled():
  30. self.operationStopped.emit(self)
  31. return False
  32. def getSelectedObjectId(self):
  33. selected_object = Selection.getSelectedObject(0)
  34. selected_object_id = id(selected_object)
  35. return selected_object_id
  36. def getContainerID(self):
  37. selected_object = Selection.getSelectedObject(0)
  38. try:
  39. return selected_object.callDecoration("getStack").getId()
  40. except AttributeError:
  41. return ""
  42. def getSelectedActiveExtruder(self):
  43. """Gets the active extruder of the currently selected object.
  44. :return: The active extruder of the currently selected object.
  45. """
  46. selected_object = Selection.getSelectedObject(0)
  47. return selected_object.callDecoration("getActiveExtruder")
  48. def setSelectedActiveExtruder(self, extruder_stack_id):
  49. """Changes the active extruder of the currently selected object.
  50. :param extruder_stack_id: The ID of the extruder to print the currently
  51. selected object with.
  52. """
  53. selected_object = Selection.getSelectedObject(0)
  54. stack = selected_object.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  55. if not stack:
  56. selected_object.addDecorator(SettingOverrideDecorator())
  57. selected_object.callDecoration("setActiveExtruder", extruder_stack_id)
  58. def setMeshType(self, mesh_type: str) -> bool:
  59. """Returns True when the mesh_type was changed, False when current mesh_type == mesh_type"""
  60. old_mesh_type = self.getMeshType()
  61. if old_mesh_type == mesh_type:
  62. return False
  63. selected_object = Selection.getSelectedObject(0)
  64. if selected_object is None:
  65. Logger.log("w", "Tried setting the mesh type of the selected object, but no object was selected")
  66. return False
  67. stack = selected_object.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  68. if not stack:
  69. selected_object.addDecorator(SettingOverrideDecorator())
  70. stack = selected_object.callDecoration("getStack")
  71. settings_visibility_changed = False
  72. settings = stack.getTop()
  73. for property_key in ["infill_mesh", "cutting_mesh", "support_mesh", "anti_overhang_mesh"]:
  74. if property_key != mesh_type:
  75. if settings.getInstance(property_key):
  76. settings.removeInstance(property_key)
  77. else:
  78. if not (settings.getInstance(property_key) and settings.getProperty(property_key, "value")):
  79. definition = stack.getSettingDefinition(property_key)
  80. new_instance = SettingInstance(definition, settings)
  81. new_instance.setProperty("value", True)
  82. new_instance.resetState() # Ensure that the state is not seen as a user state.
  83. settings.addInstance(new_instance)
  84. # Override some settings to ensure that the infill mesh by default adds no skin or walls. Or remove them if not an infill mesh.
  85. specialized_settings = {
  86. "top_bottom_thickness": 0,
  87. "top_thickness": "=top_bottom_thickness",
  88. "bottom_thickness": "=top_bottom_thickness",
  89. "top_layers": "=0 if infill_sparse_density == 100 else math.ceil(round(top_thickness / resolveOrValue('layer_height'), 4))",
  90. "bottom_layers": "=0 if infill_sparse_density == 100 else math.ceil(round(bottom_thickness / resolveOrValue('layer_height'), 4))",
  91. "wall_thickness": 0,
  92. "wall_line_count": "=max(1, round((wall_thickness - wall_line_width_0) / wall_line_width_x) + 1) if wall_thickness != 0 else 0"
  93. }
  94. for property_key in specialized_settings:
  95. if mesh_type == "infill_mesh":
  96. if settings.getInstance(property_key) is None:
  97. definition = stack.getSettingDefinition(property_key)
  98. new_instance = SettingInstance(definition, settings)
  99. new_instance.setProperty("value", specialized_settings[property_key])
  100. new_instance.resetState() # Ensure that the state is not seen as a user state.
  101. settings.addInstance(new_instance)
  102. settings_visibility_changed = True
  103. elif old_mesh_type == "infill_mesh" and settings.getInstance(property_key) and property_key in specialized_settings:
  104. settings.removeInstance(property_key)
  105. settings_visibility_changed = True
  106. if settings_visibility_changed:
  107. self.visibility_handler.forceVisibilityChanged()
  108. self.propertyChanged.emit()
  109. return True
  110. def getMeshType(self):
  111. selected_object = Selection.getSelectedObject(0)
  112. stack = selected_object.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  113. if not stack:
  114. return ""
  115. settings = stack.getTop()
  116. for property_key in ["infill_mesh", "cutting_mesh", "support_mesh", "anti_overhang_mesh"]:
  117. if settings.getInstance(property_key) and settings.getProperty(property_key, "value"):
  118. return property_key
  119. return ""
  120. def _onGlobalContainerChanged(self):
  121. global_container_stack = Application.getInstance().getGlobalContainerStack()
  122. if global_container_stack:
  123. # used for enabling or disabling per extruder settings per object
  124. self._multi_extrusion = global_container_stack.getProperty("machine_extruder_count", "value") > 1
  125. extruder_stack = ExtruderManager.getInstance().getExtruderStack(0)
  126. if extruder_stack:
  127. root_node = Application.getInstance().getController().getScene().getRoot()
  128. for node in DepthFirstIterator(root_node):
  129. new_stack_id = extruder_stack.getId()
  130. # Get position of old extruder stack for this node
  131. old_extruder_pos = node.callDecoration("getActiveExtruderPosition")
  132. if old_extruder_pos is not None:
  133. # Fetch current (new) extruder stack at position
  134. new_stack = ExtruderManager.getInstance().getExtruderStack(old_extruder_pos)
  135. if new_stack:
  136. new_stack_id = new_stack.getId()
  137. node.callDecoration("setActiveExtruder", new_stack_id)
  138. self._updateEnabled()
  139. def _updateEnabled(self):
  140. selected_objects = Selection.getAllSelectedObjects()
  141. if len(selected_objects)> 1:
  142. self._single_model_selected = False
  143. elif len(selected_objects) == 1 and selected_objects[0].callDecoration("isGroup"):
  144. self._single_model_selected = False # Group is selected, so tool needs to be disabled
  145. else:
  146. self._single_model_selected = True
  147. Application.getInstance().getController().toolEnabledChanged.emit(self._plugin_id, self._single_model_selected)