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);
}

getting Dimension Segment data

Question: Can we use the API to automatically add up all of the dimension segments in a single string of dimensions? I want to use it to check the sum of a string of dimensions against an overall dimension for coordination purposes.

Answer:

Capture

public void DimensionSegments()
{
    Document doc = this.ActiveUIDocument.Document;
    UIDocument uidoc = this.ActiveUIDocument;
    Dimension dim = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element)) as Dimension;
    double total = 0;
    string segmentsWithText = "";
    string segmentsNoText = "";
    string segmentValues = "";
    foreach (DimensionSegment dimSeg in dim.Segments)
    {
        total += (double)dimSeg.Value;
        
        segmentsWithText += dimSeg.ValueString + Environment.NewLine;
        
        string segmentString = dimSeg.ValueString;
        if (dimSeg.Above != null && dimSeg.Above != "")
            segmentString = segmentString.Replace(dimSeg.Above,"");
        if (dimSeg.Prefix != null && dimSeg.Prefix != "")
            segmentString = segmentString.Replace(dimSeg.Prefix,"");
        if (dimSeg.Suffix != null && dimSeg.Suffix != "")
            segmentString = segmentString.Replace(dimSeg.Suffix,"");
        if (dimSeg.Below != null && dimSeg.Below != "")
            segmentString = segmentString.Replace(dimSeg.Below,"");
        segmentsNoText += segmentString.TrimStart(' ') + Environment.NewLine;
    
        segmentValues += dimSeg.Value + Environment.NewLine;
    }
    TaskDialog.Show("Total Dimension", 
                    "Sum of lengths: " + total + Environment.NewLine + Environment.NewLine +
                    "Segment text:" + Environment.NewLine + segmentsWithText + Environment.NewLine + 
                    "Segments no text:" + Environment.NewLine + segmentsNoText + Environment.NewLine + 
                    "Segment values:" + Environment.NewLine + segmentValues);
}