CADXChange

全部 LISP 命令

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

把图块及其属性导出为 CSV 文件

导出每个图块的名称、图层、位置、旋转角度、比例,每个属性一列,直接得到一份材料清单。

怎么用

  1. 输入 CXBLK2CSV,选择 All 或 Selection。
  2. 选择要保存的文件。
  3. 用 Excel 或任何电子表格打开 CSV。

需要知道

  • 属性列由导出图块中出现的所有标签自动生成。

在 AutoCAD 中加载

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

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

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

代码

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

;;; ============================================================================
;;; CXBLK2CSV - Export blocks and their attributes to a CSV file
;;; Version 1.0.0
;;; CADXChange LISP tools - https://cadxchange.net/resources/lisp/cxblk2csv
;;; 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
;;; ============================================================================

;; CSV lines (header first) for the block references in NAMES: block, layer, x, y, rotation in degrees, X scale,
;; then one column per attribute tag found in any of the blocks (in order of appearance).
(defun cx:blk2csv-lines (names / tags rows ed)
  (setq tags (cx:tags names))
  (foreach e names
    (setq ed (entget e))
    (setq rows (cons (cx:csv-line
                       (append
                         (list (cx:dxf 2 ed) (cx:dxf 8 ed)
                               (cx:n4 (car (cx:dxf 10 ed))) (cx:n4 (cadr (cx:dxf 10 ed)))
                               (cx:n4 (* 180.0 (/ (if (cx:dxf 50 ed) (cx:dxf 50 ed) 0.0) pi)))
                               (cx:n4 (if (cx:dxf 41 ed) (cx:dxf 41 ed) 1.0)))
                         (mapcar (function (lambda (tg) (if (cx:att-get e tg) (cx:att-get e tg) ""))) tags)))
                     rows)))
  (cons (cx:csv-line (append '("Block" "Layer" "X" "Y" "Rotation" "Scale") tags))
        (reverse rows)))

(defun c:CXBLK2CSV ( / *error* cx:sv ss file)
  (setq *error* cx:err)
  (setq ss (if (= (cx:ask-kw "Export blocks in" "All Selection" "All") "All")
             (ssget "X" (list '(0 . "INSERT") (cons 410 (getvar "CTAB"))))
             (cx:pick "\nSelect blocks: " '((0 . "INSERT")))))
  (if ss
    (if (setq file (cx:file-save "Save the block list as CSV" "blocks"))
      (cx:csv-save file (cx:blk2csv-lines (cx:ss->list ss))))
    (princ "\nNo blocks found."))
  (princ))


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

(defun cx:dxf (code ed)
  (cdr (assoc code ed)))

;; The ATTRIB entities of a block reference, in order.
(defun cx:attribs (ins / e out)
  (setq e (entnext ins))
  (while (and e (= (cx:dxf 0 (entget e)) "ATTRIB"))
    (setq out (cons e out) e (entnext e)))
  (reverse out))

;; The attribute tags found in a list of block references, each once, in order of appearance.
(defun cx:tags (names / out)
  (foreach e names
    (foreach a (cx:attribs e)
      (if (not (member (strcase (cx:dxf 2 (entget a))) (mapcar 'strcase out)))
        (setq out (append out (list (cx:dxf 2 (entget a))))))))
  out)

;; 1-based position of SUB in STR at or after START, or nil.
(defun cx:find (str sub start / n i r)
  (setq n (strlen sub) i start r nil)
  (if (> n 0)
    (while (and (not r) (<= i (1+ (- (strlen str) n))))
      (if (= (substr str i n) sub) (setq r i))
      (setq i (1+ i))))
  r)

;; Replace every OLD in STR by NEW (case sensitive).
(defun cx:replace (str old new / n out pos)
  (setq n (strlen old) out "" pos 1)
  (if (= n 0)
    str
    (progn
      (while (<= pos (1+ (- (strlen str) n)))
        (if (= (substr str pos n) old)
          (setq out (strcat out new) pos (+ pos n))
          (setq out (strcat out (substr str pos 1)) pos (1+ pos))))
      (strcat out (substr str pos)))))

;; One CSV field: quoted when it holds a comma, a quote or a line break.
(defun cx:csv-field (s)
  (if (or (cx:find s "," 1) (cx:find s "\"" 1) (cx:find s "\n" 1) (cx:find s "\r" 1))
    (strcat "\"" (cx:replace s "\"" "\"\"") "\"")
    s))

(defun cx:join (lst delim / out)
  (setq out "")
  (foreach s lst
    (setq out (if (= out "") s (strcat out delim s))))
  (if lst out ""))

(defun cx:csv-line (fields)
  (cx:join (mapcar 'cx:csv-field fields) ","))

;; Real X as text with at most PREC decimals and no trailing zeros ("2.50" -> "2.5", "3.00" -> "3").
(defun cx:num (x prec / s)
  (setq s (rtos x 2 prec))
  (if (wcmatch s "*.*")
    (progn
      (while (= (substr s (strlen s)) "0") (setq s (substr s 1 (1- (strlen s)))))
      (if (= (substr s (strlen s)) ".") (setq s (substr s 1 (1- (strlen s)))))))
  (if (or (= s "-0") (= s "")) "0" s))

;; A real as text with at most 4 decimals.
(defun cx:n4 (x) (cx:num x 4))

;;; Geometry helpers: polylines with arcs, length, area, centroid, a point inside a shape.
;;; See helpers.lsp for the format.

;; The ATTRIB of a block reference with the tag TAG (any case), or nil.
(defun cx:att-find (ins tag / r)
  (foreach a (cx:attribs ins)
    (if (and (not r) (= (strcase (cx:dxf 2 (entget a))) (strcase tag))) (setq r a)))
  r)

(defun cx:att-get (ins tag / a)
  (if (setq a (cx:att-find ins tag)) (cx:dxf 1 (entget a))))

;; 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 for one of the keywords in BITS ("Yes No"), default DEF; returns the keyword.
(defun cx:ask-kw (msg bits def / v)
  (initget 0 bits)
  (setq v (getkword (strcat "\n" msg " [" (cx:replace bits " " "/") "] <" def ">: ")))
  (if v v def))

;; Use the objects already selected, or ask the user to select; FILTER is an ssget filter list or nil.
(defun cx:pick (msg filter / ss)
  (setq ss (if filter (ssget "_I" filter) (ssget "_I")))
  (if (null ss)
    (progn
      (princ msg)
      (setq ss (if filter (ssget filter) (ssget)))))
  ss)

;; Ask where to save a CSV file; BASE is the proposed file name without folder and extension. Returns a path or nil.
;; With FILEDIA 0 the name is typed at the command line instead of picked in a dialog.
(defun cx:file-save (title base / def f)
  (setq def (strcat (getvar "DWGPREFIX") base ".csv"))
  (if (= (getvar "FILEDIA") 0)
    (progn
      (setq f (getstring T (strcat "\n" title " <" def ">: ")))
      (if (= f "") def f))
    (getfiled title def "csv" 1)))

;; Write LINES to FILE; returns T, or nil when the file cannot be written.
(defun cx:write-lines (file lines / f)
  (if (setq f (open file "w"))
    (progn
      (foreach l lines (write-line l f))
      (close f)
      T)))

;; Write CSV LINES to FILE and report it; returns T when the file was written.
(defun cx:csv-save (file lines)
  (if (cx:write-lines file lines)
    (progn (princ (strcat "\n" (itoa (1- (length lines))) " row(s) written to " file)) T)
    (progn (princ (strcat "\nThe file could not be written: " file)) nil)))

;; Selection set to a list of entity names.
(defun cx:ss->list (ss / i out)
  (setq i 0)
  (if ss
    (repeat (sslength ss)
      (setq out (cons (ssname ss i) out)
            i   (1+ i))))
  (reverse out))

(princ "\nCXBLK2CSV loaded. Type CXBLK2CSV to start.")
(princ)

版本记录

版本 1.0.0 · 2026年10月27日

首个版本。

要对很多张图一起做?

BlockSync 可以对整个文件夹的图纸一次完成这类工作。

BlockSync

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