|

TotalLength.lsp: High-Efficiency Linear Quantity Takeoff Tool for AutoCAD

Program Description

TotalLength.lsp is a high-performance AutoLISP utility engineered for CAD managers and quantity surveyors who need fast, accurate linear measurements without breaking workflow. Unlike standard AutoCAD commands that process objects one by one, this routine aggregates the length of multiple entities—including lines, arcs, splines, circles, ellipses, and all polyline variants—into a single cumulative value.

The script is built on the Visual LISP curve-evaluation framework (vlax-curve-*), which ensures mathematical consistency across complex geometries, including NURBS splines and elliptical segments. It also uses defensive error handling (vl-catch-all-apply) to avoid command failure when encountering problematic entities.

This is not a convenience tool. It is designed for production use in:

  • construction quantity takeoffs
  • material estimation workflows
  • infrastructure audits
  • large-scale CAD QA checks

Program Source Code

Below is the complete implementation of the TOTLEN command as used in production environments:

;;; ==========================================================================
;;; TotalLength.lsp
;;; Command: TOTLEN
;;; Description: Calculates the total length of selected lines, arcs, 
;;;              polylines, splines, circles, and ellipses.
;;; ==========================================================================

(defun c:TOTLEN (/ ss i count totalLen entName obj len)
  (vl-load-com) 

  (setq totalLen 0.0)

  (prompt "\nSelect curves (lines, arcs, polylines, splines, circles, ellipses): ")

  (if (setq ss (ssget '((0 . "LINE,ARC,LWPOLYLINE,POLYLINE,SPLINE,CIRCLE,ELLIPSE"))))
    (progn
      (setq i 0)
      (setq count (sslength ss))

      (while (< i count)
        (setq entName (ssname ss i))
        (setq obj (vlax-ename->vla-object entName))
        (setq len nil)

        (if (not (vl-catch-all-error-p 
                   (setq len (vl-catch-all-apply 
                               'vlax-curve-getDistAtParam 
                               (list obj (vlax-curve-getEndParam obj))))))
          (setq totalLen (+ totalLen len))
        )

        (setq i (1+ i))
      )

      (princ "\n========================================")
      (princ (strcat "\nTOTAL LENGTH: " (rtos totalLen 2 4)))
      (princ "\n========================================")

      (alert (strcat "Total length of " (itoa count) " selected objects:\n\n" (rtos totalLen 2 4)))
    )
    (princ "\nNo valid curve objects selected.")
  )

  (princ)
)

(princ "\nTotalLength.lsp loaded. Type TOTLEN to run the command.")
(princ)

Step-by-Step Implementation

Script Preparation

Copy the AutoLISP code into a text editor such as (or download the zipped file here):

  • Notepad
  • VS Code

Save the file as:

TotalLength.lsp

Make sure the encoding is clean (ANSI or UTF-8 without BOM).


Loading the Routine

  1. In AutoCAD, type:
    APPLOAD
    
  2. Browse to your .lsp file
  3. Click Load
  4. Accept the security prompt if it appears

For frequent use, add the file to the Startup Suite inside APPLOAD.

Reference: https://help.autodesk.com/view/ACD/2025/ENU/?guid=GUID-APPLOAD


Command Execution

Type:

TOTLEN

The routine initializes immediately.


Object Selection

Select your objects using:

  • window selection
  • crossing selection
  • manual picking

The routine automatically filters for valid curve objects:

  • LINE
  • ARC
  • LWPOLYLINE / POLYLINE
  • SPLINE
  • CIRCLE
  • ELLIPSE

Non-relevant entities (text, blocks, hatches) are ignored.


Data Retrieval

Press Enter to confirm selection.

The result is:

  • displayed in a popup alert
  • printed in the command line
  • preserved in command history for traceability

Technical Compatibility & Optimization

This utility is optimized for:

  • AutoCAD 2024
  • AutoCAD 2025
  • AutoCAD 2026+

Why it works reliably

Most legacy LISP routines rely on the Length property. That approach fails on:

  • splines
  • ellipses
  • certain polylines
  • corrupted geometry

This routine avoids that entirely by using:

  • vlax-curve-getDistAtParam
  • vlax-curve-getEndParam

These functions evaluate the curve geometrically, not by property lookup.

Reference: https://help.autodesk.com/view/ACD/2025/ENU/?guid=GUID-7F7C0E4E


Error Handling Strategy

The use of:

vl-catch-all-apply

prevents hard failures when encountering:

  • invalid entities
  • zero-length objects
  • proxy geometry
  • corrupted DWG elements

This ensures the command runs to completion even in dirty production drawings.


Practical Use Cases

Quantity Takeoff (QTO)

  • Total cable length
  • Pipe routing
  • Road centerlines
  • Rebar path estimation

Drawing Audit

  • Verify total geometry length before export
  • Cross-check with BOQ values
  • Detect missing segments

Pre-Fabrication Workflows

  • Generate cutting lengths
  • Validate CNC input geometry

Performance Notes

  • Handles large selection sets efficiently
  • Minimal overhead per object
  • No dependency on external libraries
  • Works in both Model Space and Paper Space

For very large datasets (10,000+ entities), performance depends mainly on:

  • DWG integrity
  • hardware (RAM / CPU)
  • presence of proxy objects

If you plan to deploy this in a team environment, consider extending it with:

  • unit conversion (mm → m, ft → m)
  • length by layer breakdown
  • export to AutoCAD table
  • CSV export for Excel workflows
  • clipboard copy (for reports)

Common Pitfalls

Mixed Units

If your drawing is in millimeters but your report is in meters, results will be off.

Check:

INSUNITS

Proxy Objects

Objects from Civil 3D or Plant 3D may not behave like standard curves.


Corrupt Geometry

Zero-length or broken splines can still exist in production drawings. This routine skips them safely but does not repair them.


Best Practices

  • Run OVERKILL before using TOTLEN on imported drawings
  • Clean DWG with AUDIT
  • Use layer filters to isolate relevant geometry before selection

Reference: https://help.autodesk.com/view/ACD/2025/ENU/?guid=GUID-OVERKILL


FAQ

What objects are supported?

All standard curve types:

  • Lines
  • Arcs
  • Polylines (LW + 2D/3D)
  • Splines
  • Circles
  • Ellipses

Why not use the built-in LENGTH command?

Because it:

  • works on one object at a time
  • does not provide a cumulative total
  • slows down significantly in batch workflows

Is the result precise?

Yes. The calculation is based on curve evaluation, not approximations.

Precision depends only on:

  • drawing accuracy
  • unit system

Can I trust it on large projects?

Yes. The routine is designed to handle:

  • large selections
  • mixed geometry
  • imperfect drawings

It will not crash on bad entities.


Can I modify the precision?

Yes. In the code:

(rtos totalLen 2 4)

Change 4 to:

  • 2 → less precision
  • 6 → more precision

Does it work in Civil 3D?

Partially.

Standard objects: OK Civil 3D objects (alignments, feature lines): not directly supported unless converted.


Can I automate it?

Yes. You can:

  • call it from scripts (.scr)
  • integrate into tool palettes
  • attach reactors for automatic updates