AutoLISP read and write from file line by line
Posted: Tue Oct 11, 2011 9:47 pm
Hi all,
I am rather new to AutoLISP and I'm still learning how to properly program it. I am running a AutoLISP to generate a field of circular polylines with position x,y and radius R read from a (user-selected) file.
At the moment I read the file completely before I start generating the polylines, but this causes me to run into the error "internal stack limit reached (simulated)". If I understood correctly, this means that I try to have to much in the memory at the same time, right?!
What I would like to do to get rid of this problem is to read the file line-by-line and write a polyline for each line immediately so that I only have to keep single lines in stack.
Can somebody help me/give me some pointers on how to adapt the code below to do this?!
Thanks!
I am rather new to AutoLISP and I'm still learning how to properly program it. I am running a AutoLISP to generate a field of circular polylines with position x,y and radius R read from a (user-selected) file.
At the moment I read the file completely before I start generating the polylines, but this causes me to run into the error "internal stack limit reached (simulated)". If I understood correctly, this means that I try to have to much in the memory at the same time, right?!
What I would like to do to get rid of this problem is to read the file line-by-line and write a polyline for each line immediately so that I only have to keep single lines in stack.
Can somebody help me/give me some pointers on how to adapt the code below to do this?!
Thanks!
Code: Select all
;; convert string to list of strings
(defun strlist (strExp strDel / strLst)
(while (setq pos (vl-string-position (ascii strDel) strExp))
(setq itm (substr strExp 1 pos))
(setq strLst (append strLst (list itm)))
(setq strExp (substr strExp (+ pos 2)))
)
(setq strLst (append strLst (list strExp)))
)
; read file
(defun readfile (fname / line)
(if (setq line (read-line fname))
(if (eq "" line)
(readfile fname)
(cons ( strlist (vl-string-trim " " line) " ") (readfile fname))
)
)
)
;main prog
(defun C:CONN (/ adata fd fname itema)
(setq fd (getfiled "* Select \"PointA\" Data File *" (getvar "dwgprefix") "txt" 8))
(setq fname (open fd "R"))
(setq adata (readfile fname))
(close fname)
(setq adata (mapcar (function (lambda(x)(vl-string-subst " " "\t" (car x)))) adata)
adata (mapcar (function (lambda(x)(vl-string-subst " " "\t" x))) adata)
adata (mapcar (function (lambda(x)(strlist x " "))) adata)
adata (mapcar (function (lambda (x)
(mapcar 'atof
(mapcar (function (lambda (y)
(vl-string-subst "." "," y))) x))))
adata)
)
(while (setq itema (car adata))
(entmake (list
'(0 . "LWPOLYLINE")
'(100 . "AcDbEntity")
(cons 410 (getvar "CTAB"))
'(8 . "0")
'(62 . 7)
'(100 . "AcDbPolyline")
'(90 . 2)
'(70 . 1)
(cons 43 0.5)
(cons 10 (list (- (car itema)(/ (caddr itema) 2)) (cadr itema)))
'(42 . 1.0)
(cons 10 (list (+ (car itema)(/ (caddr itema) 2 )) (cadr itema)))
'(42 . 1.0)
(cons 210 (list 0.0 0.0 1.0))))
(setq adata (cdr adata))
)
(command "._zoom" "_e")
(princ)
)