Module: UI

Overview

The UI module contains a number of methods for creating simple UI elements from a SketchUp Ruby script.

Version:

  • SketchUp 6.0

Defined Under Namespace

Classes: Command, HtmlDialog, Notification, Toolbar, WebDialog

Class Method Summary # collapse

Class Method Details

.add_context_menu_handler {|menu| ... } ⇒ Object

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.

Be careful with what you do in a context menu handler. If you perform an operation takes take a long time, such as traversing the model or selection in a large model it will delay the menu.

See the contextmenu.rb script in the Plugins/examples directory for an example.

Examples:

# Right click on anything to see a Hello World item.
UI.add_context_menu_handler do |context_menu|
  context_menu.add_item("Hello World") {
    UI.messagebox("Hello world")
  }
end

Yields:

  • (menu)

    A block of code that takes a menu as its only as its only argument.

Returns:

  • count - the number of context handlers that are registered

Version:

  • SketchUp 6.0

.beepObject

The beep method plays a system beep sound.

The beep method does not accept any arguments nor return any values.

Examples:

UI.beep

Returns:

  • nil

Version:

  • SketchUp 6.0

.create_cursor(filename, hot_x, hot_y) ⇒ Object

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.

Since SketchUp 2016 it is possible to provide vector images for the cursors. SVG format for Windows and PDF format for OS X.

Examples:

cursor_id = nil
cursor_path = Sketchup.find_support_file("Pointer.png", "Plugins/")
if cursor_path
  cursor_id = UI.create_cursor(cursor_path, 0, 0)
end

def onSetCursor
  UI.set_cursor(cursor_id)
end

Returns ID - ID associated with the cursor

Parameters:

  • filename

    Filename for an image.

  • hot_x

    An x coordinate that is the “hotpoint” for the cursor computed from the left edge of your cursor image.

  • hot_y

    A y coordinate that is the “hotpoint” for the cursor computed from the top edge of the of your cursor image. For example, a value of (hot_x, hot_y) = (5,10) would identify the hotpoint of the cursor at 5 pixels from the left edge of your cursor image and 10 pixels from the top edge of your cursor image.

Returns:

  • ID - ID associated with the cursor

Version:

  • SketchUp 6.0

.inputbox(prompts, defaults, title_or_list, title) ⇒ Object

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.

Examples:

# 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.")

Returns results - An array of returned values if the user did not cancel the dialog. If the user canceled the dialog, false is returned. The returned values in the array will be in the same order as the input fields.

Parameters:

  • prompts

    An array of prompt names appearing in the input box adjacent to input fields.

  • defaults (optional)

    An array of default values for the input fields.

  • title_or_list (optional)

    If 3 params are passed, then this value is a string containing the title of the input box. If a total of 4 params are passed, then this is an array containing pipe-separated strings of options.

  • title (optional)

    The title for the input box.

Returns:

  • results - An array of returned values if the user did not cancel the dialog. If the user canceled the dialog, false is returned. The returned values in the array will be in the same order as the input fields.

Version:

  • SketchUp 6.0

.inspector_namesObject

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.

Examples:

inspectors = UI.inspector_names

Returns:

  • inspectors - an array of strings containing the names of inspectors.

Version:

  • SketchUp 6.0

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”, “Draw”, “Tools”, “Window”, “Extensions” and “Help”.

Note that the “Extensions” menu was named “Plugins” prior to SketchUp 2015. For backward compatibility “Plugins” still works.

Examples:

tool_menu = UI.menu("Tools")
tool_menu.add_item("Cheese Tool") {
  UI.messagebox("Cheese activated.")
}

Returns menu - a menu object.

Parameters:

  • menuname (defaults to: "Extensions")

    Optional. The name of an existing menu. Default menu is “Extensions”

Returns:

  • menu - a menu object.

Version:

  • SketchUp 6.0

.messagebox(message, type = MB_OK) ⇒ Integer

Creates a dialog box containing static text with a series of buttons for the user to choose from.

Valid message box types are:

  • MB_OK - Contains an OK button.

  • MB_OKCANCEL - Contains OK and Cancel buttons.

  • MB_ABORTRETRYIGNORE - Contains Abort, Retry, and Ignore buttons.

  • MB_YESNOCANCEL - Contains Yes, No, and Cancel buttons.

  • MB_YESNO - Contains Yes and No buttons.

  • MB_RETRYCANCEL - Contains Retry and Cancel buttons.

  • MB_MULTILINE - Contains and OK button.

Return values can be any of following:

  • IDOK

  • IDCANCEL

  • IDABORT

  • IDRETRY

  • IDIGNORE

  • IDYES

  • IDNO

In an MB_MULTILINE message box, the message is displayed as a multi-line message with scrollbars (as needed). MB_MULTILNE also allows a third string argument that will be used as the title for the messagebox.

Examples:

result = UI.messagebox('Do you like cheese?', MB_YESNO)
if result == IDYES
  UI.messagebox('SketchUp likes cheese too!')
end

Parameters:

  • message (String)

    The message that you want to display.

  • type (Integer) (defaults to: MB_OK)

    The message box type, which will be a constant from the list in the method comments.

Returns:

  • (Integer)

    A number corresponding to what the user selected.

Version:

  • SketchUp 6.0

.model_info_pagesObject

The 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.

Examples:

mypages = UI.model_info_pages

Returns:

  • mypages - an array of strings containing the names of model info pages.

Version:

  • SketchUp 6.0

.openpanel(title, directory, filename) ⇒ Object

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.

Bug Fixed in SketchUp 2014: Wildcards were not working properly from SU7 to SU2013. Wildcard filters did not populate the file type dropdown. The filter string would be shown in the file name field with '*' characters converted to '_' characters. Note, the format of a wildcard filter string has been changed.

See the description of the filename parameter below for details.

Examples:

chosen_image = UI.openpanel("Open SKP File", "c:/", "model.skp")
chosen_image = UI.openpanel("Open Image File", "c:/", "Image Files|*.jpg;*.png;||")
chosen_image = UI.openpanel("Open CAD File", "c:/", "DXF|*.dxf|DWG|*.dwg||")

Returns string - the full path and name of the file selected, or nil if the dialog was canceled.

Parameters:

  • title

    The title to apply to the open dialog box.

  • directory

    The default directory for the open panel.

  • filename

    The default filename for the open panel. On Windows, you can alternatively pass a wildcard filter using this format: UIname|wildcard||. Additional filter dropdown list items can be added by adding additional pairs of filter name and filter like this: UIname1|wildcard1|UIname2|wildcard2||. Also multiple wildcard filters can be combined into a single line using a semicolon-separated list in the filter field: ui_name|wildcard1;wildcard2||.

Returns:

  • string - the full path and name of the file selected, or nil if the dialog was canceled.

Version:

  • SketchUp 6.0

.openURL(url) ⇒ Boolean

The #openURL method is used to open the default Web browser to a URL.

Examples:

status = UI.openURL("http://www.sketchup.com")

Parameters:

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

.play_sound(filename) ⇒ Object

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.

Examples:

UI.play_sound "Plugins/mediadiscussion.wav"

Returns nil

Parameters:

  • filename

    the relative path to the filename from the SketchUp install directory, or an absolute path to the file. (See Sketchup.find_support_file for a way to search for a specific file.)

Returns:

  • nil

Version:

  • SketchUp 6.0

.preferences_pagesObject

The preferences_pages method is used to returns the names of all the preferences pages. These include windows like Extensions.

Examples:

prefs = UI.preferences_pages

Returns:

  • prefspages - an array of strings containing the names of prefspages.

Version:

  • SketchUp 6.0

.refresh_inspectorsObject

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.

Examples:

UI.refresh_inspectors

Returns:

  • nil

Version:

  • SketchUp 7.0

.savepanel(title, directory, filename) ⇒ Object

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.

Bug Fixed in SketchUp 2014: Wildcards were not working properly from SU7 to SU2013. Semicolon-separated lists of wildcards did not populate the file type dropdown. The filter string would be shown in the file name field with '*' characters converted to '_' characters.

Examples:

path_to_save_to = UI.savepanel("Save Image File", "c:\\", "Shapes.jpg")

Returns string - the full path and name of the file selected or nil if the dialog was canceled.

Parameters:

  • title

    The title to apply to the save dialog box.

  • directory

    The default directory for the save panel.

  • filename

    The default filename for the save panel. On Windows, you can alternatively pass a mask, like “*.txt”, to have all the .txt files display. If you want multiple file types to display, you can supply multiple masks for the filename and separate them with a semicolon, like this: “.txt;.doc”.

Returns:

  • string - the full path and name of the file selected or nil if the dialog was canceled.

Version:

  • SketchUp 6.0

.scale_factorFloat

Note:

SU2017M0 will automatically scale up line width and text size, but will not scale up the points provided to Sketchup::View#draw2d.

Returns the scaling factor SketchUp uses on high DPI monitors. Useful for things like Sketchup::View#draw2d.

Examples:

# Scale a set of points representing 2d screen points to account for high
# DPI monitors.
points2d = [
  Geom::Point3d.new(0, 0, 0),
  Geom::Point3d.new(8, 0, 0),
  Geom::Point3d.new(8, 4, 0),
  Geom::Point3d.new(0, 4, 0)
]
tr = Geom::Transformation.scaling(UI.scale_factor)
points2d.each { |point| point.transform!(tr)

Returns:

  • (Float)

Version:

  • SketchUp 2017

.select_directory(options) ⇒ Object

The select_directory method is used to display the OS dialog for selecting one or several directories from the file system.

Options:

  • :title string The title for the dialog.

  • :directory string Force the starting directory for the dialog. If

    not specified the last chosen directory will be
    used
    
  • :select_multiple boolean Set to true to allow multiple items to be

    selected.

Examples:

# Default title and folder:
chosen_folder = UI.select_directory

# Custom dialog title:
chosen_folder = UI.select_directory(title: "Select Image Directory")

# Force a start folder:
chosen_folder = UI.select_directory(directory: "C:/images")

# Allow multiple items to the selected:
chosen_folder = UI.select_directory(select_multiple: true)

# Custom dialog title and force a start folder:
chosen_folder = UI.select_directory(
  title: "Select Image Directory",
  directory: "C:/images"
)

Returns string or array - A string with the full path of the directory selected when :select_multiple option is set to false otherwise an array of strings or nil if the user cancelled.

Parameters:

  • options

    hash (optional) The dialog can be customized by providing a hash or named arguments of options.

Returns:

  • string or array - A string with the full path of the directory selected when :select_multiple option is set to false otherwise an array of strings or nil if the user cancelled.

Version:

  • SketchUp 2015

.set_cursor(cursor_id) ⇒ Object

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.

Examples:

def onSetCursor
  UI.set_cursor(cursor_id)
end

Returns nil

Parameters:

  • cursor_id

    The id of the cursor you want to display.

Returns:

  • nil

Version:

  • SketchUp 6.0

.set_toolbar_visible(name, visible) ⇒ Object

The set_toolbar_visible method is used to set whether a given toolbar is visible. Note that the toolbars and their names are different on the Mac vs. PC, so be careful and be sure to test when using this method in a cross-platform script.

Examples:

status = UI.set_toolbar_visible("Camera", true)

Returns result - true if successful, false if not.

Parameters:

  • name

    The name of a Ruby toolbar.

  • visible

    True to make the toolbar visible, false to hide it.

Returns:

  • result - true if successful, false if not.

Version:

  • SketchUp 6.0

.show_extension_managernil

The show_extension_manager method is used to display the Extension Manager dialog.

Examples:

UI.show_extension_manager

Returns:

  • (nil)

Version:

  • SketchUp 2017

.show_inspector(name) ⇒ Object

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.

Examples:

status = UI.show_inspector "Components"

Returns status - true if successful, false if unsuccessful

Parameters:

  • name

    The name of inspector that you want to display.

Returns:

  • status - true if successful, false if unsuccessful

Version:

  • SketchUp 6.0

.show_model_info(page_name) ⇒ Object

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.

Examples:

status = UI.show_model_info('Credits')

Returns status - true

Parameters:

  • page_name

    The name of the model info dialog you want to display.

Returns:

  • status - true

Version:

  • SketchUp 6.0

.show_preferences(page_name) ⇒ Object

The show_preferences method is used to display a SketchUp preferences dialog. You can get the list of valid dialogs with UI.preferences_pages.

Note that under OSX this method doesn't currently work.

Examples:

status = UI.show_preferences('GraphicsCard')

Returns status - true

Parameters:

  • page_name

    The name of the preferences dialog you want to display.

Returns:

  • status - true

Version:

  • SketchUp 6.0

.start_timer(seconds, repeat = false) {|procedure| ... } ⇒ Object

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: sketchupapi.blogspot.com/2008/10/animate-yo-cheese.html

Note that there is a bug that if you open a modal window in a non-repeating timer the timer will repeat until the window is closed.

Examples:

# Beep once after 10 seconds.
id = UI.start_timer(10, false) { UI.beep }

Returns timer - a timer ID

Parameters:

  • seconds

    The time in seconds before your code should be called.

  • repeat (optional) (defaults to: false)

    true if you want the timer to repeat, false (or omit) if you do not want it to repeat.

Yields:

  • (procedure)

    The procedure you want to execute after seconds has expired.

Returns:

  • timer - a timer ID

Version:

  • SketchUp 6.0

.stop_timer(id) ⇒ Object

The stop_timer method is used to stop a timer based on its id.

Examples:

# Stop timer before it triggers.
id = UI.start_timer(10) { UI.beep }
UI.stop_timer(id)

Returns nil

Parameters:

  • id

    The timer id for the timer that you want to stop.

Returns:

  • nil

Version:

  • SketchUp 6.0

.toolbar(name) ⇒ Object

The toolbar method is used to get a Ruby toolbar by name. If the toolbar doesn't exist a new one will be created.

Examples:

toolbar = UI.toolbar('Test')

Returns toolbar - a Toolbar object

Parameters:

  • name

    The name of the Ruby toolbar.

Returns:

  • toolbar - a Toolbar object

Version:

  • SketchUp 6.0

.toolbar_namesObject

The toolbar_names method is used to return the name of all the available native toolbars (this differs between PC and Mac). These toolbar names do not include Ruby toolbars.

Examples:

names = UI.toolbar_names

Returns:

  • names - Array of strings representing toolbar names.

Version:

  • SketchUp 6.0

.toolbar_visible?(name) ⇒ Object

The toolbar_visible? method is used to determine whether a given toolbar is visible. Note that the toolbars and their names are different on the Mac vs. PC, so be careful and be sure to test when using this method in a cross-platform script.

Examples:

status = UI.toolbar_visible?("Camera")

Returns status - true if successful, false if unsuccessful

Parameters:

  • name

    The name of a native toolbar.

Returns:

  • status - true if successful, false if unsuccessful

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0