SketchUp ships with a number of simple examples to help you get started programming with Ruby. The examples illustrate some of the techniques that you can use in a script.
When SketchUp starts up, it will automatically load any scripts that it finds in the plugins folder. Ruby scripts have the extension .rb. Additional example scripts are located in a sub-folder of the plugins folder called Examples.
The location of the plugins folder varies depending on whether you are running on OS X or Windows.
On OS X,the plugins are installed in the plugins directory within /Library/Application Support/Google SketchUp [X]/SketchUp. Note that the plugins directory does not start with an uppercase P here. In particular, the plugin scripts that ship with SketchUp are installed in:
/Library/Application Support/Google SketchUp [X]/SketchUp/plugins
Note that the plugins directory does not start with an uppercase P here.
The examples are in:
/Library/Application Support/Google SketchUp [X]/SketchUp/plugins/Examples
On Windows, the plugins are installed into the same folder where you install SketchUp. This location may vary depending on where you installed SketchUp. The default location is:
C:\Program Files\Google\Google SketchUp [X]\Plugins
The examples are in:
C:\Program Files\Google\Google SketchUp [X]\Plugins\Examples
The following Ruby scripts are located in the Plugins directory and are loaded by default.
Loads exampleScripts.rb, bringing in all of the examples in that file.
The following Ruby scripts are located in the Examples directory within the Plugins directory. Go to Preferences > Extensions within SketchUp and check the Ruby Script Examples checkbox to see how these scripts work. SketchUp will automatically load these scripts when you check this checkbox. Checking this checkbox also adds the following items to the menus: Draw > Box, Plugins > Cost, and Camera > Animations. Note: Do not change or move these scripts as you will break the functionality of the previously mentioned menu items.
This example contains three methods: setLayer, totalArea, and perimeter. The setLayer method sets the layer of all selected entities to a layer with a given name (you must provide the layer name). The totalArea method computes the total area for all faces in a model. The perimeter method computes the perimeter of the selected faces. Methods in this file can be loaded by typing the function name in the Ruby console. For example:
This example shows how you can create animations in Ruby. The animation.rb example creates a simple animation that spins the camera around.
This example adds a menu item called Animations at the end of the Camera menu. The Animations menu contains two options: spin view and stop spinning. Select spin view to rotate the camera continuously around your model. Select stop spinning to stop the spin view.
This example shows how to attach arbitrary application specific attribute data to SketchUp entities. The attributes.rb example also illustrates the use of input boxes to get data from the user and how to query data on the model using cost data information to compute a simple materials cost estimate.
This example adds a Cost menu item to the Plugins menu. The Cost menu contains three items: Assign Estimate to Material, Assign Estimate to Faces, and Computer Estimate. Select Assign Estimate to Material to assign a per/foot cost to all items with a specific material. Select Assign Estimate to Faces to assign a per/foot cost to all selected faces. Select Compute Estimate to get a dollar amount for the currently calculated estimate.
This example shows how to create simple geometry using Ruby. Additionally, the box.rb example shows how to create a dialog box to prompt for user input and how to add an item to a menu.
This example will add a menu item called Box at the end of the Draw menu when loaded. This menu item displays a dialog where you input the size of the box you want to create and then, after clicking OK, creates the box.
This example shows how you can add new choices to context menus. The contextmenu.rbexample adds a Point at Centeritem to the context menu for arcs and circles to create a point at the center of the arc or circle. To use this feature, create an arc or circle and context click on the arc or circle to use Point at Center (bottom-most option).
This example shows how you can create tools that respond to mouse events in Ruby. The linetool.rb example defines a simple tool that behaves similar to the pencil tool in SketchUp except that it creates finite length construction lines instead of regular SketchUp edges.
The following command will load the linetool.rb example:
This example contains a number of examples of how to traverse a model and select geometry. Methods in this file can be loaded by typing the function name in the Ruby console. For example: