Nikon Posted Wednesday at 11:25 AM Share Posted Wednesday at 11:25 AM (edited) Good day to all! How to replace only the numbers at the end in the text (mtext). Select multiple texts and replace the numbers with the same number. Is there a lisp that allows you to replace only numbers in selected texts? For example: Text 1 Text text 1 Text text text 1 MLeader 1 Replace with: Text 34 Text text 34 Text text text 34 MLeader 34 Is it possible to add MLeaders to the selection? Thank you in advance... Replace numbers.dwg Edited Wednesday at 01:06 PM by Nikon Quote Link to comment Share on other sites More sharing options...
hosneyalaa Posted Wednesday at 12:24 PM Share Posted Wednesday at 12:24 PM CAN YOU USE THIS https://www.lee-mac.com/parsenumbers.html ;; Parse Numbers - Lee Mac ;; Parses a list of numerical values from a supplied string. (defun LM:parsenumbers ( str ) ( (lambda ( l ) (read (strcat "(" (vl-list->string (mapcar '(lambda ( a b c ) (if (or (< 47 b 58) (and (= 45 b) (< 47 c 58) (not (< 47 a 58))) (and (= 46 b) (< 47 a 58) (< 47 c 58)) ) b 32 ) ) (cons nil l) l (append (cdr l) '(())) ) ) ")" ) ) ) (vl-string->list str) ) ) Quote Link to comment Share on other sites More sharing options...
Emmanuel Delay Posted Wednesday at 12:42 PM Share Posted Wednesday at 12:42 PM @ Nikon Those are bad examples. What do you want when it's something defferent from a 1? What about two ones? What about two numbers on different places? "Text 1text 1" "T3322223333665589657ext text" "Tex44t text text9000" What do you expect there? Quote Link to comment Share on other sites More sharing options...
Nikon Posted Wednesday at 01:04 PM Author Share Posted Wednesday at 01:04 PM (edited) 25 minutes ago, Emmanuel Delay said: "Text 1text 1" "T3322223333665589657ext text" "Tex44t text text9000" What do you expect there? I have text (or MLeader) with the same numbers at the end (1 or 12 or 33...) I need to replace all the same numbers with other same ones... see the attached .dwg The numbers are only at the end Edited Wednesday at 01:08 PM by Nikon Quote Link to comment Share on other sites More sharing options...
Tsuky Posted yesterday at 01:03 AM Share Posted yesterday at 01:03 AM With your exemple seem to do the job. (defun extractNumbers (str / l rslt) (setq l (mapcar '(lambda (x) (if (and (> x 44) (< x 58) (/= x 47)) x 32) ) (vl-string->list str) ) l (mapcar '(lambda (x y) (if (not (= x y 32)) x) ) l (append (cdr l) '(32)) ) l (vl-remove-if-not '(lambda (x) (eq (type x) 'INT) x) l) l (mapcar '(lambda (x) (if (not (eq x 32)) x (list nil))) l) ) (eval (read (strcat "(setq rslt (list " (apply 'strcat (mapcar '(lambda (x) (if (not (listp x)) (chr x) " ")) l)) "))"))) ) (defun c:replacenumber ( / ss nb n ename dxf_ent txt old_num old_list) (setq ss (ssget '((0 . "*TEXT")))) (cond (ss (initget 1) (setq nb (getint "\nEnter number to substitue: ") nb (vl-string->list (itoa nb)) ) (repeat (setq n (sslength ss)) (setq ename (ssname ss (setq n (1- n))) dxf_ent (entget ename) txt (cdr (assoc 1 dxf_ent)) ) (setq old_num (vl-string->list (itoa (car (extractNumbers txt)))) old_list (vl-string->list txt) ) (foreach e old_num (setq old_list (vl-remove e old_list))) (entmod (subst (cons 1 (vl-list->string (append old_list nb))) (assoc 1 dxf_ent) dxf_ent)) ) ) ) (prin1) ) 1 Quote Link to comment Share on other sites More sharing options...
Nikon Posted 20 hours ago Author Share Posted 20 hours ago 5 hours ago, Tsuky said: (defun c:replacenumber @Tsuky Thanks a lot! This code handles text and mtext well. Is it possible to add MLeader and attributes? I have similar designs that I copy, modify and change the names with numbers. I changed 30-40 titles one by one, I caught myself at number 35 and asked for help, I will need to change 100 or more numbers. You have helped me a lot! Quote Link to comment Share on other sites More sharing options...
Tsuky Posted 15 hours ago Share Posted 15 hours ago Quote Is it possible to add MLeader and attributes? Tested briefly .... (vl-load-com) (defun extractNumbers (str / l rslt) (setq l (mapcar '(lambda (x) (if (and (> x 44) (< x 58) (/= x 47)) x 32) ) (vl-string->list str) ) l (mapcar '(lambda (x y) (if (not (= x y 32)) x) ) l (append (cdr l) '(32)) ) l (vl-remove-if-not '(lambda (x) (eq (type x) 'INT) x) l) l (mapcar '(lambda (x) (if (not (eq x 32)) x (list nil))) l) ) (eval (read (strcat "(setq rslt (list " (apply 'strcat (mapcar '(lambda (x) (if (not (listp x)) (chr x) " ")) l)) "))"))) ) (defun c:replacenumber ( / ss nb n ename txt rslt old_num old_list) (setq ss (ssget (list (cons 0 "*TEXT,MULTILEADER,INSERT") (cons 67 (if (eq (getvar "CVPORT") 2) 0 1)) (cons 410 (if (eq (getvar "CVPORT") 2) "Model" (getvar "CTAB"))) ) ) ) (cond (ss (initget 1) (setq nb (getint "\nEnter number to substitue: ") nb (vl-string->list (itoa nb)) ) (repeat (setq n (sslength ss)) (setq ename (vlax-ename->vla-object (cadar (ssnamex ss (setq n (1- n))))) ) (cond ((vlax-property-available-p ename 'TextString) (setq txt (vlax-get ename 'TextString)) (cond ((eq (type (setq rslt (car (extractNumbers txt)))) 'INT) (setq old_num (vl-string->list (itoa rslt)) old_list (vl-string->list txt) ) (foreach e old_num (setq old_list (vl-remove e old_list))) (vlax-put ename 'TextString (vl-list->string (append old_list nb))) ) ) ) (T (mapcar '(lambda (att) (setq txt (vlax-get-property att 'TextString)) (cond ((eq (type (setq rslt (car (extractNumbers txt)))) 'INT) (setq old_num (vl-string->list (itoa rslt)) old_list (vl-string->list txt) ) (foreach e old_num (setq old_list (vl-remove e old_list))) (vlax-put-property att 'TextString (vl-list->string (append old_list nb))) ) ) ) (vlax-invoke ename 'GetAttributes) ) ) ) ) ) ) (prin1) ) 1 Quote Link to comment Share on other sites More sharing options...
Nikon Posted 15 hours ago Author Share Posted 15 hours ago 50 minutes ago, Tsuky said: (cons 0 "*TEXT,MULTILEADER,INSERT") @Tsuky Thanks for the quick reaction! It works well with mleaders. But it doesn't work with attributes. Quote Link to comment Share on other sites More sharing options...
Tsuky Posted 14 hours ago Share Posted 14 hours ago Please give me an example drawing so I can identify what is wrong. Quote Link to comment Share on other sites More sharing options...
Nikon Posted 14 hours ago Author Share Posted 14 hours ago (edited) 1 hour ago, Tsuky said: Please give me an example drawing so I can identify what is wrong. I have attributes in a dynamic block, I will need to change the numbers in one of them (maybe you need to specify an attribute tag to select?) maybe it will be easier to write a separate code for attributes? GRAPH_F.dwg Edited 12 hours ago by Nikon Quote Link to comment Share on other sites More sharing options...
Tsuky Posted 13 hours ago Share Posted 13 hours ago This version ? replacenumber.lsp 1 Quote Link to comment Share on other sites More sharing options...
Steven P Posted 13 hours ago Share Posted 13 hours ago If it was me I'd use something like entsel to grab the text values, then use Lee Macs string to List with a delimitator of space ( " ") to split the string up into part, reverse the list and subs the new value into what is now the first item in the list. Reverse the list again, Lee Macs List to String, and pop that back into the text. Noting that uses a similar method. For attribute blocks, VLA- functions to get and replace the text strings in the attributes rather than entsel 1 Quote Link to comment Share on other sites More sharing options...
Emmanuel Delay Posted 12 hours ago Share Posted 12 hours ago Has anyone ever written a decent function that extracts all kinds of numeric values from a string? for example "foo12.3e+6bar" should find 12.3E+6 (12.3 million) What I would like is for the function either to return a list of the numbers along with position and length in the string. Like "foo36bar12.4test" =>(list (list 3 2 36) (list 8 4 12.4) ) Or return alternate string / number ... (list "foo" 36 "bar" 12.4 "test") Quote Link to comment Share on other sites More sharing options...
Nikon Posted 12 hours ago Author Share Posted 12 hours ago 50 minutes ago, Tsuky said: This version ? replacenumber.lsp @Tsuky very, very grateful! This code performs the task wonderfully! Now the work will go faster... Quote Link to comment Share on other sites More sharing options...
BIGAL Posted 4 hours ago Share Posted 4 hours ago @Emmanuel Delay over at Theswamp, AutoLisp (Vanilla/Visual) is a sub forum "Challenges" its a good place to put this type of question as I expect some form of mapcar expression to solve. https://www.theswamp.org/index.php?board=2.0 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.