Nikon Posted December 12, 2023 Share Posted December 12, 2023 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. Quote Link to comment Share on other sites More sharing options...
Steven P Posted December 12, 2023 Share Posted December 12, 2023 (edited) 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 December 12, 2023 by Steven P Quote Link to comment Share on other sites More sharing options...
Nikon Posted December 12, 2023 Author Share Posted December 12, 2023 (edited) 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 December 12, 2023 by Nikon Quote Link to comment Share on other sites More sharing options...
fuccaro Posted December 12, 2023 Share Posted December 12, 2023 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))) ) 1 Quote Link to comment Share on other sites More sharing options...
Nikon Posted December 12, 2023 Author Share Posted December 12, 2023 fuccaro, thanks, only underscores are not supported... Quote Link to comment Share on other sites More sharing options...
Nikon Posted December 12, 2023 Author Share Posted December 12, 2023 Lisp works great, is it possible to leave the result underlined? Thank... Quote Link to comment Share on other sites More sharing options...
ronjonp Posted December 12, 2023 Share Posted December 12, 2023 FIND? Quote Link to comment Share on other sites More sharing options...
Steven P Posted December 12, 2023 Share Posted December 12, 2023 (edited) 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 December 12, 2023 by Steven P Quote Link to comment Share on other sites More sharing options...
Steven P Posted December 12, 2023 Share Posted December 12, 2023 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) Quote Link to comment Share on other sites More sharing options...
Nikon Posted December 13, 2023 Author Share Posted December 13, 2023 (edited) 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 December 13, 2023 by Nikon Quote Link to comment Share on other sites More sharing options...
EnM4st3r Posted December 13, 2023 Share Posted December 13, 2023 you can use this for the strcat if its text obj: (strcat "%%u" nr " - " nr) and if its mtext you can use this: (strcat "{\\L" nr " - " nr "}") Quote Link to comment Share on other sites More sharing options...
Steven P Posted December 13, 2023 Share Posted December 13, 2023 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 ) Quote Link to comment Share on other sites More sharing options...
Nikon Posted December 13, 2023 Author Share Posted December 13, 2023 (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 "}") Quote Link to comment Share on other sites More sharing options...
Nikon Posted December 13, 2023 Author Share Posted December 13, 2023 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 ? ? ? Quote Link to comment Share on other sites More sharing options...
EnM4st3r Posted December 13, 2023 Share Posted December 13, 2023 (edited) you did that correct. your just missing a closing bracket Edited December 13, 2023 by EnM4st3r Quote Link to comment Share on other sites More sharing options...
EnM4st3r Posted December 13, 2023 Share Posted December 13, 2023 (edited) 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 December 13, 2023 by EnM4st3r Quote Link to comment Share on other sites More sharing options...
Nikon Posted December 13, 2023 Author Share Posted December 13, 2023 (edited) EnM4st3r, thank. it works with mtext if you select underlined text - {\L6 - 6} ??? how can this be fixed? Edited December 13, 2023 by Nikon Quote Link to comment Share on other sites More sharing options...
EnM4st3r Posted December 13, 2023 Share Posted December 13, 2023 thats why i said for text this one, you could also implement both in one code and check if its text or mtext (strcat "%%u" nr " - " nr) Quote Link to comment Share on other sites More sharing options...
EnM4st3r Posted December 13, 2023 Share Posted December 13, 2023 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)) ) 1 Quote Link to comment Share on other sites More sharing options...
EnM4st3r Posted December 13, 2023 Share Posted December 13, 2023 @Steven P i think yours was not working since he is using spaces in between the numbers 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.