#RTCNA wish 4 granted! Delete empty tags

Its great when a tiny bit of code can do something useful!

  • Filter for only elements in the active view: FilteredElementCollector(doc, doc.ActiveView.Id)
  • Get only tags with no text: Where(q => q.TagText == "")
  • Convert from Elements into a list of ElementIds (because that’s what Document.Delete() requires as an input): .Select(q => q.Id).ToList()

BEFORE

 before

AFTER

after

public void deleteEmptyTags()
{
    Document doc = this.ActiveUIDocument.Document;
    using (Transaction t = new Transaction(doc, "Delete empty tags"))
    {
        t.Start();
        doc.Delete(new FilteredElementCollector(doc, doc.ActiveView.Id)
             .OfClass(typeof(IndependentTag))
             .Cast<IndependentTag>()
             .Where(q => q.TagText == "")
             .Select(q => q.Id).ToList());
        t.Commit();
    }
}
Advertisements

2 thoughts on “#RTCNA wish 4 granted! Delete empty tags

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