The Ruby console is similar in functionality to Ruby's Interactive Ruby (irb) allowing you to experiment with Ruby commands and methods. This is a valuable tool for exploring the API and debugging your scripts. If you aspire to be a SketchUp plugins programmer, then getting familiar with the console is a good place to start.
In this tutorial we'll show you how to get to it and enter our first API commands.
The Ruby Console can beused to explore the API
one command at a time.
Start SketchUp or SketchUp Pro.
Open the Ruby Console (Window > Ruby Console).
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.
Type the following in the text input field:
UI.messagebox("Hello World")
Type the following in the text input field to draw a line in SketchUp (TYPE ONE LINE OF TEXT AT A TIME! Press the Enter or Return key after each line).
pt1 = [0, 0, 0]
pt2 = [10, 10, 0]
model = Sketchup.active_model
model.entities.add_line(pt1, pt2)
The first line defines a variable called pt1 (point one) and assigns it an array of three values [0,0,0]. This is the starting Point3d for the line that will be drawn.
The second line defines a variable called pt2 (point two) and assigns it an array of three values [10,10,0]. This is the ending Point3d for the line that will be drawn.
The third line defines a variable which receives a reference to the currently active Model.
The fourth line adds a Edge entity (from pt1 to pt2) to the currently active model. Go zoom in on the origin of your drawing and you should see our line:
Extra Credit:
Try adding more edges. Can you draw a square out of them? Can you draw a cube? HINT: Press the up arrow to "get back" to earlier commands.
So what about faces? Try typing in the following lines. Again, one line at a time:
model = Sketchup.active_model
model.entities.add_face([0,0,0], [9,0,0], [9,0,9], [0,0,9])
Try UI.openURL to take the user to the Trimble homepage. HINT: You need to pass this method a string URL, something like UI.openURL("http://www.trimble.com") Can you make it go to your favorite website?
Try typing arithmetic, like (5+5) * 100
Try assigning some variables. Type in each of these lines in order. Can you guess what the messagbox will show?
a = 4.0 b = 5.0 c = a * b UI.messagebox(c)