lisp如何将文本中的数据写到文件上
在网上找了代码改的,目的是把文本文件中的数据写到文件上,目前的代码看不太懂,只能写第一个逗号那块,并且好像list那块不对,求大侠帮改改,能大概实现附图的结果就行,可以接受选一个基点写一行数据(defun c:get( / b1 b2 b3 d dat_file ff n path1 txt1 x y)
; 用于lsp与txt之间数据传输
(setq path1 (getvar "dwgprefix"))
(setq ff (open (strcat path1 "样表" ".txt" ) "w")) ;写文件,如不存在则新建文件,如存在新资料将覆盖旧资料。
(write-line "C1215,1200,1500,20,左10右10,2层" ff)
(write-line "C1515,1500,1500,16,左8右8,4层" ff)
(write-line "C1815,1800,1500,22,左11右11,4层" ff)
(write-line "C2118,2100,1800,14,左7右7,12层" ff)
(write-line "C0921,900,2100,6,左6,4层" ff)
(close ff)
(setq dat_file (getfiled "选择txt的文件或样表" path1 "txt" 2))
(setq n 1 b1 '() b2 '() b3 '() ff (open dat_file "r"))
(while (setq txt1 (read-line ff))
(setq b1 (parse txt1 ","))
(setq d (nth 0 b1) x (atof (nth 1 b1)) y (atof (nth 2 b1)) )
(setq b2 (list n d x y) b3 (cons b2 b3) n (1+ n) )
)
(setq b3 (reverse b3))
(setq d (nth 1 (nth 2 b3)) )
(setq p1 (getpoint "\n请指定插入点:"))
(command "text"p1 100 0 d "")
(print b3) (print "第3行第1个数值:")(princ d)
(princ)
)
(defun parse (str delim / lst pos)
;明经Gu-xl编写,将字符串按照区别符delim分成表str
(setq pos (vl-string-search delim str))
(while pos
(setq lst (cons (substr str 1 pos) lst)
str (substr str (+ pos 2))
pos (vl-string-search delim str)
)
)
(if (> (strlen str) 0)
(setq lst (cons str lst))
)
(if (= " " delim) (setq lst (vl-remove "" lst)))
(reverse lst)
)
;测试
; (setq s (list 'a 'b 'c))
(defun c:get( / b1 b2 b3 d dat_file ff n path1 txt1 x y
CH FX I LC OLD_CMD P1 P2 P3 P4 P5 P6 PT SL STR STR_LST )
; 用于lsp与txt之间数据传输
(setq path1 (getvar "dwgprefix"))
(setq ff (open (strcat path1 "样表" ".txt" ) "w")) ;写文件,如不存在则新建文件,如存在新资料将覆盖旧资料。
(write-line "C1215,1200,1500,20,左10右10,2层" ff)
(write-line "C1515,1500,1500,16,左8右8,4层" ff)
(write-line "C1815,1800,1500,22,左11右11,4层" ff)
(write-line "C2118,2100,1800,14,左7右7,12层" ff)
(write-line "C0921,900,2100,6,左6,4层" ff)
(close ff)
(setq dat_file (getfiled "选择txt的文件或样表" path1 "txt" 2))
(setq n 1 b1 '() b2 '() b3 '() ff (open dat_file "r"))
(setq str_lst '())
(while (setq txt1 (read-line ff))
(setq b1 (parse txt1 ","))
(setq str_lst(cons b1 str_lst))
;(setq d (nth 0 b1) x (atof (nth 1 b1)) y (atof (nth 2 b1)) )
;(setq b2 (list n d x y) b3 (cons b2 b3) n (1+ n) )
)
(setq str_lst (reverse str_lst))
(setq pt(getpoint "\n请指定插入点:") i 0)
(if (and pt str_lst)
(progn
(setq old_cmd(getvar "cmdecho"))
(setvar "cmdecho" 0)
(command "_.undo" "be")
(repeat (length str_lst)
(setq str(nth i str_lst))
(setq p1(list(+(car pt) 4 )(- (cadr pt) 5))
p2(list(+(car pt) 40 )(- (cadr pt) 5))
p3(list(+(car pt) 54 )(- (cadr pt) 5))
p4(list(+(car pt) 4 )(- (cadr pt) 13))
p5(list(+(car pt) 254 )(- (cadr pt) 13))
p6(list(+(car pt) 138 )(- (cadr pt) 5))
)
(setq ch(car str)
x (cadr str)
y (caddr str)
sl (cadddr str)
fx(nth 4 str)
lc(nth 5 str))
(command "_.text" "_non" p1 3 0 ch nil)
(command "_.text" "_non" p2 3 0 x nil)
(command "_.text" "_non" p3 3 0 y nil)
(command "_.text" "_non" p4 3 0 sl nil)
(command "_.text" "_non" p5 3 0 fx nil)
(command "_.text" "_non" p6 3 0 lc nil)
(setq i(1+ i)
pt(list(+(car pt) 0 )(- (cadr pt) 23))
)
)
(command "_.undo" "e")
(setvar "cmdecho" old_cmd)
)
)
;;; (setq b3 (reverse b3))
;;; (setq d (nth 1 (nth 2 b3)) )
;;; (setq p1 (getpoint "\n请指定插入点:"))
;;; (command "text"p1 100 0 d "")
;;; (print b3) (print "第3行第1个数值:")(princ d)
(princ)
)
(defun parse (str delim / lst pos)
;明经Gu-xl编写,将字符串按照区别符delim分成表str
(setq pos (vl-string-search delim str))
(while pos
(setq lst (cons (substr str 1 pos) lst)
str (substr str (+ pos 2))
pos (vl-string-search delim str)
)
)
(if (> (strlen str) 0)
(setq lst (cons str lst))
)
(if (= " " delim) (setq lst (vl-remove "" lst)))
(reverse lst)
)
;测试
; (setq s (list 'a 'b 'c))
edata 发表于 2014-1-8 23:07 static/image/common/back.gif
大侠你太厉害了,解决了我老大的问题了,太感谢了
页:
[1]