[AutoCAD LISP] Combine and Merge Multiple Texts Instantly (CTX.lsp)
Have you ever found yourself manually retyping multiple individual text
objects or copying and pasting them one by one just to merge them into a
single line? It is a tedious, repetitive task that easily leads to human
error, missed typos, and a massive waste of drawing time.
Without this LISP, combining separate text elements in AutoCAD requires
tedious workarounds. You either have to manually edit an existing text box and
type everything out, or rely on complex explode/join workarounds that mess up
your text alignment, formatting, and layer settings.
The CTX LISP solves this problem entirely. It allows you to select multiple
Text or MText objects—either by picking them individually in your preferred
order or via a window selection—and instantly merges them into a single, clean
text object placed exactly where you want it.
Why You Should Use the 'CTX' LISP
- Effortless Text Consolidation: Instantly merge multiple scattered text strings into a single text entity without manual copying and pasting.
- Intelligent Selection Modes: Supports both individual point-and-click selection (allowing custom text ordering) and standard bounding-box crossing selection.
- Smart Directional Sorting: When using window selection, the LISP automatically detects your selection drag direction (left-to-right, top-to-bottom, etc.) to sort and merge the text in a logical reading order.
- Preserved Formatting: The newly generated text automatically inherits the height, layer, and text style of the first text object you selected, ensuring perfect drawing consistency.
How It Works (Step-by-Step Guide)
Here is a step-by-step breakdown of how to use the CTX command.
- Launch the Command: Type CTX in the AutoCAD command line and press Enter.
- Select First Text or Area: Click on the first text object you want to merge, or click an empty space to start a crossing window selection.
- Refine Your Selection:
- Individual Method: Continue clicking additional text objects in the exact order you want them merged. If you accidentally click the wrong text, simply click it again to remove it from the selection list. Press Enter when finished.
- Window Method: If you specified a corner in Step 2, drag your selection box to cover all target texts. The LISP will automatically highlight and sort them based on your selection box direction.
- Specify Insertion Point: Click on the screen where you want to place your newly merged text string.
- Completion: The LISP will instantly generate the combined text and reset the original highlighted texts back to their normal display state.
Wrapping Up
The CTX LISP is an essential addition to any CAD professional’s toolkit,
designed to eliminate the frustration of fragmented text strings. By
automating the text-merging process, it helps maintain a clean drawing
structure, minimizes typing mistakes, and significantly accelerates your
documentation and design workflow.
Try incorporating this into your daily CAD drafting sessions and see how
much time you save!
The LISP Code
(defun c:CTX ( / p1 ss1 entList run p ssNext ent p2 ss dx dy i elist hgt lay sty mergedStr pt)
(vl-load-com)
(setq p1 (getpoint "\nSelect text or specify first corner: "))
(if p1
(progn
(setq ss1 (ssget p1 '((0 . "TEXT,MTEXT"))))
(if ss1
(progn
(setq ent (ssname ss1 0))
(redraw ent 3)
(setq entList (list ent))
(setq run T)
(while run
(setq p (getpoint "\nSelect next text (Click selected again to remove) [Enter to finish]: "))
(if p
(progn
(setq ssNext (ssget p '((0 . "TEXT,MTEXT"))))
(if ssNext
(progn
(setq ent (ssname ssNext 0))
(if (member ent entList)
(progn
(redraw ent 4)
(setq entList (vl-remove ent entList))
)
(progn
(redraw ent 3)
(setq entList (append entList (list ent)))
)
)
)
)
)
(setq run nil)
)
)
)
(progn
(setq p2 (getcorner p1 "\nSpecify opposite corner: "))
(if p2
(progn
(setq ss (ssget "_C" p1 p2 '((0 . "TEXT,MTEXT"))))
(if ss
(progn
(setq dx (- (car p2) (car p1)))
(setq dy (- (cadr p2) (cadr p1)))
(setq i 0)
(setq entList nil)
(repeat (sslength ss)
(setq ent (ssname ss i))
(redraw ent 3)
(setq entList (cons ent entList))
(setq i (1+ i))
)
(if (> (abs dx) (abs dy))
(if (> dx 0)
(setq entList (vl-sort entList '(lambda (a b) (< (car (cdr (assoc 10 (entget a)))) (car (cdr (assoc 10 (entget b))))))))
(setq entList (vl-sort entList '(lambda (a b) (> (car (cdr (assoc 10 (entget a)))) (car (cdr (assoc 10 (entget b))))))))
)
(if (> dy 0)
(setq entList (vl-sort entList '(lambda (a b) (< (cadr (cdr (assoc 10 (entget a)))) (cadr (cdr (assoc 10 (entget b))))))))
(setq entList (vl-sort entList '(lambda (a b) (> (cadr (cdr (assoc 10 (entget a)))) (cadr (cdr (assoc 10 (entget b))))))))
)
)
)
)
)
)
)
)
(if entList
(progn
(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)))
(cons 8 (cdr (assoc 8 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)
))
)
)
(foreach ent entList
(redraw ent 4)
)
)
)
)
)
(princ)
)

Post a Comment