Total length of multiple lines

There’s been a bunch of discussion in the Autodesk forum about how to get the total length of multiple elements.

http://forums.autodesk.com/t5/revit-architecture/how-to-determine-the-total-length-of-multiple-lines/td-p/3297855

Here’s an API solution that works with lines, walls, beams, conduit, or anything else with a “Length” parameter

Capture

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(), UnitType.UT_Length, length, false, false);
    TaskDialog.Show("Length", ids.Count + " elements = " + lengthWithUnits);
}

5 thoughts on “Total length of multiple lines

  1. Hey I’ve been using this amazing API, but in Revit 2021
    Autodesk.Revit.DB.UnitFormatUtils.Format and
    Autodesk.Revit.DB.UnitType
    became obsolete, rendering this Api ineffective
    what is the new Code?

Leave a comment