CADXChange

すべての LISP コマンド

CXLAYER2CSV書き出しと取り込み(CSV)バージョン 1.0.0AutoCAD で動作確認済み

画層の一覧を CSV ファイルに書き出す

すべての画層について、色、線種、線の太さ、オン・フリーズ・ロック・印刷の状態、各画層の図形数を表にして保存します。

使い方

  1. CXLAYER2CSV と入力します。
  2. 保存するファイルを選びます。
  3. 開いて確認したり、自社の画層標準と見比べたりします。

知っておくこと

  • CXLAYSTD と組み合わせれば、画層標準を図面間で受け渡せます。

AutoCAD で読み込む

  1. ファイルを好きな場所に保存します。
  2. APPLOAD と入力し、ファイルを選んで「ロード」を押します。
  3. コマンド名を入力します:CXLAYER2CSV。

起動のたびに使うには、同じ画面で「スタートアップ スイート」に追加するか、任意のローダーでフォルダーごと読み込みます。 任意のローダーについて

AutoCAD(フル版)で動作確認しています。AutoLISP を実行できるほかのソフトでも動く場合がありますが、確認はしていません。

コード

隠している部分のないテキストです。cxlayer2csv.lsp という名前のファイルにコピーするか、ダウンロードしてください。

;;; ============================================================================
;;; CXLAYER2CSV - Export the layer list to a CSV file
;;; Version 1.0.0
;;; CADXChange LISP tools - https://cadxchange.net/resources/lisp/cxlayer2csv
;;; 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) describing every layer: name, color number, linetype, lineweight (1/100 mm, -3 is the
;; default), on, frozen, locked, plot, and how many objects in the drawing are on it.
(defun cx:layer2csv-lines ( / rows r color flags en ss)
  (setq r (tblnext "LAYER" T))
  (while r
    (setq color (cx:dxf 62 r)
          flags (cx:dxf 70 r)
          en    (tblobjname "LAYER" (cx:dxf 2 r))
          ss    (ssget "X" (list (cons 8 (cx:dxf 2 r)))))
    (setq rows (cons (cx:csv-line
                       (list (cx:dxf 2 r) (itoa (abs color)) (cx:dxf 6 r)
                             (itoa (if (cx:dxf 370 (entget en)) (cx:dxf 370 (entget en)) -3))
                             (if (< color 0) "No" "Yes")
                             (if (= 1 (logand 1 flags)) "Yes" "No")
                             (if (= 4 (logand 4 flags)) "Yes" "No")
                             (if (= 0 (if (cx:dxf 290 (entget en)) (cx:dxf 290 (entget en)) 1)) "No" "Yes")
                             (itoa (if ss (sslength ss) 0))))
                     rows))
    (setq r (tblnext "LAYER")))
  (cons "Layer,Color,Linetype,Lineweight,On,Frozen,Locked,Plot,Objects" (reverse rows)))

(defun c:CXLAYER2CSV ( / *error* cx:sv file)
  (setq *error* cx:err)
  (if (setq file (cx:file-save "Save the layer list as CSV" "layers"))
    (cx:csv-save file (cx:layer2csv-lines)))
  (princ))


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

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

;; 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) ","))

;; 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 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)))

(princ "\nCXLAYER2CSV loaded. Type CXLAYER2CSV to start.")
(princ)

バージョン履歴

バージョン 1.0.0 · 2026年10月27日

最初のリリース。

たくさんの図面にまとめて使いたい?

LayerSync なら、フォルダー内の図面すべてに対してこの種の作業を一度に行えます。

LayerSync

ほかのコマンド:書き出しと取り込み(CSV)