Class: Sketchup::Layers

Inherits:
Entity
  • Object
show all
Includes:
Enumerable

Overview

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) ⇒ Object

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

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[1]
layer_by_name = layers["test layer"]

Parameters:

  • index_or_name

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

Returns:

  • layer - a Layer object

Version:

  • SketchUp 6.0

#add(layer_name) ⇒ Object

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")

Returns layer - the new Layer object

Parameters:

  • layer_name

    The name of the added layer.

Returns:

  • layer - the new Layer object

Version:

  • SketchUp 6.0

#add_observer(observer) ⇒ Object

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 success - true if successful, false if unsuccessful.

Parameters:

  • observer

    An observer.

Returns:

  • success - true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0

#at(index_or_name) ⇒ Object

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

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[1]
layer_by_name = layers["test layer"]

Parameters:

  • index_or_name

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

Returns:

  • layer - a Layer object

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.

Examples:

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

Yields:

  • (layer)
    • a variable that will hold each Layer object as they are found.

Version:

  • SketchUp 6.0

#lengthObject

The #length method retrieves the number of layers.

The #size method is an alias for #length added in SketchUp 2014.

Examples:

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

Returns:

  • integer - the number of layers in the collection

Version:

  • SketchUp 6.0

#purge_unusedObject

The purged_unused method is used to remove unused layers.

Examples:

layers = Sketchup.active_model.layers
status = layers.purge_unused

Returns:

  • success - true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0

#remove(layer, remove_geometry = false) ⇒ Object

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 boolean - true if successful, false if unsuccessful.

Parameters:

  • layer

    Integer index, String name or Layer

  • remove_geometry (defaults to: false)

    Boolean - 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) ⇒ Object

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 success - true if successful, false if unsuccessful.

Parameters:

  • observer

    An observer.

Returns:

  • success - true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0

#sizeObject

The #length method retrieves the number of layers.

The #size method is an alias for #length added in SketchUp 2014.

Examples:

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

Returns:

  • integer - the number of layers in the collection

Version:

  • SketchUp 6.0

#unique_name(base_name) ⇒ Object

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"

Returns name - the unique name

Parameters:

  • base_name (optional)

    The base name to build the unique name from.

Returns:

  • name - the unique name

Version:

  • SketchUp 6.0