SketchUp  Ruby API Reference 

Class Index

Method Index

Developers Guide

Examples

Command class

The Command class is the preferred class for adding tools to the menus and Ruby toolbars.

Parent: Object

Methods: new , large_icon= , menu_text=, set_validation_proc, small_icon=, status_bar_text=,tooltip=  

Example code: commandtests.rb 

Class Methods

new

The new method is used to create a new command.

Syntax

command = UI::Command.new "menutext" {...}

Arguments

"menutext" - the text that will appear for this command's menu item if it appears on a menu

... - code that executes the command when the menu item or toolbar item is selected

Return Value

command - the new Command object

Example

add_separator_to_menu("Draw")

# Adds a Test submenu to the Draw menu where the Tester menu item appears

testmenu = UI.menu("Draw").add_submenu($tStrings.GetString("Test"))

# This menu item simply displays Hello World on the screen when clicked

cmd = UI::Command.new($tStrings.GetString("Tester")) { helloWorld }

testmenu.add_item cmd

Instance Methods

large_icon=

The large_icon= method is used to identify the icon file for the command's large icon.

Syntax

command = command.large_icon = path

Arguments

path - the path to the large icon

Return Value

command - the Command object

Example

toolbar = UI::Toolbar.new "Test"
# This toolbar tool simply displays Hello World on the screen when clicked
cmd = UI::Command.new($tStrings.GetString("Test")) { helloWorld }
cmd.small_icon = "ToolPencilSmall.png"
cmd.large_icon = "ToolPencilLarge.png"
toolbar = toolbar.add_item cmd
toolbar.show

 

 

menu_text=

The menu_text= method is used to set the menu item name for the command.

Syntax

command = command.menu_text= "menuitem"

Arguments

"menuitem" - a string representing the menu item for the command

Return Value

command - a Command object

Example

add_separator_to_menu("Draw")
# Adds a Test submenu to the Draw menu where the Tester menu item appears
testmenu = UI.menu("Draw").add_submenu($tStrings.GetString("Test"))
# This menu item simply displays Hello World on the screen when clicked
cmd = UI::Command.new($tStrings.GetString("Tester")) { helloWorld }
cmd = cmd.menu_text = "New String"
testmenu.add_item cmd

 

  

set_validation_proc

The set_validation_proc method is used to identify the command validation procedure.

Syntax

command = menu.set_validation_proc item_id {...} 

Arguments

 

Return Value

command - a Command object

Comments

Your procedure should return either MF_ENABLED, MF_DISABLED< MF_CHECKED, MF_UNCHECKED, or MF_GRAYED.

Example

require 'sketchup.rb'



def validate_selection
ss = Sketchup.active_model.selection
if ss.count == 1
return MF_CHECKED
elseif ss.count == 2
return MF_ENABLED
else
return MF_GRAYED
end
end


mydir = File.dirname(__FILE__)
cmd = UI::Command.new('SayHi') { UI::messagebox 'Hello!' }
cmd.menu_text = "Say Hello"
cmd.set_validation_proc { validate_selection }
cmd.large_icon = mydir + "/myimage.gif"
cmd.small_icon = mydir + "/myimage.gif"

UI.menu('plugins').add_item(cmd)


tb = UI::Toolbar.new('SayHiTB')

tb.add_item(cmd)

tb.show

 

 

small_icon=

The small_icon= method is used to identify the icon file for the command's small icon.

Syntax

command = command.small_icon = path

Arguments

path - a path to the small icon.

Return Value

command - the Command object

Example

toolbar = UI::Toolbar.new "Test"
# This toolbar tool simply displays Hello World on the screen when clicked
cmd = UI::Command.new($tStrings.GetString("Tester")) { helloWorld }    
cmd.small_icon = "ToolPencilSmall.png"
cmd.large_icon = "ToolPencilLarge.png"
toolbar = toolbar.add_item cmd
toolbar.show

 

    

status_bar_text=

The status_bar_text= method is used to set the status bar text for the command.

Syntax

command = command.status_bar_text= "text"

Arguments

"text" - the text that will appear on the status bar when the cursor is over the command's menu item

Return Value

command - the Command object

Example

toolbar = UI::Toolbar.new "Test"
# This toolbar tool simply displays Hello World on the screen when clicked
cmd = UI::Command.new($tStrings.GetString("Tester")) { helloWorld }    
cmd.small_icon = "ToolPencilSmall.png"
cmd.large_icon = "ToolPencilLarge.png"
cmd.status_bar_text = $tStrings.GetString("Testing the toolbars class")
toolbar = toolbar.add_item cmd
toolbar.show

 

     


tooltip=

The tooltip= method is used to set the tooltip for the command's icon.

Syntax

command = command.tooltip= "tooltip"

Arguments

"tooltip" - the text that appears when the cursor is over the command's icons

Return Value

command - a Command object

Example

toolbar = UI::Toolbar.new "Test"
# This toolbar tool simply displays Hello World on the screen when clicked
cmd = UI::Command.new($tStrings.GetString("Tester")) { helloWorld }    
cmd.small_icon = "ToolPencilSmall.png"
cmd.large_icon = "ToolPencilLarge.png"
cmd.tooltip = $tStrings.GetString("Test Toolbars")
toolbar = toolbar.add_item cmd
toolbar.show

 

SketchUp  Ruby API Reference: Command class

© Google Inc. 2007 sketchup.google.com