|
SketchUp Ruby API Reference |
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 |
The get value method is used to retrieve the attribute with a given key.
value = attributedictionary ["key"]
"key" - the name of the attribute
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.
value = attributedictionary[“key"]=value
“key” – the valid key
value – the value to be set
value – the value that was set if successful, or false if unsuccessful.
Creates a new attribute for the given key if needed.
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
The delete_key method is used to delete an attribute with a given key.
value = attributedictionary.delete_key “key”
“key” – the key to be deleted
value - the value of the key
attrdict = attrdicts[“testdictionary”]
value = attrdict.delete_key(“testkey”)
The each method is used to iterate through all of the attributes.
attributedictionary.each { | key, value | … }
key, value – variables that will hold each key and value as they are found.
Throws an exception if there are no keys.
attrdict= model.attribute_dictionaries
# iterates through all attributes and prints the key to the screen
attrdict.each { | key, value | UI.messagebox key }
The each_key method is used to iterate through all of the attribute keys.
attributedictionary.each { | key | … }
key– a variable that will hold each key as they are found.
Throws an exception if there are no keys.
attrdict= model.attribute_dictionaries
# iterates through all attributes and prints the key to the screen
attrdict.each_key { | key | UI.messagebox key }
An alias for each. See AttributeDictionary.each
attributedictionary.each_pair {|key, value | ...}
key, value – variables that will hold each key and value as they are found.
Throws an exception if there are no keys.
attrdict = model.attribute_dictionaries
# iterates through all attributes and prints the key to the screen
attrdict.each_pair { | key, value | UI.messagebox key }
The keys method is used to retrieve an array with all of the attribute keys.
keys = attributedictionary.keys
keys – an array of keys within the attribute dictionary if successful
keys = attrdict.keys
if (keys)
# display first key in the array
UI.messagebox keys[0]
else
# code to handle no keys
end
An alias for size. See AttributeDictionary.size.
length = attributedictionary.length
length – the length (size) of the attribute dictionary.
length = attrdict.length
if (length)
UI.messagebox length
else
# code to handle no attributes
end
The name method is used to retrieve the name of an attribute dictionary.
name = attributedictionary.name
name – the name of the attribute dictionary if successful
name = attrdict.name
if (name)
UI.messagebox name
else
# code to handle no keys
end
The length method is used to retrieve the size (number of elements) of an attribute dictionary.
size = attributedictionary.size
size – the size of the attribute dictionary if successful
size = attrdict.size
if (size)
UI.messagebox size
else
# code to handle no attributes
end
The values method is used to retrieve an array of all of the attribute values.
values = attributedictionary.values
values – an array of values within the attribute dictionary if successful
values = attrdict.values
if (values)
# display first value in the array
UI.messagebox values[0]
else
# code to handle no values
end