Here's a way to get spaces to work when you type them in at the start and end of a string. You need to set dynmode to 0 to allow this.
Credit to Lee Mac for the main replacement function.
;;
;; Text_Prefix_Suffix_Sel_Single
;;
;; This will allow only a single selection.
;; With this you can modify single ATTRIB,TEXT,MTEXT,MULTILEADER objects each at a time.
;;
;; Author: 3dwannab on 2024.06.09
;; Dependencies: Lee Macs pstext - Found here: https://www.cadtutor.net/forum/topic/35009-prefixsuffix-add-to-numbers-or-text/?do=findComment&comment=284307
;; or the mod of mine found in this code.
;;
(defun c:Text_Prefix_Suffix_Single_Sel (/ acDoc prefix suffix var_cmdecho var_dynmode var_dynprompt)
(vl-load-com)
(defun *error* (errmsg)
(and acDoc (vla-EndUndoMark acDoc))
(and errmsg
(not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*"))
(princ (strcat "\n<< Error: " errmsg " >>\n"))
)
(setvar 'cmdecho var_cmdecho)
(setvar 'dynmode var_dynmode)
(setvar 'dynprompt var_dynprompt)
)
; Start the undo mark here
(setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
(or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc))
; Get any system variables here
(setq var_cmdecho (getvar "cmdecho"))
(setq var_dynmode (getvar "dynmode"))
(setq var_dynprompt (getvar "dynprompt"))
; Set any system variables here
(setvar 'cmdecho 0)
(setvar 'dynmode 0) ;; This needs to be set to 0 in order for whitespaces to be added after a prefix and before a suffix.
(setvar 'dynprompt 0) ;; This needs to be set to 0 in order for whitespaces to be added after a prefix and before a suffix.
(if
(and
(setq prefix (getstring T (strcat "\nSpecify prefix string for ATTRIB,TEXT,MTEXT,MULTILEADER: ")))
(setq suffix (getstring T (strcat "\nSpecify suffix string for ATTRIB,TEXT,MTEXT,MULTILEADER: ")))
)
(vla-pstext prefix suffix 0) ;; 0 here allows a single selection only. Supported objects are: ATTRIB,TEXT,MTEXT,MULTILEADER
)
(vla-EndUndoMark acDoc)
(*error* nil)
(princ)
)
;;
;; Text_Prefix_Suffix_Sel_Multiple
;;
;; This will allow mulitiple selections.
;; With this you can modify single TEXT,MTEXT,MULTILEADER objects.
;;
;; Author: 3dwannab on 2024.06.09
;; Dependencies: Lee Macs pstext - Found here: https://www.cadtutor.net/forum/topic/35009-prefixsuffix-add-to-numbers-or-text/?do=findComment&comment=284307
;; or the mod of mine found in this code.
;;
(defun c:Text_Prefix_Suffix_Sel_Multiple (/ acDoc prefix suffix var_cmdecho var_dynmode var_dynprompt)
(vl-load-com)
(defun *error* (errmsg)
(and acDoc (vla-EndUndoMark acDoc))
(and errmsg
(not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*"))
(princ (strcat "\n<< Error: " errmsg " >>\n"))
)
(setvar 'cmdecho var_cmdecho)
(setvar 'dynmode var_dynmode)
(setvar 'dynprompt var_dynprompt)
)
; Start the undo mark here
(setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
(or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc))
; Get any system variables here
(setq var_cmdecho (getvar "cmdecho"))
(setq var_dynmode (getvar "dynmode"))
(setq var_dynprompt (getvar "dynprompt"))
; Set any system variables here
(setvar 'cmdecho 0)
(setvar 'dynmode 0) ;; This needs to be set to 0 in order for whitespaces to be added after a prefix and before a suffix.
(setvar 'dynprompt 0) ;; This needs to be set to 0 in order for whitespaces to be added after a prefix and before a suffix.
(if
(and
(setq prefix (getstring T (strcat "\nSpecify prefix string for TEXT,MTEXT,MULTILEADER: ")))
(setq suffix (getstring T (strcat "\nSpecify suffix string for TEXT,MTEXT,MULTILEADER: ")))
)
(vla-pstext prefix suffix 1) ;; 1 here allows multiple selections. Supported objects are: TEXT,MTEXT,MULTILEADER
)
(vla-EndUndoMark acDoc)
(*error* nil)
(princ)
)
;; (vla-pstext "Prefix Text" "Suffix Text" <mode>)
;;
;; <mode> = 0 - single selection
;; = 1 - window selection
;;
;; Author: Lee Mac 2011 - www.lee-mac.com
;; Modified: 3dwannab on 2024.06.09
;; - Changed the method to the vla-put-TextString method.
;; - Added Multileaders to the objects that can be prefixed and suffixed.
;; - Added the selection of the objects in multi selection mode after the program is finished to prevent losing it.
(defun vla-pstext (preftext sufftext mode / a e i s o len)
(vl-load-com)
(cond
; Do the single selection mode
((= 0 mode)
(while
(progn
(setvar 'ERRNO 0)
(setq e (car (nentsel)))
(cond
((= 7 (getvar 'ERRNO))
(princ "\nMissed, try again.")
)
((eq 'ENAME (type e))
(if (wcmatch (cdr (assoc 0 (entget e))) "ATTRIB,TEXT,MTEXT,MULTILEADER")
;; If the correct ATTRIB,TEXT,MTEXT,MULTILEADER are picked
(progn
(setq o (vlax-ename->vla-object e))
(vla-put-TextString o (strcat preftext (vla-get-TextString o) sufftext)) ;; Replaces the content for ATTRIB,TEXT,MTEXT,MULTILEADER
(setq e (entget e)) ;; Leave this to last to keep picking
)
;; If objects above are not picked.
(princ "\nUnsupported Object.")
)
)
)
)
)
)
; Do the window selection mode
((setq s (ssget "_:L" (list '(0 . "TEXT,MTEXT,MULTILEADER"))))
(foreach tx
(mapcar 'vlax-ename->vla-object
(vl-remove-if 'listp
(mapcar 'cadr (ssnamex s))
)
)
(vla-put-TextString tx (strcat preftext (vla-get-TextString tx) sufftext))
)
;; Added selection of objects after so it's not lost
(if (> (sslength s) 0)
(progn
(sssetfirst nil s)
(command "_.regen")
(princ (strcat "\n: -------------------------\n" (itoa (setq len (sslength s))) (if (> len 1) " TEXT, MTEXT or MULTILEADER objects" " object") " changed.\n: -------------------------\n"))
)
(princ (strcat "\n: -------------------------\nNo TEXT, MTEXT or MULTILEADER objects found in selected object(s) !\n: -------------------------\n"))
)
)
)
(princ)
) ;; end vla-pstext defun
; (c:Text_Prefix_Suffix_Single_Sel) ;; Unlblock for testing
; (c:Text_Prefix_Suffix_Sel_Multiple) ;; Unlblock for testing