Jump to content

Prefix/Suffix - add to numbers or text


coniferous

Recommended Posts

1 hour ago, G Prameela said:

I am looking for prefixing 'C' in front of a column layer which is shown only in integers. Example instead of C1, they have mentioned  1, due to some reason I wanted prefix C IN front of all these integers.

Althought this shouldn't be in this post, I think this can solve your needs.

;;;;;;;;;;;; Program to put a prefix to all the layers except by 0 and Defpoints.
;;;;;;;;;;;; By Isaac Alcántara 20210319
;;;;;;;;;;;;
(vl-load-com)
(defun c:preflay ( / lay lst lyn new prefix)
   (setvar "cmdecho" 0)
   (vl-cmdf "_.undo" "_begin")
   (setq prefix (getstring T "\nType the prefix to add to the layers: "))

   (while (setq lay (tblnext "LAYER" (null lay)))
      (or (wcmatch (setq lyn (cdr (assoc 2 lay))) "0,Defpoints")
          (or (tblsearch "LAYER" (setq new (strcat prefix lyn)))
              (entmod (subst (cons 2 new) (assoc 2 (setq lst (entget (tblobjname "LAYER" lyn)))) lst))
          )
      )
   )
   (vl-cmdf "_.undo" "_end")
   (princ)
)

 

Link to comment
Share on other sites

  • 9 months later...

Does this also work with multileaders or does that require a completely different approach?

Edited by mickeforsberg
Link to comment
Share on other sites

20 hours ago, mickeforsberg said:

Does this also work with multileaders or does that require a completely different approach?

I guess this can help you with what you are looking for

(defun c:test ( )
   (pstext (getstring t "\nSpecify Prefix <none>: ") (getstring t "\nSpecify Suffix <none>: ") 1)
)
;; (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,*LEADER,*DIMENSION")
                               (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,ATTRIB,*LEADER,*DIMENSION"))))
           (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)
)

 

  • Like 1
Link to comment
Share on other sites

"APTXT" from Cadalyst is a dialog based routine that allows you to add prefixes or suffixes to any text with many options.


DDAPTXT.LSP Append to Text (c)June 1999, Scott A. Matthews
added support for MTEXT & DIMENSION's March 2003, Tom Beauford

https://forums.augi.com/showthread.php?6364-Text-prefix-suffix&p=37577&viewfull=1#post37577

 

Link to comment
Share on other sites

  • 1 month later...
On 1/15/2022 at 6:37 AM, Isaac26a said:

I guess this can help you with what you are looking for

Thanks! Sorry I haven't gotten back, I haven't had the chance to try it until now.

I get an "error: bad argument type: stringp nil"

 

Any idea? (it works with text still, but not multileaders)

Edited by mickeforsberg
Link to comment
Share on other sites

  • 2 years later...
On 26/11/2011 at 03:03, Lee Mac said:

Below is a generic function requiring optional prefix and suffix text and an integer mode...


Hi Lee, thanks for posting your function.

I am running into a problem however...
PSTEXT returns the error "malfomed list on input"
Would you happen to know what is causing this / are you able to replicate the error?
 

Link to comment
Share on other sites

5 hours ago, dpatts said:

Hi Lee, thanks for posting your function.

I am running into a problem however...
PSTEXT returns the error "malfomed list on input"
Would you happen to know what is causing this / are you able to replicate the error?

 

That error would indicate that there is a missing closing parenthesis - have you copied the code correctly?

Link to comment
Share on other sites

  • 2 weeks later...
Posted (edited)
On 15/01/2022 at 16:37, Isaac26a said:

I guess this can help you with what you are looking for

(defun c:test ( )
   (pstext (getstring t "\nSpecify Prefix <none>: ") (getstring t "\nSpecify Suffix <none>: ") 1)
)
;; (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,*LEADER,*DIMENSION")
                               (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,ATTRIB,*LEADER,*DIMENSION"))))
           (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)
)

 

 

Anyway to incorporate a Block selection into this? Seems like even though ATTRIB is an option I can't physically select the block. One other issue is that when selecting a Dimension it wipes the Distance when dropping in the Prefix or Suffix. 


Seems to be quite a few routines out there but I think this one has the greatest potential for almost any situation
Other than that it works perfectly thankyou

Edited by KraZeyMike
Link to comment
Share on other sites

  • 2 weeks later...
Posted (edited)

If I run this it doesn't add that space after prefix or before the suffix. Any way to fix this @Lee Mac?

 

;; If enter "Help " as the prefix and " Me" as the suffix I don't get those whitespaces.

(defun c:test1 (/ suffix prefix) 

  (setq prefix (getstring T (strcat "\nSpecify prefix string: ")))
  (setq suffix (getstring T (strcat "\nSpecify suffix string: ")))

  (pstext prefix suffix 1)
)

(c:test1)

 

Edit:

It can be fixed it by setting the dynmode and dynprompt variables to 0 for the duration of the program as far as I can see.

 

Thanks again Lee!

 

See below:

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

 

Edited by 3dwannab
Added fix and program for single and multiple selections
Link to comment
Share on other sites

Posted (edited)
On 1/14/2022 at 9:06 AM, mickeforsberg said:

Does this also work with multileaders or does that require a completely different approach?

 

I needed this one myself so please see this mod along with other tweaks

 

Credit to Lee for the original code.

 

This one in this thread doesn't work.

 

;; (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

 

Edited by 3dwannab
Link to comment
Share on other sites

  • 2 weeks later...

Thankyou Lee Mac. Your Lisp routine was helpfull, but as I am a noob with that, even after using AutoCAD for 25 years... IT still did not make sense, but with a bit of hunting, found a working lisp routine, with a training site, which together, got me on the right road. 

Link to comment
Share on other sites

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