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 />

axis (the green axis), and a third for the z axis (the blue axis). We will also use the pushpull<br />

method of the Face object to extrude the faces into boxes.<br />

Paste the following code into the Ruby Code Editor in <strong>SketchUp</strong> and click on Run Code:<br />

# Creates a 3D grid of similar boxes<br />

ent = Sketchup.active_model.entities<br />

n = 5 # number of boxes<br />

s = 100 # spacing<br />

w = 20 # width<br />

(0..n-1).each { |i|<br />

(0..n-1).each { |j|<br />

(0..n-1).each { |k|<br />

# add a face first<br />

face = ent.add_face [i*s,j*s,k*s],[i*s,j*s+w,k*s],[i*s+w,j*s+w,k*s],<br />

[i*s+w,j*s,k*s]<br />

# then pushpull it to get a box<br />

face.pushpull -w<br />

}<br />

}<br />

}<br />

The result will look like Figure 6.8.<br />

As this demonstrates, creating extruded geometry<br />

is very easy. First, we needed to create a face. This<br />

was done using the add_face method. As always, the<br />

face was simply added to the Entities collection.<br />

Instead of just adding it, though, we assigned<br />

it at the same time to the variable face. This gave<br />

us a way to apply the pushpull method to the face<br />

and extrude it by a given amount w.<br />

You might have noticed that w is entered as a negative<br />

number (there is a minus sign in front of it). This<br />

is because whenever you create a face in <strong>SketchUp</strong>,<br />

the white side of the face (the “front”) points downward.<br />

A positive w would have extruded it downward<br />

as well. Because we want to extrude it upward, w<br />

must be negative (or you would have needed to first<br />

flip the face’s orientation using its reverse! method).<br />

Of course, you have to be careful <strong>with</strong> this script.<br />

As you can imagine, changing a single number (n)<br />

significantly impacts the amount of geometry created.<br />

After all, each cube consists of 18 elements:<br />

6 faces and 12 edges. Figure 6.8: 125 boxes created quickly by a script<br />

247

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

Saved successfully!

Ooh no, something went wrong!