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| e.name =~ /_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) && e.name =~ /_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| e.name =~ /_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)
1 Like

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 - #2 by DanRathbun
… 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) - #12 by DanRathbun


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.

1 Like

You search and read …

1 Like

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.

We’ve been working on a RuboCop extension, to analyse Ruby code with SketchUp extensions in mind: GitHub - SketchUp/rubocop-sketchup: Rubocop cops for SketchUp - test against our Extension Warehouse technical requirements and other pitfalls

Key purpose of that was to provide a tool to automate some of the techincal requirements we have for extensions submitted to Extension Warehouse.

But we also added checks beyond that, like known performance issues of .typename etc. It’s still in development but it’s already detecting a lot of potential issues. We need to improve the messages though, with links to more detailed explanations on how to address that it finds.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.