"""Detailed hinged single-pole fuse carrier. Run with Blender's Python."""
import math, os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()
# Materials
phenolic = v4.plastic("charcoal_thermoset", (0.115, 0.13, 0.13), roughness=0.48, texture=0.10)
carrier_mat = v4.plastic("warm_black_moulding", (0.085, 0.073, 0.058), roughness=0.48, texture=0.12)
steel = v4.metal("tin_plated_steel", color=(0.72, 0.75, 0.76), roughness=0.24, brushed=True)
brass = v4.metal("spring_copper_alloy", color=(0.77, 0.48, 0.20), roughness=0.28, brushed=True)
bright = v4.metal("nickel_contact", color=(0.86, 0.88, 0.88), roughness=0.2, brushed=False)
ceramic = v4.plastic("ivory_ceramic", color=(0.88, 0.83, 0.68), roughness=0.32, texture=0.04)
red = v4.painted("rating_red", (0.72, 0.055, 0.035), roughness=0.36)
white = v4.mat("engraving_white", (0.91, 0.88, 0.76), roughness=0.62)

# Principal envelope: 88 W x 108 H x 49 D mm closed.
base = v4.body("base")
carrier = v4.body("carrier")

# Fixed moulded mounting base, back wall and raised front field.
v4.box("base_backplate", (0.088, 0.012, 0.108), (0, 0.010, 0.058), bevel=0.0035, material=phenolic, body=base)
v4.box("front_insulator", (0.078, 0.010, 0.096), (0, -0.001, 0.058), bevel=0.003, material=phenolic, body=base)
# Raised perimeter shoulders are split into four rails and frame the mechanism.
v4.box("rim_left", (0.004, 0.012, 0.097), (-0.040, -0.003, 0.058), bevel=0.0018, material=carrier_mat, body=base)
v4.box("rim_right", (0.004, 0.012, 0.097), (0.040, -0.003, 0.058), bevel=0.0018, material=carrier_mat, body=base)
v4.box("rim_top", (0.078, 0.012, 0.004), (0, -0.003, 0.105), bevel=0.0015, material=carrier_mat, body=base)
# Side wire-terminal wells, copper clamps and accessible slotted clamp screws.
for sx in (-1, 1):
    x = sx * 0.028
    v4.box(f"terminal_well_{sx}", (0.020, 0.014, 0.013), (x, -0.012, 0.025), bevel=0.002, material=carrier_mat, body=base)
    v4.box(f"terminal_copper_{sx}", (0.017, 0.004, 0.008), (x, -0.021, 0.025), bevel=0.001, material=brass, body=base)
    v4.cylinder(f"terminal_screw_{sx}", 0.0042, 0.0022, (x, -0.024, 0.025), rot=(math.pi/2, 0, 0),
                segments=32, material=steel, body=base)
    v4.box(f"terminal_slot_{sx}", (0.0055, 0.0008, 0.0008), (x, -0.0253, 0.025), bevel=0.00025, material=carrier_mat, body=base)
# Fixed contacts sit directly above and below the fuse end-cap positions. Each has
# paired spring fingers and a broad silver contact land facing the removable cap.
for s, zc in ((-1, 0.046), (1, 0.095)):
    # The clip frame is a compact copper U around the cartridge end.
    v4.box(f"line_clip_{s}_bridge", (0.014, 0.004, 0.003), (0, -0.010, zc + s*0.004), bevel=0.0006, material=brass, body=base)
    for sx in (-1, 1):
        v4.box(f"line_clip_{s}_finger_{sx}", (0.002, 0.006, 0.008), (sx*0.0078, -0.014, zc), bevel=0.0006, material=brass, body=base)
        v4.box(f"line_contact_pad_{s}_{sx}", (0.002, 0.003, 0.006), (sx*0.0058, -0.014, zc), bevel=0.0004, material=bright, body=base)
    # Terminal strap runs from spring clip back to the clamping well.
    v4.box(f"line_strap_{s}", (0.006, 0.015, 0.005), (0, -0.0125, zc+s*0.004), bevel=0.0007, material=brass, body=base)
# Hinge ears on the fixed base, pin axis is X.
for sx in (-1, 1):
    v4.box(f"hinge_ear_{sx}", (0.009, 0.014, 0.014), (sx*0.019, -0.015, 0.019), bevel=0.002, material=phenolic, body=base)
    # Annular bushing with 5.2 mm clear bore leaves 0.7 mm running clearance
    # around the 4.5 mm carrier barrel while remaining concentric on the pin.
    v4.torus(f"hinge_bushing_{sx}", 0.0062, 0.001, loc=(sx*0.019, -0.020, 0.019), rot=(0, math.pi/2, 0),
             material=brass, body=base)
# Panel fixing countersinks, top and bottom flanges.
for z in (0.010, 0.106):
    for sx in (-1, 1):
        x = sx*0.034
        v4.cylinder(f"mount_screw_{z}_{sx}", 0.0035, 0.0015, (x, -0.006, z), rot=(math.pi/2,0,0),
                    segments=24, material=steel, body=base)
        v4.box(f"mount_slot_{z}_{sx}", (0.0038, 0.0007, 0.0006), (x, -0.0069, z), bevel=0.0002, material=carrier_mat, body=base)
# Discreet fixed label at the lower edge.
v4.box("rating_plate", (0.044, 0.0012, 0.007), (0, -0.007, 0.015), bevel=0.001, material=steel, body=base)
v4.text("base_marking", "FUSE  250V", 0.0030, loc=(-0.018, -0.0078, 0.0138), rot=(math.pi/2,0,0),
        extrude_depth=0.00025, material=white, body=base)

# Swinging carrier rests upright in front of the fixed contacts. Its lower boss
# is concentric with the hinge axis; side cheeks and spine make one stiff moulding.
H_Y, H_Z = -0.020, 0.019
# Open central window exposes the fuse while leaving a strong molded perimeter frame.
v4.box("carrier_lower_crossbar", (0.050, 0.009, 0.018), (0, -0.028, 0.032), bevel=0.002, material=carrier_mat, body=carrier)
v4.box("carrier_upper_crossbar", (0.050, 0.009, 0.007), (0, -0.028, 0.096), bevel=0.0018, material=carrier_mat, body=carrier)
for sx in (-1, 1):
    v4.box(f"carrier_window_rail_{sx}", (0.006, 0.009, 0.053), (sx*0.015, -0.028, 0.0695), bevel=0.0015, material=carrier_mat, body=carrier)
for sx in (-1, 1):
    v4.box(f"carrier_cheek_{sx}", (0.008, 0.012, 0.071), (sx*0.021, -0.027, 0.058), bevel=0.0018, material=carrier_mat, body=carrier)
# Raised finger grip, sculpted from stacked transverse ribs on the pull handle.
v4.box("pull_handle_neck", (0.024, 0.010, 0.018), (0, -0.030, 0.091), bevel=0.003, material=phenolic, body=carrier)
v4.box("pull_handle_grip", (0.041, 0.012, 0.012), (0, -0.031, 0.099), bevel=0.004, material=carrier_mat, body=carrier)
for i in range(5):
    x = -0.014 + i*0.007
    v4.box(f"grip_rib_{i}", (0.0015, 0.0012, 0.007), (x, -0.0372, 0.099), bevel=0.0005, material=phenolic, body=carrier)
# Fuse link is a small cylindrical ceramic cartridge seated vertically in sprung clips.
LINK_Y = -0.014
v4.cylinder("fuse_ceramic_body", 0.0052, 0.040, (0, LINK_Y, 0.0705), segments=48, material=ceramic, body=carrier)
for s, z in ((-1, 0.047), (1, 0.094)):
    v4.cylinder(f"fuse_endcap_{s}", 0.0061, 0.006, (0, LINK_Y, z), segments=48, material=bright, body=carrier)
    v4.torus(f"endcap_bead_{s}", 0.0058, 0.00045, loc=(0, LINK_Y, z+s*0.0024), material=steel, body=carrier)
    # Two formed plated clip straps crimp around the cartridge ends.
    v4.box(f"fuse_clip_{s}_front", (0.014, 0.002, 0.006), (0, LINK_Y-0.007, z), bevel=0.0007, material=brass, body=carrier)
    for sx in (-1, 1):
        v4.box(f"fuse_clip_{s}_wing_{sx}", (0.002, 0.008, 0.006), (sx*0.006, LINK_Y-0.002, z), bevel=0.0006, material=brass, body=carrier)
# Narrow red maker/rating band on the porcelain middle, oriented toward the viewer.
v4.box("fuse_rating_band", (0.009, 0.0012, 0.008), (0, LINK_Y-0.0054, 0.071), bevel=0.001, material=red, body=carrier)
v4.text("fuse_rating_text", "16A", 0.0032, loc=(-0.0041, LINK_Y-0.0062, 0.070), rot=(math.pi/2,0,0),
        extrude_depth=0.0002, material=white, body=carrier)
# Visible pivot barrel and through-pin; carrier-owned pin rotates with the cradle.
v4.cylinder("carrier_hinge_barrel", 0.0045, 0.047, (0, H_Y, H_Z), rot=(0, math.pi/2, 0), segments=40, material=carrier_mat, body=carrier)
v4.cylinder("hinge_pin", 0.0022, 0.055, (0, H_Y, H_Z), rot=(0, math.pi/2, 0), segments=32, material=steel, body=carrier)
for sx in (-1, 1):
    v4.cylinder(f"hinge_pin_head_{sx}", 0.0033, 0.0018, (sx*0.0284, H_Y, H_Z), rot=(0, math.pi/2, 0),
                segments=24, material=bright, body=carrier)
# Moulded raised service lettering on the carrier cheek face.
v4.text("carrier_brand", "PULL", 0.0031, loc=(-0.011, -0.0343, 0.087), rot=(math.pi/2,0,0),
        extrude_depth=0.00025, material=white, body=carrier)

v4.joint("carrier_hinge", "revolute", base, carrier, origin=(0, H_Y, H_Z), axis=(1,0,0),
         limits=(0, math.radians(72)))

# Set camera and lights for convenient opening in Blender; check.py supplies its own renders.
# Keep the saved asset at the mechanically correct closed/rest state.
v4.save(os.path.join(os.path.dirname(os.path.abspath(__file__)), "asset.blend"))
print("saved asset.blend")
