π‘ LightUp 7.1 | SketchUp's only real-time renderer that uses object-based rendering
Download Trial
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... } end
So 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