Skip to content

Revit

Common Operations

Selecting Elements

The general workflow for dealing with user selections in a Revit plugin is:

  • Define a Selection filter class.
  • Initialize a Reference object to accept the picked result.
  • Initialize a selection filter object from your defined class.
  • Initialize a Selection object.
  • Call one of the Pick... methods on the Selection object and assign the result to the reference object.
    • Note: It is at this stage that you will pass the Selection Filter object as an argument.
  • Get the Element from the reference using doc.GetElement(reference)
  • Check if the element is null.
  • Do stuff with your selection.

For example:

// Filter object set up elsewhere in code

// Initialize a reference object to accept the pick result
Reference signRef = null;

// Initialize a Filter Object
FilterNamedFamily signFilter = new FilterNamedFamily("wall_based");

// Initialize a Selection Object
Selection signSel = uiApp.ActiveUIDocument.Selection;

// Call a pick method on the selection object. Assign result to ref object.
signRef = signSel.PickObject(ObjectType.Element, signFilter, "Pick a sign family!");

// Get the element from the reference
Element signElem = doc.GetElement(signRef);

// Cast element as familyinstance
FamilyInstance family = signElem as FamilyInstance;
if (family == null)
{
    return Result.Failed; // just in case
}

// do stuff here!