from koko.lib.shapes import *
from math import sqrt

# One cad unit is 1cm
cad.mm_per_unit = 10

# laser kerf is orders of magnitude less than material thickness
# shopbot kerf is same order as material thickness, so joint design needs modification
# Some parameters are size related.  Some of the design is based on how the machinen behaves

# TODO
# Laser version
# 1) Slightly increase size of center pin for better fit
# 2) Add bigger chamfer to end of center pin
# 3) Make center pin base triangular for extra strength
# Shop bot version
# 1) **** Sort out dogs on slots
# 2) **** Increase material thickness
# 3) **** Make slots slightly taller, SB cuts around line so end of tang is currently an 'exact fit', needs some play
# 4) increase length of tang to improve

machine = 'laser'
#machine = 'shopbot'

if machine == 'laser':
    # setup for Epilog Laser and 3mm ply
    k = 0.17/cad.mm_per_unit    # Tool Kerf
    t = 3.0/cad.mm_per_unit     # Material thickness
    c = 0.2/cad.mm_per_unit     # Material Compliance. Estimate of the amount of non-elastic deformation that will occur once on the contact surfaces of the interferance fit
    sl = 50/cad.mm_per_unit      # Test part length
    sh = 20/cad.mm_per_unit      # Test part height
    tang_indent = 3
elif machine == 'shopbot':
    # setup for shopbot 10mm ply
    # machine setup on vs around vector is also different
    k = 6/cad.mm_per_unit    # Tool Kerf
    t = 12/cad.mm_per_unit #11.2/cad.mm_per_unit     # Material thickness
    c = 0.5/cad.mm_per_unit     # Material Compliance. Estimate of the amount of non-elastic deformation that will occur once on the contact surfaces of the interferance fit
    sl = 150/cad.mm_per_unit      # Test part length
    sh = 80/cad.mm_per_unit      # Test part height
    tang_indent = 4

# Joint size definitions
j_ph = t # Pin height. All pins / tangs are same height.
if j_ph < (3*k):
    j_ph = (3*k) # Minimum pin height has to allow for a tool cut down the center
j_cpl = t*2 # Center locating pin length beyond edge of material, should be slightly longer than tangs to help ease joint assembly
j_tl = t*1.8 # Tang length beyond edge of material
j_tl2 = j_cpl*tang_indent # Tang length back into material from edge
j_fsh = (j_ph/3)*1.05 #k # Flexure slot height in tang, add a bit to make sure tool fits if j_ph is only 3*k
j_fullh = j_ph*6 # Full joint height, should be sum of parts

# Joint part A - with the tangs
# Origin of joint is half way up join on the boundary edge of the material
# Made by adding a chunk of material and then cutting out tangs.
# Add rect make it overlap by j_cpl*0.1 with existing material to gaurentee no math error gaps.
jointa_add = rectangle(-(j_cpl*0.1), j_cpl, -j_fullh/2, j_fullh/2)
# Remove material all around upper half of center locating pin and upper tang
jointa_remove = (rectangle(-j_tl2,0, (j_ph*2.5)-(k*0.1),(j_ph*2.5)+(k*1)) # thin free movement cut back into material on upper edge of upper tang.  Make slightly bigger than kerf to gaurentee tool fits
                + rectangle(0,t-c, (j_ph*2.5)-(k*0.1),j_fullh/2)
                + triangle(t-c,j_ph*2.5, t-c,j_fullh/2, t+c,j_fullh/2) # preasure fit edge slope on upper tang (take off kerf to aid pressure fit). Need to be careful that tool still fits
                + triangle(j_tl*0.8,j_fullh/2, j_tl,j_fullh/2, j_tl,j_ph*2.0) # chamfer upper side of upper tang
                + rectangle(j_tl,j_cpl, j_ph/2,j_fullh/2) # remove material beyond end of tang
                #+ triangle(-(t*1.5),-(t*1.5), -(t*1.8),-(t*1.5), -(t*1.8),-(t*1.8)) # chamfer lower side of upper tang
                + rectangle(-j_tl2,j_cpl, j_ph*0.5,j_ph*1.5) # remove material between tang and center locating pin
                + triangle(j_cpl*0.9,j_ph*0.5, j_cpl,j_ph*0.5, j_cpl,j_ph*0.4) # chamfer upper side of center locating pin
                + rectangle(-j_tl2,j_tl*0.6, j_ph*2-(j_fsh/2), j_ph*2+(j_fsh/2)) # Flexure slot in middle of tang
                )
# Reflect the above around the line of symetry of the joint
jointa_remove = jointa_remove + reflect_y(jointa_remove, 0)


# Joint part B - the holes
# Must add dogs to the corners of these holes
jointb_remove = (
                rectangle(-t/2, t/2, j_ph*0.9, j_ph*2.7)
                + circle(-t/2, (j_ph*0.9)+k/2, k/2)
                + circle(-t/2, (j_ph*2.7)-k/2, k/2)
                + circle(t/2, (j_ph*0.9)+k/2, k/2)
                + circle(t/2, (j_ph*2.7)-k/2, k/2)

                + rectangle(-t/2, t/2, -(j_ph*0.5), j_ph*0.5)
                + circle(-t/2, -(j_ph*0.5)+k/2, k/2)
                + circle(-t/2, (j_ph*0.5)-k/2, k/2)
                + circle(t/2, -(j_ph*0.5)+k/2, k/2)
                + circle(t/2, (j_ph*0.5)-k/2, k/2)

                + rectangle(-t/2, t/2, -j_ph*2.7, -j_ph*0.9)
                + circle(-t/2, -(j_ph*0.9)-k/2, k/2)
                + circle(-t/2, -(j_ph*2.7)+k/2, k/2)
                + circle(t/2, -(j_ph*0.9)-k/2, k/2)
                + circle(t/2, -(j_ph*2.7)+k/2, k/2)

                #slot(0, j_ph*2.5, t, (j_ph*1.5)/2, 0) # upper tang hole
                #+ slot(0, j_ph*1, t, (j_ph*1.5)/2, 180)
                
                #+ slot(0, j_ph/2, t, j_ph/2, 0) # center locating pin hole
                #+ slot(0, -(j_ph/2), t, j_ph/2, 180)
                #+ slot(0, -(j_ph*1), t, (j_ph*1.5)/2, 0) # lower tang hole
                #+ slot(0, -(j_ph*2.5), t, (j_ph*1.5)/2, 180)
                )


joint_test = (rectangle( 0, sl, -sh, sh)
                + move(jointa_add, sl, 0, 0) 
                - move(jointa_remove, sl, 0, 0))
joint_test = joint_test- move(jointb_remove, sl/4, 0, 0)

cad.shape = joint_test
