SupportEraser.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 UM.Math.Vector import Vector
  7. from UM.Tool import Tool
  8. from UM.Application import Application
  9. from UM.Event import Event, MouseEvent
  10. from UM.Mesh.MeshBuilder import MeshBuilder
  11. from UM.Scene.Selection import Selection
  12. from UM.Scene.Iterator.BreadthFirstIterator import BreadthFirstIterator
  13. from cura.Scene.CuraSceneNode import CuraSceneNode
  14. from cura.PickingPass import PickingPass
  15. from UM.Operations.GroupedOperation import GroupedOperation
  16. from UM.Operations.AddSceneNodeOperation import AddSceneNodeOperation
  17. from UM.Operations.RemoveSceneNodeOperation import RemoveSceneNodeOperation
  18. from cura.Operations.SetParentOperation import SetParentOperation
  19. from cura.Scene.SliceableObjectDecorator import SliceableObjectDecorator
  20. from cura.Scene.BuildPlateDecorator import BuildPlateDecorator
  21. from UM.Scene.GroupDecorator import GroupDecorator
  22. from cura.Settings.SettingOverrideDecorator import SettingOverrideDecorator
  23. from UM.Settings.SettingInstance import SettingInstance
  24. class SupportEraser(Tool):
  25. def __init__(self):
  26. super().__init__()
  27. self._shortcut_key = Qt.Key_G
  28. self._controller = Application.getInstance().getController()
  29. self._selection_pass = None
  30. Application.getInstance().globalContainerStackChanged.connect(self._updateEnabled)
  31. # Note: if the selection is cleared with this tool active, there is no way to switch to
  32. # another tool than to reselect an object (by clicking it) because the tool buttons in the
  33. # toolbar will have been disabled. That is why we need to ignore the first press event
  34. # after the selection has been cleared.
  35. Selection.selectionChanged.connect(self._onSelectionChanged)
  36. self._had_selection = False
  37. self._skip_press = False
  38. self._had_selection_timer = QTimer()
  39. self._had_selection_timer.setInterval(0)
  40. self._had_selection_timer.setSingleShot(True)
  41. self._had_selection_timer.timeout.connect(self._selectionChangeDelay)
  42. def event(self, event):
  43. super().event(event)
  44. if event.type == Event.MousePressEvent and self._controller.getToolsEnabled():
  45. if self._skip_press:
  46. # The selection was previously cleared, do not add/remove an anti-support mesh but
  47. # use this click for selection and reactivating this tool only.
  48. self._skip_press = False
  49. return
  50. if self._selection_pass is None:
  51. # The selection renderpass is used to identify objects in the current view
  52. self._selection_pass = Application.getInstance().getRenderer().getRenderPass("selection")
  53. picked_node = self._controller.getScene().findObject(self._selection_pass.getIdAtPosition(event.x, event.y))
  54. node_stack = picked_node.callDecoration("getStack")
  55. if node_stack:
  56. if node_stack.getProperty("anti_overhang_mesh", "value"):
  57. self._removeEraserMesh(picked_node)
  58. return
  59. elif node_stack.getProperty("support_mesh", "value") or node_stack.getProperty("infill_mesh", "value") or node_stack.getProperty("cutting_mesh", "value"):
  60. # Only "normal" meshes can have anti_overhang_meshes added to them
  61. return
  62. # Create a pass for picking a world-space location from the mouse location
  63. active_camera = self._controller.getScene().getActiveCamera()
  64. picking_pass = PickingPass(active_camera.getViewportWidth(), active_camera.getViewportHeight())
  65. picking_pass.render()
  66. picked_position = picking_pass.getPickedPosition(event.x, event.y)
  67. # Add the anti_overhang_mesh cube at the picked location
  68. self._createEraserMesh(picked_node, picked_position)
  69. def _createEraserMesh(self, parent: CuraSceneNode, position: Vector):
  70. node = CuraSceneNode()
  71. node.setName("Eraser")
  72. node.setSelectable(True)
  73. mesh = MeshBuilder()
  74. mesh.addCube(10,10,10)
  75. node.setMeshData(mesh.build())
  76. node.setPosition(position)
  77. active_build_plate = Application.getInstance().getMultiBuildPlateModel().activeBuildPlate
  78. node.addDecorator(SettingOverrideDecorator())
  79. node.addDecorator(BuildPlateDecorator(active_build_plate))
  80. node.addDecorator(SliceableObjectDecorator())
  81. stack = node.callDecoration("getStack") # created by SettingOverrideDecorator
  82. settings = stack.getTop()
  83. definition = stack.getSettingDefinition("anti_overhang_mesh")
  84. new_instance = SettingInstance(definition, settings)
  85. new_instance.setProperty("value", True)
  86. new_instance.resetState() # Ensure that the state is not seen as a user state.
  87. settings.addInstance(new_instance)
  88. root = self._controller.getScene().getRoot()
  89. op = GroupedOperation()
  90. # First add the node to the scene, so it gets the expected transform
  91. op.addOperation(AddSceneNodeOperation(node, root))
  92. # Determine the parent group the node should be put in
  93. if parent.getParent().callDecoration("isGroup"):
  94. group = parent.getParent()
  95. else:
  96. # Create a group-node
  97. group = CuraSceneNode()
  98. group.addDecorator(GroupDecorator())
  99. group.addDecorator(BuildPlateDecorator(active_build_plate))
  100. group.setParent(root)
  101. center = parent.getPosition()
  102. group.setPosition(center)
  103. group.setCenterPosition(center)
  104. op.addOperation(SetParentOperation(parent, group))
  105. op.addOperation(SetParentOperation(node, group))
  106. op.push()
  107. Application.getInstance().getController().getScene().sceneChanged.emit(node)
  108. # Select the picked node so the group does not get drawn as a wireframe (yet)
  109. if not Selection.isSelected(parent):
  110. Selection.add(parent)
  111. if Selection.isSelected(group):
  112. Selection.remove(group)
  113. def _removeEraserMesh(self, node: CuraSceneNode):
  114. group = node.getParent()
  115. if group.callDecoration("isGroup"):
  116. parent = group.getChildren()[0]
  117. op = GroupedOperation()
  118. op.addOperation(RemoveSceneNodeOperation(node))
  119. if len(group.getChildren()) == 2:
  120. op.addOperation(SetParentOperation(parent, group.getParent()))
  121. op.push()
  122. Application.getInstance().getController().getScene().sceneChanged.emit(node)
  123. # Select the picked node so the group does not get drawn as a wireframe (yet)
  124. if parent and not Selection.isSelected(parent):
  125. Selection.add(parent)
  126. if Selection.isSelected(group):
  127. Selection.remove(group)
  128. def _updateEnabled(self):
  129. plugin_enabled = False
  130. global_container_stack = Application.getInstance().getGlobalContainerStack()
  131. if global_container_stack:
  132. plugin_enabled = global_container_stack.getProperty("anti_overhang_mesh", "enabled")
  133. Application.getInstance().getController().toolEnabledChanged.emit(self._plugin_id, plugin_enabled)
  134. def _onSelectionChanged(self):
  135. # When selection is passed from one object to another object, first the selection is cleared
  136. # and then it is set to the new object. We are only interested in the change from no selection
  137. # to a selection or vice-versa, not in a change from one object to another. A timer is used to
  138. # "merge" a possible clear/select action in a single frame
  139. if Selection.hasSelection() != self._had_selection:
  140. self._had_selection_timer.start()
  141. def _selectionChangeDelay(self):
  142. has_selection = Selection.hasSelection()
  143. if not has_selection and self._had_selection:
  144. self._skip_press = True
  145. else:
  146. self._skip_press = False
  147. self._had_selection = has_selection