from koko.lib.shapes import *

#rectangle with chamfered corners
def chamfered_rectangle(x0,x1,y0,y1,c=.2):
    r = rectangle(x0,x1,y0,y1)
    c1 = triangle(x0,y0,x0,y0+c,x0+c,y0)
    c2 = triangle(x1,y1, x1, y1-c, x1-c, y1)
    c3 = triangle(x0,y1, x0+c, y1, x0, y1-c)
    c4 = triangle(x1,y0, x1-c, y0, x1, y0+c)
    return r-c1-c2-c3-c4

#rectangle with filletted corners
def filletted_rectangle(x0,x1,y0,y1,f=.2):
    r = rectangle(x0,x1,y0,y1)
    c1 = rectangle(x1-f,x1+f,y1-f,y1+f) - circle(x1-f,y1-f,f)
    c2 = rectangle(x0-f,x0+f,y1-f,y1+f) - circle(x0+f,y1-f,f)
    c3 = rectangle(x1-f,x1+f,y0-f,y0+f) - circle(x1-f,y0+f,f)
    c4 = rectangle(x0-f,x0+f,y0-f,y0+f) - circle(x0+f,y0+f,f)
    return r-c1-c2-c3-c4

def reflect_xy(thing,x=0,y=0):
    thing += reflect_x(thing,x)
    thing += reflect_y(thing,y)
    return thing

trace_width = .1
width = 2.
height = 2.
#stem = .5
pad_diam = .25
border_width = width/8

n_x = 3
n_y = 3

border_track = .01


eff_width = .5*width+border_width
eff_height = .5*height+border_width

cad.xmin = -eff_width +border_track
cad.xmax = (2*n_x-1)*eff_width +border_track
cad.ymin = -(2*eff_height+pad_diam/2+border_track)
cad.ymax = (2*n_y-1)*eff_height

pad = filletted_rectangle(-.5*width,.5*width,-.5*height,.5*height,.4)
trace = filletted_rectangle(-.5*trace_width,.5*trace_width,0,2*n_y*eff_height,.5*trace_width)
solder = circle(0,0,.5*pad_diam)
array=0
for i in range(n_x):
    for j in range(n_y):
        array += move(pad, 2*i*eff_width, 2*j*eff_height)
    array += move(trace,2*i*eff_width,-2*eff_height)
    array += move(solder,2*i*eff_width,-2*eff_height)
#pad += rectangle(-.5*trace_width,.5*trace_width,-.5*height-stem,-.5*height)
#pad += circle(0,-.5*height-stem,pad_diam/2)

#border = rectangle(-eff_width-.5*border_width, 3*eff_width+.5*border_width,-eff_height-stem-.5*pad_diam-.5*border_width,eff_height+.5*border_width) 
#border -= rectangle(-eff_width+.5*border_width, 3*eff_width-.5*border_width,-eff_height+.5*border_width-stem-.5*pad_diam,eff_height-.5*border_width)
#border += rectangle(eff_width-.5*border_width,eff_width+.5*border_width,-eff_height-stem-.5*pad_diam,eff_height)

#border = rectangle(-eff_width-.5*border_track, eff_width+.5*border_track,-eff_height-stem-.5*pad_diam-.5*border_track,eff_height+.5*border_track+stem+.5*pad_diam) 
#border -= rectangle(-eff_width+.5*border_track, eff_width-.5*border_track,-eff_height+.5*border_track-stem-.5*pad_diam,eff_height-.5*border_track+stem+.5*pad_diam)
#pad += border

#pad += move(pad,width+2*border_width,0)

cad.function = array
