CADXChange

全部 LISP 命令

CXCSV2TXT导出与导入(CSV)版本 1.0.0已在 AutoCAD 中测试

按 CSV 文件放置文字

从电子表格文件读取 x、y 和文字,在每个位置放一个文字,每行还可以单独指定字高和旋转角度。

怎么用

  1. 准备一个 CSV,列为 x、y、文字,可选的字高和旋转角度(度)。
  2. 输入 CXCSV2TXT,选择该文件。
  3. 文字被放在当前图层和当前样式中。

需要知道

  • 表头会被自动跳过。

在 AutoCAD 中加载

  1. 把文件保存在任意位置。
  2. 输入 APPLOAD,选择文件,点“加载”。
  3. 输入命令名:CXCSV2TXT。

想每次启动都可用,在同一个对话框里加入“启动组”,或用可选的加载器一次加载整个文件夹。 关于可选的加载器

已在 AutoCAD(完整版)中测试。其他能运行 AutoLISP 的软件也许可用,但未经测试。

代码

纯文本,没有任何隐藏内容。复制后存成名为 cxcsv2txt.lsp 的文件,或直接下载。

;;; ============================================================================
;;; CXCSV2TXT - Place text from a CSV file
;;; Version 1.0.0
;;; CADXChange LISP tools - https://cadxchange.net/resources/lisp/cxcsv2txt
;;; Tested in: AutoCAD (full version). Other programs that run AutoLISP may work but are not tested.
;;; Copyright (c) CADXChange. Free for personal and commercial use; do not resell or
;;; republish it as a collection. Terms: https://cadxchange.net/legal/lisp-terms
;;; ============================================================================

;; Place text from CSV ROWS: x, y, text, and optionally height (default HEIGHT) and rotation in degrees.
;; A row whose coordinates are not numbers (a header, for instance) is skipped. Returns how many texts were made.
(defun cx:csv2txt-run (rows height layer / n)
  (setq n 0)
  (foreach r rows
    (if (and (cdr r) (cddr r) (numberp (read (car r))) (numberp (read (cadr r))))
      (progn
        (entmakex (list '(0 . "TEXT") (cons 8 layer) (list 10 (atof (car r)) (atof (cadr r)) 0.0)
                        (cons 40 (if (and (> (length r) 3) (> (atof (nth 3 r)) 0.0)) (atof (nth 3 r)) height))
                        (cons 1 (caddr r))
                        (cons 50 (if (> (length r) 4) (* pi (/ (atof (nth 4 r)) 180.0)) 0.0))
                        (cons 7 (getvar "TEXTSTYLE")) '(100 . "AcDbText")))
        (setq n (1+ n)))))
  n)

(defun c:CXCSV2TXT ( / *error* cx:sv file rows)
  (setq *error* cx:err)
  (if (setq file (cx:file-open "CSV file with x, y, text"))
    (if (setq rows (cx:csv-rows file))
      (progn
        (cx:start '(("CMDECHO" . 0)))
        (princ (strcat "\n" (itoa (cx:csv2txt-run rows (getvar "TEXTSIZE") (getvar "CLAYER"))) " text(s) placed."))
        (cx:end))
      (princ "\nThe file could not be read.")))
  (princ))


;;; ---- Shared helpers (used by the command above; identical in every CADXChange tool) ----

;; Close the undo step and put the system variables back.
(defun cx:end ()
  (foreach v cx:sv (setvar (car v) (cdr v)))
  (command "_.UNDO" "_End")
  (setq cx:sv nil)
  (princ))

;; Error handler: restores everything, stays quiet on Esc.
(defun cx:err (msg)
  (if (> (getvar "CMDACTIVE") 0) (command))
  (cx:end)
  (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")))
    (princ (strcat "\nError: " msg)))
  (princ))

;; Ask which CSV file to read. Returns a path or nil. With FILEDIA 0 the name is typed at the command line.
(defun cx:file-open (title / f)
  (if (= (getvar "FILEDIA") 0)
    (progn
      (setq f (getstring T (strcat "\n" title ": ")))
      (if (= f "") nil f))
    (getfiled title (getvar "DWGPREFIX") "csv" 4)))

;; All lines of a text file, or nil when it cannot be opened.
(defun cx:read-lines (file / f l out)
  (if (setq f (open file "r"))
    (progn
      (while (setq l (read-line f)) (setq out (cons l out)))
      (close f)
      (reverse out))))

;; One CSV line to a list of strings (handles quotes and doubled quotes).
(defun cx:csv-parse (line / out cur q i c)
  (setq out nil cur "" q nil i 1)
  (while (<= i (strlen line))
    (setq c (substr line i 1))
    (cond
      ((and q (= c "\"") (= (substr line (1+ i) 1) "\""))
       (setq cur (strcat cur "\"") i (1+ i)))
      ((= c "\"") (setq q (not q)))
      ((and (= c ",") (not q)) (setq out (cons cur out) cur ""))
      (T (setq cur (strcat cur c))))
    (setq i (1+ i)))
  (reverse (cons cur out)))

;; Remove spaces and tabs at both ends.
(defun cx:trim (s / a b)
  (setq a 1 b (strlen s))
  (while (and (<= a b) (member (substr s a 1) '(" " "\t"))) (setq a (1+ a)))
  (while (and (>= b a) (member (substr s b 1) '(" " "\t"))) (setq b (1- b)))
  (substr s a (max 0 (1+ (- b a)))))

;; The rows of a CSV file as lists of strings, empty lines skipped; nil when the file cannot be read.
(defun cx:csv-rows (file / out)
  (foreach l (cx:read-lines file)
    (if (/= (cx:trim l) "") (setq out (cons (cx:csv-parse l) out))))
  (reverse out))

;; Save the system variables in VARS ((name . value) ...), set them, and open one undo step.
;; The caller declares cx:sv as a local so the saved values live exactly as long as the command.
(defun cx:start (vars)
  (setq cx:sv (mapcar (function (lambda (v) (cons (car v) (getvar (car v))))) vars))
  (command "_.UNDO" "_Begin")
  (foreach v vars (setvar (car v) (cdr v)))
  (princ))

(princ "\nCXCSV2TXT loaded. Type CXCSV2TXT to start.")
(princ)

版本记录

版本 1.0.0 · 2026年10月27日

首个版本。

更多命令:导出与导入(CSV)