乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
楼主: terra1982

[编程交流] LISP程序读取csv文件

[复制链接]

1

主题

116

帖子

115

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 00:24:14 | 显示全部楼层
我不会做出任何承诺,当然也不会在短期内做出承诺,但如果你愿意的话,我会详细说明理想的结果是什么。你说的是积木。你说的是插入块和文本,还是相反?或者更进一步,你是说插入一个带有属性的块,属性的值来自电子表格?你可以用这个做很多事情。我不能说是Intellicad,但至少在AutoCAD中,如果能获得最佳结果,您可以将数据与字段相关联。。。。
 
如果你愿意的话,考虑一下你的理想结果并加以描述。如果我真的有时间去做的话,我不愿意创造出妥协的东西,然后不得不改变它。
回复

使用道具 举报

1

主题

7

帖子

6

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 00:27:22 | 显示全部楼层
你好
 
该文件将简单如下:
 
 
布局、图层、样式、X线、Y线、文字、,
22X34 LAND,INV BOLD,HAND,31,1.6,CATHY ENGEL,
22X34 LAND,INV BOLD,HAND,31,1.4,LOT 3 KEYES ROAD,
22X34 LAND,INV BOLD,HAND,31,1.2,WARREN MA,
2012年7月31日,1日,18日,手部,INV BOLD,22X34土地,
 
忽略文件的第一行。(标题信息)
 
然后,将文字“CATHY ENGEL”放置在22X34横向布局表的INV BOLD图层上,文字样式手放在x线31处,y线1.6处。这会将该文本放置在我的绘图(标题栏)的右下角。
 
然后对csv的其余部分执行此操作。
 
也许要画一个块,我可以用文字块替换文字样式,用块名替换文字值。
 
 
类似于:
 
22X34土地,INV BOLD,街区,31,1.6,化粪池。图纸,
 
因此,不要放置文本,而是放置一个名为化粪池的块。图纸将在31.1.6处插入
 
这些块都可以存储在同一个文件夹中以简化它。
 
这就是目标,只需将文本或块放置在指定的位置。
 
我有点精通excel,所以什么样的格式最适合csv,我可以做到。
 
谢谢
 
做记号
回复

使用道具 举报

1

主题

116

帖子

115

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 00:29:49 | 显示全部楼层
你好,马克,
 
根据你所说的你需要的,这将是我的第一次跑步。正如我所说,我不知道LISP在Intellicad中的应用是否有任何限制。
 
您也没有提到任何关于文本高度的内容,所以我现在使用当前textsize变量的值来实现它。文本为多行文字,默认为背景遮罩。
 
这是一个开始。
 
  1. (defun C:ImpDes ( /
  2.                 ;;All Functions declared locally
  3.                 ImpDes:err ImpDes:sav ImpDes:res
  4.                 parsestring layouts layers
  5.                 ;;Saved variables for restore
  6.                 ocmd olderr
  7.                 ;;Local Variables
  8.                 data file fileline objad lay
  9.                 )
  10. (vl-load-com)
  11. (setq objad (vla-get-activedocument (vlax-get-acad-object)))
  12. ;*******************************************************************
  13. ; Error Handling, Save and Restore
  14. ;*******************************************************************
  15. (defun ImpDes:err (s / )
  16.    (if (/= s "Function cancelled")
  17.      (if (= s "quit / exit abort")
  18.        (princ)
  19.        (princ (strcat "\nError < " s " >"))
  20.        );if
  21.      );if
  22.    (ImpDes:res);restore environment
  23.    (princ)
  24.    )
  25. ;---------------------------------------------------------
  26. (defun ImpDes:sav ( / )
  27.    (vla-StartUndoMark objad)
  28.    (setq ocmd (getvar "cmdecho")
  29.          olderr *error*
  30.          *error* ImpDes:err
  31.          )
  32.    (setvar "cmdecho" 0)
  33.    )
  34. ;---------------------------------------------------------
  35. (defun ImpDes:res ( / )
  36.    (setvar "cmdecho" ocmd)
  37.    (setq *error* olderr); restore old *error* handler
  38.    (gc);garbage collection
  39.    (vla-EndUndoMark objad)
  40.    (princ)
  41.    )
  42. ;;******************************************************************
  43. ;; Local Functions
  44. ;;******************************************************************
  45. (defun parsestring ( str / res found previous)
  46.    (setq str (vl-string-right-trim "," str)
  47.          found (vl-string-position 44 str 0)
  48.          previous -1
  49.          )
  50.    (while (and found (< found (strlen str)))
  51.      (setq res (cons (substr str (+ 2 previous) (- found previous 1)) res)
  52.            previous found
  53.            found (vl-string-position 44 str (1+ previous))
  54.            )
  55.      )
  56.    (setq res (cons (substr str (+ 2 previous)) res))
  57.    (reverse res)
  58.    )
  59. ;;******************************************************************
  60. (defun layouts ( layout )
  61.    (if (and (not (member layout (layoutlist)))
  62.             (/= layout "Model")
  63.             )
  64.      (vla-add (vla-get-layouts objad) layout)
  65.      )
  66.    )
  67. ;;******************************************************************
  68. (defun layers ( lay / objlay actlyr )
  69.    (if (null (tblsearch "LAYER" lay))
  70.      (vla-add (vla-get-layers objad) lay)
  71.      (progn
  72.        (setq objlay (vla-item (vla-get-layers objad) lay)
  73.              actlyr (vla-get-name (vla-get-activelayer objad))
  74.              )
  75.        (vla-put-layeron objlay :vlax-true)
  76.        (vla-put-lock objlay :vlax-false)
  77.        (if (/= lay actlyr)
  78.          (vla-put-freeze objlay :vlax-false)
  79.          )
  80.        )
  81.      );if
  82.    )
  83. ;;******************************************************************
  84. ;; Main Program Code
  85. ;;******************************************************************
  86. (ImpDes:sav);Save variables for error handling
  87. (if (and (setq file (getfiled "Design File To Import" "" "csv" 4))
  88.           (setq file (open file "r"))
  89.           )
  90.    (progn
  91.      (read-line file) ;ignore header info
  92.      (while (setq fileline (read-line file)) ;read whole file
  93.        (if (/= fileline "")
  94.          (setq data (append data (list (parsestring fileline)))) ;store data
  95.          )
  96.        )
  97.      (close file)
  98.      (foreach line data
  99.        (if (and (>= (length line) 6)
  100.                 (apply 'and
  101.                        (mapcar
  102.                          (function
  103.                            (lambda ( x )
  104.                              (> (strlen x) 0)
  105.                              )
  106.                            )
  107.                          line
  108.                          )
  109.                        )
  110.                 );and
  111.          (progn
  112.            (layouts (car line))
  113.            (layers (cadr line))
  114.            (if (= (strcase (caddr line)) "BLOCK")
  115.              (if (tblsearch "BLOCK" (nth 5 line))
  116.                ;LAYOUT,LAYER,BLOCK,X-CORD,Y-CORD,BLOCKNAME
  117.                (entmake (list '(0 . "INSERT")
  118.                               (cons 410 (car line)) ;layout
  119.                               (cons 8 (cadr line)) ;layer
  120.                               (cons 2 (nth 5 line)) ;blockname
  121.                               (cons 10 (list (atof (nth 3 line)) (atof (nth 4 line)) 0)) ;insert point
  122.                               )
  123.                         )
  124.                (princ "\nBlockname does not exist in the drawing.")
  125.                )
  126.              ;LAYOUT,LAYER,TEXTSTYLE,X-CORD,Y-CORD,TEXTSTRING
  127.              (entmake (list '(0 . "MTEXT")
  128.                             '(100 . "AcDbEntity")
  129.                             (cons 410 (car line)) ;layout
  130.                             (cons 8 (cadr line)) ;layer
  131.                             '(100 . "AcDbMText")
  132.                             (cons 10 (list (atof (nth 3 line)) (atof (nth 4 line)) 0)) ;insert point
  133.                             [color=red](cons 40 (getvar "TEXTSIZE")) ;is textheight needed?[/color]
  134.                             (cons 1 (nth 5 line)) ;textstring
  135.                             (cons 7 (if (tblsearch "STYLE" (caddr line)) (caddr line) (getvar "TEXTSTYLE"))) ;textstyle
  136.                             '(90 . 3)
  137.                             '(63 . 256)
  138.                             '(45 . 1.15)
  139.                             )
  140.                       )
  141.              );if
  142.            );progn
  143.          );if
  144.        );foreach
  145.      );progn
  146.    );if
  147. (ImpDes:res);Restore variables for error handling
  148. );defun
  149. (Prompt "\nTHIS ROUTINE IMPORTS DESIGN DATA FROM CSV FILE (VERSION 0.0) by C.WAKEFIELD.")
  150. (Prompt "\nType ImpDes to run.")
  151. (princ)
回复

使用道具 举报

pBe

32

主题

2722

帖子

2666

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
211
发表于 2022-7-6 00:32:23 | 显示全部楼层
香草味:
  1. (defun c:demo (/ _delFinder collect _remd file data)
  2. ;;        pBe Dec 2013        ;;
  3. [color="#006400"](defun _delFinder( str n / a b lst )
  4.      (while (/= "" str)
  5.            (while
  6.          (and (<= n (strlen str))   
  7.          (not (wcmatch (Setq a (substr str 1 n)) "*`,*")))
  8.                       (setq n (1+ n) b a))
  9.                      (setq lst (cons b lst)
  10.                      str (substr str (1+ n))
  11.                      n 1)
  12.            )
  13.      (reverse lst)
  14.      )[/color]
  15. ;;;        cwakes' idea        ;;;
  16. [color="#006400"](defun _layoutlist  (/ lyt ll)
  17. (Setq lyt (dictsearch (namedobjdict) "ACAD_LAYOUT"))
  18. (while (setq a (assoc 3 lyt))
  19.      (setq ll  (cons (cdr a) ll)
  20.            lyt (cdr (member a lyt))))
  21. (cdr ll)
  22. )    [/color]
  23. (setq        collect        (lambda        (f / a b opf)
  24.           (setq opf (open f "r"))
  25.           (read-line opf)
  26.           (while (setq a (read-line opf))
  27.             (setq b (cons (_delfinder a [color="#006400"][b]1[/b][/color]) b))
  28.           )
  29.           (close opf)
  30.           b
  31.         )
  32. )
  33. (setq        _remd (lambda (lst / a b)
  34.         (while (setq a (car lst))
  35.           (setq        b   (if        (not (member a b))
  36.                       (cons a b)
  37.                       b
  38.                     )
  39.                 lst (cdr lst)
  40.           )
  41.         )
  42.         b
  43.       )
  44. )
  45. (and
  46.    (setq
  47.      file (getfiled "Select Data file" (getvar 'dwgprefix) "csv" 16)
  48.    )
  49.    (setq data (collect file))
  50.    (foreach itm (_remd (mapcar 'car data))
  51.      (if (and (not (member itm (_layoutlist)))
  52.        (snvalid itm)
  53.   )
  54. (command "_.layout" "_New" itm)
  55.      )
  56.      itm
  57.    )
  58.    (foreach itm (_remd (mapcar 'cadr data))
  59.      (if (and (not (tblsearch "Layer" itm))
  60.        (snvalid itm)
  61.   )
  62. (entmake (list (cons 0 "LAYER")
  63.                (cons 100 "AcDbSymbolTableRecord")
  64.                (cons 100 "AcDbLayerTableRecord")
  65.                (cons 2 itm)
  66.                (cons 70 0)
  67.          )
  68. )
  69. itm
  70.      )
  71.    )
  72.    (foreach itm (_remd (mapcar 'caddr data))
  73.      (if (and (not (tblsearch "Style" itm))
  74.        (snvalid itm)
  75.   )
  76. (entmake (list '(0 . "STYLE")
  77.                '(100 . "AcDbSymbolTableRecord")
  78.                '(100 . "AcDbTextStyleTableRecord")
  79.                (cons 2 itm)
  80.                '(70 . 0)
  81.                '(40 . 0.0125)        ;<-- Text height for style
  82.                '(41 . 0.
  83.                '(50 . 0.0)
  84.                '(71 . 0)
  85.                '(3 . "ARIAL.TTF")
  86.                '(4 . "")
  87.          )
  88. )
  89. itm
  90.      )
  91.    )
  92.    (foreach info data
  93.      (entmakex
  94. (list
  95.   (cons 0 "TEXT")
  96.   (cons
  97.     10
  98.     (setq p (list (distof (nth 3 info)) (distof (nth 4 info))))
  99.   )
  100.   (cons 11 p)
  101.   (cons 8 (cadr info))
  102.   (cons 7 (caddr info))
  103.   '(40 . 0.0125)
  104.   (cons 1 (nth 5 info))
  105.   '(71 . 0)
  106.   '(72 . 1)
  107.   '(73 . 2)
  108.   (cons 410 (car info))
  109. )
  110.      )
  111.    )
  112. )
  113. (princ)
  114. )

 
作品它应该在一个框中显示一条警报消息。
回复

使用道具 举报

1

主题

116

帖子

115

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 00:37:34 | 显示全部楼层
回复

使用道具 举报

1

主题

7

帖子

6

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 00:39:06 | 显示全部楼层
Hello,
 
Not being an expert I would think that IntelliCAD AutoLISP would not support ActiveX methods.
 
It is probably a simpler version and would support the long standing AutoLISP functions and coding methods. Nothing cutting edge.
 
I tried both and got error messages.
 
Do I need to place the csv file in a specific directory? Define anything specific in my template drawing? Any other OP actions?
 
Here were the error messages:
 
Command: impdes
null function :error#0
 
Command: demo
bad argument type :error#0
 
Thanks!!
 
Mark Farrell
Sturbridge, MA USA
回复

使用道具 举报

1

主题

7

帖子

6

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 00:41:10 | 显示全部楼层
btw, pBe you signature doesn't work and cwake you don't have a link...
回复

使用道具 举报

1

主题

116

帖子

115

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 00:45:09 | 显示全部楼层
You shouldn't have to. Both functions make use of the getfiled function which should present you with a dialogue box to browse and select the file. If you haven't seen the dialogue box, then both routines must have fallen over before they get to that.
In the case of my routine, I would guess that "null function :error#0" means as you guessed... that ActiveX methods are not supported in Intellicad.
 
Which means it will be better to start with pBe's routine and see why it thinks a function is being passed a "bad argument".
回复

使用道具 举报

1

主题

7

帖子

6

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 00:47:38 | 显示全部楼层
No dialog box presented...
 
Can I embed statements in the code to see where it might have fallen over?
回复

使用道具 举报

1

主题

116

帖子

115

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 00:50:28 | 显示全部楼层
 
You could, and I was thinking the same. Keeping in mind that I'm suggesting sticking with pBe's routine... First I'd check to see if Intellicad has a variable "dwgprefix". Because I'm thinking that there is most likely where it might fall over. There isn't much else to evaluate before that.
 
Also as a method of "embedding statements", see if
  1. (defun c:helpme ()(alert "This is debug location number 1"))
 
works? It should bring up an alert message in a box.
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-10 21:19 , Processed in 0.622968 second(s), 70 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表