Jump to content

Copytolayer command for locked layers (eOnLockedLayer error)


Recommended Posts

Posted

I want to be able to copy a locked layer to a new one with the COPYTOLAYER command.

 

I've tried to get around this by unlocking the layers before, then after the COPYTOLAYER command.

 

This will throw an error like so when I try to copy a locked layer:

image.png.8f0c3b82164395ffa3e5de2b6e511723.png

 

Here's the code:

(vl-load-com)

;; PROBLEM
;; AutoCAD gives unhandled exception error with eOnLockedLayer.

;; TO ADD
;; Change this to have undo handling

(defun c:Lay_Copy_Locked (/ layUlist laydata ss)

  (setq doc (vla-get-ActiveDocument (vlax-get-Acad-Object)))

  ; Unlock all layers - https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/unlocking-layers-is-painfully-slow/m-p/2881866#M294136
  (vlax-for item (vla-get-layers doc)
    (if (= (vlax-get-property item "Lock") :vlax-true)
      (progn
        (setq Locked_Layers (cons item Locked_Layers)) ; set variable "locked_layers" to all locked layers.
        (vlax-put-property item "Lock" :vlax-false) ; unlock each layer if it is locked.
      ) ; end progn
    ) ; end if
  ) ; end vlax

  ;; Copy to layer
  (if (setq ss (ssget "_:L"))
    (command "._-copytolayer" ss "")
  ) ; end if

  ; If any layers were locked at the beginning, lock them again. -https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/unlocking-layers-is-painfully-slow/m-p/2881866#M294136
  (if Locked_Layers
    (mapcar '(lambda (x) (vlax-put-property x "Lock" :vlax-true)) Locked_Layers)
  ) ; end if

  (princ)
) ; defun

(c:Lay_Copy_Locked)

 

Posted
(defun c:Lay_Copy_Locked ( / *error* layUlist laydata ss )

  (vl-load-com)

  (defun *error* ( m )
    (if command-s
      (command-s "_.UNDO" "_E")
      (vl-cmdf "_.UNDO" "_E")
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  (setq doc (vla-get-ActiveDocument (vlax-get-Acad-Object)))

  (if (= 8 (logand 8 (getvar 'undoctl)))
    (if command-s
      (command-s "_.UNDO" "_E")
      (vl-cmdf "_.UNDO" "_E")
    )
  )
  (if command-s
    (command-s "_.UNDO" "_G")
    (vl-cmdf "_.UNDO" "_G")
  )

  ; Unlock all layers - https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/unlocking-layers-is-painfully-slow/m-p/2881866#M294136
  (vlax-for item (vla-get-layers doc)
    (if (= (vlax-get-property item "Lock") :vlax-true)
      (progn
        (setq Locked_Layers (cons item Locked_Layers)) ; set variable "locked_layers" to all locked layers.
        (vlax-put-property item "Lock" :vlax-false) ; unlock each layer if it is locked.
      ) ; end progn
    ) ; end if
  ) ; end vlax

  ;; Copy to layer
  (if (setq ss (ssget "_:L"))
    (command "._-copytolayer" ss "")
  ) ; end if

  ; If any layers were locked at the beginning, lock them again. -https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/unlocking-layers-is-painfully-slow/m-p/2881866#M294136
  (if Locked_Layers
    (mapcar '(lambda (x) (vlax-put-property x "Lock" :vlax-true)) Locked_Layers)
  ) ; end if

  (*error* nil)
) ; defun

 

  • Thanks 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...