SupportEraser.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Math.Vector import Vector
  4. from UM.Tool import Tool
  5. from PyQt5.QtCore import Qt, QUrl
  6. from UM.Application import Application
  7. from UM.Event import Event
  8. from UM.Mesh.MeshBuilder import MeshBuilder
  9. from UM.Operations.AddSceneNodeOperation import AddSceneNodeOperation
  10. from UM.Settings.SettingInstance import SettingInstance
  11. from cura.Scene.CuraSceneNode import CuraSceneNode
  12. from cura.Scene.SliceableObjectDecorator import SliceableObjectDecorator
  13. from cura.Scene.BuildPlateDecorator import BuildPlateDecorator
  14. from cura.Settings.SettingOverrideDecorator import SettingOverrideDecorator
  15. import os
  16. import os.path
  17. class SupportEraser(Tool):
  18. def __init__(self):
  19. super().__init__()
  20. self._shortcut_key = Qt.Key_G
  21. self._controller = Application.getInstance().getController()
  22. def event(self, event):
  23. super().event(event)
  24. if event.type == Event.ToolActivateEvent:
  25. # Load the remover mesh:
  26. self._createEraserMesh()
  27. # After we load the mesh, deactivate the tool again:
  28. self.getController().setActiveTool(None)
  29. def _createEraserMesh(self):
  30. node = CuraSceneNode()
  31. node.setName("Eraser")
  32. node.setSelectable(True)
  33. mesh = MeshBuilder()
  34. mesh.addCube(10,10,10)
  35. node.setMeshData(mesh.build())
  36. # Place the cube in the platform. Do it manually so it works if the "automatic drop models" is OFF
  37. move_vector = Vector(0, 5, 0)
  38. node.setPosition(move_vector)
  39. active_build_plate = Application.getInstance().getMultiBuildPlateModel().activeBuildPlate
  40. node.addDecorator(SettingOverrideDecorator())
  41. node.addDecorator(BuildPlateDecorator(active_build_plate))
  42. node.addDecorator(SliceableObjectDecorator())
  43. stack = node.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  44. if not stack:
  45. node.addDecorator(SettingOverrideDecorator())
  46. stack = node.callDecoration("getStack")
  47. settings = stack.getTop()
  48. if not (settings.getInstance("anti_overhang_mesh") and settings.getProperty("anti_overhang_mesh", "value")):
  49. definition = stack.getSettingDefinition("anti_overhang_mesh")
  50. new_instance = SettingInstance(definition, settings)
  51. new_instance.setProperty("value", True)
  52. new_instance.resetState() # Ensure that the state is not seen as a user state.
  53. settings.addInstance(new_instance)
  54. scene = self._controller.getScene()
  55. op = AddSceneNodeOperation(node, scene.getRoot())
  56. op.push()
  57. Application.getInstance().getController().getScene().sceneChanged.emit(node)