SupportEraser.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import Qt, QTimer
  4. from PyQt5.QtWidgets import QApplication
  5. from UM.Math.Vector import Vector
  6. from UM.Tool import Tool
  7. from UM.Event import Event, MouseEvent
  8. from UM.Mesh.MeshBuilder import MeshBuilder
  9. from UM.Scene.Selection import Selection
  10. from cura.CuraApplication import CuraApplication
  11. from cura.Scene.CuraSceneNode import CuraSceneNode
  12. from cura.PickingPass import PickingPass
  13. from UM.Operations.GroupedOperation import GroupedOperation
  14. from UM.Operations.AddSceneNodeOperation import AddSceneNodeOperation
  15. from UM.Operations.RemoveSceneNodeOperation import RemoveSceneNodeOperation
  16. from cura.Operations.SetParentOperation import SetParentOperation
  17. from cura.Scene.SliceableObjectDecorator import SliceableObjectDecorator
  18. from cura.Scene.BuildPlateDecorator import BuildPlateDecorator
  19. from UM.Settings.SettingInstance import SettingInstance
  20. class SupportEraser(Tool):
  21. def __init__(self):
  22. super().__init__()
  23. self._shortcut_key = Qt.Key_G
  24. self._controller = self.getController()
  25. self._selection_pass = None
  26. CuraApplication.getInstance().globalContainerStackChanged.connect(self._updateEnabled)
  27. # Note: if the selection is cleared with this tool active, there is no way to switch to
  28. # another tool than to reselect an object (by clicking it) because the tool buttons in the
  29. # toolbar will have been disabled. That is why we need to ignore the first press event
  30. # after the selection has been cleared.
  31. Selection.selectionChanged.connect(self._onSelectionChanged)
  32. self._had_selection = False
  33. self._skip_press = False
  34. self._had_selection_timer = QTimer()
  35. self._had_selection_timer.setInterval(0)
  36. self._had_selection_timer.setSingleShot(True)
  37. self._had_selection_timer.timeout.connect(self._selectionChangeDelay)
  38. def event(self, event):
  39. super().event(event)
  40. modifiers = QApplication.keyboardModifiers()
  41. ctrl_is_active = modifiers & Qt.ControlModifier
  42. if event.type == Event.MousePressEvent and MouseEvent.LeftButton in event.buttons and self._controller.getToolsEnabled():
  43. if ctrl_is_active:
  44. self._controller.setActiveTool("TranslateTool")
  45. return
  46. if self._skip_press:
  47. # The selection was previously cleared, do not add/remove an anti-support mesh but
  48. # use this click for selection and reactivating this tool only.
  49. self._skip_press = False
  50. return
  51. if self._selection_pass is None:
  52. # The selection renderpass is used to identify objects in the current view
  53. self._selection_pass = Application.getInstance().getRenderer().getRenderPass("selection")
  54. picked_node = self._controller.getScene().findObject(self._selection_pass.getIdAtPosition(event.x, event.y))
  55. if not picked_node:
  56. # There is no slicable object at the picked location
  57. return
  58. node_stack = picked_node.callDecoration("getStack")
  59. if node_stack:
  60. if node_stack.getProperty("anti_overhang_mesh", "value"):
  61. self._removeEraserMesh(picked_node)
  62. return
  63. elif node_stack.getProperty("support_mesh", "value") or node_stack.getProperty("infill_mesh", "value") or node_stack.getProperty("cutting_mesh", "value"):
  64. # Only "normal" meshes can have anti_overhang_meshes added to them
  65. return
  66. # Create a pass for picking a world-space location from the mouse location
  67. active_camera = self._controller.getScene().getActiveCamera()
  68. picking_pass = PickingPass(active_camera.getViewportWidth(), active_camera.getViewportHeight())
  69. picking_pass.render()
  70. picked_position = picking_pass.getPickedPosition(event.x, event.y)
  71. # Add the anti_overhang_mesh cube at the picked location
  72. self._createEraserMesh(picked_node, picked_position)
  73. def _createEraserMesh(self, parent: CuraSceneNode, position: Vector):
  74. node = CuraSceneNode()
  75. node.setName("Eraser")
  76. node.setSelectable(True)
  77. mesh = MeshBuilder()
  78. mesh.addCube(10,10,10)
  79. node.setMeshData(mesh.build())
  80. active_build_plate = CuraApplication.getInstance().getMultiBuildPlateModel().activeBuildPlate
  81. node.addDecorator(BuildPlateDecorator(active_build_plate))
  82. node.addDecorator(SliceableObjectDecorator())
  83. stack = node.callDecoration("getStack") # created by SettingOverrideDecorator that is automatically added to CuraSceneNode
  84. settings = stack.getTop()
  85. definition = stack.getSettingDefinition("anti_overhang_mesh")
  86. new_instance = SettingInstance(definition, settings)
  87. new_instance.setProperty("value", True)
  88. new_instance.resetState() # Ensure that the state is not seen as a user state.
  89. settings.addInstance(new_instance)
  90. op = GroupedOperation()
  91. # First add node to the scene at the correct position/scale, before parenting, so the eraser mesh does not get scaled with the parent
  92. op.addOperation(AddSceneNodeOperation(node, self._controller.getScene().getRoot()))
  93. op.addOperation(SetParentOperation(node, parent))
  94. op.push()
  95. node.setPosition(position, CuraSceneNode.TransformSpace.World)
  96. CuraApplication.getInstance().getController().getScene().sceneChanged.emit(node)
  97. def _removeEraserMesh(self, node: CuraSceneNode):
  98. parent = node.getParent()
  99. if parent == self._controller.getScene().getRoot():
  100. parent = None
  101. op = RemoveSceneNodeOperation(node)
  102. op.push()
  103. if parent and not Selection.isSelected(parent):
  104. Selection.add(parent)
  105. CuraApplication.getInstance().getController().getScene().sceneChanged.emit(node)
  106. def _updateEnabled(self):
  107. plugin_enabled = False
  108. global_container_stack = CuraApplication.getInstance().getGlobalContainerStack()
  109. if global_container_stack:
  110. plugin_enabled = global_container_stack.getProperty("anti_overhang_mesh", "enabled")
  111. CuraApplication.getInstance().getController().toolEnabledChanged.emit(self._plugin_id, plugin_enabled)
  112. def _onSelectionChanged(self):
  113. # When selection is passed from one object to another object, first the selection is cleared
  114. # and then it is set to the new object. We are only interested in the change from no selection
  115. # to a selection or vice-versa, not in a change from one object to another. A timer is used to
  116. # "merge" a possible clear/select action in a single frame
  117. if Selection.hasSelection() != self._had_selection:
  118. self._had_selection_timer.start()
  119. def _selectionChangeDelay(self):
  120. has_selection = Selection.hasSelection()
  121. if not has_selection and self._had_selection:
  122. self._skip_press = True
  123. else:
  124. self._skip_press = False
  125. self._had_selection = has_selection