LISP - layers draw order

LISP - layers draw order

clifford.c
Contributor Contributor
1,648 Views
5 Replies
Message 1 of 6

LISP - layers draw order

clifford.c
Contributor
Contributor

I was looking for a lisp the automatically change the draw order of my layers. I found one but I still need to manually change the draw order of each layer. so I'm planning to start writing a LISP, while I don't have experience in LISP, hopefully my background in VBA helps. 

This is what in my mind

let's say I have five layers (layer 1, layer 2, layer 3, layer 4 & layer 5)

I want a lisp that every time I run it, it will arrange the draw order of the layer in these order

Layer 3

Layer 2

Layer 5

Layer 1

Layer 4

 

can anyone show my how to write this code? Thank you

 

 

0 Likes
Accepted solutions (3)
1,649 Views
5 Replies
Replies (5)
Message 2 of 6

clifford.c
Contributor
Contributor

So I use chatGPT to create a code. 😆 does not work. of course 🤣😂

 

(defun c:GPTorder ()
(setq layer-order '("Line" "Grass" "Road")) ; Set the desired layer order
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(setq layer-col (vla-get-layers doc))
(setq layer-list (vl-sort (mapcar 'vla-get-name layer-col) 'string<))
(setq layer-id-list (mapcar 'vla-get-objectid (vlax-invoke layer-col 'getitem layer-list)))
(setq sorted-layer-id-list (mapcar (lambda (x) (nth (1- (position (vla-get-name x) layer-list)) layer-id-list)) (sort layer-col '< :key (lambda (x) (position (vla-get-name x) layer-order))))) ; Sort the layer IDs according to the desired order
(vla-put-layers doc (vlax-make-safearray vlax-vbObject (reverse sorted-layer-id-list))) ; Set the new layer order
(vla-regen doc acAllViewports) ; Regenerate the drawing to update the display
(princ))

 

appreciate if anyone could help.

0 Likes
Message 3 of 6

ronjonp
Mentor
Mentor

HERE is a good place to start.

Message 4 of 6

Kent1Cooper
Consultant
Consultant
Accepted solution

Something as simple as this?

(defun C:LAYORD ()
  (foreach lay '("Layer4" "Layer1" "Layer5" "Layer2" "Layer3"); top-most last
    (command "_.draworder" (ssget "_X" (list (cons 8 lay))) "" "_front")
  )
  (prin1)
)

It assumes this is all happening in Model space, and you're in Model space at the time, and none of the Layers are locked.

Kent Cooper, AIA
Message 5 of 6

ronjonp
Mentor
Mentor
Accepted solution

@Kent1Cooper A simple IF will keep it from bombing if no selection is made.

(defun c:layord	(/ s)
  (foreach lay '("Layer4" "Layer1" "Layer5" "Layer2" "Layer3") ; top-most last
    (if	(setq s (ssget "_X" (list (cons 8 lay))))
      (command "_.draworder" s "" "_front")
    )
  )
  (prin1)
)

 

Message 6 of 6

clifford.c
Contributor
Contributor
Accepted solution
THANK YOU SO MUCH
0 Likes