CuraActions.py 7.0 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.TranslateOperation import TranslateOperation
  14. from cura.Operations.SetParentOperation import SetParentOperation
  15. from cura.MultiplyObjectsJob import MultiplyObjectsJob
  16. from cura.Settings.SetObjectExtruderOperation import SetObjectExtruderOperation
  17. from cura.Settings.ExtruderManager import ExtruderManager
  18. from cura.Operations.SetBuildPlateNumberOperation import SetBuildPlateNumberOperation
  19. from UM.Logger import Logger
  20. class CuraActions(QObject):
  21. def __init__(self, parent = None):
  22. super().__init__(parent)
  23. @pyqtSlot()
  24. def openDocumentation(self):
  25. # Starting a web browser from a signal handler connected to a menu will crash on windows.
  26. # So instead, defer the call to the next run of the event loop, since that does work.
  27. # Note that weirdly enough, only signal handlers that open a web browser fail like that.
  28. event = CallFunctionEvent(self._openUrl, [QUrl("http://ultimaker.com/en/support/software")], {})
  29. Application.getInstance().functionEvent(event)
  30. @pyqtSlot()
  31. def openBugReportPage(self):
  32. event = CallFunctionEvent(self._openUrl, [QUrl("http://github.com/Ultimaker/Cura/issues")], {})
  33. Application.getInstance().functionEvent(event)
  34. ## Reset camera position and direction to default
  35. @pyqtSlot()
  36. def homeCamera(self) -> None:
  37. scene = Application.getInstance().getController().getScene()
  38. camera = scene.getActiveCamera()
  39. camera.setPosition(Vector(-80, 250, 700))
  40. camera.setPerspective(True)
  41. camera.lookAt(Vector(0, 0, 0))
  42. ## Center all objects in the selection
  43. @pyqtSlot()
  44. def centerSelection(self) -> None:
  45. operation = GroupedOperation()
  46. for node in Selection.getAllSelectedObjects():
  47. current_node = node
  48. while current_node.getParent() and current_node.getParent().callDecoration("isGroup"):
  49. current_node = current_node.getParent()
  50. # This was formerly done with SetTransformOperation but because of
  51. # unpredictable matrix deconstruction it was possible that mirrors
  52. # could manifest as rotations. Centering is therefore done by
  53. # moving the node to negative whatever its position is:
  54. center_operation = TranslateOperation(current_node, -current_node._position)
  55. operation.addOperation(center_operation)
  56. operation.push()
  57. ## Multiply all objects in the selection
  58. #
  59. # \param count The number of times to multiply the selection.
  60. @pyqtSlot(int)
  61. def multiplySelection(self, count: int) -> None:
  62. min_offset = Application.getInstance().getBuildVolume().getEdgeDisallowedSize() + 2 # Allow for some rounding errors
  63. job = MultiplyObjectsJob(Selection.getAllSelectedObjects(), count, min_offset = max(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)