"""Articulated product model: hose-end concentrate sprayer."""
import math, os, sys
import bpy
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()
# Moulded plastics, bottle polymer and hardware
shell = v4.plastic('deep_forest_green_moulded_polypropylene', (0.075, 0.22, 0.12), 0.3, 0.12)
trim = v4.plastic('dark_charcoal_polypropylene', (0.055, 0.065, 0.058), 0.38, 0.08)
rubber = v4.rubber('soft_black_grip_elastomer', (0.025, 0.032, 0.028))
white = v4.plastic('warm_white_print', (0.92, 0.89, 0.76), 0.48, 0.02)
orange = v4.plastic('safety_orange_selector', (0.93, 0.29, 0.045), 0.3, 0.05)
steel = v4.metal('satin_nickel_hose_hardware', (0.64, 0.69, 0.70), 0.32, brushed=True)
bottle = v4.glass('clear_amber_tinted_bottle_polymer', (0.92, 0.78, 0.48), 0.11)
liquid = v4.plastic('amber_concentrate_fill', (0.78, 0.31, 0.055), 0.24, 0.01)

head = v4.body('head')
# Head shell: broad ergonomic transverse body with a slightly raised top deck.
v4.box('main_moulded_head_shell', (0.166, 0.080, 0.057), (0.012, 0, -0.004), bevel=0.014, material=shell, body=head)
v4.box('upper_selector_deck', (0.090, 0.073, 0.017), (-0.018, 0, 0.030), bevel=0.008, material=shell, body=head)
# Molded carry loop arches over the selector and gives the head a tool-like silhouette.
# Slim smoothly curved molded rear grip replaces the angular high carry loop.
grip_pts=[]
for i in range(25):
    t=i/24
    grip_pts.append((-0.085+0.035*t,0,0.035+0.032*math.sin(math.pi*t)))
v4.tube('integral_curved_rear_grip',grip_pts,0.0028,material=shell,body=head,resolution=12)
# Fine parting seam along both long flanks of the molded housing.
for sy in (-1,1):
    v4.tube('head_long_mould_seam_%s'%sy,[(-0.060,sy*0.039,0.010),(0.010,sy*0.040,0.010),(0.075,sy*0.039,0.010)],
            0.00065,material=trim,body=head,resolution=6)
# Rear hose inlet: axial swivel stem and ribbed plastic coupling, all fixed to the head.
v4.cylinder('hose_swivel_stem', 0.017, 0.068, (-0.102, 0, -0.003), rot=(0, math.pi/2, 0), material=steel, body=head)
v4.cylinder('ribbed_swivel_coupling',0.027,0.027,(-0.126,0,-0.003),rot=(0,math.pi/2,0),segments=16,
            bevel=0.0015,material=trim,body=head)
# Bored female swivel face with a recessed rubber washer and molded internal thread beads.
inlet_body=bpy.data.objects['ribbed_swivel_coupling']
inlet_cutter=v4.cylinder('temporary_inlet_bore_cutter',0.0182,0.040,(-0.126,0,-0.003),
                          rot=(0,math.pi/2,0),segments=48,bevel=0,material=None)
v4.boolean(inlet_body,inlet_cutter)
v4.torus('inlet_rubber_washer',0.0145,0.002,(-0.139,0,-0.003),rot=(0,math.pi/2,0),material=rubber,body=head)
for x in (-0.135,-0.130,-0.125):
    v4.torus('hose_internal_thread_%s'%str(x),0.0180,0.00065,(x,0,-0.003),
             rot=(0,math.pi/2,0),material=steel,body=head)
# Bottle receiving spigot under the head; pickup tube stays centered in jar cavity.
v4.lathe('wide_mouth_head_boss_and_siphon_pickup',
         [(0,-0.022),(0.018,-0.022),(0.019,-0.028),(0.019,-0.037),(0.018,-0.039),
          (0.016,-0.046),(0.015,-0.050),(0.015,-0.057),(0.014,-0.061),
          (0.0045,-0.062),(0.0032,-0.064),(0.0032,-0.222),(0,-0.222),(0,-0.022)],
         loc=(-0.045,0,0), material=trim, body=head)
# Nozzle carrier: fixed socket and shoulder.
v4.cylinder('nozzle_carrier_base', 0.027, 0.028, (0.102, 0, 0.002), rot=(0, math.pi/2, 0), material=trim, body=head)
v4.torus('nozzle_carrier_seam', 0.025, 0.0015, (0.115, 0, 0.002), rot=(0, math.pi/2, 0), material=shell, body=head)
# Mold seam and hand texture on underside rear of shell.
v4.torus('body_shell_parting_line', 0.001, 0.00045, (0.018, 0, -0.030), rot=(math.pi/2, 0, 0), material=trim, body=head)
for i in range(5):
    x = 0.000 + i*0.012
    v4.box('underside_grip_rib_%d'%i, (0.006, 0.060, 0.002), (x, 0, -0.033), bevel=0.0008, material=trim, body=head)
# Top dial scale, symbols and raised index marker fixed to shell.
for i, label in enumerate(('0', '1', '2', '3', '4', '5')):
    a = math.radians(205 + i*26)
    x, y = -0.018 + 0.034*math.cos(a), 0.034*math.sin(a)
    v4.text('dose_mark_%d'%i, label, 0.008, (x-0.0025, y-0.003, 0.039), material=white, body=head)
v4.box('dial_index_tick', (0.0025, 0.010, 0.001), (-0.018, -0.034, 0.040), bevel=0.0005, material=orange, body=head)
v4.text('MIX', 'MIX', 0.006, (0.027, -0.015, 0.039), material=white, body=head)
# Hose flow arrow on top rear shell

# Concentrate jar: hollow wall cross-section, so the fixed pickup tube passes through its cavity.
jar = v4.body('jar')
jar_profile = [
    (0.022, -0.236), (0.029, -0.232), (0.034, -0.223), (0.036, -0.211),
    (0.036, -0.091), (0.034, -0.080), (0.026, -0.070), (0.021, -0.066),
    (0.024, -0.061), (0.024, -0.052), (0.0225, -0.050), # broad mouth lip
    (0.0170, -0.050), (0.0160, -0.054), (0.0160, -0.065),
    (0.019, -0.074), (0.030, -0.086), (0.032, -0.095),
    (0.032, -0.211), (0.029, -0.221), (0.022, -0.228), (0.022, -0.236)
]
v4.lathe('hollow_translucent_concentrate_jar', jar_profile, loc=(-0.045, 0, 0), material=bottle, body=jar)
# Amber concentrate sits inside the hollow bottle, with an air gap above its visible fill line.
v4.lathe('visible_concentrate_fill_level',[(0.005,-0.225),(0.027,-0.225),(0.031,-0.218),(0.031,-0.132),(0.027,-0.127),(0.005,-0.127),(0.005,-0.225)],
         loc=(-0.045,0,0),material=liquid,body=jar)
# Locking collar and shallow molded neck threads.
v4.torus('jar_neck_locking_collar',0.0232,0.0015,(-0.045,0,-0.052),material=trim,body=jar)
for z in (-0.057,-0.062):
    v4.torus('jar_neck_thread_%s'%str(z),0.0235,0.00055,(-0.045,0,z),material=bottle,body=jar)
# Printed graduations sit on the clear wall; a single broad ring below the mouth protects the label area.
v4.torus('jar_shoulder_strengthening_bead',0.0358,0.0006,(-0.045,0,-0.092),material=bottle,body=jar)
for i in range(6):
    z = -0.112 - i*0.020
    v4.box('graduation_line_%d'%i, (0.014 if i%2 == 0 else 0.009, 0.0015, 0.0012), (-0.030, -0.033, z), bevel=0.0004, material=white, body=jar)
# Printed 7-segment capacity figures built as attached strokes (500, 300, 100 ml).
seven_seg={
    '0':('a','b','c','d','e','f'),'1':('b','c'),'2':('a','b','g','e','d'),
    '3':('a','b','g','c','d'),'4':('f','g','b','c'),'5':('a','f','g','c','d'),
    '6':('a','f','g','e','c','d'),'7':('a','b','c'),'8':('a','b','c','d','e','f','g'),'9':('a','b','c','d','f','g')}
def printed_digit(name, digit, x, z):
    w,h,t=0.0038,0.0060,0.00075
    segs={
        'a':((0,-h/2), (w,t)), 'g':((0,0),(w,t)), 'd':((0,h/2),(w,t)),
        'f':((-w/2, -h/4),(t,h/2)), 'b':((w/2,-h/4),(t,h/2)),
        'e':((-w/2,h/4),(t,h/2)), 'c':((w/2,h/4),(t,h/2))}
    for s in seven_seg[digit]:
        (dx,dz),(sx,sz)=segs[s]
        v4.box('%s_%s'%(name,s),(sx,0.0015,sz),(x+dx,-0.0345,z+dz),bevel=0.0002,material=white,body=jar)
for label,z in (('500',-0.117),('300',-0.177),('100',-0.217)):
    for i,d in enumerate(label):
        printed_digit('volume_%s_digit_%d'%(label,i),d,-0.052+i*0.005,z)
v4.text('jar_label_brand', 'GARDEN', 0.006, (-0.055, -0.0358, -0.151), rot=(math.pi/2, 0, 0), material=white, body=jar)
v4.text('jar_label_concentrate', 'FEED', 0.006, (-0.052, -0.036, -0.161), rot=(math.pi/2, 0, 0), material=white, body=jar)
# Bottle bottom is rounded; neck seats against spigot and seal.
v4.joint('jar_screw', 'continuous', head, jar, origin=(-0.045, 0, -0.050), axis=(0, 0, 1))

# Top mounted mixing ratio dial with scalloped grip edge and clear pointer.
dial = v4.body('mixing_dial')
v4.cylinder('dial_lower_hub', 0.021, 0.009, (-0.018, 0, 0.042), material=trim, body=dial)
v4.lathe('dose_selector_dial', [(0,0), (0.019,0), (0.023,0.003), (0.024,0.009), (0.022,0.014), (0.018,0.016), (0,0.016)],
         loc=(-0.018,0,0.048), material=orange, body=dial)
for i in range(24):
    a=2*math.pi*i/24
    v4.box('dial_ridge_%02d'%i, (0.003,0.005,0.009), (-0.018+0.023*math.cos(a), 0.023*math.sin(a), 0.058),
           rot=(0,0,a), bevel=0.001, material=orange, body=dial)
v4.box('dial_pointer', (0.0035,0.014,0.0015), (-0.018,-0.006,0.0645), bevel=0.0007, material=white, body=dial)
# Raised arrow head on the cap clarifies the selected ratio at a glance.
v4.extrude('dial_cap_pointer_arrow',[(0.0,-0.014),(0.004,-0.007),(-0.004,-0.007)],0.0015,
           loc=(-0.018,0,0.0648),bevel=0.0005,material=white,body=dial)
# Raised skirt index points toward the printed 0–5 scale on the deck.
v4.extrude('dial_skirt_alignment_pointer',[(-0.004,0.003),(0.004,0.003),(0,-0.005)],0.0012,
           loc=(-0.018,-0.023,0.056),rot=(math.pi/2,0,0),bevel=0.0004,material=white,body=dial)
v4.joint('dial_turn', 'continuous', head, dial, origin=(-0.018,0,0.048), axis=(0,0,1))

# Pivoting thumb trigger, broad paddle swept down and forward from a transverse hinge.
trigger = v4.body('trigger_lever')
pivot=(0.050, 0, -0.039)
# Side silhouette in X/Z, extruded across Y. Round edges make it a molded lever, not a flat plate.
outline=[(-0.008,0.004),(0.009,0.004),(0.014,-0.006),(0.014,-0.020),(0.021,-0.033),
         (0.025,-0.045),(0.022,-0.051),(0.016,-0.052),(0.010,-0.046),(0.002,-0.034),(-0.003,-0.018)]
v4.extrude('curved_thumb_trigger_paddle', outline, 0.014, loc=pivot, rot=(math.pi/2,0,0), bevel=0.004, material=shell, body=trigger)
# black thumb pad and raised transverse ribs on the outer (camera-visible) face
v4.extrude('trigger_soft_grip_pad', [(0.006,-0.010),(0.012,-0.022),(0.020,-0.036),(0.020,-0.041),(0.014,-0.041),(0.006,-0.030)],
           0.002, loc=(pivot[0],-0.008,pivot[2]), rot=(math.pi/2,0,0), bevel=0.002, material=rubber, body=trigger)
for k in range(4):
    v4.box('trigger_grip_rib_%d'%k,(0.013,0.002,0.0014),(0.065,-0.011,-0.057-k*0.005),
           rot=(0,0,0.18),bevel=0.0007,material=trim,body=trigger)
# Transverse hinge barrel mounted on the paddle's upper end; two visible end caps.
v4.cylinder('trigger_hinge_barrel', 0.006, 0.086, pivot, rot=(math.pi/2,0,0), material=trim, body=trigger)
for sy in (-1,1):
    v4.cylinder('trigger_pin_cap_%s'%sy, 0.008, 0.003, (pivot[0],sy*0.044,pivot[2]), rot=(math.pi/2,0,0), material=steel, body=trigger)
# Matching molded pivot bosses on both flanks of the fixed head.
for sy in (-1,1):
    v4.cylinder('head_trigger_pivot_boss_%s'%sy,0.011,0.006,(pivot[0],sy*0.043,pivot[2]),
                rot=(math.pi/2,0,0),material=shell,body=head)
# Molded webs tie the axle bosses into the underside shell wall.
for sy in (-1,1):
    v4.box('trigger_boss_support_web_%s'%sy,(0.020,0.010,0.016),(pivot[0]-0.020,sy*0.039,-0.034),
           bevel=0.003,material=shell,body=head)
# A compact torsion return spring wraps the hinge barrel on the trigger body.
spring_pts=[]
for i in range(97):
    u=i/96
    a=2*math.pi*3*u
    spring_pts.append((pivot[0]+0.0065*math.cos(a),-0.049+0.003*u,pivot[2]+0.0065*math.sin(a)))
v4.tube('trigger_hinge_torsion_spring',spring_pts,0.0010,material=steel,body=trigger,resolution=6)
v4.joint('trigger_hinge','revolute',head,trigger,origin=pivot,axis=(0,1,0),limits=(-0.12,0.48))

# Rotating nozzle selector ring and spray face.
nozzle=v4.body('nozzle')
v4.cylinder('nozzle_inner_spout',0.013,0.018,(0.125,0,0.002),rot=(0,math.pi/2,0),material=trim,body=nozzle)
v4.cylinder('nozzle_selector_ring',0.023,0.025,(0.145,0,0.002),rot=(0,math.pi/2,0),material=orange,body=nozzle)
v4.torus('nozzle_front_bezel',0.019,0.002,(0.159,0,0.002),rot=(0,math.pi/2,0),material=trim,body=nozzle)
v4.cylinder('nozzle_faceplate',0.018,0.003,(0.162,0,0.002),rot=(0,math.pi/2,0),material=steel,body=nozzle)
# Concentric outlet and spray holes on the face
v4.cylinder('central_spray_orifice',0.004,0.001,(0.164,0,0.002),rot=(0,math.pi/2,0),material=trim,body=nozzle)
for i in range(8):
    a=2*math.pi*i/8
    y,z=0.011*math.cos(a),0.002+0.011*math.sin(a)
    v4.cylinder('face_micro_orifice_%d'%i,0.0018,0.001,(0.164,y,z),rot=(0,math.pi/2,0),segments=16,bevel=0,material=trim,body=nozzle)
# Two pattern labels are printed around the outer rim of the spray-face holes.
v4.text('spray_face_jet_label','JET',0.0026,(0.163,-0.015,0.002),rot=(0,math.pi/2,0),material=white,body=nozzle)
v4.text('spray_face_fan_label','FAN',0.0026,(0.163,0.015,0.002),rot=(math.pi,math.pi/2,0),material=white,body=nozzle)
for i in range(18):
    a=2*math.pi*i/18
    v4.box('nozzle_knurl_%02d'%i,(0.012,0.002,0.004),(0.145,0.022*math.sin(a),0.002+0.022*math.cos(a)),rot=(a,0,0),bevel=0.0005,material=orange,body=nozzle)
# Pattern names are printed on the rotating bezel at its visible tangent faces.
v4.text('pattern_jet','JET',0.0038,(0.139,-0.0235,-0.006),rot=(math.pi/2,0,0),material=white,body=nozzle)
v4.text('pattern_fan','FAN',0.0038,(0.139,0.0235,-0.006),rot=(-math.pi/2,0,0),material=white,body=nozzle)
v4.text('pattern_shower','SHWR',0.0035,(0.139,-0.008,0.025),material=white,body=nozzle)
v4.joint('nozzle_turn','continuous',head,nozzle,origin=(0.116,0,0.002),axis=(1,0,0))

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