Setting Classification Values

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

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.

Each IFC attribute appears to be an attribute_dictionary.

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

_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>


1 Like

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.

# 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)