+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Full Member
    Computer Details
    Zykl0's Computer Details
    Operating System:
    Windows XP
    Motherboard:
    ASUS P5LD2
    CPU:
    Intel P4 3.0 ghz
    RAM:
    2 gb
    Graphics:
    Nvidia 6600GT
    Primary Storage:
    80 gb
    Monitor:
    Benq 22" wide
    Using
    AutoCAD 2007
    Join Date
    Oct 2008
    Location
    Montréal
    Posts
    47

    Question Replace multiple textstring

    Registered forum members do not see this ad.

    Hello,

    I try to find a lisp that will search and replace text within a user selection.
    This piece of code that I found on a very old forums on the net do almost exactly what I need.
    The only issue is I want to replace single word inside sentence and this code replace the word only if it's alone.

    Code:
    (defun c:TRANSLATE()
    (command "cmdecho" "0")
    (command "_.undo" "begin")
    (setq mylist (list
    (list "APPLE" "PEAR")
    (list "BANANA" "STRAWBERRY")
    )
    )
    (setq sset (ssget '((0 . "TEXT"))))
    (setq i 0)
    (while (< i (sslength sset))
      (setq obj (ssname sset i))
      (setq str (cdr (assoc 1 (setq lst (entget obj)))))
     (princ str)
      (princ "\n")
    (foreach item mylist
        (if (eq str (nth 0 item))
          (progn
          (setq newstr (nth 1 item))
       	  (if newstr
        		(entmod (subst (cons 1 newstr)(assoc 1 lst) lst))
      	  )
          )
        )
    )
      (setq i (1+ i))
    )
    (command "cmdecho" "1")
    (command "_.undo" "end")
    )
    Can someone modify it to make it work for me?

    Thank you

  2. #2
    Senior Member
    Computer Details
    ronjonp's Computer Details
    Operating System:
    Windows 10
    Using
    AutoCAD 2018
    Join Date
    Apr 2009
    Location
    Colorado
    Posts
    452

    Default

    Quick mod:
    Code:
    (defun c:translate (/ el mylist sset str)
      (command "cmdecho" "0")
      (command "_.undo" "begin")
      (setq mylist '(("APPLE" "PEAR") ("STRAWBERRY" "BANANA")))
      (if (setq sset (ssget ":L" '((0 . "TEXT"))))
        (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex sset)))
          (setq str (cdr (assoc 1 (setq el (entget e)))))
          (foreach item mylist
    	(if (vl-string-search (car item) str)
    	  (progn (while	(vl-string-search (car item) str)
    		   (setq str (vl-string-subst (cadr item) (car item) str))
    		 )
    		 (entmod (subst (cons 1 str) (assoc 1 el) el))
    	  )
    	)
          )
        )
      )
      (command "cmdecho" "1")
      (command "_.undo" "end")
      (princ)
    )

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

    Default

    This subfunction might be handy, but I'm unable to test it ATM:

    Code:
    ; (_ReplaceWords 
    ; "APPLE IS MY FAVOURITE FRUIT BUT I HAVE ONLY STRAWBERRYS"
    ; '(("APPLE" "PEAR") ("STRAWBERRY" "BANANA"))
    ; )
    (defun _ReplaceWords ( str aL )
      (mapcar     
        '(lambda (old new) (while (vl-string-search old str) (setq str (vl-string-subst new old str))))
        (mapcar 'car aL) (mapcar 'cadr aL)
      )
      str
    ); defun _ReplaceWords

  4. #4
    Full Member
    Computer Details
    Zykl0's Computer Details
    Operating System:
    Windows XP
    Motherboard:
    ASUS P5LD2
    CPU:
    Intel P4 3.0 ghz
    RAM:
    2 gb
    Graphics:
    Nvidia 6600GT
    Primary Storage:
    80 gb
    Monitor:
    Benq 22" wide
    Using
    AutoCAD 2007
    Join Date
    Oct 2008
    Location
    Montréal
    Posts
    47

    Default

    Thank you very much guys this is working flawlessly!

  5. #5
    Super Member
    Using
    not applicable
    Join Date
    Jun 2016
    Posts
    521

    Default

    Note:
    Code:
    (while (vl-string-search old str) ...)
    is problematic if the new string contains the old string: '(("APPLE" "APPLES")).

    Here is my suggestion:
    Code:
    ; (String_Subst "aabbaacc" "aa" "xx")   => "xxbbxxcc"
    ; (String_Subst "aabbaacc" "aa" "xxaa") => "xxaabbxxaacc"
    (defun String_Subst (str old new / idx)
      (setq idx 0)
      (while (setq idx (vl-string-search old str idx))
        (setq str (vl-string-subst new old str idx))
        (setq idx (+ idx (strlen new)))
      )
      str
    )
    BricsCAD 16

  6. #6
    Senior Member
    Computer Details
    ronjonp's Computer Details
    Operating System:
    Windows 10
    Using
    AutoCAD 2018
    Join Date
    Apr 2009
    Location
    Colorado
    Posts
    452

    Default

    Quote Originally Posted by Roy_043 View Post
    Note:
    Code:
    (while (vl-string-search old str) ...)
    is problematic if the new string contains the old string: '(("APPLE" "APPLES")).

    Here is my suggestion:
    Code:
    ; (String_Subst "aabbaacc" "aa" "xx")   => "xxbbxxcc"
    ; (String_Subst "aabbaacc" "aa" "xxaa") => "xxaabbxxaacc"
    (defun String_Subst (str old new / idx)
      (setq idx 0)
      (while (setq idx (vl-string-search old str idx))
        (setq str (vl-string-subst new old str idx))
        (setq idx (+ idx (strlen new)))
      )
      str
    )
    Good call Roy

  7. #7
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,063

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by Roy_043 View Post
    Note:
    Code:
    (while (vl-string-search old str) ...)
    is problematic if the new string contains the old string: '(("APPLE" "APPLES")).

    Here is my suggestion:
    ...
    Thanks Roy, I didn't think of that problem!

Similar Threads

  1. Multiple block replace
    By Organic in forum AutoLISP, Visual LISP & DCL
    Replies: 17
    Last Post: 23rd Mar 2016, 05:37 pm
  2. Replace multiple text
    By mrdd in forum AutoLISP, Visual LISP & DCL
    Replies: 7
    Last Post: 20th Sep 2012, 06:23 pm
  3. Replace multiple blocks using a table
    By hosannabizarre in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 10th Aug 2010, 07:57 am
  4. If you've vla-put-TextString'ed one block...
    By Freerefill in forum AutoLISP, Visual LISP & DCL
    Replies: 18
    Last Post: 13th May 2009, 07:04 pm
  5. Get a Block TextString using VBA?
    By muck in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 11th May 2009, 04:56 am

Tags for this Thread

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