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?
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.
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)