|
SketchUp Ruby API Reference |
A set of the currently selected entities. Use the Selection method from the Model class to get a Selection object.
Parent: Object
Methods: [], add, add_observer, at, clear, contains?, count, each, empty?, first, include?, is_curve? , is_surface? , length, model, nitems, remove,remove_observer, shift, single_object?, toggle
Example Code: selectiontests.rb
The [] method is used to retrieve an Entity object from the selection by index
entity = selection[index]
index - the index of the Entity object to retrieve
entity - an Entity object if successful
This method is not very efficient.
entity = model.selection[0]
depth = 100
width = 100
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the face to the entities in the model
face = entities.add_face pts
selection = model.selection
entity = entities[0]
status = selection.add entity
entity = selection[0]
if (entity)
UI.messagebox entity
else
UI.messagebox "Failure"
end
The add method is used to add entities to the selection.
status = selection.add(ent1)
status = selection.add(ent1, ent2, ...)
status = selection.add([entity1, entity2, ...])
ent1, ent2, ... - Entity objects
[entity1, entity2, ...] - an Array of Entity objects
status - the number of Entity objects added
You can pass it individual Entities or an Array of Entities.
ss.add(e1, e2, e3, ...)
or
ss.add([e1, e2, e3,...])
entities = model.active_entities
entity = entities[0]
status = selection.add entity
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 used to retrieve an Entity object from the selection by index.
entity = selection.at index
index - the index of the Entity object to retrieve
entity - an Entity object if successful
This method is not very efficient. It is equivalent to using selection[]. In the case of this method, you have to start at index 0 to retrieve the first entity.
entity = entities[0]
status = selection.add entity
# looks like you have to retrieve from the selection set starting
# at zero.
entity = selection.at(0)
The clear method is used to clear the selection.
selection.clear
entity = entities[0]
selection.add entity
UI.messagebox "Ready to Clear"
selection.clear
The contains? method is used to determine if a given entity is in the selection.
status = selection.contains? entity
entity - an Entity object
status - true if the selection contains the entity. False if the selection does not contain the entity.
entity = entities[0]
status = selection.add entity
status = selection.contains? entity
if (status)
UI.messagebox status
else
UI.messagebox "Failure"
end
The count method is an alias for length. See also length.
number = selection.count
number - the number of Entities in the selection if successful
number = selection.count
The each method is used to iterate through all of the selected entities.
selection.each {| entity | ...}
entity - a variable that will hold each Entity object as they are found.
If you want to do something with all of the selected Entities, this is more efficient than using []
selection = model.selection
entity = entities[0]
status = selection.add entity
selection.each { | entity| UI.messagebox entity }
The empty? method is used to determine if there are entities in the selection.
status = selection.empty?
status - true if the selection is empty. False if the selection is not empty.
status = selection.add entity
status = selection.empty?
The first method is used to retrieve the first selected entity
entity = selection.first
entity - the first selected Entity object if successful
Returns nil if nothing is selected. This method is useful when you know that only a single entity is selected, or you are only interested in the first selected entity.
status = selection.add entity
entity = selection.first
The include? method is an alias for contains?
status = selection.include? entity
entity - an Entity object
status - true if the Entity object is in the selection. False if the Entity object is not in the selection.
selection.add entity
status = selection.include? entity
The is_curve? method is used to determine if the selection contains all edges that belong to a single curve.
status = selection.is_curve?
status - true if the selection contains all edges that belong to a single curve. False if the selectio does not contain all edges that belong to a single curve.
selection.add entity
status = selection.is_curve?
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.
status = selection.is_surface?
status - true if the selection contains all faces that belong to a single curved surface. False if the selection does not contain all faces that belong to a single curved surface.
selection.add entity
status = selection.is_surface?
The length method is used to retrieve the number of selected Entities.
number = selection.length
length - the number of entities in the selection if successful
number = selection.length
The model method retrieves the model for the selection.
model = selection.model
model - the model that includes the selection if successful
model = selection.model
if (model)
UI.messagebox status
else
UI.messagebox "Failure"
end
The nitems method is an alias for length.
number = selection.nitems
number - the number of items in the selection if successful
number = selection.nitems
if (number)
UI.messagebox number
else
UI.messagebox "Failure"
end
The remove method is used to remove entities from the selection.
status = selection.remove entity
status = selection.remove entity1, entity2, ...
status = selection.remove([entity1, entity2, ...])
ent1, ent2, ... - Entity objects
[entity1, entity2, ...] - an Array of Entity objects
status - the number of Entity objects removed if successful
entity = entities[0]
status = selection.add entity
UI.messagebox "Ready to remove item from selection set"
# returns number of items removed
status = selection.remove entity
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 shift method is used to remove the first entity from the selection and returns it
entity = selection.shift
entity - the first Entity object in the selection set if successful
status = selection.add entity
UI.messagebox "Ready to remove item from selection set"
entity = selection.shift
The single_object? method is used to determine if the selection contains a single object
status - selection.single_object?
status - true if the selection contains a single object. False if the selection does not contain a single object.
It can either be a single Entity or a group of Entities for which is_curve? or is_surface? will return true.
status = selection.single_object?
The toggle method is used to add or remove entities from the selection.
status = selection.toggle entity
status = selection.toggle entity1, entity2, ...
status = selection.toggle([entity1, entity2, ...])
ent1, ent2, ... - Entity objects
[entity1, entity2, ...] - an Array of Entity objects
status - status - the number of Entity objects toggled if successful
You can pass it individual Entities or an Array of Entities. Entities that are not already selected are added. Entities that are already selected are removed.
status = selection.add entity
UI.messagebox "Ready to toggle item in selection set"
status = selection.toggle entity