Iterate selection
-
As the title implies I'm trying to iterate a selected component to retrieve the subcomponents. I use this code to iterate the whole model but can't figure out how to do it for only selected components.
@component_list=definitions.find_all() {|e| e.entities.find()}.map() {|e| e.instances }.flatten
-
You can do it recursively
def iterate_ents(ents = Sketchup.active_model.selection) ents.each { |e| if e.is_a?(Sketchup;;Group) iterate_ents(e.entities) next end if e.is_a?(Sketchup;;ComponentInstance) iterate_ents(e.definition.entities) next end # Do something here... } endSo to get all sub-components you can do it this way:
def get_all_sub_components(ents = Sketchup.active_model.selection) sub_components = [] ents.each { |e| if e.is_a?(Sketchup;;ComponentInstance) sub_components.concat get_all_sub_components(e.definition.entities) sub_components << e elsif e.is_a?(Sketchup;;Group) sub_components.concat get_all_sub_components(e.entities) end } sub_components.uniq end -
Thanks Anton_s!
Even though I'm still trying to figure out what and how your code works it does work perfectly!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement