+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
  1. #1
    Forum Newbie
    Using
    Civil 3D 2011
    Join Date
    Nov 2011
    Posts
    1

    Default Prefix/Suffix - add to numbers or text

    Registered forum members do not see this ad.

    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?

  2. #2
    Forum Deity Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draftsman
    Discipline
    Mechanical
    Details
    HVAC, Drainage, Water Supply, Fire Fighting and a little about Electrical.
    Using
    AutoCAD 2014
    Join Date
    Oct 2009
    Location
    Great Syria , Lives in Abu Dhabi
    Posts
    3,953

    Default

    Welcome to the Forum .

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

    Code:
    (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)
    )
    - When aim is being settled in my mind , I have to reach it and get it in hand whatever it costs and wherever it is and will never give up . Tharwat said

  3. #3
    Forum Deity pBe's Avatar
    Computer Details
    pBe's Computer Details
    Operating System:
    Windows XP
    Discipline
    Construction
    pBe's Discipline Details
    Discipline
    Construction
    Details
    Camp Construction planning and details
    Using
    AutoCAD 2009
    Join Date
    Apr 2010
    Posts
    2,642

    Default

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

  4. #4
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,882

    Default

    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:

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


    Code:
    (pstext <PrefixText> <SuffixText> <Mode>)
    Where:

    <PrefixText> = Text to be appended to the front of the selected text objects

    <SuffixText> = Text to be appended to the end of the selected text objects

    <Mode> = Integer determining how text objects are selected.

    <Mode> = 0 - single selection of objects

    <Mode> = 1 - window selection of objects

    Here are some example programs:


    Code:
    (defun c:test1 ( )
        (pstext "Prefix" "Suffix" 0)
    )
    Code:
    (defun c:test2 ( )
        (pstext "" " mysuffix" 1)
    )
    Code:
    (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.
    Last edited by Lee Mac; 25th Nov 2011 at 03:20 pm.
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  5. #5
    Full Member
    Using
    AutoCAD 2005
    Join Date
    Mar 2008
    Posts
    55

    Default

    very nice!

  6. #6
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,882

    Default

    Thanks Least
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  7. #7
    Full Member eric_monceaux's Avatar
    Computer Details
    eric_monceaux's Computer Details
    Operating System:
    Windows 7 Home Premium (64-Bit)
    Computer:
    Toshiba Qosmio X505-Q880
    Motherboard:
    TOSHIBA Qosmio X505
    CPU:
    1.60 gigahertz Intel Core i7 Q 720 64-bit
    RAM:
    6GB DDR3
    Graphics:
    NVIDIA GeForce 360M
    Primary Storage:
    64 SSD
    Secondary Storage:
    500GB SATA
    Monitor:
    18.4" Laptop Monitor, 19" LCD Secondary
    Discipline
    Architectural
    eric_monceaux's Discipline Details
    Occupation
    3D Designer/Draftsman
    Discipline
    Architectural
    Using
    AutoCAD 2011
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    94

    Default

    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.

    Code:
    ;; (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)
    "If at first you don't succeed, then skydiving is probably not for you" -Unknown

  8. #8
    Full Member
    Using
    AutoCAD 2005
    Join Date
    Mar 2008
    Posts
    55

    Default

    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

  9. #9
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,882

    Default

    Quote Originally Posted by eric_monceaux View Post
    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:

    Code:
    (defun c:test1 ( )
        (print (mapcar '+ '(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:

    Code:
    (defun c:test2 ( )
        (pstext "" " 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
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  10. #10
    Full Member eric_monceaux's Avatar
    Computer Details
    eric_monceaux's Computer Details
    Operating System:
    Windows 7 Home Premium (64-Bit)
    Computer:
    Toshiba Qosmio X505-Q880
    Motherboard:
    TOSHIBA Qosmio X505
    CPU:
    1.60 gigahertz Intel Core i7 Q 720 64-bit
    RAM:
    6GB DDR3
    Graphics:
    NVIDIA GeForce 360M
    Primary Storage:
    64 SSD
    Secondary Storage:
    500GB SATA
    Monitor:
    18.4" Laptop Monitor, 19" LCD Secondary
    Discipline
    Architectural
    eric_monceaux's Discipline Details
    Occupation
    3D Designer/Draftsman
    Discipline
    Architectural
    Using
    AutoCAD 2011
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    94

    Default

    Registered forum members do not see this ad.

    @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?
    "If at first you don't succeed, then skydiving is probably not for you" -Unknown

Similar Threads

  1. Adding Prefix or Suffix to block attribute
    By harilalmn in forum AutoLISP, Visual LISP & DCL
    Replies: 10
    Last Post: 24th Sep 2013, 02:43 am
  2. Add Prefix or suffix to all layers in a drawing
    By gman in forum AutoLISP, Visual LISP & DCL
    Replies: 19
    Last Post: 10th Oct 2011, 12:26 pm
  3. Add Prefix/Suffix to Multiple Dimensions?
    By ILoveMadoka in forum AutoLISP, Visual LISP & DCL
    Replies: 13
    Last Post: 17th Aug 2011, 01:41 pm
  4. Lisp to add a prefix or suffix to selected text
    By JB_In_FLA in forum AutoLISP, Visual LISP & DCL
    Replies: 12
    Last Post: 6th Jan 2011, 04:57 pm
  5. text suffix
    By ollie in forum AutoCAD General
    Replies: 10
    Last Post: 31st Jan 2008, 02:36 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts