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

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

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

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

# Get the one component’s definition<br />

comp = nil<br />

sel.each { |e|<br />

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

comp = e.definition<br />

end<br />

}<br />

# Iterate through all ungrouped faces<br />

sel.each { |e|<br />

if e.typename == “Face”<br />

# Place a copy at the center of the bounding box<br />

center = e.bounds.center<br />

t = Geom::Transformation.new (center, e.normal)<br />

new = ent.add_instance (comp, t)<br />

# Now scale objects by south orientation<br />

scale = 1-[(e.normal.angle_between [0,-1,0]),0.2].max<br />

new.transform! Geom::Transformation.scaling center, scale, scale,<br />

scale*2<br />

# Explode it so that we can remove the face afterwards<br />

new.explode<br />

}<br />

end<br />

As you can see in Figure 6.26, this code places scaled circles (due to unequal scaling,<br />

they actually become ellipses) onto each of the selected faces. If the face’s orientation points<br />

south, the circles become enlarged, yet the farther the face deviates from this direction, the<br />

smaller the circle becomes.<br />

In the preceding code, you can see that we kept the first part as before: We loop through<br />

the entities in the selection to find the one ComponentInstance. Next, we again iterate<br />

through all the faces and place copies of the circle at their centers (oriented <strong>with</strong> the face<br />

normal). Finally, we scale them based on orientation. To do this, we need to calculate the<br />

angle between the face normal and the south orientation. This is conveniently done using<br />

the angle_between method, which operates on the face normal and checks it against a unit<br />

vector in the south direction ([0,-1,0]). This yields the angle in radians, but since this number<br />

is between 0 and 1, it is perfect for us to use as a scale factor. To ensure that the circles don’t<br />

get too small, we’ll pick as a scale factor the maximum value of either the angle or 0.2 using<br />

272

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

Saved successfully!

Ooh no, something went wrong!