Class: Sketchup::Selection

Inherits:
Object
  • Object
show all
Includes:
Enumerable

Overview

A set of the currently selected drawing elements. Use the Model.selection method to get a Selection object. Note that the order of drawing elements (selection[0], selection[1] and so on) in the set is in no particular order and should not be assumed to be in the same order as the user selected the drawing elements.

Examples:

# Get a handle to the selection set.
model = Sketchup.active_model
selection = model.selection

Version:

  • SketchUp 6.0

Instance Method Summary # collapse

Instance Method Details

#[](index) ⇒ Sketchup::Drawingelement?

The #[] method is used to retrieve an Drawingelement from the selection by index. Index 0 is the first Drawingelement in the selection.

This method is not very efficient. If you need to look at every entity in the selection, consider using #each instead of using this method to manually grab each one.

Examples:

model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
selection.add(entities.to_a)
p selection[0]

Parameters:

  • index (Integer)

    The index of the Drawingelement object to retrieve.

Returns:

See Also:

Version:

  • SketchUp 6.0

#add(drawing_elements) ⇒ Integer #add(*drawing_elements) ⇒ Integer

The #add method is used to add Drawingelement to the selection. Drawingelements that are added to the Selection are visually indicated by the selection bounding box.

You can pass it individual Drawingelements or an Array of Drawingelements: Note that the add, remove, and toggle methods are all aliases for one another. So if you call remove on a Drawingelement that is not selected, it will be toggled to be selected, not removed! Be cautious when writing your code to not make the assumption about the currently selected state of a given Drawingelement.

Examples:

# Add by listing the Drawingelements...
ss.add(e1, e2, e3)

# ...or add by passing an Array of Drawingelements.
ss.add([e1, e2, e3])
entities = model.active_entities
entity = entities[0]
status = selection.add entity

Overloads:

Returns:

  • (Integer)

    the number of Drawingelement objects added

Version:

  • SketchUp 6.0

#add_observer(observer) ⇒ Boolean

The add_observer method is used to add an observer to the selection object.

Examples:

selection = Sketchup.active_model.selection
status = selection.add_observer observer

Parameters:

  • observer (Object)

    An observer.

Returns:

  • (Boolean)

    true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0

#at(index) ⇒ Sketchup::Drawingelement?

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

Examples:

model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
selection.add(entities.to_a)
p selection.at(0)

Parameters:

  • index (Integer)

    The index of the Drawingelement object to retrieve.

Returns:

See Also:

Version:

  • SketchUp 6.0

#clearnil

The clear method is used to clear the selection.

Examples:

entity = entities[0]
selection.add entity
UI.messagebox "Ready to Clear"
selection.clear

Returns:

  • (nil)

Version:

  • SketchUp 6.0

#contains?(drawing_element) ⇒ Boolean

The #contains? method is and alias of #include?.

Examples:

model = Sketchup.active_model
entity = model.active_entities.first
selection = model.selection
selection.add(entity)
p selection.contains?(entity)

Parameters:

Returns:

  • (Boolean)

See Also:

Version:

  • SketchUp 6.0

#countInteger

Note:

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

Examples:

selection = Sketchup.active_model.selection
number = selection.count

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 6.0

#each {|drawing_element| ... } ⇒ nil

Note:

Don't remove content from this collection while iterating over it with #each. This would change the size of the collection and cause elemnts 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 selected Drawingelements.

If you want to do something with all of the selected Drawingelements, this is more efficient than using #[].

Examples:

selection.each { |drawing_element| puts drawing_element }

Yield Parameters:

Returns:

  • (nil)

Version:

  • SketchUp 6.0

#empty?Boolean

The #empty? method is used to determine if there are drawing elements in the selection.

Examples:

status = selection.add drawing_element
status = selection.empty

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#firstSketchup::Drawingelement

The #first method is used to retrieve the first selected Drawingelement

Returns nil if nothing is selected. This method is useful when you know that only a single Drawingelement is selected, or you are only interested in the first selected Drawingelement.

Examples:

status = selection.add drawing_element
drawing_element = selection.first

Returns:

Version:

  • SketchUp 6.0

#include?(drawing_element) ⇒ Boolean

The #include? method is used to determine if a given Drawingelement is in the selection.

Examples:

model = Sketchup.active_model
entity = model.active_entities.first
selection = model.selection
selection.add(entity)
p selection.include?(entity)

Parameters:

Returns:

  • (Boolean)

See Also:

Version:

  • SketchUp 6.0

#invertnil

The #invert method is used to invert the selection.

Examples:

model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
# Create a cube
face = entities.add_face([0, 0, 0], [9, 0, 0], [9, 9, 0], [0, 9, 0])
face.pushpull(-9)
# Add the first two faces to the selection
faces = entities.grep(Sketchup::Face).take(2)
selection.add(faces)
# Invert selection
selection.invert

Returns:

  • (nil)

Version:

  • SketchUp 2019.2

#is_curve?Boolean

The #is_curve? method is used to determine if the selection contains all edges that belong to a single curve.

Examples:

selection.add drawing_element
status = selection.is_curve?

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#is_surface?Boolean

The #is_surface? method is used to determine if the selection contains only all of the faces that are part of a single curved surface.

Examples:

selection.add drawing_element
status = selection.is_surface

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#lengthInteger

The #length method is used to retrieve the number of selected drawing elements.

Examples:

selection = Sketchup.active_model.selection
number = selection.length

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 6.0

#modelSketchup::Model

The #model method retrieves the model for the selection.

Examples:

model = selection.model

Returns:

  • (Sketchup::Model)

    the model that includes the selection if successful

Version:

  • SketchUp 6.0

#nitemsInteger

The #nitems method is an alias for #length.

Examples:

selection = Sketchup.active_model.selection
number = selection.nitems

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 6.0

#remove(drawing_elements) ⇒ Integer #remove(*drawing_elements) ⇒ Integer

The #remove method is used to remove Drawingelements from the selection.

You can pass it individual Drawingelements or an Array of Drawingelements: Note that the add, remove, and toggle methods are all aliases for one another. So if you call remove on a Drawingelement that is not selected, it will be toggled to be selected, not removed! Be cautious when writing your code to not make the assumption about the currently selected state of a given Drawingelement.

Examples:

# Remove by listing the Drawingelements...
ss.remove(e1, e2, e3)

# ...or remove by passing an Array of Drawingelements.
ss.remove([e1, e2, e3])
entities = model.active_entities
entity = entities[0]
status = selection.add entity

Overloads:

Returns:

  • (Integer)

    the number of Drawingelement objects removed

Version:

  • SketchUp 6.0

#remove_observer(observer) ⇒ Boolean

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

Examples:

selection = Sketchup.active_model.selection
status = object.remove_observer observer

Parameters:

  • observer (Object)

    An observer.

Returns:

  • (Boolean)

    true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0

#shiftSketchup::Drawingelement

The #shift method is used to remove the first Drawingelement from the selection and returns it.

Examples:

status = selection.add drawing_element
UI.messagebox "Ready to remove item from selection set"
drawing_element = selection.shift

Returns:

Version:

  • SketchUp 6.0

#single_object?Boolean

The #single_object? method is used to determine if the selection contains a single object.

It can either be a single DrawingElement or a group of DrawingElements for which is_curve? or is_surface? will return true.

Examples:

status = selection.single_object

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#sizeInteger

The #size method is an alias for #length.

Examples:

selection = Sketchup.active_model.selection
number = selection.size

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 2014

#toggle(drawings_elements) ⇒ Integer #toggle(*drawing_elements) ⇒ Integer

The #toggle method is used to change whether a Drawingelement is part of the selection. Drawingelements that are not already selected are added. Drawingelements that are already selected are removed.

You can pass it individual Drawingelements or an Array of Drawingelements: Note that the add, remove, and toggle methods are all aliases for one another. So if you call remove on a Drawingelement that is not selected, it will be toggled to be selected, not removed! Be cautious when writing your code to not make the assumption about the currently selected state of a given Drawingelement.

Examples:

# Toggle by listing the Drawingelements...
ss.toggle(e1, e2, e3)

# ...or toggle by passing an Array of Drawingelements.
ss.toggle([e1, e2, e3])
entities = model.active_entities
entity = entities[0]
status = selection.add entity

Overloads:

Returns:

  • (Integer)

    the number of Drawingelement objects changed

Version:

  • SketchUp 6.0