Hi I need a lisp for save and load view.
I tired to many thins but I couldn't...
I want to save current view, and load saved view.
So finally set the viewports has same views.
But it has different sacle. Please help me
How’s about this to save:
-view _save myview
and this to load:
-view _restore myview
I'm not exactly sure what you want, either, but given what the sample drawing asks for, if the idea is to get the Scale factor from a selected Viewport and assign that same Scale factor to any other selected Viewport(s), this seems to do that [lightly tested]:
(defun C:MVSF ; = Match Viewport Scale Factor
(/ tm vp1 vp1data n vp2 vp2data locked)
(setq tm (getvar 'tilemode))
(setvar 'tilemode 0); into Layout
(command "_.pspace"); in case in Model Space through a Viewport
(setq
vp1 (car (entsel "\nSource Viewport with desired Scale factor: "))
vp1data (entget vp1)
); setq
(prompt "\nTo assign its Scale factor to other Viewport(s),")
(setq ss (ssget "_:L" '((0 . "VIEWPORT"))))
(if ss
(progn ; then
(ssdel vp1 ss); remove source if chosen with others
(repeat (setq n (sslength ss))
(setq
vp2 (ssname ss (setq n (1- n)))
vp2data (entget vp2)
locked (= (logand 16384 (cdr (assoc 90 vp2data))) 16384)
); setq
(if locked (command "_.mview" "_lock" "_off" vp2 ""))
(command "_.mspace")
(setvar 'cvport (cdr (assoc 69 vp2data))); make current
(command
"_.zoom" "_scale" (strcat (rtos (/ (cdr (assoc 41 vp1data)) (cdr (assoc 45 vp1data))) 2 😎 "XP")
"_.pspace"
); command
(if locked (command "_.mview" "_lock" "_on" vp2 ""))
); repeat
); progn
(prompt "\nNo Viewports on unlocked Layer(s) selected."); else
); if
(setvar 'tilemode tm)
); defun
It prevents selection of Viewports on locked Layers, but does not prevent selection of Viewports whose Display is locked like those you want changed in the sample drawing. Since you obviously want to change those, even though their Display is locked, it unlocks any that are locked, and locks them again after changing their Scale factor. If it was a mistake to have the Display locked in those you want to change, it could be modified to change only those with unlocked Display, or to ask the User whether or not to change those with locked Display.
run the following command sequence at the Command prompt:
MVIEW
LOCK
OFF
ALL <or select the vports you want to unlock display>
MATCHPROP
Select source object: <select original vport>
Select destination object(s) or [Settings]: <select all the other vports you want to match view>
When done hit <ENTER>
MVIEW
LOCK
ON
ALL <or select the vports you want to lock display>
There ya go. And when the alias for -View is -V, then...
Command: -V S "Twisted"
<later> or <elsewhere>...
-v r "Twisted"
Ya don't need no stinkin' lisp.
You'd be lookin' like the northbound end of a southbound mule.
John F. Uhden
Hi I need a lisp for make viewports has same view, and save and load the view.
But view means not command "view" It means like command "zoom".
I have many viewports in model space and each has different size.
I can make them has same view, but different scale.
So I need to unlock viports and matchprops them.
In conclusion, I want to make viewports has same view and same scale in one step.
(And i will save the value in the registry for load the view another dwg. But it can myself)
Thank you for read this.
example lisp
(defun c:t1 ( / ) ;; Save View
(setq _vctr (getvar 'viewctr))
(setq _vsize (getvar 'viewsize))
(vl-propagate '_vctr)
(vl-propagate '_vsize)
(princ)
)
(defun c:t2 ( / ) ;; Load View
(if (or (= _vctr nil) (= _vsize nil))
(princ "View Empty.")
(vl-cmdf "_.zoom" "c" _vctr _vsize)
)
(princ)
)
1. save the original view
2. load saved view
3. matchprops for same scale (I want to delete this step)
🙂 Thank you for the advice. It's simple and certain way.
But I have many viewports and even another dwg.
I have never tried it, but can MATCHPROP take entity names as input?
If so, then (with our help) he could create a function to apply MATCHPROP to a selection set, perhaps one at a time, but better than manually, including unlocking and locking. I wonder if the source and the targets have to be in the same layout, or if just being in paperspace is good enough.
John F. Uhden
@darkfprh perhaps something simple like this:
Save this as VPMV.lsp and then (load"VPMV") and run: VPMV
; VPMV function selects a source vport, then selects target vports to match original source vport's view
; target vports displays are unlocked and then left locked
(princ "\nLoading...")
(defun c:VPMV (/ vp ss)
(if(= (getvar"cvport")1)
(progn
(princ "\nSelect Source Vport:")(princ)
(if (setq vp(ssget "_+.:E:S" '((0 . "VIEWPORT"))))
(progn
(princ "\nSelect Target Vports to Match View:")
(if(setq ss(ssget '((0 . "VIEWPORT"))))
(progn
(command "_.Mview""_Lock""_Off" ss "") ; unlock target vports
(command "_Matchprop" vp ss "") ; match target selections to source vp
(command "_.Mview""_Lock""_On" ss "") ; lock target vports
)
(alert "No Target Vports Selected...function Cancelled.")
)
)
(alert "No Vport Selected...function Cancelled.")
)
) ; progn
(alert "VPMV function can only be run in Pspace.")
)(princ)
) ; defun
(princ "\nVPMV function loaded susccessfully.")(princ)
@darkfprh this version will leave the vports display status as they were prior to running the function:
; VPMV function selects a source vport, then selects target vports to match original source vport's view
; target vports displays status is not impacted
(princ "\nLoading...")
(defun c:VPMV (/ en i lock obj vp ss)
(if(= (getvar"cvport")1)
(progn
(princ "\nSelect Source Vport:")(princ)
(if (setq vp(ssget "_+.:E:S" '((0 . "VIEWPORT"))))
(progn
(princ "\nSelect Target Vports to Match View:")
(if(setq ss(ssget '((0 . "VIEWPORT"))))
(progn
(setq i 0)
(repeat (sslength ss)
(setq en (ssname ss i))
(setq obj (vlax-ename->vla-object en))
(if (= :vlax-true (vla-get-displaylocked obj))
(progn
(setq lock 1)
(vla-put-displaylocked obj :vlax-false) ; unlock target vport
)
(setq lock 0)
)
(command "_Matchprop" vp en "") ; match target selection to source vp
(if (not(zerop lock))
(vla-put-displaylocked obj :vlax-true) ; relock target vport
)
(setq i (1+ i))
) ; repeat
) ; progn
(alert "No Target Vports Selected...function Cancelled.")
) ; if
) ; progn
(alert "No Vport Selected...function Cancelled.")
) ; if
) ; progn
(alert "VPMV function can only be run in Pspace.")
)(princ)
) ; defun
(princ "\nVPMV function loaded susccessfully.")(princ)
you should try the VPMV lisp codes I posted in Messages 10 & 11 in the other thread you posted:
Thank you! But I tired the lisp but i think it just unlock and matchprops viewports using ssget.
I want to use the lisp in model space. (inside viewport)
try this VPRV lisp code which does not require you to start in modelspace. you can actually be in paperspace and select the vport as the source view and then select all the other vports as target. Then it'll automatically jump inside each of the target vports and change the view to match without impacting the scale:
; VPRV function selects a source vport, then selects target vports to match original source vport's view not impacting scale
; also target vports display status is not impacted
(princ "\nLoading...")
(defun c:VPRV (/ aec_cdate_only en get_vp_num i lock obj set_vp_lock_stat ss vnam vnum vp)
(vl-load-com)
(defun aec_cdate_only ( / D mo dax yr mi hr se)
(setq D (rtos (getvar "CDATE") 2 6)) ; get Julian date
(setq mo (substr D 5 2)); month
(setq dax (substr D 7 2)); day
(setq yr (substr D 1 4)); year (4 digit)
(setq mi (substr D 12 2)); minute
(setq hr (substr D 10 2)); hour
;(setq se (substr D 14 2)); second
(cond
((> (atoi hr) 12)
(setq hr (itoa (- (atoi hr) 12)))
(if(< (strlen hr) 2)(setq hr (strcat "0" hr)))
(strcat yr "-" mo "-" dax "-" hr "-" mi "pm")
)
((= (atoi hr) 12)
(strcat yr "-" mo "-" dax "-" hr "-" mi "pm")
)
(t
(if(< (strlen hr) 2)(setq hr (strcat "0" hr)))
(strcat yr "-" mo "-" dax "-" hr "-" mi "am")
)
)
) ; defun
(defun set_vp_lock_stat (ob / lok)
(if (= :vlax-true (vla-get-displaylocked ob))
(progn
(setq lok 1)
(vla-put-displaylocked ob :vlax-false) ; unlock target vport
)
(setq lok 0)
)
lok
) ; defun
(defun get_vp_num (ent)
(cdr(assoc 69 (entget ent))) ; get vport #
) ; defun
(if(= (getvar"cvport")1)
(progn
(princ "\nSelect Source Vport:")(princ)
(if (setq vp(ssget "_+.:E:S" '((0 . "VIEWPORT"))))
(progn
(princ "\nSelect Target Vports to Match View Not Impacting Scale:")
(if(setq ss(ssget '((0 . "VIEWPORT"))))
(progn
(setq vnam (aec_cdate_only)) ; get unique view name
(setq en (ssname vp 0)) ; get entity of source vp
(setq vnum (get_vp_num en)) ; get vport #
(setq obj (vlax-ename->vla-object en)) ; convert to vl obj
(setq lock (set_vp_lock_stat obj)) ; set display lock status
(command"_.Mspace""_CVport" vnum "_.-View" "_Save" vnam) ; goto mspace goto source vp and save view
(if (not(zerop lock))
(vla-put-displaylocked obj :vlax-true) ; relock target vport
)
(setq i 0)
(repeat (sslength ss)
(setq en (ssname ss i))
(setq vnum (get_vp_num en)) ; get vport #
(setq obj (vlax-ename->vla-object en))
(setq lock (set_vp_lock_stat obj)) ; set display lock status
(command"_.CVport" vnum "_View" "_Restore" vnam) ; goto target mspace and restore view
(if (= (setq i (1+ i)) (sslength ss))
(command"_.View""_Delete" vnam "_.Pspace") ; at last loop del unique view name and return to pspace
)
(if (not(zerop lock))
(vla-put-displaylocked obj :vlax-true) ; relock target vport
)
) ; repeat
) ; progn
(alert "No Target Vports Selected...function Cancelled.")
) ; if
) ; progn
(alert "No Vport Selected...function Cancelled.")
) ; if
) ; progn
(alert "VPRV function can only be run in Pspace.")
)(princ)
) ; defun
(princ "\nVPRV function loaded susccessfully.")(princ)
I've modified VPRV lisp so that it'll work for both when you're outside of the Pspace vport as well as inside the Pspace vport (in mspace):
; VPRV function selects a source vport, then selects target vports to match original source vport's view not impacting scale
; also target vports display status is not impacted
(princ "\nLoading...")
(defun c:VPRV
(/ aec_cdate_only cmdecho cvport en get_vp_num i locksource locktarget
menuecho objsource objtarget set_vp_lock_stat ss vnam vnum vp
)
(vl-load-com)
(defun aec_cdate_only ( / D mo dax yr mi hr se)
(setq D (rtos (getvar "CDATE") 2 6)) ; get Julian date
(setq mo (substr D 5 2)); month
(setq dax (substr D 7 2)); day
(setq yr (substr D 1 4)); year (4 digit)
(setq mi (substr D 12 2)); minute
(setq hr (substr D 10 2)); hour
;(setq se (substr D 14 2)); second
(cond
((> (atoi hr) 12)
(setq hr (itoa (- (atoi hr) 12)))
(if(< (strlen hr) 2)(setq hr (strcat "0" hr)))
(strcat yr "-" mo "-" dax "-" hr "-" mi "pm")
)
((= (atoi hr) 12)
(strcat yr "-" mo "-" dax "-" hr "-" mi "pm")
)
(t
(if(< (strlen hr) 2)(setq hr (strcat "0" hr)))
(strcat yr "-" mo "-" dax "-" hr "-" mi "am")
)
)
) ; defun
(defun set_vp_lock_stat (ob / lok)
(if (= :vlax-true (vla-get-displaylocked ob))
(progn
(setq lok 1)
(vla-put-displaylocked ob :vlax-false) ; unlock target vport
)
(setq lok 0)
)
lok
) ; defun
(defun get_vp_num (ent)
(cdr(assoc 69 (entget ent))) ; get vport #
) ; defun
; setup environment
(setq cmdecho(getvar"cmdecho") menuecho(getvar"menuecho"))
(setvar"cmdecho"0)
(setvar"menuecho"0)
(if(zerop (getvar"tilemode"))
(progn
(setq vnam (aec_cdate_only)) ; get unique view name
(if (= (setq cvport (getvar"cvport")) 1) ; if in psace
(progn ; outside vport
(princ "\nSelect Source Vport:")
(while (not(setq vp(ssget "_+.:E:S" '((0 . "VIEWPORT"))))))
(setq en (ssname vp 0)) ; get entity of source vp
(setq vnum (get_vp_num en)) ; get vport #
(setq objsource (vlax-ename->vla-object en)) ; convert to vl obj
(setq locksource (set_vp_lock_stat objsource)) ; set display lock status
(command"_.Mspace""_CVport" vnum) ; goto mspace to source vp
)
(progn ; inside vport
(setq objsource (VLA-GET-activePviewport (vla-get-activedocument (vlax-get-acad-object)))) ; get vl obj
(setq locksource (set_vp_lock_stat objsource)) ; set display lock status
)
) ; if inside floating vp
(command"_.-View" "_Save" vnam "_.Pspace") ; save as unique view name and goto pspace
(princ "\nSelect Target Vports to Match View Not Impacting Scale:")
(if(setq ss(ssget '((0 . "VIEWPORT"))))
(progn
(setq i 0)
(command"_.Mspace") ; goto mspace
(repeat (sslength ss)
(setq en (ssname ss i))
(setq vnum (get_vp_num en)) ; get vport #
(setq objtarget (vlax-ename->vla-object en)) ; convert to vl obj
(setq locktarget (set_vp_lock_stat objtarget)) ; set display lock status
(command"_.CVport" vnum "_View" "_Restore" vnam) ; goto target mspace and restore view
(if (= (setq i (1+ i)) (sslength ss))
(command"_.View""_Delete" vnam "_.Pspace") ; at last loop del unique view name and return to pspace
)
(if (not(zerop locktarget))
(vla-put-displaylocked objtarget :vlax-true) ; relock target vport
)
) ; repeat
(if (not(zerop locksource))
(vla-put-displaylocked objsource :vlax-true) ; relock source vport
)
(if(> cvport 1)
(command"_.MSpace""_CVport" cvport) ; return to original space
)
) ; progn
(alert "No Target Vports Selected...function Cancelled.")
) ; if
) ; progn
(alert "VPRV function can only be run in Pspace.")
) ; if
; restore environment
(if menuecho (setvar"menuecho"menuecho))
(if cmdecho (setvar"cmdecho"cmdecho))(princ)
) ; defun
(princ "\nVPRV function loaded susccessfully.")(princ)
@darkfprh wrote:
.... I want to use the lisp in model space. (inside viewport)
I don't understand. Does that mean you always want to set all Viewports to the same Scale factor as the one you're in when you start? If not all of them, then presumably you need to select some, and you can't do that from in Model Space. My routine in Message 4 lets you start in either, but if you're in Model Space it forces you into Paper Space where you can select the source and target Viewports.
this VPRZ lisp function variation uses your t1 and t2 lisp functions. I also added t0 to clear the zoom settings so you can save new zoom settings. give this a try and let me know if this is what you're looking for:
; VPRZ function selects a source vport, then selects target vports to match original source vport's view not impacting scale
; also target vports display status is not impacted
(princ "\nLoading...")
; t0 command removes zoom settings
(defun c:t0 ( / ) ;; Remove View
(setq _vctr nil _vsize nil)
(vl-propagate '_vctr)
(vl-propagate '_vsize)
(princ)
)
; t1 command saves zoom settings
(defun c:t1 ( / ) ;; Save View
(setq _vctr (getvar 'viewctr))
(setq _vsize (getvar 'viewsize))
(vl-propagate '_vctr)
(vl-propagate '_vsize)
(princ)
)
; t2 command restrieves zoom settings
(defun c:t2 ( / ) ;; Load View
(if (or (= _vctr nil) (= _vsize nil))
(princ "View Empty.")
(vl-cmdf "_.zoom" "_c" _vctr _vsize)
)
(princ)
)
(defun c:VPRZ
(/ cmdecho cvport en get_vp_num i locksource locktarget
menuecho objsource objtarget set_vp_lock_stat ss vnam vnum vp
)
(vl-load-com)
(defun set_vp_lock_stat (ob / lok)
(if (= :vlax-true (vla-get-displaylocked ob))
(progn
(setq lok 1)
(vla-put-displaylocked ob :vlax-false) ; unlock target vport
)
(setq lok 0)
)
lok
) ; defun
(defun get_vp_num (ent)
(cdr(assoc 69 (entget ent))) ; get vport #
) ; defun
; setup environment
(setq cmdecho(getvar"cmdecho") menuecho(getvar"menuecho"))
(setvar"cmdecho"0)
(setvar"menuecho"0)
(if(zerop (getvar"tilemode"))
(progn
(if (or (= _vctr nil) (= _vsize nil))
(progn
(if (= (setq cvport (getvar"cvport")) 1) ; if in psace
(progn ; outside vport
(princ "\nSelect Source Vport:")
(while (not(setq vp(ssget "_+.:E:S" '((0 . "VIEWPORT"))))))
(setq en (ssname vp 0)) ; get entity of source vp
(setq vnum (get_vp_num en)) ; get vport #
(command"_.Mspace""_CVport" vnum) ; goto mspace to source vp
)
) ; if inside floating vp
(princ"\nSave Zoom Settings")
(c:t1) ; save zoom settings
(command "_.Pspace") ; goto pspace
) ; progn
(princ"\nZoom Settings Already Saved.")
) ; if
(princ "\nSelect Target Vports to Match View Not Impacting Scale:")
(if(setq ss(ssget '((0 . "VIEWPORT"))))
(progn
(setq i 0)
(command"_.Mspace") ; goto mspace
(repeat (sslength ss)
(setq en (ssname ss i))
(setq vnum (get_vp_num en)) ; get vport #
(setq objtarget (vlax-ename->vla-object en)) ; convert to vl obj
(setq locktarget (set_vp_lock_stat objtarget)) ; set display lock status
(command"_.CVport" vnum) ; goto target mspace
(princ"\nRetrieve Zoom Settings")
(c:t2) ; restrieve zoom settings
(if (= (setq i (1+ i)) (sslength ss))
(command"_.Pspace") ; at last loop return to pspace
)
(if (not(zerop locktarget))
(vla-put-displaylocked objtarget :vlax-true) ; relock target vport
)
) ; repeat
(if(> cvport 1)
(command"_.MSpace""_CVport" cvport) ; return to original space
)
) ; progn
(alert "No Target Vports Selected...function Cancelled.")
) ; if
) ; progn
(alert "VPRZ function can only be run in Pspace.")
) ; if
; restore environment
(if menuecho (setvar"menuecho"menuecho))
(if cmdecho (setvar"cmdecho"cmdecho))(princ)
) ; defun
(princ "\nVPRZ function loaded susccessfully.")(princ)
Can't find what you're looking for? Ask the community or share your knowledge.