CADLISPMASTER
Stop wasting time on repetitive AutoCAD tasks. CAD LISP Master provides free, efficient AutoLISP codes and clear tutorials designed to automate your workflow and speed up your drafting.

AutoCAD Length to Meters: Label & Sum Segment Lengths

Automatically measure, label, and sum object lengths in meters in AutoCAD using LM.lsp. Convert mm to meters instantly with midpoint text placement.

Label and Sum Object Lengths Instantly in Meters

When working on site plans, piping layouts, or electrical cable runs, you frequently need to measure line segments and annotate their lengths in meters. However, since most architectural and engineering drawings are set up in millimeters, you end up doing mental math—dividing by 1,000—and manually creating text labels one by one.

The LM.lsp (Length in Meters) routine completely automates this calculation and labeling process. It converts millimeter drawing units straight into formatted meter labels (e.g., 12.345m) and places them right on your drawing. You can also switch to Multiple mode to quickly sum up dozens of lines or arcs for quantity take-offs.


What Makes LM.lsp Helpful?

  • Automatic Metric Conversion: Converts raw drawing units (mm) into meters on the fly and adds the m unit suffix automatically.

  • Centered Midpoint Placement: In Single mode, the script finds the exact midpoint of a line, polyline, or arc, placing a perfectly aligned text label directly over the object.

  • Instant Cumulative Summation: In Multiple mode, you can select an entire group of objects using a window selection to calculate and display the total combined length (Total: XX.XXXm).

  • Adjustable Text Size: You can tweak the text height setting directly within the command prompt without interrupting your work.


How to Run the Command

  1. Load LM.lsp into your AutoCAD session using the APPLOAD command.

  2. Type LM in the command prompt and hit Enter.

  3. Choose your desired mode from the prompt options [Single/Multiple/Unit]:

    • Type U to adjust the output text height first.

    • Type S for Single object labeling.

    • Type M for calculating cumulative totals across multiple objects.

  4. Single Mode: Click on any Line, Polyline, or Arc. A meter label will instantly appear at its midpoint. Keep clicking objects to continue.

  5. Multiple Mode: Select all the lines or arcs you want to measure, press Enter, then pick an insertion point on screen where you want the total length text placed.


AutoLISP Code (LM.lsp)

Copy the script below and save it as LM.lsp in your AutoCAD support path:

(defun c:LM ( / *error* acDoc cur_os cur_cm temp_mode temp_size temp_ent ss i ent len total_len mid_pt txt_pt)
  (vl-load-com)
  (setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (vla-StartUndoMark acDoc)
  
  (defun *error* (msg)
    (if cur_os (setvar "OSMODE" cur_os))
    (if cur_cm (setvar "CMDECHO" cur_cm))
    (vla-EndUndoMark acDoc)
    (princ "\nCAD Lisp [www.daolpro.com]")
    (princ)
  )

  (setq cur_os (getvar "OSMODE") cur_cm (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)

  (if (not global_mode) (setq global_mode "Single"))
  (if (not global_txt_size) (setq global_txt_size (getvar "TEXTSIZE")))

  (initget "Single Multiple Unit")
  (setq temp_mode (getkword (strcat "\nSelect option [Single/Multiple/Unit] <" global_mode ">: ")))
  
  (if (= temp_mode "Unit")
    (progn
      (setq temp_size (getreal (strcat "\nSpecify text height <" (rtos global_txt_size 2 2) ">: ")))
      (if temp_size (setq global_txt_size temp_size))
      (initget "Single Multiple")
      (setq temp_mode (getkword (strcat "\nSelect option [Single/Multiple] <" global_mode ">: ")))
      (if temp_mode (setq global_mode temp_mode))
    )
    (if temp_mode (setq global_mode temp_mode))
  )

  (if (= global_mode "Single")
    (while (setq temp_ent (entsel "\nSelect Line, Polyline, or Arc: "))
      (setq ent (car temp_ent))
      (if (member (cdr (assoc 0 (entget ent))) '("LINE" "LWPOLYLINE" "POLYLINE" "ARC"))
        (progn
          (setq len (* (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent)) 0.001))
          (setq mid_pt (vlax-curve-getPointAtParam ent (/ (+ (vlax-curve-getStartParam ent) (vlax-curve-getEndParam ent)) 2.0)))
          (entmake (list '(0 . "TEXT") '(100 . "AcDbEntity") '(100 . "AcDbText") 
                         (cons 10 mid_pt) (cons 40 global_txt_size) (cons 1 (strcat (rtos len 2 3) "m")) 
                         '(50 . 0.0) '(72 . 1) (cons 11 mid_pt) '(73 . 2)))
        )
        (princ "\nInvalid object selected.")
      )
    )
    (progn
      (princ "\nSelect Lines, Polylines, or Arcs to sum: ")
      (if (setq ss (ssget '((0 . "LINE,LWPOLYLINE,POLYLINE,ARC"))))
        (progn
          (setq total_len 0.0 i 0)
          (repeat (sslength ss)
            (setq ent (ssname ss i))
            (setq len (* (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent)) 0.001))
            (setq total_len (+ total_len len) i (1+ i))
          )
          (if (setq txt_pt (getpoint "\nPick insertion point for total length text: "))
            (entmake (list '(0 . "TEXT") '(100 . "AcDbEntity") '(100 . "AcDbText") 
                           (cons 10 txt_pt) (cons 40 global_txt_size) (cons 1 (strcat "Total: " (rtos total_len 2 3) "m")) '(50 . 0.0)))
          )
        )
        (princ "\nNo valid objects selected.")
      )
    )
  )

  (setvar "OSMODE" cur_os)
  (setvar "CMDECHO" cur_cm)
  (vla-EndUndoMark acDoc)
  (princ "\nCAD Lisp [www.daolpro.com]")
  (princ)
)

Quick Drafting Tip

If your drawing is already set up in meters (instead of millimeters), you can adjust the scale factor in the LISP code from 0.001 to 1.0 so it doesn't divide your measured values twice.

Post a Comment