SketchUp  Ruby API Reference 

Class Index

Method Index

Developers Guide

Examples

UI module

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

Parent: N/A

Methods: add_context_menu_handler, beep, create_cursor, inputbox, inspector_names, menu, messagebox, model_info_pages, openURL, openpanel, play_sound, preferences_pages, savepanel, set_cursor, show_inspector, show_model_info, show_preferences, start_timer, stop_timer, toolbar, toolbar_names, toolbar_visible?, toolbar_visible=

Example Code: uitests.rb, contextmenu.rb

Module Methods


add_context_menu_handler

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.

Syntax

count = UI.add_context_menu_handler menu

Arguments

menu - a block of code that takes a menu as its only as its only argument

Return Value

count - the number of context handlers that are registered

Comments

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

Example

UI.add_context_menu_handler do |menu|

 

 


beep 

The beep method plays a system beep sound.

Syntax

UI.beep

Comments

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

 

 


create_cursor 

The create_cursor method is used to create a cursor from an image file at the specified location.

Syntax

ID = UI.create_cursor filename, x, y

Arguments

filename - filename for an image

x - an x coordinate

y - a y coordinate

Return Value

ID - ID associated with the cursor

Example

def 
 GetCursorID(filename, hotx, hoty)
cursorPath = Sketchup.find_support_file(filename, "Tools/Sandbox/Images")
if cursorPath
id = UI.create_cursor(cursorPath, hotx, hoty)
else
id=0
end
return id
end

 

 


inputbox 

Creates a dialog box for inputting user information. The dialog box contains input fields with static text prompts, optional default values, and optional name.

Syntax

results = UI.inputbox prompts[], defaultvalues[], “inputboxtitle”

Arguments

prompts[] – An array of prompt names appearing in the input box adjacent to input fields

defaultvalues[] – (optional) An array of default values for the input fields

“inputboxtitle” – (optional) The name for the input box

Return Value

result -  true if successful; false if unsuccessful

Comments

You will have to pass specific units (such as feet in the following examples) if you want your box to conform to a certain scale. Also note, you  cannot pass just prompts and a name, you must either pass no arguments or all arguments.

Example 

prompts = ["Width", "Height", "Depth"]
values = [6.feet, 5.feet, 4.feet]
results = inputbox prompts, values, "Box Dimensions"
if (!results)
#failure
return
else
#success: you can extract values from the results array
end

 

 


inspector_names

The inspector_names method is used to returns the names of all the inspectors.

Syntax

inspectors = UI.inspector_names	

Return Value

inspectors - a vector of strings containing the names of inspectors.

Example

 

 


menu 

The menu method retrieves a SketchUp’s menu object with a given name.

Syntax

menu = UI.menu “menuname” 

Arguments

“menuname” – the name of an existing menu

Return Value

menu – a menu object.

Comments

Valid menu names are: "File", "Edit", "View", "Camera", "Create", "Tools", "Page", "Window", "Plugins" and "Help".

Example

Draw_menu = UI.menu “Draw”



if (!draw_menu)

#failure

return

else

#success: you can use the menu object now.

end

 

 


messagebox 

Creates a dialog box containing static text.

Syntax

UI.messagebox "message", mbtype, “messageboxtitle”

Arguments

“message” – The message that you want to appear in the message box

mbtype – (optional) message box type

“messageboxtitle” – (optional) title for the message box

Comments

The default message box type is MB_OK and the default title for the message box is “Validity Check.” Valid message box types are:

MB_OK – Contains an OK (1) button
MB_OKCANCEL – Contains OK (1) and CANCEL (2)buttons
MB_ABORTRETRYCANCEL – Contains ABORT (3), RETRY (4), and CANCEL (2) buttons
MB_YESNOCANCEL – Contains YES (6), NO (7), and CANCEL (2) buttons
MB_YESNO – Contains YES (6) and NO (7) buttons
MB_RETRYCANCEL – Contains RETRY (4) and CANCEL (2) buttons
MB_MULTILINE – Contains and OK (1) button. In a MB_MULTILINE message box, the message is displayed as a multi-line message with scrollbars (as needed).

The messagebox method returns a number corresponding to the button pressed (in parentheses above)

 

 


model_info_pages

The model_info_pages method is used to returns the names of all the available model info pages.

Syntax

mipages = UI.model_info_pages

Return Value

mipages - a vector of strings containing the names of model info pages.

Example

 

 


openURL 

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

Syntax

status = UI.openURL "URL"

Arguments

"URL" - a string URL

Return Value

status - true if successful

Example 

status = UI.openURL("http://www.sketchup.com")
if (status)
UI.messagebox status
else
UI.messagebox "Failure" end

 

 


openpanel 

The openpanel method is used to display the Open dialog box.

Syntax

UI.openpanel "Title", "Directory", "Filename"

Arguments

"Title" - The tile to apply to the open dialog box

"Directory" - the default directory for the open panel

"Filename" - the default filename for the open panel

Example

UI.openpanel "Open Image File", "c:\\Program 
 Files\\@Last Software\\Sketchup 5\\Plugins\\", "Shapes.jpg"

 

 


play_sound 

The play_sound method is used to play a sound file.

Syntax

UI.play_sound "Filename"

Arguments

"Filename" - the relative path to the filename from the Plugins directory.

Example

UI.play_sound "mediadiscussion.wav"

 

 


preferences_pages

The preferences_pages method is used to returns the names of all the preferences pages.

Syntax

prefspages = UI.preferences_pages

Return Value

prefspages - a vector of strings containing the names of prefspages.

Example

 

 


savepanel 

The savepanel method is used to display the Save dialog box.

Syntax

UI.savepanel "Title", "Directory", "Filename"

Arguments

"Title" - The tile to apply to the open dialog box

"Directory" - the default directory for the open panel

"Filename" - the default filename for the open panel

Example

UI.openpanel "Open Image File", "c:\\Program Files\\@Last Software\\Sketchup 5\\Plugins\\", "Shapes.jpg"

 

 


set_cursor 

The set_cursor method is used to change the cursor to a new cursor with a new ID

Syntax

UI.set_cursor ID

Arguments

ID - a new cursor ID

Example 

def onSetCursor
if (@activeEntity==true and @state==0) or @state>0 or @activeSel==false
UI.set_cursor(@@cursorID)
end
end

 

 


show_inspector

The show_inspector method is used to display the inspector with the given name.

Syntax

status = UI.show_inspector inspector_name

Arguments

inspector_name - the name of inspector that you want to display.

Return Value

status - true if successful, false if unsuccessful

Example

 

 


show_model_info

The show_model_info method is used to displays the ModelInfo dialog for a specific page.

Syntax

status = UI.show_model_info page_name

Arguments

page_name - the name of page_name whose model_info dialog you want to display.

Return Value

status - true if successful, false if unsuccessful

Example

 

 


show_preferences

The show_preferences method is used to return the names of all the preferences pages.

Syntax

prefs= UI.show_preferences 

Return Value

prefs - vector of strings representing preferences pages.

Example

 

 


start_timer 

The start_timer method is used to start a timer.

Syntax

timer = UI.start_timer(seconds, repeat) {...}

Arguments

seconds - the timer in seconds

repeat - true if you want the timer to repeat, false if you do not want it to repeat

{...} - the procedure you want to execute after seconds has expired

Return Value

timer - a timer ID

Example

id = UI.start_timer(10) {UI.beep} 
if (id)
UI.messagebox id
else
UI.messagebox "Failure"
end

 

 


stop_timer 

The stop_timer method is used to stop a timer.

Syntax

UI.stop_timer id

Arguments

id - the timer idea for the timer that you want to stop

Example

id = UI.start_timer(10) {UI.beep}
if (id)
UI.messagebox id
else
UI.messagebox "Failure"
end
UI.stop_timer id
UI.messagebox "Stopped"

 

 


toolbar 

The toobar method is used to get a Ruby toolbar by name.

Syntax

toolbar = UI.toolbar "Name"

Arguments

"Name" - the name of the Ruby toolbar

Return Value

toolbar - a Toolbar object

Example

toolbar = UI::Toolbar.new "Test"
toolbar = UI.toolbar "Test"
if (toolbar)
UI.messagebox toolbar
else
UI.messagebox "Failure"
end

 

 


toolbar_names

The toolbar_names method is used to returns the name of all the available toolbars (this differs between PC and Mac).

Syntax

names = UI.toolbar_names

Return Value

names - vector of strings representing toolbar names.

Example

 

 


toolbar_visible?

The toolbar_visible? method is used to determine whether a toolbar is visible.

Syntax

status = UI.toolbar_visible?

Return Value

status - true if successful, false if unsuccessful

Example

 

 


toolbar_visible=

The toolbar_visible= method is used to make a specific toolbar visible.

Syntax

status = UI.toolbar_visible= name

Arguments

name - the name of the toolbar to display

Return Value

status - true if successful, false if unsuccessful

Example