Class: Sketchup::Layers

Inherits:
Entity
  • Object
show all
Includes:
Enumerable

Overview

Note:

As of SketchUp 2020 “Layers” were renamed to “Tags” in the UI. The API retains the use of “Layer” for compatibility and is synonymous with “Tag”.

The Layers collection allows you to see and manage all of the layers in a model. You get a pointer to the Layers object from within the Model.

Examples:

model = Sketchup.active_model
layers = model.layers

Version:

  • SketchUp 6.0

Instance Method Summary # collapse

Methods inherited from Entity

#attribute_dictionaries, #attribute_dictionary, #delete_attribute, #deleted?, #entityID, #get_attribute, #inspect, #model, #parent, #persistent_id, #set_attribute, #to_s, #typename, #valid?

Instance Method Details

#[](index_or_name) ⇒ Sketchup::Layer?

The #[] method is used to retrieve a layer by index or name.

Examples:

model = Sketchup.active_model
layers = model.layers
new_layer = layers.add "test layer"
layer_by_number = layers[1]
layer_by_name = layers["test layer"]

Parameters:

  • index_or_name (Integer, String)

    A number representing the layer's index in an array of Layer objects, or the name of the layer.

Returns:

See Also:

Version:

  • SketchUp 6.0

#add(layer_name) ⇒ Sketchup::Layer Also known as: add_layer

The #add method is used to add a new layer.

If you give the name of a Layer that is already defined, it will return the existing Layer rather than adding a new one.

Examples:

layers = Sketchup.active_model.layers
layer = layers.add("Test Layer")

Parameters:

  • layer_name (String)

    The name of the added layer.

Returns:

Version:

  • SketchUp 6.0

#add_folder(name) ⇒ Sketchup::LayerFolder #add_folder(folder) ⇒ Sketchup::LayerFolder

The #add_folder method adds or moves a layer folder.

Examples:

manager = Sketchup.active_model.layers
folder = manager.add_folder('Doors')

Overloads:

Returns:

See Also:

Version:

  • SketchUp 2021.0

#add_observer(observer) ⇒ Boolean

The #add_observer method is used to add an observer to the layers collection.

Examples:

layers = Sketchup.active_model.layers
status = layers.add_observer observer

Returns true if successful, false if unsuccessful.

Parameters:

Returns:

  • (Boolean)

    true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0

#at(index_or_name) ⇒ Sketchup::Layer?

The #at method is an alias for #[].

Examples:

model = Sketchup.active_model
layers = model.layers
new_layer = layers.add "test layer"
layer_by_number = layers.at(1)
layer_by_name = layers.at("test layer")

Returns:

See Also:

Version:

  • SketchUp 6.0

#countObject

Note:

Since SketchUp 2014 the count method is inherited from Ruby's Enumable mix-in module. Prior to that the #count method is an alias for #length.

Returns integer - the number of layers in the collection.

Examples:

layers = Sketchup.active_model.layers
number = layers.count

Returns:

  • integer - the number of layers in the collection

See Also:

Version:

  • SketchUp 6.0

#count_foldersInteger

The #count_folders method counts the number of folders which are direct children of the layer manager.

Examples:

manager = Sketchup.active_model.layers
folder = manager.add_folder('Doors')
num_folders = manager.count_folders

Returns:

  • (Integer)

Version:

  • SketchUp 2021.0

#count_layersInteger

The #count_layers method retrieves the number of layers not in a folder.

Examples:

layers = Sketchup.active_model.layers
number = layers.count_layers

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 2021.0

#each {|layer| ... } ⇒ Object

Note:

Don't remove content from this collection while iterating over it with #each. This would change the size of the collection and cause elements to be skipped as the indices change. Instead copy the current collection to an array using to_a and then use each on the array, when removing content.

The #each method is used to iterate through all of the layers in the model. This include layers that are nested inside folders.

Examples:

model = Sketchup.active_model
layers = model.layers
layers.add("Test layer")
layers.each { | layer | puts layer.name }

Yields:

  • (layer)

Yield Parameters:

Version:

  • SketchUp 6.0

#each_folder {|folder| ... } ⇒ Object

The #each_folder method is used to iterate through the folders that are direct children to the layer manager.

Examples:

manager = Sketchup.active_model.layers
folder = manager.add_folder('Doors')
folder = manager.add_folder('Windows')
manager.each_folder { |folder|
  puts folder.name
}

Yields:

  • (folder)

Yield Parameters:

Version:

  • SketchUp 2021.0

#each_layer {|layer| ... } ⇒ Object

The #each_layer method is used to iterate through the layers that are not inside a layer folder.

Examples:

model = Sketchup.active_model
layers = model.layers
layers.add('Test layer')
layers.each_layer { | layer | puts layer.name }

Yields:

  • (layer)

Yield Parameters:

Version:

  • SketchUp 2021.0

#foldersArray<Sketchup::LayerFolder>

Note:

This does not return all the folders in the model, only those that are direct children of the layer manager.

The #folders method returns the folders of the layer manager.

Examples:

manager = Sketchup.active_model.layers
manager.add_folder('Doors')
manager.add_folder('Windows')
folders = manager.folders

Returns:

Version:

  • SketchUp 2021.0

#layersArray<Sketchup::Layer>

The #layers method retrieves the layers not in a folder.

Examples:

manager = Sketchup.active_model.layers
layers = manager.layers

Returns:

Version:

  • SketchUp 2021.0

#lengthInteger

The #length method retrieves the number of layers.

Examples:

layers = Sketchup.active_model.layers
number = layers.length

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 6.0

#purge_unusedInteger Also known as: purge_unused_layers

The #purge_unused method is used to remove unused layers.

Examples:

layers = Sketchup.active_model.layers
num_layers_removed = layers.purge_unused

Returns:

  • (Integer)

    Number of unused layers removed

See Also:

Version:

  • SketchUp 6.0

#purge_unused_folders {|folder| ... } ⇒ Object

The #purge_unused_folders method is used to remove all layer folder with no children.

Examples:

manager = Sketchup.active_model.layers
folder = manager.add_folder('Doors')
folder = manager.add_folder('Windows')
manager.purge_unused_folders

Yields:

  • (folder)

Yield Parameters:

Version:

  • SketchUp 2021.0

#remove(layer, remove_geometry = false) ⇒ Boolean Also known as: remove_layer

Remove the given layer from the model, optionally removing the geometry.

Examples:

# Remove layer by layer reference.
layer = Sketchup.active_model.layers.add("MyLayer")
Sketchup.active_model.layers.remove(layer)

# Remove layer by name.
Sketchup.active_model.layers.add("MyLayer")
Sketchup.active_model.layers.remove("MyLayer")

# Remove layer by index.
Sketchup.active_model.layers.remove(1)

# Remove layer and the entities on the layer.
edge = Sketchup.active_model.entities.add_line([0, 0, 0], [9, 9, 9])
edge.layer = Sketchup.active_model.layers.add("MyLayer")
Sketchup.active_model.layers.remove("MyLayer", true)

Returns true if successful, false if unsuccessful.

Parameters:

  • layer (Sketchup::Layer, Integer, String)
  • remove_geometry (Boolean) (defaults to: false)

    If true, geometry in the removed layer will be removed as well. If false (which is the default), this geometry will be placed on Layer 0.

Returns:

  • (Boolean)

    true if successful, false if unsuccessful.

Version:

  • SketchUp 2015

#remove_folder(folder) ⇒ nil

The #remove_folder method removes the folder from the model. All children are preserved, but moved up one level.

Examples:

manager = Sketchup.active_model.layers
folder = manager.add_folder('Doors')
manager.remove_folder(folder)

Parameters:

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    if the folder is not a direct child of the receiver.

Version:

  • SketchUp 2021.0

#remove_observer(observer) ⇒ Boolean

The #remove_observer method is used to remove an observer from the current object.

Examples:

layers = Sketchup.active_model.layers
status = layers.remove_observer observer

Returns true if successful, false if unsuccessful.

Parameters:

Returns:

  • (Boolean)

    true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0

#sizeInteger

The #size method is an alias of #length.

Examples:

layers = Sketchup.active_model.layers
number = layers.size

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 2014

#unique_nameString #unique_name(base_name) ⇒ String

The #unique_name method can be used to get a string that will be a unique layer name inside this collection.

Examples:

model = Sketchup.active_model
layers = model.layers
# Will return "Joe" since there are probably no other layers named that.
# Or might return something like "Joe #2" if there is already a layer
# named Joe.
good_name = layers.unique_name("Joe")

Overloads:

  • #unique_nameString

    Returns Will default to using “Layer” (SketchUp2019 and older) or “Tag” as basename for a unique name.

    Returns:

    • (String)

      Will default to using “Layer” (SketchUp2019 and older) or “Tag” as basename for a unique name.

  • #unique_name(base_name) ⇒ String

    Parameters:

    • base_name (String)

      The base name to build the unique name from.

    Returns:

Version:

  • SketchUp 6.0