SupportEraser.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from UM.Tool import Tool
  2. from PyQt5.QtCore import Qt, QUrl
  3. from UM.Application import Application
  4. from UM.Event import Event
  5. from UM.Mesh.MeshBuilder import MeshBuilder
  6. from UM.Operations.AddSceneNodeOperation import AddSceneNodeOperation
  7. from UM.Settings.SettingInstance import SettingInstance
  8. from cura.Scene.CuraSceneNode import CuraSceneNode
  9. from cura.Scene.SliceableObjectDecorator import SliceableObjectDecorator
  10. from cura.Scene.BuildPlateDecorator import BuildPlateDecorator
  11. from cura.Settings.SettingOverrideDecorator import SettingOverrideDecorator
  12. import os
  13. import os.path
  14. class SupportEraser(Tool):
  15. def __init__(self):
  16. super().__init__()
  17. self._shortcut_key = Qt.Key_G
  18. self._controller = Application.getInstance().getController()
  19. def event(self, event):
  20. super().event(event)
  21. if event.type == Event.ToolActivateEvent:
  22. # Load the remover mesh:
  23. self._createEraserMesh()
  24. # After we load the mesh, deactivate the tool again:
  25. self.getController().setActiveTool(None)
  26. def _createEraserMesh(self):
  27. # Selection.clear()
  28. node = CuraSceneNode()
  29. node.setName("Eraser")
  30. node.setSelectable(True)
  31. mesh = MeshBuilder()
  32. mesh.addCube(10,10,10)
  33. node.setMeshData(mesh.build())
  34. active_build_plate = Application.getInstance().getBuildPlateModel().activeBuildPlate
  35. node.addDecorator(SettingOverrideDecorator())
  36. node.addDecorator(BuildPlateDecorator(active_build_plate))
  37. node.addDecorator(SliceableObjectDecorator())
  38. stack = node.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  39. if not stack:
  40. node.addDecorator(SettingOverrideDecorator())
  41. stack = node.callDecoration("getStack")
  42. print(stack)
  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)