2010-05-26 29 views
1

我正在制作一个插件来总结素描中所有材质的面积。 我已经成功地获得了所有的面孔等,但现在组件进入了图片。计算零部件材料的总计面积Google Sketchup

即时通讯使用术语单或多级组件,因为我不知道任何更好的方式来解释在组件内发生的一个组件,等等。

我注意到,一些组件也有更多的我,而不仅仅是1级。因此,如果您进入一个组件内部,则可能会在该组件内嵌入也含有材料的组件。所以我想要总结一个特定组件的所有材料,并获得组件内的所有“递归”材料(如果有的话)。

那么,如何计算组件(单层或多层)内所有材料的面积?

回答

2

这是我会做的,让我们假设你循环遍历所有实体并检查实体的类型。

if entity.is_a? Sketchup::ComponentInstance 
    entity.definition.entities.each {|ent| 
    if ent.is_a? Sketchup::Face 
     #here do what you have to do to add area to your total 
    end 
    } 
end 

你可以做同样的一组:

if entity.is_a? Sketchup::Group 
    entity.entities.each {|ent| 
    if ent.is_a? Sketchup::Face 
     #here do what you have to do to add area to your total 
    end 
    } 
end 

希望它可以帮助 拉吉斯拉夫

2

拉吉斯拉夫的例子并没有深入到各个层面。

为此你需要一个递归方法:

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