I'm new to VLISP and I've been seeking the online references for this task it's time to ask others.
I have a bunch of street addresses on the screen that are all TEXT entities, e.g. 4621 N KERBY AVE. I simply want to parse the text to the first space character so it only has the number, in this case it would leave 4621 only.
Here's the basic start so I can add on conditionals or whatever it is I would need. Can anyone suggest what direction I should take this in?
(defun c:addresses (/ addr)
(setq addr (ssget '((0 . "TEXT"))))
(read addr)
)
Thank You!
Solved! Go to Solution.
Solved by Jason.Piercey. Go to Solution.
This will print out the numbers to the commandline... IDK what your final goal is to do w/ the numbers.. Do you want to modify the text objects to only have the number? Regardless, here is 1 demo:
(defun c:addresses (/ addr n str) (setq addr (ssget '((0 . "TEXT")))) (setq n (- (sslength addr) 1)) (while (> n -1) (setq str (cdr (assoc 1 (entget (ssname addr n))))) (princ (substr str 1 (vl-string-search " " str))) (terpri) (setq n (1- n)) );while (princ) );defun
Yes, I want to modify the text objects so they only have the number.
And thank you for the clarification; the READ command is the wrong choice becuase the return can only be output to the command line. Got it!
Something like the following:
(defun c:addresses (/ ss i object string result) (if (setq ss (ssget '((0 . "TEXT")))) (progn (setq i 0) (repeat (sslength ss) (setq object (vlax-ename->vla-object (ssname ss i))) (setq string (vla-get-textstring object)) (setq result (car (parse string (chr 32)))) (if (numberp (read result)) (vla-put-textstring object result) ) (setq i (1+ i)) ) ) ) (princ) ) ; creates multiple strings from a single string ; [s] - string, string to breakdown ; [d] - string, separator ; return: list of strings (defun parse (s d / i l tmp rtn) (cond ((/= "" d) (setq l (strlen d)) (while (setq i (vl-string-search d s)) (setq tmp (substr s 1 i)) (setq rtn (cons tmp rtn)) (setq s (substr s (1+ (+ l (strlen tmp))) (strlen s))) ) (vl-remove "" (reverse (cons s rtn))) ) (t s) ) )
Almost! It didn't seem to parse to the first space, however, because the address
4635-4637 N CONGRESS AVE
remained the same.
Without using READ and removing verification the first characters are numbers:
(defun c:addresses (/ ss i object string result) (if (setq ss (ssget '((0 . "TEXT")))) (progn (setq i 0) (repeat (sslength ss) (setq object (vlax-ename->vla-object (ssname ss i))) (setq string (vla-get-textstring object)) (setq result (car (parse string (chr 32)))) (vla-put-textstring object result) (setq i (1+ i)) ) ) ) (princ) ) ; creates multiple strings from a single string ; [s] - string, string to breakdown ; [d] - string, separator ; return: list of strings (defun parse (s d / i l tmp rtn) (cond ((/= "" d) (setq l (strlen d)) (while (setq i (vl-string-search d s)) (setq tmp (substr s 1 i)) (setq rtn (cons tmp rtn)) (setq s (substr s (1+ (+ l (strlen tmp))) (strlen s))) ) (vl-remove "" (reverse (cons s rtn))) ) (t s) ) )