Lisp to read user .txt file loop doesn't make sense.

3rduser
Contributor

Lisp to read user .txt file loop doesn't make sense.

3rduser
Contributor
Contributor

I'm working on a lisp to draw lines from coordinates provided in a .txt file. I have gotten it to work from selecting a start point via this code and .txt file contents:

 

3rduser_0-1717519194314.png

 

(defun c:test\ ( / path file rline1 rline2 stpoint)
	(setq path "c:\\desktop\\test.txt")
	(setq file (open path "r"))
	(setq rline1 (read-line file))
	(setq rline2 (read-line file))
	(close file)
	(setq stpoint (getpoint "SELECT START POINT > "))
	(command "_.line" stpoint rline1 rline2 "")
	(princ)
)

 

 

my question is how do I loop this? (it can be assumed coordinates will always be in the correct order) I have looked through multiple websites and forums but can't find anything that works or makes sense to me. I have added the "while" function but it breaks the code. Essentially I need it to read every line of a .txt file until there is no content left and make every line it's own individual string for lisp use.

 

 

(defun c:test\ ( / path file rline stpoint)
	(setq path "c:\\desktop\\test.txt")
	(setq file (open path "r"))
	(while (setq rline (read-line file)))
	(close file)
	(setq stpoint (getpoint "SELECT START POINT > "))
	(command "_.line" stpoint rline "")
	(princ)
)

 

 

I could do this manually with the code I have already but would become very tedious since I would need an individual lisp for every possible line situation. For example if I had one that had 16 lines of coordinates i could just use this:

 

 

(defun c:test\

	( / 
	path file stpoint rline1 rline2 rline3 rline4 rline5 rline6 rline7 rline8
	rline9 rline10 rline11 rline12 rline13 rline14 rline15 rline16
	)
	
	(setq path "c:\\desktop\\test.txt")
	(setq file (open path "r"))
	(setq rline1 (read-line file))
	(setq rline2 (read-line file))
	(setq rline3 (read-line file))
	(setq rline4 (read-line file))
	(setq rline5 (read-line file))
	(setq rline6 (read-line file))
	(setq rline7 (read-line file))
	(setq rline8 (read-line file))
	(setq rline9 (read-line file))
	(setq rline10 (read-line file))
	(setq rline11 (read-line file))
	(setq rline12 (read-line file))
	(setq rline13 (read-line file))
	(setq rline14 (read-line file))
	(setq rline15 (read-line file))
	(setq rline16 (read-line file))
	(close file)
	(setq stpoint (getpoint "SELECT START POINT > "))
	(command "_.line" stpoint rline1 rline2 rline3 rline4 rline5 rline6 rline7 rline8
	rline9 rline10 rline11 rline12 rline13 rline14 rline15 rline16 "")
	(princ)
)

 

 

but that's terrible code and I know it. I would need hundreds of files to account for every situation. The thing that worries me though is even looking at successful "while" functions it seems like it turns the entire list into a single string and that wouldn't work either for this use case.

0 Likes
Reply
Accepted solutions (1)
279 Views
3 Replies
Replies (3)

paullimapa
Mentor
Mentor
Accepted solution

You should start the Line command and then within the while loop as each point is read from the text feed that as points to continue the line command. Then when all lines have been read from the text file end the line command like this:

(defun c:test\ ( / path file rline stpoint)
	(setq path "c:\\desktop\\test.txt")
	(if(setq file (open path "r")) ; chk if successful in opening file to read
         (progn ; then do the following
  	  (setq stpoint (getpoint "SELECT START POINT > "))
          (command "_.Line" stpoint) ; start line command
  	  (while (setq rline (read-line file)) ; begin while loop reading liens from text file
  	   (command rline) ; enter each line as points
          ) ; while
 	  (close file)
	  (command "") ; end line command
         ) ; progn
        ) ; if
	(princ)
)

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

3rduser
Contributor
Contributor

@paullimapa 

 

This works great thank you! Much appreciated.

0 Likes

paullimapa
Mentor
Mentor

Glad to have helped…cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes