Saturday, September 5, 2009

Roof structure

Here is a script that generates some randomly placed roof structure...
No geometry is required

import py2rhino as p2r
import random as rnd
#===============================================================================
# Parameters
#===============================================================================
num_roof_pnts = (8,8)
num_columns = 10
roof_size = (100,100)
roof_height = 50
#===============================================================================
# Main Script
#===============================================================================
points = ( (roof_size[0]/-2, roof_size[1]/-2, roof_height), #x,y,z
(roof_size[0]/2, roof_size[1]/-2, roof_height),
(roof_size[0]/2, roof_size[1]/2, roof_height),
(roof_size[0]/-2, roof_size[1]/2, roof_height) )

roof = p2r.obj.NurbsSurface.create_by_corner_pnts(points)
roof.modf.rebuild((5,5), num_roof_pnts)
roof_pnts = roof.prop.pnts(True)

column_upper_pnts = []
column_lower_pnts = []

#create columns in random positions
for col_num in range(num_columns):

#create the lower and upper points
x_size = 2 * roof_size[0]/5
y_size = 2 * roof_size[1]/5
x_pos = rnd.randint(-1*x_size, x_size)
y_pos = rnd.randint(-1*y_size, y_size)
col_lower_pnt = ( x_pos, y_pos, 0 )
col_upper_pnt = (col_lower_pnt[0], col_lower_pnt[1], roof_height/2)

#save the points
column_lower_pnts.append( col_lower_pnt )
column_upper_pnts.append( col_upper_pnt )

#create the column
base = p2r.obj.Circle.create(col_lower_pnt, 2)
top = p2r.obj.Circle.create(col_upper_pnt, 1)
p2r.obj.NurbsSurface.create_by_loft( (base, top) )

for roof_pnt in roof_pnts:

#for each point on the roof, find the closest column
closest = p2r.util.point.points_closest_point(column_upper_pnts, roof_pnt)
closest_column_upper_pnt = column_upper_pnts[closest]

#get the planes that are at right angles to the centre line of the branch
center_ln = p2r.obj.Line.create(closest_column_upper_pnt, roof_pnt)
domain = center_ln.prop.domain()
base_plane = center_ln.eval.perp_frame(domain[0]) #((x,y,z), (p,q,r), (p,q,r), (p,q,r))
top_plane = center_ln.eval.perp_frame(domain[1])

#create the branch
base = p2r.obj.Circle.create(base_plane, 1)
top = p2r.obj.Circle.create(top_plane, 0.5)
p2r.obj.NurbsSurface.create_by_loft( (base, top) )

#delete the centre line - it is no longer needed
center_ln.modf.delete()

print "done"

No comments:

Post a Comment