Retrieve all entities in a layer

(1) Layers in SketchUp do not own an entities collection. Layers in SketchUp are behavioral property objects that entities can share, in order to control color, visibility, etc.

(2) In SketchUp all primitives (edges, curves, arcs, circles and faces) need to be assigned to use “Layer0”. (So the implication is that filtering by “Layer0” will not do you much good.)

(3) Only groups, component instances (or Clines & Cpoints) should be assigned to use other layers.

(4) In the SketchUp API (and it’s Document Object Model,) it is not just the model that has an entities collection. All types of definitions (groups, images and components) also have entities collections.

There are examples here in this forum on how to search for groups or components by name. You can just as easily change the method call to check the layer name that an entity is assigned to use.

Basically something like:

model = Sketchup::active_model
grps = model.entities.grep(Sketchup::Group)
grps_on_layer_twan = grps.find_all {|grp|
  grp.layer.name == "twan"
}

In geometry, objects have transformations. Component and Group instances have a transformation method that returns a Geom::Transformation instance.

grps_on_layer_twan.each {|grp|
  puts "Group '#{grp.name}' located at: #{grp.transformation.origin.to_s}"
}

Firstly, one must learn how the application works from the manual user’s point of view, before you can automate using the Ruby API.

Second, one must learn standard Ruby in order to apply the extensions that the SketchUp API adds to Ruby. See the Ruby Learning Resources “pinned post” here in this topic.

Then study the examples given on the API site, and those by the SketchUp team downloadable from the Extension Warehouse.

Look at how other authors have written their extensions (if they are not scrambled.)

There are several threads in this category about how to begin learning SketchUp Ruby.

1 Like