类:Sketchup::Selection

继承于:
Object
  • Object
显示所有
包括:
Enumerable

概述

一组当前选择的实体。使用Model.selection方法来获得一个Selection对象。注意,实体的顺序(selection[0],selection[1]等等)在集合中没有特定的顺序,不应该被认为是与用户选择实体的顺序相同。

例子:

# 获取选择集的句柄。model = Sketchup.active_model
selection = model.selection

版本:

  • SketchUp 6.0

实例方法总结# collapse

实例方法细节

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

#[]方法是用来检索一个Entity从选择中按索引检索一个实体。索引0是选择中的第一个实体。

这种方法不是很有效。如果你需要查看选择中的每个实体,可以考虑使用#each而不是用这种方法来手动抓取每一个实体。

例子:

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

参数:

  • index (整数)

    要检索的实体对象的索引。

返回:

也见:

版本:

  • SketchUp 6.0

#add(实体) ⇒整数 #add(*entities) ⇒整数

add方法用于将实体添加到选择中。被添加到选区中的实体由选区的边界框直观地表示出来。

你可以传递给它单个的实体或一个实体数组。请注意,添加、删除和切换方法都是彼此的别名。因此,如果你在一个没有被选中的实体上调用移除方法,它将被切换为被选中,而不是被移除。在编写你的代码时要谨慎,不要对一个给定的实体的当前选择状态做出假设。

例子。

# 通过列出实体来添加...ss.add(e1, e2, e3)

# ...或者通过传递一个实体数组来添加。ss.add([e1, e2, e3])
entities = model.active_entities
entity = entities[0]
status = selection.add entity

重载:

返回:

  • (整数)

    增加的实体对象的数量

版本:

  • SketchUp 6.0

#add_observer(observer) ⇒Boolean

add_observer方法用于将一个观察者添加到选择的对象。

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::Entity?

The#atmethod 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 Entity 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?(entity) ⇒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'sEnumerablemix-in module. Prior to that the#countmethod is an alias for#length.

Examples:

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

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 6.0

#each{|Sketchup::Entity| ... } ⇒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 usingto_aand then useeachon the array, when removing content.

The each method is used to iterate through all of the selected entities.

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

Examples:

selection.each { |entity| puts entity }

Yields:

  • (Sketchup::Entity)

    A variable that will hold each Entity object as they are found.

Returns:

  • (nil)

Version:

  • SketchUp 6.0

#empty?Boolean

The empty? method is used to determine if there are entities in the selection.

Examples:

status = selection.add entity
status = selection.empty

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#firstSketchup::Entity

The first method is used to retrieve the first selected entity

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.

Examples:

status = selection.add entity
entity = selection.first

Returns:

Version:

  • SketchUp 6.0

#include?(entity) ⇒Boolean

The#include?method is used to determine if a givenEntityis 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#invertmethod is used to invert the selection.

Examples:

model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
# Create a cubeface = 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 selectionfaces = entities.grep(Sketchup::Face).take(2)
selection.add(faces)
# Invert selectionselection.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 entity
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 entity
status = selection.is_surface

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#lengthInteger

The#lengthmethod is used to retrieve the number of selected entities.

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:

Version:

  • SketchUp 6.0

#nitemsInteger

The#nitemsmethod is an alias for#length.

Examples:

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

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 6.0

#remove(entities) ⇒Integer #remove(*entities) ⇒Integer

The remove method is used to remove entities from the selection.

You can pass it individual Entities or an Array of Entities: Note that the add, remove, and toggle methods are all aliases for one another. So if you call remove on an entity 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 entity.

Examples:

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

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

Overloads:

Returns:

  • (Integer)

    the number of Entity 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::Entity

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

Examples:

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

Returns:

  • (Sketchup::Entity)

    the first Entity object in the selection set if successful

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 Entity or a group of Entities for which is_curve? or is_surface? will return true.

Examples:

status = selection.single_object

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#sizeInteger

The#sizemethod is an alias for#length.

Examples:

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

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 2014

#toggle(entities) ⇒Integer #toggle(*entities) ⇒Integer

The toggle method is used to change whether an entity is part of the selection. Entities that are not already selected are added. Entities that are already selected are removed.

You can pass it individual Entities or an Array of Entities: Note that the add, remove, and toggle methods are all aliases for one another. So if you call remove on an entity 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 entity.

Examples:

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

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

Overloads:

Returns:

  • (Integer)

    the number of Entity objects changed

Version:

  • SketchUp 6.0