SketchUp  Ruby API Reference 

Class Index

Method Index

Developers Guide

Examples

AttributeDictionary class

The AttributeDictionary class allows you to attach arbitrary collections of attributes to a SketchUp entity. The attributes are defined by key/value pairs where the keys are strings.  An Entity or Model object can have any number of AttributeDictionaries.  

Parent: Object

Methods: [] , []= , delete_key, each, each_key, each_pair, keys, length, name, size, values

Example Code: attrdicttests.rb


Instance Methods

Instance Methods


[]

The get value method is used to retrieve the attribute with a given key.

Syntax

value = attributedictionary ["key"]

Arguments

"key" - the name of the attribute

Return Value

Example

model = Sketchup.active_model        
value = model.set_attribute "testdictionary", "test", 115
attrdicts = model.attribute_dictionaries
attrdict = attrdicts["testdictionary"]
value = attrdict["test"] if (attrdict)
UI.messagebox value
else
UI.messagebox "Failure"
end

 

 


[]=

The set value ([]=) method is used to set the value of an attribute with a given key.

Syntax

value = attributedictionary[“key"]=value

Arguments

“key” – the valid key

value – the value to be set

Return Value

value – the value that was set if successful, or false if unsuccessful.

Comments

Creates a new attribute for the given key if needed.

Example

model = Sketchup.active-model
value = model.set_attribute "testdictionary", "test', 115
attrdicts = model.attribute_dictionaries
attrdict = attrdicts["testdictionary"]
value = attrdict["test2"]=120
if (value)
UI.messagebox value else

UI.messagebox "Failure"

end
 

 

 


delete_key

The delete_key method is used to delete an attribute with a given key.

Syntax

value = attributedictionary.delete_key “key”

Arguments

“key” – the key to be deleted

Return Value

value - the value of the key

Example

attrdict = attrdicts[“testdictionary”] 
value = attrdict.delete_key(“testkey”)

 

 


each

The each method is used to iterate through all of the attributes.

Syntax

attributedictionary.each { | key, value |  … }

Arguments

key, value – variables that will hold each key and value as they are found.

Comments

Throws an exception if there are no keys.

Example

attrdict= model.attribute_dictionaries

# iterates through all attributes and prints the key to the screen
attrdict.each { | key, value | UI.messagebox key }

 

 


each_key

The each_key method is used to iterate through all of the attribute keys.

Syntax

attributedictionary.each { | key |  … }

Arguments

key– a variable that will hold each key as they are found.

Comments

Throws an exception if there are no keys.

Example

attrdict= model.attribute_dictionaries

# iterates through all attributes and prints the key to the screen
attrdict.each_key { | key | UI.messagebox key }

 

 


each_pair

An alias for each. See AttributeDictionary.each

Syntax

attributedictionary.each_pair {|key, value | ...}

Arguments

key, value – variables that will hold each key and value as they are found.

Comments

Throws an exception if there are no keys.

Example

attrdict = model.attribute_dictionaries

# iterates through all attributes and prints the key to the screen
attrdict.each_pair { | key, value | UI.messagebox key }

 

 


keys

The keys method is used to retrieve an array with all of the attribute keys.

Syntax

keys = attributedictionary.keys

Return Value

keys – an array of keys within the attribute dictionary if successful

Example

keys = attrdict.keys

if (keys)
# display first key in the array
UI.messagebox keys[0]
else
# code to handle no keys
end

 

 


length

An alias for size. See AttributeDictionary.size.

Syntax

length = attributedictionary.length

Return Value

length – the length (size) of the attribute dictionary.

Example

length = attrdict.length

if (length)
UI.messagebox length
else
# code to handle no attributes
end

 

 


name

The name method is used to retrieve the name of an attribute dictionary.

Syntax

name = attributedictionary.name

Return Value

name – the name of the attribute dictionary if successful

Example

name = attrdict.name

if (name)
UI.messagebox name
else
# code to handle no keys
end

 

 


size

The length method is used to retrieve the size (number of elements) of an attribute dictionary.

Syntax

size = attributedictionary.size

Return Value

size – the size of the attribute dictionary if successful

Example

size = attrdict.size

if (size)
UI.messagebox size
else
# code to handle no attributes
end

 

 


values

The values method is used to retrieve an array of all of the attribute values.

Syntax

values = attributedictionary.values

Return Value

values – an array of values within the attribute dictionary if successful

Example

values = attrdict.values

if (values)
# display first value in the array
UI.messagebox values[0]
else
# code to handle no values
end