GetElement method and Get Element Type

In the far distant past, the Revit API boasted a property on the Element class that gave direct access to the element type.
It was declared obsolete, and a new way to access the element type had to be implemented, as we discussed

two years back
.
This can still raise an issue even today:

Question: I have an old plug-in that I am trying to update to Revit 2013. The code uses a property (element.ObjectType) that has been made obsolete and that is no longer in the 2013 API. The obsolete call uses objElement.ObjectType.Parameters. The suggested replacement (Element.GetElementType) does not have a Parameters property. So, my question is, in Revit 2013, how do I get the Type parameters from an element in a project?
Here is a section of my code containing this obsolete call:


  // Repeat the loop for the Type parameters 
  // on the element and assign values
  foreach( Parameter param in
    objElement.ObjectType.Parameters )
  {
    // . . . 
  }

Answer: The Element ObjectType property was actually declared deprecated quite a few releases back, and still remained dangling until a whole host of obsolete calls were cleaned up in the Revit 2013 API.

The replacement is simple:

Use GetTypeId to obtain the element id of the type of your element, and then use the document GetElement method to access the type object itself from its id. Here is the code to achieve that:


  ElementId id = e.GetTypeId();
 
  ElementType type = doc.GetElement( id )
    as ElementType;

I discussed this

two years back
.
Since then, another change occurred as well, which Saikat mentioned in his

macro migration notes
:

The Document.Element property taking an element id argument has been converted to a method named GetElement in Revit 2013.

The obsolete Element property was converted to a method with a “get_” prefix in C#, so it had to be accessed by calling get_Element, which sometimes

caused a bit of confusion
.

Anyway, translated to your example code snippet, the new access might look like this in Revit 2013:


  // Repeat the loop for the element 
  // type parameters and assign values
 
  Element elementType = doc.GetElement(
    e.GetTypeId() );
 
  foreach (Parameter param in elementType.Parameters)

Element Display Overrides and Visibility Hierarchy

Revit provides a large number of ways to affect and override the display of an element, and it may not always be clear which method has the final say.

We had a look at one of these ways when describing how to

change element colour using ProjColorOverrideByElement
.
<!–

The developer guide has little to say about how this can be managed programmatically. –>

The Revit Clinic provides a useful overview of the

Revit visibility hierarchy
which
helps understand the various possibilities and how they relate to each other.

Happy Easter!

I wish a Happy Easter to everybody celebrating this holiday, and a good time to everybody else as well!

Easter bunny postcard

Good luck and much success with your Revit add-in development efforts and all other important aspects of life, such as love, peace, and happiness for all beings :-)


Comments

4 responses to “GetElement method and Get Element Type”

  1. I am also in the process of upgrading some existing code in Revit 2012 and was running into an issue. I was previously using famiysymbol class to reference light fixture types. In the process of upgrading my FamilySymbols to ElementTypes, I noticed that the Family property is missing. It is no longer an option in the API as it was with FamilySymbol.family and I did not see any reference to it in the DB snoop (although Light Fixtures are still classified as FamilySymbols in Revit 2012). Any idea how I can access the family of an ElementType? Thanks.

  2. Aleksey Avatar
    Aleksey

    How to get the element in Revit 2011 API?

  3. Dear Aleksey,
    In C#, you can use
    doc.get_Element( id );
    Cheers, Jeremy.

  4. i s s ramesh kumar Avatar
    i s s ramesh kumar

    Hai JEREMY TAMMIK
    how do i retrieve the element parameters properties and their values

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading