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.

Label and Sum Object Lengths Instantly in Meters

Free AutoCAD LISP to measure lengths, convert to meters, and sum multiple lines.

 [AutoCAD LISP] Label and Sum Object Lengths Instantly in Meters (LM.lsp)

Label and Sum Object Lengths Instantly in Meters

How many times have you found yourself manually checking the properties of a line, polyline, or arc, only to convert millimeter units into meters in your head, and then manually type out a text label?

Without a LISP script, measuring and labeling object lengths in AutoCAD is an exhausting, multi-step chore. You have to open the Properties palette, find the length, divide it by 1,000, and then create a separate text object to place on the drawing. If you need to find the cumulative total of multiple segments, you are stuck using the MEASUREGEOM or JOIN command, or worse—adding them up with a calculator. It’s a tedious, repetitive process that slows down your drafting workflow and opens the door to human error.

Why You Should Use the 'LM' LISP

  • Instant Metric Conversion: Automatically converts your drawing units (assuming millimeters) into meters by dividing by 1,000 and appends the "m" unit suffix instantly.
  • Smart Text Placement: In Single mode, the LISP calculates the exact midpoint of lines, polylines, or arcs, and places a perfectly centered text label right on top of the object.
  • Efficient Cumulative Summation: Switch to Multiple mode to select dozens of objects at once and output a beautifully formatted "Total: X.XXXm" text at any insertion point you choose.
  • On-the-Fly Customization: You can adjust the text height and switch between Single or Multiple modes directly within the command line options without interrupting your command flow.

How It Works (Step-by-Step Guide)

Before you start, load the LM.lsp file into your AutoCAD using the APPLOAD command.
  1. Launch the Command: Type LM in the command line and hit Enter.
  2. Choose Your Mode/Settings: The script prompts you with options: [Single/Multiple/Unit].
  • Type U (Unit) first if you wish to adjust the text height before placing labels.
  • Type S (Single) to label objects individually.
  • Type M (Multiple) to calculate the total sum of multiple objects.
  1. Select Objects (Single Mode): If you are in Single mode, simply click on any Line, Polyline, or Arc. The LISP will instantly place a text label (e.g., 12.345m) right at the object's midpoint. The command repeats so you can keep clicking other objects.
  2. Select Objects (Multiple Mode): If you are in Multiple mode, use a crossing window or click to select all the Lines, Polylines, or Arcs you want to sum up, then press Enter.
  3. Pick Output Point: For Multiple mode, the script will prompt you to pick a point on your screen to place the final calculation text (e.g., Total: 45.678m).

Wrapping Up

The Length in Meters (LM.lsp) utility is an essential time-saver for civil engineers, architects, and designers who constantly need to annotate lengths or prepare take-offs. By eliminating the need for manual conversions and manual text creation, it keeps your focus where it belongs—on design precision and productivity.

Try implementing this into your daily CAD routine and see how many clicks you save!

The LISP Code


(defun c:LM ( / *error* cur_os cur_cm temp_mode temp_size temp_ent ss i ent len total_len mid_pt txt_val txt_pt)
  (defun *error* (msg)
    (if cur_os (setvar "OSMODE" cur_os))
    (if cur_cm (setvar "CMDECHO" cur_cm))
    (if (not (member msg '("Function cancelled" "quit / exit abort")))
      (princ (strcat "\nError: " msg))
    )
    (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)))
          ;; Fix: Align group 10 and 11 to prevent text displacement during middle-center alignment (72=1, 73=2)
          (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)
  (princ)
)
(vl-load-com)

Post a Comment