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

v4.reset()
# Finishes
shell = v4.plastic("graphite_polymer", (0.115, 0.135, 0.15), roughness=0.34, texture=0.12)
yellow = v4.plastic("safety_yellow_polymer", (0.96, 0.78, 0.02), roughness=0.31, texture=0.10)
black = v4.rubber("slider_elastomer", (0.035, 0.043, 0.047), roughness=0.65)
steel = v4.metal("satin_stainless", (0.68, 0.72, 0.75), roughness=0.31, brushed=True)
blade_mat = v4.metal("blade_ground_steel", (0.78, 0.80, 0.82), roughness=0.24, brushed=True)
fastener = v4.metal("dark_zinc_fasteners", (0.24, 0.28, 0.31), roughness=0.28, brushed=False)
ink = v4.mat("dark_ink", (0.025, 0.035, 0.038), metallic=0.0, roughness=0.62)

handle = v4.body("handle")
# Flat molded grip outline, chamfered in plan; a lower shell plus two colored upper scales.
outline = [(-0.076,-0.010),(-0.069,-0.0155),(-0.050,-0.016), (0.052,-0.016),
           (0.067,-0.014), (0.076,-0.009), (0.076,0.009), (0.067,0.014),
           (0.052,0.016),(-0.050,0.016),(-0.069,0.0155),(-0.076,0.010)]
v4.extrude("lower_grip_shell", outline, 0.0075, loc=(0,0,0.001), bevel=0.0012, material=shell, body=handle)
# Two top scales flank a narrow open slide track. Left side has a molded grip texture.
left_outline=[(-0.073,-0.010),(-0.067,-0.0147),(-0.050,-0.015),(0.048,-0.015),(0.060,-0.012),
              (0.066,-0.008),(0.066,-0.0062),(-0.054,-0.0062),(-0.071,-0.0062)]
right_outline=[(-0.073,0.010),(-0.067,0.0147),(-0.050,0.015),(0.048,0.015),(0.060,0.012),
               (0.066,0.008),(0.066,0.0062),(-0.054,0.0062),(-0.071,0.0062)]
v4.extrude("left_yellow_scale", left_outline, 0.0065, loc=(0,0,0.0085), bevel=0.001, material=yellow, body=handle)
v4.extrude("right_yellow_scale", right_outline, 0.0065, loc=(0,0,0.0085), bevel=0.001, material=yellow, body=handle)
# Fixed steel guide: parallel tracks provide a visible, mechanically plausible captive path.
v4.box("channel_web", (0.106,0.0098,0.001), (-0.001,0,0.0085), bevel=0.00035, material=steel, body=handle)
for sy in (-1,1):
    v4.box("guide_rail_%s" % sy, (0.108,0.0011,0.0022), (-0.002,sy*0.0050,0.0100), bevel=0.00035, material=steel, body=handle)
# Nose collar is a squared, stepped metal throat around the blade exit.
v4.box("nose_collar", (0.014,0.024,0.010), (0.070,0,0.0065), bevel=0.002, material=steel, body=handle)
v4.box("nose_top_lip", (0.012,0.022,0.0012), (0.070,0,0.0122), bevel=0.0005, material=fastener, body=handle)
# Tail cap and lanyard eye are fixed to handle.
v4.box("tail_bumper", (0.006,0.020,0.010), (-0.073,0,0.0065), bevel=0.002, material=shell, body=handle)
v4.torus("lanyard_eye", 0.0042, 0.0010, loc=(-0.069,0,0.0065), rot=(math.pi/2,0,0), material=fastener, body=handle)
# Grip ribs along the palm side scales; narrow raised ribs, each attached to a shell face.
for side in (-1,1):
    y=side*0.0149
    for i in range(6):
        x=-0.043+i*0.013
        v4.box("grip_rib_%s_%02d"%(side,i), (0.0022,0.0007,0.0048), (x,y,0.0118), bevel=0.00035, material=shell, body=handle)
# Top shell fasteners: low profile heads and shallow drive slots.
for x in (-0.052,0.046):
    for y in (-0.0115,0.0115):
        v4.cylinder("shell_screw",0.0022,0.0008,(x,y,0.0151),segments=24,bevel=0.0002,material=fastener,body=handle)
        v4.box("screw_slot",(0.0023,0.00035,0.00025),(x,y,0.01558),bevel=0.00008,material=ink,body=handle)
# Fine maker text lies on the upper polymer scale.
v4.text("brand_mark","K-18  /  CUTTER",0.0046,loc=(-0.012,0.0105,0.0151),rot=(0,0,0),material=ink,body=handle,on="auto",height=0.00012)
v4.text("blade_size_mark","18 mm",0.0035,loc=(0.041,0.0102,0.0151),rot=(0,0,0),material=ink,body=handle,on="auto",height=0.00010)
# Belt clip under the handle (stylized spring steel, anchored at tail with formed free end).
v4.box("clip_anchor",(0.018,0.014,0.0012),(-0.052,0,0.0006),bevel=0.001,material=steel,body=handle)
v4.box("belt_clip_mid",(0.074,0.010,0.0012),(-0.013,0,-0.0002),bevel=0.001,material=steel,body=handle)
v4.box("belt_clip_tip",(0.019,0.010,0.0012),(0.033,0,-0.0008),rot=(0,math.radians(-12),0),bevel=0.001,material=steel,body=handle)

# Moving assembly: the shoe is held between the fixed rails; blade, lock tooth and thumb pad
# all belong to the same rigid body and translate with it.
carriage=v4.body("blade_carriage")
v4.box("carrier_shoe",(0.047,0.0083,0.0014),(-0.006,0,0.0119),bevel=0.0005,material=fastener,body=carriage)
v4.box("carrier_blade_clamp",(0.018,0.016,0.0014),(0.018,0,0.0126),bevel=0.0005,material=steel,body=carriage)
# Thumb slider with a low shoulder and rounded raised pad.
v4.box("slider_neck",(0.030,0.010,0.0032),(-0.006,0,0.0142),bevel=0.001,material=black,body=carriage)
v4.box("thumb_pad",(0.021,0.018,0.0042),(-0.006,0,0.0172),bevel=0.003,material=black,body=carriage)
for i in range(5):
    x=-0.014+i*0.004
    v4.box("thumb_ridge_%02d"%i,(0.0012,0.013,0.0007),(x,0,0.01945),bevel=0.0003,material=steel,body=carriage)
# Steel blade, 18 mm class snap-off format. Exits from the throat by 1 mm at rest.
blade_outline=[(0.027,-0.009),(0.063,-0.009),(0.074,-0.007),(0.077,0),(0.074,0.007),
               (0.063,0.009),(0.027,0.009),(0.022,0.007),(0.022,-0.007)]
v4.extrude("utility_blade",blade_outline,0.0010,loc=(0,0,0.0110),bevel=0.00025,material=blade_mat,body=carriage)
# Blade bevel / snap score marks, shallow dark etched transverse lines near the heel.
for i in range(3):
    x=0.037+i*0.007
    v4.box("blade_snap_score_%d"%i,(0.00035,0.016,0.00008),(x,0,0.01208),bevel=0.00004,material=fastener,body=carriage)
# Retaining tooth is a visible small steel tongue below the thumb pad.
v4.box("lock_tooth",(0.008,0.003,0.0012),(0.001,0,0.0108),bevel=0.00025,material=steel,body=carriage)

v4.joint("carriage_slide","prismatic",handle,carriage,origin=(-0.024,0,0.012),axis=(1,0,0),limits=(0,0.040))
v4.save(os.path.join(os.getcwd(),"asset.blend"))
print("saved asset.blend")
