Count and manipulate groups nested in a component

Hello there,

After developing dynamic components for a few months I’m diving into Ruby coding to learn to manipulate geometries more effectively. I am currently reading Ruby guides and Sketchup Ruby API, but couldn’t find a solution to my problem, and decided to ask help to the community.

My model is composed of several components, and each is composed of 3 groups named LOD0, LOD1 and LOD2 that contain geometries of the component with different level of details. LOD0 is a really simple shape like a bounding box, and LOD2 contain the fully detailed geometry (with screws, holes, notches and stuff). LOD1 is in-between.

During manipulations and experimentation with my beginner-Ruby-scripts it appeared that I accidently deleted LOD0 and LOD1 in some of the components. This cause some of my previous scripts to fail.

Simple example of my model structure. The CUBE_2 component misses LOD0 & LOD1, causing trouble.

I don’t want to spend hours looking for each items in my model to retrieve the ones with missing LODs and repairing them, so I want to write a script in Ruby to retrieve the components in my model that contain less than 3 groups, and duplicate/renaming the still existing LOD2 in order to retrieve the LOD0, LOD1 & LOD2 original structure for each component (doesn’t matter if all 3 LODs are the same).

The thing is, I can’t find a way to inspect each instance in my model and count/manipulate the groups inside.
Is this due to the particular nature of groups in SU ? Or simply to my lack of experience in Ruby coding ? Is there a method or a trick I don’t know yet that could help me solving this ? Do I have to explode everything in my component and recreate groups ?

Thank you very much for your help !

1 Like

To get a collection [array] of all instances in your model try something like this:

model = Sketchup.active_model
all_used_compos = model.definitions.find_all{|d| ! && !d.image? && d.instances[0] } 

This gives an array of all used component definitions.
To limit it to those containing < 3 groups manipulate it further…

all_3gp_compos = all_used_compos.find_all{|d| d.entities.grep(Sketchup::Group).length < 3 }

This gives an array of those component definitions containing less that three groups…

Next you do to those whatever it is you want ??


Thank you very much TIG !
As often with Ruby : now I see it written it seems pretty logic and obvious to me.

I think I simply lack practice in Ruby, as I didn’t know well the grep method and how to use it to extract specific items in an array (in this case : Sketchup::Groups).

You gave me the keys to access groups inside components, now I want to try by myself to write the code to duplicate groups to restore 3 groups structure components.

Thank you again !

Also, if you are still running SU 17.3, bookmark these pages for Ruby 2.2.4 docs …

In the SketchUp API doc pages for it’s classes, pay attention to the top of the page where it lists what libraries are included. You will find that most all of the collection classes mix in the Enumerable library.

It is from this library that the #grep and #find_all methods come.

Thank you @DanRathbun for taking time to post these useful links !
As a self-taught Ruby apprentice, they will surely be full of valuable resources for continuing building my “Ruby syntax culture” and memorize the possibilities offered by each method.

1 Like