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_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

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

The #each method is used to iterate through all of the layers in the model.

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

#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

Version:

  • SketchUp 6.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_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