Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
π£οΈ Road Profile Builder | Generate roads, curbs and pavements easily Download
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!
Advertisement