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.

https://boostyourbim.wordpress.com/2016/06/21/total-length-of-multiple-lines/
undefined

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)
            continue;
        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
https://help.autodesk.com/view/RVT/2021/ENU/?guid=Revit_API_Revit_API_Developers_Guide_Introduction_Application_and_Document_Units_html

What’s New in 2021 API
https://thebuildingcoder.typepad.com/blog/2020/04/whats-new-in-the-revit-2021-api.html#4.1.3

Advertisement