Artek Posted May 30, 2014 Posted May 30, 2014 Hi! Is there anyone out there got an idea or willing to share a code on how to sort numbers and symbols in a text/mtext/attribute? I want to sort the numbers in numerical order and then affix the symbol "&" before the last number. For example: I got this line in text/mtext/attribute value in this random order: 4, 3, 5, 2 & 1 OR 4, & 3, 5, 2, 1 (misplaced "&") OR 4 3 5 2 1 (missing "," and "&") Is it possible to re-arrange them in this numerical order, including the symbols "," and "&" (or add them if they're missing or misplaced) to look like this? 1, 2, 3, 4 & 5 I need to tidy up a lot of old drawings with texts typed in different formats and it's a total nightmare doing them. Any help would be highly appreciated. Thanks. Quote
pBe Posted May 30, 2014 Posted May 30, 2014 (edited) Is this limited to single digit numbers? nothing like this ---> "11, 12, 14, 13" Will there be instances there are missing numbers? ---> "5, 4, 2, 1" What about duplicates? "1, 2, 3, 3, 4, 5" No reply yet : anyhoo try this (defun _sortto (str / nstr a b) (setq nstr "" a (vl-sort (read (strcat "(" (vl-string-translate ",&" " " (strcase STR) ) ")" ) ) '< ) ) (while (and (setq b (Car a)) (> (length a) 2) ) (setq nstr (strcat nstr (itoa b) ", ") a (cdr a) ) ) (strcat nstr (itoa (car a)) " & " (itoa (cadr a))) ) _$ (_sortto "4, 3, 5, 2 & 1") "1, 2, 3, 4 & 5" _$ (_sortto "4 3 5 2 1") "1, 2, 3, 4 & 5" _$ (_sortto "4, & 3, 5, 2, 1") "1, 2, 3, 4 & 5" _$ (_sortto "14, 3, 5, 23 & 1") "1, 3, 5, 14 & 23" Edited May 30, 2014 by pBe Quote
Artek Posted May 30, 2014 Author Posted May 30, 2014 Hi pBE, Thanks for your quick reply. To answer your questions: Is this limited to single digit numbers? nothing like this ---> "11, 12, 14, 13" No. It's not limited to single digit numbers. Some of them are up to 4 digits. Will there be instances there are missing numbers? ---> "5, 4, 2, 1" No. Only what's already there. But the "," and the "&" symbol could be missing. What about duplicates? "1, 2, 3, 3, 4, 5" No duplicates. Cheers Quote
ymg3 Posted May 30, 2014 Posted May 30, 2014 Artek, How about posting a typical drawing that you need to process ? Would remove some of the guess work. ymg Quote
Artek Posted May 30, 2014 Author Posted May 30, 2014 (edited) Hi ymg3, Thanks. It's just a normal line of text, mtext or attribute value with multiple numbers sometimes arranged in random orders. I've attached a sample drawing as requested. pBe's code seems to do what I'm looking for based on his sample tests but I'm struggling to fill in the missing lines. Block Number.dwg Edited May 30, 2014 by Artek Removed repeating words. Quote
pBe Posted May 31, 2014 Posted May 31, 2014 Thanks. It's just a normal line of text, mtext or attribute value with multiple numbers sometimes arranged in random orders. I've attached a sample drawing as requested. pBe's code seems to do what I'm looking for based on his sample tests but I'm struggling to fill in the missing lines. For the attributes, will you be using the routine specifically for this block name "Block Number"? Quote
Artek Posted May 31, 2014 Author Posted May 31, 2014 For the attributes, will you be using the routine specifically for this block name "Block Number"? Yes. It's only the block named "Block Number" with that attribute value that needs sorting out. If it's possible to make a single code to work on three different objects (text/mtext/attributes) then that would be ideal. But if it's impossible, then I would rather have a routine that can update the attribute value because it's the most time consuming to do. Quote
pBe Posted May 31, 2014 Posted May 31, 2014 (edited) CODE UPDATED [June012014] (defun c:sns ( / _sortto ss i e dz) ;;; pBe Jun2014 ;;; [color="blue"](setq dz (getvar 'Dimzin))(setvar 'Dimzin [/color] (defun _sortto (str / _roi nstr a b) [color="blue"](setq _roi (lambda (v) ((if (Eq (type v) 'INT) itoa rtos) v)))[/color] (setq nstr "" [color="blue"]str (vl-string-translate "-" "." str)[/color] a (vl-sort (read (strcat "(" (vl-string-translate [color="blue"] "ABCDEFGHIJKLMNOPQRSTUVWXYZ,&!@#$%^*()_+=/\\<>{}[]|:;'\"" " "[/color] (Strcase str) ) ")" ) ) '< ) ) (while (and (setq b (Car a)) (> (length a) 2) ) (setq nstr (strcat nstr (_roi b) ", ") a (cdr a) ) ) (if (>= (length a) 2) [color="blue"](vl-string-translate "." "-"[/color] (strcat nstr (_roi (car a)) " & " (_roi (cadr a)))) str) ) (princ "\nSelect TEXT/MTEXT/ATTRIBUTE") (if (setq ss (ssget "_:L" '((-4 . "<OR") (-4 . "<AND") (0 . "INSERT")(2 . "Block Number") (-4 . "AND>") (-4 . "<AND") (0 . "TEXT,MTEXT") (1 . "#*#") (-4 . "AND>")(-4 . "OR>") ) ) ) (repeat (setq i (sslength ss)) (if (eq "AcDbBlockReference" (vla-get-ObjectName (setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)))) ) ) ) (foreach att (vlax-invoke e 'GetAttributes) (if (wcmatch (setq str (vla-get-textstring att)) [color="blue"] "#*#"[/color] ) (Vla-put-textstring att (_sortto str)) ) ) (vla-put-textstring e (_sortto (vla-get-textstring e))) ) ) )[color="blue"](setvar 'Dimzin dz)[/color] (princ) ) Edited June 1, 2014 by pBe UPDATED: remove _strans <does not make sense> Quote
Artek Posted May 31, 2014 Author Posted May 31, 2014 Wow!!! You're a genius pBe! It's absolutely bang on! Thank you very much. Quote
Artek Posted May 31, 2014 Author Posted May 31, 2014 Sorry to be a pain, pBe. While I was running your code, I just encountered an attribute with some long numbers being shortened by a hyphen like 34-37 and I got this error message: bad argument type for compare. Could this be easily fixed? Quote
pBe Posted May 31, 2014 Posted May 31, 2014 no worries Artek, its an easy fix , we just need to add more symbols to replace with space " " for comparison. CODE UPDATED AT POST # 8 EDIT shortened by a hyphen like 34-37 Hang on. does that mean you want to fill the missing numbers like in your example add 35 & 36 to make 34, 35, 36, 37? Quote
Artek Posted June 1, 2014 Author Posted June 1, 2014 No. It should remain exactly as 34-37. No need to fill the missing numbers. Quote
pBe Posted June 1, 2014 Posted June 1, 2014 (edited) No. It should remain exactly as 34-37. No need to fill the missing numbers. CODE UPDATED AT POST # 8 [i hope it would be the last ] Just a thought, its odd if you are going to use a "range" type of notation for some and not for others. "1, 2, 3, 5-7 & 9" <--> "[b]1-3[/b], 5-7, & 9" <--> "1, 2, 3, [b]5 ,6, 7[/b] & 9" But that will open up a whole can of worms , perhaps lets just stay with what you already have Artek. Lucky for you i have time to play with the code. HTH Edited June 1, 2014 by pBe Quote
Artek Posted June 1, 2014 Author Posted June 1, 2014 You're a star pBe. I'm really glad that you're around to help me out. Thank you so much and my apologies again for the trouble. Quote
pBe Posted June 1, 2014 Posted June 1, 2014 You're a star pBe. I'm really glad that you're around to help me out. Thank you so much and my apologies again for the trouble. You are welcome. Make sure you understand the code and learn from it. Otherwise it would be all for nothing. Happy to help Artek. Quote
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.