Jump to content

LISP for Select and Replace text, not find & replace


Aphrodisian

Recommended Posts

Hi,

 

I know that there are hundreds of topics about replacing texts, but after searching for hours, i am here empty-handed because they do not fulfill my requirements.

 

What i want to achieve is extremely easy, i want to select multiple mtext, and change all of them together to an input that i want to enter. I don't and am not able to use any kind of find through this process, because these texts have different values. 

1- Select texts

2- Enter the new value

3- Voila! All selected texts changes into the entered value.

 

can anybody help me on this LISP? i have a very narrow knowledge about coding, so i am not able to offer any starters.

 

Thanks in advance

Link to comment
Share on other sites

Sure.  Like this?

Command RMT.

 

It also works for TEXT objects.  If you don't want this, replace (cons 0 "TEXT,MTEXT") by (cons 0 "MTEXT")

 

 (vl-load-com)
 
;; RMT for Replace Mtext
(defun c:rmt ( / ss newtext i)
  (princ "\nSelect TEXT/MTEXT elements: ")
  (setq ss (ssget (list (cons 0 "TEXT,MTEXT") )))
  (setq newtext (getstring "\nNew text: " 1))
  (setq i 0)
  (repeat (sslength ss)
      (vla-put-textstring (vlax-ename->vla-object (ssname ss i)) newtext)
    (setq i(+ i 1))
  )
  (princ)
)

 

  • Like 2
Link to comment
Share on other sites

Will work on text and mtext.

 

;;----------------------------------------------------------------------------;;
;; Select text to replace
(defun C:Sel2Replace (/ SS str)
  (vl-load-com)
  (prompt "\nSelect Text")
  (setq ss (ssget '((0 . "*TEXT")))
        newstr (getstring t "\nNew Text: ")
  )
  (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
    (vla-put-textstring (vlax-ename->vla-object ent) newstr)
  )
)

 

Shouldn't have got that coffee! @Emmanuel Delay

Edited by mhupp
  • Like 2
Link to comment
Share on other sites

5 hours ago, Aphrodisian said:

Hi,

 

I know that there are hundreds of topics about replacing texts, but after searching for hours, i am here empty-handed because they do not fulfill my requirements.

 

What i want to achieve is extremely easy, i want to select multiple mtext, and change all of them together to an input that i want to enter. I don't and am not able to use any kind of find through this process, because these texts have different values. 

1- Select texts

2- Enter the new value

3- Voila! All selected texts changes into the entered value.

 

can anybody help me on this LISP? i have a very narrow knowledge about coding, so i am not able to offer any starters.

 

Thanks in advance

Code is not needed for this. Select all the text you want to change then update the contents in the properties palette (CNTRL+1).

image.thumb.png.98f08d5bba733dbcf0e8210d708b1ba8.png

  • Like 1
  • Agree 1
Link to comment
Share on other sites

44 minutes ago, devitg said:

 

because these texts have different values.

 

@ronjonp is right tho. it will just display as  * if text is different. if you enter anything and hit enter they will all be changed.

 

 

  • Like 2
Link to comment
Share on other sites

Ronjons method works very well if all the text is the same type, mtext, dtext or whatever - otherwise, just need to select the type at the top of the properties dialogue and do it twice. A lot quicker than writing, debugging, and checking a LISP

 

Not sure what is faster to use, since you are selecting the texts and writing in what it is to be, not a lot in it with using a LISP or doing it via properties, think properties method wins.

 

below is my option, also works on Dimensions (and maybe tables but they annoy me so I try not to use them). A bit longer than the others, it is made up from building blocks of my text LISPS... so long winded for this but elsewhere does other stuff. Command txtchange. There is a second defun textchange (without the C:), if you call that from another LISP you can get that LISP to work out the text to use and then update the selected text (for example, 'today', my LISP uses this to put todays date into that text string

 

 

 

(defun getent ( aprompt / enta entb pt )
  (princ "\n")
  (setq enta (car (nentsel aprompt)))
  (setq pt (cdr (assoc 10 (entget enta))) )
  (setq entb (last (last (nentselp pt)))) ;;fix for nenset or entsel requirements
  (if (and (/= entb nil) (/= (type entb) 'real) )
    (if (wcmatch (cdr (assoc 0 (entget entb))) "ACAD_TABLE,*DIMENSION")(setq enta entb))
  )
  enta
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun addinnewtext (newtext newentlist newent / )
  (if (/= newtext nil)
    (progn
      (if (= (cdr (assoc 0 newentlist)) "DIMENSION") 
        (progn
          (entmod (setq newentlist (subst (cons 1 newtext) (assoc 1 newentlist) newentlist)))
          (entupd newent)
        );end progn
        (progn
          (vla-put-textstring (vlax-ename->vla-object newent) newtext) ;;vla-put-text string for large text blocks + 2000 characters?
        );end progn
      ) ;end if
    ) ;end progn
    (princ "\nSource text is not 'text'")
  );end if
)


(defun c:txtchange ( / texta)
  (setq texta (getstring t "\nEnter New Text : ") )
  (while (setq ent1 (getent "\nSelect Text (or escape): "))
    (txtchange texta ent1)
  );end while
  (princ)
)
(defun txtchange ( texta ent1 / )
  (setq entlist1 (entget ent1))
;;  (setq entcodes1 (list 3 4 1 172 304) ) ;list of ent codes containing text. Don't think I use this here
;;  (if (= (cdr (assoc 0 entlist1)) "DIMENSION") (setq entcodes1 (list 4 1 42 172 304))) ;;if dimension. Don't think I use this here
  (addinnewtext texta entlist1 ent1)
  (command "redraw")
  (command "regen") ;;update it all
)

 

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Ah, yes, I understand.

 

So c:txtchange runs the function without the c:, it collects the information required to run the second function (in this case the new text and the name of the text item to change). It gets the second function to run with this line:

 

(txtchange texta ent1)

 

Where texta is the new text string and ent1 is the name of the text entity to change. You could combine the 2 functions if you wanted but I'll often make a core function without the c:, in this case to change a text, and I can link many other functions to run it. For example, I have another function that increments a number, the first function does all that calculation and then the updating uses the one above, another I have copies texts from one to another.. again a first function to get the text string to copy and then the above to update the new text. Saves copying the same thing many times and only needs 1 update if I change my way of thinking ever.

 

So to use the code without the 'c:' you can use this in your code:

 

(txtchange **New Text String** **Text Entity Definition**)

 

where the **Text Entity Definition** is what you might get from:

 

(setq TextEnt (car (entsel "\nSelect a text string: ")))

 

 

Note also that the codes above might appear to be a long way round, they are shortened versions that run a whole load of things - the whole LISP file is quite large and LISPs in it are linked, shortened here to answer the question asked

Edited by Steven P
  • Like 1
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...