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"]

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

Parameters:

  • key

    The name of the attribute.

Returns:

  • attribute - 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

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

Parameters:

  • key

    The valid key.

  • value

    The value to be set.

Returns:

  • value - 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)

    the size of the attribute dictionary.

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"

# Gets an array of values
attrdict = model.attribute_dictionaries['test_dict']
value = attrdict.delete_key("attr_one")

Returns value - the value of the key

Parameters:

  • key

    The key to be deleted.

Returns:

  • value - the value of the key

Version:

  • SketchUp 6.0

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

An alias for each. See AttributeDictionary.each.

Throws an exception if there are no 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_pair { | key, value |
  UI.messagebox key.to_s + '=' + value.to_s
}

Yields:

  • (key, value)

    @yieldparam key The key of each attribute as it is found. @yieldparam value The value of each attribute as it is found.

Returns:

  • nil

Version:

  • SketchUp 6.0

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

The each_key method is used to iterate through all of the attribute keys. This is the same method as AttributeDictionary.each.

Throws an exception if there are no 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 | UI.messagebox key }

Yields:

  • (key)

    A variable that will hold each key as they are found.

Returns:

  • nil

Version:

  • SketchUp 6.0

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

An alias for each. See AttributeDictionary.each.

Throws an exception if there are no 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_pair { | key, value |
  UI.messagebox key.to_s + '=' + value.to_s
}

Yields:

  • (key, value)

    @yieldparam key The key of each attribute as it is found. @yieldparam value The value of each attribute as it is found.

Returns:

  • nil

Version:

  • SketchUp 6.0

#keysObject

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:

  • keys - 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.

The #size method is an alias for #length.

Examples:

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

Returns:

  • (Integer)

    the size of the attribute dictionary if successful

Version:

  • SketchUp 6.0

#nameObject

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:

  • name - the name of the attribute dictionary if successful

Version:

  • SketchUp 6.0

#sizeInteger

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

The #size method is an alias for #length.

Examples:

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

Returns:

  • (Integer)

    the size of the attribute dictionary if successful

Version:

  • SketchUp 6.0

#valuesObject

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:

  • keys - an array of keys within the attribute dictionary if successful

Version:

  • SketchUp 6.0