CuraActions.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, TYPE_CHECKING, 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. if TYPE_CHECKING:
  22. from UM.Scene.SceneNode import SceneNode
  23. class CuraActions(QObject):
  24. def __init__(self, parent: QObject = None) -> None:
  25. super().__init__(parent)
  26. @pyqtSlot()
  27. def openDocumentation(self) -> None:
  28. # Starting a web browser from a signal handler connected to a menu will crash on windows.
  29. # So instead, defer the call to the next run of the event loop, since that does work.
  30. # Note that weirdly enough, only signal handlers that open a web browser fail like that.
  31. event = CallFunctionEvent(self._openUrl, [QUrl("https://ultimaker.com/en/resources/manuals/software")], {})
  32. cura.CuraApplication.CuraApplication.getInstance().functionEvent(event)
  33. @pyqtSlot()
  34. def openBugReportPage(self) -> None:
  35. event = CallFunctionEvent(self._openUrl, [QUrl("https://github.com/Ultimaker/Cura/issues")], {})
  36. cura.CuraApplication.CuraApplication.getInstance().functionEvent(event)
  37. ## Reset camera position and direction to default
  38. @pyqtSlot()
  39. def homeCamera(self) -> None:
  40. scene = cura.CuraApplication.CuraApplication.getInstance().getController().getScene()
  41. camera = scene.getActiveCamera()
  42. if camera:
  43. diagonal_size = cura.CuraApplication.CuraApplication.getInstance().getBuildVolume().getDiagonalSize()
  44. camera.setPosition(Vector(-80, 250, 700) * diagonal_size / 375)
  45. camera.setPerspective(True)
  46. camera.lookAt(Vector(0, 0, 0))
  47. ## Center all objects in the selection
  48. @pyqtSlot()
  49. def centerSelection(self) -> None:
  50. operation = GroupedOperation()
  51. for node in Selection.getAllSelectedObjects():
  52. current_node = node
  53. parent_node = current_node.getParent()
  54. while parent_node and parent_node.callDecoration("isGroup"):
  55. current_node = parent_node
  56. parent_node = current_node.getParent()
  57. # This was formerly done with SetTransformOperation but because of
  58. # unpredictable matrix deconstruction it was possible that mirrors
  59. # could manifest as rotations. Centering is therefore done by
  60. # moving the node to negative whatever its position is:
  61. center_operation = TranslateOperation(current_node, -current_node._position)
  62. operation.addOperation(center_operation)
  63. operation.push()
  64. ## Multiply all objects in the selection
  65. #
  66. # \param count The number of times to multiply the selection.
  67. @pyqtSlot(int)
  68. def multiplySelection(self, count: int) -> None:
  69. min_offset = cura.CuraApplication.CuraApplication.getInstance().getBuildVolume().getEdgeDisallowedSize() + 2 # Allow for some rounding errors
  70. job = MultiplyObjectsJob(Selection.getAllSelectedObjects(), count, min_offset = max(min_offset, 8))
  71. job.start()
  72. ## Delete all selected objects.
  73. @pyqtSlot()
  74. def deleteSelection(self) -> None:
  75. if not cura.CuraApplication.CuraApplication.getInstance().getController().getToolsEnabled():
  76. return
  77. removed_group_nodes = [] #type: List[SceneNode]
  78. op = GroupedOperation()
  79. nodes = Selection.getAllSelectedObjects()
  80. for node in nodes:
  81. op.addOperation(RemoveSceneNodeOperation(node))
  82. group_node = node.getParent()
  83. if group_node and group_node.callDecoration("isGroup") and group_node not in removed_group_nodes:
  84. remaining_nodes_in_group = list(set(group_node.getChildren()) - set(nodes))
  85. if len(remaining_nodes_in_group) == 1:
  86. removed_group_nodes.append(group_node)
  87. op.addOperation(SetParentOperation(remaining_nodes_in_group[0], group_node.getParent()))
  88. op.addOperation(RemoveSceneNodeOperation(group_node))
  89. # Reset the print information
  90. cura.CuraApplication.CuraApplication.getInstance().getController().getScene().sceneChanged.emit(node)
  91. op.push()
  92. ## Set the extruder that should be used to print the selection.
  93. #
  94. # \param extruder_id The ID of the extruder stack to use for the selected objects.
  95. @pyqtSlot(str)
  96. def setExtruderForSelection(self, extruder_id: str) -> None:
  97. operation = GroupedOperation()
  98. nodes_to_change = []
  99. for node in Selection.getAllSelectedObjects():
  100. # If the node is a group, apply the active extruder to all children of the group.
  101. if node.callDecoration("isGroup"):
  102. for grouped_node in BreadthFirstIterator(node): #type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
  103. if grouped_node.callDecoration("getActiveExtruder") == extruder_id:
  104. continue
  105. if grouped_node.callDecoration("isGroup"):
  106. continue
  107. nodes_to_change.append(grouped_node)
  108. continue
  109. # Do not change any nodes that already have the right extruder set.
  110. if node.callDecoration("getActiveExtruder") == extruder_id:
  111. continue
  112. nodes_to_change.append(node)
  113. if not nodes_to_change:
  114. # If there are no changes to make, we still need to reset the selected extruders.
  115. # This is a workaround for checked menu items being deselected while still being
  116. # selected.
  117. ExtruderManager.getInstance().resetSelectedObjectExtruders()
  118. return
  119. for node in nodes_to_change:
  120. operation.addOperation(SetObjectExtruderOperation(node, extruder_id))
  121. operation.push()
  122. @pyqtSlot(int)
  123. def setBuildPlateForSelection(self, build_plate_nr: int) -> None:
  124. Logger.log("d", "Setting build plate number... %d" % build_plate_nr)
  125. operation = GroupedOperation()
  126. root = cura.CuraApplication.CuraApplication.getInstance().getController().getScene().getRoot()
  127. nodes_to_change = [] # type: List[SceneNode]
  128. for node in Selection.getAllSelectedObjects():
  129. parent_node = node # Find the parent node to change instead
  130. while parent_node.getParent() != root:
  131. parent_node = cast(SceneNode, parent_node.getParent())
  132. for single_node in BreadthFirstIterator(parent_node): # type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
  133. nodes_to_change.append(single_node)
  134. if not nodes_to_change:
  135. Logger.log("d", "Nothing to change.")
  136. return
  137. for node in nodes_to_change:
  138. operation.addOperation(SetBuildPlateNumberOperation(node, build_plate_nr))
  139. operation.push()
  140. Selection.clear()
  141. def _openUrl(self, url: QUrl) -> None:
  142. QDesktopServices.openUrl(url)