1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
# Copyright (c) 2015 Ultimaker B.V.
# Uranium is released under the terms of the LGPLv3 or higher.
from UM.Tool import Tool
from UM.Job import Job
from UM.Event import Event, MouseEvent, KeyEvent
from UM.Message import Message
from UM.Scene.ToolHandle import ToolHandle
from UM.Scene.Selection import Selection
from UM.Math.Plane import Plane
from UM.Math.Vector import Vector
from UM.Math.Quaternion import Quaternion
from PyQt5.QtCore import Qt
from UM.Operations.RotateOperation import RotateOperation
from UM.Operations.GroupedOperation import GroupedOperation
from UM.Operations.SetTransformOperation import SetTransformOperation
from UM.Operations.LayFlatOperation import LayFlatOperation
from . import RotateToolHandle
import math
import time
## Provides the tool to rotate meshes and groups
#
# The tool exposes a ToolHint to show the rotation angle of the current operation
class RotateTool(Tool):
def __init__(self):
super().__init__()
self._handle = RotateToolHandle.RotateToolHandle()
self._snap_rotation = True
self._snap_angle = math.radians(15)
self._angle = None
self._angle_update_time = None
self._shortcut_key = Qt.Key_Z
self._progress_message = None
self._iterations = 0
self._total_iterations = 0
self._rotating = False
self.setExposedProperties("ToolHint", "RotationSnap", "RotationSnapAngle")
self._saved_node_positions = []
## Handle mouse and keyboard events
#
# \param event type(Event)
def event(self, event):
super().event(event)
if event.type == Event.KeyPressEvent and event.key == KeyEvent.ShiftKey:
# Snap is toggled when pressing the shift button
self._snap_rotation = (not self._snap_rotation)
self.propertyChanged.emit()
if event.type == Event.KeyReleaseEvent and event.key == KeyEvent.ShiftKey:
# Snap is "toggled back" when releasing the shift button
self._snap_rotation = (not self._snap_rotation)
self.propertyChanged.emit()
if event.type == Event.MousePressEvent and self._controller.getToolsEnabled():
# Start a rotate operation
if MouseEvent.LeftButton not in event.buttons:
return False
id = self._selection_pass.getIdAtPosition(event.x, event.y)
if not id:
return False
if self._handle.isAxis(id):
self.setLockedAxis(id)
else:
# Not clicked on an axis: do nothing.
return False
handle_position = self._handle.getWorldPosition()
# Save the current positions of the node, as we want to rotate around their current centres
self._saved_node_positions = []
for node in Selection.getAllSelectedObjects():
self._saved_node_positions.append((node, node.getPosition()))
if id == ToolHandle.XAxis:
self.setDragPlane(Plane(Vector(1, 0, 0), handle_position.x))
elif id == ToolHandle.YAxis:
self.setDragPlane(Plane(Vector(0, 1, 0), handle_position.y))
elif self._locked_axis == ToolHandle.ZAxis:
self.setDragPlane(Plane(Vector(0, 0, 1), handle_position.z))
else:
self.setDragPlane(Plane(Vector(0, 1, 0), handle_position.y))
self.setDragStart(event.x, event.y)
self._rotating = False
self._angle = 0
if event.type == Event.MouseMoveEvent:
# Perform a rotate operation
if not self.getDragPlane():
return False
if not self.getDragStart():
self.setDragStart(event.x, event.y)
if not self.getDragStart(): #May have set it to None.
return False
if not self._rotating:
self._rotating = True
self.operationStarted.emit(self)
handle_position = self._handle.getWorldPosition()
drag_start = (self.getDragStart() - handle_position).normalized()
drag_position = self.getDragPosition(event.x, event.y)
if not drag_position:
return
drag_end = (drag_position - handle_position).normalized()
try:
angle = math.acos(drag_start.dot(drag_end))
except ValueError:
angle = 0
if self._snap_rotation:
angle = int(angle / self._snap_angle) * self._snap_angle
if angle == 0:
return
rotation = None
if self.getLockedAxis() == ToolHandle.XAxis:
direction = 1 if Vector.Unit_X.dot(drag_start.cross(drag_end)) > 0 else -1
rotation = Quaternion.fromAngleAxis(direction * angle, Vector.Unit_X)
elif self.getLockedAxis() == ToolHandle.YAxis:
direction = 1 if Vector.Unit_Y.dot(drag_start.cross(drag_end)) > 0 else -1
rotation = Quaternion.fromAngleAxis(direction * angle, Vector.Unit_Y)
elif self.getLockedAxis() == ToolHandle.ZAxis:
direction = 1 if Vector.Unit_Z.dot(drag_start.cross(drag_end)) > 0 else -1
rotation = Quaternion.fromAngleAxis(direction * angle, Vector.Unit_Z)
else:
direction = -1
# Rate-limit the angle change notification
# This is done to prevent the UI from being flooded with property change notifications,
# which in turn would trigger constant repaints.
new_time = time.monotonic()
if not self._angle_update_time or new_time - self._angle_update_time > 0.1:
self._angle_update_time = new_time
self._angle += direction * angle
self.propertyChanged.emit()
# Rotate around the saved centeres of all selected nodes
op = GroupedOperation()
for node, position in self._saved_node_positions:
op.addOperation(RotateOperation(node, rotation, rotate_around_point = position))
op.push()
self.setDragStart(event.x, event.y)
if event.type == Event.MouseReleaseEvent:
# Finish a rotate operation
if self.getDragPlane():
self.setDragPlane(None)
self.setLockedAxis(None)
self._angle = None
self.propertyChanged.emit()
if self._rotating:
self.operationStopped.emit(self)
return True
## Return a formatted angle of the current rotate operation
#
# \return type(String) fully formatted string showing the angle by which the mesh(es) are rotated
def getToolHint(self):
return "%d°" % round(math.degrees(self._angle)) if self._angle else None
## Get the state of the "snap rotation to N-degree increments" option
#
# \return type(Boolean)
def getRotationSnap(self):
return self._snap_rotation
## Set the state of the "snap rotation to N-degree increments" option
#
# \param snap type(Boolean)
def setRotationSnap(self, snap):
if snap != self._snap_rotation:
self._snap_rotation = snap
self.propertyChanged.emit()
## Get the number of degrees used in the "snap rotation to N-degree increments" option
#
# \return type(Number)
def getRotationSnapAngle(self):
return self._snap_angle
## Set the number of degrees used in the "snap rotation to N-degree increments" option
#
# \param snap type(Number)
def setRotationSnapAngle(self, angle):
if angle != self._snap_angle:
self._snap_angle = angle
self.propertyChanged.emit()
## Reset the orientation of the mesh(es) to their original orientation(s)
def resetRotation(self):
for node in Selection.getAllSelectedObjects():
node.setMirror(Vector(1,1,1))
Selection.applyOperation(SetTransformOperation, None, Quaternion(), None)
## Initialise and start a LayFlatOperation
#
# Note: The LayFlat functionality is mostly used for 3d printing and should probably be moved into the Cura project
def layFlat(self):
self.operationStarted.emit(self)
self._progress_message = Message("Laying object flat on buildplate...", lifetime = 0, dismissable = False, title = "Object Rotation")
self._progress_message.setProgress(0)
self._iterations = 0
self._total_iterations = 0
for selected_object in Selection.getAllSelectedObjects():
self._layObjectFlat(selected_object)
self._progress_message.show()
operations = Selection.applyOperation(LayFlatOperation)
for op in operations:
op.progress.connect(self._layFlatProgress)
job = LayFlatJob(operations)
job.finished.connect(self._layFlatFinished)
job.start()
## Lays the given object flat. The given object can be a group or not.
def _layObjectFlat(self, selected_object):
if not selected_object.callDecoration("isGroup"):
self._total_iterations += len(selected_object.getMeshDataTransformed().getVertices()) * 2
else:
for child in selected_object.getChildren():
self._layObjectFlat(child)
## Called while performing the LayFlatOperation so progress can be shown
#
# Note that the LayFlatOperation rate-limits these callbacks to prevent the UI from being flooded with property change notifications,
# \param iterations type(int) number of iterations performed since the last callback
def _layFlatProgress(self, iterations):
self._iterations += iterations
self._progress_message.setProgress(100 * self._iterations / self._total_iterations)
## Called when the LayFlatJob is done running all of its LayFlatOperations
#
# \param job type(LayFlatJob)
def _layFlatFinished(self, job):
if self._progress_message:
self._progress_message.hide()
self._progress_message = None
self.operationStopped.emit(self)
## A LayFlatJob bundles multiple LayFlatOperations for multiple selected objects
#
# The job is executed on its own thread, processing each operation in order, so it does not lock up the GUI.
class LayFlatJob(Job):
def __init__(self, operations):
super().__init__()
self._operations = operations
def run(self):
for op in self._operations:
op.process()
|