View Full Version : Locking Viewports
Cad4men
2004-07-28, 03:03 AM
Does anyone have available a routine that will automatically lock all paperspace viewports upon opening a drawing file, regardless of the quantity or whether the drawing was previously saved in model space.
I would like to load it in acad2004doc.lsp, as this file loads from our server, across all cad stations.
Thanks,
Jerry
Jeff_M
2004-07-28, 05:04 AM
Add this to the end of your lisp file....
(defun vplock (yesno / ss lock x obj)
(vl-load-com)
(if (setq ss (ssget "X" '((0 . "VIEWPORT")(-4 . "/=")(69 . 1))))
(progn
(if (= "y" yesno) (setq lock :vlax-true)(setq lock :vlax-false))
(setq x 0)
(while (< x (sslength ss))
(setq obj (vlax-ename->vla-object (ssname ss x)))
(vla-put-displaylocked obj lock)
(setq x (1+ x))
)
)
)
(princ)
)
(vplock "y")
HTH
peter
2004-07-28, 11:21 AM
Here are my versions
You may have to change the method of selecting the objects with the acaddoc.lsp. If you get errors from these let me know.
Peter Jamtgaard
(defun C:LockVP (/ intCount ssSelections)
(setq ssSelections (ssget "x" (list (cons 0 "VIEWPORT"))))
(repeat (setq intCount (sslength SSET))
(vla-put-DisplayLocked (vlax-ename->vla-object
(ssname ssSelections
(setq intCount (1- intCOunt))))
:vlax-true
)
)
(prin1)
)
(defun C:UnlockVP (/ intCount ssSelections)
(setq ssSelections (ssget "x" (list (cons 0 "VIEWPORT"))))
(repeat (setq intCount (sslength SSET))
(vla-put-DisplayLocked (vlax-ename->vla-object
(ssname ssSelections
(setq intCount (1- intCOunt))))
:vlax-false
)
)
(prin1)
)
CAB2k
2004-07-28, 12:30 PM
Here is one that setsthe vp to red when locked & green when unlocked.
(defun c:vpunlockall () ; 06/07/04
(vl-load-com)
(vlax-for lay
(vla-get-layouts
(vla-get-activedocument
(vlax-get-acad-object)
)
)
(if (eq :vlax-false (vla-get-modeltype lay))
(vlax-for ent (vla-get-block lay) ; for each ent in layout
(if (= (vla-get-objectname ent) "AcDbViewport")
(progn
(vla-put-displaylocked ent :vlax-false)
(vla-put-color ent 3); 3 green
)
)
)
)
)
)
(defun c:vplockall () ; 06/07/04
(vl-load-com)
(vlax-for lay
(vla-get-layouts
(vla-get-activedocument
(vlax-get-acad-object)
)
)
(if (eq :vlax-false (vla-get-modeltype lay))
(vlax-for ent (vla-get-block lay) ; for each ent in layout
(if (= (vla-get-objectname ent) "AcDbViewport")
(progn
(vla-put-displaylocked ent :vlax-true)
(vla-put-color ent 1);1 red
)
)
)
)
)
)
gfreddog
2010-07-27, 05:00 PM
Here is one that setsthe vp to red when locked & green when unlocked.
(defun c:vpunlockall () ; 06/07/04
(vl-load-com)
(vlax-for lay
(vla-get-layouts
(vla-get-activedocument
(vlax-get-acad-object)
)
)
(if (eq :vlax-false (vla-get-modeltype lay))
(vlax-for ent (vla-get-block lay) ; for each ent in layout
(if (= (vla-get-objectname ent) "AcDbViewport")
(progn
(vla-put-displaylocked ent :vlax-false)
(vla-put-color ent 3); 3 green
)
)
)
)
)
)
(defun c:vplockall () ; 06/07/04
(vl-load-com)
(vlax-for lay
(vla-get-layouts
(vla-get-activedocument
(vlax-get-acad-object)
)
)
(if (eq :vlax-false (vla-get-modeltype lay))
(vlax-for ent (vla-get-block lay) ; for each ent in layout
(if (= (vla-get-objectname ent) "AcDbViewport")
(progn
(vla-put-displaylocked ent :vlax-true)
(vla-put-color ent 1);1 red
)
)
)
)
)
)
Very kewl. Great help CAB
BlackBox
2010-07-27, 06:56 PM
Jerry,
This will do exactly what you specified in the OP... it's fast, and looks good doing it! 8)
Try this:
(defun c:TEST (/ ss)
(vl-load-com)
(cond
(*activeDoc*)
((setq *activeDoc* (vla-get-activedocument (vlax-get-acad-object)))))
(if (setq ss (ssget "_x" '((0 . "VIEWPORT"))))
(progn
(vlax-for each (setq ss (vla-get-activeselectionset *activeDoc*))
(if (= :vlax-false (vla-get-displaylocked each))
(vla-put-displaylocked each :vlax-true)))
(vla-delete ss)))
(princ)) ;_end defun
(if (= 1 (getvar 'dwgtitled)) ; If dwg is titled
(c:TEST)) ; Autorun
The method included here is most similar to Peter's routines, but a keen eye will reveal some subtle differences. As always, use what works best for your situation, from all of the great offerings here.
Also, the blue lines of code are there for your bennefit. I include them already in my ACADDOC.lsp, so if you do something similar, you can remove them accordingly.
I too use a toggle for working purposes, but there have already been several offerings in that regard.
Cheers! :beer:
alanjt
2010-07-27, 07:14 PM
Here's mine. Gives option to lock or unlock all.
(defun c:LV (/ ss opt)
;; Lock/Unlock all viewports
;; Alan J. Thompson, 09.10.09
(if (and (setq ss (ssget "_X" '((0 . "VIEWPORT"))))
(not (initget 0 "Yes No"))
(setq opt (cond ((getkword "\nLock Viewports? [Yes/No] <Yes>: "))
("Yes")
)
)
)
(progn
(vlax-for x (setq ss (vla-get-activeselectionset
(cond (*AcadDoc*)
((setq *AcadDoc* (vla-get-activedocument
(vlax-get-acad-object)
)
)
)
)
)
)
(vla-put-displaylocked
x
(if (eq opt "Yes")
:vlax-true
:vlax-false
)
)
)
(vla-delete ss)
(and (zerop (getvar 'tilemode)) (vla-put-mspace *AcadDoc* :vlax-false))
(princ (strcat "\nAll Viewports have been "
(if (eq opt "Yes")
"Locked!"
"Unlocked!"
)
)
)
)
(alert "\nNo viewports in drawing.")
)
(princ)
)
aaronic_abacus
2010-07-27, 09:36 PM
This autolisp program turns on all view ports.
(DEFUN C:VPO ()
(PROMPT "\n*VEIWPORTS ON(all)* ")
(SETQ TMV (GETVAR "TILEMODE"))
(SETVAR "TILEMODE" 1)
(SETQ VPS (SSGET "X" '((0 . "VIEWPORT")) ))
(SETVAR "TILEMODE" TMV)
(SETQ VPSL (SSLENGTH VPS))
(SETQ CT (- VPSL 1))
(SETQ LP 1)
(WHILE LP
(SETQ VPEN (SSNAME VPS CT))
(SETQ VPENL (ENTGET VPEN))
(SETQ VPENLN (CDR (ASSOC 410 VPENL)))
(COMMAND "LAYOUT" "SET" VPENLN)
(COMMAND "MVIEW" "ON" "ALL" "")
(SETQ CT (- CT 1))
(IF (< CT 0) (SETQ LP NIL))
);END LP
(COMMAND "REGENALL")
(PRINC)
);END VPO
BlackBox
2010-07-27, 09:59 PM
This autolisp program turns on all view ports.
(DEFUN C:VPO (/ tmv vps vpsl ct lp vpen vpenl vpenln)
(setvar 'cmdecho 0)
(PROMPT "\n*VEIWPORTS ON(all)* ")
(SETQ TMV (GETVAR "TILEMODE"))
(SETVAR "TILEMODE" 1)
(SETQ VPS (SSGET "X" '((0 . "VIEWPORT")) ))
(SETVAR "TILEMODE" TMV)
(SETQ VPSL (SSLENGTH VPS))
(SETQ CT (- VPSL 1))
(SETQ LP 1)
(WHILE LP
(SETQ VPEN (SSNAME VPS CT))
(SETQ VPENL (ENTGET VPEN))
(SETQ VPENLN (CDR (ASSOC 410 VPENL)))
(COMMAND "._LAYOUT" "SET" VPENLN)
(COMMAND "._MVIEW" "ON" "ALL" "")
(SETQ CT (- CT 1))
(IF (< CT 0) (SETQ LP NIL))
);END LP
(COMMAND "._REGENALL")
(setvar 'cmdecho 1)
(PRINC)
);END VPO
There are many ways of accomplishing the same goal (as this thread demonstrates).
However, I am curious... in choosing your particular method, is there a reason for not localizing variables, avoiding the "._" or "_." command name prefix, and avoiding the 'cmdecho sysvar? :confused:
aaronic_abacus
2010-07-27, 11:01 PM
I'm just lazy.
Good edit, definitely needs to list the local variables so it doesn't conflict with other programs global variables, cmdecho so people don't see all the commands echoing, and ._ so it works with international autocad.
alanjt
2010-07-28, 03:41 AM
Mat, you really should store the cmdecho variable beforehand, instead of assuming it should be set back to 1. I know many people (including myself) that set cmdecho to 0 on startup.
BlackBox
2010-07-28, 01:17 PM
Mat, you really should store the cmdecho variable beforehand, instead of assuming it should be set back to 1. I know many people (including myself) that set cmdecho to 0 on startup.
Hmmm... I hear what you're saying, but...
I'm not going to change my programming practices just because they're slightly difficult for beginners to comprehend. However, you have given sound advice and from now on, I will try and make my examples easier to comprehend.
I'm just saying... See what I did just there? Yeah, I'm fiesty today. :lol::razz:
You may very well be correct, Alan... I use a setting of 1 as a default due to my employer's standards (at least my old one, not sure what the new one's standards are yet? We got bought out).
alanjt
2010-07-28, 01:31 PM
Hmmm... I hear what you're saying, but...
I'm just saying... See what I did just there? Yeah, I'm fiesty today. :lol::razz:
You may very well be correct, Alan... I use a setting of 1 as a default due to my employer's standards (at least my old one, not sure what the new one's standards are yet? We got bought out).
There's a difference there. I'm taking a different road to the same destination. You are choosing the destination for the user. It's not a matter of preference, it's a matter of good programming practice to reset everything back as it was, not as you think it should be.;)
BlackBox
2010-07-28, 02:08 PM
There's a difference there. I'm taking a different road to the same destination. You are choosing the destination for the user. It's not a matter of preference, it's a matter of good programming practice to reset everything back as it was, not as you think it should be.;)
Alan,
You have given sound advice, and from now on, I will try to provide better examples of good programming practices.
alanjt
2010-07-28, 02:11 PM
Alan,
You have given sound advice, and from now on, I will try to provide better examples of good programming practices.I hope I didn't offend.
BlackBox
2010-07-28, 03:01 PM
I hope I didn't offend.
One who is offended by truth, has no place among those who seek wisdom.
"I prefer clarity over agreement."
"First tell the truth, then give your opinion."
- Dennis Prager
Like it or not, your observations are correct, my friend. ;)
alanjt
2010-07-28, 03:02 PM
One who is offended by truth, has no place among those who seek wisdom.
Like it or not, your observations are correct, my friend. ;)Well said. I just wanted to make sure. I'm happy to accept criticism, but I'm also quick to give it - isn't why we come to these forums?
BlackBox
2010-07-28, 03:16 PM
... isn't why we come to these forums?
In deed it is.
You've simply challenged me to meet a higher standard, and if I succeed, I will have only increased my overall potential. As a force-multiplier; the more capable I am, the more effective I can be.
How's this for profundity -
"A professional developer is an amateur who didn’t quit."
alanjt
2010-07-28, 03:19 PM
In deed it is.
You've simply challenged me to meet a higher standard, and if I succeed, I will have only increased my overall potential. As a force-multiplyer; the more capable I am, the more effective I can be.
How's this for profundity -
"A professional developer is an amateur who didn’t quit."Typo and all. :lol:
BlackBox
2010-07-28, 03:37 PM
Typo and all. :lol:
What's life without a little levity. :Oops:
...I have a lot of heart, equally fat fingers, and a small keyboard. :p
abuv_all
2011-06-15, 02:34 PM
Here's mine. Gives option to lock or unlock all.
(defun c:LV (/ ss opt)
;; Lock/Unlock all viewports
;; Alan J. Thompson, 09.10.09
(if (and (setq ss (ssget "_X" '((0 . "VIEWPORT"))))
(not (initget 0 "Yes No"))
(setq opt (cond ((getkword "\nLock Viewports? [Yes/No] <Yes>: "))
("Yes")
)
)
)
(progn
(vlax-for x (setq ss (vla-get-activeselectionset
(cond (*AcadDoc*)
((setq *AcadDoc* (vla-get-activedocument
(vlax-get-acad-object)
)
)
)
)
)
)
(vla-put-displaylocked
x
(if (eq opt "Yes")
:vlax-true
:vlax-false
)
)
)
(vla-delete ss)
(and (zerop (getvar 'tilemode)) (vla-put-mspace *AcadDoc* :vlax-false))
(princ (strcat "\nAll Viewports have been "
(if (eq opt "Yes")
"Locked!"
"Unlocked!"
)
)
)
)
(alert "\nNo viewports in drawing.")
)
(princ)
)
When I run this lisp.. I get this:
Lock Viewports? [Yes/No] <Yes>:
; error: no function definition: VLAX-GET-ACAD-OBJECT
What am I missing? I don't lisp...I just borrow from others.thanks
alanjt
2011-06-15, 02:41 PM
Ahh, I have (vl-load-com) in my startup file, so I forget to add it to code some times. Just add (vl-load-com) to the beginning of the code and try again.
abuv_all
2011-06-15, 02:43 PM
Ahh, I have (vl-load-com) in my startup file, so I forget to add it to code some times. Just add (vl-load-com) to the beginning of the code and try again.
??? you mean just copy and paste (vl-load-com) at the top of the code?
alanjt
2011-06-15, 02:44 PM
??? you mean just copy and paste (vl-load-com) at the top of the code?
correct.....
abuv_all
2011-06-15, 02:46 PM
correct.....
(defun c:VPL (/ ss opt)
;; Lock/Unlock all viewports
;; Alan J. Thompson, 09.10.09
(vl-load-com)(if (and (setq ss (ssget "_X" '((0 . "VIEWPORT"))))
(not (initget 0 "Yes No"))
(setq opt (cond ((getkword "\nLock Viewports? [Yes/No] <Yes>: "))
("Yes")
)
)
)
(progn
(vlax-for x (setq ss (vla-get-activeselectionset
(cond (*AcadDoc*)
((setq *AcadDoc* (vla-get-activedocument
(vlax-get-acad-object)
)
)
)
)
)
)
(vla-put-displaylocked
x
(if (eq opt "Yes")
:vlax-true
:vlax-false
)
)
)
(vla-delete ss)
(and (zerop (getvar 'tilemode)) (vla-put-mspace *AcadDoc* :vlax-false))
(princ (strcat "\nAll Viewports have been "
(if (eq opt "Yes")
"Locked!"
"Unlocked!"
)
)
)
)
(alert "\nNo viewports in drawing.")
)
(princ)
)
thanks for the help alanjt....it runs correctly now.
alanjt
2011-06-15, 02:54 PM
Yes, that would be fine...
(defun c:LV (/ ss)
;; Lock/Unlock all viewports
;; Alan J. Thompson, 09.10.09
(vl-load-com)
(if (setq ss (ssget "_X" '((0 . "VIEWPORT"))))
((lambda (lock)
(vlax-for x (setq ss (vla-get-activeselectionset
(cond (*AcadDoc*)
((setq *AcadDoc* (vla-get-activedocument
(vlax-get-acad-object)
)
)
)
)
)
)
(vla-put-displaylocked x lock)
)
(vla-delete ss)
(and (zerop (getvar 'tilemode)) (vla-put-mspace *AcadDoc* :vlax-false))
(princ (strcat "\nAll Viewports have been "
(if (eq lock :vlax-true)
"Locked!"
"Unlocked!"
)
)
)
)
(if (eq "Yes"
(progn (initget 0 "Yes No")
(cond ((getkword "\nLock Viewports? [Yes/No] <Yes>: "))
("Yes")
)
)
)
:vlax-true
:vlax-false
)
)
(alert "\nNo viewports in drawing.")
)
(princ)
)
Lee Mac
2011-06-15, 04:27 PM
A few more variations...
;; Lock Selected Viewport
(defun c:vpl nil
(if (SSVPLock (ssget "_+.:E:S:L" '((0 . "VIEWPORT"))) :vlax-true)
(princ "\n--> Viewport Locked.")
)
(princ)
)
;; Unlock Selected Viewport
(defun c:vpu nil
(if (SSVPLock (ssget "_+.:E:S:L" '((0 . "VIEWPORT"))) :vlax-false)
(princ "\n--> Viewport Unlocked.")
)
(princ)
)
;; Lock All Viewports
(defun c:vpla nil
(SSVPLock (ssget "_X" '((0 . "VIEWPORT"))) :vlax-true)
(princ "\n--> All Viewports Locked.")
(princ)
)
;; Unlock All Viewports
(defun c:vplu nil
(SSVPLock (ssget "_X" '((0 . "VIEWPORT"))) :vlax-false)
(princ "\n--> All Viewports UnLocked.")
(princ)
)
(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
)
)
)
irneb
2011-06-20, 02:49 PM
An extremely old one of mine, I think it's more than 10 years old:
; Adjust viewport lock
(defun iVPAdjustLock ( lck / ss oldcmdecho )
(if (< 0 (getvar "TILEMODE"))
(print "You have to be in Paperspace to change viewport settings.")
(progn
(setq oldcmdecho (getvar "cmdecho")) (setvar "cmdecho" 0)
(command "_undo" "_begin") ; Start Undo Set
(command "_.pspace")
(print "Select the Viewports:")
(setq ss (ssget (list (cons 0 "VIEWPORT"))))
(if lck
(command "_-vports" "_lock" "_on" ss "")
(command "_-vports" "_lock" "_off" ss "")
)
(setq ss nil)
(command "_undo" "_end") ; Close Undo Set
(print "Finito...")
(setvar "cmdecho" oldcmdecho)
)
)
)
; Lock viewports
(defun c:iVPLock ( / )
(iVPAdjustLock T)(princ)
)
; Unlock viewports
(defun c:iVPunLock ( / )
(iVPAdjustLock nil)(princ)
)I remember using it in R14 :mrgreen:
Though it certainly isn't as good as some of the others here. And I've found with interactive selection it seems to not want to select clipped viewports???
Powered by vBulletin® Version 4.1.11 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.