Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotation by input & snap angle input #978

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions plugins/Tools/RotateTool/RotateTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(self):
self._iterations = 0
self._total_iterations = 0
self._rotating = False
self.setExposedProperties("ToolHint", "RotationSnap", "RotationSnapAngle", "SelectFaceSupported", "SelectFaceToLayFlatMode")
self.setExposedProperties("ToolHint", "RotationSnap", "RotationSnapAngle", "SelectFaceSupported", "SelectFaceToLayFlatMode", "RotationX", "RotationY", "RotationZ")
self._saved_node_positions = []

self._active_widget = None # type: Optional[RotateToolHandle.ExtraWidgets]
Expand Down Expand Up @@ -229,6 +229,30 @@ def event(self, event):
self.operationStopped.emit(self)
return True

def setRotationX(self, rotation_x: str) -> None:
angle = math.radians(float(rotation_x))
self._rotateModel(angle, Vector.Unit_X)
self.propertyChanged.emit()

def setRotationY(self, rotation_y: str) -> None:
angle = math.radians(float(rotation_y))
self._rotateModel(angle, Vector.Unit_Y)
self.propertyChanged.emit()

def setRotationZ(self, rotation_z: str) -> None:
angle = math.radians(float(rotation_z))
self._rotateModel(angle, Vector.Unit_Z)
self.propertyChanged.emit()

def getRotationX(self) -> float:
return 0

def getRotationY(self) -> float:
return 0

def getRotationZ(self) -> float:
return 0

def _onSelectedFaceChanged(self):
if not self._select_face_mode:
return
Expand Down Expand Up @@ -307,10 +331,11 @@ def setRotationSnap(self, snap):
def getRotationSnapAngle(self):
"""Get the number of degrees used in the "snap rotation to N-degree increments" option"""

return self._snap_angle
return math.degrees(self._snap_angle)

def setRotationSnapAngle(self, angle):
"""Set the number of degrees used in the "snap rotation to N-degree increments" option"""
angle = math.radians(float(angle))

if angle != self._snap_angle:
self._snap_angle = angle
Expand Down Expand Up @@ -400,6 +425,23 @@ def _layFlatFinished(self, job):

self.operationStopped.emit(self)

def _rotateModel(self, angle, vector_unit) -> None:
rotation = Quaternion.fromAngleAxis(angle, vector_unit)
self._saved_node_positions = []
for node in self._getSelectedObjectsWithoutSelectedAncestors():
self._saved_node_positions.append((node, node.getPosition()))

# Rotate around the saved centers of all selected nodes
if len(self._saved_node_positions) > 1:
op = GroupedOperation()
for node, position in self._saved_node_positions:
op.addOperation(RotateOperation(node, rotation, rotate_around_point=position))
op.push()
else:
for node, position in self._saved_node_positions:
RotateOperation(node, rotation, rotate_around_point=position).push()
return True


class LayFlatJob(Job):
"""A LayFlatJob bundles multiple LayFlatOperations for multiple selected objects
Expand Down
257 changes: 255 additions & 2 deletions plugins/Tools/RotateTool/RotateTool.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,44 @@
// Uranium is released under the terms of the LGPLv3 or higher.

import QtQuick 2.2
import UM 1.5 as UM
import UM 1.7 as UM

Item
{
id: base
width: childrenRect.width
height: childrenRect.height
UM.I18nCatalog { id: catalog; name: "uranium"}

property string xText
property string yText
property string zText
property string snapText
property bool snapEnabled: snapRotationCheckbox.checked

//Rounds a floating point number to 4 decimals. This prevents floating
//point rounding errors.
//
//input: The number to round.
//decimals: The number of decimals (digits after the radix) to round to.
//return: The rounded number.
function roundFloat(input, decimals)
{
//First convert to fixed-point notation to round the number to 4 decimals and not introduce new floating point errors.
//Then convert to a string (is implicit). The fixed-point notation will be something like "3.200".
//Then remove any trailing zeroes and the radix.
var output = ""
if (input !== undefined)
{
output = input.toFixed(decimals).replace(/\.?0*$/, ""); //Match on periods, if any ( \.? ), followed by any number of zeros ( 0* ), then the end of string ( $ ).
}
if (output == "-0")
{
output = "0"
}
return output
}

UM.ToolbarButton
{
id: resetRotationButton
Expand Down Expand Up @@ -53,7 +83,8 @@ Item
// visible: ! UM.Controller.properties.getValue("SelectFaceSupported");
}

UM.ToolbarButton{
UM.ToolbarButton
{
id: alignFaceButton

anchors.left: layFlatButton.visible ? layFlatButton.right : resetRotationButton.right
Expand All @@ -77,6 +108,57 @@ Item
visible: UM.Controller.properties.getValue("SelectFaceSupported") == true //Might be undefined if we're switching away from the RotateTool!
}

Grid
{
id: textfields

anchors.leftMargin: UM.Theme.getSize("default_margin").width
anchors.top: snapRotationCheckbox.bottom
visible: snapRotationCheckbox.checked

columns: 2
flow: Grid.TopToBottom
spacing: Math.round(UM.Theme.getSize("default_margin").width / 2)

UM.Label
{
height: UM.Theme.getSize("setting_control").height
text: "Snap Angle"
width: Math.ceil(contentWidth) //Make sure that the grid cells have an integer width.
}

UM.TextFieldWithUnit
{
id: angleTextField
width: UM.Theme.getSize("setting_control").width
height: UM.Theme.getSize("setting_control").height
unit: "°"
text: snapText

validator: UM.FloatValidator
{
maxBeforeDecimal: 3
maxAfterDecimal: 2
}
onEditingFinished:
{
var modified_text = text.replace(",", ".") // User convenience. We use dots for decimal values
if(text != "")
{
UM.Controller.setProperty("RotationSnapAngle", modified_text)
}
}
onActiveFocusChanged:
{
if(!activeFocus && text == "")
{
snapText = 0.1; // Need to change it to something else so we can force it to getvalue
snapText = UM.Controller.properties.getValue("RotationSnapAngle")
}
}
}
}

UM.CheckBox
{
id: snapRotationCheckbox
Expand All @@ -90,6 +172,149 @@ Item
onClicked: UM.Controller.setProperty("RotationSnap", checked)
}

Item
{
id: dynamicContainer
anchors.top: snapRotationCheckbox.bottom
anchors.topMargin: UM.Theme.getSize("default_margin").width

width: manualInputTextFields.visible ? manualInputTextFields.width : 0
height: manualInputTextFields.visible ? manualInputTextFields.height : 0

Grid
{
id: manualInputTextFields
columns: 2
flow: Grid.TopToBottom
spacing: Math.round(UM.Theme.getSize("default_margin").width / 2)
visible: !snapEnabled

UM.Label
{
height: UM.Theme.getSize("setting_control").height
text: "X"
color: UM.Theme.getColor("x_axis")
width: Math.ceil(contentWidth) // Make sure that the grid cells have an integer width.
}

UM.Label
{
height: UM.Theme.getSize("setting_control").height
text: "Y"
color: UM.Theme.getColor("z_axis"); // This is intentional. The internal axis are switched.
width: Math.ceil(contentWidth) // Make sure that the grid cells have an integer width.
}

UM.Label
{
height: UM.Theme.getSize("setting_control").height
text: "Z"
color: UM.Theme.getColor("y_axis"); // This is intentional. The internal axis are switched.
width: Math.ceil(contentWidth) // Make sure that the grid cells have an integer width.
}

UM.TextFieldWithUnit
{
id: xAngleTextField
width: UM.Theme.getSize("setting_control").width
height: UM.Theme.getSize("setting_control").height
unit: "°"
text: xText

validator: UM.FloatValidator
{
maxBeforeDecimal: 3
maxAfterDecimal: 2
}
onEditingFinished:
{
var modified_text = text.replace(",", ".") // User convenience. We use dots for decimal values
if(text != "")
{
UM.Controller.setProperty("RotationX", modified_text)
text = "0"
}
}
onActiveFocusChanged:
{
if(!activeFocus && text == "")
{
xText = 0.1; // Need to change it to something else so we can force it to getvalue
xText = 0
}
}
}
wawanbreton marked this conversation as resolved.
Show resolved Hide resolved

UM.TextFieldWithUnit
{
id: yAngleTextField
width: UM.Theme.getSize("setting_control").width
height: UM.Theme.getSize("setting_control").height
unit: "°"
text: yText

validator: UM.FloatValidator
{
maxBeforeDecimal: 3
maxAfterDecimal: 2
}
onEditingFinished:
{
var modified_text = text.replace(",", ".") // User convenience. We use dots for decimal values
if(text != "")
{
// Yes this is intentional. Y & Z are flipped between model axes and build plate axes
UM.Controller.setProperty("RotationZ", modified_text)
text = "0"
}
}
onActiveFocusChanged:
{
if(!activeFocus && text == "")
{
yText = 0.1; // Need to change it to something else so we can force it to getvalue
// Yes this is intentional. Y & Z are flipped between model axes and build plate axes
yText = 0
}
}
}

UM.TextFieldWithUnit
{
id: zAngleTextField
width: UM.Theme.getSize("setting_control").width
height: UM.Theme.getSize("setting_control").height
unit: "°"
text: zText

validator: UM.FloatValidator
{
maxBeforeDecimal: 3
maxAfterDecimal: 2
}
onEditingFinished:
{
var modified_text = text.replace(",", ".") // User convenience. We use dots for decimal values
if(text != "")
{
// Yes this is intentional. Y & Z are flipped between model axes and build plate axes
UM.Controller.setProperty("RotationY", modified_text)
text = "0"
}
}
onActiveFocusChanged:
{
if(!activeFocus && text == "")
{
zText = 0.1; // Need to change it to something else so we can force it to getvalue
// Yes this is intentional. Y & Z are flipped between model axes and build plate axes
zText = 0
}
}
}
}
}

Binding
{
target: snapRotationCheckbox
Expand All @@ -103,4 +328,32 @@ Item
property: "checked"
value: UM.Controller.properties.getValue("SelectFaceToLayFlatMode")
}

Binding
{
target: base
property: "snapText"
value: base.roundFloat(UM.Controller.properties.getValue("RotationSnapAngle"), 2)
}

Binding
{
target: base
property: "xText"
value: base.roundFloat(UM.Controller.properties.getValue("RotationX"), 2)
}

Binding
{
target: base
property: "zText"
value: base.roundFloat(UM.Controller.properties.getValue("RotationY"), 2)
}

Binding
{
target: base
property: "yText"
value: base.roundFloat(UM.Controller.properties.getValue("RotationZ"), 2)
}
}
1 change: 1 addition & 0 deletions plugins/Tools/ScaleTool/ScaleTool.qml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Item

checked: !UM.Controller.properties.getValue("NonUniformScale")
onClicked: UM.Controller.setProperty("NonUniformScale", !checked)
tooltip: catalog.i18nc("@checkbox:description", "If a model has been rotated then 'Non-Uniform Scaling' might result in skewing of the model.")
}

Binding
Expand Down
Loading