Selection
Contains the current user selection of elements within the project.
Inheritance Hierarchy
System.Object
Autodesk.Revit.UI.Selection.Selection
Namespace: Autodesk.Revit.UI.Selection
Assembly: RevitAPIUI (in RevitAPIUI.dll) Version: 25.0.0.0 (25.0.0.0)
Syntax
public class Selection : IDisposable
The Selection type exposes the following members.
Properties
Name
Description
Public Property
IsValidObject
Specifies whether the .NET object represents a valid Revit entity.
Methods
Name
Description
Public Method
Dispose
Releases all resources used by the Selection
Public Method
Equals
Determines whether the specified object is equal to the current object.
(Inherited from Object)
Public Method
GetElementIds
Returns the ids of the elements that are currently selected within the project. The selection may not be complete. See GetReferences for more options.
Public Method
GetHashCode
Serves as the default hash function.
(Inherited from Object)
Public Method
GetReferences
Returns the references that are currently selected.
Public Method
GetType
Gets the Type of the current instance.
(Inherited from Object)
Public Method
PickBox(PickBoxStyle)
Invokes a general purpose two-click editor that lets the user to specify a rectangular area on the screen.
Public Method
PickBox(PickBoxStyle, String)
Invokes a general purpose two-click editor that lets the user to specify a rectangular area on the screen.
Public Method
PickElementsByRectangle.
Prompts the user to select multiple elements by drawing a rectangle.
Public Method
PickElementsByRectangle(ISelectionFilter)
Prompts the user to select multiple elements by drawing a rectangle which pass a customer filter.
Public Method
Code Example
PickElementsByRectangle(String)
Prompts the user to select multiple elements by drawing a rectangle while showing a custom status prompt string.
Public Method
PickElementsByRectangle(ISelectionFilter, String)
Prompts the user to select multiple elements by drawing a rectangle which pass a customer filter while showing a custom status prompt string.
Public Method
Code Example
PickObject(ObjectType)
Prompts the user to select one object.
Public Method
PickObject(ObjectType, ISelectionFilter)
Prompts the user to select one object which passes a custom filter.
Public Method
PickObject(ObjectType, String)
Prompts the user to select one object while showing a custom status prompt string.
Public Method
PickObject(ObjectType, ISelectionFilter, String)
Prompts the user to select one object which passes a custom filter while showing a custom status prompt string.
Public Method
PickObjects(ObjectType)
Prompts the user to select multiple objects.
Public Method
PickObjects(ObjectType, ISelectionFilter)
Prompts the user to select multiple objects which pass a customer filter.
Public Method
PickObjects(ObjectType, String)
Prompts the user to select multiple objects while showing a custom status prompt string.
Public Method
Code Example
PickObjects(ObjectType, ISelectionFilter, String)
Prompts the user to select multiple objects which pass a custom filter while showing a custom status prompt string.
Public Method
PickObjects(ObjectType, ISelectionFilter, String, IList.Reference.)
Prompts the user to select multiple objects which pass a custom filter while showing a custom status prompt string. A preselected set of objects may be supplied and will be selected at the start of the selection.
Public Method
PickPoint.
Prompts the user to pick a point on the active work plane.
Public Method
PickPoint(ObjectSnapTypes)
Prompts the user to pick a point on the active work plane using specified snap settings.
Public Method
PickPoint(String)
Prompts the user to pick a point on the active work plane while showing a custom status prompt string.
Public Method
Code Example
PickPoint(ObjectSnapTypes, String)
Prompts the user to pick a point on the active work plane using specified snap settings while showing a custom status prompt string.
Public Method
SetElementIds
Selects the elements.
Public Method
SetReferences
Selects the references. The references can be an element or a subelement in the host or a linked document.
Public Method
ToString
Returns a string that represents the current object.
(Inherited from Object)
Remarks
The Selection object is used to retrieve the current user selected elements when an external API command is executed.
Example
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)]public class Document_Selection : IExternalCommand{ public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { try { // Select some elements in Revit before invoking this command
// Get the handle of current document. UIDocument uidoc = commandData.Application.ActiveUIDocument;
// Get the element selection of current document. Selection selection = uidoc.Selection; ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
if (0 == selectedIds.Count) { // If no elements selected. TaskDialog.Show("Revit","You haven't selected any elements."); } else { String info = "Ids of selected elements in the document are: "; foreach (ElementId id in selectedIds) { info += "\n\t" + id.ToString(); }
TaskDialog.Show("Revit",info); } } catch (Exception e) { message = e.Message; return Autodesk.Revit.UI.Result.Failed; }
return Autodesk.Revit.UI.Result.Succeeded; } /// </ExampleMethod>}
private void ChangeSelection(UIDocument uidoc){ // Get selected elements from current document. ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
// Display current number of selected elements TaskDialog.Show("Revit", "Number of selected elements: " + selectedIds.Count.ToString());
// Go through the selected items and filter out walls only. ICollection<ElementId> selectedWallIds = new List<ElementId>();
foreach (ElementId id in selectedIds) { Element elements = uidoc.Document.GetElement(id); if (elements is Wall) { selectedWallIds.Add(id); } }
// Set the created element set as current select element set. uidoc.Selection.SetElementIds(selectedWallIds);
// Give the user some information. if (0 != selectedWallIds.Count) { TaskDialog.Show("Revit", selectedWallIds.Count.ToString() + " Walls are selected!"); } else { TaskDialog.Show("Revit","No Walls have been selected!"); }}