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.

<strong>Architectural</strong> <strong>Design</strong> <strong>with</strong> <strong>SketchUp</strong><br />

This gets the job done, but it is not very efficient. We are creating geometry for each of<br />

the many similar boxes. Ultimately, all these faces, edges, and vertices need to be stored in<br />

the <strong>SketchUp</strong> file that you save to your hard disk. The next section shows you how to improve<br />

on this using components and transformations.<br />

Lots of Boxes <strong>with</strong> Color<br />

Let’s create a slight variation of the preceding code snippet. This time, we’ll put each of the<br />

boxes into a group, and we’ll add color to them all!<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 colored boxes<br />

ent = Sketchup.active_model.entities<br />

n = 5 # number of boxes<br />

s = 100 # spacing<br />

w = 50 # box width<br />

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

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

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

# create a group for each box<br />

group = ent.add_group<br />

# add the face to the group’s entities<br />

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

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

# add a material (an RGB color)<br />

face.back_material = [(255/n*i).round,(255/n*j).round,<br />

(255/n*k).round]<br />

# now extrude the box<br />

face.pushpull -w<br />

}<br />

}<br />

}<br />

This should at least significantly improve the visuals of the previous example. Figure 6.9<br />

shows what you will see.<br />

What did we do differently this time? We didn’t just add the faces to the Entities collection<br />

as before; this time, we first added a group to that collection using the add_group<br />

method. Because we assigned a variable to the group (group), we were able to add geometry<br />

to the Entities collection of that group. That is all you need to create grouped geometry<br />

using Ruby.<br />

The other trick we used was to add a material to the face before we extruded it. This<br />

added the material to the entire box by way of extrusion.<br />

248

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

Saved successfully!

Ooh no, something went wrong!