Exercise Four – Hierarchy, Composition and Transformation

 

 

 

 

 

This is Exercise Four: Hierarchy, Composition and Transformation. For this exercise, we were asked to create a record of the objects created by our program in the form of a data structure.

Here is the Python Code:


import rhinoscriptsyntax as rs
import random as r

lotSizeX = 60
lotSizeY = 100

roomSizeX = 6
roomSizeY = 10
roomSizeH = 3.5

offsetDist = 0.25
thickness = 0.1
Crv = []

def makeBlock (room, blockHeight):
    #find curves where the block will be created.
    origin = rs.CurveStartPoint (room)
    midPoint = [origin[0] + roomSizeX / 2, origin[1] + roomSizeY / 2, 0]
    plan = rs.OffsetCurve (room, midPoint, offsetDist)
    #extrude the exterior facades
    roof = [origin[0] + roomSizeX / 2, origin[1] + roomSizeY / 2, blockHeight]
    Crv.append (plan)
    facade = rs.ExtrudeCurveStraight (plan, midPoint, roof)
    #find and extrude the inner facades
    innerPlan = rs.OffsetCurve (plan, midPoint, thickness)
    innerFacade = rs.ExtrudeCurveStraight (innerPlan, midPoint, roof)
    #create surface between exterior and inner facades, and copy it to the top
    bottomFacade = rs.AddLoftSrf ([plan, innerPlan])
    topFacade = rs.CopyObject (bottomFacade, (0,0,blockHeight))
    #explode the exterior facades and find the one that will have a window
    facade2 = rs.ExplodePolysurfaces (facade, False)
    wall = facade2[1]
    #join the facades together to create solid wall
    thickWall = rs.JoinSurfaces ([facade, innerFacade, bottomFacade, topFacade], True)
    #find the center point of the surface, and create a sphere on it.
    point = rs.SurfaceAreaCentroid (wall)[0]
    sphere = rs.AddSphere (point, 0.5)
    #booleandifference between the wall and the sphere to create the window hole.
    openWall = rs.BooleanDifference (thickWall, sphere)
    rs.DeleteObjects (facade2)
    #create floor slab and copy it to the ceiling
    floor = rs.ExtrudeCurveStraight (plan, [0,0,0], [0,0,thickness])
    floorSlab = rs.CapPlanarHoles (floor)

    ceiling = rs.CopyObject (floor, (0, 0, blockHeight - thickness))
    #booleanunion all the solid walls together to create the block wanted.
    block = rs.BooleanUnion ([openWall, floor, ceiling])
    
    rs.DeleteObjects (plan)
    rs.DeleteObjects (innerPlan)

    return block

rs.DeleteObjects (rs.AllObjects ())

#we need to define the workplane before create a rectangle.
plane = rs.WorldXYPlane ()
C = rs.AddRectangle (plane, lotSizeX, lotSizeY)

nrRoomsX = lotSizeX // roomSizeX
nrRoomsY = lotSizeY // roomSizeY

#use three "for" loop to create blocks in three directions.
for y in range (0, nrRoomsX):

    for x in range (0, nrRoomsX):
        
        for z in range (0, nrRoomsX):
            #print (str(x) + "," + str(y) + "," + str(z))
            #we also need to move the workplane so that to crate a new rectangle at the new place.
            newPlane = rs.MovePlane (plane, [x*(roomSizeX+roomSizeY),y*roomSizeY,z*roomSizeH])
            newRoom = rs.AddRectangle (newPlane, roomSizeX, roomSizeY)
            Crv.append (newRoom)
            #make the block with the defined makeBlock function.
            block = makeBlock (newRoom, 3)
            
            #find the center point which will be used to rotate the blocks
            pl = rs.AddPlanarSrf (newRoom)
            pt = rs.SurfaceAreaCentroid (pl)[0]
            rs.DeleteObjects (pl)
            
            #use "if" to define which blocks are going to be rotated
            #use variable to create connection between functions!!
            if x == 0:
                rotation = -90
            if x == 1:
                rotation = 0
            if x == nrRoomsX-1:
                rotation = 90
            if r.random () < .25:
                rs.RotateObjects (block, pt, rotation, False)

            #use random to randomly delete 25% of the blocks.
            if r.random () < .25:
                rs.DeleteObject (block)

#delete the previouslly created curves.
rs.DeleteObjects (Crv)
rs.DeleteObjects (C)

Leave a Reply

Your email address will not be published. Required fields are marked *