+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Full Member
    Using
    AutoCAD 2010
    Join Date
    Aug 2015
    Posts
    25

    Default LISP for adding a letter inside text

    Registered forum members do not see this ad.

    Hi,

    I found a lisp for adding suffix or prefix to text but I need a function that allows me to add some letter only after text (not digit).

    For examle:

    I add letter "D" to "g400" and as the result I get "gD400" etc.

    The second way is just to add letter in certain place of string: adding D on second place of word "g400" (that consists of 4 letters): g-1, D-2, 4-3, 0-4, 0-5 etc

    Is something like that possible to process?

  2. #2
    Senior Member
    Discipline
    Civil
    Hippe013's Discipline Details
    Occupation
    Survey Crew Chief / Civil Designer
    Discipline
    Civil
    Details
    Land Survey / Civil Engineering
    Using
    Civil 3D 2017
    Join Date
    Feb 2010
    Location
    North Dakota
    Posts
    367

    Default

    Yes, this is possible. Though I would recommend that you look into string handling functions for AutoLisp. Unless of course, this is something that you want somebody else to just write for you.

  3. #3
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    981

    Default

    Try this:
    Code:
    (defun C:test ( / newstr lt SS i enx str )
      (defun newstr ( str lt / foo n r )
        (defun foo ( str / cnt )
          (setq cnt -1)
          (vl-some 
            '(lambda (x) (setq cnt (1+ cnt)) (if (member x (mapcar 'chr (vl-string->list "1234567890"))) cnt))
            (mapcar 'chr (vl-string->list str))
          )
        )
        (and
          (setq n (foo str))
          (setq r (apply 'strcat (LM:insertnth lt n (mapcar 'chr (vl-string->list str)))))
        )
        r
      )  
      (and
        (setq lt (GetLetter))
        (setq SS (ssget "_:L-I" (list (cons 0 "*TEXT"))))
        (repeat (setq i (sslength SS))
          (setq enx (entget (ssname SS (setq i (1- i)))))
          (if (setq str (newstr (cdr (assoc 1 enx)) lt))
            (entmod (subst (cons 1 str) (assoc 1 enx) enx))
          )
        )
      )
      (princ)
    ) (vl-load-com) (princ)
    
    ; wrote this on the fly, nice, huh?:
    (defun GetLetter ( / grr x )
      (setvar 'errno 0)
      (while (/= 52 (getvar 'errno))
        (princ "\nInput letter, [ENTER] to exit: ")
        (setq grr (grread T))
        (and
          (= (car grr) 2)
          (setq x (chr (cadr grr)))
          (if (= x "\r") (progn (setvar 'errno 52) (setq x nil)) x)
          (not (member x (list "\t" "\r" " ")))
          (setvar 'errno 52)  
        )
      ); while
      x
    ); defun GetLetter
    
    ;; Insert Nth  -  Lee Mac
    ;; Inserts an item at the nth position in a list.
    ;; x - [any] Item to be inserted
    ;; n - [int] Zero-based index at which to insert item
    ;; l - [lst] List in which item is to be inserted
    
    (defun LM:insertnth ( x n l / i )
      (setq i -1)
      (apply 'append (mapcar '(lambda ( a ) (if (= n (setq i (1+ i))) (list x a) (list a))) l))
    )

  4. #4
    Full Member
    Using
    AutoCAD 2010
    Join Date
    Aug 2015
    Posts
    25

    Default

    Quote Originally Posted by Grrr View Post
    Try this:
    Code:
    (defun C:test ( / newstr lt SS i enx str )
      (defun newstr ( str lt / foo n r )
        (defun foo ( str / cnt )
          (setq cnt -1)
          (vl-some 
            '(lambda (x) (setq cnt (1+ cnt)) (if (member x (mapcar 'chr (vl-string->list "1234567890"))) cnt))
            (mapcar 'chr (vl-string->list str))
          )
        )
        (and
          (setq n (foo str))
          (setq r (apply 'strcat (LM:insertnth lt n (mapcar 'chr (vl-string->list str)))))
        )
        r
      )  
      (and
        (setq lt (GetLetter))
        (setq SS (ssget "_:L-I" (list (cons 0 "*TEXT"))))
        (repeat (setq i (sslength SS))
          (setq enx (entget (ssname SS (setq i (1- i)))))
          (if (setq str (newstr (cdr (assoc 1 enx)) lt))
            (entmod (subst (cons 1 str) (assoc 1 enx) enx))
          )
        )
      )
      (princ)
    ) (vl-load-com) (princ)
    
    ; wrote this on the fly, nice, huh?:
    (defun GetLetter ( / grr x )
      (setvar 'errno 0)
      (while (/= 52 (getvar 'errno))
        (princ "\nInput letter, [ENTER] to exit: ")
        (setq grr (grread T))
        (and
          (= (car grr) 2)
          (setq x (chr (cadr grr)))
          (if (= x "\r") (progn (setvar 'errno 52) (setq x nil)) x)
          (not (member x (list "\t" "\r" " ")))
          (setvar 'errno 52)  
        )
      ); while
      x
    ); defun GetLetter
    
    ;; Insert Nth  -  Lee Mac
    ;; Inserts an item at the nth position in a list.
    ;; x - [any] Item to be inserted
    ;; n - [int] Zero-based index at which to insert item
    ;; l - [lst] List in which item is to be inserted
    
    (defun LM:insertnth ( x n l / i )
      (setq i -1)
      (apply 'append (mapcar '(lambda ( a ) (if (= n (setq i (1+ i))) (list x a) (list a))) l))
    )
    I have no idea how to thank you. I'd like to be able to create lisp routines the same like you do. I believe I will some day. Fantastic !

  5. #5
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,382

    Default

    Registered forum members do not see this ad.

    Just a thought the other way is just using substr function and strcat

    Code:
    (defun c:Text-x-end ( / tex tstr tnew num begin newtxt)
    (setq tex (vlax-ename->vla-object (car (entsel "\nPick Text"))))
    (setq tstr (vla-get-textstring tex))
    (setq tnew (Getstring "\nEnter additional text ")) 
    (setq num (getint "\nEnter start position"))
    (setq begin (substr tstr 1 num))
    (setq tend (substr tstr (+ 1 num)))
    (setq newtxt (strcat begin tnew tend))
    (vla-put-textstring tex newtxt)
    )
    A man who never made mistakes never made anything

Similar Threads

  1. Replies: 1
    Last Post: 18th Jun 2015, 04:01 am
  2. Shift+letter shortcuts results typing capilal letter instead executing command
    By kepasa in forum The CUI, Hatches, Linetypes, Scripts & Macros
    Replies: 4
    Last Post: 16th Jun 2014, 06:45 pm
  3. find and replace text inside dynamic blocks (lisp routine)
    By djeetn069 in forum AutoLISP, Visual LISP & DCL
    Replies: 6
    Last Post: 4th Jun 2014, 09:51 pm
  4. LISP code getting text inside a text file
    By Ahmeds in forum AutoLISP, Visual LISP & DCL
    Replies: 5
    Last Post: 27th Nov 2013, 03:04 am
  5. rename all blocks adding a letter
    By maksolino in forum AutoLISP, Visual LISP & DCL
    Replies: 8
    Last Post: 21st Apr 2010, 03:56 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