CuraActions.py 7.8 KB

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