Class: UI::Command

Inherits:
Object
  • Object
show all

Overview

The Command class is the preferred class for adding tools to the menus and Ruby toolbars. For example, you could add a menu item and pass it a code block directly, or you could first create a Command.

Using Commands gives you greater control over how the item works in the UI, and it allows multiple spots in the UI to call the same code. For example, You might want a toolbar button and a context-click menu item to both point to the same command, and to control the tooltip and its “graying” from a single spot in your code.

Examples:

# You can add menu items as procedure blocks, as shown here, but
# you have no control over whether it is grayed out, for example.
UI.menu("Draw").add_item("My Procedure") {
  UI.messagebox("My Procedure")
}

# Better to create a command object.
cmd = UI::Command.new("Tester") {
  UI.messagebox("My Command")
}
cmd.menu_text = "My Command"
cmd.set_validation_proc {
  if Sketchup.active_model.selection.length == 0
    MF_GRAYED
  else
    MF_ENABLED
  end
}
UI.menu("Draw").add_item cmd

Version:

  • SketchUp 6.0

Class Method Summary # collapse

Instance Method Summary # collapse

Class Method Details

.new(menutext) { ... } ⇒ UI::Command

Note:

Prior to SketchUp 2019 it was not possible to sub-class UI::Command due to a bug in how SketchUp initialized the class.

The new method is used to create a new command.

Examples:

UI.menu("Draw").add_separator

# Adds a Test submenu to the Draw menu where the Tester menu item appears
testmenu = UI.menu("Draw").add_submenu("Test")

# This menu item simply displays Hello World on the screen when clicked.
cmd = UI::Command.new("Tester") { UI.messagebox("Hello World") }
testmenu.add_item cmd

Parameters:

  • menutext (String)

    The text that will appear for this command's menu item if it appears on a menu.

Yields:

  • Code that executes the command when the menu item or toolbar item is selected.

Returns:

Version:

  • SketchUp 6.0

Instance Method Details

#extensionSketchupExtension?

Note:

This is an advanced feature that extension developers normally won't have to deal with. It's purpose is to address scenarios when SketchUp isn't able to automatically infer which extension the command belongs to.

The #extension method returns the command's associated extension.

Examples:

extension = Sketchup.extensions['Sandbox Tools']
cmd = UI::Command.new("Tester") {}
cmd.extension = extension
p cmd.extension == extension

Returns:

Version:

  • SketchUp 2022.0

#extension=(extension) ⇒ Object

Note:

This is an advanced feature that extension developers normally won't have to deal with. It's purpose is to address scenarios when SketchUp isn't able to automatically infer which extension the command belongs to. These scenarios are for example an extension using a library to add its commands or command manager extensions.

The #extension= method explicitly sets the command's associated extension.

Examples:

extension = Sketchup.extensions['Sandbox Tools']
cmd = UI::Command.new("Tester") {}
cmd.extension = extension

Parameters:

Version:

  • SketchUp 2022.0

#get_validation_procProc?

The #get_validation_proc method returns the command's validation proc.

Examples:

cmd = UI::Command.new("Tester") {}
cmd.set_validation_proc { MF_DISABLED }
proc = cmd.get_validation_proc

Returns:

  • (Proc, nil)

See Also:

Version:

  • SketchUp 2022.0

#large_iconString

The large_icon method returns the icon file for the command's large icon.

Examples:

toolbar = UI::Toolbar.new "Test"
# This command displays Hello World on the screen when clicked
cmd = UI::Command.new("Test") { UI.messagebox("Hello World") }
cmd.small_icon = "ToolPencilSmall.png"
cmd.large_icon = "ToolPencilLarge.png"
toolbar = toolbar.add_item cmd
toolbar.show
puts cmd.large_icon

Returns:

  • (String)

    the path to the large icon.

Version:

  • SketchUp 8.0 M1

#large_icon=(path) ⇒ String

The large_icon= method is used to identify the icon file for the command's large icon. large icons should be 32x32 pixel images for best display quality.

Since SketchUp 2016 it is possible to provide vector images for the command. SVG format for Windows and PDF format for OS X. Since the vector images scale for both small and large icon sizes, you may choose to use only one vector image for both variants.

Examples:

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

Parameters:

  • path (String)

    The path to the large icon.

Returns:

Version:

  • SketchUp 6.0

The menu_text method returns the menu item name for the command.

Examples:

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("Test")
cmd = UI::Command.new("Tester") { UI.messagebox("Hello World") }
cmd.menu_text = "New String"
testmenu.add_item cmd
puts cmd.menu_text

Returns:

Version:

  • SketchUp 8.0 M1

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

Examples:

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("Test")
cmd = UI::Command.new("Tester") { UI.messagebox("Hello World") }
cmd.menu_text = "New String"
testmenu.add_item cmd

Parameters:

  • menuitem (String)

    A string representing the menu item for the command.

Returns:

Version:

  • SketchUp 6.0

#procProc

The #proc method returns the command's proc that is called when the command is invoked.

Examples:

cmd = UI::Command.new("Tester") {}
cmd.set_validation_proc { MF_DISABLED }
proc = cmd.proc
proc.call

Returns:

  • (Proc)

Version:

  • SketchUp 2022.0

#set_validation_proc { ... } ⇒ UI::Command

Note:

Avoid disabling an command as it often isn't obvious to the user why it is disabled. Prefer keeping the command enabled but show an error message if pressed when it cannot be used.

The #set_validation_proc method allows you to change whether the command is enabled, checked, etc. For instance, the command toggling a dialog window may be displayed as checked while the dialog is open.

Examples:

# Create a command object.
cmd = UI::Command.new("Tester") {
  UI.messagebox("My Command")
}
cmd.menu_text = "My Command"
cmd.set_validation_proc {
  if Sketchup.active_model.selection.length == 0
    MF_GRAYED
  else
    MF_ENABLED
  end
}
UI.menu("Draw").add_item(cmd)

Yields:

Yield Returns:

  • (Integer)

    MF_ENABLED, MF_DISABLED, MF_CHECKED, MF_UNCHECKED, or MF_GRAYED

Returns:

See Also:

Version:

  • SketchUp 6.0

Known Bugs:

  • On Mac the validation proc isn't called as often as it should. For instance a selection change doesn't trigger it.

#small_iconString

The small_icon method returns the icon file for the command's small icon.

Examples:

toolbar = UI::Toolbar.new "Test"
# This toolbar command displays Hello World on the screen when clicked.
cmd = UI::Command.new("Tester") { UI.messagebox("Hello World") }
cmd.small_icon = "ToolPencilSmall.png"
cmd.large_icon = "ToolPencilLarge.png"
toolbar = toolbar.add_item cmd
toolbar.show
puts cmd.small_icon

Returns:

  • (String)

    the path to the small_icon

Version:

  • SketchUp 8.0 M1

#small_icon=(path) ⇒ String

The small_icon= method is used to identify the icon file for the command's small icon. Small icons should be 24x24 pixel images for best display quality.

Since SketchUp 2016 it is possible to provide vector images for the cursors. SVG format for Windows and PDF format for OS X. Since the vector images scale for both small and large icon sizes, you may choose to use only one vector image for both variants.

Examples:

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

Parameters:

  • path (String)

    A path to the small icon.

Returns:

Version:

  • SketchUp 6.0

#status_bar_textString

The status_bar_text method returns the status bar text for the command.

Examples:

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

Returns:

  • (String)

    the status bar text.

Version:

  • SketchUp 8.0 M1

#status_bar_text=(text) ⇒ String

The status_bar_text= method is used to set the status bar text for the command. This should be a description what the command does.

Examples:

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

Parameters:

  • text (String)

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

Returns:

Version:

  • SketchUp 6.0

#tooltipString

The tooltip method returns command item's tooltip text.

Examples:

toolbar = UI::Toolbar.new "Test"
# This command displays Hello World on the screen when clicked
cmd = UI::Command.new("Test") { UI.messagebox("Hello World") }
cmd.tooltip = "Hello World Tool"
toolbar = toolbar.add_item cmd
toolbar.show
puts cmd.tooltip

Returns:

  • (String)

    the tooltip text

Version:

  • SketchUp 8.0 M1

#tooltip=(text) ⇒ String

Note:

The tooltip text should repeat the commands' title text. For the command description, use #status_bar_text.

The #tooltip= method is used to define a command item's tooltip header. Tooltips will appear when the command is attached to a tool bar and the user hovers their cursor over the icon.

Examples:

toolbar = UI::Toolbar.new "Test"
# This command displays Hello World on the screen when clicked
cmd = UI::Command.new("Test") { UI.messagebox("Hello World") }
cmd.tooltip = "Hello World Tool"
toolbar = toolbar.add_item cmd
toolbar.show

Parameters:

  • text (String)

    The text of the tooltip.

Returns:

Version:

  • SketchUp 6.0