Type issues

Type issues

StrRevitEng
Contributor Contributor
655 Views
5 Replies
Message 1 of 6

Type issues

StrRevitEng
Contributor
Contributor

The main reason I wanted ti try the RevitAPI with C# is because of type hinting.

 

I want to use FilteredElementCollector in order to collect all the FireAlarmDevice Tags, and then iterate them and do stuff.

I've tried 2 things, code is below:

 

1 - Declaring the tags as Element, but then when trying to use the GetTaggedLocalElement() method, I get a type Error because this method belongs to the IndependentTag class.

 

2- Declaring the tags as IndependentTag, but then I get an error on the FEC...

 

How can I get the tags and iterate them while using the GetTaggedLocalElement() method?

 

StrRevitEng_1-1727635796246.png

 

 

0 Likes
Accepted solutions (1)
656 Views
5 Replies
Replies (5)
Message 2 of 6

jeremy_tammik
Alumni
Alumni

You are on the right track. Just as you have already done, you can use OfCategory, OfClass and other methods on the filtered element collector to filter out the element of the desired type. Even though the resulting collection now only contains a very specific class, e.g., tags, the result of the filtered element collector is still Element, the base class. Once you are sure that you have the elements you want, you can cast them to the to the more specific subclass that they really belong to. You can either cast them one by one after retrieving them from the collector, or apply a generic Cast template to the entire collection, Search for things like ".net cast" or ".net cast generic collection":

   

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 6

GaryOrrMBI
Collaborator
Collaborator
Accepted solution

As @jeremy_tammik indicates

.ToElements()

is a RevitAPI function built into the filtered element collector (which creates a .net generic collection wrapper) that directly casts the returned objects to the Revit API "Element" object type. That is not what you want.

 

The base .net collection contains the capability that you are attempting to do in your second attempt (cast the returned objects to IndependentTag objects). You just need to use that .net function/method instead of the Revit API .ToElements() function:

 

.Cast(ObjectType)

 

Take a look at the .net help for usage:

https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.icollection-1?view=net-8.0 

 

-G

 

Gary J. Orr
GaryOrrMBI (MBI Companies 2014-Current)
aka (past user names):
Gary_J_Orr (GOMO Stuff 2008-2014);
OrrG (Forum Studio 2005-2008);
Gary J. Orr (LHB Inc 2002-2005);
Orr, Gary J. (Gossen Livingston 1997-2002)
0 Likes
Message 4 of 6

TripleM-Dev.net
Advisor
Advisor

Or something like this, see code snipplet below, use OfType for casting.

Viewtype is a property of View, and can't be retrieved from a Element object, with the OfType it can)

 

And returns a List of that object type.

IEnumerable<View> CollViews = 
    new FilteredElementCollector(this.iRvtDoc)
    .WhereElementIsNotElementType()
    .OfType<View>()
    .Where(v => v.ViewType != ViewType.Schedule);

 

0 Likes
Message 5 of 6

ctm_mka
Collaborator
Collaborator

 I could be missing something, but your select statement is pulling the integer value of the Element ID, not the element itself. Try removing ".Id.IntegerValue".

0 Likes
Message 6 of 6

StrRevitEng
Contributor
Contributor

Thank you for the clear explanation, this helped me get to the where I wanted.

 

@ctm_mka  you're probably right. I uploaded the code snippets after a lot of tinkering, but the essence of my issue is that I was lacking a cast.

 

Thanks for all the rest for helping out as well.

0 Likes