Selecting Groups by Name


I’m trying to grab all of the top level groups that have the follow text in their instance name: WALL_ASSEMBLY_

My original line of code is giving me errors since it is also picks up any random lines or other geometry drawn and then it is throwing an error since the name parameter does not exist for some of these entities.

My code is:

# Gets all Wall Assemblies

	wall_list = Sketchup.active_model.entities.find_all { |e| =~ /_WALL_ASSEMBLY_/ }


You need to signify that ‘e’ is a group

wall_list = Sketchup.active_model.entities.find_all { |e| e.is_a?(Sketchup::Group) && =~ /_WALL_ASSEMBLY_/ }


This is what I did but I don’t know if it is much different or computationally expensive than yours:

    group_list = Sketchup.active_model.entities.find_all { |e| e.typename == "Group" }
	wall_list = group_list.find_all { |e| =~ /_WALL_ASSEMBLY_/ }


Rather than the find_all for e.typename == “Group”, which is very inefficient, use

group_list = Sketchup.active_model.entities.grep(Sketchup::Group)


I will replace that line of code accordingly, thank-you.


I guess it is my inexperience with Ruby and the API but how do you know what is efficient or not?

I am really trying to tighten up my code as much as possible since this new plugin is already dealing with a large amount of geometry and every little bit of speed I can get out of the code can only help.


Reminding you that we answered this 6 days ago in your other thread … Traversing through Entities in a Group
… in which you seemed to have understood.

In that post I also gave a link to an older thread that gives a whole method that will filter by definition first …
Selecting Specific Entity, Group or Component (By Name)

In addition we seem to constantly be fighting (a losing battle over) the use of #typename when #grep is faster and more appropriate … (ie String comparison in Ruby is slow! Class comparison is FAST!)

It seems we have repeat this at least once a month. The above post was only 2 days ago.

The main point here is that these things are quite EASY to find using the forum search if you enter into the category you wish to search first.


You search and read …


The other thread went over my head. I was also getting bogged down with the whole definition thing when it is not needed in this case, just a quick search of the entities.