Search code examples
c#revitrevit-api

How to get a list of all elements in a revit with c#


I would like to add a plug in that reads in a file of data that has a string of RevitIds and paints them.

I can't figure out how to find a given element in Revit based on a string elementId using C#.

UIApplication uiApp = commandData.Application;
 Document doc = uiApp.ActiveUIDocument.Document;

I know that this gives me a document but I don;t know how to get all of the ids. I was thinking of having a foreach loop that checks the string of element id with that of all in the document until it finds a match. Then, I can manipulate it.


Solution

  • One way is to use a FilteredElementCollector to iterate through specific element types to get their elementId's.

    FilteredElementCollector docCollector = new FilteredElementCollector(document).OfCategory(BuiltInCategory.OST_Walls);
    

    followed by(as you suggested):

    foreach(Element el in docCollector)
    {
    ElementId elID = el.Id;
    //....
    }
    

    Modified version:

    List<ElementId> ids = new FilteredElementCollector(document).OfCategory(BuiltInCategory.OST_Walls).ToElementIds().ToList();
    

    followed by(as you suggested):

    foreach(ElementId elId in ids)
    {
    //....
    }
    

    If you are thinking about iterating through ALL elements I suggest looking at this blog post from The Building Coder: Do Not Filter For All Elements