SketchUp  Ruby API Reference 

Class Index

Method Index

Developers Guide

Examples

Model class

This is the interface to a SketchUp model.  

Parent: Object

Methods: abort_operation, active_entities, active_layer, active_layer=, active_view, add_note, add_observer, attribute_dictionaries, attribute_dictionary, behavior, bounds, close_active, commit_operation, definitions, description, description=, entities, export, get_attribute, get_datum, guid, import, latlong_to_point, layers, list_datums, materials, modified?, options, pages, path, place_component, point_to_latlong, point_to_utm, raytest, remove_observer, rendering_options, save, save_thumbnail, select_tool, selection, set_attribute, set_datum, shadow_info, start_operation, styles, title, tools, utm_to_point, valid?

Example Code: modeltests.rb

Instance Methods


abort_operation

The abort_operation method aborts the current operation started with the start_operation method.

Syntax

status = model.abort_operation

Return Value

status – true if successful, false if unsuccessful

Comments

The abort_operation method is normally called from inside of a rescue clause to cancel an operation if something goes wrong.

Example

status = model.abort_operation

 

 


active_entities 

The active_entities method is used to retrieve an Entities object containing all of the entities in the active model or component (if you are within a component)

Syntax

entities = model.active_entities

Return Value

entities – an entities object

Example

model=Sketchup.active_model

entities=model.active_entities

 

 


active_layer 

The active_layer method retrieves the active Layer.

Syntax

layer = model.active_layer

Return Value

layer – a layer object containing the currently active layer

Comments

The default layer in SketchUp is layer 0.

Example

model = Sketchup.active_model
layers = model.layers
layers.add “My Layer”
activelayer = model.active_layer=layers[1]
layer=model.active_layer

 

 


active_layer = 

The active_layer= method sets the active Layer object.

Syntax

activelayer = model.active_layer=layer

Arguments

layer – the layer object to be set as the active layer

Return Value

activelayer – the newly set active layer object

Example

model = Sketchup.active_model
layers = model.layers
layers.add “My Layer”
activelayer = model.active_layer=layers[1]
layer=model.active_layer
name = layer.name

if (name != “My Layer”)
UI.messagebox “Failure: wrong Layer”
else
UI.messagebox name
end

 

 


active_view 

The active_view method returns the active View object for this model.

Syntax

view = model.active_view

Return Value

view – a view object

Example

model = Sketchup.active_model
view = model.active_view

 

 


add_observer

The add_observer method is used to add an observer to the current object.

Syntax

status = object.add_observer observer

Arguments

observer - an observer

Return Value

true if successful, false if unsuccessful.

Example

 

 


add_note 

The add_note method is used to add a text note to the model.

Syntax

note = Sketchup.active_model.add_note “note” x, y

Arguments

“note” – a string note

x – a distance along the x axis between 0 and 1

y – a distance along the y axis between 0 and 1

Return Value

note – a note object or an exception if it is unsuccessful.

Comments

The position of the note is given as relative window positions between 0 and 1. For example, the following command would create a note that start one-tenth of the way down the screen from the upper left corner of the window.

Example

model = Sketchup.active_model

begin
note = mode.add_note “Test Note”, 0.1, 0.1
rescue
UI.messagebox $!.message
end

 

 


attribute_dictionaries 

The attribute_dictionaries method retrieves the AttributeDictionaries object that is associated with the Model.

Syntax

attributedictionaries = model.attribute_dictionaries

Return Value

attributedictionaries – an array of attribute dictionary if successful, nil if there are no attribute dictionaries

Example

model = Sketchup.active_model
attdicts = model.attribute_dictionaries

if (attdicts)
# code to do something if attribute dictionaries exist (usually you parse the array of
# dictionaries
else
# code to do something if attribute dictionaries do not exist
end

 

 


attribute_dictionary 

The attribute_dictionary method retrieves an attribute dictionary with a given name that is associated with the model.

Syntax

attributedictionary = model.attribute_dictionary “name” create

Arguments

“name” – the name of the dictionary you are attempting to retrieve

create – (optional) if set to true an attribute dictionary of the given “name” will be created if not found

Return Value

attributedictionary – an attribute dictionary object if successful, nil if unsuccessful

Comments

The model and entities do not have attribute dictionaries by default. You must create model-related attribute dictionaries using the create flag.

 

 


behavior 

The behavior method retrieves the behavior of the model.

Syntax

behavior = model.behavior

Return Value

behavior = a behavior object for the model if successful

Example

model = Sketchup.active_model
behavior = model.behavior
if (behavior)
UI.messagebox behavior
else
UI.messagebox "Failure"
end

 

 


bounds 

The bounds method retrieves the bounding box of the model.

Syntax

boundingbox = model.bounds

Return Value

boundingbox = a bounding box for the model if successful

Example

model = Sketchup.bounds
bounds = model.bounds if (bounds)

length = bounds.length

UI.messagebox length

else

UI.messagebox “Failure: No bounding box found”

end

 

 


close_active

The close_active method is used to close the currently active (open) group or component.

Syntax

status = close_active

Return Value

status - true if successful, false if unsuccessful.

Example

 

 


commit_operation 

The commit_operation method commits an operation for undo.

Syntax

status = model.commit_operation

Return Value

status – true if successful, false if unsuccessful

Comments

The commit_operation method is normally called at the end of a method to commit the operation that the method performs.  

Example

status = model.commit_operation

 

 


definitions 

The definitions method retrieves a definition list containing all of the component definitions in the model.

Syntax

definitionslist = model.definitions

Return Value

definitions – a definitions list if successful.

Comments

The returned definitions can be empty.

Example

model = Sketchup.active_model
deflist = model.definitions
# parse array of definitions and act upon the definitions found

 

 


description 

The description method retrieves a description of the model as found in the Model Info > Files panel.

Syntax

description = model.description

Return Value

description – a description if successful.

Comments

The returned description can be empty. The default description for all models is empty.

Example

model = Sketchup.active_model

desc = model.description

 

 


description=

The description= method sets the description of the model.

Syntax

description = model.description “description”

Arguments

“description” – (optional) the description string to be set.

Return Value

description – the currently set description string.

Comments

If you do not include a description string, the description is cleared and an empty description is returned.

Example

model = Sketchup.active_model

description = model.description=”This is a model of a house on the North West Corner of 10th and Dolores Street in Carmel, California”

 

 


entities 

The entities method returns an Entities object containing an array of entities in the model.

Syntax

entities = model.entities

Return Value

entities – an Entities object if successful

Comments

If no entities are in your model, this method returns an empty Entities object (an empty array)

Example

model = Sketchup.active_model
entities = model.entities # Add a group to the entities in the model

entities = entities.addgroup

 

 


export

The export method is used to export to the specified file format by recognizing the given file extension and calling the appropriate exporter.

Syntax

status = model.import "filename", show_summary

Arguments

 “filename” – the name of the file to import.

show_summary - true if you want to show a summary window, false if you do not want to show a summary window.

Return Value

status – true if successful, false if unsuccessful

Example

 

 


get_attribute 

The get_attribute method gets the value of an attribute that in the AttributeDictionary with the given name.

Syntax

value = model.get_attribute “dictionaryname”, key, defaultvalue

Arguments

“dictionaryname” – the name of the dictionary containing the value

“key” – the key containing the value

defaultvalue – (optional) default value that will be returned if a value does not exist

Return Value

value – the value for a given key in the given dictionary if a value exists; the default value if a defaultvalue is provided and the value does not exist; nil if the value does not exist and no defaultvalue is provided.

 

 


get_datum 

the get_datum method retrieves the datum, in the form of a string, used in UTM conversions.

Syntax

datum = model.get_datum

Return Value

datum – a datum represented as a string if successful.

Example

model = Sketchup.active_model

datum = model.get_datum

 

 


guid 

The guid method retrieves the globally unique identifier, in the form of a string, for the Model.

Syntax

guid = model.guid

Return Value

guid – a globally unique identifier, in the form of a string, for the model

Example

model = Sketchup.active_model

guid = model.guid

 

 


import

The import method is used to load a file by recognizing the file extension and calling appropriate importer.

Syntax

status = model.import "filename", show_summary

Arguments

“filename” – the name of the file to import.

show_summary - true if you want to show a summary window, false if you do not want to show a summary window.

Return Value

status – true if successful, false if unsuccessful

Example

 

 


latlong_to_point 

The latlong_to_point method convert a latitude and longitude to a Point3d object in the model.

Syntax

point = model.latlong_to_point latlong

Arguments

latlong – a latlong object

Return Value

point – a point3d object if successful, false if unsuccessful.

Example

latitude = [40.01700, 105.28300]
model = Sketchup.active_model

begin
latlong = Geom::LatLong.new(latitude)
rescue
UI.messagebox $!.message
end

point = model.latlong_to_point latlong

 

 


layers 

The layers method retrieves a collection of all Layers objects in the model.

Syntax

layers = model.layers

Return Value

layers – a Layers object containing a collection of Layers in the model

Example 

model = Sketchup.active_model

layers = model.layers

 

 


list_datums 

This method retrieves an Array of all of the datums recognized by SketchUp.

Syntax

datums = model.list_datums

Return Value

datums – An Array object containing the datums supported by SketchUp

Example

model = Sketchup.active_model
datums = model.list_datums

if (datums)
# display a paragraph with all of the datums
UI.messagebox datums
end

 

 


materials 

The materials method returns a collection of all of the Materials in the model.

Syntax

materials = model.materials

Example 

model = Sketchup.active_model
materials = model.materials

if (materials)
# List number of materials
count = materials.count
UI.messagebox count
end

 

 


modified? 

The modified? method determines if the Model has been modified since the last save.

Syntax

status = model.modified?

Return Value

status = true if the model has been modified since last save (and requires a save), false if the model has not been modified.

Example

model = Sketchup.active_model
entities = model.active_entities

# Add a group to force the status return value to be true
entities.add_group
status = model.modified? if (status)

# If status is true, the following message is displayed

UI.messagebox “You better save soon.”

end

 

 


options 

The options method retrieves the options manager that defines the options settings for the model.

Syntax

optionsmanager = model.options

Return Value

optionsmanager – an OptionsManager object containing one or more options providers if successful.

Comments:

There are 5 default options managers. These are: PageOptions, AxisOptions, SlideshowOptions, UnitsOptions, and NamedOptions.

Example

model = Sketchup.active_model
optionsmanager = model.options

if (optionsmanager)
# If an options manager is returned, iterate through each option provider within the                  
# options
# options manager and display its name.
size = optionsmanager.size
UI.messagebox size
for option in optionsmanager
option = optionsmanager[x]
name = optionsmanager.name
UI.messagebox name
x = x + 1
end
else
UI.messagebox “Failure”
end

 

 


pages 

The pages method retrieves a Pages object containing all of the pages in the Model.

Syntax

pages = model.pages

Return Value

pages – returns a Pages object with 0 or more pages.

Example

model = Sketchup.active_model
pages = model.pages
# Add a page to the Pages object
page = pages.add
size = pages.size
# Size should be 1
UI.messagebox size

 

 


path 

The path method retrieves the path of the file from which the model was opened.

Syntax

path = model.path

Return Value

path – an string containing the path for the currently opened model.

Comments

An empty string is returned for a new model (one which has not been saved and opened)

Example

model = Sketchup.active_model
# Return the current path (empty)
path = model.path
UI.messagebox path

 

 


place_component 

The place_component method places a new component in the Model using the component placement tool.

The first argument is a ComponentDefinition. There is a second optional argument. If it is true, it says to stay in the tool and place multiple instances of the component.

Syntax

status = model.placecomponent componentdefinition, repeat

Arguments

componentdefinition – A component definition object containing the definition (blueprint) for the component

repeat – if set to true, stay in the component placement tool and place multiple components

 

 


point_to_latlong 

The point_to_latlong method converts a point in the model to a LatLong so that you can get its latitude and longitude.

Syntax

latlong = model.point_to_latlong point 
 

Arguments

point – a Point3d object

Return Value

latlong – a LatLong object

Comments

This method uses the location information set in ShadowInfo.

Example

coordinates = [10,10,10]
model = Sketchup.active_model
begin
point = Geom::Point3d.new(coordinates)
rescue
UI.messagebox $!.message
end latlong = model.point_to_latlong(point) if (latlong)

# if a latlong object was returned, convert contents to a string

latlongvalue = latlong.to_s

UI.messagebox latlongvalue

else

UI.messagebox “Failure”

end

 

 


point_to_utm 

This method converts a Point3d object in the Model to UTM coordinates.

Syntax

utm = model.point_to_utm(point)

Arguments

point – A Point3d object

Return Value

utm – a UTM object

Comments

This method uses the location information set in ShadowInfo. See also UTM.

Example

coordinates = [10,10,10]
model = Sketchup.active_model begin
point = Geom::Point3d.new(coordinates)
rescue
UI.messagebox $!.message
end utm = model.point_to_utm(point)

if (utm)

# if a utm object was returned, convert contents to a string

utmvalue = utm.to_s

UI.messagebox utmvalue

else

UI.messagebox “Failure”

end

 

 


raytest 

The raytest method is used to cast a ray (line) through the model and return the first thing that the ray hits.

Syntax

item - model.raytest ray

Arguments

ray - a two element array containing a point and a vector

Return Value

item - an array of two values. The first value is a Point3d where the item that the ray passed through exists. The second element is the instance path array of the entity that the ray hit. For example, if the ray hits a face that contained by a component instance the instance path would be [Component1]. If the ray hit a face that is contained by a component instance, which is contained by another component instance and so on, the instance path would be [Component1, Component2, Componten3...].

Comments

A ray is a two element array containing a point and a vector [Geom::Point3d(), Geom::Vector3d()]. The point defines the start point of the ray and the vector defines the direction.

 

 



remove_observer

The remove_observer method is used to remove an observer from the current object.

Syntax

status = object.remove_observer observer

Arguments

observer - an observer

Return Value

true if successful, false if unsuccessful.

Example

 

'


rendering_options

The rendering_options method retrieves the RenderingOptions object for this Model.

Syntax

renderingoptions = model.rendering_options

Return Value

renderingoptions – a RenderingOptions object

Example 

model = Sketchup.active_model
begin
renderingoptions = model.rendering_options
rescue
UI.messagebox $!.message
end
if (renderingoptions)
option = renderingoptions[0]
UI.messagebox option           
else
UI.messagebox "Failure"
end

 

 


save 

This method is used to save the model to a file.

Syntax

status = model.save “filename”

Arguments

“filename” – the name of the file to save

Return Value

status – true if successful, false if unsuccessful

Example

model = SketchUp.active_model
status = model.save “mysketchup.skp”

 

 


save_thumbnail 

The save_thumbnail method is used to save a thumbnail image to a file.

Syntax

status = model.save_thumbnail "filename"

Arguments

"filename" - the name of the file, with extension, to save the thumbnail as

Return Value

status - true if successful, false if unsuccessful

Comments

Returns nil if it is an internal component.

Example

depth = 100
width = 100
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the face to the entities in the model
face = entities.add_face pts
status = model.save_thumbnail "testthumbnail2.jpg"
UI.messagebox status

 

 


select_tool 

This method is used to select a SketchUp Tool object  as the active tool. You must implement the SketchUp Tool interface to create a tool prior to calling this method.

Syntax

tool = model.select_tool tool

Arguments

tool – the Tool object you want to select.

Return Value

tool – A Tool object.

Comments

The select tool is returned if you pass nil to the select_tool method. You must implement the SketchUp Tool interface to create a tool, prior to calling this method, and then instance the tool implementation and pass the object to this method.

Example

model = Sketchup.active_model
tool = model.select_tool nil

 

 


selection 

This method retrieves a  Selection object for the model, containing the currently selected entities.

Syntax

selection = model.selection

Return Value

selection – A Selection object with 0 or more entities that are currently selected.

Example 

model = Sketchup.active_model
selection = model.selection

 

 


set_attribute 

This method is used to set the value of an attribute in an attribute dictionary with the given name.

Syntax

value = model.set_attribute “attributedictionaryname”, “key”, “value”

Arguments

“attributedictionaryname” – the name of the attribute dictionary whose attribute you wish to set

“key” – the attribute name

“value” – the value to set

Return Value

value – the value that was set

Comments

This method can be used create a new AttributeDictionary object, if needed.

Example 

model = Sketchup.active_model

value = model.set_attribute “testdictionary” “test” “115”

 

 


set_datum 

This method sets the datum used in conversions between the internal coordinate system and UTM.

Syntax

value = model.setdatum "datum"

Comments

The default datum is WGS84. You can use the method list_datums to get a list of all of the datums supported in SketchUp. If you pass an invalid datum to set_datum, set_datum returns the default datum.

Example

model = Sketchup.active_model

value = model.set_datum “Adindan”

 

 


shadow_info 

This method is used to retrieve the shadow information for the Model.

Syntax

shadowinfo = model.shadow_info

Return Value

shadowinfo – a ShadowInfo object.

Example

model = Sketchup.active_model

shadowinfo = model.shadow_info

 

 


start_operation 

The start_operation method is used to  notify Edit > Undo that a new operation (which can be undone) is starting.

Syntax

status = model.start_operation

Return Value

status – true if successful, false if unsuccessful

Comments

The String is a description for the operation that is displayed adjacent to the Edit > Undo menu item.

Example

status = model.start_operation

 

 


styles

The styles method retrieves the styles associated with the model.

Syntax

styles = model.styles

Return Value

styles – the Styles object if successful

Example

 

 

 


title 

The tile method retrieves the name of the model.

Syntax

title = model.title

Return Value

title – the title of the model or an empty string (if the title is not set)

Example

model = Sketchup.active_model

title = model.title

 

 


tools

The tools method is used to retrieve the current Tools object.

Syntax

tools = model.tools

 

Return Value

tools - a Tools object.

Example

 

 


utm_to_point 

The utm_to_point method converts a position given in UTM coordinates to a Point3d in the Model.

Syntax

point = model.utm_to_point utm

Arguments

utm – a UTM object.

Return Value

point - A Point3d object.

Example 

point = [10,10,10]
model = Sketchup.active_model
begin
point = Geom::Point3d.new(point)
rescue
UI.mesagebox $!.message
end # Create a UTM object from the point
utm = model.point_to_utm point
# Pass that UTM object right back into utm_to_point to get the UTM back again
point = model.utm_to_point utm if (point)

pointvalue = point.to_s

UI.messagebox pointvalue

else

UI.messagebox “Failure”

end

 

 


valid? 

The valid method determines if the model is valid.

Syntax

status = model.valid?

Return Value

status – true if the model is valid, false if it is invalid.

Example

model = Sketchup.active_model

valid = model.valid?