"""Detailed quarter-turn cabinet cam latch. Run with Blender's Python."""
import math
import os
import sys

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

v4.reset()

# Materials: powder-coated cabinet face, zinc hardware, stainless moving tongue,
# and a dark elastomer pad under the mounting plate.
cabinet = v4.painted("warm_ivory_enamel", (0.72, 0.59, 0.40), roughness=0.42, coat=0.25)
zinc = v4.metal("satin_zinc", (0.68, 0.72, 0.74), roughness=0.34, brushed=True)
steel = v4.metal("brushed_stainless", (0.78, 0.81, 0.83), roughness=0.27, brushed=True)
dark = v4.rubber("black_epdm", (0.055, 0.06, 0.062), roughness=0.83)
ink = v4.mat("black_marking", (0.018, 0.021, 0.023), roughness=0.72)
brass = v4.metal("warm_detent_brass", (0.68, 0.43, 0.18), roughness=0.31, brushed=False)

# All dimensions in metres. Front face is toward -Y; Z is up.
base = v4.body("base")
moving = v4.body("cam_shaft")

# A cabinet door sample makes the surface-mounted installation legible.
v4.box("cabinet_door_sample", (0.140, 0.016, 0.112), (0, 0, 0.056), bevel=0.0025,
       material=cabinet, body=base)
# Door/frame reveal: 14 mm left of the keeper center, between the latch plate
# and keeper. The cam bridges this seam when locked.
v4.box("door_reveal", (0.0008, 0.0005, 0.106), (0.008, -0.00825, 0.056), bevel=0.0002,
       material=ink, body=base)
# Fine chamfered shoulders soften the two painted leaf edges around the dark reveal.
for side in (-1, 1):
    v4.box(f"seam_bevel_edge_{side}", (0.0010, 0.0007, 0.104),
           (0.008 + side * 0.0010, -0.00845, 0.056), bevel=0.00035,
           material=cabinet, body=base)

# Two compact decorative butt hinges on the far edge clarify that the left
# leaf is the moving door and the right side of the reveal is its frame.
for hi, hz in enumerate((0.032, 0.083)):
    hx = -0.059
    v4.box(f"far_hinge_leaf_{hi}", (0.016, 0.002, 0.020), (hx, -0.009, hz),
           bevel=0.0007, material=zinc, body=base)
    v4.cylinder(f"far_hinge_knuckle_{hi}", 0.0030, 0.018, (-0.068, -0.012, hz),
                segments=32, bevel=0.0004, material=steel, body=base)
    for sx in (-1, 1):
        v4.cylinder(f"far_hinge_screw_{hi}_{sx}", 0.0017, 0.0010,
                    (hx + sx * 0.005, -0.0105, hz), rot=(math.pi / 2, 0, 0),
                    segments=24, bevel=0.0002, material=steel, body=base)

px, pz = -0.027, 0.036
plate_outline = [(-0.020, 0.000), (-0.022, 0.004), (-0.022, 0.049),
                 (-0.019, 0.053), (0.019, 0.053), (0.022, 0.049),
                 (0.022, 0.004), (0.019, 0.000)]
# Elastomer gasket and stamped steel plate both sit flush against the door face.
v4.extrude("mounting_gasket", plate_outline, 0.0012,
           loc=(px, -0.008, pz), rot=(math.pi / 2, 0, 0), bevel=0.0007,
           material=dark, body=base)
v4.extrude("stamped_mounting_plate", plate_outline, 0.0018,
           loc=(px, -0.0092, pz), rot=(math.pi / 2, 0, 0), bevel=0.0009,
           material=zinc, body=base)

# Four countersunk mounting screws, seated at the plate corners.
for sx in (-1, 1):
    for sz in (0.008, 0.045):
        x, z = px + sx * 0.016, pz + sz
        v4.cylinder(f"plate_screw_{sx}_{sz}", 0.0031, 0.0011, (x, -0.01105, z),
                    rot=(math.pi / 2, 0, 0), segments=32, bevel=0.00035,
                    material=steel, body=base)

# Fixed formed keeper 40 mm from the shaft axis. Its short back strap and
# lower return lip capture the broad cam plate while leaving the quarter-turn clear.
kx, kz = 0.007, 0.062
v4.box("keeper_back_strap", (0.010, 0.008, 0.020), (kx + 0.015, -0.012, kz),
       bevel=0.0012, material=zinc, body=base)
v4.box("keeper_return_lip", (0.014, 0.002, 0.003), (kx + 0.013, -0.0255, kz - 0.011),
       bevel=0.0008, material=zinc, body=base)
# Sloped strike lead-in above the lower hook; cam tip rides past its ramp.
v4.extrude("keeper_lead_in_ramp", [(0.0, 0.0), (0.009, 0.0), (0.0, 0.004)], 0.002,
           loc=(kx + 0.006, -0.0265, kz - 0.0095), rot=(math.pi / 2, 0, 0),
           bevel=0.00035, material=steel, body=base)
# Short formed web bridges the lip back to the mounted strap, making one solid bracket.
v4.box("keeper_return_web", (0.004, 0.0105, 0.004), (kx + 0.018, -0.02025, kz - 0.010),
       bevel=0.0006, material=zinc, body=base)
v4.box("keeper_stiffening_bead", (0.003, 0.003, 0.014), (kx + 0.019, -0.015, kz),
       bevel=0.0007, material=steel, body=base)
for i, z in enumerate((kz - 0.006, kz + 0.006)):
    v4.cylinder(f"keeper_screw_{i}", 0.0025, 0.0015, (kx + 0.015, -0.0168, z),
                rot=(math.pi / 2, 0, 0), segments=32, bevel=0.0003,
                material=steel, body=base)

# Raised, high-contrast position labels sit left of the lever's full sweep.
v4.text("label_lock", "LOCK", 0.0048, (px - 0.010, -0.01145, pz + 0.047),
        rot=(math.pi / 2, 0, 0), extrude_depth=0.00028, material=ink, body=base)
v4.text("label_open", "OPEN", 0.0048, (px - 0.010, -0.01145, pz + 0.006),
        rot=(math.pi / 2, 0, 0), extrude_depth=0.00028, material=ink, body=base)

# Two brass position bosses mark the lever's locked and open endpoints.
for name, dx, dz in (("locked", 0.007, 0.025), ("open", -0.025, 0.009)):
    v4.cylinder(f"detent_boss_{name}", 0.0025, 0.0012,
                (px + 0.009 + dx, -0.0120, pz + 0.026 + dz), rot=(math.pi / 2, 0, 0),
                segments=32, bevel=0.0003, material=brass, body=base)

# One keyed rotor: front thumb lever and hub, the through-shaft, and the rear
# cam tongue rotate as a single rigid body. The lever is phased 90 degrees from
# the tongue, as on common quarter-turn latches.
pivot = (px + 0.009, -0.0205, pz + 0.026)
v4.cylinder("shaft_spindle", 0.0030, 0.026, (pivot[0], -0.016, pivot[2]),
            rot=(math.pi / 2, 0, 0), segments=40, bevel=0.00035,
            material=steel, body=moving)
# Rear bearing shoulder / washer against the mounting plate.
v4.cylinder("rear_shaft_washer", 0.0062, 0.002, (pivot[0], -0.0121, pivot[2]),
            rot=(math.pi / 2, 0, 0), segments=40, bevel=0.0004,
            material=zinc, body=moving)
# Flat, broad cam plate with clipped/rounded corners. Its tip is 40 mm from
# the shaft axis and its width is 18 mm, so the full open sweep stays on-panel.
cam_shape = [(-0.005, -0.007), (-0.002, -0.009), (0.024, -0.009),
             (0.036, -0.005), (0.040, 0.000), (0.036, 0.005),
             (0.024, 0.009), (-0.002, 0.009), (-0.005, 0.007)]
v4.extrude("cam_tongue", cam_shape, 0.0032,
           loc=(pivot[0], -0.0205, pivot[2]), rot=(math.pi / 2, 0, 0),
           bevel=0.0008, material=steel, body=moving)
# Circular riveted boss reinforces the blade at the shaft.
v4.cylinder("cam_root_boss", 0.0080, 0.0034, (pivot[0], -0.0222, pivot[2]),
            rot=(math.pi / 2, 0, 0), segments=48, bevel=0.0005,
            material=zinc, body=moving)

# Front bezel and short turned grip. Shaft axis is along Y; lever points upward
# at rest while the cam tongue points right, and both turn through 90 degrees.
v4.cylinder("front_bezel", 0.0100, 0.0034, (pivot[0], -0.0272, pivot[2]),
            rot=(math.pi / 2, 0, 0), segments=48, bevel=0.0008,
            material=zinc, body=moving)
v4.torus("bezel_recess_ring", 0.0074, 0.00055,
         loc=(pivot[0], -0.0291, pivot[2]), rot=(math.pi / 2, 0, 0),
         material=dark, body=moving)
v4.cylinder("hub_cap", 0.0054, 0.0020, (pivot[0], -0.0301, pivot[2]),
            rot=(math.pi / 2, 0, 0), segments=40, bevel=0.00065,
            material=steel, body=moving)
# Visible six-sided retaining nut at the front shaft end.
v4.cylinder("shaft_retaining_nut", 0.0036, 0.0020,
            (pivot[0], -0.03205, pivot[2]), rot=(math.pi / 2, 0, 0),
            segments=6, bevel=0.00035, material=steel, body=moving)
# Faired stamped lever root blends the hub into the slim handle stem.
v4.extrude("faired_lever_root",
           [(-0.0055, 0.004), (0.0055, 0.004), (0.0040, 0.010),
            (0.0030, 0.023), (-0.0030, 0.023), (-0.0040, 0.010)],
           0.0022, loc=(pivot[0], -0.0310, pivot[2]), rot=(math.pi / 2, 0, 0),
           bevel=0.0010, material=steel, body=moving)
# Slim, gently tapered thumb lever, with a rounded grip cap.
v4.rod("thumb_lever_stem", (pivot[0], -0.0330, pivot[2] + 0.006),
       (pivot[0], -0.0330, pivot[2] + 0.026), 0.0035,
       bevel=0.001, material=steel, body=moving)
v4.cylinder("rubber_grip_sleeve", 0.0046, 0.0090,
            (pivot[0], -0.0330, pivot[2] + 0.022),
            bevel=0.00045, material=dark, body=moving)
for ri, dz in enumerate((0.019, 0.021, 0.023, 0.025)):
    v4.torus(f"sleeve_rib_{ri}", 0.00445, 0.00025,
             loc=(pivot[0], -0.0330, pivot[2] + dz),
             segments=40, ring_segments=10, material=steel, body=moving)
v4.sphere("thumb_grip", 0.0062, (pivot[0], -0.0330, pivot[2] + 0.029),
          segments=32, material=zinc, body=moving)
for gi, dz in enumerate((-0.0018, 0.0, 0.0018)):
    v4.torus(f"rubber_grip_knurl_{gi}", 0.00595, 0.00035,
             loc=(pivot[0], -0.0330, pivot[2] + 0.029 + dz),
             rot=(math.pi / 2, 0, 0), segments=48, ring_segments=10,
             material=dark, body=moving)
v4.cylinder("grip_end_button", 0.0032, 0.0012,
            (pivot[0], -0.0390, pivot[2] + 0.029), rot=(math.pi / 2, 0, 0),
            segments=32, bevel=0.0004, material=steel, body=moving)

v4.joint("cam_quarter_turn", "revolute", base, moving,
         origin=pivot, axis=(0, -1, 0), limits=(0.0, math.pi / 2))

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