class AttributeDictionary

 

Parent: Object

Class Index

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

 

Sample Code: attrdicttests.rb

 

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.  
 

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:

value - the value associated with the "key" if successful

Comments:

 

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 that was deleted if successful

Comments:

 

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.

Return Value:

 

Comments:

Throws an exception if there are no keys.

Example:

dictionaries = 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.

Return Value:

 

Comments:

Throws an exception if there are no keys.

Example:

dictionaries = 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.

Return Value:

 

Comments:

Throws an exception if there are no keys.

Example:

dictionaries = 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

Arguments:

 

Return Value:

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

Comments:

 

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

Arguments:

 

Return Value:

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

Comments:

 

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

Arguments:

 

Return Value:

name – the name of the attribute dictionary if successful

Comments:

 

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

Arguments:

 

Return Value:

size – the size of the attribute dictionary if successful

Comments:

 

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

Arguments:

 

Return Value:

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

Comments:

 

Example:

values = attrdict.values

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