#Johanna Kremser

import rhinoscriptsyntax as rs
import random as ran

#delete and start again
allobjs = rs.AllObjects()
rs.DeleteObjects(allobjs)
rs.EnableRedraw(False)

#variables
A = 5                       #A = Module size (distance between columns)
B = A/3                     #B = Distance of columns to end of plate
thick = 0.2                 #thickness of all slabs
hgt = 3.5                   #height of room
xcol = 3                    #columns in x directions
ycol = 5                    #cloumns in y directions
levels = 5                  #number of floor plates
f_height = 0.5              #f_height = foundation height
f_size = 0.8                #f_size = foundations edge size

grho = thick+f_height       #foundation + plate height
hb = (levels-1)*(thick+hgt) #height building
gh = thick+hgt              #height geschoss

#derived values:
center_pt = [A*(xcol-1)/2,A*(ycol-1)/2,f_height]       #insertion point of floor plate
p_width = A*(xcol-1)+2*B                               #width of floor plate (x)
p_lenght = A*(ycol-1) + f_size                         #lenght of floor plate (y)

def make_box(insertion=[0,0,0],xsize=10,ysize=10,zsize=10):
    #create a box
    corners = [[0,0,0], [xsize,0,0], [xsize,ysize,0], [0,ysize,0],
            [0,0,zsize], [xsize,0,zsize], [xsize,ysize,zsize], [0,ysize,zsize]]
    box=rs.AddBox(corners)
    rs.MoveObject(box, (-xsize/2,-ysize/2,0))
    rs.MoveObject(box, insertion)
    return(box)

#function to create a field of foundations
def make_foundations(A=5.0, f_size=0.8, f_height=0.5, xcol=2, ycol=3):
    fns = []        #foundations list
    for i in range(xcol):
        for j in range(ycol):
            fns.append(make_box([i*A,j*A,0], f_size, f_size, f_height))
    return(fns)

#function to create a field of columns
def make_columns(A=3.0, level=0.7, thick=0.2, hgt=3.0, xcol=2, ycol=3):
    cls = []        #columns list
    for i in range(xcol):
        for j in range(ycol):
            cls.append(make_box([i*A,j*A,level], thick, thick, hgt))
    return(cls)

#make a letter
def make_letter(insertion=(0,0,0), letter="1", xsize=10, ysize=10, zsize=10):
    mytext = rs.AddText(letter,(0,zsize,0),height=zsize, font="Century")
    crv = rs.ExplodeText(mytext,True)[0]
    path= rs.AddLine([0,0,0],[0,0,ysize])
    obj = rs.ExtrudeCurve(crv, path)
    rs.CapPlanarHoles(obj)
    rs.DeleteObjects([crv,path])
    rs.RotateObject(obj, [0,0,0], 90, [1,0,0])
    bbx = rs.BoundingBox(obj)
    #print bbx
    #rs.AddPolyline(bbx)
    rs.MoveObject(obj, (-bbx[0][0],-bbx[0][1],-bbx[0][2]))
    scalex = xsize/rs.Distance(bbx[0],bbx[1])
    scalez = zsize/rs.Distance(bbx[0],bbx[4])
    rs.ScaleObject(obj,(0,0,0),(scalex,1, scalez))
    rs.MoveObject(obj, (-xsize/2, -ysize/2,0))
    rs.MoveObject(obj, insertion)

#building dom-ino
f_list = []         #list of foundations
c_list = []         #list of columns
p_list = []         #list of plates
def build_my_domino (A=A, level= thick+f_height, thick=thick, hgt=hgt, xcol=xcol, ycol=ycol): 
    for i in range(levels):
        center_pt[2] = f_height + i*(thick+hgt)
        level = f_height + thick + (i-1)*(hgt+thick)
        if i==0:
            f_list = make_foundations(A, f_size, f_height, xcol, ycol)
        else:
            c_list.extend(make_columns(A, level, thick, hgt, xcol, ycol))
        p_list.append(make_box(center_pt, p_width, p_lenght, thick))

    rs.AddLayer("foundation")
    rs.LayerColor("foundation", (220,60,60))
    rs.ObjectLayer(f_list, "foundation")
    rs.AddLayer("columns", (60,220,60))
    rs.ObjectLayer(c_list, "columns")
    rs.AddLayer("plates")
    rs.LayerColor("plates", (60,60,220))
    rs.ObjectLayer(p_list, "plates")

build_my_domino()

##create a facade
#if rs.IsLayer("letter"):
#    rs.CurrentLayer("letter")
#else: 
#    rs.AddLayer("letter")
#    rs.CurrentLayer("letter")
#
#win = rs.ObjectsByLayer("facade")
#pts =[]
#
#for i in range(400):
#    x= ran.uniform(-1,p_width-1)
#    y =p_lenght
#    z=ran.uniform(f_height,hb)
#    pts.append([x,y,z])
#    rs.AddPoint([x,y,z])
#    for w in win:
#        if rs.PointInPlanarClosedCurve(pts[-1], w, plane):
#            pts.pop(-1)
#
#facadeletters = ['D','O','M','I','N','O','F','E','R','T','I','G']
#for p in pts:
#    rs.AddPoint(p)
#    make_letter(insertion=p,letter = ran.choice(facadeletters),xsize=0.5,ysize=0.5,zsize=0.5)
#
## finishing
#delobjs = rs.ObjectsByType(4) + rs.ObjectsByType(1)
#rs.DeleteObjects(delobjs)
#
#tourists =[]
#
#for i in range(300):
#    x= ran.randint(-8,15)
#    y= ran.randint(-8,15)
#    z= 0
#    ng = 10
#    dist = 14
#    if len(tourists) > ng:
#        for i in range(ng):
#         if rs.Distance([x,y,z], tourists[-1]) <dist: 
#            if (x <= -3 or x >= 7) and (y <= -2 or y >= 11):
#                tourists.append([x,y,z])
#                break
#    else:
#        if (x <= -3 or x >= 7) and (y <= -2 or y >= 11):
#            tourists.append([x,y,z])
#
#for p in tourists:
#    rs.AddPoint(p)
#    make_letter(p, "J", .35,.35,1.75)
#
#delobjs = rs.ObjectsByType(5)
#rs.DeleteObjects(delobjs)

#make stairs

rs.AddLayer("make_me_happy")
rs.LayerColor("make_me_happy", (0,0,0))
rs.CurrentLayer("make_me_happy")

sad = rs.ObjectsByLayer("make_me_happy")
pts=[]

#steps variables
steps=9                     #Stufenanzahl
steps_back=10               #Stufenanzahl back
#xsize_s=0.29                #Stufenbreite
ysize_s=1                   #Stufenlaenge
yoffs_s=0                   #verschiebung in y
stairs_up=levels-1                 #treppenteile
#zsize_s=0.17                #Stufenhoehe

zsize_s1 = (hgt/(steps+steps_back))
xsize_s1 = 0.63-2*zsize_s1

#plattforms variables
a_plf = levels                  #Plattformanzahl
a_zwipo = a_plf-1               #zwischenpodetanzahl
xsize_p=2                       #Breite
ysize_p=2                       #Laenge
zsize_p=0.2                     #Hoehe

x_move=xsize_p+xsize_s1*steps    #podest move x
zwipo=zsize_p+steps*zsize_s1     #zwischenpodest z

def make_steps(insertion=[0,0,0],xsize=xsize_s1,ysize=ysize_s,zsize=zsize_s1):
    corners = [[0,0,0], [xsize_s1,0,0], [xsize_s1,ysize_s,0], [0,ysize_s,0],
        [0,0,zsize_s1], [xsize_s1,0,zsize_s1], [xsize_s1,ysize_s,zsize_s1], [0,ysize_s,zsize_s1]]
    for i in range(steps):
        for j in range(stairs_up):
            for k in range(steps):
                if not (i-k):
                    box = rs.AddBox(corners)
                    rs.MoveObject(box, (i*xsize_s1,j*yoffs_s,k*zsize_s1))
                    rs.MoveObject(box, [xsize_p,0,zsize_p+j*gh])
                    rs.MoveObject(box, [0,-(ysize_p+f_size/2),f_height])
    for i in range(steps_back):
        for j in range(stairs_up):
            for k in range(steps_back):
                if not (i+k-steps):
                    box = rs.AddBox(corners)
                    rs.MoveObject(box, (i*xsize_s1-xsize_s1,j*yoffs_s,k*zsize_s1+zsize_p))
                    rs.MoveObject(box, [xsize_p,ysize_p/2,zwipo+j*gh])
                    rs.MoveObject(box, [0,-(ysize_p+f_size/2),f_height])
    #return(stairs)
make_steps()

#plattforms

def make_plattforms(insertion=[0,0,0],xsize=xsize_p,ysize=ysize_p,zsize=zsize_p):
    corners = [[0,0,0], [xsize,0,0], [xsize,ysize,0], [0,ysize,0],
        [0,0,zsize], [xsize,0,zsize], [xsize,ysize,zsize], [0,ysize,zsize]]
    for i in range(a_plf):
        box=rs.AddBox(corners)
        rs.MoveObject(box, [0,0,i*gh])
        rs.MoveObject(box, [0,-(ysize_p+f_size/2),f_height])
    for i in range (a_zwipo):
        box=rs.AddBox(corners)
        rs.MoveObject(box, [x_move,0,i*gh+zwipo])
        rs.MoveObject(box, [0,-(ysize_p+f_size/2),f_height])
    #return(plf)
make_plattforms()