A number of simple examples are available in SketchUp Extension Warehouse to help you get started programming with Ruby. The examples illustrate some of the techniques that you can use in a script.
You can find these scripts in the Example Ruby Scripts extension which includes the following:
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: