CuraActions.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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
  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("http://ultimaker.com/en/support/software")], {})
  32. cura.CuraApplication.CuraApplication.getInstance().functionEvent(event)
  33. @pyqtSlot()
  34. def openBugReportPage(self) -> None:
  35. event = CallFunctionEvent(self._openUrl, [QUrl("http://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. while current_node.getParent() and current_node.getParent().callDecoration("isGroup"):
  54. current_node = current_node.getParent()
  55. # This was formerly done with SetTransformOperation but because of
  56. # unpredictable matrix deconstruction it was possible that mirrors
  57. # could manifest as rotations. Centering is therefore done by
  58. # moving the node to negative whatever its position is:
  59. center_operation = TranslateOperation(current_node, -current_node._position)
  60. operation.addOperation(center_operation)
  61. operation.push()
  62. ## Multiply all objects in the selection
  63. #
  64. # \param count The number of times to multiply the selection.
  65. @pyqtSlot(int)
  66. def multiplySelection(self, count: int) -> None:
  67. min_offset = cura.CuraApplication.CuraApplication.getInstance().getBuildVolume().getEdgeDisallowedSize() + 2 # Allow for some rounding errors
  68. job = MultiplyObjectsJob(Selection.getAllSelectedObjects(), count, min_offset = max(min_offset, 8))
  69. job.start()
  70. ## Delete all selected objects.
  71. @pyqtSlot()
  72. def deleteSelection(self) -> None:
  73. if not cura.CuraApplication.CuraApplication.getInstance().getController().getToolsEnabled():
  74. return
  75. removed_group_nodes = [] #type: List[SceneNode]
  76. op = GroupedOperation()
  77. nodes = Selection.getAllSelectedObjects()
  78. for node in nodes:
  79. op.addOperation(RemoveSceneNodeOperation(node))
  80. group_node = node.getParent()
  81. if group_node and group_node.callDecoration("isGroup") and group_node not in removed_group_nodes:
  82. remaining_nodes_in_group = list(set(group_node.getChildren()) - set(nodes))
  83. if len(remaining_nodes_in_group) == 1:
  84. removed_group_nodes.append(group_node)
  85. op.addOperation(SetParentOperation(remaining_nodes_in_group[0], group_node.getParent()))
  86. op.addOperation(RemoveSceneNodeOperation(group_node))
  87. # Reset the print information
  88. cura.CuraApplication.CuraApplication.getInstance().getController().getScene().sceneChanged.emit(node)
  89. op.push()
  90. ## Set the extruder that should be used to print the selection.
  91. #
  92. # \param extruder_id The ID of the extruder stack to use for the selected objects.
  93. @pyqtSlot(str)
  94. def setExtruderForSelection(self, extruder_id: str) -> None:
  95. operation = GroupedOperation()
  96. nodes_to_change = []
  97. for node in Selection.getAllSelectedObjects():
  98. # If the node is a group, apply the active extruder to all children of the group.
  99. if node.callDecoration("isGroup"):
  100. for grouped_node in BreadthFirstIterator(node): #type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
  101. if grouped_node.callDecoration("getActiveExtruder") == extruder_id:
  102. continue
  103. if grouped_node.callDecoration("isGroup"):
  104. continue
  105. nodes_to_change.append(grouped_node)
  106. continue
  107. # Do not change any nodes that already have the right extruder set.
  108. if node.callDecoration("getActiveExtruder") == extruder_id:
  109. continue
  110. nodes_to_change.append(node)
  111. if not nodes_to_change:
  112. # If there are no changes to make, we still need to reset the selected extruders.
  113. # This is a workaround for checked menu items being deselected while still being
  114. # selected.
  115. ExtruderManager.getInstance().resetSelectedObjectExtruders()
  116. return
  117. for node in nodes_to_change:
  118. operation.addOperation(SetObjectExtruderOperation(node, extruder_id))
  119. operation.push()
  120. @pyqtSlot(int)
  121. def setBuildPlateForSelection(self, build_plate_nr: int) -> None:
  122. Logger.log("d", "Setting build plate number... %d" % build_plate_nr)
  123. operation = GroupedOperation()
  124. root = cura.CuraApplication.CuraApplication.getInstance().getController().getScene().getRoot()
  125. nodes_to_change = []
  126. for node in Selection.getAllSelectedObjects():
  127. parent_node = node # Find the parent node to change instead
  128. while parent_node.getParent() != root:
  129. parent_node = parent_node.getParent()
  130. for single_node in BreadthFirstIterator(parent_node): #type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
  131. nodes_to_change.append(single_node)
  132. if not nodes_to_change:
  133. Logger.log("d", "Nothing to change.")
  134. return
  135. for node in nodes_to_change:
  136. operation.addOperation(SetBuildPlateNumberOperation(node, build_plate_nr))
  137. operation.push()
  138. Selection.clear()
  139. def _openUrl(self, url: QUrl) -> None:
  140. QDesktopServices.openUrl(url)