# -*- coding: UTF-8 -*-
# 
# ============================================================================
# CC0 "No Rights Reserved"
# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain 
# worldwide. See http://creativecommons.org/publicdomain/zero/1.0/.
# ============================================================================

# Contributed to Mecway Forum by @Victor

# \\ Modified by cwharpe 8/11/22.  Permits Anchoring the operations to Last Picked Node.
# \\ Skews nodes parallel to the specified axis & by an angle about the Normal (drilling) axis.
# \\ Select nodes before using.  CTRL Re-Pick Anchor Node LAST.

mw.message("PRE-Select Nodes to SKEW.  CTRL Re-Pick Anchor Node LAST:")

import math

options = ["x", "y", "z"]
skew = "abc"
axis = "abc"
i = 1
quit = 5

while (skew not in options) or (axis not in options):
    mw.message("Choose Anchor pin Drill axis Normal to Skew direction.")
    axis = mw.input("Drill axis (Normal to skew plane): (x, y, z)").lower()
    skew = mw.input("Skewing axis (parallel to skew plane): (x, y, z)").lower()
    i=i+1
    if i == quit:
        break
    if axis == skew:    # No provision for axis=skew
        axis = "abc"    
        skew = "abc"
        continue
        
angle = float(mw.input(u"Skew Angle (about drill axis) +/- (°)"))
ratio = math.tan(angle * math.pi/180)

#    Re-assign selection
cloud = mw.selected_nodes()
nn = len(cloud)
#    The CTRL Re-pick node (Hub) is last in selection list.
hub = mw.node(cloud[nn-1])

# Get the node's existing coordinates
for node_id in cloud:
    coordinates = mw.node(node_id)

    # Choose the skew
    if axis == "x":
        if skew == "y":
            existing_coordinate = coordinates.y
            reference_coordinate = coordinates.z
            setter = mw.set_node_y
            anchor = hub.z
        if skew == "z":
            existing_coordinate = coordinates.z
            reference_coordinate = coordinates.y
            setter = mw.set_node_z
            anchor = hub.y    

    if axis == "y":
        if skew == "x":
            existing_coordinate = coordinates.x
            reference_coordinate = coordinates.z
            setter = mw.set_node_x
            anchor = hub.z
        if skew == "z":
            existing_coordinate = coordinates.z
            reference_coordinate = coordinates.x
            setter = mw.set_node_z
            anchor = hub.x

    if axis == "z":
        if skew == "x":
            existing_coordinate = coordinates.x
            reference_coordinate = coordinates.y
            setter = mw.set_node_x
            anchor = hub.y
        if skew == "y":
            existing_coordinate = coordinates.y
            reference_coordinate = coordinates.x
            setter = mw.set_node_y
            anchor = hub.x
            
    # Change the node's coordinate.
    setter(node_id, (existing_coordinate + ratio * (reference_coordinate - anchor)))