Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Determine if element is ElementType

Anonymous

Determine if element is ElementType

Anonymous
Not applicable

I'm listening to the Application.DocumentChanged event and I would like to see if the element modified is an Instance or an ElementType. I'm currently doing the following but it doesn't quite satisfy all the conditions. I'm not native to Revit sorry if my terminology is off. How does the `FilterCollector.WhereElementIsElementType()` determine it's an ElementType?

 

private static bool IsElementType(Element element)
{
    var typeId = element?.GetTypeId();
    if (typeId == null || typeId == ElementId.InvalidElementId)
    {
        if (!(element is Room) && !(element is PipeSegment))
        {
            return true;
        }
    }    
    return false;
}

 

0 Likes
Reply
Accepted solutions (1)
2,509 Views
5 Replies
Replies (5)

RPTHOMAS108
Mentor
Mentor
Accepted solution

 

//This will return false if type is ElementType but true if is derived from ElementType
bool Bl = element.GetType().IsSubclassOf(typeof(ElementType));

//This will return true if type is ElementType or is derived from ElementType
bool B2 = typeof(ElementType).IsAssignableFrom(element.GetType());

 

0 Likes

RPTHOMAS108
Mentor
Mentor

Here's a more Revit orientated approach you asked how a collector determines an ElementType but you can always pass a list of a single element into a collector and filter that:

 

Dim FEC As New FilteredElementCollector(element.Document, {element.Id}.ToList)
Dim B3 As Boolean = FEC.WhereElementIsElementType.ToElementIds.Count > 0

 

You can't do it in C# in this abbreviated fashion due to the need to use ICollection<T> directly (option Strict off in vb)  but you can do similar.

0 Likes

mhannonQ65N2
Advocate
Advocate

Here's a simpler way of putting it.

private static bool IsElementType(Element element)
{
    return element is ElementType;
}

I'd suggest not even making a method for this and just using the is expression.

Sean_Page
Collaborator
Collaborator

I use the "is" operator quite often in my work.

 

Here is an online method which will give you the EType as a variable:

 

if(element is ElementType EType)
{
    TaskDialog.Show("Type Name",EType.Name);
}
Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes

RPTHOMAS108
Mentor
Mentor

Interesting to note that in VB the 'is' statement only returns true if the exact same type is compared. Since 'is' is used to check if two things are the same object instance. The equivalent to the way it is used in C# seems to be:

 

Dim B1 As Boolean = TypeOf Element Is ElementType

 

Which is something I've never really used to be honest.

0 Likes