Alphabetically Sort List of Layers in a drawing

Alphabetically Sort List of Layers in a drawing

betsy.kirtland
Enthusiast Enthusiast
928 Views
2 Replies
Message 1 of 3

Alphabetically Sort List of Layers in a drawing

betsy.kirtland
Enthusiast
Enthusiast

Hi all,

I have a lisp routine that retrieves a list of all the layers in the active drawing using the following code, stolen entirely from Afralisp (https://www.afralisp.net/visual-lisp/tutorials/visual-lisp-and-layers.php😞

(setq acadobject (vlax-get-Acad-Object))
(setq activedocument (vla-get-activedocument acadobject))
(setq LayerTable (vla-get-layers activedocument))
(vlax-for each LayerTable
    (setq LayerList (cons (vla-get-Name each) LayerList))
    );end vlax-for

 

The trouble is that the resulting list in a seeming random order.  The list is going to be used to populate a DCL popup_list, so I'd like the list to be in alphabetical order.

 

I tried the following, but as you see, I get an error:

_$ (vl-sort LayerList (function (lambda (s1 s2) (< (vl-symbol-name s1) (vl-symbol-name s2)))))
; error: bad argument type: symbolp "GE-TEXT-LABL"

 

I feel like there has to be an easy way to do this... or is it more complicated than I realize??  Any help would be greatly appreciated!

0 Likes
Accepted solutions (1)
929 Views
2 Replies
Replies (2)
Message 2 of 3

Kent1Cooper
Consultant
Consultant
Accepted solution

If LayerList is a list of text strings, just:

(vl-sort LayerList '<)

 

EDIT:  The making of that list of Layer names can be simpler -- no need to get the Acad object and the active document, generate a table of Layers and then step through that to extract the names, etc.  As long as 'lay' is a localized variable so for purposes of the routine it will always start as nil:

 

(while (setq lay (tblnext "layer" (not lay)))

  (setq LayerList (cons (cdr (assoc 2 lay)) LayerList))

)

Kent Cooper, AIA
Message 3 of 3

betsy.kirtland
Enthusiast
Enthusiast

Thank you @Kent1Cooper !  I also realized that acad_strlsort would also accomplish the alphabetical sorting without the need for an anonymous function. 

0 Likes