SupportEraser.py 6.9 KB

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