读取 CSV 并使用值插入和填充块

问题描述 投票:0回答:1

我正在尝试编写一个代码来读取 csv 文件,并为每一行吐出一个块,其中包含使用该 csv 定义的属性。无论我尝试什么,我似乎都会遇到错误,尽管我有大量的编程知识,但 AutoLISP 对我来说非常令人困惑。起初我尝试使用“vl-string-split”,但 AutoCAD 告诉我该函数不存在(尽管有 VisualLISP)。接下来我尝试使用 Lee Mac 的 read-csv 代码,现在我收到“参数太少”的错误。我知道我可能在这里遗漏了一些非常基本的东西,但我已经达到了在论坛中搜索合适答案的极限。如果有人能指出这个新手正确的方向,我将不胜感激!

不确定是否相关,但我正在使用 AutoCAD Map 3D 2020

(defun c:poleTest ()

;; Read CSV  -  Lee Mac
;; Parses a CSV file into a matrix list of cell values.
;; csv - [str] filename of CSV file to read

(defun LM:readcsv ( csv / des lst sep str )
    (if (setq des (open csv "r"))
        (progn
            (setq sep (cond ((vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (",")))
            (while (setq str (read-line des))
                (setq lst (cons (LM:csv->lst str sep 0) lst))
            )
            (close des)
        )
    )
    (reverse lst)
)

;; CSV -> List  -  Lee Mac
;; Parses a line from a CSV file into a list of cell values.
;; str - [str] string read from CSV file
;; sep - [str] CSV separator token
;; pos - [int] initial position index (always zero)

(defun LM:csv->lst ( str sep pos / s )
    (cond
    (   (not (setq pos (vl-string-search sep str pos)))
        (if (wcmatch str "\"*\"")
            (list (LM:csv-replacequotes (substr str 2 (- (strlen str) 2))))
            (list str)
        )
    )
    (   (or (wcmatch (setq s (substr str 1 pos)) "\"*[~\"]")
            (and (wcmatch s "~*[~\"]*") (= 1 (logand 1 pos)))
        )
        (LM:csv->lst str sep (+ pos 2))
        )
        (   (wcmatch s "\"*\"")
            (cons
                (LM:csv-replacequotes (substr str 2 (- pos 2)))
                (LM:csv->lst (substr str (+ pos 2)) sep 0)
            )
        )
        (   (cons s (LM:csv->lst (substr str (+ pos 2)) sep 0)))
    )
)

(defun LM:csv-replacequotes ( str / pos )
(setq pos 0)
(while (setq pos (vl-string-search  "\"\"" str pos))
    (setq str (vl-string-subst "\"" "\"\"" str pos)
          pos (1+ pos)
    )
)
str
)
;; End of Lee Mac code
    (setq filePath (getfiled "Select CSV file" "" "csv" 0))
    (progn
(setq file (open filePath "r"))
        (while (setq line (LM:csv->lst file))
            (setq filePath "Z:\\Project Tools\\Customized Tools\\CAD\\prog\\BELL\\polebase1.dwg")
            (setq xyz "0,0,0")
            (setq scale "1,1,1")
            (setq height (car (last line)))
            (setq circ (car (nth 14 line)))
            (setq year (car (nth 13 line)))
            (setq txno (car (nth 12 line)))
            (setq ground (car (nth 17 line)))
            (setq slground (car (nth 18 line)))
            (setq pics (car (nth 6 line)))
            (setq poleno (car (nth 4 line)))
            (setq poleID (car (nth 5 line)))
            (setq owner (car (car line)))
            (princ filePath)
            (princ xyz)
            (princ scale)
            (princ height)
            (princ circ)
            (princ year)
            (princ txno)
            (princ ground)
            (princ slground)
            (princ pics)
            (princ poleno)
            (princ poleID)
            (princ owner)
            (command "insert" filePath xyz scale height circ year txno ground slground pics poleno poleID owner)

        )
    )
)
lisp autocad-plugin autolisp
1个回答
0
投票

我的

LM:readcsv
函数会将 CSV 文件的内容读取到列表列表中,然后您可以对其进行迭代(例如,使用
foreach
循环)以插入块。

当前代码的问题是您正在调用我的

LM:csv->lst
函数,该函数需要三个参数:要解析的字符串、分隔符和初始字符位置(始终为 0)。相反,您应该调用我的
LM:readcsv
函数。

考虑以下代码:

(defun c:poleTest ( / atr circ fnm ground height lst owner pics poleID poleno rot scl slground txno xyz year )
    (cond
        (   (not (setq fnm (getfiled "Select CSV file" "" "csv" 16)))
            (princ "\n*Cancel*")
        )
        (   (not (setq lst (LM:readcsv fnm)))
            (princ "\nError reading CSV file.")
        )
        (   t
            (setq blk "Z:\\Project Tools\\Customized Tools\\CAD\\prog\\BELL\\polebase1.dwg"
                  xyz '(0 0 0)
                  scl 1.0
                  rot 0.0
            )
            (setq atr (getvar 'attreq))
            (setvar 'attreq 1)
            (foreach row lst
                (setq height   (last   row)
                      circ     (nth 14 row)
                      year     (nth 13 row)
                      txno     (nth 12 row)
                      ground   (nth 17 row)
                      slground (nth 18 row)
                      pics     (nth  6 row)
                      poleno   (nth  4 row)
                      poleID   (nth  5 row)
                      owner    (nth  0 row)
                )
                (command 
                    "_.-insert" blk "_S" scl "_R" rot "_non" xyz
                    height circ year txno ground slground pics poleno poleID owner
                )
            )
            (setvar 'attreq atr)
        )
    )
    (princ)
)

;; Read CSV  -  Lee Mac
;; Parses a CSV file into a matrix list of cell values.
;; csv - [str] filename of CSV file to read

(defun LM:readcsv ( csv / des lst sep str )
    (if (setq des (open csv "r"))
        (progn
            (setq sep (cond ((vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (",")))
            (while (setq str (read-line des))
                (setq lst (cons (LM:csv->lst str sep 0) lst))
            )
            (close des)
        )
    )
    (reverse lst)
)

;; CSV -> List  -  Lee Mac
;; Parses a line from a CSV file into a list of cell values.
;; str - [str] string read from CSV file
;; sep - [str] CSV separator token
;; pos - [int] initial position index (always zero)

(defun LM:csv->lst ( str sep pos / s )
    (cond
    (   (not (setq pos (vl-string-search sep str pos)))
        (if (wcmatch str "\"*\"")
            (list (LM:csv-replacequotes (substr str 2 (- (strlen str) 2))))
            (list str)
        )
    )
    (   (or (wcmatch (setq s (substr str 1 pos)) "\"*[~\"]")
            (and (wcmatch s "~*[~\"]*") (= 1 (logand 1 pos)))
        )
        (LM:csv->lst str sep (+ pos 2))
        )
        (   (wcmatch s "\"*\"")
            (cons
                (LM:csv-replacequotes (substr str 2 (- pos 2)))
                (LM:csv->lst (substr str (+ pos 2)) sep 0)
            )
        )
        (   (cons s (LM:csv->lst (substr str (+ pos 2)) sep 0)))
    )
)

(defun LM:csv-replacequotes ( str / pos )
    (setq pos 0)
    (while (setq pos (vl-string-search  "\"\"" str pos))
        (setq str (vl-string-subst "\"" "\"\"" str pos)
              pos (1+ pos)
        )
    )
    str
)

(princ)
© www.soinside.com 2019 - 2024. All rights reserved.