Class: Sketchup::AttributeDictionary

Inherits:
Entity
  • Object
show all
Includes:
Enumerable

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 Method Summary # collapse

Methods inherited from Entity

#add_observer, #attribute_dictionaries, #attribute_dictionary, #delete_attribute, #deleted?, #entityID, #get_attribute, #inspect, #model, #parent, #persistent_id, #remove_observer, #set_attribute, #to_s, #typename, #valid?

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) ⇒ Object?

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
if (value)
  UI.messagebox value
end

Parameters:

Returns:

  • (Object, nil)

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

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

Returns:

  • (Integer)

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| ... } ⇒ Object

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.

Returns:

  • (nil)

Version:

  • SketchUp 6.0

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

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

Returns:

  • (Integer)

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

Returns:

  • (Integer)

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