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

NN<br />

NN<br />

NN<br />

NN<br />

A multiline comment starts <strong>with</strong> =begin and ends <strong>with</strong> =end.<br />

Parentheses are optional in method calls (to enclose the arguments), though it might be<br />

a good idea to add them for readability.<br />

You can often make things easier in repetitive Ruby code by using ranges. A range<br />

comes in a form like this: 1..5, which includes all integer numbers from 1 to 5. A<br />

good example is (1..5).to_a, which creates an array like this: [1, 2, 3, 4, 5].<br />

Other valid ranges are: 0.1..0.9 and ‘a’..’m’.<br />

There are two ways to create code blocks: One uses curly brackets, { at the beginning<br />

and } at the end; the other uses do at the beginning and end at the end. In many cases,<br />

you can use either one to enclose code (e.g., for an each statement).<br />

What Else Is There?<br />

There is more—much more. Particularly when you need to define your own classes or use<br />

some specialized Ruby classes and extensions, there is more to discover. But—as I’ve said<br />

before—this is a pragmatic introduction and we’ll limit language functions to only what is<br />

absolutely necessary to complete our tasks. There are several useful links at the end of this<br />

section that will help you delve deeper into Ruby. We will also cover file operations in some<br />

of the upcoming examples.<br />

What’s in <strong>SketchUp</strong>’s Ruby API?<br />

<strong>SketchUp</strong>’s Ruby API is an extension to plain Ruby (covered previously) and includes objects and<br />

methods that are useful in <strong>SketchUp</strong>. Some of these allow you to interact <strong>with</strong> the <strong>SketchUp</strong><br />

application or the currently open model. Others allow you to add or modify geometry (entities).<br />

There are also methods to attach observers to anything in the software. This is useful if<br />

you need your code to spring into action when the user changes something. Look through<br />

Appendix C to get an understanding of how extensive this API actually is.<br />

The full API documentation can be viewed at https://developers.google.com/sketchup, where you<br />

can also find numerous code examples and revision notes. The links in the next section give<br />

you even more resources for finding documentation, code samples, and discussion forums.<br />

You can use any of the <strong>SketchUp</strong> API’s objects and methods in your code whenever you<br />

like. As long as your Ruby code runs inside <strong>SketchUp</strong>, there is no need to “include” or define<br />

any external files. As demonstrated when you added a triangle to the model, one line is<br />

enough to get things started. Let’s quickly look at this code again and take it apart:<br />

Sketchup.active_model.entities.add_face [0,0,0],[0,10,0],[0,10,10]<br />

Here we are accessing the currently open model (the active_model) through the Sketchup<br />

object. If we want to add any geometry in there, we can’t simply add it to the model; we<br />

need to add it to the Entities collection in the model. This makes sense, of course, because<br />

everything in a model is somehow organized into collections (there are also layer, material,<br />

241

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

Saved successfully!

Ooh no, something went wrong!