Jump to content

Prefix/Suffix - add to numbers or text


coniferous

Recommended Posts

I am looking for a lisp routine that will add text to the front or back of text or to an attributed block style text. Is there a good one out there? Also, if I find one, how do I copy the lisp file to my computer?

Link to comment
Share on other sites

Welcome to the Forum . :)

 

Try this .... and I hope it is a watertight routine . :D

 

(defun c:TesT (/ rep str ss i sn vl e)
 (vl-load-com)
 ;;; Tharwat 25. Nov. 2011 ;;
 (if
   (and (progn
          (initget "Prefix Suffix Both")
          (setq
            rep (getkword "\n Specify your aim [Prefix/Suffix/Both] :")
          )
          (initget 1)
          (setq str (getstring t "\n Enter Text :"))
        )
        (setq ss (ssget "_:L" '((0 . "TEXT,MTEXT"))))
   )
    (repeat
      (setq i (sslength ss))
       (setq sn (ssname ss (setq i (1- i))))
       (setq vl (vlax-ename->vla-object sn))
       (setq e (entget sn))
       (cond (
              (eq rep "Prefix")
              (vla-put-textstring vl (strcat str (cdr (assoc 1 e))))
             )
             (
              (eq rep "Suffix")
              (vla-put-textstring vl (strcat (cdr (assoc 1 e)) str))
             )
             (
               (eq rep "Both")
              (vla-put-textstring vl (strcat str (cdr (assoc 1 e)) str))
             )
       )
    )
    (princ)
 )
 (princ)
)

Link to comment
Share on other sites

 
(defun c:PST (/ PreSuf Str ent Cstr)
 (vl-load-com)
 (initget "P S")
 (setq PreSuf (getkword "\nChoose [Prefix/Suffix]  <Suffix>: "))
 (if (not PreSuf)
   (setq PreSuf "S")
 )
 (while (not str)
   (setq str (getstring T "\nEnter String: "))
   (cond ((and (eq str "")
 (princ "Null Input Try again")
 (setq str nil)
   )
  )
   )
 )
 (while (and (setq ent (car (nentsel "\nSelect Text/Attribute: ")))
      (member (cdr (assoc 0 (entget ent)))
       '("TEXT" "MTEXT" "ATTRIB")
      )
 )
   (setq ent  (vlax-ename->vla-object ent)
  Cstr (vla-get-textstring ent)
   )
   (vla-put-textstring
     ent
     (if (eq PreSuf "S")
(strcat Cstr " " str)
(strcat str " " Cstr)
     )
   )
 )(princ)
)

Link to comment
Share on other sites

An alternative approach...

How about a function that allows you to create your own short & simple prefix/suffix programs?

 

Say you need to repeatedly prefix many text object with the same thing and don't want to repeatedly specify the prefix to use, you can quickly write a custom command to prefix the text without a prompt for the prefix.

 

Below is a generic function requiring optional prefix and suffix text and an integer mode to determine the method of selection:

 

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

Open a new file in Notepad and copy the above code into the new file.

 

Now, you can start creating your own commands to prefix/suffix your text; the pstext function takes the format:

 

(pstext <PrefixText> <SuffixText> <Mode>)
 

Where:

 

= Text to be appended to the front of the selected text objects

 

= Text to be appended to the end of the selected text objects

 

= Integer determining how text objects are selected.

 

= 0 - single selection of objects

= 1 - window selection of objects

 

Here are some example programs:

 

(defun c:test1 ( )
   (pstext "Prefix" "Suffix" 0)
)
 
(defun c:test2 ( )
   (pstext "" " mysuffix" 1)
)
 
(defun c:test3 ( )
   (pstext "myprefix " "" 1)
)
 

In your open notepad file, add a few new lines after the previously copied code above, then copy the above three commands to the notepad file.

 

Save the notepad file as anything you like with a ".lsp" extension (ensure the filetype is set to 'All Files').

 

Now Open AutoCAD and type appload at the command-line.

 

Select your saved notepad .lsp file.

 

Load the file.

 

Type either 'test1', 'test2', or 'test3' to start any of the defined commands.

Edited by Lee Mac
Link to comment
Share on other sites

  • 8 months later...

I recently have developed a need to be able to quickly add a "L" or "R" to the end of a part number (i.e. A-1 to A-1R) which can be a pain when doing manually.

 

Lee, I don't think I quite understand the instructions given because when I tried to invoke the command, I receive the error"

"; error: no function definition: PSTEXT"

 

I have attached the code for review.

 

;; (pstext "Prefix Text" "Suffix Text" <mode>)
;;
;; <mode> = 0  -  single selection
;;        = 1  -  window selection
;;
;; Author: Lee Mac 2011  -  www.lee-mac.com

(defun c:rx ( )
   (pstext "" " R" 0)
)
   (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) 

Link to comment
Share on other sites

put this bit back in

(defun pstext ( preftext sufftext mode / a e i s )

 

and move

 

(defun c:rx ( ) (pstext "" " R" 0) )

 

all the way down to the end of the lisp, reload it and give it a go

Link to comment
Share on other sites

Lee, I don't think I quite understand the instructions given because when I tried to invoke the command, I receive the error"

"; error: no function definition: PSTEXT"

 

Eric,

 

Firstly: there is no need to modify my code in any way.

 

My custom function 'pstext' can be viewed as just another LISP function, no different from mapcar, defun, apply, +, cond, ... etc.

 

Just as mapcar requires a function argument and number of list arguments, my pstext function requires three arguments: the prefix text, suffix text and an integer mode argument to determine how the function will operate.

 

Just like the other LISP functions, my pstext function can be called from within a program, for example, just as you might use mapcar in a program, supplied with a function argument and a number of list arguments:

 

(defun c:test1 ( )
   (print ([color=blue]mapcar[/color] '+ '(1 2 3 4 5) '(6 7 8 9 0)))
   (princ)
)

 

You would call my pstext function from a program, called with the necessary arguments:

 

(defun c:test2 ( )
   ([color=blue]pstext[/color] "" " R" 0)
   (princ)
)

 

Here, the prefix text argument is an empty string, the suffix text is " R" and the program is set for single selection as the mode argument is 0.

 

Everything else regarding how to save the code is explained in detail in my previous post, and so to save me repeating it all here, I would suggest carefully re-reading my earlier post step by step and, if you are still stuck, let me know.

 

@Least: Thank you for your assistance :thumbsup:

Link to comment
Share on other sites

@Least - Thank you very much for your assistance on this post.

 

@Lee Mac - My apologies for misunderstanding, once you broke it down that PSTEST was a program in itself, it made sense, and is now working great.

 

...On a side note, what kind of changes would need to be made to PSTEST.lsp to REMOVE a prefix or suffix?

Link to comment
Share on other sites

@Lee Mac - My apologies for misunderstanding, once you broke it down that PSTEST was a program in itself, it made sense, and is now working great.

 

You're welcome.

 

...On a side note, what kind of changes would need to be made to PSTEST.lsp to REMOVE a prefix or suffix?

 

A function could quite easily be constructed to use the wcmatch function to test for the presence of the prefix / suffix string in the selected text, then, if a match is found, shorten the string by the appropriate number of characters (length of prefix / suffix) using the substr function.

Link to comment
Share on other sites

  • 3 weeks later...
An alternative approach...

How about a function that allows you to create your own short & simple prefix/suffix programs?

 

Say you need to repeatedly prefix many text object with the same thing and don't want to repeatedly specify the prefix to use, you can quickly write a custom command to prefix the text without a prompt for the prefix.

 

Below is a generic function requiring optional prefix and suffix text and an integer mode to determine the method of selection:

 


[color=GREEN];; (pstext "Prefix Text" "Suffix Text" <mode>)[/color]
[color=GREEN];;[/color]
[color=GREEN];; <mode> = 0  -  single selection[/color]
[color=GREEN];;        = 1  -  window selection[/color]
[color=GREEN];;[/color]
[color=GREEN];; Author: Lee Mac 2011  -  www.lee-mac.com[/color]

([color=BLUE]defun[/color] pstext ( preftext sufftext mode [color=BLUE]/[/color] a e i s )
   ([color=BLUE]cond[/color]
       (   ([color=BLUE]=[/color] 0 mode)
           ([color=BLUE]while[/color]
               ([color=BLUE]progn[/color] ([color=BLUE]setvar[/color] 'ERRNO 0) ([color=BLUE]setq[/color] e ([color=BLUE]car[/color] ([color=BLUE]nentsel[/color])))
                   ([color=BLUE]cond[/color]
                       (   ([color=BLUE]=[/color] 7 ([color=BLUE]getvar[/color] 'ERRNO))
                           ([color=BLUE]princ[/color] [color=MAROON]"\nMissed, try again."[/color])
                       )
                       (   ([color=BLUE]eq[/color] 'ENAME ([color=BLUE]type[/color] e))
                           ([color=BLUE]if[/color] ([color=BLUE]wcmatch[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 ([color=BLUE]entget[/color] e))) [color=MAROON]"TEXT,MTEXT,ATTRIB"[/color])
                               ([color=BLUE]entmod[/color]
                                   ([color=BLUE]setq[/color] e ([color=BLUE]entget[/color] e)
                                         a ([color=BLUE]assoc[/color] 1 e)
                                         e ([color=BLUE]subst[/color] ([color=BLUE]cons[/color] 1 ([color=BLUE]strcat[/color] preftext ([color=BLUE]cdr[/color] a) sufftext)) a e)
                                   )
                               )
                               ([color=BLUE]princ[/color] [color=MAROON]"\nInvalid Object."[/color])
                           )
                       )
                   )
               )
           )
       )
       (   ([color=BLUE]setq[/color] s ([color=BLUE]ssget[/color] [color=MAROON]"_:L"[/color] ([color=BLUE]list[/color] '(0 . [color=MAROON]"TEXT,MTEXT"[/color]))))
           ([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] i ([color=BLUE]sslength[/color] s))
               ([color=BLUE]entmod[/color]
                   ([color=BLUE]setq[/color] e ([color=BLUE]entget[/color] ([color=BLUE]ssname[/color] s ([color=BLUE]setq[/color] i ([color=BLUE]1-[/color] i))))
                         a ([color=BLUE]assoc[/color] 1 e)
                         e ([color=BLUE]subst[/color] ([color=BLUE]cons[/color] 1 ([color=BLUE]strcat[/color] preftext ([color=BLUE]cdr[/color] a) sufftext)) a e)
                   )
               )
           )
       )
   )
   ([color=BLUE]princ[/color])
)

Open a new file in Notepad and copy the above code into the new file.

 

Now, you can start creating your own commands to prefix/suffix your text; the pstext function takes the format:

 

([color=blue]pstext [/color][color=darkred]<PrefixText> <SuffixText> <Mode>[/color])

Where:

 

= Text to be appended to the front of the selected text objects

 

= Text to be appended to the end of the selected text objects

 

= Integer determining how text objects are selected.

 

= 0 - single selection of objects

= 1 - window selection of objects

 

Here are some example programs:

 

([color=BLUE]defun[/color] c:test1 ( )
   (pstext [color=MAROON]"Prefix"[/color] [color=MAROON]"Suffix"[/color] 0)
)

([color=BLUE]defun[/color] c:test2 ( )
   (pstext [color=MAROON]""[/color] [color=MAROON]" mysuffix"[/color] 1)
)

([color=BLUE]defun[/color] c:test3 ( )
   (pstext [color=MAROON]"myprefix "[/color] [color=MAROON]""[/color] 1)
)

In your open notepad file, add a few new lines after the previously copied code above, then copy the above three commands to the notepad file.

 

Save the notepad file as anything you like with a ".lsp" extension (ensure the filetype is set to 'All Files').

 

Now Open AutoCAD and type appload at the command-line.

 

Select your saved notepad .lsp file.

 

Load the file.

 

Type either 'test1', 'test2', or 'test3' to start any of the defined commands.

 

a very useful routine! thanks a lot Lee!

Link to comment
Share on other sites

  • 2 years later...

This lisp was so helpfulT Thanks a bunch!

 

Is there a way to eliminate the space between the text to the suffix/preffix?

 

ex is its showing: 20 m should be 20m? or T 50 should be T50.

 

Thanks

Edited by SanMiguel
Link to comment
Share on other sites

ok while searching i found Lee's lisp for removing space between test.

 

Now is there a way to combine 2 lisp into 1 new command? Thanks

 

1st lisp:

;; (pstext "Prefix Text" "Suffix Text" )

;;

;; = 0 - single selection

;; = 1 - window selection

;;

;; Author: Lee Mac 2011 - http://www.lee-mac.com



(defun c:pstext ( )

(pstext "" "m" 1)

)

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

)

 

2nd lisp:

(defun c:nospace ( / nospace ss )



(defun nospace ( s )

(

(lambda ( o )

(repeat (strlen s)

(if (/= 32 (ascii s))

(setq o (strcat o (substr s 1 1)))

)

(setq s (substr s 2))

)

o

)

""

)

)



(if (setq ss (ssget "_:L" '((0 . "TEXT,MTEXT"))))

(

(lambda ( i / e )

(while (setq e (ssname ss (setq i (1+ i))))

(entupd

(cdr

(assoc -1

(entmod

(subst

(cons 1

(nospace

(cdr

(assoc 1 (entget e))

)

)

)

(assoc 1 (entget e))

(entget e)

)

)

)

)

)

)

)

-1

)

)



(princ)

)

 

Link to comment
Share on other sites

  • 1 month later...

This is an excellent lisp, I use it several times most days.

Now if it could be developed to include a text replace function...

(defun c:Test10 ( )
   (pRstext "" "TEXT TO BE REPLACED" "" 1))

Link to comment
Share on other sites

  • 5 years later...

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.

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