lock all the viewports

lock all the viewports

mruPRQUJ
Advocate Advocate
1,829 Views
16 Replies
Message 1 of 17

lock all the viewports

mruPRQUJ
Advocate
Advocate

Hi there,

Could you please help me to write a lisp to lock all the viewports (display lock) in one drawing for multiple layouts? Please see attached drawing below for your reference, thank you very much in advance! 🙂

0 Likes
Accepted solutions (2)
1,830 Views
16 Replies
Replies (16)
Message 2 of 17

Sea-Haven
Mentor
Mentor
Accepted solution

Try this

(defun c:lll ( / layouts lay vs x)
(setq layouts (layoutlist))
(foreach lay layouts
  (setvar 'ctab lay)
  (command "Pspace")
  (setq vs (ssget "X" (list (cons 0 "Viewport")(cons 410 (getvar 'ctab)))))
  (repeat (setq x (sslength vs))
    (setq vp (vlax-ename->vla-object (ssname vs (setq x (1- x)))))
    (vlax-put vp 'displaylocked 1)
  )
)
(princ)
)
Message 3 of 17

mruPRQUJ
Advocate
Advocate

Hi there,

 

It works perfect, thank you very much! 🙂

0 Likes
Message 4 of 17

LDShaw
Collaborator
Collaborator

Late to the party.

 

 

;;; Use 'c:mvunlock' to unlock all viewports and 'c:mvlock' to lock all viewports.
;;;
;;; Usage:
;;; 1. Load the Lisp routine.
;;; 2. Run the command "MVUNLOCK" to unlock all viewports.
;;; 3. Run the command "MVLOCK" to lock all viewports.
;;;
;;; Parameters:
;;; None
;;;
;;; Returns:
;;; None
;;;
;;; Notes:
;;; - This routine modifies viewport locking settings. Use with caution.
;;;
;;; ---------------------------- Main program --------------------------------;



(defun c:mvunlock ()
  (command "tilemode" "0")
  (command "mview" "lock" "off" "all" "")
)


(defun c:mvlock ()
  (command "tilemode" "0")
  (command "mview" "lock" "on" "all" "")
)

 

We redefine our save and add (mvl) this too.

 

Message 5 of 17

mruPRQUJ
Advocate
Advocate

Hi there,

 

Could you please outline the main difference between them? Many thanks. 🙂

0 Likes
Message 6 of 17

LDShaw
Collaborator
Collaborator

In a nutshell. His is more versatile and locks multiple layouts in more than one paperspace. 
Mine may be a little faster and is better (debatable.) for single paperspace. 

I may update mine to his. I have to think about it.


Message 7 of 17

mruPRQUJ
Advocate
Advocate

Hi there,

could you please clarify yours can lock multiple vport in multiple layouts?Many thanks.

0 Likes
Message 8 of 17

LDShaw
Collaborator
Collaborator

It can't it only does one layout but all the views in that layout.

The more I am thinking about it the more I think his way is superior. 
I am going to update my lisp to his.

Sigh. Teach me to come up here.


 

0 Likes
Message 9 of 17

LDShaw
Collaborator
Collaborator
Accepted solution

Thanks. @Sea-Haven I updated my routines to match yours better.

 

;;; Use 'c:mvunlock' to unlock all viewports and 'c:mvlock' to lock all viewports.
;;;
;;; Updated 2024-08-28 
;;; Thanks to Sea-Haven for his help
;;; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lock-all-the-viewports/m-p/12982770#M470837
;;; Usage:
;;; 1. Load the Lisp routine.
;;; 2. Run the command "MVUNLOCK" to unlock all viewports.
;;; 3. Run the command "MVLOCK" to lock all viewports.
;;;
;;; Parameters:
;;; cmdsave vs x vp lay
;;;
;;; Returns:
;;; None
;;;
;;; Notes:
;;; - This routine modifies viewport locking settings. Use with caution.
;;;
;;; ---------------------------- Main program --------------------------------;

(defun c:mvunlock (/ cmdsave vs x vp lay)
  ;; Save current cmdecho value and turn off command echoing
  (setq cmdsave (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  ;; Loop through each layout and unlock viewports
  (foreach lay (layoutlist)
    (setvar 'ctab lay)
    (command "Pspace")
    (setq vs (ssget "X" (list (cons 0 "VIEWPORT") (cons 410 (getvar 'ctab)))))
    (repeat (setq x (sslength vs))
      (setq vp (vlax-ename->vla-object (ssname vs (setq x (1- x)))))
      (vlax-put vp 'displaylocked 0)  ; Unlock viewport
    )
  )
  ;; Restore cmdecho value
  (setvar "cmdecho" cmdsave)
  (princ)
)

(defun c:mvlock (/ cmdsave vs x vp lay)
  ;; Save current cmdecho value and turn off command echoing
  (setq cmdsave (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  ;; Loop through each layout and lock viewports
  (foreach lay (layoutlist)
    (setvar 'ctab lay)
    (command "Pspace")
    (setq vs (ssget "X" (list (cons 0 "VIEWPORT") (cons 410 (getvar 'ctab)))))
    (repeat (setq x (sslength vs))
      (setq vp (vlax-ename->vla-object (ssname vs (setq x (1- x)))))
      (vlax-put vp 'displaylocked 1)  ; Lock viewport
    )
  )
  ;; Restore cmdecho value
  (setvar "cmdecho" cmdsave)
  (princ)
)

 

and 

 

;; qsave

(COMMAND "UNDEFINE" "QSAVE")
(defun C:QSAVE (/ cmdsave vs x vp)
  (setq cmdsave (getvar "cmdecho"))
(setvar "cmdecho" 0)					  
  (foreach lay (layoutlist)
    (setvar 'ctab lay)
    (command "Pspace")
    (setq vs (ssget "X" (list (cons 0 "VIEWPORT") (cons 410 (getvar 'ctab)))))
    (repeat (setq x (sslength vs))
      (setq vp (vlax-ename->vla-object (ssname vs (setq x (1- x)))))
      (vlax-put vp 'displaylocked 1)
    )
  )
  (COMMAND "REDEFINE" "QSAVE")
  (COMMAND "QSAVE")
  (setvar "cmdecho" cmdsave)
  (princ)
)

(COMMAND "UNDEFINE" "SAVE")
(defun C:SAVE (/ cmdsave vs x vp)
  (setq cmdsave (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  (foreach lay (layoutlist)
    (setvar 'ctab lay)
    (command "Pspace")
    (setq vs (ssget "X" (list (cons 0 "VIEWPORT") (cons 410 (getvar 'ctab)))))
    (repeat (setq x (sslength vs))
      (setq vp (vlax-ename->vla-object (ssname vs (setq x (1- x)))))
      (vlax-put vp 'displaylocked 1)
    )
  )																  
  (COMMAND "REDEFINE" "SAVE")
  (COMMAND "SAVE")
  (setvar "cmdecho" cmdsave)
  (princ)
)

 

though the qsave is not consistently working. 



 

Message 10 of 17

mruPRQUJ
Advocate
Advocate

Hi there,

First part works great! Could you please advise me how the second part works? which command should be run? many thanks. 🙂

0 Likes
Message 11 of 17

LDShaw
Collaborator
Collaborator

Sorry that second part was an easter egg showing Sea-Haven I heavily used his work. (Ouch I forgot to give credit.) will fix that. 
I put that in my startup lisp so my save and qsave are redefined to automatically lock the views for my users. This way they don't keep messing up the scales. Since we only use one paperspace it did not matter but as of late people are not adhering to standards so having any view in any random paperspace they may create lock may be a good thing. 

Message 12 of 17

mruPRQUJ
Advocate
Advocate

many thanks! 🙂

Message 13 of 17

Sea-Haven
Mentor
Mentor

Glad it worked out, one of those quick answers.

 

 

 

(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(setq ans (ah:butts 2 "h"  '("Lock or unlock " "Lock" "Unlock"))) ; ans holds the button picked value

 

 

SeaHaven_0-1724907981912.png

 

You dont need to redefine the save close etc you can set up a reactor that will trap those commands then run your lock code. Add to say Appload start up suite.

Just the relevant bit of code 

 

 

;Reactor callback function
(defun BeginCloseFunc (reactor lst / blocks blk efname )
(if (not _BeginCloseReactor) (setq _BeginCloseReactor (VLR-Dwg-Reactor nil '((:VLR-beginClose . BeginCloseFunc)))))
(if (not _BeginSaveReactor ) (setq _BeginSaveReactor  (VLR-Dwg-Reactor nil '((:VLR-beginSave  . BeginCloseFunc)))))
..........do stuff with blocks
..........
(cond
((= (vlr-current-reaction-name) ':VLR-beginSave) (Princ "\nThis function has been triggered by a Document Save event."))
((= (vlr-current-reaction-name) ':VLR-beginClose)(princ "\nThis function has been triggered by a Document Close event."))
)

 

The vlr code thanks to others here.

 

 

Message 14 of 17

mruPRQUJ
Advocate
Advocate

Hi there,

 

Neither of them works. I may make a mistake. Could you please provide some advice to me? Please see attached DWG file for your reference, many thanks.

0 Likes
Message 15 of 17

Sea-Haven
Mentor
Mentor

Are you talking about my last post ? The radio buttons is just a way of asking for on/off you need to add to code.

 

The rector code is just a start example of using a reactor based on close and save commands. You need to add the code about lock viewports. 

Message 16 of 17

mruPRQUJ
Advocate
Advocate

Yes, it is your last post.

Could you please advise me where to add the codes? Is the space needed between the two parts? many thanks.

0 Likes
Message 17 of 17

Sea-Haven
Mentor
Mentor

Try this make sure that Multi radio buttons is saved in a support directory so will load. It has one bug in that lock or unlock is asked twice will try to find out why.

 

 

;;; Use 'c:mvunlock' to unlock or lock all viewports 
;;;
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
;;; ---------------------------- Main program --------------------------------;

(defun c:mvunlock (onoff / cmdsave vs x vp lay)
  ;; Save current cmdecho value and turn off command echoing
  (setq cmdsave (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  ;; Loop through each layout and unlock viewports
  (foreach lay (layoutlist)
    (setvar 'ctab lay)
    (command "Pspace")
    (setq vs (ssget "X" (list (cons 0 "VIEWPORT") (cons 410 (getvar 'ctab)))))
    (repeat (setq x (sslength vs))
      (setq vp (vlax-ename->vla-object (ssname vs (setq x (1- x)))))
      (vlax-put vp 'displaylocked onoff)  ; Unlock viewport
    )
  )
  ;; Restore cmdecho value
  (setvar "cmdecho" cmdsave)
  (princ)
)

;Reactor callback function
(defun BeginCloseFunc (reactor lst / blocks blk )
(if (not _BeginCloseReactor) (setq _BeginCloseReactor (VLR-Dwg-Reactor nil '((:VLR-beginClose . BeginCloseFunc)))))
(if (not _BeginSaveReactor ) (setq _BeginSaveReactor  (VLR-Dwg-Reactor nil '((:VLR-beginSave  . BeginCloseFunc)))))
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(setq ans (ah:butts 1 "h"  '("Lock or unlock " "Lock" "Unlock"))) ; ans holds the button picked value
(if (= ans "Lock")
(c:mvunlock 1)
(c:mvunlock 0)
)


(cond
((= (vlr-current-reaction-name) ':VLR-beginSave) (Princ "\nThis function has been triggered by a Document Save event."))
((= (vlr-current-reaction-name) ':VLR-beginClose)(princ "\nThis function has been triggered by a Document Close event."))
)
(princ)
) ; end defun

(if (not _BeginCloseReactor) (setq _BeginCloseReactor (VLR-Dwg-Reactor nil '((:VLR-beginClose . BeginCloseFunc)))))
(if (not _BeginSaveReactor ) (setq _BeginSaveReactor  (VLR-Dwg-Reactor nil '((:VLR-beginSave  . BeginCloseFunc)))))

 

 I would load it via Appload and add to startup suite. 

0 Likes