Jump to content

Replace the text with a single digit


Nikon

Recommended Posts

Good day everyone!

Is there a way to write code in lisp that can replace text like: 1-1 with
2-2, etc. in one digit.
I.e. I call the command, select the text 1-1, enter the number 3, for example, and get 3-3.
If the text and mtext are underlined, then good.

Image 2.png

Link to comment
Share on other sites

Just to check you wanting to select each text in turn rather than all of them globally - such as 'Find' would do?

 

If you assume it is all text or mtext (rather than dimension texts, leaders and other special cases) the way I would do it is:

 

Use Entsel to select the text, perhaps with a loop or something to check that text was selected (or otherwise a quick check anyway)

Use Entget to get the entity definition found by entsel: (entget (car (entsel......

Use (Assoc 1 ) to get the text string (cdr (assoc 1 (entget...... )

Use getstring to ask for the new character

Use vl-string-subs twice on the string returned by the (assoc 1 line and using the new string from the getstring

Use entmod to replace the (assoc 1 list in the entity you selected

 

That is one way to do it.

 

 

EDIT

Have to use VLA-put-textstring to retain formatting, see below

 

Edited by Steven P
Link to comment
Share on other sites

You just need to replace the text "1-1" (number - number) with "2-2", for example, but enter only one number 2.
Unfortunately, I am not a programmer...

Edited by Nikon
Link to comment
Share on other sites

Like this?

(defun c:pp()
  (setq el (entget (car (entsel "pick (m)text")))
	nr (itoa (getint "enter number"))
	el (entmod (subst (cons 1 (strcat nr " - " nr)) (assoc 1 el) el)))
  )

 

  • Like 1
Link to comment
Share on other sites

Try this one, should retain formatting and should replace the text so long as the text string contains text in a suitable string (xx-xx). Examples "1-1" and "abc 1-1 def" can both be changed/

If the text formatting changes part way through this won't work (for example 123-123 )

 

(defun c:test ( / MyEnt MyEntGet MyTExt MyStart OldNumber NewNumber NewText)
  (setq MyEnt (car (entsel "\nSelect Text")))
  (while ;; while loop, checking that text was selected
    (and
      (/= (cdr (assoc 0 (entget MyEnt))) "TEXT")
      (/= (cdr (assoc 0 (entget MyEnt))) "MTEXT")
    )
    (princ "\nWell, that isn't text is it now? ")
    (setq MyEnt (car (entsel "Please select Text")))
  )
  (setq MyEntGet (entget MyEnt))         ; get text entity definition
  (setq MyText (cdr (assoc 1 MyEntGet))) ; get text string
  (setq MyStart (vl-string-search "-" MyText)) ; look for first instance of "-" in text string. Error check it exists?
  (setq NewNumber (getstring "\nEnter New Number : " t)) ; ask for replacement number, t to allow for spaces in text

  (if (wcmatch MyText "*#`-#*") (setq OldNumber (substr MyText (- MyStart 0) 3)))   ; for 0-0 to 9-9
  (if (wcmatch MyText "*##`-##*") (setq OldNumber (substr MyText (- MyStart 1) 5)))   ; for 10-10 to 99-99
  (if (wcmatch MyText "*###`-###*") (setq OldNumber (substr MyText (- MyStart 2) 7)))   ; for 100-100 to 999-999


  (if (= OldNumber nil) ; if text contained a valid format, update text
    (princ "Text string does not contain 'xx-xx' format text. Going for a nap now.")
    (progn
      (setq NewText (vl-string-subst (strcat NewNumber "-" NewNumber) OldNumber MyText)) ; substitute new text in
      (vla-put-TextString (vlax-ename->vla-object MyEnt) Newtext)
    )
  )

  (princ) ; exit quietly
)

 

Edited by Steven P
Link to comment
Share on other sites

1 hour ago, ronjonp said:

 

 

My first thought too, but I'm guessing the OP has reasons not to use Find (perhaps a drawing with 100 suitable texts, wants to change just one, for example)

Link to comment
Share on other sites

Steven P, thank, but lisp doesn't work
Select Text
Enter New Number : 5
Text string does not contain 'xx-xx' format text. 

Lisp fuccaro does a great job!
If you add an underscore "%%u" to the fuccaro lisp, so that the text remains underlined...

The underlined text in the properties looks like this:

%%u1-1

How to add this to lisp correctly?

 

Edited by Nikon
Link to comment
Share on other sites

Probably needs (vl-load-com) adding in but I have added some annotations as it runs to give a clue what it is doing on your installation:

 

(defun c:test ( / MyEnt MyEntGet MyTExt MyStart OldNumber NewNumber NewText)
  (vl-load-com)
  (setq MyEnt (car (entsel "\nSelect Text")))
  (while ;; while loop, checking that text was selected
    (and
      (/= (cdr (assoc 0 (entget MyEnt))) "TEXT")
      (/= (cdr (assoc 0 (entget MyEnt))) "MTEXT")
    )
    (princ "\nWell, that isn't text is it now? ")
    (setq MyEnt (car (entsel "Please select Text")))
  )
  (setq MyEntGet (entget MyEnt))         ; get text entity definition
  (setq MyText (cdr (assoc 1 MyEntGet))) ; get text string
  (setq MyStart (vl-string-search "-" MyText)) ; look for first instance of "-" in text string. Error check it exists?
  (setq NewNumber (getstring "\nEnter New Number : " t)) ; ask for replacement number, t to allow for spaces in text

(princ "\nText string: ")(princ MyText)
(princ "\n'-' found in position ")(princ (+ MyStart 1))
(princ "\nReplace text with: ")(princ NewNumber)

  (if (wcmatch MyText "*#`-#*") (setq OldNumber (substr MyText (- MyStart 0) 3)))   ; for 0-0 to 9-9
  (if (wcmatch MyText "*##`-##*") (setq OldNumber (substr MyText (- MyStart 1) 5)))   ; for 10-10 to 99-99
  (if (wcmatch MyText "*###`-###*") (setq OldNumber (substr MyText (- MyStart 2) 7)))   ; for 100-100 to 999-999

(princ "\nNew string: ")(princ OldNumber)

  (if (= OldNumber nil) ; if text contained a valid format, update text
    (princ "Text string does not contain 'xx-xx' format text. Going for a nap now.")
    (progn
      (setq NewText (vl-string-subst (strcat NewNumber "-" NewNumber) OldNumber MyText)) ; substitute new text in
      (vla-put-TextString (vlax-ename->vla-object MyEnt) Newtext)
    )
  )

  (princ) ; exit quietly
)

 

Link to comment
Share on other sites

(defun c:pp()
  (setq el (entget (car (entsel "pick (m)text")))
	nr (itoa (getint "enter number"))
	el (entmod (subst (cons 1 (strcat nr " - " nr)) (assoc 1 el) el)))
  )

EnM4st3r, thank.  How to add this to lisp correctly?

(strcat  "%%u" nr " - " nr)
(strcat  "{\\L" nr " - " nr  "}")
Link to comment
Share on other sites

If so:

(defun c:pp2()
  (setq el (entget (car (entsel "pick (m)text")))
	nr (itoa (getint "enter number"))
	el (entmod (subst (cons 1 (strcat  "{\\L" nr " - " nr  "}") (assoc 1 el) el)))
  )

5.1.msg: incorrectly generated list at the entrance

? ? ?

Link to comment
Share on other sites

you deleted the closing bracket from the strcat.. so add there another one

(defun c:pp()
  (setq el (entget (car (entsel "pick (m)text")))
	nr (itoa (getint "enter number"))
	el (entmod (subst (cons 1 (strcat  "{\\L" nr " - " nr  "}")) (assoc 1 el) el)))
)

 

Edited by EnM4st3r
Link to comment
Share on other sites

EnM4st3r, thank.  it works with mtext

if you select underlined text  -   {\L6 - 6} ???

how can this be fixed?

Edited by Nikon
Link to comment
Share on other sites

this should work for both
 

(defun c:pp(/ el nr str)
  (setq el (entget (car (entsel "pick (m)text")))
	      nr (itoa (getint "enter number"))
  )
  (cond 
    ((wcmatch (cdr (assoc 0 el)) "MTEXT")
     (setq str (strcat  "{\\L" nr " - " nr  "}"))
     )
    ((wcmatch (cdr (assoc 0 el)) "TEXT")
     (setq str (strcat  "%%u" nr " - " nr))
     )
    (t
      (alert "thats no text obj")
      (exit)
    )
  )
	(entmod (subst (cons 1 str) (assoc 1 el) el))
)

 

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