2021 changes Units

Autodesk made a bunch of changes to the Revit API for Units in 2021. Many frequently-used methods are now marked as obsolete. They still work just fine in 2021 but Autodesk will probably remove them in 2022.

For one example of how to update your code, this old sample computes the total length of all selected objects and shows the result as a formatted string. The new code, which uses the method UnitFormatUtils.Format Method (Units, ForgeTypeId, Double, Boolean) is shown below.


public void lineLength()
    double length = 0;
    Document doc = this.ActiveUIDocument.Document;
    UIDocument uidoc = this.ActiveUIDocument;
    ICollection<ElementId> ids = uidoc.Selection.GetElementIds();
    foreach (ElementId id in ids)
        Element e = doc.GetElement(id);
        Parameter lengthParam = e.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH);
        if (lengthParam == null)
        length += lengthParam.AsDouble();
    string lengthWithUnits = UnitFormatUtils.Format(doc.GetUnits(), SpecTypeId.Length, length, false);
    TaskDialog.Show("Length", ids.Count + " elements = " + lengthWithUnits);

More info on the new Units API is at

Revit Developer Guide documentation

What’s New in 2021 API