Lisp for creating a table with Layer name, Linetype, and other information.

Lisp for creating a table with Layer name, Linetype, and other information.

Baker_ADG
Advocate Advocate
632 Views
6 Replies
Message 1 of 7

Lisp for creating a table with Layer name, Linetype, and other information.

Baker_ADG
Advocate
Advocate

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
0 Likes
633 Views
6 Replies
  • Lisp
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant

@Baker_ADG wrote:

.... The first works well, but .... (see attached)
The second doesn't work...


Just to confirm:  is the laytable.lsp attachment the first one [implied by your "(see attached)"], and the code window in the Message the second one?  [It's confusing because the code window looks "first" in the Message.]

 

And "doesn't work" is never enough information.  Does it not load?  Does it load but the command name is not recognized?  Does it accept the command but not do what you want?  What happens when you try to run it?  Nothing at all?  If it does some but not all, how far does it get?  Are there any message?  Etc., etc., etc.

Kent Cooper, AIA
0 Likes
Message 3 of 7

Baker_ADG
Advocate
Advocate
Well, i thought the paperclip meant attached. and the posted code was "below" Sorry for the confusion

as far as doesnt work when i load the lsp..
(LOAD "/CMLIST.lsp") ; error: ActiveX Server returned the error: unknown name: Layer
Jon Baker | Land Development & Infrastructure Design Manager
Alignment Design Group | Denver | Colorado
Civil 3D 2025 | Windows 11
0 Likes
Message 4 of 7

paullimapa
Mentor
Mentor

There are all kinds of unknown functions in CMLIST.lsp.  Even some of the vla functions are wrong. Looks like another AI generated code which is a mess.

The first error in the code is what you see because no function called vla-get-layer:

(setq layername (vla-get-layer activedocument))

But should be this:

(setq layername (vla-get-layers activedocument))

So ok, maybe a typo with the missing "s"...but then it gets worse with the next line of code:

(while layername (not (vlax-null layername))

Again no such function called vlax-null.

The whole while loop is a mess. Then there's errors with not converting the numbers to strings. What I would do here is loop through the layer list and get the properties like this with the lines of bad code commented out:

;  (while layername (not (vlax-null layername))
   (setq i 0)
   (repeat (vla-get-count layername)
    ; Get layer properties
;    (setq layerelement (vlax-get-object layername))
    (setq layerelement (vla-item layername i))
;    (setq color     (vlax-get-property layerelement 'Color))
    (setq color     (itoa (vlax-get-property layerelement 'Color)))
    (setq linetype  (vlax-get-property layerelement 'Linetype))
;    (setq lineweight (vlax-get-property layerelement 'Lineweight))
    (setq lineweight (itoa (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))
    (setq i (1+ i))
  )

From this point forward there are some custom functions that are not available like sort & string-split:

  ; Sort table content by layer name (using a custom sort function)
  (setq table (sort (string-split table "\n") 'layer-name-sort))

then there's an insert of a block which we also don't have called "1inch"

(command "INSERT" "1inch" (list 0 0 0) 1 1 0) ; Insert block "1inch" at origin with scale 1

Finally all down hill from here with more unknown functions like vlax-loaded? and vlax-doc-object:

  ; Try using table object (if supported)
  (if (and (vlax-loaded? 'Autodesk.AutoCAD.EditorInput)
           (vlax-doc-object activedocument 'Autodesk.AutoCAD.EditorInput.Table))

So instead of doing all the above, I'd look into Lee Mac's code on getting a table of the current drawing's layer properties here: https://www.lee-mac.com/layerextract.html

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 5 of 7

Baker_ADG
Advocate
Advocate
well, I tried Lee Mac's code. works just like a copy clip of the "Layers" flyout.

I'd like to use the LAYTABLE lisp to insert a table - but the lisp does not create the table in ABC order. so, if we can get that lisp to actually work 100% of the way...
Jon Baker | Land Development & Infrastructure Design Manager
Alignment Design Group | Denver | Colorado
Civil 3D 2025 | Windows 11
0 Likes
Message 6 of 7

Sea-Haven
Mentor
Mentor

Will try to find had something close and it would add a linetype also in the table. 

 

Its not exactly what you want but close. Good starting point.

Message 7 of 7

paullimapa
Mentor
Mentor

Awhile back I found and modified this lisp I call LyrTbl.lsp

It should generate a Table in AutoCAD that looks like this:

paullimapa_0-1721889050434.png

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos