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

v4.reset()
# industrial enamel, steel, elastomer and veneer
teal = v4.painted('deep_teal_enamel', color=(0.075,0.25,0.27), roughness=0.28)
navy = v4.painted('base_graphite', color=(0.105,0.12,0.13), roughness=0.38)
steel = v4.metal('satin_steel', color=(0.67,0.70,0.72), roughness=0.27)
bright = v4.metal('polished_edges', color=(0.82,0.84,0.84), roughness=0.2)
black = v4.rubber('roller_rubber', color=(0.075,0.08,0.08))
red = v4.plastic('safety_red', color=(0.8,0.035,0.018), roughness=0.26)
cream = v4.mat('warm_veneer', color=(0.72,0.53,0.29), roughness=0.55)
thread = v4.mat('dark_stitch_thread', color=(0.16,0.09,0.045), roughness=0.8)

frame = v4.body('frame')
# pedestal and industrial feet
v4.box('foot_plinth', (0.88,0.70,0.075), (0,0.05,0.045), bevel=.025, material=navy, body=frame)
for x in (-.34,.34):
  for y in (-.22,.31):
    v4.cylinder('leveling_foot', .044,.035,(x,y,.0175), material=black, body=frame)
    v4.cylinder('foot_bolt', .014,.012,(x,y,.002), material=steel, body=frame)
# pedestal cabinet with front access panel and vents
v4.box('pedestal_cabinet',(0.67,.48,.43),(0,.08,.29),bevel=.025,material=navy,body=frame)
v4.box('front_door',(0.43,.014,.28),(0,-.165,.32),bevel=.008,material=teal,body=frame)
v4.box('door_seam',(0.40,.003,.002),(0,-.172,.18),bevel=.0005,material=steel,body=frame)
for i in range(7):
  v4.box('cabinet_vent',(0.20,.004,.006),(0,-.175,.25+i*.018),bevel=.002,material=steel,body=frame)
v4.cylinder('door_latch',.012,.012,(.19,-.18,.33),rot=(math.pi/2,0,0),material=bright,body=frame)
# deep bed structure, ribs, work plate, front apron
v4.box('bed_casting_front',(0.91,.41,.12),(0,-.055,.65),bevel=.03,material=teal,body=frame)
v4.box('bed_casting_rear',(0.91,.13,.12),(0,.315,.65),bevel=.025,material=teal,body=frame)
v4.box('bed_worktop_front',(0.88,.32,.018),(0,-.01,.719),bevel=.012,material=steel,body=frame)
v4.box('bed_worktop_rear',(0.88,.11,.018),(0,.305,.719),bevel=.008,material=steel,body=frame)
# Four substantial webs bridge the cabinet to the cantilevered bed casting.
for x in (-.27,.27):
  for y in (-.08,.25):
    v4.box('bed_support_web',(.10,.095,.09),(x,y,.545),bevel=.012,material=navy,body=frame)
v4.box('front_apron',(0.78,.045,.075),(0,-.188,.645),bevel=.008,material=teal,body=frame)
for x in (-.32,-.16,0,.16,.32):
  v4.box('bed_rib',(0.018,.34,.05),(x,.13,.58),bevel=.004,material=navy,body=frame)
# needle throat plate and feed slot near front half
v4.box('throat_plate',(.20,.145,.004),(0,-.125,.730),bevel=.002,material=bright,body=frame)
# Fixed lower roller bearing brackets and split caps
for x in (-.30,.30):
  v4.box('lower_bearing_pedestal',(.072,.09,.115),(x,.20,.765),bevel=.009,material=teal,body=frame)
  v4.box('lower_bearing_cap',(.078,.10,.022),(x,.20,.823),bevel=.006,material=steel,body=frame)
  v4.cylinder('bearing_bolt',.009,.008,(x,.145,.826),rot=(math.pi/2,0,0),material=bright,body=frame)
# rear pillar and overarm: broad C-head cast in several joined volumes
v4.box('rear_pillar',(.22,.19,.59),(.34,.30,1.00),bevel=.055,material=teal,body=frame)
v4.box('head_arm',(.77,.20,.17),(-.055,.25,1.30),bevel=.055,material=teal,body=frame)
v4.box('arm_lower_lip',(.57,.13,.055),(-.12,.16,1.22),bevel=.018,material=teal,body=frame)
# head housing beneath arm
v4.box('needle_bar_housing',(.22,.19,.28),(-.18,.10,1.10),bevel=.04,material=teal,body=frame)
v4.cylinder('needle_bushing',.043,.045,(-.18,.095,.94),rot=(math.pi/2,0,0),material=steel,body=frame)
# head face plate and service cover
v4.box('faceplate',(.145,.012,.20),(-.18,-.001,1.105),bevel=.018,material=navy,body=frame)
# thread tension disc on the front face
v4.cylinder('thread_tension_disc',.018,.008,(-.105,-.011,1.16),rot=(math.pi/2,0,0),material=bright,body=frame)
# take-up mechanism details fixed pivot/link guide
v4.cylinder('takeup_pivot',.022,.012,(-.18,-.018,1.235),rot=(math.pi/2,0,0),material=bright,body=frame)
v4.rod('takeup_link',(-.18,-.026,1.235),(-.13,-.027,1.19),.006,material=steel,body=frame)
v4.torus('thread_guide_eye',.011,.002,loc=(-.13,-.032,1.19),rot=(math.pi/2,0,0),material=bright,body=frame)
# roller bracket, rear motor and belt guard
v4.box('roller_cross_bracket',(.76,.105,.055),(0,.30,.845),bevel=.012,material=teal,body=frame)
v4.box('motor_mount',(.30,.28,.045),(.31,.36,.76),bevel=.012,material=steel,body=frame)
v4.cylinder('drive_motor',.095,.24,(.31,.39,.88),rot=(math.pi/2,0,0),material=navy,body=frame)
for y in (.26,.28,.30,.32,.34,.36,.38,.40,.42,.44,.46,.48,.50):
  v4.box('motor_fin',(.19,.006,.012),(.31,y,.88),bevel=.003,material=steel,body=frame)
v4.cylinder('motor_endcap',.075,.012,(.31,.265,.88),rot=(math.pi/2,0,0),material=teal,body=frame)
# belt guard side plate with inset outline
v4.box('belt_guard',(.035,.34,.23),(.46,.245,.91),bevel=.035,material=navy,body=frame)
# handwheel shaft support and belt pulley on machine side
v4.rod('handwheel_drive_shaft',(.33,.25,1.29),(.50,.25,1.29),.015,material=steel,body=frame)
# control box on front-right of bed
v4.box('control_pod',(.15,.07,.12),(.34,-.20,.77),bevel=.018,material=navy,body=frame)
v4.cylinder('speed_dial',.026,.018,(.34,-.242,.79),rot=(math.pi/2,0,0),material=bright,body=frame)
v4.cylinder('emergency_stop',.023,.020,(.39,-.245,.74),rot=(math.pi/2,0,0),material=red,body=frame)
v4.box('brand_badge',(.13,.005,.034),(-.06,.155,1.365),bevel=.003,material=bright,body=frame)
# adjustable work light at arm end
v4.rod('lamp_stem',(-.34,.16,1.365),(-.38,.055,1.45),.009,material=steel,body=frame)
v4.cylinder('lamp_shade',.048,.075,(-.38,.035,1.45),rot=(math.pi/2,0,0),material=navy,body=frame)
v4.cylinder('lamp_lens',.035,.004,(-.38,-.005,1.45),rot=(math.pi/2,0,0),material=cream,body=frame)
# veneer sample sheet: stops at the nip so it does not intersect moving roller surfaces
v4.box('veneer_sheet',(.46,.55,.003),(0,-.135,.733),bevel=.001,material=cream,body=frame)
# seam / stitching indication on sample edge
for i in range(16):
  y=-.365+i*.027
  v4.rod('stitch_mark',(-.025,y,.735),(.025,y+.009,.735),.0014,material=thread,body=frame)
# Fixed thread spool and thread route mounted on arm
v4.cylinder('spool_pin',.006,.10,(-.32,.26,1.40),material=steel,body=frame)
v4.cylinder('thread_spool',.034,.05,(-.32,.26,1.44),material=cream,body=frame)
v4.cylinder('spool_flange_top',.043,.006,(-.32,.26,1.467),material=bright,body=frame)
v4.cylinder('spool_flange_bottom',.043,.006,(-.32,.26,1.413),material=bright,body=frame)
thread_pts=[(-.32,.26,1.47),(-.27,.16,1.46),(-.20,.01,1.32),(-.13,-.03,1.19),(-.18,-.03,1.02)]
for a,b in zip(thread_pts,thread_pts[1:]):
  v4.rod('thread_path',a,b,.0018,material=thread,body=frame)
# Moving needle bar slider
needlebar=v4.body('needle_bar')
v4.cylinder('needle_bar_rod',.010,.25,(-.18,.095,.91),material=bright,body=needlebar)
v4.box('needle_clamp',(.036,.034,.045),(-.18,.095,.795),bevel=.005,material=steel,body=needlebar)
v4.cylinder('needle_clamp_screw',.006,.018,(-.18,.074,.795),rot=(math.pi/2,0,0),material=bright,body=needlebar)
v4.cylinder('needle',.0022,.095,(-.18,.095,.725),material=bright,body=needlebar)
v4.joint('needle_slide','prismatic',frame,needlebar,origin=(-.18,.095,.91),axis=(0,0,1),limits=(-.045,.025))
# Continuously turning handwheel mounted on exposed shaft (axis X)
wheel=v4.body('handwheel')
v4.cylinder('handwheel_hub',.045,.11,(.535,.25,1.29),rot=(0,math.pi/2,0),material=steel,body=wheel)
v4.torus('handwheel_rim',.135,.009,loc=(.535,.25,1.29),rot=(0,math.pi/2,0),material=bright,body=wheel)
for a in range(8):
  q=2*math.pi*a/8
  v4.rod('wheel_spoke',(.535,.25,1.29),(.535,.25+.126*math.cos(q),1.29+.126*math.sin(q)),.006,material=steel,body=wheel)
v4.cylinder('wheel_cap',.052,.012,(.595,.25,1.29),rot=(0,math.pi/2,0),material=navy,body=wheel)
v4.joint('handwheel_axle','continuous',frame,wheel,origin=(.49,.25,1.29),axis=(1,0,0))
# lower driven knurled feed roller and its shaft
lower=v4.body('lower_feed_roller')
v4.cylinder('lower_roller_core',.031,.64,(0,.20,.705),rot=(0,math.pi/2,0),material=steel,body=lower)
v4.cylinder('lower_grip_surface',.036,.52,(0,.20,.705),rot=(0,math.pi/2,0),material=steel,body=lower)
for i in range(31):
  x=-.245+i*.0163
  v4.torus('roller_knurl_ring',.035,.0015,loc=(x,.20,.705),rot=(0,math.pi/2,0),segments=32,material=bright,body=lower)
for x in (-.31,.31):
  v4.cylinder('roller_end_collar',.027,.025,(x,.20,.705),rot=(0,math.pi/2,0),material=navy,body=lower)
v4.joint('lower_feed_axle','continuous',frame,lower,origin=(0,.20,.705),axis=(1,0,0))
# pivoting bifurcated pressure lever/yoke; two arms join the rear transverse crossbar
lever=v4.body('pressure_lever')
for x in (-.30,.30):
  v4.cylinder('lever_pivot_hub',.041,.026,(x,.34,.87),rot=(0,math.pi/2,0),material=bright,body=lever)
  v4.rod('yoke_arm',(x,.34,.87),(x,.205,.795),.018,material=teal,body=lever)
v4.rod('yoke_crossbar',(-.30,.205,.795),(.30,.205,.795),.018,material=teal,body=lever)
# operator handle extends forward from right end with a turned grip
v4.rod('pressure_handle_stem',(.30,.34,.87),(.37,.05,.98),.012,material=steel,body=lever)
v4.cylinder('pressure_grip',.018,.105,(.37,-.002,.98),rot=(math.pi/2,0,0),material=black,body=lever)
for sx in (-1,1):
  v4.cylinder('lever_pivot_bolt',.013,.008,(sx*.318,.34,.87),rot=(0,math.pi/2,0),material=navy,body=lever)
v4.joint('pressure_lever_hinge','revolute',frame,lever,origin=(0,.34,.87),axis=(1,0,0),limits=(-.10,.10))
# upper freely turning contact roller carried by yoke
upper=v4.body('upper_pressure_roller')
v4.cylinder('upper_roller_shaft',.014,.66,(0,.20,.795),rot=(0,math.pi/2,0),material=steel,body=upper)
v4.cylinder('upper_rubber_sleeve',.037,.52,(0,.20,.795),rot=(0,math.pi/2,0),material=black,body=upper)
for x in (-.31,.31):
  v4.cylinder('upper_roller_collar',.027,.022,(x,.20,.795),rot=(0,math.pi/2,0),material=bright,body=upper)
v4.joint('upper_feed_axle','continuous',lever,upper,origin=(0,.20,.795),axis=(1,0,0))
# fine control marks and hardware on work surface
for x in (-.34,.34):
  for i in range(5):
    v4.box('bed_scale_tick',(.002,.012,.001),(x,-.10+i*.035,.729),bevel=0,material=navy,body=frame)

# Bake bevel/normal modifiers and weld coincident vertices. The checker sees
# evaluated triangles and otherwise treats bevel-generated seams as islands.
dg=bpy.context.evaluated_depsgraph_get()
for obj in list(v4.asset_objects()):
  if obj.type != 'MESH':
    continue
  ev=obj.evaluated_get(dg)
  me=ev.to_mesh()
  baked=me.copy()
  baked.name=obj.name+'_baked'
  ev.to_mesh_clear()
  obj.data=baked
  for mod in list(obj.modifiers):
    obj.modifiers.remove(mod)
  bm=bmesh.new()
  bm.from_mesh(obj.data)
  bmesh.ops.remove_doubles(bm,verts=list(bm.verts),dist=1e-5)
  bm.to_mesh(obj.data)
  bm.free()

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