[AutoCAD LISP] Match Text Content Instantly Across Multiple Objects (MTC.lsp)
Have you ever found yourself frustrated while double-clicking and
copy-pasting the exact same text over and over again in AutoCAD?
Without this LISP, updating multiple annotations, grid labels, or room
numbers with the exact same value is a tedious chore. You have to open the
source text, copy it, escape out, open the target text, paste it, and repeat
for every single object. It’s a repetitive, click-heavy process that drains
your productivity, kills your wrist, and opens the door for manual typos.
Why You Should Use the 'MTC' LISP
- Instant Batch Updates: Change dozens of text and mtext items to match your source text in just a few clicks.
- Zero Typos: Eliminates human error completely because it extracts and copies the string data directly via the backend.
- Keeps Formatting: It only alters the content (string), leaving your target text layers, heights, text styles, and colors completely untouched
How It Works (Step-by-Step Guide)
Here is a simple breakdown of how to use the MTC command once you load it
into AutoCAD:
- Launch the Command: Type MTC into the command line and hit Enter.
- Select Source: The script prompts you to select a single Source Text (Works perfectly with both TEXT and MTEXT).
- Store Content: The LISP extracts and remembers the exact text string from your selected source object.
- Select Targets: You are then prompted to select the Destination Texts. You can select them one by one, or use a window/crossing selection to grab multiple text objects at once.
- Batch Overwrite: The code loops through your target selection and replaces the contents instantly.
- Confirmation: It displays a neat success message in the command line showing exactly how many text objects were updated.
Wrapping Up
This Match Text Content (MTC) utility is a massive timesaver for heavy
documentation, re-numbering drawings, or syncing repeated notes. By
eliminating repetitive copy-paste actions, it keeps your eyes on the design
and streamlines your drafting workflow. Give it a try in your next drafting
session, and let me know how much time it saved you in the comments below!
The LISP Code
Copy the code below, save it as a .lsp file (e.g.,
MTC_MatchTextContent.lsp), and load it into AutoCAD using the APPLOAD
command.
(defun c:MTC ( / ssSource entSource strSource ssTarget i entTarget objTarget *error*)
;; Error handling to ensure a clean exit
(defun *error* (msg)
(if (not (member msg '("Function cancelled" "quit / exit *Cancel*")))
(princ (strcat "\nError: " msg))
)
(vl-load-com)
(princ)
)
(vl-load-com)
;; Step 1: Select single source text
(setq ssSource (ssget ":S" '((0 . "TEXT,MTEXT"))))
(if ssSource
(progn
(setq entSource (ssname ssSource 0))
(setq strSource (vla-get-TextString (vlax-ename->vla-object entSource)))
;; Step 2: Select multiple destination texts
(princ "\nSelect destination text(s) to match [Window selection allowed]: ")
(setq ssTarget (ssget '((0 . "TEXT,MTEXT"))))
(if ssTarget
(progn
(setq i 0)
;; Step 3: Loop and apply content change
(repeat (sslength ssTarget)
(setq entTarget (ssname ssTarget i))
(setq objTarget (vlax-ename->vla-object entTarget))
(vla-put-TextString objTarget strSource)
(setq i (1+ i))
)
(princ (strcat "\n>> Successfully matched " (itoa i) " text object(s)."))
)
(princ "\n-> No destination text selected.")
)
)
(princ "\n-> No source text selected.")
)
(princ)
)

Post a Comment