Message 1 of 10
Find and Replace Text - Command Line
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
An idea that has been mentioned in quite a few threads I've explored looking for a simple command line approach that loosely resembles AutoCads native Find and Replace. This started out as a simple AI test to see how close it could get, but I was surprised at how accurate it was so I started refining it. This is a conglomeration of about 50/50 AI and human coding. It has passed every test I've thrown at it so far, and I should mention that I have no need for Blocks, Attributes, or Hyperlinks, so those are not included. This is a very simple and succinct function and I hope others find it useful either as-is, or as a start to expand upon.
;Find and Replace Text
(defun c:FRT (/ find replace rCount cCount row col cellText)
(setq find (getstring "\nEnter the text to find: "))
(setq replace (getstring "\nEnter the replacement text: "))
(if (ssget '((0 . "TEXT,MTEXT,DIMENSION,ACAD_TABLE,MULTILEADER")))
(progn
(vlax-for obj (vla-get-ActiveSelectionSet (vla-get-ActiveDocument (vlax-get-acad-object)))
(cond
((= (vla-get-ObjectName obj) "AcDbText")
(while (vl-string-search find (vla-get-TextString obj))
(vla-put-TextString obj (vl-string-subst replace find (vla-get-TextString obj)))
)
)
((= (vla-get-ObjectName obj) "AcDbMText")
(while (vl-string-search find (vla-get-TextString obj))
(vla-put-TextString obj (vl-string-subst replace find (vla-get-TextString obj)))
)
)
((= (vla-get-ObjectName obj) "AcDbMLeader")
(while (vl-string-search find (vla-get-TextString obj))
(vla-put-TextString obj (vl-string-subst replace find (vla-get-TextString obj)))
)
)
((= (wcmatch (vla-get-ObjectName obj) "AcDb*Dimension") T)
(while (vl-string-search find (vla-get-TextOverride obj))
(vla-put-TextOverride obj (vl-string-subst replace find (vla-get-TextOverride obj)))
)
)
((= (vla-get-ObjectName obj) "AcDbTable")
(setq rCount (vla-get-rows obj))
(setq cCount (vla-get-columns obj))
(setq row 0)
(while (< row rCount)
(setq col 0)
(while (< col cCount)
(setq cellText (vla-gettext obj row col))
(while (vl-string-search find cellText)
(setq cellText (vl-string-subst replace find cellText))
(vla-SetText obj row col cellText)
)
(setq col (1+ col))
)
(setq row (1+ row))
)
)
);cond
);vlax-for
)
)
(princ)
)
AutoCad 2018 (full)
Win 11 Pro
Win 11 Pro