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.

Merge Text Objects in AutoCAD: Combine Multiple Strings Fast

Combine and merge multiple TEXT and MTEXT objects into a single string in AutoCAD using CTX.lsp. Free AutoLISP script to speed up text editing.
Combine and Merge Multiple Texts Instantly

When working with exported schedule tables, title block annotations, or scattered notes in AutoCAD, you often end up with fragmented text objects that really belong in a single line. Merging them manually usually means double-clicking one text string, copying it, editing the adjacent text, and pasting it over—only to repeat that process for every single word or callout.

The CTX.lsp (Combine Text) routine replaces that entire manual copy-paste loop. It grabs multiple separate TEXT or MTEXT entities, automatically sorts them in reading order from left to right, and creates a clean combined text string right where you place it.


Why Manually Merging Text Wastes Your Time

Merging text in standard AutoCAD without a dedicated script comes with several headaches:

  • Typo risks: Manually retyping text strings to join them often introduces spelling mistakes or missed spaces.

  • Loss of layout order: If you select objects in a random order using standard selection boxes, basic scripts can scramble the text sequence. CTX.lsp automatically sorts selected text items from left to right based on their X-coordinates.

  • Preserves text properties: The new combined text inherits the layer, text height, and text style of the first text item in the selection, keeping your drawing styles perfectly uniform.


Step-by-Step Instructions

  1. Load CTX.lsp into AutoCAD using the APPLOAD command.

  2. Type CTX in the command line and hit Enter.

  3. Select the text objects you want to merge (you can click individual text items or drag a selection window over all target text).

  4. Pick an insertion point on the screen where you want your new merged text string placed.

  5. The combined text is generated instantly, leaving your original text untouched in case you still need it for reference.


AutoLISP Code (CTX.lsp)

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

(defun c:CTX ( / *error* acDoc cur_cm ss i ent entList mergedStr pt elist hgt lay sty)
  (vl-load-com)
  (setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (vla-StartUndoMark acDoc)
  
  (defun *error* (msg)
    (if cur_cm (setvar "CMDECHO" cur_cm))
    (vla-EndUndoMark acDoc)
    (princ "\nCAD Lisp [www.daolpro.com]")
    (princ)
  )

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

  (princ "\nSelect TEXT or MTEXT to combine: ")
  (if (setq ss (ssget '((0 . "TEXT,MTEXT"))))
    (progn
      (setq i 0 entList nil)
      (repeat (sslength ss)
        (setq ent (ssname ss i))
        (setq entList (cons ent entList))
        (setq i (1+ i))
      )
      (setq entList (vl-sort entList '(lambda (a b) 
                        (< (car (cdr (assoc 10 (entget a)))) (car (cdr (assoc 10 (entget b))))))))
      
      (setq mergedStr "")
      (foreach ent entList
        (setq mergedStr (strcat mergedStr (cdr (assoc 1 (entget ent)))))
      )
      
      (setq pt (getpoint "\nSpecify insertion point for merged text: "))
      (if pt
        (progn
          (setq elist (entget (car entList)))
          (setq hgt (cdr (assoc 40 elist)))
          (setq lay (cdr (assoc 8 elist)))
          (setq sty (cdr (assoc 7 elist)))
          (entmake (list
                     '(0 . "TEXT")
                     '(100 . "AcDbEntity")
                     (cons 8 lay)
                     '(100 . "AcDbText")
                     (cons 10 pt)
                     (cons 40 hgt)
                     (cons 1 mergedStr)
                     (cons 7 sty)
                   ))
        )
      )
    )
  )
  
  (if cur_cm (setvar "CMDECHO" cur_cm))
  (vla-EndUndoMark acDoc)
  (princ "\nCAD Lisp [www.daolpro.com]")
  (princ)
)

Quick Customization Tip

By default, this script merges the selected strings directly end-to-end without adding spaces between them (ideal for part numbers or codes). If you prefer a space between merged words, you can edit the string concatenation line in the LISP code from (strcat mergedStr ...) to include a space " ".

Post a Comment