19.12.2016 Views

Architectural_Design_with_SketchUp

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 Creating Geometry Using Ruby Scripting<br />

}<br />

}<br />

}<br />

transformation = Geom::Transformation.new([i*s,j*s,k*s])<br />

ent.add_instance(comp.definition, transformation)<br />

As you can see, this code creates a grid of boxes similar to Figure 6.8. In contrast to<br />

what we did before, this time we created one box that has the appropriate geometry. Then<br />

we changed it into a component using the group.to_component method. It is only after this<br />

that we loop through the locations, define an insertion point using a transformation, and<br />

add a component instance (as comp.definition) using the add_instance method at that<br />

insertion point.<br />

Building a Curved Wall<br />

Let’s expand a bit on the previous code. In this snippet, we’ll take a preexisting component—<br />

a 1f × 1f × 2f block in the example used here—and place it multiple times while rotating it.<br />

The rotation is again guided by a combination of sine and cosine in the horizontal and vertical<br />

directions.<br />

To use this code, you must first have a component instance in your model (the block you<br />

see in Figure 6.17 is the one used here; place it anywhere you like). Before you run the code,<br />

make sure you select the component—otherwise, nothing happens.<br />

# Rotate-copy placement of a component<br />

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

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

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

s = 28 # x-spacing<br />

t = 12 # z-spacing<br />

h = 20 # number in x<br />

v = 20 # number in z<br />

max_deg = 90 # Max ”opening” angle<br />

if (sel[0].typename == ’ComponentInstance’)<br />

v.times { |j|<br />

h.times { |i|<br />

# Place a brick at the specified location<br />

t1 = Geom::Transformation.translation([i*s,0,j*t])<br />

instance = ent.add_instance(sel[0].definition, t1)<br />

# Calculate angle and rotate the brick vertically<br />

angle = (sin((i*180/h).degrees)+sin((j*360/v).degrees))*max_deg/2<br />

261

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

Saved successfully!

Ooh no, something went wrong!