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

.interpolate .inverse .invert!<br />

.new .origin .rotation .scaling<br />

.set! .to_a .translation<br />

.xaxis .yaxis .zaxis<br />

Although this (and the online documentation) might appear a bit confusing, let’s look at<br />

just a subset of the available functionality. This provides enough functionality for what we<br />

want to do. The relevant methods are these:<br />

# Point Transformation—use for placing objects<br />

t1 = Geom::Transformation.new( point )<br />

# Translation Transformation—use for moving<br />

t2 = Geom::Transformation.translation( vector )<br />

# Rotation Transformation—use for rotating<br />

t3 = Geom::Transformation.rotation( center, axis, angle )<br />

# Scaling Transformation—use for scaling<br />

t4 = Geom::Transformation.scaling( center, scale )<br />

t4 = Geom::Transformation.scaling( center, xscale, yscale, zscale )<br />

As you can see, they all use perfectly understandable parameters. If you want to move<br />

something from location A to location B, then the vector between those points (expressed<br />

as “go m units in the x direction, n units in the y direction, and o units in the z direction”) is<br />

enough information to get it there. And if you want to rotate something, <strong>SketchUp</strong> needs to<br />

know about which point you want it to rotate, about which axis you want it to rotate, and<br />

by how many degrees it should turn. Finally, the scaling translation accepts either a uniform<br />

scale or different scale factors in each coordinate direction.<br />

In order to use these transformations, you need to add them as parameters to appropriate<br />

methods. There are various approaches to this: Use transform_entities on the model’s<br />

Entities collection as a universal approach. You can also directly apply a transformation to<br />

groups or components, especially when you place an instance of a component (the transformation<br />

describes the insertion point in that case).<br />

Let’s look at this in action in a short code snippet. Select an object in your model (if you<br />

select multiple, only the first gets used). Then paste the following code into the Ruby Code<br />

Editor and click Run Code:<br />

# Test transformations on selected object<br />

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

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

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

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

center = sel[0].bounds.center<br />

259

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

Saved successfully!

Ooh no, something went wrong!