CuraActions.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import QObject, QUrl
  4. from PyQt5.QtGui import QDesktopServices
  5. from typing import List, cast
  6. from UM.Event import CallFunctionEvent
  7. from UM.FlameProfiler import pyqtSlot
  8. from UM.Math.Vector import Vector
  9. from UM.Scene.Selection import Selection
  10. from UM.Scene.Iterator.BreadthFirstIterator import BreadthFirstIterator
  11. from UM.Operations.GroupedOperation import GroupedOperation
  12. from UM.Operations.RemoveSceneNodeOperation import RemoveSceneNodeOperation
  13. from UM.Operations.TranslateOperation import TranslateOperation
  14. import cura.CuraApplication
  15. from cura.Operations.SetParentOperation import SetParentOperation
  16. from cura.MultiplyObjectsJob import MultiplyObjectsJob
  17. from cura.Settings.SetObjectExtruderOperation import SetObjectExtruderOperation
  18. from cura.Settings.ExtruderManager import ExtruderManager
  19. from cura.Operations.SetBuildPlateNumberOperation import SetBuildPlateNumberOperation
  20. from UM.Logger import Logger
  21. from UM.Scene.SceneNode import SceneNode
  22. class CuraActions(QObject):
  23. def __init__(self, parent: QObject = None) -> None:
  24. super().__init__(parent)
  25. @pyqtSlot()
  26. def openDocumentation(self) -> None:
  27. # Starting a web browser from a signal handler connected to a menu will crash on windows.
  28. # So instead, defer the call to the next run of the event loop, since that does work.
  29. # Note that weirdly enough, only signal handlers that open a web browser fail like that.
  30. event = CallFunctionEvent(self._openUrl, [QUrl("https://ultimaker.com/en/resources/manuals/software?utm_source=cura&utm_medium=software&utm_campaign=dropdown-documentation")], {})
  31. cura.CuraApplication.CuraApplication.getInstance().functionEvent(event)
  32. @pyqtSlot()
  33. def openBugReportPage(self) -> None:
  34. event = CallFunctionEvent(self._openUrl, [QUrl("https://github.com/Ultimaker/Cura/issues/new/choose")], {})
  35. cura.CuraApplication.CuraApplication.getInstance().functionEvent(event)
  36. @pyqtSlot()
  37. def homeCamera(self) -> None:
  38. """Reset camera position and direction to default"""
  39. scene = cura.CuraApplication.CuraApplication.getInstance().getController().getScene()
  40. camera = scene.getActiveCamera()
  41. if camera:
  42. diagonal_size = cura.CuraApplication.CuraApplication.getInstance().getBuildVolume().getDiagonalSize()
  43. camera.setPosition(Vector(-80, 250, 700) * diagonal_size / 375)
  44. camera.setPerspective(True)
  45. camera.lookAt(Vector(0, 0, 0))
  46. @pyqtSlot()
  47. def centerSelection(self) -> None:
  48. """Center all objects in the selection"""
  49. operation = GroupedOperation()
  50. for node in Selection.getAllSelectedObjects():
  51. current_node = node
  52. parent_node = current_node.getParent()
  53. while parent_node and parent_node.callDecoration("isGroup"):
  54. current_node = parent_node
  55. parent_node = current_node.getParent()
  56. # Find out where the bottom of the object is
  57. bbox = current_node.getBoundingBox()
  58. if bbox:
  59. center_y = current_node.getWorldPosition().y - bbox.bottom
  60. else:
  61. center_y = 0
  62. # Move the object so that it's bottom is on to of the buildplate
  63. center_operation = TranslateOperation(current_node, Vector(0, center_y, 0), set_position = True)
  64. operation.addOperation(center_operation)
  65. operation.push()
  66. @pyqtSlot(int)
  67. def multiplySelection(self, count: int) -> None:
  68. """Multiply all objects in the selection
  69. :param count: The number of times to multiply the selection.
  70. """
  71. min_offset = cura.CuraApplication.CuraApplication.getInstance().getBuildVolume().getEdgeDisallowedSize() + 2 # Allow for some rounding errors
  72. job = MultiplyObjectsJob(Selection.getAllSelectedObjects(), count, min_offset = max(min_offset, 8))
  73. job.start()
  74. @pyqtSlot()
  75. def deleteSelection(self) -> None:
  76. """Delete all selected objects."""
  77. if not cura.CuraApplication.CuraApplication.getInstance().getController().getToolsEnabled():
  78. return
  79. removed_group_nodes = [] #type: List[SceneNode]
  80. op = GroupedOperation()
  81. nodes = Selection.getAllSelectedObjects()
  82. for node in nodes:
  83. op.addOperation(RemoveSceneNodeOperation(node))
  84. group_node = node.getParent()
  85. if group_node and group_node.callDecoration("isGroup") and group_node not in removed_group_nodes:
  86. remaining_nodes_in_group = list(set(group_node.getChildren()) - set(nodes))
  87. if len(remaining_nodes_in_group) == 1:
  88. removed_group_nodes.append(group_node)
  89. op.addOperation(SetParentOperation(remaining_nodes_in_group[0], group_node.getParent()))
  90. op.addOperation(RemoveSceneNodeOperation(group_node))
  91. # Reset the print information
  92. cura.CuraApplication.CuraApplication.getInstance().getController().getScene().sceneChanged.emit(node)
  93. op.push()
  94. @pyqtSlot(str)
  95. def setExtruderForSelection(self, extruder_id: str) -> None:
  96. """Set the extruder that should be used to print the selection.
  97. :param extruder_id: The ID of the extruder stack to use for the selected objects.
  98. """
  99. operation = GroupedOperation()
  100. nodes_to_change = []
  101. for node in Selection.getAllSelectedObjects():
  102. # If the node is a group, apply the active extruder to all children of the group.
  103. if node.callDecoration("isGroup"):
  104. for grouped_node in BreadthFirstIterator(node): #type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
  105. if grouped_node.callDecoration("getActiveExtruder") == extruder_id:
  106. continue
  107. if grouped_node.callDecoration("isGroup"):
  108. continue
  109. nodes_to_change.append(grouped_node)
  110. continue
  111. # Do not change any nodes that already have the right extruder set.
  112. if node.callDecoration("getActiveExtruder") == extruder_id:
  113. continue
  114. nodes_to_change.append(node)
  115. if not nodes_to_change:
  116. # If there are no changes to make, we still need to reset the selected extruders.
  117. # This is a workaround for checked menu items being deselected while still being
  118. # selected.
  119. ExtruderManager.getInstance().resetSelectedObjectExtruders()
  120. return
  121. for node in nodes_to_change:
  122. operation.addOperation(SetObjectExtruderOperation(node, extruder_id))
  123. operation.push()
  124. @pyqtSlot(int)
  125. def setBuildPlateForSelection(self, build_plate_nr: int) -> None:
  126. Logger.log("d", "Setting build plate number... %d" % build_plate_nr)
  127. operation = GroupedOperation()
  128. root = cura.CuraApplication.CuraApplication.getInstance().getController().getScene().getRoot()
  129. nodes_to_change = [] # type: List[SceneNode]
  130. for node in Selection.getAllSelectedObjects():
  131. parent_node = node # Find the parent node to change instead
  132. while parent_node.getParent() != root:
  133. parent_node = cast(SceneNode, parent_node.getParent())
  134. for single_node in BreadthFirstIterator(parent_node): # type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
  135. nodes_to_change.append(single_node)
  136. if not nodes_to_change:
  137. Logger.log("d", "Nothing to change.")
  138. return
  139. for node in nodes_to_change:
  140. operation.addOperation(SetBuildPlateNumberOperation(node, build_plate_nr))
  141. operation.push()
  142. Selection.clear()
  143. def _openUrl(self, url: QUrl) -> None:
  144. QDesktopServices.openUrl(url)