stuarth wrote:The plugin's fairly straight forward. Essentially it's traversing the model and counting components and material usage.
You should not really need to "traverse" the model entities.
- Code: Select all
numc = 0
numi = 0
matls = 0
model.definitions.each {|cdef|
if cdef.instances.size > 0
numc += 1
numi += cdef.instances.size
cdef.instances.each {|i| matls += 1 unless i.material.nil? }
end
}
Each component definition keeps a list of all it's instances, no matter what their nesting level.
ADD: By iterating the
Sketchup::DefinitionList collection object, for the model,... you skip having to ignore entity types, that you do not need to test, ie Edges, etc. And your iteration should be much faster. (I suspect the random beach-balling occurs with larger models, or those that may return entity types during the iteration that you did not expect, and your code may not handle well. ie Images, EdgeLoop, etc.)
Also.. as each
Sketchup::ComponentInstance object
shares it's
definition's entities with
all it's sibling instances (ie, the instance does not have unique drawing elements of it's own,) ... each used component's entities collection need only be iterated
ONCE, to see if there are any "hardset" materials assigned to faces, etc. (Those with default material ==
nil will get the material assigned to the instance itself.)
So access to a definition entities collection, is quickest via the model's
Sketchup::DefinitionList.
ADD #2: Do not use
obj.typename == "Typestring" as it is
very slow.. instead, use
obj.is_a?(Sketchup::ClassName)