123456789_123456789_123456789_123456789_123456789_

Class: Sketchup::AttributeDictionary

Relationships
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Entity
Instance Chain:
self, Enumerable, Entity
Inherits: Sketchup::Entity

Overview

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 AttributeDictionary objects (see the AttributeDictionaries class).

The Entity class is a popular parent class in SketchUp, meaning you can attach attribute dictionaries to almost anything, from geometric items like edges and faces and components to more conceptual things like pages or materials.

Version:

  • SketchUp 6.0

Instance Attribute Summary

Entity - Inherited

#deleted?

The deleted? method is used to determine if your entity is still valid (not deleted by another script, for example.).

#valid?

The #valid? method is used to determine if your entity is still valid (not deleted by another script, for example).

Instance Method Summary

Entity - Inherited

#add_observer

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

#attribute_dictionaries

The attribute_dictionaries method is used to retrieve the AttributeDictionaries collection attached to the entity.

#attribute_dictionary

The attribute_dictionary method is used to retrieve an attribute dictionary with a given name that is attached to an Entity.

#delete_attribute

The #delete_attribute method is used to delete an attribute from an entity.

#entityID

The entityID method is used to retrieve a unique ID assigned to an entity.

#get_attribute

The #get_attribute method is used to retrieve the value of an attribute in the entity’s attribute dictionary.

#inspect

The #inspect method is used to retrieve the string representation of the entity.

#model

The model method is used to retrieve the model for the entity.

#parent

The parent method is used to retrieve the parent of the entity.

#persistent_id

The #persistent_id method is used to retrieve a unique persistent id assigned to an entity.

#remove_observer

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

#set_attribute

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

#to_s

The #to_s method is used to retrieve the string representation of the entity.

#typename

The typename method retrieves the type of the entity, which will be a string such as “Face”, “Edge”, or “Group”.

Instance Method Details

#[](key) ⇒ Object?

The [] method is used to retrieve the attribute with a given key.

Examples:

model = Sketchup.active_model
value = model.set_attribute "testdictionary", "test", 115
attrdicts = model.attribute_dictionaries
attrdict = attrdicts["testdictionary"]

# value will contain 115
value = attrdict["test"]

Parameters:

  • key (String)

    The name of the attribute.

Returns:

  • (Object, nil)

    the attribute stored under your key, or nil if not found

Version:

  • SketchUp 6.0

#[]=(key, value)

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

Creates a new attribute for the given key if needed.

Examples:

model = Sketchup.active_model
value = model.set_attribute("testdictionary", "test", 110)
attrdicts = model.attribute_dictionaries
attrdict = attrdicts["testdictionary"]
value = attrdict["test2"] = 120
p value

Parameters:

Version:

  • SketchUp 6.0

#countInteger

The count method is inherited from the Enumerable mix-in module.

Examples:

model = Sketchup.active_model
model.set_attribute('Example', 'Hello', 'World')
dictionary = model.attribute_dictionary('Example')
number = dictionary.count

Version:

  • SketchUp 2014

#delete_key(key) ⇒ Object?

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

Examples:

create_if_nil = true
model = Sketchup.active_model
attrdict = model.attribute_dictionary("test_dict", create_if_nil)
attrdict["attr_one"] = "one"
attrdict["attr_two"] = "two"

# Delete a key/value pair and get the deleted value.
attrdict = model.attribute_dictionaries['test_dict']
value = attrdict.delete_key("attr_one")

Parameters:

  • key (String)

    The key to be deleted.

Returns:

  • (Object, nil)

    the value of the key

Version:

  • SketchUp 6.0

#each {|key, value| ... }

Note:

Don’t remove content from this collection while iterating over it with #each. This would change the size of the collection and cause elements to be skipped as the indices change. Instead copy the current collection to an array using to_a and then use each on the array, when removing content.

The #each method iterate through all of the attributes.

Examples:

create_if_nil = true
model = Sketchup.active_model
attrdict = model.attribute_dictionary("test_dict", create_if_nil)
attrdict["attr_one"] = "one"
attrdict["attr_two"] = "two"

# Iterates through all attributes and prints the key to the screen.
attrdict = model.attribute_dictionaries['test_dict']
attrdict.each { | key, value |
  puts "#{key} = #{value}"
}

Yields:

  • (key, value)

Yield Parameters:

  • key (String)

    The key of each attribute as it is found.

  • value (Object)

    The value of each attribute as it is found.

See Also:

Version:

  • SketchUp 6.0

#each_key {|key| ... } ⇒ nil

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

Examples:

create_if_nil = true
model = Sketchup.active_model
attrdict = model.attribute_dictionary("test_dict", create_if_nil)
attrdict["attr_one"] = "one"
attrdict["attr_two"] = "two"

# iterates through all attributes and prints the key to the screen
attrdict = model.attribute_dictionaries['test_dict']
attrdict.each_key { |key| puts key }

Yield Parameters:

  • key (String)

    The key of each attribute as it is found.

Version:

  • SketchUp 6.0

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

The #each_pair method is an alias for #each.

Examples:

create_if_nil = true
model = Sketchup.active_model
attrdict = model.attribute_dictionary("test_dict", create_if_nil)
attrdict["attr_one"] = "one"
attrdict["attr_two"] = "two"

# iterates through all attributes and prints the key to the screen
attrdict = model.attribute_dictionaries['test_dict']
attrdict.each_pair { | key, value |
  puts "#{key} = #{value}"
}

Yields:

  • (key, value)

Yield Parameters:

  • key (String)

    The key of each attribute as it is found.

  • value (Object)

    The value of each attribute as it is found.

See Also:

Version:

  • SketchUp 6.0

#keysArray<String>

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

Examples:

create_if_nil = true
model = Sketchup.active_model
attrdict = model.attribute_dictionary "test_dict", create_if_nil
attrdict["attr_one"] = "one"
attrdict["attr_two"] = "two"

# Gets an array of keys
attrdict = model.attribute_dictionaries['test_dict']
keys = attrdict.keys

Returns:

  • (Array<String>)

    an array of keys within the attribute dictionary if successful

Version:

  • SketchUp 6.0

#lengthInteger

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

Examples:

model = Sketchup.active_model
model.set_attribute('Example', 'Hello', 'World')
dictionary = model.attribute_dictionary('Example')
number = dictionary.length

See Also:

Version:

  • SketchUp 6.0

#nameString

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

Examples:

create_if_nil = true
model = Sketchup.active_model
attrdict = model.attribute_dictionary "test_dict", create_if_nil
attrdict["attr_one"] = "one"
attrdict["attr_two"] = "two"

# Show the name.
UI.messagebox attrdict.name

Returns:

  • (String)

    the name of the attribute dictionary if successful

Version:

  • SketchUp 6.0

#sizeInteger

The #size method is an alias of #length.

Examples:

model = Sketchup.active_model
model.set_attribute('Example', 'Hello', 'World')
dictionary = model.attribute_dictionary('Example')
number = dictionary.size

See Also:

Version:

  • SketchUp 6.0

#valuesArray<Object>

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

Examples:

create_if_nil = true
model = Sketchup.active_model
attrdict = model.attribute_dictionary "test_dict", create_if_nil
attrdict["attr_one"] = "one"
attrdict["attr_two"] = "two"

# Gets an array of values
attrdict = model.attribute_dictionaries['test_dict']
values = attrdict.values

Returns:

  • (Array<Object>)

    an array of values within the attribute dictionary if successful

Version:

  • SketchUp 6.0