Lsp to turn on selected object's layer

Lsp to turn on selected object's layer

aitorm
Advocate Advocate
513 Views
3 Replies
Message 1 of 4

Lsp to turn on selected object's layer

aitorm
Advocate
Advocate

Hi!

 

There is an operation that I keep repeating and that would love to optimize. However, I am not very good at programing with lisp so I would like to ask you for some help. I would like Autocad to turn every layer on, so I can choose a layer, then return to the previous layer settings and then turn on selected layer. How would I approach this? Probably doing something like this:

 

  • Turn All Layers On: It makes all layers visible using the LAYON command.
  • Select an Object: The user selects an object, and the layer name of that object is retrieved.
  • Save object's layer: The layer name of that object is saved as "target".
  • Restore Layer Settings: Restores the layer states to what they were before turning them all on using the LAYERP command.
  • Turn "target" Layer On: The selected object's layer is turned on.
  • EXTRA: If the layer is already on, then say: "Caution! Layer was already on".

Should I try to write LSP code? Ok, I did have a conversation with chatgpt and tried to guide him,  but I don't get him to do it right!

(defun c:LayerOn (/ selObj layerName)
  ;; Turn all layers ON (but do not unfreeze them)
  (command "_.LAYON")
  
  ;; Prompt the user to select an object
  (princ "\nSelect an object to keep its layer on: ")
  (setq selObj (entsel))
  
  (if selObj
    (progn
      ;; Get the layer name of the selected object
      (setq layerName (cdr (assoc 8 (entget (car selObj)))))
      
      ;; Restore the previous layer settings
      (command "_.LAYERP")
      
      ;; Turn on the selected layer if it was off
I DON'T KNOW HOW TO DO THIS
      
      ;; Confirm the action
      (princ (strcat "\nLayer " layerName " is now visible."))
    )
    ;; If no object is selected
    (princ "\nNo object selected.")
  )
  
  (princ)
)


Thanks a lot

 

0 Likes
Accepted solutions (2)
514 Views
3 Replies
Replies (3)
Message 2 of 4

ВeekeeCZ
Consultant
Consultant
Accepted solution

Try this routine that I have used for years. 

Message 3 of 4

aitorm
Advocate
Advocate
SOOO GOOD!!

Cool that you can choose several layers.

Thanks!!!
0 Likes
Message 4 of 4

Kent1Cooper
Consultant
Consultant
Accepted solution

Another is LayerThawOnSelect.lsp, >here<.  It shows you which Layers were frozen and/or off [not just those that were off], by highlighting, and when you pick something on one of those Layers, unhighlights everything on it.

Kent Cooper, AIA