CuraActions.py 6.9 KB

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