Collecting groups and components with certain material

Collecting groups and components with certain material

Postby Pixero » Thu May 30, 2019 9:23 pm

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.
0
Last edited by Pixero on Thu May 30, 2019 9:47 pm, edited 1 time in total.
User avatar
Pixero 
 

Re: Collecting groups and components with certain material

Postby TIG » Thu May 30, 2019 9:45 pm

Code: Select all
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 !
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Collecting groups and components with certain material

Postby Pixero » Thu May 30, 2019 9:50 pm

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).
0
User avatar
Pixero 
 

Re: Collecting groups and components with certain material

Postby TIG » Thu May 30, 2019 9:55 pm

Well !

You need to replace ss=model.selection with ss=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 ??
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Collecting groups and components with certain material

Postby Pixero » Thu May 30, 2019 10:05 pm

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?
0
User avatar
Pixero 
 

Re: Collecting groups and components with certain material

Postby TIG » Thu May 30, 2019 10:28 pm

'Containers' are groups or component-definitions, which 'contain' entities - they are usually referred to as 'entities-collections' - but 'containers' is synonymous...
1
TIG
User avatar
TIG 
Global Moderator
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: KoenVerschuere, TMRick and 8 guests

Visit our sponsors: