19.12.2016 Views

Architectural_Design_with_SketchUp

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6 Creating Geometry Using Ruby Scripting<br />

Feel free to experiment <strong>with</strong> different components using this code. You can even place<br />

ones that have thickness (e.g., a cylinder) using this method.<br />

Randomizing Everything<br />

In the following code, we revisit the use of random numbers and combine this <strong>with</strong> what we<br />

learned about transformations. Specifically, we take the selected objects (all must be component<br />

instances) and apply a random rotation as well as random scaling to each. Before<br />

you run this code, add some components to your model—trees or other shrubbery make for<br />

great examples. Just make sure you are not using face-me components.<br />

# Apply randomized rotation and scaling to selection<br />

mod = Sketchup.active_model # Open model<br />

ent = mod.entities # All entities in model<br />

sel = mod.selection # Current selection<br />

max_rotation_angle = 90<br />

size_var = 0.5 # Keep below 1<br />

sel.each { |e|<br />

if e.typename == ”ComponentInstance”<br />

# Get the center of the selected object<br />

center = e.bounds.center<br />

# Also get the base center for scaling<br />

base = center.clone<br />

z_height = (e.bounds.max.z—e.bounds.min.z)<br />

base.z = base.z—z_height/2<br />

# Transform this object<br />

t1 = Geom::Transformation.rotation(center, [0,0,1],<br />

(rand * max_rotation_angle).degrees)<br />

t2 = Geom::Transformation.scaling base,<br />

1—size_var/2 + rand*size_var<br />

# Combine transformations<br />

t = t1 * t2<br />

e.transform! t<br />

end<br />

}<br />

265

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!