See the top rated post in this thread. Click here

Results 1 to 8 of 8

Thread: read-line loop column text file

  1. #1
    Member
    Join Date
    2006-05
    Posts
    13
    Login to Give a bone
    0

    Cool read-line loop column text file

    I would be most grateful if anyone can suggest what to change so that the data from each line is read and the circles drawn according data provide on each line.

    I am trying to create a lisp file to read the contents from a sample text file.
    COLUMNS.TXT

    COLUMN C1,8,STEEL,%
    COLUMN C2,10,STEEL,%
    COLUMN C3,11,CONC,%
    COLUMN C4,12,CONC,%
    COLUMN C5,13,CONC,%
    COLUMN C6,14,CONC,%
    z

    Last character in the file is z even though nil could be used to recognize the end of the file.

    My lisp program thus far draws the circles according the data read in the first line.

    filelist.lsp

    Code:
    (defun c:flst ()
      (setq cecho (getvar "cmdecho"))
      (setvar "cmdecho" 0)
      (command "units" "2" "256" "8" "2" "0" "0" "N")
      (setvar "angdir" 0)
      (setvar "angbase" 0)
      (setq fileL "")
      (setq fileLlen "")
      (setq char "")
      (setq columnvalu "")
      (setq columncount 0)
      (setq columnnum "")
      (setq chrloop 0)
      (setq column "")
      (setq c-type "")
      (setq dia 0)
      (setq nxtstpt 16)
      (setq clay (getvar "clayer"))
      (setq angd (getvar "angdir"))
      (setq angb (getvar "angbase"))
      (setq orgos (getvar "osmode"))
      (setq startpt (getpoint "\n Pick the start point... "))
      (setq file (open "c:\\columns.txt" "r"))
     ;(print "start")
      (while (/= fileL "z")
    	(progn
    	  (setq fileL (substr (read-line file) 1 200))
     ;(print fileL)
     ;(print "testx")
    	  (while (/= char "%")
    	(progn
    	  (setq chrloop (+ chrloop 1))
    	  (setq fileLlen (strlen fileL))
    	  (setq char (substr fileL chrloop 1))
     ;(print char)
    	  (if (= char ",")
    		(progn
    		  (setq columnnum columnvalu)
    		  (setq columnvalu "")
    		  (setq columncount (+ columncount 1))
    		)
    	  )
    	  (if (/= char ",")
    		(progn
    		  (setq columnvalu (strcat columnvalu char))
    		)
    	  )
    	)
    	(progn
    	  (if (= columncount 0)
    		(progn
    		  (setq column columnvalu)
     ;(print column)
    		)
    	  )
    	  (if (= columncount 1)
    		(progn
    		  (setq dia columnvalu)
     ;(print dia)
    		)
    	  )
    	  (if (= columncount 2)
    		(progn
    		  (setq c-type columnvalu)
     ;(print c-type)
    		)
    	  )
    	)
    	  )
    	  (if (/= fileL "nil")
    	(progn
     ;(print fileL)
     ;text file sample - COLUMN C1,8,STEEL,%
    	  (setq diaval (atof dia)) ;Diameter (d)
    	  (setq rcol (/ diaval 2)) ;calc column radius
    	  (setq zf (* 2.5 diaval)) ;calc zoom factor
     ;(print "test1") ;error check
    	  (setvar "angdir" 0)
    	  (setvar "angbase" 0)
    	  (command "osmode" "1") ; SET OSNAP TO END POINT
     ;(print "test2") ;error check
    	  (setq xptc (car startpt)) ; get x coord
    	  (setq xptr (rtos xptc 2 2)) ; convert to string
    	  (setq yptc (cdr startpt)) ; get y coord
    	  (setq ypt (car yptc))
    	  (setq yptr (rtos ypt 2 2)) ; convert to string
     ;(print "test3") ;error check
    	  (command "zoom" "c" startpt zf)
    	  (command "osmode" "0")
    	  (command "circle" startpt dia "")
    	  (command "text" startpt "1" "0" column "" "")
     ;(print "test4") ;error check
    	  (setq nxtstptx (+ nxtstpt xptc))
    	  (setq txstpt (list nxtstptx ypt 0))
    	  (setq nxtstpty (+ nxtstpt ypt))
    	  (setq startpt (list xptc nxtstpty 0))
     ;(print "test5") ;error check
    	)
    	  )
    	)
      )
     ;(print column)
     ;(princ "\ncolumn")
     ;(print c-type)
     ;(princ "\nc-type")
     ;(print d)
     ;(princ "\nd")
     ;(print col)
     ;(princ "\ncol")
      (setvar "clayer" clay)
      (setvar "angdir" angd)
      (setvar "osmode" orgos)
      (setvar "angbase" angb)
      (setvar "cmdecho" cecho)
      (command "units" "2" "256" "8" "2" "0" "0" "N")
      (command "graphscr")
    ) ; end of file
    [ Moderator Action = ON ] What are [ CODE ] tags... [ Moderator Action = OFF ]
    Last edited by Mike.Perry; 2006-05-12 at 04:04 AM. Reason: [CODE] tags added.

  2. #2
    Member
    Join Date
    2005-01
    Posts
    8
    Login to Give a bone
    1

    Default Re: read-line loop column text file

    Give this a try. Not sure what you are trying to achieve but this shows the new circles and text.

    Code:
    (defun c:flst ()
     ;(setq cecho (getvar "cmdecho"))
     ;(setvar "cmdecho" 0)
      (command "units" "2" "256" "8" "2" "0" "0" "N")
      (setvar "angdir" 0)
      (setvar "angbase" 0)
      (setq fileL "")
      (setq fileLlen "")
      (setq char "")
      (setq columnvalu "")
      (setq columncount 0)
      (setq columnnum "")
      (setq chrloop 0)
      (setq column "")
      (setq c-type "")
      (setq dia 0)
      (setq nxtstpt 16)
      (setq clay (getvar "clayer"))
      (setq angd (getvar "angdir"))
      (setq angb (getvar "angbase"))
      (setq orgos (getvar "osmode"))
      (setq startpt (getpoint "\n Pick the start point... "))
      (setq file (open "c:\\columns.txt" "r"))
      (setq fileL (read-line file))
     ;(print "start")
      (while (/= fileL "z")
     ;(setq fileL (substr (read-line file) 1 200))
    	(setq char "")
    	(setq chrloop 0)
    	(setq column "")
    	(setq columnvalu "")
    	(setq columncount 0)
     ;(print fileL)
     ;(print "testx")
    	(while (/= char "%")
     ; (progn
    	  (setq chrloop (+ chrloop 1))
    	  (setq fileLlen (strlen fileL))
    	  (setq char (substr fileL chrloop 1))
     ;(print char)
    	  (if (= char ",")
    	(progn
    	  (setq columnnum columnvalu)
    	  (setq columnvalu "")
    	  (setq columncount (+ columncount 1))
    	)
    	  )
    	  (if (/= char ",")
    	(progn
    	  (setq columnvalu (strcat columnvalu char))
    	)
    	  )
     ; )
    
     ; (progn
    	  (if (= columncount 0)
    	(progn
    	  (setq column columnvalu)
     ;(print column)
    	)
    	  )
    	  (if (= columncount 1)
    	(progn
    	  (setq dia columnvalu)
     ;(print dia)
    	)
    	  )
    	  (if (= columncount 2)
    	(progn
    	  (setq c-type columnvalu)
     ;(print c-type)
    	)
    	  )
     ; )
    	) ;_while /= %
    
     ; (if (/= fileL "nil")
     ; (progn
     ;(print fileL)
     ;text file sample - COLUMN C1,8,STEEL,%
    	(setq diaval (atof dia)) ;Diameter (d)
    	(setq rcol (/ diaval 2)) ;calc column radius
    	(setq zf (* 2.5 diaval)) ;calc zoom factor
     ;(print "test1") ;error check
    	(setvar "angdir" 0)
    	(setvar "angbase" 0)
    	(command "osmode" "1") ; SET OSNAP TO END POINT
     ;(print "test2") ;error check
    	(setq xptc (car startpt)) ; get x coord
    	(setq xptr (rtos xptc 2 2)) ; convert to string
    	(setq yptc (cdr startpt)) ; get y coord
    	(setq ypt (car yptc))
    	(setq yptr (rtos ypt 2 2)) ; convert to string
     ;(print "test3") ;error check
    	(command "zoom" "c" startpt zf)
    	(command "osmode" "0")
    	(command "circle" startpt dia)
    	(command "text" startpt "1" "0" column)
     ;(print "test4") ;error check
    	(setq nxtstptx (+ nxtstpt xptc))
    	(setq txstpt (list nxtstptx ypt 0))
    	(setq nxtstpty (+ nxtstpt ypt))
    	(setq startpt (list xptc nxtstpty 0))
     ;(print "test5") ;error check
     ; )
     ; )
    	(setq fileL (read-line file))
      ) ;_while /= z
      (setq file (close file))
     ;(print column)
     ;(princ "\ncolumn")
     ;(print c-type)
     ;(princ "\nc-type")
     ;(print d)
     ;(princ "\nd")
     ;(print col)
     ;(princ "\ncol")
      (setvar "clayer" clay)
      (setvar "angdir" angd)
      (setvar "osmode" orgos)
      (setvar "angbase" angb)
     ;(setvar "cmdecho" cecho)
     ;(command "units" "2" "256" "8" "2" "0" "0" "N")
     ;(command "graphscr")
    )
    [ Moderator Action = ON ] What are [ CODE ] tags... [ Moderator Action = OFF ]
    Last edited by Mike.Perry; 2006-05-12 at 04:07 AM. Reason: [CODE] tags added.

  3. #3
    Member
    Join Date
    2006-05
    Posts
    13
    Login to Give a bone
    0

    Smile Re: read-line loop column text file

    Gerhardt.Nortje
    Thanks for the help! I'm glad you got it to work.
    That certainly does the job!
    I'm making a table of columns, there's going to be more stuff added.

    I'm sure there are easier ways of making the table.
    But this exercise was more of a learning how to read text values from a file.

    I changed my approach to the following:
    (setq filelist (read (strcat "(" (read-line cfile) ")")))

    (setq column (nth 0 filelist))

    Using the method above took much less time to trouble shoot and my little lisp file works now.
    This way there is less while and if statements to get stuck in.

    Thanks again...

  4. #4
    All AUGI, all the time CAB2k's Avatar
    Join Date
    2016-01
    Location
    Brandon, Florida
    Posts
    687
    Login to Give a bone
    0

    Default Re: read-line loop column text file

    Threw this together, did not test, but should get you started.
    Read the help file on the (polar pt ang dir) function
    Code:
    (defun c:flst (/ angb angd cecho clay datalist file filename orgos startpt x)
    
      ;;  save enviornment
      (setq clay  (getvar "clayer")
            angd  (getvar "angdir")
            angb  (getvar "angbase")
            orgos (getvar "osmode")
            cecho (getvar "cmdecho")
      )
    
      ;; set enviornment
      (setvar "cmdecho" 0)
      (command "units" "2" "256" "8" "2" "0" "0" "N")
      (setvar "angdir" 0)
      (setvar "angbase" 0)
    
    
      (setq filename "c:\\columns.txt")
    
      ;;  must have items before the circles can be created
      (if (and (setq startpt (getpoint "\n Pick the start point... "))
               (setq filename (findfile filename))
               (setq file (open filename "r"))
               (setq datalist (read_file file))
               (setq datalist (mapcar '(lambda (x) (sparser x ",")) datalist))
          )
        ;;  datalist at this point is
        ;;  (("C1" "8" "STEEL")("C2" "10" "STEEL") (.....))
        (foreach itm datalist
          ;;  not nil, is a list, & length of 3 or more
          (if (and itm (listp itm) (> (length itm) 2))
            (progn ; ready to go
              ;;  more error testing could be needed for each item
              ;;  add circle, override osmode
              (command "circle" "non" startpt (atof (cadr itm)) "")
              ;;  add text, you should test for text height when using command
              ;;  as the order of entry changes if not zero height
              (command "text" "non" startpt "1" "0" (caddr itm) "")
              ;;  reset the start point, look up the polar function
              (setq startpt (polar startpt 0.0 (* (atof (cadr itm)) 2.0)))
            )
          )
        )
      )
    
    
      ;;  restore enviornment
      (setvar "clayer" clay)
      (setvar "angdir" angd)
      (setvar "osmode" orgos)
      (setvar "angbase" angb)
      (setvar "cmdecho" cecho)
      (command "units" "2" "256" "8" "2" "0" "0" "N")
      (command "graphscr")
    
      (princ) ; exit quietly
    ) ; end of file
    
    
    
    
    ;;  text file structure
    ;|
    Not sure what you are trying to do here ???????????????
    ?? , diameter, text
    C1,8,STEEL
    C2,10,STEEL
    C3,11,CONC
    C4,12,CONC
    C5,13,CONC
    C6,14,CONC
        |;
    
    
    ;;  phraser by CAB  
    (defun sparser (str delim / ptr lst)
      (while (setq ptr (vl-string-search delim str))
        (setq lst (cons (substr str 1 ptr) lst))
        (setq str (substr str (+ ptr 2)))
      )
      (reverse (cons str lst))
    )
    
    ;;  read file, return list for each line in file
    (defun read_file (fn / stream result)
      (while (setq stream (read-line fn))
        (setq result (cons stream result))
      )
      (close fn)
      (reverse result)
    )

  5. #5
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: read-line loop column text file

    Here are two utilities from my toolbox

    The syntax importing a csv file into a routine (without the z end of file you have)

    (setq lstOfSublist (csvfiletolist (findfile "COLUMNS.CSV") ","))



    Code:
    ; Import a CSV File to a list of Sublists
    (defun csvFiletoList (strFilename strChar / lstOfSublists strText z)
     (setq z (open strFilename "r"))
     (while (setq strText (read-line z))
      (setq lstOfSublists (cons (CSVStringToList strText strChar) lstOfSublists))
     )
     (close z)
     (reverse lstOfSublists)
    )
    ; Parsing a textstring to a list.
    (defun CSVStringToList  (strText strChar / intPosition lstStrings)
     (while (setq intPosition (vl-string-search strChar strText 0))
      (setq lstStrings  (cons (substr strText 1 intPosition) lstStrings)
    		strText	 (substr strText (+ intPosition 1 (strlen strChar)))
      )
     )
     (reverse (cons strText lstStrings))
    )

  6. #6
    All AUGI, all the time CAB2k's Avatar
    Join Date
    2016-01
    Location
    Brandon, Florida
    Posts
    687
    Login to Give a bone
    0

    Default Re: read-line loop column text file

    dukwing16
    After reading your 2nd post i revised the routine to this.
    Code:
    (defun c:flst (/ angb angd cecho clay datalist file filename orgos startpt x)
    
      ;;  save enviornment
      (setq clay  (getvar "clayer")
            angd  (getvar "angdir")
            angb  (getvar "angbase")
            orgos (getvar "osmode")
            cecho (getvar "cmdecho")
      )
    
      ;; set enviornment
      (setvar "cmdecho" 0)
      ;|
      (command "units" "2" "256" "8" "2" "0" "0" "N")
      (setvar "angdir" 0)
      (setvar "angbase" 0)
    |;
    
      (setq filename "c:\\columns.txt")
    
      ;;  must have items before the circles can be created
      (if (and (setq startpt (getpoint "\n Pick the start point... "))
               (setq filename (findfile filename))
               (setq file (open filename "r"))
               (setq datalist (read_file file))
               (setq datalist (mapcar '(lambda (x) (sparser x ",")) datalist))
          )
        ;;  datalist at this point is (mark diameter type)
        ;;  (("C1" "8" "STEEL")("C2" "10" "STEEL") (.....))
        (foreach itm datalist
          ;;  not nil, is a list, & length of 3 or more
          (if (and itm (listp itm) (> (length itm) 2))
            (progn ; ready to go
              ;;  more error testing could be needed for each item
              ;;  add circle, override osmode
              (command "circle" "non" startpt (/ (setq dia (atof (cadr itm))) 2.0) "")
              ;;  add text, you should test for text height when using command
              ;;  as the order of entry changes if not zero height
              (command "text" "J" "MC" "non" startpt "1" "0" (car itm) "")
              (command "text" "J" "ML" "non" (polar startpt 0 20)  "1" "0" (caddr itm) "")
              ;;  reset the start point, look up the polar function
              (setq startpt (polar startpt (* pi 1.5) 18))
            )
          )
        )
      )
    
    
      ;;  restore enviornment
      (setvar "clayer" clay)
      (setvar "angdir" angd)
      (setvar "osmode" orgos)
      (setvar "angbase" angb)
      (setvar "cmdecho" cecho)
      ; (command "units" "2" "256" "8" "2" "0" "0" "N")
      ; (command "graphscr")
    
      (princ) ; exit quietly
    ) ; end of file
    
    
    
    
    ;;  text file structure
    ;|
    Not sure what you are trying to do here ???????????????
    ?? , diameter, text
    C1,8,STEEL
    C2,10,STEEL
    C3,11,CONC
    C4,12,CONC
    C5,13,CONC
    C6,14,CONC
        |;
    
    
    ;;  phraser by CAB  
    (defun sparser (str delim / ptr lst)
      (while (setq ptr (vl-string-search delim str))
        (setq lst (cons (substr str 1 ptr) lst))
        (setq str (substr str (+ ptr 2)))
      )
      (reverse (cons str lst))
    )
    
    ;;  read file, return list for each line in file
    (defun read_file (fn / stream result)
      (while (setq stream (read-line fn))
        (setq result (cons stream result))
      )
      (close fn)
      (reverse result)
    )

  7. #7
    Member
    Join Date
    2005-01
    Posts
    8
    Login to Give a bone
    0

    Default Re: read-line loop column text file

    Hi Dukwing

    I am happy to have been of help.

    If I can suggest to you to use the following site to learn about lisp.

    http://www.afralisp.net/

    I'm sure you will find it both informative and entertaining.


  8. #8
    Member
    Join Date
    2006-05
    Posts
    13
    Login to Give a bone
    0

    Default Re: read-line loop column text file

    Gerhardt,
    Ya mon! That's a great looking site! Thanks!

    Duk

Similar Threads

  1. Replies: 4
    Last Post: 2015-02-26, 11:46 AM
  2. Read File from text file to restore settings
    By dilek in forum AutoCAD Customization
    Replies: 1
    Last Post: 2013-04-22, 12:47 PM
  3. open and read text file
    By bandent in forum AutoLISP
    Replies: 5
    Last Post: 2010-04-15, 08:46 PM
  4. Read / Write To Text File
    By caddog71 in forum VBA/COM Interop
    Replies: 2
    Last Post: 2008-05-27, 06:40 PM
  5. read-line from txt file
    By clovis in forum AutoLISP
    Replies: 4
    Last Post: 2008-04-07, 01:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •