|
SketchUp Ruby API Reference |
A collection of the Layers in a Model
Parent:Entity
Methods: [], add, add_observer, at, count, each, length, purge_unused, remove_observer, unique_name
Example Code: layerstests.rb
The [] method is used to retrieve a layer by index or name
layer = layers[index]
layer = layers["name"]
index - a number representing the layer's index in an array of Layer objects
"name" - the name of the layer
layer - a Layer object
model = Sketchup.active_model
layers = model.layers
status = layers.add "test layer"
layer1 = layers[0]
The add method is used to add a new layer.
layer = layers.add("layer name")
"layer name" - the name of the added layer
layer - the new Layer object
If you give the name of a Layer that is already defined, it will return the existing Layer rather than adding a new one.
model = Sketchup.active_model
layers = model.layers
status = layers.add "test layer"
The add_observer method is used to add an observer to the current object.
status = object.add_observer observer
observer - an observer
true if successful, false if unsuccessful.
The at method is an alias for []. See [].
The count method is an alias for length. See length.
The each method is used to iterate through all of the layers.
layers.each {|layer| ...}
layer - a variable that will hold each Layer object as they are found.
model = Sketchup.active_model
layers = model.layers
status = layers.add "test layer"
layers.each {| layer | UI.messagebox layer }
The length method retrieves the number of layers.
length = layers.length
length - the number of entities in the collection of entities if successful
model = Sketchup.active_model
layers = model.layers
status = layers.add "test layer"|
length = layers.length
The purged_unused method is used to remove unused layers.
true if successful, false if unsuccessful.
The remove_observer method is used to remove an observer from the current object.
status = object.remove_observer observer
observer - an observer
true if successful, false if unsuccessful.
The unique_name method generates a unique layer name.
newname = layers.unique_name <basename>
<basename> - (optional) basename used as part of the generated name
newname - the new layer name
With no arguments it generates a new unique name using the default base. With a basename String passed in, it will generate a new unique name that uses the given base as a prefix.
For example you could get a Layer name that starts with the string "Walls" with the command:
name = layers.unique_name "Walls".
You could then use this new unique name to add a new Layer.
model = Sketchup.active_model
layers = model.layers
status = layers.add "test layer"
uniquename = layers.unique_name "walls"