What it does
Find & Replace Selected Text with the REPTEXT AutoLISP command
Use a literal find-and-replace operation on selected drawing text. It is useful for tag prefixes and repeated notes while keeping the scope visible and user-controlled.
- AutoLISP command
- REPTEXT
- Difficulty
- Intermediate
Applicable disciplines
CAD workflows that use this routine
Requirements and compatibility
What it needs
Software rows marked “Not tested” are not compatibility claims. Test the routine in a copy of your drawing and CAD version.
Downloadable source code
Review the rename-selected-text.lsp AutoLISP code
;; CADLisp Tools - Find & Replace Selected Text
(defun ct-rp:replace-all (text find replacement / position start)
(setq start 0)
(while (setq position (vl-string-search find text start))
(setq text (strcat (substr text 1 position) replacement
(substr text (+ position (strlen find) 1)))
start (+ position (strlen replacement))))
text)
(defun ct-rp:editable-p (data / flags row)
(and data
(setq row (tblsearch "LAYER" (cdr (assoc 8 data))))
(setq flags (cdr (assoc 70 row)))
(= 0 (logand flags 20))))
(defun ct-rp:text-value (data / pair value)
(setq value "")
(foreach pair data
(if (member (car pair) '(1 3)) (setq value (strcat value (cdr pair)))))
value)
(defun ct-rp:mtext-control-p (value)
(or (vl-string-search "\\" value)
(vl-string-search "{" value)
(vl-string-search "}" value)))
(defun ct-rp:text-pairs (value / pairs)
(setq pairs nil)
(while (> (strlen value) 250)
(setq pairs (cons (cons 3 (substr value 1 250)) pairs)
value (substr value 251)))
(reverse (cons (cons 1 value) pairs)))
(defun ct-rp:set-value (data value / inserted item new out pair)
(setq inserted nil new (ct-rp:text-pairs value) out nil)
(foreach pair data
(if (member (car pair) '(1 3))
(if (not inserted)
(progn
(foreach item new (setq out (cons item out)))
(setq inserted T)))
(setq out (cons pair out))))
(reverse out))
(defun c:REPTEXT (/ *error* changed data entity find index kind new old replacement set skipped undo-started)
(defun *error* (message)
(if undo-started (command-s "_.UNDO" "_End"))
(if (and message (not (wcmatch (strcase message) "*CANCEL*,*QUIT*,*EXIT*")))
(prompt (strcat "\nREPTEXT error: " message)))
(princ))
(setq find (getstring T "\nFind text: "))
(if (= find "")
(prompt "\nFind text cannot be blank.")
(progn
(setq replacement (getstring T "\nReplace with: "))
(prompt "\nSelect TEXT and unformatted MTEXT to update: ")
(if (setq set (ssget '((0 . "TEXT,MTEXT"))))
(progn
(setq index 0 changed 0 skipped 0)
(if (= 0 (logand 8 (getvar "UNDOCTL")))
(progn (command "_.UNDO" "_Begin") (setq undo-started T)))
(repeat (sslength set)
(setq entity (ssname set index) data (and entity (entget entity))
kind (and data (cdr (assoc 0 data))) old (and data (ct-rp:text-value data)))
(if (and (ct-rp:editable-p data) old
(not (and (= kind "MTEXT")
(or (ct-rp:mtext-control-p old)
(ct-rp:mtext-control-p replacement)))))
(progn
(setq new (ct-rp:replace-all old find replacement))
(if (/= old new)
(if (entmod (ct-rp:set-value data new))
(progn (entupd entity) (setq changed (1+ changed)))
(setq skipped (1+ skipped)))))
(setq skipped (1+ skipped)))
(setq index (1+ index)))
(if undo-started (progn (command "_.UNDO" "_End") (setq undo-started nil)))
(prompt (strcat "\nUpdated " (itoa changed) " object(s); skipped " (itoa skipped)
" locked, referenced, invalid, or formatted MTEXT object(s).")))
(prompt "\nNo text selected."))))
(princ))
Setup
How to install the rename-selected-text.lsp file
- 1Download the LISP file.
- 2Load it with APPLOAD in a test drawing.
- 3Confirm the selection set before editing.
Command workflow
How to run the REPTEXT command
- 1Run REPTEXT.
- 2Enter the exact case-sensitive text to find.
- 3Enter the replacement.
- 4Select TEXT and unformatted MTEXT to process.
Worked example
Example: Change prefix PNL- to DB- in selected tags.
- Enter PNL- as the find value.
- Enter DB- as the replacement.
- Window only the intended labels.
Matching fragments are replaced without re-scanning inserted text.
Read before use
Notes and limitations
- Matching is case-sensitive.
- Formatted MTEXT is skipped to avoid changing control codes.
- Locked, xref-layer, and invalid entities are skipped.
- Requires Visual LISP vl-string-search support.
- Not manually CAD-tested.
Version history