Retrieve all entities in a layer

Could anyone show me how to retrieve all entities in a layer ? and how to access properties of entities like location, width, height, …?

I am completely new to Sketchup.

Thank very much,

(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.

2 Likes

Thank you very much Dan !

Hello Dan, i hope you’re allways on this forum !!

I wrote an algorithm that traverses the tags tree (layers in older versions).
I browse the folders, then when I arrive at a tag, I would like to know all the components that are marked by this tag.
If I understood you correctly, the trick would then be to hide all the tags, and only make one of them visible, and thus browse all the visible entities? So you can have all entities marked by the selected tag.

But there is some entities without tag …

No not always. I check in several times each day.

I showed how above. Just change the class argument of the #grep call to Sketchup::ComponentInstance

model = Sketchup::active_model
layers = model.layers
comps = model.entities.grep(Sketchup::ComponentInstance)
layers.each do |this_layer|
  comps_using_this_layer = comps.find_all { |comp|
    comp.layer == this_layer
  }
  # Do something with comps_using_this_layer here ...
end

However, notice that the example only checks the top-level model entities collection. It does not check the entities collections of nested components (or groups.)
.

NO … I never said anything in this thread about hiding tags / layers. The SketchUp API collection iterators will iterate over all members of a collection regardless of whether it can be hidden or not, … or if so is hidden. Ie, its visibility state is just another one of its many properties.

1 Like

Thanks to you Dan, I just understand a little better the mechanics of ruby on sketchup extension

Edit : ok sorry, I understood why I can’t find any components, it’s because i must traverse the entities tree, I correct my mistake and I make you a return!!

Edit : So i have delete all my message because i understand my error

if it’s help here my code to scan the layers tree and find all components of each layer

# calling procedure in console
def allLayer
  model = Sketchup.active_model
  layers = model.layers
  puts "---------------------"
  puts "nombre de dossiers : " + "#{layers.count_folders}"
  puts "nombre de balises : " + "#{layers.count_layers}"
  # lit all folder in layers
  listFolder(layers, '')
  puts "---------------------"
end

# list all folder
def listFolder ( layers , tab)
  layers.each_folder { | folder | 
    puts "folder " + tab + " " + folder.name + "(d=#{folder.count_folders},b=#{folder.count_layers})"
    # list all folder in this folder
    listFolder(folder, tab + '   >')
    # list all layer in this folder
    listLayer(folder, tab + '   ')
  }
end

# list all layer in a folder
def listLayer (folder, tab)
  folder.each { | layer | 
    puts "balise " + tab + " " + layer.name + " " + layer.typename 
    listComponentFromLayer(layer, false, tab + '   ')
  }
end  

# find all component
# find all component
def listComponentFromLayer(this_layer, entities, tab) 
  if(entities == false)
    model = Sketchup.active_model
    entities = model.entities
  end
  entities.each { | ent| 
    if(ent.hidden? == false)
      if ent.is_a?(Sketchup::ComponentInstance)
        #puts "composant : " + tab + " " + ent.name
        if ent.layer == this_layer
          # component is in this layer display name and definition name
          puts "composant : " + tab + " " + ent.name + "<" + ent.definition.name + ">"
        end
      elsif ent.is_a?(Sketchup::Group)
        # find all component in this group
        listComponentFromLayer(this_layer, ent.entities, tab + '>' + ent.name)
      end
    end
  }
end

maybe it would be more efficient to make at first an array of all components and then search for the components of a layer in this array

Yes, I edited my post above to move the greping of the component instances before the layer walking loop. It did not make sense to keep greping this array each time through the loop.


You could also iterate the DefinitionList collection and iterate each definition’s instances collection and collect them per layer (tag) and you can get all component instances at all nesting levels of the model.

1 Like

In actuality, the model’s layers (aka tags) collection is a flat list that contains all of the layers (tags) owned by the model.

The (so called) layer folder tree is just a display feature built using folder pointer properties and folders lists of LayerFolder objects.


For general information. I posted some refinements for merging duplicate layer folders:

1 Like