Get all Tags

Get all Tags

david_rock
Enthusiast Enthusiast
3,382 Views
5 Replies
Message 1 of 6

Get all Tags

david_rock
Enthusiast
Enthusiast

Hello,

 

The following code does not include the tags in a model.

 

Dim objCollectorAllFamilyInstance As FilteredElementCollector = New FilteredElementCollector(activeDBdoc).OfClass(GetType(FamilyInstance))

I need a similar line to give me all the tags (or IndependantTag) elements. However OfClass(GetType(IndependentTag)) does not work.

 

Kind Regards

David

 

0 Likes
3,383 Views
5 Replies
Replies (5)
Message 2 of 6

jeremytammik
Autodesk
Autodesk

Dear David,

 

I very strongly suggest you install RevitLookup and take a closer look at the tags you wish to select using that tool:

 

https://github.com/jeremytammik/RevitLookup

 

It will tell you all you need to know in just a few clicks, such as .NET class, Revit category, etc.

 

Tags are not family instances.

 

You can also look at the Revit API help file to see that it defines a separate class for independent tags, very imaginatively named IndependentTag:

 

http://www.revitapidocs.com/2018.1/e52073e2-9d98-6fb5-eb43-288cf9ed2e28.htm

 

Try filtering for those and see what you get.

 

Please install RevitLookup and check it out yourself first!

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 6

david_rock
Enthusiast
Enthusiast

Thank you for your suggestions, yes I use RevitLookup regularly. However in this case it didn't provide me with any ideas.

 

I ended up using CategoryType to find the Annotation categories.

It is not very elegant, so I would welcome alternative solutions. 

 

Dim ListOfElementId As New List(Of ElementId)
Dim objCategories As Categories = objDocument.Settings.Categories
    For Each objCategory As Category In objCategories
         If objCategory.CategoryType.Equals(CategoryType.Annotation) Then
              Dim objCollector As FilteredElementCollector = New FilteredElementCollector(objDocument).OfCategoryId(objCategory.Id)
              ListOfElementId.AddRange(objCollector.ToElementIds)
         End If
    Next
Return ListOfElementId

Kind Regards

David

 

0 Likes
Message 4 of 6

jeremytammik
Autodesk
Autodesk

Dear David,

 

Congratulations on finding a solution that suits your needs and thank you for sharing it with us.

 

I cannot comment on whether the filtering criteria can be improved.

 

You can optimise it slightly by returning the `ICollection` of element ids generated by the call to `ToElementIds`.

 

There is probably no need nor any advantage in using that to create yet another own `List` of element ids.

 

Every new list or other collection creation requires a new memory allocation and copying across of the existing data, cf. FindElement and Collector Optimisation, especially item 3:

 

http://thebuildingcoder.typepad.com/blog/2012/09/findelement-and-collector-optimisation.html

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 5 of 6

perry.swoboda
Advocate
Advocate

Just so that people not despair for easily querying all tags, the original poster's code works in Revit 2020.
Here is my version tweaked to only return Material Tags. (in C#)

var collector = new FilteredElementCollector(doc).OfClass(typeof(IndependentTag)).Cast<IndependentTag>().Where<IndependentTag>(tag => tag.IsMaterialTag == true);
0 Likes
Message 6 of 6

jeremytammik
Autodesk
Autodesk

Thank you for sharing this. I added it to The Building Coder samples for future reference:

 

https://github.com/jeremytammik/the_building_coder_samples/compare/2020.0.147.16...2020.0.147.17

 

    /// <summary>
    /// Return all tags, optionally 
    /// material tags only
    /// </summary>
    IEnumerable<IndependentTag> GetMaterialTags(
      Document doc,
      bool material_only )
    {
      IEnumerable<IndependentTag> tags
        = new FilteredElementCollector( doc )
          .OfClass( typeof( IndependentTag ) )
          .Cast<IndependentTag>();

      return material_only
        ? tags
        : tags.Where<IndependentTag>( 
          tag => tag.IsMaterialTag );
    }

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes