The UI module contains a number of methods for creating simple UI elements from a SketchUp Ruby script.
The add_context_menu_handler method is used to register a block of code with
SketchUp that will be called when a context menu is to be displayed. The
context menu handler can then display the context menu with the items that
you have added.
See the contextmenu.rb script in the Plugins/examples directory for an
example.
Arguments:
Returns:
# Right click on anything to see a Hello World item.
UI.add_context_menu_handler do |context_menu|
context_menu.add_separator
context_menu.add_item("Hello World") {
UI.messagebox("Hello world")
}
end
The beep method plays a system beep sound.
The beep method does not accept any arguments nor return any values.
Returns:
UI.beep
The create_cursor method is used to create a cursor from an image file at the specified location. This must be called from within a custom Tool. See the Tool class for a complete example.
Arguments:
Returns:
cursor_path = Sketchup.find_support_file("Pointer.png", "Plugins/")
if cursor_path
id = UI.create_cursor(cursor_path, 0, 0)
UI.set_cursor(id)
else
id=0
end
Creates a dialog box for inputting user information. The dialog box contains
input fields with static text prompts, optional default values, optional
drop down selections, and optional title.
You can also use this method to display drop down lists of options, by
passing an optional param.
Arguments:
Returns:
# With three params, it shows all text boxes: prompts = ["What is your Name?", "What is your Age?", "Gender"] defaults = ["Enter name", "", "Male"] input = UI.inputbox prompts, defaults, "Tell me about yourself." # With four params, it shows a drop down box for prompts that have # pipe-delimited lists of options. In this case, the Gender prompt # is a drop down instead of a text box. prompts = ["What is your Name?", "What is your Age?", "Gender"] defaults = ["Enter name", "", "Male"] list = ["", "", "Male|Female"] input = UI.inputbox prompts, defaults, list, "Tell me about yourself."
The inspector_names method is used to returns the names of all the inspectors. Inspectors are another name for the various floating dialog windows that you can activate from withing SketchUp, such as the Materials window.
Returns:
inspectors = UI.inspector_names
The menu method retrieves a SketchUp's menu object with a given name. This
is the first step toward adding your own custom items to the bottom
of SketchUp's menus.
Valid menu names are: "File", "Edit", "View", "Camera", "Create", "Tools",
"Page", "Window", "Plugins" and "Help".
Arguments:
Returns:
tool_menu = UI.menu "Tools"
tool_menu.add_item("Cheese Tool") {
UI.messagebox("Cheese activated.")
}
Creates a dialog box containing static text with a series of buttons for
the user to choose from.
The default message box type is MB_OK and the default title for the message
box is "Validity Check." Valid message box types are:
Arguments:
Returns:
result = UI.messagebox "Do you like cheese?", MB_YESNO
if result == 6 # Yes
UI.messagebox("Sketchup likes cheese, too.")
endThe model_info_pages method is used to returns the names of all the available model info pages. These include UI windows such as Components, Credits, and Units.
Returns:
mipages = UI.model_info_pages
The openURL method is used to open the default Web browser to a URL.
Arguments:
Returns:
status = UI.openURL "http://www.google.com"
The openpanel method is used to display the Open dialog box. The path that
is returned can then be used inside code to open a text or image file.
See the standard Ruby class File for examples of reading and writing from
disk.
SU7 Bug: The wildcards are not working properly if more than one is
entered (e.g. "*.jpg;*.png") or if a directory is included. The '*' gets
converted to a '_'.
Arguments:
Returns:
chosen_image = UI.openpanel "Open Image File", "c:\\", "*.jpg"
The play_sound method is used to play a sound file. Valid sound files include .wav and .mp3 files on the Mac and .wav files on the PC.
Arguments:
Returns:
UI.play_sound "Plugins/mediadiscussion.wav"
The preferences_pages method is used to returns the names of all the preferences pages. These include windows like Extensions.
Returns:
prefs = UI.preferences_pages
Tells SketchUp to refresh all inspectors such as the Component Browser and the Outliner. This is useful when you need to manually force a refresh after you've made a change to the document via Ruby. Generally, SketchUp will keep these in sync for you, but occasionally it does not, such as when model.start_operation has disabled UI updates.
Returns:
UI.refresh_inspectors
The savepanel method is used to display the Save dialog box. The path that is returned can then be used inside code to save out a text or image file. See the standard Ruby class File for examples of reading and writing from disk.
Arguments:
Returns:
path_to_save_to = UI.savepanel "Save Image File", "c:\\", "Shapes.jpg"
The set_cursor method is used to change the cursor to a new cursor with a
given cursor id. See UI.create_cursor and the Tool class for details
on creating your own tools with arbitrary cursors.
If you call this while a standard SketchUp tool is active, you will not
see your custom cursor, as these tools are constantly setting their
own cursors to indicate SketchUp's state.
Arguments:
Returns:
def onSetCursor UI.set_cursor(@@my_custom_cursor_id) end
The set_toolbar_visible method is used to set whether a given toolbar is visible.
Arguments:
Returns:
status = UI.set_toolbar_visible("Camera", true)The show_inspector method is used to display the inspector with the given name. You can get the list of valid inspectors with UI.inspector_names.
Arguments:
Returns:
status = UI.show_inspector "Components"
The show_model_info method is used to display the model info dialog for a specific page. You can get the list of valid dialogs with UI.model_info_pages.
Arguments:
Returns:
status = UI.show_model_info "Credits"
The show_preferences method is used to display a SketchUp preferences dialog. You can get the list of valid dialogs with UI.preferences_pages.
Arguments:
Returns:
status = UI.show_preferences "GraphicsCard"
The start_timer method is used to start a timer. This is an effective method
to create a repeating snippet of code for arbitrary animation.
See this blog post for an detailed example of custom animation using timers:
http://sketchupapi.blogspot.com/2008/10/animate-yo-cheese.html
Arguments:
Returns:
# Beep after 10 seconds.
id = UI.start_timer(10, false) { UI.beep }
if (id)
UI.messagebox id
else
UI.messagebox "Failure"
endThe stop_timer method is used to stop a timer based on its id.
Arguments:
Returns:
# Beep after 10 seconds.
id = UI.start_timer(10) { UI.beep }
if (id)
UI.messagebox id
else
UI.messagebox "Failure"
end
UI.stop_timer id
UI.messagebox "Stopped"The toolbar method is used to get a Ruby toolbar by name.
Arguments:
Returns:
toolbar = UI::Toolbar.new "Test" toolbar = UI.toolbar "Test" if (toolbar) UI.messagebox toolbar else UI.messagebox "Failure" end
The toolbar_names method is used to returns the name of all the available toolbars (this differs between PC and Mac).
Returns:
names = UI.toolbar_names
The toolbar_visible? method is used to determine whether a given toolbar is visible.
Arguments:
Returns:
status = UI.toolbar_visible? "Camera"