Collecting groups and components with certain material
-
I’m looking for a way to make an array of all groups and components that have a certain material either on the group or the faces inside.
If there are nested groups/components only the innermost groups containing that material should be collected. -
def collect_it() model=Sketchup.active_model ss=model.selection mats=model.materials @mat=mats['SomeName'] ### perhaps check if it exists ?? @collection=[] ss.grep(Sketchup;;Group).each{|e| @collection << e if e.material==@mat do_group(e) } ss.grep(Sketchup;;ComponentInstance).each{|e| @collection << e if e.material==mat do_inst(e) } puts @collection return @collection end def do_group(e) e.entities.each{|e| @collection << e if e.respond_to?(;material) && e.material==@mat if e.is_a?(Sketchup;;Group) do_group(e) elsif e.is_a?(Sketchup;;ComponentInstance) do_inst(e) end } end def do_inst(e) e.definition.entities.each{|e| @collection << e if e.respond_to?(;material) && e.material==@mat if e.is_a?(Sketchup;;Group) do_group(e) elsif e.is_a?(Sketchup;;ComponentInstance) do_inst(e) end } end
Now running
collect_it()
returns@collection
, which includes references to all things within the selection [groups/instances AND nested-equivalents - including faces] using@mat
from the name given...
But, beware that they are in different entities collections, so some changes could BugSplat ! -
Wow, that was fast. Did you see my edited message about innermost groups? In this case I’m not going through a selection but looking at all groups in the model with that material.
What I want to do is to set an attribute for all the groups and component instances ( not definition). -
Well !
You need to replace
ss=model.selection
withss=model.entities
to iterate the whole model.Do you want to find only the most deeply nested things ?
Or just those below the top-level ?Also I think you want to collect 'containers' as they get your attributes ??
Seems like you need to collect the 'entities' and determine those.
Then iterate through those and get the things using the specified material ?? -
Think of for example a window component which consists of the frame and other parts that can be nested groups/components. In that case I want to only collect the groups/components that have let’s say glass as material and set an attribute to them.
Sorry I don’t understand what you mean by containers? -
'Containers' are groups or component-definitions, which 'contain' entities - they are usually referred to as 'entities-collections' - but 'containers' is synonymous...
Advertisement