拉吉斯拉夫的例子并没有深入到各个层面。
为此你需要一个递归方法:
def sum_area(material, entities, tr = Geom::Transformation.new)
area = 0.0
for entity in entities
if entity.is_a?(Sketchup::Group)
area += sum_area(material, entity.entities, tr * entity.transformation)
elsif entity.is_a?(Sketchup::ComponentInstance)
area += sum_area(material, entity.definition.entities, tr * entity.transformation)
elsif entity.is_a?(Sketchup::Face) && entity.material == material
# (!) The area returned is the unscaled area of the definition.
# Use the combined transformation to calculate the correct area.
# (Sorry, I don't remember from the top of my head how one does that.)
#
# (!) Also not that this only takes into account materials on the front
# of faces. You must decide if you want to take into account the back
# size as well.
area += entity.area
end
end
area
end