Sambuddy Posted January 13, 2020 Posted January 13, 2020 The lisp below is lee macs Parsing numerical values from a text. I am wondering how to use this to change the colour within the selected Mtext only for real numbers that accompany "m", for example: 112.35m etc... <number> <dot> <number><m> sometimes there is one space between the real number and "m", so if I could first remove the space for every real number within the text for each, then change the colour for each. I gave it try and I seem not to be successful with this task. Thanks ;; 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
Lee Mac Posted January 13, 2020 Posted January 13, 2020 This form of problem is typically easier to solve using Regular Expressions, for example: (defun c:numcol ( / *error* col idx new obj rgx sel ) (setq col 6) ;; Colour for numerical text (defun *error* ( msg ) (if (and (= 'vla-object (type rgx)) (not (vlax-object-released-p rgx))) (vlax-release-object rgx) ) (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))) (princ (strcat "\nError: " msg)) ) (princ) ) (if (setq sel (ssget "_:L" '((0 . "MTEXT")(-4 . "<OR")(1 . "*#*")(3 . "*#*")(-4 . "OR>")))) (if (setq rgx (vlax-get-or-create-object "vbscript.regexp")) (progn (vlax-put-property rgx 'global actrue) (vlax-put-property rgx 'ignorecase acfalse) (vlax-put-property rgx 'multiline actrue) (vlax-put-property rgx 'pattern "( |^)(-?\\d+(?:\\.\\d+)?\\s*m)( |$)") (setq new (strcat "$1{\\C" (itoa col) ";$2}$3")) (repeat (setq idx (sslength sel)) (setq idx (1- idx) obj (vlax-ename->vla-object (ssname sel idx)) ) (vla-put-textstring obj (vlax-invoke rgx 'replace (vla-get-textstring obj) new)) ) ) (princ "\nUnable to interface with RegExp object.") ) ) (*error* nil) (princ) ) (vl-load-com) (princ) Note that the above does not account for pre-existing formatting within the MText. Quote
Sambuddy Posted January 14, 2020 Author Posted January 14, 2020 (edited) @Lee Mac How can I adjust this line to account for any dots, paran or other characters before and after the numbers Lee? Example I have 12.75m . or 215.56 m (blah) also could you let me know how to correct the letter <m> so it will correct for example: 34.50 m to 34.50m (no space between <m>) please? Also maybe due to formatting the lisp above works sometimes - I have another lisp to remove characters and change colour on certain words, so the colours are overridden and not by layer - Does this pose an issue, the reason why my output is random? (vlax-put-property rgx 'pattern "( |^)(-?\\d+(?:\\.\\d+)?\\s*m)( |$)") Thanks text.dwg Edited January 14, 2020 by Sambuddy different question Quote
ronjonp Posted January 14, 2020 Posted January 14, 2020 2 hours ago, Sambuddy said: also could you let me know how to correct the letter <m> so it will correct for example: 34.50 m to 34.50m (no space between <m>) please? You could use find and replace for that " m" with "m". Quote
Sambuddy Posted January 14, 2020 Author Posted January 14, 2020 @ronjonp I meant it to be as par of the lisp not multiple tasks. That is the reason why I was thinking about it - can I use "find" as a macro command in lisp? how? Thanks. Quote
BIGAL Posted January 15, 2020 Posted January 15, 2020 Find appears to be one of those that as it uses a dialogue for entry you can not control by lisp. I have tried vl-cmdf etc (vl-cmdf "FIND" "Prelim" (chr 9) "alan")) it resets once only tried counting tabs to move to findall then pass a return (chr 13) Quote
Lee Mac Posted January 15, 2020 Posted January 15, 2020 On 1/14/2020 at 12:51 PM, Sambuddy said: @Lee Mac How can I adjust this line to account for any dots, paran or other characters before and after the numbers Lee? Example I have 12.75m . or 215.56 m (blah) also could you let me know how to correct the letter <m> so it will correct for example: 34.50 m to 34.50m (no space between <m>) please? Also maybe due to formatting the lisp above works sometimes - I have another lisp to remove characters and change colour on certain words, so the colours are overridden and not by layer - Does this pose an issue, the reason why my output is random? (vlax-put-property rgx 'pattern "( |^)(-?\\d+(?:\\.\\d+)?\\s*m)( |$)") Thanks text.dwg 158.87 kB · 0 downloads Try the following instead: (defun c:numcol ( / *error* col idx new obj rgx sel ) (setq col 6) ;; Colour for numerical text (defun *error* ( msg ) (if (and (= 'vla-object (type rgx)) (not (vlax-object-released-p rgx))) (vlax-release-object rgx) ) (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))) (princ (strcat "\nError: " msg)) ) (princ) ) (if (setq sel (ssget "_:L" '((0 . "MTEXT")))) (if (setq rgx (vlax-get-or-create-object "vbscript.regexp")) (progn (vlax-put-property rgx 'global actrue) (vlax-put-property rgx 'ignorecase acfalse) (vlax-put-property rgx 'multiline actrue) (vlax-put-property rgx 'pattern "( |^)(-?\\d+(?:\\.\\d+)?)\\s*(m)(\\s|\\W|$)") (setq new (strcat "$1{\\C" (itoa col) ";$2$3}$4")) (repeat (setq idx (sslength sel)) (setq idx (1- idx) obj (vlax-ename->vla-object (ssname sel idx)) ) (vla-put-textstring obj (vlax-invoke rgx 'replace (vla-get-textstring obj) new)) ) ) (princ "\nUnable to interface with RegExp object.") ) ) (*error* nil) (princ) ) (vl-load-com) (princ) Quote
Sambuddy Posted January 15, 2020 Author Posted January 15, 2020 Hey Lee, I have no words to describe how much i appreciate your work - simply wonderful! Can I ask where I could possible obtain 'patten syntax or formula for future use. It is VERY obvious that you know all about autolisp but if I want to twick the property, would there be a website I could use? THANK YOU VERY MUCH! Quote
Sambuddy Posted January 15, 2020 Author Posted January 15, 2020 @Lee Mac I have tried it a couple of times now and it seems to work just as expected. It is great to have your input and your work and help is indeed invaluable. Thank you again Quote
Lee Mac Posted January 15, 2020 Posted January 15, 2020 You're most welcome @Sambuddy, I'm pleased that you'll find this example code useful. Similar to wildcards in AutoCAD, Regular Expressions provide a standard means of describing patterns within text, and are implemented in many different APIs; as such, there are many resources online describing their syntax & usage - simply Google "Regular Expressions". Quote
Sambuddy Posted February 26, 2020 Author Posted February 26, 2020 @Lee Mac Hey Lee, I have been scratching my head for a while now with the regular expression for expression/ numbers that have ± before the number for the routine you wrote above, but could not figure it out. example ±12.25 m to be colour 210. As of now everything that does not include ± changes to colour 210 and it corrects the spacing between numbers and "m" to show them as 12.25m which is exactly what i wanted, but as soon as add ±12.25 m nothing happens. I tried to fiddle around with this part but no luck. My intention is to include ± so that the entire ±12.25m to change to colour 210 by manipulating your expression. That would go for 12.25 m as well as anytime there is ±12.25 m. (-?\\d+ (vlax-put-property rgx 'pattern "( |^)(-?\\d+(?:\\.\\d+)?)\\s*(m)(\\s|\\W|$)") could you help please? Thanks Quote
Lee Mac Posted February 26, 2020 Posted February 26, 2020 1 hour ago, Sambuddy said: I have been scratching my head for a while now with the regular expression for expression/ numbers that have ± before the number for the routine you wrote above, but could not figure it out. example ±12.25 m to be colour 210. As of now everything that does not include ± changes to colour 210 and it corrects the spacing between numbers and "m" to show them as 12.25m which is exactly what i wanted, but as soon as add ±12.25 m nothing happens. I tried to fiddle around with this part but no luck. My intention is to include ± so that the entire ±12.25m to change to colour 210 by manipulating your expression. That would go for 12.25 m as well as anytime there is ±12.25 m. Try changing the RegEx pattern to: "( |^)((?:-|%%P|±)?\\d+(?:\\.\\d+)?)\\s*(m)(\\s|\\W|$)" Quote
Sambuddy Posted February 26, 2020 Author Posted February 26, 2020 @Lee Mac I thank you very much Lee. I have no words to express my gratitude other than with the simplest words: YOU ARE INVALUABLE. Like of you are rare to find and I can only hope to be as knowledgeable are are in a 100 years! Thank you again! Works like a charm I will now try to manipulate it to correct the spacing between ± and numbers, same as how you corrected the spacing between numbers and "m" ± 12.25 m > ±12.25m if there is spacing. I doublt I would every succeed but I will try! Quote
Lee Mac Posted February 26, 2020 Posted February 26, 2020 Thank you for your kind words @Sambuddy - that's very nice of you to say and I appreciate your gratitude. Let me know if you get stuck amending the pattern and replacement text and I'll be happy to lend a hand. Quote
Sambuddy Posted February 27, 2020 Author Posted February 27, 2020 @Lee Mac No luck, still with removing the spacing between the ± and the numbers. It still works great but for those that are mistakenly written with a space between ± and numbers as: ± 12.25 m > ± 12.25m and ± remains uncoloured. I was meaning to ask you a different question though. If you could pick your brain - Do you think it is possible for Mtext to each time I could select a portion of the context then right-click or better yet ' command to give me access to a string list to change the text to that of list selection? Example: before> - RETIRER LES XXX (X) ANTENNES EXISTANTES selection process > - RETIRER LES XXX (X) ANTENNES EXISTANTES then key in an integrated command with ' command to show me a list of say ONE (1) TWO (2) THREE (3) ... and when I select ONE (1), it would replace XXX (X) with the selection. I am fiddling around with this idea, and if successful, it would really ease a lot of problems - This is not an elegant example but once i get a few lines going, then I would replace names, add stuff and so on. Do you think it is possible Lee? Appreciate any feed backs Quote
Lee Mac Posted February 27, 2020 Posted February 27, 2020 8 hours ago, Sambuddy said: @Lee Mac No luck, still with removing the spacing between the ± and the numbers. It still works great but for those that are mistakenly written with a space between ± and numbers as: ± 12.25 m > ± 12.25m and ± remains uncoloured. Try the following: (defun c:numcol ( / *error* col idx new obj rgx sel ) (setq col 6) ;; Colour for numerical text (defun *error* ( msg ) (if (and (= 'vla-object (type rgx)) (not (vlax-object-released-p rgx))) (vlax-release-object rgx) ) (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))) (princ (strcat "\nError: " msg)) ) (princ) ) (if (setq sel (ssget "_:L" '((0 . "MTEXT")))) (if (setq rgx (vlax-get-or-create-object "vbscript.regexp")) (progn (vlax-put-property rgx 'global actrue) (vlax-put-property rgx 'ignorecase acfalse) (vlax-put-property rgx 'multiline actrue) (vlax-put-property rgx 'pattern "( |^)(?:(-|%%P|±)\\s*)?(\\d+(?:\\.\\d+)?)\\s*(m)(\\s|\\W|$)") (setq new (strcat "$1{\\C" (itoa col) ";$2$3$4}$5")) (repeat (setq idx (sslength sel)) (setq idx (1- idx) obj (vlax-ename->vla-object (ssname sel idx)) ) (vla-put-textstring obj (vlax-invoke rgx 'replace (vla-get-textstring obj) new)) ) ) (princ "\nUnable to interface with RegExp object.") ) ) (*error* nil) (princ) ) (vl-load-com) (princ) Quote
Sambuddy Posted February 28, 2020 Author Posted February 28, 2020 @Lee Mac It is working as you magically make the problems disappear! I thank you very much Lee 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.