Find elements that are not tagged

Here’s a question from a reader who is taking my Revit API course at Udemy.com

“Is it possible via the api to verify if all instances of a particular category have tags?  For instance, if I have 100 light fixtures in a project, I would like to verify that each one has a tag and if not, then to find the ones without tags.”

Below is a bit of code that uses the IndependentTag.TaggedLocalElementId property to select all Lighting Fixtures that have no tags.

public void notTagged()
{
    Document doc = this.ActiveUIDocument.Document;
    UIDocument uidoc = this.ActiveUIDocument;
    SelElementSet sel = SelElementSet.Create();
    foreach (Element e in new FilteredElementCollector(doc)
             .OfCategory(BuiltInCategory.OST_LightingFixtures)
             .OfClass(typeof(FamilyInstance)))
    {
        if (new FilteredElementCollector(doc)
            .OfClass(typeof(IndependentTag))
            .Cast<IndependentTag>()
            .FirstOrDefault(q => q.TaggedLocalElementId == e.Id) == null)
            sel.Add(e);
    }
    uidoc.Selection.Elements = sel;
    uidoc.RefreshActiveView();
}
Advertisements

5 thoughts on “Find elements that are not tagged

  1. Thanks for this share.
    I have a question, if the element is not tagged on a view, and tagged in another view,
    how can we check that the element are not tagged in the first view ?
    because when you check if idenpendanttag.TaggedElementId = element.Id , the condition is true because the element is tagged in the second view.
    Thanks for the answer.
    HFloris

    • Instead of iterating through every element in the document with:

      foreach (Element e in new FilteredElementCollector(doc)

      you can do something like add another foreach loop to iterate through all graphical views and then use the FilteredElementCollector(Document, ElementId) constructor that will search only those elements visible in that view.

  2. Hi – It would be something like this, though you would want additional filtering in the outer loop to get the right set of views

    foreach (View v in new FilteredElementCollector(doc).OfClass(typeof(View))
    {
    foreach (Element e in new FilteredElementCollector(doc, v.Id)
    .OfCategory(BuiltInCategory.OST_LightingFixtures)
    .OfClass(typeof(FamilyInstance)))
    {
    if (new FilteredElementCollector(doc)
    .OfClass(typeof(IndependentTag))
    .Cast()
    .FirstOrDefault(q => q.TaggedLocalElementId == e.Id) == null)
    sel.Add(e);
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s