Nick-1971 Posted August 14, 2012 Posted August 14, 2012 Hello, I have seen a lots of posts regarding adding Prefix and Suffix to mulitple text items , i have just updated to 2012 and have lost my previous program. Can anyone help? Quote
Lee Mac Posted August 14, 2012 Posted August 14, 2012 Welcome to CADTutor Nick Would this function help you at all? Quote
Nick-1971 Posted August 14, 2012 Author Posted August 14, 2012 I need to edit around 1700 pieces of tree text so that they have the prefix G: and the suffix M, its not an uncommon thing for me to do so i thought that there must be away of doing it globally? Quote
Nick-1971 Posted August 14, 2012 Author Posted August 14, 2012 Might be making a complete muppet of myself but i cant see any other information on thread? Quote
Lee Mac Posted August 14, 2012 Posted August 14, 2012 Did you view the link I provided? (click on the word 'this' in my earlier post) Quote
Nick-1971 Posted August 14, 2012 Author Posted August 14, 2012 Thats great thanks, how do i chnage this to have the command word as an option in the drop down menu? Quote
Nick-1971 Posted August 14, 2012 Author Posted August 14, 2012 How can i make the prefix and suffix user defined? Quote
Lee Mac Posted August 14, 2012 Posted August 14, 2012 How can i make the prefix and suffix user defined? Use the getstring function to prompt the user for the prefix/suffix, e.g.: (defun c:test ( ) (pstext (getstring t "\nSpecify Prefix <none>: ") (getstring t "\nSpecify Suffix <none>: ") 1) ) 1 Quote
CAD Diva Posted June 17, 2020 Posted June 17, 2020 @Lee Mac I am not familiar with lisp routines but am willing to try this one because I am desperate. I have to add a prefix to over 30,000 text objects so this will help immensely. However, I cannot decipher where to put what. Can you give me the lisp that will do the following? Select multiple text by drawing polygon(s) on the drawing When running the lisp routine allow me to enter the prefix text whenever I run it Thank you. Quote
Lee Mac Posted June 17, 2020 Posted June 17, 2020 31 minutes ago, CAD Diva said: I am not familiar with lisp routines but am willing to try this one because I am desperate. I have to add a prefix to over 30,000 text objects so this will help immensely. However, I cannot decipher where to put what. Can you give me the lisp that will do the following? Select multiple text by drawing polygon(s) on the drawing When running the lisp routine allow me to enter the prefix text whenever I run it Using the code found here, you can define a program such as the following: (defun c:pretext ( / p ) (if (/= "" (setq p (getstring t "\nSpecify prefix: "))) (pstext p "" 1)) (princ) ) ;; (pstext "Prefix Text" "Suffix Text" <mode>) ;; ;; <mode> = 0 - single selection ;; = 1 - window selection ;; ;; Author: Lee Mac 2011 - www.lee-mac.com (defun pstext ( preftext sufftext mode / a e i s ) (cond ( (= 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))) "TEXT,MTEXT,ATTRIB") (entmod (setq e (entget e) a (assoc 1 e) e (subst (cons 1 (strcat preftext (cdr a) sufftext)) a e) ) ) (princ "\nInvalid Object.") ) ) ) ) ) ) ( (setq s (ssget "_:L" (list '(0 . "TEXT,MTEXT")))) (repeat (setq i (sslength s)) (entmod (setq e (entget (ssname s (setq i (1- i)))) a (assoc 1 e) e (subst (cons 1 (strcat preftext (cdr a) sufftext)) a e) ) ) ) ) ) (princ) ) The command for the above is pretext. 1 Quote
BIGAL Posted June 18, 2020 Posted June 18, 2020 Nice code Lee as usual maybe OP has limited lisp experience added a couple of extras. (defun c:pretext ( / p ) (if (/= "" (setq p (getstring t "\nSpecify prefix: "))) (pstext p "" 1)) (princ) ) (defun c:sufftext ( / s ) (if (/= "" (setq s (getstring t "\nSpecify suffix: "))) (pstext "" s 1)) (princ) ) (defun c:presuff( / p s) (if (and (/= "" (setq p (getstring t "\nSpecify prefix: "))) (/= "" (setq s (getstring t "\nSpecify suffix: "))) ) (pstext p s 1) ) (princ) ) 1 Quote
lamensterms Posted January 23 Posted January 23 Just hopping in to say thanks to Lee and Bigal - very useful LISP as always Quote
Lee Mac Posted January 23 Posted January 23 7 hours ago, lamensterms said: Just hopping in to say thanks to Lee and Bigal - very useful LISP as always I'm pleased it's still proving useful after all these years! Quote
3dwannab Posted June 9 Posted June 9 (edited) Anyone having an issue whereby you want to add a space to the suffix or prefix, like: prefix = "Bug with dynmode " suffix = " set it to 0 for the duraction of the program". This issue has been around for a long time. Here's that fix for anyone that may need it. (defun c:test1 (/ acDoc prefix suffix var_cmdecho var_dynprompt) (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 '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_dynprompt (getvar "dynprompt")) ; Set any system variables here (setvar 'cmdecho 0) (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: "))) (setq suffix (getstring T (strcat "\nSpecify suffix string: "))) ) (pstext prefix suffix 0) (princ "Suffix or prefix cancelled") ) (vla-EndUndoMark acDoc) (*error* nil) (princ) ) Edited June 9 by 3dwannab Quote
TheGru11 Posted November 15 Posted November 15 Not sure how to tie all the above together - Can someone post the comprehensive lisp routine that allows gives you a prompt for prefix and/or suffix, while allowing spaces to be included? Quote
pkenewell Posted November 15 Posted November 15 (edited) On 8/14/2012 at 2:07 PM, Lee Mac said: Use the getstring function to prompt the user for the prefix/suffix, e.g.: (defun c:test ( ) (pstext (getstring t "\nSpecify Prefix <none>: ") (getstring t "\nSpecify Suffix <none>: ") 1) ) On 8/14/2012 at 8:27 AM, Lee Mac said: Welcome to CADTutor Nick Would this function help you at all? @TheGru11 The answer is in the above quoted conversations. Below is putting it together for you. Add the following into notepad, save it as pstext.lsp, then load it with appload: EDIT: added an option to add a space between the Prefix, the original text, and the suffix. For some reason (getstring t ...), strips spaces off the end. Change the "T" argument to "nil" to turn it off. (defun c:pstext ( ) (pstext (getstring t "\nSpecify Prefix <none>: ") (getstring t "\nSpecify Suffix <none>: ") 1 T);<-- Change to NIL for no space after prefix / before suffix. ) ;; (pstext "Prefix Text" "Suffix Text" <mode>) ;; ;; <mode> = 0 - single selection ;; = 1 - window selection ;; ;; Author: Lee Mac 2011 - www.lee-mac.com ;; "Addspace" added by PJK 11/15/2024 (defun pstext ( preftext sufftext mode addspace / a e i s ) ;; Added by PJK (if addspace (setq preftext (strcat preftext " ") sufftext (strcat " " sufftext) ) ) (cond ( (= 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))) "TEXT,MTEXT,ATTRIB") (entmod (setq e (entget e) a (assoc 1 e) e (subst (cons 1 (strcat preftext (cdr a) sufftext)) a e) ) ) (princ "\nInvalid Object.") ) ) ) ) ) ) ( (setq s (ssget "_:L" (list '(0 . "TEXT,MTEXT")))) (repeat (setq i (sslength s)) (entmod (setq e (entget (ssname s (setq i (1- i)))) a (assoc 1 e) e (subst (cons 1 (strcat preftext (cdr a) sufftext)) a e) ) ) ) ) ) (princ) ) Edited November 15 by pkenewell Quote
3dwannab Posted November 17 Posted November 17 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 1 Quote
pkenewell Posted November 18 Posted November 18 (edited) On 11/17/2024 at 7:08 AM, 3dwannab said: 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. @3dwannab Very nice! Didn't know that the dynamic prompts caused that problem. Learn something new every day! Edited November 18 by pkenewell 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.