# import Rhino Commands
import rhinoscriptsyntax as rs
import random as ran

# delete everything and start from scratch
allobjs = rs.AllObjects()
rs.DeleteObjects(allobjs)

##############################################################################
# domino variables
##############################################################################
A = 8                           # A = Module size (distance between columns)
B = A*1.5                       # B = Distance of columns to end of plate
thick = 0.4                     # thickness of all slabs
hgt = 5                         # height of room
xcol = 3                        # columns in x direction 
ycol = 5                        # columns in y direction
levels = 4                      # number of floors
f_height = 1                    # f_height = foundation height
f_size = 1                      # f_size = foundation edge size
##############################################################################
# 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_length = A*(ycol-1) + f_size              # length of floor plate (y)

# function to create a box
def make_box(insertion=[0,0,0],xsize=10,ysize=10,zsize=10):
    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 =[]
    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=5.0, level=0.7, thick=0.2, hgt=3.0, xcol=2, ycol=3):
    cls =[]
    for i in range(xcol):
        for j in range(ycol):
            cls.append(make_box([i*A,j*A,level], thick, thick, hgt))
    return(cls)

rs.EnableRedraw(False)

# building dom-ino
f_list=[]           # list of foundations
c_list=[]           # list of columns
p_list=[]           # list of plates
for i in range(levels+1):
    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_length, thick))


rs.AddLayer("foundation")
rs.LayerColor("foundation", (200,180,90))
rs.ObjectLayer(f_list,"foundation")
rs.AddLayer("columns")
rs.LayerColor("columns", (60,220,60))
rs.ObjectLayer(c_list,"columns")
rs.AddLayer("plates")
rs.LayerColor("plates", (60,60,220))
rs.ObjectLayer(p_list,"plates")

rs.EnableRedraw(True)