"""Run in Fusion > Utilities > Scripts. Requires the Keystone Vise document.
Only updates the three existing motion joints. Does not model thread rotation.
"""
import adsk.core, adsk.fusion

def run(context):
    app=adsk.core.Application.get(); ui=app.userInterface
    d=adsk.fusion.Design.cast(app.activeProduct)
    assert d and 'Keystone' in app.activeDocument.name, 'Activate the Keystone Vise document.'
    result,cancelled=ui.inputBox('Carriage X, fine advance, key lift (mm).\nLocked rack positions: 22, 28, ... 100.\nExample: 82, 2, 0','Keystone pose','82, 0, 0')
    if cancelled:return
    x,f,k=[float(n.strip()) for n in result.split(',')]
    assert 22<=x<=100 and 0<=f<=6.5 and 0<=k<=4.2, 'Outside design travel.'
    assert x-22-f>=0, 'The jaw would pass the closed position.'
    assert k>=4.2 or abs((x-22)/6-round((x-22)/6))<0.001, 'Seat the key at a 6 mm rack position.'
    assert not (k>0 and f>0), 'Retract fine adjustment before releasing the key.'
    joints=d.rootComponent.asBuiltJoints
    coarse=adsk.fusion.SliderJointMotion.cast(joints.itemByName('01 COARSE - lift key first').jointMotion)
    fine=adsk.fusion.SliderJointMotion.cast(joints.itemByName('02 FINE - screw advance preview').jointMotion)
    key=adsk.fusion.SliderJointMotion.cast(joints.itemByName('03 KEY - lift 4.2 mm to release').jointMotion)
    fine.slideValue=0;key.slideValue=-.42;coarse.slideValue=(82-x)/10
    key.slideValue=-k/10;fine.slideValue=f/10
    app.activeViewport.refresh()
