Iterate through nested groups/components to get edges

Iterate through nested groups/components to get edges

Postby Pixero » Thu Mar 14, 2019 7:16 pm

I want to get all edges from a selection of one or several nested groups or components.
How?
0
User avatar
Pixero 
 

Re: Iterate through nested groups/components to get edges

Postby Pixero » Fri Mar 15, 2019 2:16 pm

A little further but still not working...
I have attached a simple test scene but only the "free" edges in the first level gets selected.


Code: Select all
mod = Sketchup.active_model
sel = mod.selection
ent = mod.active_entities
edges = []
       
Sketchup.active_model.selection.each{|e|
    if e.is_a?(Sketchup::Edge) #This works
      edges << e
     
    elsif e.is_a?(Sketchup::Group)
      e.entities.each{|edge|
        next unless edge.is_a?(Sketchup::Edge)     
        edges << edge
        }
     
     elsif e.is_a?(Sketchup::ComponentInstance)
        e.definition.entities.each{|edge|
        next unless edge.is_a?(Sketchup::Edge)     
        edges << edge       
        }       
     end
}
sel.clear
sel.add(edges)


Select test.skp
0
User avatar
Pixero 
 

Re: Iterate through nested groups/components to get edges

Postby TIG » Fri Mar 15, 2019 2:47 pm

To process the selection you 'run' it...
Code: Select all
def mine_edges(ents)
  @edges << ents.grep(Sketchup::Edge)
  ents.grep(Sketchup::Group).each{|gp|
    @edges << mine_edges(gp.entities)
  }
  ents.grep(Sketchup::ComponentInstance).each{|co|
    @edges << mine_edges(co.definition.entities)
  }
end
def run()
  mod = Sketchup.active_model
  sel = mod.selection
  @edges = []
  mine_edges(sel)
  @edges.flatten! ### combine sub-arrays into one array
  sel.clear
  sel.add(@edges)
end

Why do you want to 'select' all these nested edges ?
you cannot do much to manipulate selections that span different entities without a BugSplat, however, if you change the last line to do something to the @edges array - e.g. remove their materials or layers, then than is acceptable...
2
TIG
User avatar
TIG 
Global Moderator
 

Re: Iterate through nested groups/components to get edges

Postby Pixero » Fri Mar 15, 2019 4:23 pm

This is just a start of a script and one thing I want to do is to hide all the edges in nested groups and components all the way down.
Selecting is just to see if it works.

I ran your code but it also selecting the sub groups and components.
0
User avatar
Pixero 
 

Re: Iterate through nested groups/components to get edges

Postby TIG » Fri Mar 15, 2019 5:12 pm

I thought that's what you wanted, otherwise your earlier code should select all edges in the active_entities AND within the groups/instances in that context.
My code collects ALL edges in the context and any edges within any groups/instances in that context AND nested deeper: it should not be selecting anything more than edges ?

So instead of selecting the collected edges you just need to iterate @edges and set each edge.hidden=true ?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Iterate through nested groups/components to get edges

Postby Pixero » Fri Mar 15, 2019 5:21 pm

This is a screenshot after running your code.
As you can see the inner components and groups are also selected.

Skärmklipp.JPG
0
User avatar
Pixero 
 

Re: Iterate through nested groups/components to get edges

Postby TIG » Fri Mar 15, 2019 5:37 pm

I do see it, and if you type @edges after it's run the list does include some component-instances.
This is weird since the code specifically collects just Edges !
BUT it's because the 'miner' was exiting without a specific 'empty' return statement, and therefore it also added the last thing it processed == the instances etc.
By forcing it to return [] [an empty array], then that is lost by the @edges.flatten! command at the end - so it now works as expected - @edges is an array of only edges...
Here's the fixed code...
Code: Select all
def mine_edges(ents)
  @edges << ents.grep(Sketchup::Edge)
  ents.grep(Sketchup::Group).each{|gp|
    @edges << mine_edges(gp.entities)
  }
  ents.grep(Sketchup::ComponentInstance).each{|co|
    @edges << mine_edges(co.definition.entities)
  }
  return []
end
def run()
  mod = Sketchup.active_model
  sel = mod.selection
  @edges = []
  mine_edges(sel)
  @edges.flatten! ### combine sub-arrays into one array
  sel.clear
  sel.add(@edges)
end
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Iterate through nested groups/components to get edges

Postby Pixero » Fri Mar 15, 2019 5:49 pm

Thank you. That works as I wanted.
0
User avatar
Pixero 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: bukyo1126 and 8 guests

Visit our sponsors: