SketchUp Ruby Documentation

SketchUp contains a Ruby application programming interface (API) for users who are familiar with (or want to learn) Ruby scripting and want to extend the functionality of SketchUp. This interface allows users to create macros, such as automated component generators and additional tools, to be included in the menus within SketchUp. In addition to the API, SketchUp also includes a Ruby console, which is an environment where you can experiment with Ruby commands and methods.

## Loading Existing Scripts

The following steps are for SketchUp users who simply want to download and load existing Ruby scripts (such as those distributed by users on the SketchUp Ruby Forum):

1. Ensure that your script has the .rb extension. Any files with the extension .rb that are in the Plugins folder are automatically loaded when you start SketchUp.

2. Copy the Ruby script into the SketchUp Plugins folder. This folder is located under the SketchUp installation directory (Windows) or in the Contents\Resources\ directory within the SketchUp package (OS X).

3. Execute SketchUp. Most Ruby scripts are designed to create a new menu item somewhere within the SketchUp menus. The location of the menu item depends solely on how the Ruby script was written by the author (it can be in any of the existing menus or in a newly added menu). Refer to the Ruby script’s instructions for further details on how to run and use the script within SketchUp.

## SketchUp Ruby API

The SketchUp Ruby API consists of a series of SketchUp-specific Ruby modules, called classes in the object-oriented programming (OOP) world, and corresponding commands, called methods in the OOP world, for creating macros and manipulating geometry in SketchUp. Classes can be thought of as a mechanism for grouping related SketchUp ruby commands. Click below to reference the SketchUp Ruby API documentation using either an index of Classes or Methods.

SketchUp Ruby Class Index

SketchUp Ruby Method Index

## SketchUp Ruby API Sample Code

This API documentation contains sample code for each SketchUp Ruby method. There is one sample code file, also called a test file, for each SketchUp Ruby class file. All of the sample code is available on the SketchUp Web site (www.sketchup.com) under the Downloads tab.

Unzip the test files in the Plugins folder within the SketchUp installation directory. You can examine each test file using a text editor. You can also execute the tests to see how each API works within SketchUp. To execute these tests:

1. Execute SketchUp. There will be a new menu item, under the Plugins menu, for each of the script files. Each menu item represents a different SketchUp Ruby class. For example, ArcCurve APIs are within a menu item titled “ArcCurve Tests.”

2. Select the menu item representing the SketchUp Ruby class containing the API tests you want to execute. There are submenu items for each API in each class that you can click on and execute to test the API. For example, the ArcCurve Tests menu item contains tests for ArcCurve.center, ArcCurve.end_angle, and so on.

These tests are not comprehensive in that they do not contain every permutation of every API.

## Ruby Console

The Ruby console is similar in functionality to Ruby's Interactive Ruby (irb) allowing you to experiment with Ruby commands and methods, specifically most all methods in the in the standard Ruby API and Sketchup Ruby API. For example, you can type in simple Ruby syntax from the SketchUp API:

1. Open the Ruby Console (Window > Ruby Console)

2. Type the following in the text input field at the bottom of the console:

UI.beep

This command invokes the beep method of the UI module to play a warning beep.

3. Type the following in the text input field:

UI.messagebox("Hello World")

This command creates a simple message box with the string "Hello World" displayed.

4. Type the following in the text input field to draw a line in SketchUp (press the Enter Return key after each line).

pt1 = [0, 0, 0]

pt2 = [10, 10, 10]

model = Sketchup.active_model

model.entities.add_line(pt1, pt2)

· The first line defines a variable called p1 (point one) and assigns it an array of three values (0,0,0). This is the origin starting point for the line that will be drawn.

· The second line defines a variable called p2 (point two) and assigns it an array of three values (10,10,10). This is the end point for the line that will be drawn.

· The third line defines a variable called model which receives a reference to the currently active model.

· The fourth line adds a line entity (from pt1 to pt2) to the currently active model (This statement draws a line in the SketchUp drawing area).

These examples can be combined with other SketchUp Ruby APIs to create a SketchUp macro or utility. These files can then be included as a menu item within SketchUp for use within your models.

## Learning Ruby and the SketchUp Ruby API

There is an abundance of ways you can begin to learn Ruby scripting if you are not already familiar with the language. The following is a list of recommended ways to learn the Ruby language, including the new SketchUp Ruby API.

1. Read a book or tutorial on Ruby. There are many books and tutorials on Ruby available (some free and some for a fee). Free sources for Ruby instruction are:

· "Programming Ruby" online at http://www.ruby-doc.org/docs/ProgrammingRuby

· The "Ruby user's guide" online at http://www.ruby-doc.org/docs/UsersGuide/rg/

· "Introduction to Ruby" online at http://www.ruby-doc.org/docs/Tutorial/

· "Learn to Program - A Tutorial for the Future Programmer" on line at http://hellotree.gotdns.com/LearnToProgram/

"For fee" resources can be found in your local book store's computer section or by searching online at www.amazon.com using the keyword "Ruby." These books and tutorials will help you to become familiar with both Ruby syntax (the rules of the language) and the standard Ruby API (the tools you will use).

2. Take a Ruby course. Ruby courses are beginning to be offered by universities and organizations around the world.

3. Familiarize yourself with the SketchUp Ruby API by examining the SketchUp Ruby examples and trying Ruby commands within the Ruby console.

4. Create your own SketchUp Ruby script using any text editor and deploy it within SketchUp.

SketchUp Ruby Reference: Documentation | © 2005 @Last Software, Inc. www.sketchup.com

Contents: