# Setting Classification Values

**URL:** <https://forums.sketchup.com/t/setting-classification-values/17169>\
**Category:** Ruby API\
**Created:** [November 5, 2015, 2:21pm UTC](https://forums.sketchup.com/t/setting-classification-values/17169 "2015-11-05T14:21:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim\_foltz](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jim_foltz/32/74787_2.png) [@jim\_foltz](https://forums.sketchup.com/u/jim_foltz)\
**Post date:** [November 5, 2015, 2:21pm UTC](https://forums.sketchup.com/t/setting-classification-values/17169/1 "2015-11-05T14:21:28Z")

</div>

I was reading this topic at SketchUcation about setting Classification values from the Ruby API.

Setting a value requires a path, and the path includes the type of Ifc element. My question is how do you get the ifc type of the component for the path?

[http://sketchucation.com/forums/viewtopic.php?p=574505#p574505](http://sketchucation.com/forums/viewtopic.php?p=574505#p574505)

---

<div class="post-metadata">

**Author:** ![TommyK](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tommyk/32/2998_2.png) [@TommyK](https://forums.sketchup.com/u/TommyK)\
**Post date:** [January 8, 2016, 5:40pm UTC](https://forums.sketchup.com/t/setting-classification-values/17169/2 "2016-01-08T17:40:05Z")

</div>

I am looking for this answer too.

To enlighten you a little, apply a ifcType on to a component, select the component then run this code in your Ruby Console:  
`Sketchup.active_model.selection[0].definition.attribute_dictionaries.each{|dict| dict.each_pair{ |key,value| puts key + ":" + value } }`

You will notice that ifc Type (e.g. ifcWall) for an entity is stored in the attribute\_dictionary of the entity.

That’s the two first elements of the path done, but you want to be able to see what is deeper down the path, like the “Description” and “ObjectType” fields. You will find that the attribute\_dictionary entity itself has attributes (you can call .attribute\_dictionaries method on it). Try the following code in your Ruby console:  
`Sketchup.active_model.selection[0].definition.attribute_dictionaries.each{|dict| dict.each_pair{|key,value| puts key + ":" + value } if !dict.attribute_dictionaries.nil? dict.attribute_dictionaries.each{|dict2| puts dict2.name } end }`

So, all rather complicated, I think. I am working on this myself, so I think I will write a method that extracts all the paths of an entity and post here.

---

<div class="post-metadata">

**Author:** ![jim\_foltz](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jim_foltz/32/74787_2.png) [@jim\_foltz](https://forums.sketchup.com/u/jim_foltz)\
**Post date:** [January 8, 2016, 11:37pm UTC](https://forums.sketchup.com/t/setting-classification-values/17169/3 "2016-01-08T23:37:24Z")

</div>

Each IFC attribute appears to be an `attribute_dictionary`.

```auto
SKETCHUP_CONSOLE.clear

def r(dict, i = 0)
   print " " * i
   puts "_dict_: \"#{dict.name}\""
   i += 4
   dict.each_pair{|k, v|
      print " " * i
      puts "#{k}<#{k.class}> : #{v}<#{v.class}>"
   }
   if !dict.attribute_dictionaries.nil?
      dict.attribute_dictionaries.each{|dict2| r(dict2, i) }
   end
end

model = Sketchup.active_model
sel = model.selection
df = sel[0].definition
ads = df.attribute_dictionaries

ads.each {|d| r(d)}

```

Output

```auto
_dict_: "AppliedSchemaTypes"
    IFC 2x3<String> : IfcBuilding<String>
_dict_: "IFC 2x3"
    SchemaType<String> : http://www.iai-tech.org/ifcXML/IFC2x3/FINAL:IfcBuilding<String>
    _dict_: "ElevationOfRefHeight"
        is_hidden<String> : false<FalseClass>
        _dict_: "instanceAttributes"
            is_hidden<String> : true<TrueClass>
            _dict_: "id"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "path"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "pos"
                attribute_type<String> : choice<String>
                is_hidden<String> : false<FalseClass>
                options<String> : ["http://www.w3.org/2001/XMLSchema:integer"]<Array>
                value<String> : <String>
        _dict_: "IfcLengthMeasure"
            attribute_type<String> : double<String>
            is_hidden<String> : false<FalseClass>
            value<String> : 0.0<Float>
    _dict_: "ElevationOfTerrain"
        is_hidden<String> : false<FalseClass>
        _dict_: "instanceAttributes"
            is_hidden<String> : true<TrueClass>
            _dict_: "id"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "path"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "pos"
                attribute_type<String> : choice<String>
                is_hidden<String> : false<FalseClass>
                options<String> : ["http://www.w3.org/2001/XMLSchema:integer"]<Array>
                value<String> : <String>
        _dict_: "IfcLengthMeasure"
            attribute_type<String> : double<String>
            is_hidden<String> : false<FalseClass>
            value<String> : 0.0<Float>
    _dict_: "BuildingAddress"
        is_hidden<String> : true<TrueClass>
        _dict_: "BuildingAddress"
            attribute_type<String> : choice<String>
            is_hidden<String> : false<FalseClass>
            options<String> : ["http://www.iai-tech.org/ifcXML/IFC2x3/FINAL:IfcPostalAddress"]<Array>
            value<String> : <String>
    _dict_: "LongName"
        is_hidden<String> : false<FalseClass>
        _dict_: "instanceAttributes"
            is_hidden<String> : true<TrueClass>
            _dict_: "id"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "path"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "pos"
                attribute_type<String> : choice<String>
                is_hidden<String> : false<FalseClass>
                options<String> : ["http://www.w3.org/2001/XMLSchema:integer"]<Array>
                value<String> : <String>
        _dict_: "IfcLabel"
            attribute_type<String> : string<String>
            is_hidden<String> : false<FalseClass>
            value<String> : <String>
    _dict_: "CompositionType"
        is_hidden<String> : false<FalseClass>
        _dict_: "instanceAttributes"
            is_hidden<String> : true<TrueClass>
            _dict_: "id"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "path"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "pos"
                attribute_type<String> : choice<String>
                is_hidden<String> : false<FalseClass>
                options<String> : ["http://www.w3.org/2001/XMLSchema:integer"]<Array>
                value<String> : <String>
        _dict_: "IfcElementCompositionEnum"
            attribute_type<String> : enumeration<String>
            is_hidden<String> : false<FalseClass>
            options<String> : ["complex", "element", "partial"]<Array>
            value<String> : complex<String>
    _dict_: "ObjectPlacement"
        is_hidden<String> : true<TrueClass>
        _dict_: "ObjectPlacement"
            attribute_type<String> : choice<String>
            is_hidden<String> : false<FalseClass>
            options<String> : ["http://www.iai-tech.org/ifcXML/IFC2x3/FINAL:IfcObjectPlacement"]<Array>
            value<String> : <String>
    _dict_: "Representation"
        is_hidden<String> : true<TrueClass>
        _dict_: "Representation"
            attribute_type<String> : choice<String>
            is_hidden<String> : false<FalseClass>
            options<String> : ["http://www.iai-tech.org/ifcXML/IFC2x3/FINAL:IfcProductRepresentation"]<Array>
            value<String> : <String>
    _dict_: "ObjectType"
        is_hidden<String> : false<FalseClass>
        _dict_: "instanceAttributes"
            is_hidden<String> : true<TrueClass>
            _dict_: "id"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "path"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "pos"
                attribute_type<String> : choice<String>
                is_hidden<String> : false<FalseClass>
                options<String> : ["http://www.w3.org/2001/XMLSchema:integer"]<Array>
                value<String> : <String>
        _dict_: "IfcLabel"
            attribute_type<String> : string<String>
            is_hidden<String> : false<FalseClass>
            value<String> : <String>
    _dict_: "GlobalId"
        is_hidden<String> : true<TrueClass>
        _dict_: "instanceAttributes"
            is_hidden<String> : true<TrueClass>
            _dict_: "id"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "path"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "pos"
                attribute_type<String> : choice<String>
                is_hidden<String> : false<FalseClass>
                options<String> : ["http://www.w3.org/2001/XMLSchema:integer"]<Array>
                value<String> : <String>
        _dict_: "IfcGloballyUniqueId"
            attribute_type<String> : string<String>
            is_hidden<String> : false<FalseClass>
            value<String> : <String>
    _dict_: "OwnerHistory"
        is_hidden<String> : true<TrueClass>
        _dict_: "OwnerHistory"
            attribute_type<String> : choice<String>
            is_hidden<String> : false<FalseClass>
            options<String> : ["http://www.iai-tech.org/ifcXML/IFC2x3/FINAL:IfcOwnerHistory"]<Array>
            value<String> : <String>
    _dict_: "Name"
        is_hidden<String> : false<FalseClass>
        _dict_: "instanceAttributes"
            is_hidden<String> : true<TrueClass>
            _dict_: "id"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "path"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "pos"
                attribute_type<String> : choice<String>
                is_hidden<String> : false<FalseClass>
                options<String> : ["http://www.w3.org/2001/XMLSchema:integer"]<Array>
                value<String> : <String>
        _dict_: "IfcLabel"
            attribute_type<String> : string<String>
            is_hidden<String> : false<FalseClass>
            value<String> : <String>
    _dict_: "Description"
        is_hidden<String> : false<FalseClass>
        _dict_: "instanceAttributes"
            is_hidden<String> : true<TrueClass>
            _dict_: "id"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "path"
                attribute_type<String> : string<String>
                is_hidden<String> : false<FalseClass>
                value<String> : <String>
            _dict_: "pos"
                attribute_type<String> : choice<String>
                is_hidden<String> : false<FalseClass>
                options<String> : ["http://www.w3.org/2001/XMLSchema:integer"]<Array>
                value<String> : <String>
        _dict_: "IfcText"
            attribute_type<String> : string<String>
            is_hidden<String> : false<FalseClass>
            value<String> : <String>
    _dict_: "href"
        attribute_type<String> : string<String>
        is_hidden<String> : true<TrueClass>
        value<String> : <String>
    _dict_: "ref"
        attribute_type<String> : string<String>
        is_hidden<String> : true<TrueClass>
        value<String> : <String>
    _dict_: "proxy"
        attribute_type<String> : string<String>
        is_hidden<String> : true<TrueClass>
        value<String> : <String>
    _dict_: "edo"
        attribute_type<String> : string<String>
        is_hidden<String> : true<TrueClass>
        value<String> : <String>
    _dict_: "instanceAttributes"
        is_hidden<String> : true<TrueClass>
        _dict_: "id"
            attribute_type<String> : string<String>
            is_hidden<String> : false<FalseClass>
            value<String> : <String>
        _dict_: "path"
            attribute_type<String> : string<String>
            is_hidden<String> : false<FalseClass>
            value<String> : <String>
        _dict_: "pos"
            attribute_type<String> : choice<String>
            is_hidden<String> : false<FalseClass>
            options<String> : ["http://www.w3.org/2001/XMLSchema:integer"]<Array>
            value<String> : <String>

```

---

<div class="post-metadata">

**Author:** ![TommyK](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tommyk/32/2998_2.png) [@TommyK](https://forums.sketchup.com/u/TommyK)\
**Post date:** [January 11, 2016, 11:09pm UTC](https://forums.sketchup.com/t/setting-classification-values/17169/4 "2016-01-11T23:09:59Z")

</div>

You can use the two methods below to output the paths that you need from a ComponentDefinition object. There are shorter ways to create paths, but I personally need the tree for my purposes.

```auto
# Recursive helper method that returns a hash tree of attribute dictionary objects that are set for this entity.
def getClassificationTree(entity, is_classification = false)
    # Check if entity has applied schemas
    tree = {}
    if !is_classification
      if entity.attribute_dictionaries["AppliedSchemaTypes"].nil?
        # No schemas applied, so return empty hash
        return {}
      end
      is_classification = true
      
      type_dict = entity.attribute_dictionaries["AppliedSchemaTypes"]
      type_dict.each_pair{ |schema_name, type|
        dict = entity.attribute_dictionaries[schema_name]
        tree[type_dict] = getClassificationTree(dict, is_classification)
      }

    elsif !entity.attribute_dictionaries.nil?
        entity.attribute_dictionaries.each { |dict|
          tree[dict] = getClassificationTree(dict, is_classification)
        }
    end
    
    return tree
  end

  # Helper function that returns an array of classifier paths from a
  # classficiation tree made with getClassificationTree()
  def getClassificationPaths(classification_tree, current_path = [])
    
    paths = []
    if !classification_tree.empty?
      classification_tree.each { |dict, tree|
        if tree.empty?
          paths << current_path.clone.push(dict.name)
        elsif dict.name == "AppliedSchemaTypes"
          # Add additional Type path
          dict.each{ |schema_name, type|
            next_path = current_path.clone.push(schema_name).push(type)
            paths += getClassificationPaths(tree, next_path)
          }
        else
          next_path = current_path.clone.push(dict.name)
          paths += getClassificationPaths(tree, next_path)
        end
      }
    end
    
    return paths
  end

definition = Sketchup.active_model.selection[0].definition
tree = getClassificationTree(definition)
paths = getClassificationPaths(tree)
```
