Selecting Specific Entity, Group or Component (By Name)

See: Sketchup::DefinitionList.[]

Because you are asking about filtering and searching, (and the API docs do not actually say so,…) I’ll assume you do not know that most SketchUp API collection classes mix in the Ruby Enumerable library.

So say that wasn’t exactly the definition name that you wanted to find, but one of a certain definition’s instances.

Using the definition list subscript method, you get the definition reference, then use one of the Enumerable search methods to find the exact instance by either guid or instance name (which can be different from, but based upon, it’s definition’s name.)

dlist = Sketchup.active_model.definitions
cdef = dlist["PocketDoor"]
if cdef # test for nil (not found)
  if !cdef.instances.empty?
    obj = cdef.instances.find {|i| i.name == "PocketDoor - Master Bath" }
  else
    obj = nil
  end
end

EDIT: (Adding this note on zooming to a found object, because of a post in another thread.)

Once the object is found, to zoom extents around the entity use:

Sketchup.active_model.active_view.zoom(obj) unless obj.nil?

ADD: For how to find a group instance by name, see [u]this post further down the thread[/u].

3 Likes