|
SketchUp Ruby API Reference |
The Sketchup module contains number of important utility methods for use in your Ruby scripts.
Parent:N/A
Methods: active_model, add_observer, app_name, create_texture_writer, display_name_from_action, file_new, find_support_file, find_support_files, format_angle, format_area, format_degrees, format_length, get_datfile_info, get_locale, get_shortcuts, is_online, is_valid_filename?, load, open_file, os_language, parse_length, read_default, register_extension, register_importer, remove_observer, require, save_thumbnail, send_action, set_status_text=, set_status_text, template, template=, template_dir, undo, vcb_label=, vcb_value=, version, version_number, write_default
Example Code:sketchuptests.rb
model = Sketchup.active_model
model – active model object if successful, false if unsuccessful
model = Sketchup.active_modelif (! model)UI.messagebox “Failure”else# code acting on the model
The add_observer method is used to add an observer to the current object.
status = Sketchup.add_observer observer
observer - an observer
status - true if successful, false if unsuccessful.
The app_name method is used to retrieve the current application name.
name = Sketchup.app_name
name - the name of the application, either "Google SketchUp Pro" or "Google SketchUp"
The create_texture_writer method is used to create a TextureWriter object.
texturewriter = Sketchup.create_texture_writer
texturewriter - a texturewriter object if successful.
The display_ name_from_action method is used to gets a user-friendly name from an action string.
friendly_name = Sketchup.display_name_from_action action_name
action_name = an action string. See comments for a valid list of action strings.
friendly_name = a friendly name.
showRubyPanel:
viewBack:
viewBottom:
viewFront:
viewIso:
viewLeft:
viewRight:
viewTop:
viewPerspective:
viewShowAxes:
viewShowHidden:
viewZoomExtents:
viewZoomToSelection:
viewUndo:
selectOrbitTool:
selectPositionCameraTool:
selectDollyTool:
selectTurnTool:
selectWalkTool:
selectZoomTool:
selectFieldOfViewTool:
selectZoomWindowTool:
pageAdd:
pageDelete:
pageUpdate:
pageNext:
pagePrevious:
renderWireframe:
renderHiddenLine:
renderMonochrome:
renderShaded:
renderTextures:
selectArcTool:
selectAxisTool:
selectCircleTool:
selectEraseTool:
selectFreehandTool:
selectLineTool:
selectMeasureTool:
selectMoveTool:
selectOffsetTool:
selectPaintTool:
selectPolygonTool:
selectProtractorTool:
selectPushPullTool:
selectRectangleTool:
selectRotateTool:
selectScaleTool:
selectSectionPlaneTool:
selectTextTool:
selectDimensionTool:
selectExtrudeTool:
selectSelectionTool:
editUndo:
editRedo:
editHide:
editUnhide:
fixNonPlanarFaces:
Sketchup.display_name_from_action("viewRight:")
The file_new method is used to create a new file.
Sketchup = Sketchup.file_new
Sketchup = a Sketchup object if successful.
The find_support_filess method is used to retrieve the relative path and name of a file within the SketchUp installation directory.
array = Sketchup.find_support_files “filename”, “directory”
“filename” – name of the filename you want to find
“directory” – (optional) directory relative to the SketchUp installation directory.
array - an array of files. If unsuccessful, the method returns false.
Forward slashes must be used to delimit between directory names.
The find_support_files method is used to retrieve the relative path and name of a file within the SketchUp installation directory.
path = Sketchup.find_support_file “filename”, “directory”
“filename” – name of the filename you want to find
“directory” – (optional) directory relative to the SketchUp installation directory.
path - the entire path if successful. If unsuccessful, the method returns false.
Forward slashes must be used to delimit between directory names.
help_file = Sketchup.find_support_file “index.html”, “Ruby/Docs/” 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
elseend
UI.messagebox “Failure”
The format_angle method formats a number as an angle (in radians) using the current units settings. For example, 10 becomes 573 or 10 x 180/pi.
radians = Sketchup.format_angle number
number – a number to be formatted
radians – an angle in radians if successful, false if unsuccessful
radians = Sketchup.format_angle 10 if (radians)
UI.messagebox radians end
The format_area method formats a number as an area using the current units settings.
area = Sketchup.format_area number
number – a number to be formatted
area – an area if successful, false if unsuccessful.
The default unit setting is inches. For example, 10 becomes 10 inches squared.
area = Sketchup.format_area 10 if (area)
UI.messagebox area end
The format_degrees method formats a number as an angle given in degrees. For example, 10 becomes 10.0 degrees.
degrees = Sketchup.format_degrees number
number – a number to be formatted.
degrees – degrees if successful, false if unsuccessful.
degrees = Sketchup.format_area 10 if (degrees) UI.messagebox degrees end
The format_length method formats a number as a length using the current units settings.
length = Sketchup.format_length number
number – a number to be formatted
length – length if successful, false if unsuccessful
The default unit setting is inches. For example, 10 becomes 10”.
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.
value = Sketchup.get_datfile_info key,default value
key - the key whose value you want to retrieve
default value - the default value you want returned if key is not available.
value - a string value if successful.
If the key is not found, default_value is returned.
The get_locale method returns the locale for the machine where SketchUp is installed.
locale = Sketchup.getlocale
locale - a two character locale (see comments)
Valid return values are: EN, QE, DE, FR, JP, ES, and IT
locale = Sketchup.get_locale
if (locale)
UI.messagebox locale
end
The get_shortcuts method retrieves an array of all shortcuts currently registered with SketchUp.
shortcuts = sketchup.get_shortcuts
shortcuts - an array of shortcut strings
shortcuts = Sketchup.get_shortcuts
if (shortcuts)
UI.messagebox shortcuts
end
The is_online method is used to verify a connection to the Internet.
status = Sktechup.is_online
status - true if successful, false if unsuccessful.
The is_valid_filename? method is used to determine whether a filename contains illegal characters.
status = Sketchup.is_valid_filename? filename
filename - a filename string
status - true if filename is valid, false if filename is invalid (contains illegal characters).
The load method is used to include encrypted and nonencrypted ruby files.
status = sketchup.load path
path - the path, including the filename, to the file you want to require.
status - True if the file is included. False if the file is not included.
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 SDK. Send a request to sdk@sketchup.com to obtain the SDK.
The open_file method is used to open a file.
file = sketchup.open_file filename
filename - the path and filename to open
file - a file
The os_language method is used to retrieve a two character code representing the os language.
language = SketchUp.os_language
lanuage - a two character code representing the os language. For example, EN represents English.
The parse_length method parses a string as a length.
length = Sketchup.parse_length “stringnumber”
“stringnumber” – the string number representation to be parsed as a number
length – the numerical representation of the string if successful, or false if unsuccessful.
For example, “200” becomes 200.0.
length = Sketchup.parse_length “200” if (length) UI.messagebox length end
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).
result = Sketchup.read_default “section”, “variable”, “default”
“section” – A section in an .INI or registry
“variable” – A variable within the section
“default” – (optional) A default value if the value is not found
result – True if successful, false if unsuccessful.
# Write to the Windows registry
Sketchup.write_default “Test”, “TesterName”, “Jon Bradley” # Read from the Windows registry
value = Sketchup.read_default “Test”, “TesterName,” “Default” if(value) UI.messagebox value end
The register_extension method is used to register an extension with SketchUp's extension manager (in SketchUp preferences).
status = Sketchup.register_extension extension, registered?
extension - a SketchupExtension object
registered? - true if extension should be automatically loaded the first time it is registered
status - true if extension registered properly
utilitiesExtension = SketchupExtension.new $uStrings.GetString("Utilities Tools"), "Utilities/utilitiesTools.rb"utilitiesExtension.description=$uStrings.GetString("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.
status = Sketchup.register_importer importer
importer - an Importer object representing the importer.
status - true if successful, false if unsuccessful.
The remove_observer method is used to remove an observer from the current object.
status = object.remove_observer observer
observer - an observer
true if successful, false if unsuccessful.
The require method is used to include encrypted and nonencrypted ruby files.
status = sketchup.require path
path - the path, including the filename, to the file you want to require.
status - True if the file is included. False if the file is not included.
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 SDK. Send a request to sdk@sketchup.com to obtain the SDK.
The save_thumbnail method is used to generate a thumbnail for any SKP file - not necessarily the loaded model.
status = "skp_filename", "image_filename"
"skp_filename" - the name of the SketchUp file whose model you want represented in the thumbnail.
"image_filename" - the name of the file where the thumbnail will be saved.
status - true if successful, false if unsuccessful.
The send_action method sends a message to the message queue to perform some action asynchronously.
result = Sketchup.send_action “action”
“action” – the action to be performed
result – true if successful; false if unsuccessful
Valid actions are:
showRubyPanel:
viewBack:
viewBottom:
viewFront:
viewIso:
viewLeft:
viewRight:
viewTop:
viewPerspective:
viewShowAxes:
viewShowHidden:
viewZoomExtents:
viewZoomToSelection:
viewUndo:
selectOrbitTool:
selectPositionCameraTool:
selectDollyTool:
selectTurnTool:
selectWalkTool:
selectZoomTool:
selectFieldOfViewTool:
selectZoomWindowTool:
pageAdd:
pageDelete:
pageUpdate:
pageNext:
pagePrevious:
renderWireframe:
renderHiddenLine:
renderMonochrome:
renderShaded:
renderTextures:
selectArcTool:
selectAxisTool:
selectCircleTool:
selectEraseTool:
selectFreehandTool:
selectLineTool:
selectMeasureTool:
selectMoveTool:
selectOffsetTool:
selectPaintTool:
selectPolygonTool:
selectProtractorTool:
selectPushPullTool:
selectRectangleTool:
selectRotateTool:
selectScaleTool:
selectSectionPlaneTool:
selectTextTool:
selectDimensionTool:
selectExtrudeTool:
selectSelectionTool:
editUndo:
editRedo:
editHide:
editUnhide:
fixNonPlanarFaces:
result = Sketchup.send_action “selectMoveTool:” if (result) UI.messagebox “Move Tool Selected.” else UI.messagebox “Error: Could not select the Move Tool” end
The set_status_text method is used to set the text appearing on the status bar within the drawing window.
result = Sketchup.set_status_text “status text” position
“status text” – (optional) the status text that will appear on the left-side of the status bar
position – (optional) the position where the text will appear.
“result” - true if successful; false if unsuccessful
If no arguments are passed, the status bar content is cleared. Valid POSITIONs are:
SB_PROMPT- the status text will appear
at the left-side of the status bar
SB_VCB_LABEL – the status text will appear in place of the VCB label
SB_VCB_VALUE – the status text will appear in the VCB
result = Sketchup.set_status_text “This is a Test” if (result) #code to do something if set_status_text is successful end
The template method is used to get the file name of the current template.
name = Sketchup.template
name - the current template
The template= method is used to set the file name of the current template.
status = Sketchup.template= "filename"
"filename" - the name of the template to set.
status - true if successful, false if unsuccessful.
The template_dir is used to retrieve the directory where templates are stored by the SketchUp install.
directory = Sketchup.template_dir
directory - the directory where templates are stored.
The undo method is used undo the last transaction on the undo stack.
sketchup.undo
Gets the current version of sketchup in decimal form.
version = Sketchup.version
version – the decimal form of the version
version = Sketchup.version if (version) UI.messagebox version else return end
Get the current version of sketchup as a whole number for comparisons.
version = Sketchup.version
version – the whole number form of the version
version = Sketchup.version_number if (version) UI.messagebox version else return end
The write_defaults method is used to write an entry to the specified sub-section section of a .INI file or registry (within the Software > @Last Software > SketchUp section).
result = Sketchup.write_defults “section”, “variable”, “value”
“section” – a section in an .INI or registry
“variable” – the variable to be written within the section
“value” – the value to be written
result – true if successful, false if unsuccessful.
# Write to the Windows registry Sketchup.write_default “Test”, “TesterName”, “Jon Bradley”