The Sketchup module contains a number of important utility methods for use in your Ruby scripts. Many of the classes in the API are implemented beneath this module. You can think of the Sketchup module as the "root" of the application tree. Most ruby calls start from the currently active model, and this is accessed via the Sketchup.active_model method.
# Grab a handle to the currently active model (aka the one the user is
# looking at in SketchUp.)
model = Sketchup.active_model
# Grab other handles to commonly used collections inside the model.
entities = model.entities
layers = model.layers
materials = model.materials
component_definitions = model.definitions
selection = model.selection
# Now that we have our handles, we can start pulling objects and making
# method calls that are useful.
first_entity = entities[0]
UI.messagebox("First thing in your model is a " + first_entity.typename)
number_materials = materials.length
UI.messagebox("Your model has " + number_materials.to_s + " materials.")
new_edge = entities.add_line( [0,0,0], [500,500,0])The active_model method returns the currently active SketchUp model. On the PC, this is the only model that one can have access to via the API, but Macintosh versions of SketchUp can have multiple models open at once, in which case the method will return the model that the user currently has focused.
Returns:
model = Sketchup.active_model if (! model) UI.messagebox "Failure" else # code acting on the model end
The add_observer method is used to add an observer to the current object.
Arguments:
Returns:
status = Sketchup.add_observer observer
The app_name method is used to retrieve the current application name.
Returns:
name = Sketchup.app_name
The break_edges= method can be used to disable or enable the break edges
feature. Break edges is the SketchUp 7 feature that automatically splits
edges that the user draws which cross over one another.
This feature is always on by default and cannot be disabled by the user
via the user interface, but you can call this method to disable it. Be
cautious in doing so, however, as the resulting model could then be altered
when the user later draws lines into it with the break edges feature
reactivated.
Arguments:
Returns:
is_on = Sketchup.break_edges?
The break_edges? method indicates whether the break edges feature is currently turned on. Break edges is the SketchUp 7 feature that automatically splits edges that the user draws which cross over one another. This feature is always on by default and cannot be disabled by the user via the user interface.
Returns:
is_on = Sketchup.break_edges?
The create_texture_writer method is used to create a TextureWriter object.
Returns:
texturewriter = Sketchup.create_texture_writer
The display_name_from_action method is used to gets a user-friendly name from an action string. See Sketchup.send_action for a list of valid action strings.
Arguments:
Returns:
Sketchup.display_name_from_action("viewRight:")Returns the ExtensionsManager where you can find all registered SketchupExtension objects.
Returns:
extensions = Sketchup.extensions
for extension in extensions
UI.messagebox('The next extension is named: ' + extension.name +
' and its loaded? state is: ' + extension.loaded?)
endThe file_new method is used to create a new file.
Returns:
new_sketchup = Sketchup.file_new
The find_support_files method is used to retrieve the relative path and name
of a file within the SketchUp installation directory.
Forward slashes must be used to delimit between directory names.
Arguments:
Returns:
help_file = Sketchup.find_support_file "help.html", "Plugins/" if (help_file) # Print out the help_file full path UI.messagebox help_file # Open the help_file in a web browser UI.openURL "file://" + help_file else UI.messagebox "Failure" end
The find_support_files method is used to retrieve the relative path and
name of all matching files within the SketchUp installation directory.
Forward slashes must be used to delimit between directory names.
Arguments:
Returns:
array = Sketchup.find_support_files "filename", "directory"
The fix_shadow_strings= method lets you control whether shadow rendering attempts to fix an artifact commonly referred to as "strings". The fix is actually very model dependent and not controllable from the UI, so this method can be used to control it.
Arguments:
Returns:
Sketchup.fix_shadow_strings=true
The fix_shadow_strings? method indicates whether the a fix for a shadow rendering artifact commonly referred to as "strings" is enabled. The fix is actually very model dependent and not controllable from the UI, so this method can be used to test it.
Returns:
is_on = Sketchup.fix_shadow_strings?
The format_angle method takes a number as an angle in radians and formats it into degrees. For example, format_angle(Math::PI) will return 180.0.
Arguments:
Returns:
degrees = Sketchup.format_angle Math::PI
The format_area method formats a number as an area using the current units
settings.
The default unit setting is inches. For example, 10 becomes 10 inches
squared.
Arguments:
Returns:
area = Sketchup.format_area number
The format_degrees method formats a number as an angle given in degrees. For example, 10 becomes 10.0. This is the equivalent to a to_f call.
Arguments:
Returns:
degrees = Sketchup.format_degrees number
The format_length method formats a number as a length using the current
units settings.
The default unit setting is inches. For example, 10 becomes 10".
Arguments:
Returns:
length = Sketchup.format_area 10 if (length) UI.messagebox length end
The get_datfile_info method is used to retrieve the value for the given key
from Sketchup.dat.
If the key is not found, default_value is returned.
Arguments:
Returns:
value = Sketchup.get_datfile_info(key, default_value)
The get_i18ndatfile_info method is used to retrieve the value for the
given key from the internationalization file that SketchUp uses to work
in multiple languages.
If the key is not found, default_value is returned.
Arguments:
Returns:
value = Sketchup.get_i18ndatfile_info(key, default_value)
The get_locale method returns the language code for the language SketchUp is
running in.
Valid return values include: en-US, fr, it, de, es, ja, ko, zh-CN, zh-TW, pt-BR, nl, ru.
If the OS language does not have corresponding folder and files in the SketchUp
Resources folder, the returned language is, by default, en-US.
Returns:
locale = Sketchup.get_locale if (locale) UI.messagebox locale end
The get_resource_path is used to retrieve the directory where "resource" files are stored by SketchUp. Resource files include things like language localization files.
Arguments:
Returns:
directory = Sketchup.get_resource_path "Styles.strings"
The get_shortcuts method retrieves an array of all keyboard shortcuts currently registered with SketchUp. Each shortcut is returned as a string with the shortcut and the command separated by a tab, similar to "Ctrl+A\tEdit/Select All"
Returns:
shortcuts = Sketchup.get_shortcuts
Installs the contents of a ZIP archive file into SketchUp's Plugins folder.
If the ZIP contains subfolders, these will be preserved. This allows for a
Ruby API plugin or Extension developer to distribute their plugin as a single
file regardless of how many asset files must be included.
The user will be shown a warning message that they must agree to before the
install proceeds. If they do not agree, an Interrupt error will be raised.
If the user does agree but there is a problem with the unzip process, an
Exception will be raised. You can capture these states via a begin/rescue.
See the example below.
If the install is successful, any .rb or .rbs files that have been added to
the Plugins folder will immediately be executed, saving the user a restart.
To create an archive file, use your favorite tool (7zip, Winzip, etc.) to zip
up any files and folders in your plugins directory. If the archive contains a
SketchupExtension that you would like users to be able to install from the
Preferences > Extensions panel, rename your file to have a .rbz file
extension.
Returns:
path = 'c:/temp/SomePluginPackage.zip' begin Sketchup.install_from_archive(path) rescue Interrupt => error UI.messagebox "User said 'no': " + error rescue Exception => error UI.messagebox "Error during unzip: " + error end
The is_online method is used to verify a connection to the Internet. This method can take some time to execute, so be careful not to call it more often than you need.
Returns:
status = Sketchup.is_online
Returns a boolean flag indicating whether the application is SketchUp Pro.
Returns:
if (Sketchup.is_pro?) UI.messagebox "You are running SU Pro." end
The is_valid_filename? method is used to determine whether a filename contains illegal characters.
Arguments:
Returns:
status = Sketchup.is_valid_filename? filename
The load method is used to include encrypted and nonencrypted ruby files.
You do not need to include the file extension on the path. This method will
look for .rb first (unencrypted) and then .rbs (encrypted). The scrambler
application used to encrypt SketchUp ruby scripts is available in the
SketchUp C++ SDK. See the "Distributing your Plugin" article for details.
Arguments:
Returns:
sfile = "application_loader" # .rb or .rbs not required statuts = Sketchup.load(sfile)
The open_file method is used to open a file.
Arguments:
Returns:
result = Sketchup.open_file "C:\\model.skp"
The os_language method is used to retrieve a two character code representing
the os language. This is an alias for the get_locale method.
Valid return values are: en-US, fr, it, de, es, ja, ko, zh-CN, zh-TW, pt-BR, nl, ru.
If the OS language does not have corresponding folder and files in the SketchUp
Resources folder, the returned language is, by default, en-US./p>
Returns:
language = Sketchup.os_language
The parse_length method parses a string as a length.
For example, "200" becomes 200.0.
Arguments:
Returns:
length = Sketchup.parse_length "stringnumber"
The plugins_disabled= method lets you control whether SketchUp will load Ruby scripts from the plugins directory at startup time. This is primarily a trouble-shooting method. If you are having strange behavior in SketchUp that you suspect is from a bad script, you can type Sketchup.plugins_disabled=true into the Ruby console and restart SketchUp to see if the problem is fixed.
Arguments:
Returns:
// Type this in the Ruby console then restart SketchUp. Sketchup.plugins_disabled=true // To reactivate plugins, type this into the Ruby console and restart. Sketchup.plugins_disabled=false
The plugins_disabled? method indicates whether Ruby scripts in the plugins directory will be loaded at startup time.
Returns:
is_disabled = Sketchup.plugins_disabled?
The read_defaults method is used to retrieve the string associated with a value within the specified sub-section section of a .INI file or registry (within the Software > @Last Software > SketchUp section).
Arguments:
Returns:
result = Sketchup.read_default "section", "variable", "default"
The register_extension method is used to register an extension with SketchUp's extension manager (in SketchUp preferences).
Arguments:
Returns:
utilitiesExtension = SketchupExtension.new "Utilities Tools", "Utilities/utilitiesTools.rb" utilitiesExtension.description = "Adds Tools->Utilities to the " + "SketchUp inteface. The Utilities submenu contains two tools: " + "Create Face and Query Tool." Sketchup.register_extension utilitiesExtension, false
The register_importer method is used to register an importer with SketchUp.
Arguments:
Returns:
status = Sketchup.register_importer importer
The remove_observer method is used to remove an observer from the current object.
Arguments:
Returns:
status = Sketchup.remove_observer observer
The require method is used to include encrypted and nonencrypted ruby files.
This is an alias of the Sketchup.load method.
You do not need to include the file extension on the path. This method will
look for .rb first (unencrypted) and then .rbs (encrypted). The scrambler
application used to encrypt SketchUp ruby scripts is available in the
SketchUp C++ SDK.
Arguments:
Returns:
sfile = "application_loader" # .rb or .rbs not required statuts = Sketchup::require(sfile)
The save_thumbnail method is used to generate a thumbnail for any SKP file - not necessarily the loaded model.
Arguments:
Returns:
status = Sketchup.save_thumbnail "skp_filename", "image_filename"
The send_action method sends a message to the message queue to perform some
action asynchronously.
Valid actions are:
Arguments:
Returns:
result = Sketchup.send_action "selectArcTool:"
The set_status_text method is used to
set the text appearing on the status bar within the drawing window.
If no arguments are passed, the status bar content is cleared. Valid
positions are:
Arguments:
Returns:
result = Sketchup.set_status_text "This is a Test", SB_VCB_VALUE if (result) #code to do something if set_status_text is successful end
The status_text= method is used to set the text appearing on the status
bar within the drawing window.
This is the same as calling set_status_text with a 2nd parameter of
SB_PROMPT.
Arguments:
Returns:
result = Sketchup.status_text = "This is a Test"
The template method is used to get the file name of the current template. Templates are the .skp files that are loaded when the user select File > New.
Returns:
name = Sketchup.template
The template= method is used to set the file name of the current template. Templates are the .skp files that are loaded when the user select File > New.
Arguments:
Returns:
status = Sketchup.template= "filename"
The template_dir is used to retrieve the directory where templates are stored by the SketchUp install. Templates are the .skp files that are loaded when the user select File > New.
Returns:
directory = Sketchup.template_dir
The undo method is used undo the last transaction on the undo stack.
Returns:
Sketchup.undo
The vcb_label= method is used to set the label that appears on the vcb,
or the "value control box", which is another word for the "measurements"
text entry box that appears at the bottom on the SketchUp window.
This is the same as calling set_status_text with a 2nd parameter of
SB_VCB_LABEL.
Arguments:
Returns:
result = Sketchup.vcb_label = "This is a Test"
The vcb_value= method is used to set the value that appears on the vcb,
or the "value control box", which is another word for the "measurements"
text entry box that appears at the bottom on the SketchUp window.
This is the same as calling set_status_text with a 2nd parameter of
SB_VCB_VALUE.
Arguments:
Returns:
result = Sketchup.vcb_value = "This is a Test"
Gets the current version of sketchup in decimal form.
Returns:
version = Sketchup.version if (version) UI.messagebox version else return end
Get the current version of sketchup as a whole number for comparisons.
Returns:
version = Sketchup.version_number if (version) UI.messagebox version else return end
The write_defaults method is used to set the string associated with a variable within the specified sub-section of a .plist file on the Mac or the registry on Windows (within the Software > @Last Software > SketchUp section).
Arguments:
Returns:
result = Sketchup.write_default "section", "variable", "my_value"