Get Element Type

In the Revit 2011 API, the Symbol class was been renamed to ElementType and some other changes were made to clarify the usage of element types in Revit, which were previously referred to as both types and symbols.
Some remnants of the previous naming conventions still exists, such as the FamilySymbol class and the FamilyInstance.Symbol property, but the long-term intention is to move toward calling these things element types instead of symbols.

This renaming and restructuring has causes some confusion here and there in porting existing applications, giving rise to questions such as the following:

Question: I have been updating my Revit 2010 API code to the 2011 version and realized that the Element.ObjectType property no longer exists.

I want to retrieve the AssemblyCode value from the element type properties in the Revit project.
Here is the old code for the Revit 2010 API:


  string assembly_code
    = e.ObjectType.get_Parameter(
      BuiltInParameter.UNIFORMAT_CODE )
      .AsString();

How can I rewrite this code for Revit 2011?

Answer: The first place to look in cases like this is the What’s New section of the Revit API help file RevitAPI.chm, which includes the following section:

Replacement for Symbol and properties that access types

The Symbol class has been renamed to ElementType.

The properties that access types from Elements have been replaced:

  • Old property: Element.ObjectType
    • New interfaces: Element.GetTypeId(), Element.ChangeTypeId()
    • Notes: ChangeTypeId() returns the id of a new element; in rare cases, changing the element type will result in deletion of the current element. Static versions which operate on sets of elements are also supplied.
  • Old property: Element.SimilarObjectTypes
    • New interfaces: Element.GetValidTypes()

So you can simply use GetTypeId and then open the associated ElementType object to access the parameter.

Some samples of using these new methods are given in the discussions on

The code that you show above can thus be rewritten as follows for the Revit 2011 API:


  ElementId id = e.GetTypeId();
 
  ElementType type = m_doc.get_Element( id )
    as ElementType;
 
  string AssemblyCode = type.get_Parameter(
    BuiltInParameter.UNIFORMAT_CODE )
      .AsString();

Comments

12 responses to “Get Element Type”

  1. nicholas.broadbent@hotmail.com Avatar
    nicholas.broadbent@hotmail.com

    Hi Jeremy
    Great Web site, I have a question that I hope you can answer it is about Annotation Familys or Tags.
    I’m trying to find out what Category Parameters a Label is displaying?
    I’m using C# and the API to open familys and can get the label object, I can even get the “sample text” but not the name or names of the Parameters being displayed by the Label in the family.
    This is getting so frustrating I know I’m so close, could you help?

  2. Dear Nicholas,
    Thank you for your appreciation and glad you find it useful.
    I checked with the development team about this, and unfortunately there is currently no way to obtain this information. I filed a request for it for you.
    Cheers, Jeremy.

  3. nicholas.broadbent@hotmail.com Avatar
    nicholas.broadbent@hotmail.com

    Hi thank you so much for getting back to me so fast, you have saved me hours of frustration and it is not possible yet!
    God if it was not for your blog I don’t think I would have attempted learning the Revit API.
    I need to be able find out what a Tag Family/Annotation Family is tagging? What does the label in the family Reference? Is there a way to do this?
    I can select the Label using the API but the only information I can get on what it References is the Sample Text of the “Category Parameters”.(Not very helpful as some times it is blank and I get nothing.)
    Is there a way to change the Sample text Values for each “Category Parameters” using the API?
    As I could do this then check the Sample Text of the Label, this would be a way of getting what Category a Label is referencing.
    Please let me know any help on this would be of Great Value to me
    Thank you
    Nick b

  4. Dear Nick,
    All I know about this is summarised in
    http://thebuildingcoder.typepad.com/blog/2009/04/tag-association.html
    A more recent and generic overview is given in
    http://thebuildingcoder.typepad.com/blog/2010/03/object-relationships.html
    I hope that the approaches described there will help.
    If you can use them, I would be interested in seeing your solution, and if you find an improved approach, I would be more interested still.
    Cheers, Jeremy.

  5. Dear Sir,
    How can i automatically apply/select New Type from Loaded family in Type Properties?
    Now whats happening is when i load a family, the type from family is coming below loaded types in Type properties.
    Thanks & Regards,
    Nitin J.

  6. Dear Nitin,
    I’m sorry to say you cannot. There is currently no official way to set the current type. A workaround for walls making use of the CS or Create Similar command was presented here:
    http://thebuildingcoder.typepad.com/blog/2010/11/launching-a-revit-command.html
    Cheers, Jeremy.

  7. Hello, Jeremy.
    In your post you write how to get ElementType of Element. But I have opposite situation: I need get all elements belong to particular ElementType.
    Something like this:
    ElementType elementType = /* get element type */;
    List elements = elementType.GetElements();
    Is it possible to do it via API?
    Thanks a lot, Victor.

  8. Dear Victor,
    Sure that is possible. The main issue is how to optimise the filter to retrieve the desired elements.
    Obviously, one filter that certainly can be applied is WhereElementIsNotElementType. In general, that will still return a huge number of elements. A rather simplistic approach would be to simply post-process the results of that by comparing the element id returned by the Element.GetTypeId method with the target element type id.
    I think that can be vastly improved, though: search for a parameter that is provided on all database elements which contains its element type id and use an element parameter filter.
    Here is my suggestion for an implementation:
    ///
    /// Return the all elements that
    /// use the given ElementType.
    ///
    static FilteredElementCollector
    GetAllElementsUsingType(
    Document doc,
    ElementType et )
    {
    // built-in parameter storing the type element id:
    BuiltInParameter bip
    = BuiltInParameter.ELEM_TYPE_PARAM;
    ParameterValueProvider provider
    = new ParameterValueProvider(
    new ElementId( bip ) );
    FilterNumericRuleEvaluator evaluator
    = new FilterNumericEquals();
    FilterRule rule = new FilterElementIdRule(
    provider, evaluator, et.Id );
    ElementParameterFilter filter
    = new ElementParameterFilter( rule );
    FilteredElementCollector collector
    = new FilteredElementCollector( doc )
    .WhereElementIsNotElementType()
    .WherePasses( filter );
    return collector;
    }
    Please test this for me in your context and let me know whether it fulfills your needs, and I’ll republish this as a proper blog post.
    Thank you!
    Best regards from AU in Las Vegas :-)
    Cheers, Jeremy.

  9. Hello, Jeremy! Thanks for your great answer!
    I’ve tested your code in my context. Yes, it works fine. When I use this function the first time it take little bit more time (about 1,5-2 seconds on big project) but next execution works really fast.
    I also thought about ElementIsElementTypeFilter and then compare GetTypeId() of each element. But it works very slow (8 seconds). Your solution is better!
    I always forget that many useful property of Revit element store in BuiltInParameters. Although I didn’t know about ELEM_TYPE_PARAM.
    Actually I need other task that I decided resolve with all elements of element type. I need List of possible parameters of Instance belongs to ElementType. For example, I have WallType and I need List of parameters (or parameter definitions) that Wall contains (Length, Area, Stage etc.). May be it is possible but I didn’t find way to achieve this and decided take first element of WallType and retrieve parameters of this element. If you know other way – please let me know.
    Thanks. Best Regards, Victor. Have a nice stay in Las Vegas ;)
    P.S. I also want to go to Las Vegas :)

  10. Dear Victor,
    Very glad to hear that it works!
    I think that is the right way to go … I can’t think of any other.
    I wish you were here!
    Cheers, Jeremy.

  11. Matt Brown Avatar
    Matt Brown

    How can I get the ElementType so that I can pass it into this function if I know the name of the element type ? For example, I know that I will have “Detail”, “Detail Section”, “Building Section”, and “Wall Section” ElementTypes in my drawing. However, there is no constructor defined for the ElementType class where I can pass in the name.

  12. Dear Matt,
    In general, use a filtered element collector.
    You use that to retrieve any kind of element from the Revit database given any kind of known search criteria.
    http://thebuildingcoder.typepad.com/blog/2010/12/filtered-element-collector-sample-overview.html
    http://thebuildingcoder.typepad.com/blog/2010/06/element-name-parameter-filter-correction.html
    Cheers, Jeremy.

Leave a Reply