明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1094|回复: 2

[已解答] lisp如何将文本中的数据写到文件上

[复制链接]
发表于 2014-1-8 21:47 | 显示全部楼层 |阅读模式
在网上找了代码改的,目的是把文本文件中的数据写到文件上,目前的代码看不太懂,只能写第一个逗号那块,并且好像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))



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2014-1-8 23:07 | 显示全部楼层
  1. (defun c:get( / b1 b2 b3 d dat_file ff n path1 txt1 x y
  2.        CH FX I LC OLD_CMD P1 P2 P3 P4 P5 P6 PT SL STR STR_LST )
  3. ; 用于lsp与txt之间数据传输
  4.   (setq path1 (getvar "dwgprefix"))
  5.   (setq ff (open (strcat path1 "样表" ".txt" ) "w")) ;写文件,如不存在则新建文件,如存在新资料将覆盖旧资料。
  6.   (write-line "C1215,1200,1500,20,左10右10,2层" ff)
  7.   (write-line "C1515,1500,1500,16,左8右8,4层" ff)
  8.   (write-line "C1815,1800,1500,22,左11右11,4层" ff)
  9.   (write-line "C2118,2100,1800,14,左7右7,12层" ff)
  10.   (write-line "C0921,900,2100,6,左6,4层" ff)
  11.   (close ff)
  12.   (setq dat_file (getfiled "选择txt的文件或样表" path1 "txt" 2))
  13.   (setq n 1 b1 '() b2 '() b3 '() ff (open dat_file "r"))
  14.   (setq str_lst '())
  15.   (while (setq txt1 (read-line ff))
  16.       (setq b1 (parse txt1 ","))
  17.     (setq str_lst(cons b1 str_lst))
  18.       ;(setq d (nth 0 b1) x (atof (nth 1 b1)) y (atof (nth 2 b1)) )
  19.       ;(setq b2 (list n d x y) b3 (cons b2 b3) n (1+ n) )

  20.    )
  21.   (setq str_lst (reverse str_lst))

  22. (setq pt(getpoint "\n请指定插入点:") i 0)
  23.     (if (and pt str_lst)
  24.       (progn
  25.   (setq old_cmd(getvar "cmdecho"))
  26.   (setvar "cmdecho" 0)
  27.   (command "_.undo" "be")
  28.   (repeat (length str_lst)
  29.     (setq str(nth i str_lst))
  30.     (setq p1(list(+(car pt) 4 )(- (cadr pt) 5))
  31.     p2(list(+(car pt) 40 )(- (cadr pt) 5))
  32.     p3(list(+(car pt) 54 )(- (cadr pt) 5))
  33.     p4(list(+(car pt) 4 )(- (cadr pt) 13))
  34.     p5(list(+(car pt) 254 )(- (cadr pt) 13))
  35.     p6(list(+(car pt) 138 )(- (cadr pt) 5))
  36.     )
  37.     (setq ch(car str)
  38.     x (cadr str)
  39.     y (caddr str)
  40.     sl (cadddr str)
  41.     fx(nth 4 str)
  42.     lc(nth 5 str))
  43.     (command "_.text" "_non" p1 3 0 ch nil)
  44.     (command "_.text" "_non" p2 3 0 x nil)
  45.     (command "_.text" "_non" p3 3 0 y nil)
  46.     (command "_.text" "_non" p4 3 0 sl nil)
  47.     (command "_.text" "_non" p5 3 0 fx nil)
  48.     (command "_.text" "_non" p6 3 0 lc nil)
  49.     (setq i(1+ i)
  50.     pt(list(+(car pt) 0 )(- (cadr pt) 23))
  51.     )
  52.     )
  53.   (command "_.undo" "e")
  54.   (setvar "cmdecho" old_cmd)
  55. )
  56.       )
  57. ;;;   (setq b3 (reverse b3))
  58. ;;;   (setq d (nth 1 (nth 2 b3)) )
  59. ;;;    (setq p1 (getpoint "\n请指定插入点:"))
  60. ;;;    (command "text"  p1 100 0 d "")
  61. ;;;   (print b3) (print "第3行第1个数值:")(princ d)
  62.    (princ)
  63.    )

  64.   (defun parse (str delim / lst pos)
  65.   ;明经Gu-xl编写,将字符串按照区别符delim分成表str
  66.       (setq pos (vl-string-search delim str))
  67.       (while pos
  68.         (setq lst (cons (substr str 1 pos) lst)
  69.               str (substr str (+ pos 2))
  70.              pos (vl-string-search delim str)
  71.         )
  72.       )
  73.       (if (> (strlen str) 0)
  74.         (setq lst (cons str lst))
  75.       )
  76.       (if (= " " delim) (setq lst (vl-remove "" lst)))
  77.       (reverse lst)
  78.     )
  79.     ;测试
  80. ;    (setq s (list 'a 'b 'c))

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 2014-1-9 21:05 | 显示全部楼层
edata 发表于 2014-1-8 23:07

大侠你太厉害了,解决了我老大的问题了,太感谢了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-20 03:10 , Processed in 0.211582 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表