Selecting Groups by Name


#1

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_/ }

#2

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_/ }

#3

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_/ }

#4

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

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

#5

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


#6

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.


#7

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.


#8

You search and read …


#9

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.


#10

We’ve been working on a RuboCop extension, to analyse Ruby code with SketchUp extensions in mind: https://github.com/SketchUp/rubocop-sketchup

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.


#11

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