Highlight Elements

I arrived in Boston and am now acclimatising in preparation for the AEC DevCamp conference (web site) starting on Monday and the DevLabs in Waltham following afterwards (register).

Meanwhile, here is a quick question from Soub that we have actually provided a solution for a couple of times in the past but not explicitly pointed out yet.

Question: I have a question related to the API for highlighting elements in Revit Architecture based on the Element ID input. Could you please help me by explaining how to highlight multiple elements based on IDs?

Answer: There is really no difference between highlighting a single element or multiple ones. All you need to do is add them to the current selection set managed by the Revit UI document, or to the third argument to the Execute method, and then return a Cancelled or Failed result code.

An example of the first approach is provided by the command highlighting south facing walls. The main lines of interest there in this context are the ones accessing the current selection set, adding specific elements to it, and setting the property to the new collection:

SelElementSet selElements = Document.Selection.Elements;
 
IEnumerable<Wall> walls = CollectExteriorWalls();
 
foreach( Wall wall in walls )
{
  XYZ exteriorDirection = GetExteriorWallDirection( wall );
 
  bool isSouthFacing = IsSouthFacing( exteriorDirection );
 
  if( isSouthFacing )
  {
    selElements.Add( wall );
  }
}
 
Document.Selection.Elements = selElements;

The code presented there is based on Revit 2010. It has been updated for the Revit 2011 API in the DirectionCalculation SDK sample and now looks like this:

  UIDocument uiDoc = new UIDocument( Document );
 
  Autodesk.Revit.UI.Selection.SelElementSet
    selElements = uiDoc.Selection.Elements;
 
  IEnumerable<Wall> walls
    = CollectExteriorWalls();
 
  foreach( Wall wall in walls )
  {
    XYZ exteriorDirection
      = GetExteriorWallDirection( wall );
 
    if( useProjectLocationNorth )
    {
      exteriorDirection
        = TransformByProjectLocation(
          exteriorDirection );
    }
 
    bool isSouthFacing
      = IsSouthFacing( exteriorDirection );
 
    if( isSouthFacing )
      selElements.Insert( wall );
  }
 
  // Select all walls which had the proper direction.
  uiDoc.Selection.Elements = selElements;

As said, you can also highlight elements graphically by adding them to the third argument of an external command Execute method and then returning a Cancelled or Failed result code:

public Result Execute(
  ExternalCommandData revit,
  ref string message,
  ElementSet elements )
{
  // . . .
 
  Element e;
 
  // . . .
 
  elements.Insert( e );

  return Result.Failed;
}

This is demonstrated by the Lab1_2_CommandArguments of the Revit API introduction labs.