Message 1 of 7
Lisp for creating a table with Layer name, Linetype, and other information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have 2 lisps that i use to create a DWG that lists the layers in that drawing.
The first works well, but the "table" is just line work and the rows are not in alphabetical order. (see attached)
The second doesn't work... Any help? (see below)
(defun CMLIST (/ table acadobject activedocument layername layerelement color linetype lineweight)
; Get the active AutoCAD object and document
(setq acadobject (vlax-get-Acad-Object))
(setq activedocument (vla-get-activedocument acadobject))
; Initialize an empty string for table content
(setq table "")
; Set table header row
(setq table (strcat table "Layer,Color,Linetype,Lineweight\n"))
; Get all layers
(setq layername (vla-get-layer activedocument))
(while (not (vlax-null layername))
; Get layer properties
(setq layerelement (vlax-get-object layername))
(setq color (vlax-get-property layerelement 'Color))
(setq linetype (vlax-get-property layerelement 'Linetype))
(setq lineweight (vlax-get-property layerelement 'Lineweight))
; Add layer information to table string
(setq table (strcat table (vlax-get-property layerelement 'Name) "," color "," linetype "," lineweight "\n"))
; Set layer to next layer
(setq layername (vlax-get-next layername))
)
; Sort table content by layer name (using a custom sort function)
(setq table (sort (string-split table "\n") 'layer-name-sort))
; Insert a sample block on each layer
(command "-layer" "set" "0") ; Set current layer to layer 0 (non-existent)
(command "INSERT" "1inch" (list 0 0 0) 1 1 0) ; Insert block "1inch" at origin with scale 1
(dotimes (i (length (string-split table "\n")))
(setq layername (car (string-split (elt table i) ","))) ; Get layer name from each row
(command "-layer" "set" layername) ; Set current layer
(command "INSERT" "1inch" (list 0 0 0) 1 1 0) ; Insert block again
)
; Try using table object (if supported)
(if (and (vlax-loaded? 'Autodesk.AutoCAD.EditorInput)
(vlax-doc-object activedocument 'Autodesk.AutoCAD.EditorInput.Table))
(progn
; Create table object (for newer versions)
(setq table (vlax-create-object "Autodesk.AutoCAD.EditorInput.Table"))
; Set table column names
(vlax-put-table-cell-value table 0 0 "Layer")
(vlax-put-table-cell-value table 0 1 "Color")
(vlax-put-table-cell-value table 0 2 "Linetype")
(vlax-put-table-cell-value table 0 3 "Lineweight")
; Split table string into rows and add to table object
(dolist (row (string-split table "\n"))
(setq layerelement (string-split row ","))
(vlax-put-table-cell-value table (vlax-add-table-row table) 0 (car layerelement))
(vlax-put-table-cell-value table (vlax-add-table-row table) 1 (cadr layerelement))
(vlax-put-table-cell-value table (vlax-add-table-row table) 2 (cddr layerelement))
(vlax-put-table-cell-value table (vlax-add-table-row table) 3 (caddr (cddr layerelement))))
; Display the table (using newer method if available)
(vlax-table-insert-table activedocument table (list 10 10 0 0))
)
(alert (strcat "**Layer Information**\n\n" table)) ; Fallback to alert for older versions
)
)
Jon Baker | Land Development & Infrastructure Design Manager
Alignment Design Group | Denver | Colorado
Civil 3D 2025 | Windows 11
Alignment Design Group | Denver | Colorado
Civil 3D 2025 | Windows 11