|
SketchUp Ruby API Deveoper'sGuide |
Developers Guide |
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.
The two most substantial addition to the SketchUp 6 Ruby API are the Ruby Observer Mechanism, the Tools class, the WebDialog class, the Styles class, and the Style class.
The Ruby Observer Mechanism is designed to allow Ruby scripts to be notified when objects change in the SketchUp application or model. For example, you can create an observer class that "listens" to when SketchUp quits and then performs some action.
Create a Ruby class of a specific observer type, such as AppObserver, override the desired methods, such as onQuit, and add an instance of the observer to the applicable objects in your Ruby script (using the add_observer method for that object). Refer to individual observer interfaces for further information.
The Tools class contains methods to manipulate a collection of SketchUp tools. This class is primarily used to switch between tools through the use of key or mouse actions.
The Ruby WebDialog class to create and interact with, DHTML dialog boxes, called webdialogs in this documentation, from Ruby code. For example, you can create webdialogs that are invoked from your Ruby code to display a web site, or to accept user input and use the results in your Ruby code.
The Styles class contains methods for manipulating a collection of styles in a model. The Style class contains methods for modifying information about a specific style.
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):
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 API Method Index
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 These files end in the word “Tests,” such as “EdgeTests.” All of the sample code is available in a .zip file 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:
These tests are not comprehensive in that they do not contain every permutation of every API.
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:
UI.beep
This command invokes the beep method of the UI module to play a warning beep.
UI.messagebox("Hello World")
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.
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.
"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).
|
SketchUp Ruby API Reference: What's New for SketchUp 6 |
© Google Inc. 2006 sketchup.google.com |