CXLAYSTDLayerVersione 1.0.0Provato in AutoCAD
Creare un set di layer da un elenco CSV
Costruisce il tuo standard di layer da un foglio di calcolo: nome, colore e tipo di linea per riga. I layer esistenti vengono aggiornati di conseguenza.
Come si usa
- Prepara un file CSV con nome del layer, numero di colore e tipo di linea su ogni riga. Una riga di intestazione va bene.
- Digita CXLAYSTD e scegli il file.
- I layer mancanti vengono creati, quelli esistenti prendono colore e tipo di linea dal file.
Da sapere
- I tipi di linea non ancora caricati vengono caricati dal file standard dei tipi di linea. Quelli sconosciuti ripiegano su Continuous.
Caricarlo in AutoCAD
- Salva il file dove preferisci.
- Digita APPLOAD, scegli il file e premi Carica.
- Digita il nome del comando: CXLAYSTD.
Per averlo a ogni avvio, aggiungilo alla Raccolta di avvio nella stessa finestra, oppure carica un'intera cartella in una volta con il caricatore facoltativo. Il caricatore facoltativo
Provato in AutoCAD (versione completa). Altri programmi che eseguono AutoLISP potrebbero funzionare, ma non sono stati provati.
Il codice
Testo semplice, niente di nascosto. Copialo in un file chiamato cxlaystd.lsp oppure scaricalo.
;;; ============================================================================
;;; CXLAYSTD - Create a set of layers from a CSV list
;;; Version 1.0.0
;;; CADXChange LISP tools - https://cadxchange.net/resources/lisp/cxlaystd
;;; 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
;;; ============================================================================
;; Make or update layers from CSV rows (name, color number, linetype). The linetype is optional and is loaded
;; from the standard linetype file when the drawing does not have it yet.
;; Returns (created updated).
(defun cx:laystd-run (rows / created updated name color lt ed)
(setq created 0 updated 0)
(foreach r rows
(setq name (cx:trim (car r))
color (if (and (cdr r) (> (strlen (cx:trim (cadr r))) 0)) (atoi (cadr r)) 7)
lt (if (and (cddr r) (> (strlen (cx:trim (caddr r))) 0)) (cx:trim (caddr r)) "Continuous"))
(if (and (> (strlen name) 0) (not (cx:bad-name name)) (> color 0) (< color 256))
(progn
(if (not (tblsearch "LTYPE" lt))
(command "_.-LINETYPE" "_L" lt "acad.lin" ""))
(if (not (tblsearch "LTYPE" lt)) (setq lt "Continuous"))
(if (setq ed (tblsearch "LAYER" name))
(progn
(cx:lay-set name 62 (if (< (cdr (assoc 62 ed)) 0) (- color) color))
(cx:lay-set name 6 lt)
(setq updated (1+ updated)))
(progn
(entmakex (list '(0 . "LAYER") '(100 . "AcDbSymbolTableRecord") '(100 . "AcDbLayerTableRecord")
(cons 2 name) '(70 . 0) (cons 62 color) (cons 6 lt)))
(setq created (1+ created)))))))
(list created updated))
(defun c:CXLAYSTD ( / *error* cx:sv file rows r)
(setq *error* cx:err)
(setq file (cx:file-open "Layer list (CSV: name, color number, linetype)"))
(if file
(if (setq rows (cx:csv-rows file))
(progn
;; a header line such as "name,color,linetype" has no number in its second field
(if (and rows (cdr (car rows)) (not (wcmatch (cx:trim (cadr (car rows))) "#*")))
(setq rows (cdr rows)))
(cx:start '(("CMDECHO" . 0)))
(setq r (cx:laystd-run rows))
(princ (strcat "\n" (itoa (car r)) " layer(s) created, " (itoa (cadr r)) " updated."))
(cx:end))
(princ "\nThe file could not be read.")))
(princ))
;;; ---- Shared helpers (used by the command above; identical in every CADXChange tool) ----
;; 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)))))
;; 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)
;; True when STR holds a character that is not allowed in a layer, block or style name.
(defun cx:bad-name (str / bad)
(foreach ch '("<" ">" "/" "\\" "\"" ":" ";" "?" "*" "|" "=" "`" ",")
(if (cx:find str ch 1) (setq bad T)))
bad)
;;; List helpers. See helpers.lsp for the format.
;; Set the DXF group CODE of layer NAME to VAL; returns T, or nil when there is no such layer.
(defun cx:lay-set (name code val / en ed)
(if (setq en (tblobjname "LAYER" name))
(progn
(setq ed (entget en))
(entmod (if (assoc code ed)
(subst (cons code val) (assoc code ed) ed)
(append ed (list (cons code val)))))
T)))
;; 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)))
;; 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 "\nCXLAYSTD loaded. Type CXLAYSTD to start.")
(princ)
Cronologia delle versioni
Versione 1.0.0 · 27 ottobre 2026
Prima versione.
Ti serve su molti disegni?
LayerSync svolge questo tipo di lavoro su un'intera cartella di disegni in un solo passaggio.
LayerSyncAltri comandi: Layer
CXLAYISOIsolare i layer degli oggetti che scegliCXLAYRESRipristinare i layer disattivati da CXLAYISOCXLAYOFFDisattivare i layer degli oggetti che scegliCXLAYCOLORCambiare il colore dei layer che scegliCXLAYRENRinominare i layer con trova e sostituisciCXLAYSETSpostare oggetti sul layer di un altro oggettoCXLAYMRGUnire layer in uno solo ed eliminare gli altriCXLAYCOUNTContare gli oggetti su ogni layerCXLAYSELSelezionare tutto ciò che sta sui layer che scegli