SupportEraser.py 2.5 KB

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