Community
Here's something the ChatGPT wrote to about 90%. A lot has changed since 2007 since this thread was started.
(vl-load-com) ; Load the ActiveX Automation support
;; Program below allows to export the layer name and descriptions to a .csv
;; where you can then modify it and import it back it.
;; See the very bottom of this code.
(defun compare-layer-info (layer-info-1 layer-info-2)
; "Compare layer information for sorting."
(if (equal (car layer-info-1) (car layer-info-2))
nil
(< (car layer-info-1) (car layer-info-2))
)
)
;; Credit to dlanorh. See, https://www.cadtutor.net/forum/topic/66221-how-to-split-a-string-by-character/?do=findComment&comment=543921
(defun rh:str_split (str char lst / pos)
(setq lst (cons (substr str 1 (setq pos (vl-string-position (ascii char) str))) lst)
str (substr str (+ 2 pos))
)
(if (vl-string-position (ascii char) str) (rh:str_split str char lst) (setq lst (reverse (cons str lst))))
)
;;
;; export-layer-info-to-csv function
;;
(defun export-layer-info-to-csv (output-file / file layers layer-desc layer-info layer-name)
(setq layer-info '()) ; Initialize a list to store layer information
(setq layers (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object))))
; Iterate through each layer and store its name and description
(vlax-for layer layers
(setq layer-name (vla-get-Name layer))
(setq layer-desc (vla-get-Description layer))
(setq layer-info (cons (list layer-name layer-desc) layer-info))
) ; Sort the layer information based on layer names without string-lessp and string<
(setq layer-info (vl-sort layer-info 'compare-layer-info)) ; Write the sorted layer information to the CSV file
(setq file (open output-file "w"))
(write-line "Layer Name, Description" file)
(foreach info layer-info
(setq layer-name (car info))
(setq layer-desc (cadr info))
(write-line (strcat "\"" layer-name "\",\"" layer-desc "\"") file)
)
(close file)
(princ (strcat "Layer information exported to " output-file))
) ;; end export-layer-info-to-csv function
;;
;; import-layer-descriptions function
;;
(defun import-layer-descriptions (csv-file-path / csv-file data layer-description layer-info layer-name layer-obj)
;; Read CSV file
(setq csv-file (open csv-file-path "r"))
(setq data (read-line csv-file)) ;; Skip header if exists
(setq layer-cntOK 0)
(setq layer-cntNOTOK 0)
;; Iterate through CSV data
(while (setq data (read-line csv-file))
(setq layer-info (rh:str_split data "," nil))
(setq layer-name (read (nth 0 layer-info)))
(setq layer-description (read (nth 1 layer-info)))
;; Check if layer exists
(if (tblsearch "layer" layer-name)
;; Layer exists, update description
(progn
(setq layer-obj (vla-add (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) layer-name))
(setq layer-cntOK (+ layer-cntOK 1))
(vla-put-description layer-obj layer-description)
(princ (strcat "Updated description for layer: " layer-name " with description " layer-description "\n"))
)
;; Layer doesn't exist, create new layer
(progn
(setq layer-cntNOTOK (+ layer-cntNOTOK 1))
; (vla-Add (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object))) layer-name)
; (setq layer-obj (vla-add (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) layer-name))
; (vla-put-description layer-obj layer-description)
; (princ (strcat "Created new layer: " layer-name " with description " layer-description "\n"))
)
)
)
(if layer-info
(progn
(princ "\nREPORT")
(princ (strcat "\n" (itoa layer-cntOK) " layers successfully changed :)"))
(princ (strcat "\n" (itoa layer-cntNOTOK) " layers unsuccessfully changed :(\n"))
)
)
;; Close CSV file
(close csv-file)
) ;; end import-layer-descriptions function
;; Set your path to the .csv file here
(setq csvFile "C:\\Users\\username\\Desktop\\OutputFileNew.csv")
;; Example usage for exporting:
(export-layer-info-to-csv csvFile)
;; Example usage for importing:
; (import-layer-descriptions csvFile)