Jump to content

Replace the text with a single digit


Nikon

Recommended Posts

Yes! It works for text and мtext!!! Thank you very much, EnM4st3r!

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

 

Edited by Nikon
Link to comment
Share on other sites

38 minutes ago, EnM4st3r said:

@Steven P i think yours was not working since he is using spaces in between the numbers

 

That's a easy fix, ta.

 

(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

  (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 (wcmatch MyText "*# `- #*") (setq OldNumber (substr MyText (- MyStart 1) 5)))   ; for 0-0 to 9-9
  (if (wcmatch MyText "*## `- ##*") (setq OldNumber (substr MyText (- MyStart 2) 7)))   ; for 10-10 to 99-99
  (if (wcmatch MyText "*### `- ###*") (setq OldNumber (substr MyText (- MyStart 3) 9)))   ; 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
      (if (wcmatch MyText "*#`-#*")
        (setq NewText (vl-string-subst (strcat NewNumber "-" NewNumber) OldNumber MyText)) ; substitute new text in
        (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
  • Like 1
Link to comment
Share on other sites

so @Nikon the diffrence between Stevens code and the other, is that Stevens code looks for the part with x-x within the text and replaces only that part.

The other code replaces everything inside the text obj

Link to comment
Share on other sites

Benefit for that way is it retains any other formatting in the text string, (such as mtext colour over rides (except in the required text portion), italics, and so on) and can be used to replace the text if it is in a larger text block

  • Agree 1
Link to comment
Share on other sites

2 hours ago, Steven P said:

 

Это легко исправить, ta.

 

(defun c:test ( / MyEnt MyEntGet myText, MyStart oldNumber, NewNumber, newText)
 (vl-load-com)
 (setq MyEnt (car (entsel "\nSelect Text")))
 (while ;; цикл while, проверяющий, был ли выделен текст 
 (и
 (/= (cdr (assoc 0 (entget MyEnt))) "ТЕКСТ")
 (/= (cdr (assoc 0 (entget MyEnt))) "MTEXT")
 )
 (принц "\ N Ну, это ведь не текст, не так ли? ")
 (setq MyEnt (car (укажите "Пожалуйста, выберите текст")))
 )
 (setq MyEntGet (entget MyEnt)) ; получить определение текстовой сущности 
 (setq myText (cdr (assoc 1 MyEntGet))) ; получить текстовую строку 
 (setq MyStart (vl-string-search "-" myText)) ; найдите первый экземпляр "-" в текстовой строке. Проверьте, существует ли ошибка?
 (setq NewNumber (getstring "\nEnter New Number : " t)) ; запросите номер замены, t, чтобы разрешить пробелы в тексте

 (если (wcmatch myText "*#`-#*") ( установите старый номер (substr myText (- MyStart 0) 3))) ; от 0-0 до 9-9
 (если (wcmatch myText "*##`-##*") ( установите старый номер (substr myText (- MyStart 1) 5))) ; от 10-10 до 99-99
 (если (wcmatch myText "*###`-###*") ( установите старый номер (substr myText (- MyStart 2) 7))) ; от 100-100 до 999-999

 (если (wcmatch myText "*# `- #*") ( установите старый номер (substr myText (- MyStart 1) 5))) ; от 0-0 до 9-9
 (если (wcmatch myText "*## `- ##*") ( установите старый номер (substr myText (- MyStart 2) 7))) ; от 10-10 до 99-99
 (если (wcmatch myText "*### `- ###*") ( установите старый номер (substr myText (- MyStart 3) 9))) ; от 100-100 до 999-999


 (if (= oldNumber nil) ; если текст содержит допустимый формат, обновите текст
 (принц "Текстовая строка не содержит текст в формате 'xx-xx'. Сейчас пойду вздремну.")
 (progn 
 (if (wcmatch myText "*#`-#*")
 (setq newText (vl-string-subst (strcat NewNumber "-" Новый номер) oldNumber myText)) ; замените новый текст в 
 (setq newText (vl-string-subst (strcat NewNumber " - " Новый номер) Старый номер myText)) ; замените новый текст в
 )
 (vla-put-TextString (vlax-ename-> vla-object MyEnt) Новый текст)
 )
 )

 (принцип) ; тихо завершите работу 
) (если (wcmatch myText "*# `- #*") ( установите старый номер (substr myText (- MyStart 1) 5))) ; от 0-0 до 9-9
 (если (wcmatch myText "*## `- ##*") ( установите старый номер (substr myText (- MyStart 2) 7))) ; от 10-10 до 99-99
 (если (wcmatch myText "*### `- ###*") ( установите старый номер (substr myText (- MyStart 3) 9))) ; от 100-100 до 999-999


 (if (= oldNumber nil) ; если текст содержит допустимый формат, обновите текст
 (принц "Текстовая строка не содержит текст в формате 'xx-xx'. Сейчас пойду вздремну.")
 (progn 
 (if (wcmatch myText "*#`-#*")
 (setq newText (vl-string-subst (strcat NewNumber "-" Новый номер) oldNumber myText)) ; замените новый текст в 
 (setq newText (vl-string-subst (strcat NewNumber " - " Новый номер) Старый номер myText)) ; замените новый текст в
 )
 (vla-put-TextString (vlax-ename-> vla-object MyEnt) Новый текст)
 )
 )

 (принцип) ; тихо завершите работу 
)

 

 

Steven P, thank you. Your code is very complex, but it works well for my simple task...

Link to comment
Share on other sites

Is it difficult to make such an option:
first write the number 1 (for example), the specify the insertion point and get the underlined text 1-1?

Edited by Nikon
Link to comment
Share on other sites

1 hour ago, Nikon said:

Is it difficult to make such an option:
first write the number 1 (for example), the specify the insertion point and get the underlined text 1-1?

 

like this?

(defun c:pp2 (/ nr str ip)
  (setq nr (itoa (getint "\nEnter number: "))
        str (strcat  "{\\L" nr " - " nr  "}")
        ip  (vlax-3d-point (getpoint "\nPick Insertion Point: "))
  )
  (vla-addmtext (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))) ip 0 str)
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

39 minutes ago, Nikon said:

Steven P, thank you. Your code is very complex, but it works well for my simple task...

 

No problem, sometimes a few extra minutes to make something a little more versatile is worth it (and of course, sometimes it isn't).

 

As above for Nikon, adding new text is often simpler than modifying existing text

 

 

Link to comment
Share on other sites

@Nikon you can also add a while loop so you can continue the placement. Dont know if that would be useful:
 

(defun c:pp2 (/ nr str ip)
  (setq nr (itoa (getint "\nEnter number: "))
        str (strcat  "{\\L" nr " - " nr  "}")   
  )
  (while (setq ip (vlax-3d-point (getpoint "\nPick Insertion Point: ")))
    (vla-addmtext (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))) ip 0 str)
  )
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

18 minutes ago, EnM4st3r said:

@Nikon you can also add a while loop so you can continue the placement. Dont know if that would be useful:
 

(defun c:pp2 (/ nr str ip)
  (setq nr (itoa (getint "\nEnter number: "))
        str (strcat  "{\\L" nr " - " nr  "}")   
  )
  (while (setq ip (vlax-3d-point (getpoint "\nPick Insertion Point: ")))
    (vla-addmtext (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))) ip 0 str)
  )
  (princ)
)

 

This is also useful, many thanks ...

Edited by Nikon
Link to comment
Share on other sites

3 hours ago, EnM4st3r said:

@Nikon you can also add a while loop so you can continue the placement. Dont know if that would be useful:
 

(defun c:pp2 (/ nr str ip)
  (setq nr (itoa (getint "\nEnter number: "))
        str (strcat  "{\\L" nr " - " nr  "}")   
  )
  (while (setq ip (vlax-3d-point (getpoint "\nPick Insertion Point: ")))
    (vla-addmtext (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))) ip 0 str)
  )
  (princ)
)

 

 

FWIW, You should move the vlax-3d-point into the loop so it does not error out.

(defun c:pp2 (/ nr str ip)
  (setq	nr  (itoa (getint "\nEnter number: "))
	str (strcat "{\\L" nr " - " nr "}")
  )
  (while (setq ip (getpoint "\nPick Insertion Point: "))
    (vla-addmtext
      (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
      (vlax-3d-point ip)
      0
      str
    )
  )
  (princ)
)

 

  • Like 1
  • Agree 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...