Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Simple Text Parsing Using SSGET and the READ Function

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
msisler
827 Views, 7 Replies

Simple Text Parsing Using SSGET and the READ Function

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!


You CAD!
Tags (1)
7 REPLIES 7
Message 2 of 8
Shneuph
in reply to: msisler

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

 

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Message 3 of 8
msisler
in reply to: Shneuph

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!

 


You CAD!
Message 4 of 8
Jason.Piercey
in reply to: msisler

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)
  )
 )

 

Message 5 of 8
msisler
in reply to: Jason.Piercey

Almost!  It didn't seem to parse to the first space, however, because the address

 

4635-4637 N CONGRESS AVE

 

remained the same.


You CAD!
Message 6 of 8
Jason.Piercey
in reply to: msisler

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)
  )
 )

 

Message 7 of 8
Jason.Piercey
in reply to: msisler


@msisler wrote:

Almost!  It didn't seem to parse to the first space, however, because the address

 

4635-4637 N CONGRESS AVE

 

remained the same.


Are you saying in this case you only want 4635 or 4635-4637 ?

 

 

Message 8 of 8
msisler
in reply to: Jason.Piercey

Thank you!

You CAD!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

”Boost