Jump to content

Recommended Posts

Posted

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?

Posted

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?

Posted

Might be making a complete muppet of myself but i cant see any other information on thread?

Posted

Did you view the link I provided? (click on the word 'this' in my earlier post)

Posted

Thats great thanks, how do i chnage this to have the command word as an option in the drop down menu?

Posted

How can i make the prefix and suffix user defined?

Posted
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)
)

  • Thanks 1
  • 7 years later...
Posted

@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.

Posted
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.

  • Thanks 1
Posted

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)
)

 

  • Thanks 1
  • 3 years later...
Posted

Just hopping in to say thanks to Lee and Bigal - very useful LISP as always

Posted
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!

  • 4 months later...
Posted (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 by 3dwannab
  • 5 months later...
Posted

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?

Posted (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 by pkenewell
Posted

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

 

  • Like 1
Posted (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 by pkenewell
  • Thanks 1

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...