"""Detailed articulated tabletop manual die-cutting roller press."""
import math
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()

# Finishes: molded berry housing, satin hardware, and contrasting elastomer/plates.
berry = v4.painted("berry_powdercoat", (0.52, 0.055, 0.16), roughness=0.28, coat=0.65)
berry_dark = v4.painted("deep_berry_trim", (0.24, 0.025, 0.075), roughness=0.3)
steel = v4.metal("satin_stainless", (0.72, 0.76, 0.78), roughness=0.27, brushed=True)
bright = v4.metal("bright_zinc", (0.84, 0.86, 0.87), roughness=0.22, brushed=False)
black = v4.rubber("charcoal_grip", (0.035, 0.04, 0.045), roughness=0.72)
roller_mat = v4.rubber("roller_elastomer", (0.095, 0.105, 0.115), roughness=0.62)
mat_teal = v4.plastic("cutting_mat_teal", (0.035, 0.31, 0.31), roughness=0.55, texture=0.18)
plate_ivory = v4.plastic("translucent_ivory_plate", (0.83, 0.78, 0.62), roughness=0.37, texture=0.08)
plate_clear = v4.mat("smoke_clear_plate", (0.29, 0.34, 0.37), metallic=0.15, roughness=0.28, coat=0.35)
label = v4.mat("warm_white_print", (0.95, 0.88, 0.68), roughness=0.5)

root = v4.body("main_body")

# Four compliant feet under the wide, low base.
for sy in (-1, 1):
    v4.box(f"base_cross_rail_{sy}", (0.335, 0.064, 0.052), (0, sy * 0.073, 0.054), bevel=0.010, material=berry, body=root)
for ix in (-1, 1):
    for iy in (-1, 1):
        x, y = ix * 0.135, iy * 0.078
        v4.cylinder(f"foot_mount_{ix}_{iy}", 0.014, 0.012, (x, y, 0.025), material=berry_dark, body=root)
        v4.lathe(f"rubber_foot_{ix}_{iy}", [(0, 0), (0.012, 0), (0.016, 0.004), (0.016, 0.014), (0.013, 0.019), (0, 0.019)],
                 loc=(x, y, 0.001), material=black, body=root)

# Cast side cheeks and bridge make the rigid roller frame; the centre remains open for the nip.
for side in (-1, 1):
    x = side * 0.145
    v4.box(f"frame_cheek_{'L' if side < 0 else 'R'}", (0.038, 0.166, 0.158), (x, 0.002, 0.147), bevel=0.018, material=berry, body=root)
    # Bearing seats on the inner cheek faces, with external pressed-in retaining plates.
    for z, nm in ((0.157, "upper"), (0.075, "lower")):
        v4.cylinder(f"{nm}_bearing_inner_{side}", 0.025, 0.004, (side * 0.124, 0.0, z), rot=(0, math.pi/2, 0),
                    material=berry_dark, body=root)
        v4.cylinder(f"{nm}_bearing_race_{side}", 0.018, 0.006, (side * 0.122, 0.0, z), rot=(0, math.pi/2, 0),
                    material=steel, body=root)
        # Four fastener heads on the outward cheek face.
        for dy in (-0.046, 0.046):
            dz = z + (-0.030 if dy < 0 else 0.030)
            v4.cylinder(f"frame_screw_{side}_{nm}_{dy}", 0.0042, 0.003, (side * 0.165, dy, dz),
                        rot=(0, math.pi/2, 0), segments=24, material=bright, body=root)
v4.box("upper_cross_bridge", (0.32, 0.17, 0.036), (0, 0.002, 0.235), bevel=0.014, material=berry, body=root)
v4.box("bridge_inset", (0.19, 0.006, 0.012), (0, -0.084, 0.226), bevel=0.003, material=berry_dark, body=root)

# Side fences guide the plate edges. They stop short of the roller barrel and are carried by the cheeks.
for side in (-1, 1):
    for y, nm in ((-0.095, "front"), (0.095, "rear")):
        v4.box(f"{nm}_plate_fence_{side}", (0.028, 0.106, 0.018), (side * 0.112, y, 0.120),
               bevel=0.003, material=berry, body=root)

# Main upper roller and keyed crank are one rigid assembly: turning the crank directly turns this roller.
drive = v4.body("drive_roller_crank")
v4.cylinder("upper_roller", 0.033, 0.25, (0, 0, 0.160), rot=(0, math.pi/2, 0), segments=64,
            bevel=0.002, material=roller_mat, body=drive)
# A through-shaft passes into the roller barrel and carries both ends in the fixed bearing seats.
v4.cylinder("upper_through_shaft", 0.009, 0.34, (0, 0, 0.160), rot=(0, math.pi/2, 0), material=steel, body=drive)
for side in (-1, 1):
    v4.cylinder(f"upper_shaft_collar_{side}", 0.015, 0.010, (side * 0.126, 0, 0.160), rot=(0, math.pi/2, 0),
                material=bright, body=drive)
# Crank hub and offset arm are on the outboard (right) shaft; the grip turns with the keyed arm.
v4.cylinder("crank_hub", 0.022, 0.014, (0.171, 0, 0.160), rot=(0, math.pi/2, 0), material=berry_dark, body=drive)
v4.cylinder("crank_hub_cap", 0.013, 0.004, (0.181, 0, 0.160), rot=(0, math.pi/2, 0), material=bright, body=drive)
v4.rod("crank_arm", (0.171, 0, 0.160), (0.171, -0.076, 0.160), 0.008, material=steel, body=drive)
v4.cylinder("crank_grip_core", 0.010, 0.058, (0.210, -0.076, 0.160), rot=(0, math.pi/2, 0), material=steel, body=drive)
v4.cylinder("crank_rotating_grip", 0.014, 0.052, (0.232, -0.076, 0.160), rot=(0, math.pi/2, 0), material=black, body=drive)
v4.cylinder("crank_grip_endcap", 0.014, 0.004, (0.259, -0.076, 0.160), rot=(0, math.pi/2, 0), material=berry_dark, body=drive)
v4.joint("upper_roller_spin", "continuous", root, drive, origin=(0, 0, 0.160), axis=(1, 0, 0))

# Opposing lower nip roller on its own continuous axle.
idler = v4.body("idler_roller")
v4.cylinder("lower_roller", 0.033, 0.25, (0, 0, 0.075), rot=(0, math.pi/2, 0), segments=64,
            bevel=0.002, material=roller_mat, body=idler)
v4.cylinder("lower_through_shaft", 0.009, 0.34, (0, 0, 0.075), rot=(0, math.pi/2, 0), material=steel, body=idler)
for side in (-1, 1):
    v4.cylinder(f"lower_collar_{side}", 0.015, 0.008, (side * 0.126, 0, 0.075), rot=(0, math.pi/2, 0), material=bright, body=idler)
v4.joint("lower_roller_spin", "continuous", root, idler, origin=(0, 0, 0.075), axis=(1, 0, 0))

# Layered cutting sandwich, guided from the front through the nip. Its three plates are one sliding body.
plates = v4.body("plate_sandwich")
py = -0.182
v4.box("cutting_mat", (0.196, 0.265, 0.006), (0, py, 0.114), bevel=0.002, material=mat_teal, body=plates)
v4.box("ivory_cutting_plate", (0.184, 0.244, 0.005), (0, py + 0.006, 0.1195), bevel=0.0015, material=plate_ivory, body=plates)
v4.box("smoke_die_plate", (0.164, 0.221, 0.004), (0, py + 0.012, 0.124), bevel=0.001, material=plate_clear, body=plates)
# Small alignment marks printed on the visible top sheet.
for sx in (-1, 1):
    v4.box(f"registration_tick_{sx}", (0.002, 0.026, 0.0007), (sx * 0.068, py - 0.065, 0.12635), bevel=0,
           material=label, body=plates)
v4.joint("plate_feed", "prismatic", root, plates, origin=(0, -0.13, 0.126), axis=(0, 1, 0), limits=(0, 0.22))

# Folding front and rear feed tables, with molded ribs and slim steel hinge barrels.
front = v4.body("front_platform")
v4.box("front_tray", (0.252, 0.148, 0.014), (0, -0.179, 0.104), bevel=0.006, material=berry, body=front)
v4.box("front_tray_lip", (0.252, 0.012, 0.019), (0, -0.251, 0.105), bevel=0.004, material=berry_dark, body=front)
for x in (-0.092, -0.046, 0, 0.046, 0.092):
    v4.box(f"front_tray_rib_{x}", (0.004, 0.112, 0.0015), (x, -0.179, 0.1115), bevel=0.0005, material=berry_dark, body=front)
for x in (-0.125, 0.125):
    v4.cylinder(f"front_hinge_knuckle_{x}", 0.010, 0.040, (x, -0.105, 0.111), rot=(0, math.pi/2, 0), material=steel, body=front)
v4.joint("front_tray_hinge", "revolute", root, front, origin=(0, -0.105, 0.111), axis=(1, 0, 0), limits=(0, math.pi/3))

rear = v4.body("rear_platform")
v4.box("rear_tray", (0.252, 0.148, 0.014), (0, 0.179, 0.104), bevel=0.006, material=berry, body=rear)
v4.box("rear_tray_lip", (0.252, 0.012, 0.019), (0, 0.251, 0.105), bevel=0.004, material=berry_dark, body=rear)
for x in (-0.092, -0.046, 0, 0.046, 0.092):
    v4.box(f"rear_tray_rib_{x}", (0.004, 0.112, 0.0015), (x, 0.179, 0.1115), bevel=0.0005, material=berry_dark, body=rear)
for x in (-0.125, 0.125):
    v4.cylinder(f"rear_hinge_knuckle_{x}", 0.010, 0.040, (x, 0.105, 0.111), rot=(0, math.pi/2, 0), material=steel, body=rear)
v4.joint("rear_tray_hinge", "revolute", root, rear, origin=(0, 0.105, 0.111), axis=(1, 0, 0), limits=(-math.pi/3, 0))

# Brand plaque and raised lettering on the front face of the fixed base.
v4.box("maker_badge", (0.092, 0.003, 0.022), (0, -0.1045, 0.055), bevel=0.003, material=berry_dark, body=root)
v4.text("maker_name", "CUT / PRESS", 0.009, loc=(0, -0.1065, 0.055), rot=(math.pi/2, 0, 0),
        extrude_depth=0.0004, material=label, body=root)
v4.text("top_mark", "CRAFT  •  ROLLER PRESS", 0.010, loc=(0, -0.033, 0.255), rot=(0, 0, 0),
        extrude_depth=0.0003, material=label, body=root)

v4.save(os.path.join(os.getcwd(), "asset.blend"))
print("saved asset.blend")
