Selection Class


Contains the current user selection of elements within the project.

Namespace: Autodesk.Revit.UI.Selection
Assembly: RevitAPIUI (in RevitAPIUI.dll) Version: 2015.0.0.0 (2015.0.0.0)

Syntax

C#
public class Selection : IDisposable
Visual Basic
Public Class Selection _
	Implements IDisposable
Visual C++
public ref class Selection : IDisposable

Remarks

The Selection object is used to retrieve the current user selected elements when an external API command is executed.

Examples

Copy C#
[Autodesk.Revit.Attributes.Transaction(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.IntegerValue;
                }

                TaskDialog.Show("Revit",info);
            }
        }
        catch (Exception e)
        {
            message = e.Message;
            return Autodesk.Revit.UI.Result.Failed;
        }

        return Autodesk.Revit.UI.Result.Succeeded;
    }
    /// </ExampleMethod>
}
Copy VB.NET
<Autodesk.Revit.Attributes.Transaction(TransactionMode.[ReadOnly])> _
<Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)> _
Public Class Document_Selection
    Implements IExternalCommand
    Public Function Execute(commandData As ExternalCommandData, ByRef message As String, elements As ElementSet) As Autodesk.Revit.UI.Result Implements IExternalCommand.Execute
        Try
            ' Select some elements in Revit before invoking this command


            ' Get the handle of current document.
            Dim uidoc As UIDocument = commandData.Application.ActiveUIDocument

            ' Get the element selection of current document.
            Dim selection As Selection = uidoc.Selection
            Dim selectedIds As ICollection(Of ElementId) = uidoc.Selection.GetElementIds()

            If 0 = selectedIds.Count Then
                ' If no elements selected.
                TaskDialog.Show("Revit", "You haven't selected any elements.")
            Else
                Dim info As [String] = "Ids of selected elements in the document are: "
                For Each id As ElementId In selectedIds
                    info += vbLf & vbTab + id.IntegerValue
                Next

                TaskDialog.Show("Revit", info)
            End If
        Catch e As Exception
            message = e.Message
            Return Autodesk.Revit.UI.Result.Failed
        End Try

        Return Autodesk.Revit.UI.Result.Succeeded
    End Function
    ' </ExampleMethod>
End Class

Inheritance Hierarchy

System Object
Autodesk.Revit.UI.Selection Selection

See Also