AutoLISP: ViewPort Lock & Unlock

Here is another short but sweet LISP routine by Lee-Mac found at the AUGI forums.

This one is great because it does 4 functions. You can Lock or Unlock viewports byselecting them or you can Lock or Unlock all of the viewports throughout the drawing. I have been using the option to lock all of the viewports throughout the drawing before closing a drawing.

  • VPL – Lock selected viewport
  • VPU – Unlock selected viewport
  • VPLA – Lock ALL viewports
  • VPUA – Unlock ALL viewports


If you have a chance, check out Lee’s website. And if you find anything useful make a donation to his site. He make all of his LISP routines on his website free and he helps many people with their LISP coding questions & problems.

; By Lee-Mac found at the Augi forums

;; Lock Selected Viewport


(defun c:vpl nil

(if (SSVPLock (ssget "_+.:E:S:L" '((0 . "VIEWPORT"))) :vlax-true)

(princ "\n--> Viewport Locked.")




;; Unlock Selected Viewport

(defun c:vpu nil

(if (SSVPLock (ssget "_+.:E:S:L" '((0 . "VIEWPORT"))) :vlax-false)

(princ "\n--> Viewport Unlocked.")




;; Lock All Viewports

(defun c:vpla nil

(SSVPLock (ssget "_X" '((0 . "VIEWPORT"))) :vlax-true)

(princ "\n--> All Viewports Locked.")



;; Unlock All Viewports

(defun c:vpua nil ;; changed "VPLU" to "VPUA" to be consistant with the above function

(SSVPLock (ssget "_X" '((0 . "VIEWPORT"))) :vlax-false)

(princ "\n--> All Viewports UnLocked.")



(defun SSVPLock ( ss lock / i )

(if ss

(repeat (setq i (sslength ss))

(vla-put-displaylocked (vlax-ename->vla-object (ssname ss (setq i (1- i)))) lock) t




About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, Layout, Modifying, Viewports. Bookmark the permalink.

2 Responses to AutoLISP: ViewPort Lock & Unlock

  1. Julio says:

    I’ve been following Lee Mac’s work, and in one word AMAZING, many thanks for this one

  2. Baber Beg says:

    How would you add color to show an unlocked viewport i.e. have red color for an unlocked viewport? With these routines the viewport color stays the same whether it locked or unlocked.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s