Layers.add with txt file

I wanted to use a list in a txt file to create many layers at once, is it possible?

    bar = UI::Toolbar.new "Teste"
    btao = UI::Command.new("Layers") {
      
      model = Sketchup.active_model
      layers = model.layers
      layers.add "0-Doors"
      layers.add "0-Walls"
      
     }

another doubt
can I get a button to turn all layers off and one to turn all layers on?

We call them tags now, but they’ve not been renamed in the API.

You can use Ruby’s Core IO and File class (which inherits many methods from it’s ancestor IO) to read text files.

begin
  # Always wrap IO operations in a begin ... rescue block
  filepath = "some/path/to/file.txt"
  # Read the textfile into an array of lines:
  lines = IO.readlines(filepath, chomp: true)
  lines.each do |line|
    # Strip leading and trailing whitespace:
    name = line.strip
    # Skip any empty lines that result:
    next if name.empty?
    # Only add if the layer does not exist:
    layers.add(name) unless layers[name]
  end
rescue => e
  puts e.inspect
end
1 Like

Yes, except you should never switch off “Layer0” or “untagged” and you cannot switch off the current layer/tag.

def layers_off
  model = Sketchup.active_model
  layers = model.layers
  layers.each do |layer|
    next if layer.name == "Layer0" || layer.display_name == "Untagged"
    next if layer == model.active_layer
    layer.visible= false if layer.visible?
  end
end

def layers_on
  model = Sketchup.active_model
  layers = model.layers
  layers.each do |layer|
   layer.visible= true unless layer.visible?
  end
end

for example wrap this snippet as a command

Sketchup.active_model.layers.each {|l| l.visible = false}

Note that the active layer (model#active_layer) cannot be made non-visible. It will silently ignore the setting. Good practice says that “Untagged” (aka “Layer0”) should be left active, but if you violate that practice it can be made non-visible. Usually a bad idea!

1 Like

Nice Dan, thank you very much, always helping, I will test. Sorry for the delay to answer

1 Like