I am attempting to capture viewport layer override settings but am having little success. Could someone point me to the proper way of doing this. This is what I have so far.
I have also attached text files of my progress
-You have to be inside a viewport for it to initiate properly:
(defun c:SVP (/ activeDoc targetVP layers layerSettings file response layerName vpOverrides vpColor vpLineWeight vpLineType vpPlotStyle vpTransparency vpVisibility)
;; Get the active document
(setq activeDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
;; Check if the cursor is in model space through a viewport
(if (= (vla-get-Mspace activeDoc) :vlax-false)
(progn
(princ "\n*** OPERATION CANCELED ***")
(princ "\nYou must be inside a viewport to save the VP settings.\n")
(princ)
)
(progn
;; Get the active viewport
(setq targetVP (vla-get-ActivePViewport activeDoc))
;; Check if the file already exists
(if (findfile "C:/Temp/VPsettings.txt")
(progn
;; Prompt user to proceed or cancel
(initget "C P")
(setq response (getkword "\nFile already exists. Proceed and overwrite the file? [C to cancel/P to proceed] <P>: "))
(if (not (or (equal response "P") (equal response "p") (equal response "")))
(progn
(princ "\nOperation has been canceled.\n")
(exit)
)
)
)
)
;; Open file for writing and make sure to close it properly
(setq file (open "C:/Temp/VPsettings.txt" "w"))
;; Check if file opened successfully
(if (not file)
(progn
(princ "\n*** ERROR: Unable to open file for writing ***\n")
(exit)
)
)
;; Get viewport layer overrides using entget
(setq layers (vla-get-Layers activeDoc))
(setq layerSettings '())
(vlax-for layer layers
(setq layerName (vla-get-Name layer))
(setq vpOverrides (entget (tblobjname "LAYER" layerName)))
;; Access VP-specific properties directly with checks
(setq vpColor (if (numberp (cdr (assoc 62 vpOverrides))) (cdr (assoc 62 vpOverrides)) 0))
(setq vpLineWeight (if (numberp (cdr (assoc 370 vpOverrides))) (cdr (assoc 370 vpOverrides)) 0))
(setq vpLineType (if (cdr (assoc 6 vpOverrides)) (cdr (assoc 6 vpOverrides)) "ByLayer"))
(setq vpPlotStyle (if (cdr (assoc 390 vpOverrides)) (cdr (assoc 390 vpOverrides)) ""))
(setq vpTransparency (if (numberp (cdr (assoc 440 vpOverrides))) (cdr (assoc 440 vpOverrides)) 0))
(setq vpVisibility (if (numberp (cdr (assoc 70 vpOverrides))) (cdr (assoc 70 vpOverrides)) 0))
;; Store the collected settings
(setq layerSettings
(cons (list layerName
(vla-get-Description layer)
(vla-get-Color layer)
(vla-get-LineWeight layer)
(vla-get-Linetype layer)
(vla-get-PlotStyleName layer)
(vla-get-Freeze layer)
(vla-get-Lock layer)
(vla-get-LayerOn layer)
;; VP-specific settings
vpColor
vpLineWeight
vpLineType
vpPlotStyle
vpTransparency
vpVisibility)
layerSettings))
)
;; Write settings to file
(foreach layer layerSettings
(write-line
(strcat
(nth 0 layer) "\t" ;; Layer Name
(if (nth 1 layer) (nth 1 layer) "N/A") "\t" ;; Description
(itoa (or (nth 2 layer) 0)) "\t" ;; Color
(itoa (or (nth 3 layer) 0)) "\t" ;; Line Weight
(if (nth 4 layer) (nth 4 layer) "N/A") "\t" ;; Line Type
(if (nth 5 layer) (nth 5 layer) "N/A") "\t" ;; Plot Style
(if (eq (nth 6 layer) :vlax-true) "Yes" "No") "\t" ;; Freeze
(if (eq (nth 7 layer) :vlax-true) "Yes" "No") "\t" ;; Lock
(if (eq (nth 8 layer) :vlax-true) "Yes" "No") "\t" ;; Layer On
;; VP-specific settings
(itoa (or (nth 9 layer) 0)) "\t" ;; VP Color
(itoa (or (nth 10 layer) 0)) "\t" ;; VP Line Weight
(if (nth 11 layer) (nth 11 layer) "N/A") "\t" ;; VP Line Type
(if (nth 12 layer) (nth 12 layer) "N/A") "\t" ;; VP Plot Style
(itoa (or (nth 13 layer) 0)) "\t" ;; VP Transparency
(if (eq (nth 14 layer) :vlax-true) "Yes" "No") ;; VP Visibility
)
file
)
)
;; Close the file
(close file)
;; Display completion message
(princ "\nThe \"VP\" Settings have been saved as VPSettings.txt in C:\\Temp.\n")
(princ)
)
)
)
;; Display message after LISP is loaded
(princ "\nType \"SVP\" to save viewport layer override settings.\n")
(princ "\nThe \"VP\" Settings will be saved as VPSettings.txt in C:\\Temp.\n")
(princ)
Best Regards
-Tim C.
I have this routine that served some purpose at the time... I noticed that it's driven by layer name while you're by VP, so it needs to be rewritten... Anyway, I hope you find it useful and it helps.
how’s about using the builtin layer states command to save and restore vport layer settings?
I have used / Tested them and they work but I am trying to implement the work-flow through a two-part lisp operation.
Well, I went the route that @paullimapa suggested already. Those commands have their issues... but I think I came to a solution that mostly works — and I use it. The idea is that you have a master viewport in a certain layer, there you make your setting, save the layer state, and then populate the layer state to all other VPs on the same layer.
I have tried a few different ways but keep getting an error.
; error: bad argument type: stringp nil
If you see anything that stands out as an issue could you let me know?
(defun c:SVP3 ( / activeDoc viewports vp targetVP layers file response lyr ext prp rec vpl lay lays vpColor vpLineWeight vpLineType vpPlotStyle vpTransparency vpFreeze)
;; Helper function to safely convert values
(defun safe-convert (val default)
(if (numberp val)
val
default
)
)
;; Get the active document
(setq activeDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
;; Check if the cursor is in model space through a viewport
(if (= (vla-get-Mspace activeDoc) :vlax-false)
(progn
;; Get viewports in paper space
(setq viewports (ssget "_X" '((0 . "VIEWPORT"))))
(if (not viewports)
(progn
(alert "No viewports found in paper space.")
(progn (princ) (quit))
)
)
;; Prompt user to select a viewport
(setq vp (car (entsel "\nSelect a viewport: ")))
(if (not vp)
(progn
(alert "No viewport selected.")
(progn (princ) (quit))
)
)
(setq targetVP (vlax-ename->vla-object vp))
)
(setq targetVP (vla-get-ActivePViewport activeDoc))
)
;; Check if the file already exists
(if (findfile "C:/Temp/vpsettings.txt")
(progn
;; Prompt user to proceed or cancel
(initget "C P")
(setq response (getkword "\nFile already exists. Proceed and overwrite the file? [C to cancel/P to proceed] <P>: "))
(if (not (or (equal response "") (equal (strcase response) "P")))
(progn
(princ "\nOperation has been canceled.\n")
(progn (princ) (quit))
)
)
)
)
;; Open the file for writing
(setq file (open "C:/Temp/vpsettings.txt" "w"))
(if (not file)
(progn
(princ "\n*** ERROR: Unable to open file for writing ***\n")
(progn (princ) (quit))
)
)
;; Initialize lays as an empty list
(setq lays '())
;; Iterate through each layer and get the VP properties
(setq layers (vla-get-Layers activeDoc))
(vlax-for layer layers
(setq lyr (vla-get-Name layer))
(if (setq ext (cdr (assoc 360 (entget (tblobjname "LAYER" lyr)))))
(foreach prp '("COLOR" "LINETYPE" "LINEWT" "ALPHA" "PLOTSTYLE")
(if (setq rec (dictsearch ext (strcat "ADSK_XREC_LAYER_" prp "_OVR")))
(foreach vpl (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 335)) rec))
(if (and vpl (setq lay (cdr (assoc 410 (entget vpl)))))
(if (not (member lay lays))
(setq lays (cons lay lays))
)
)))))
)
;; Save VP properties to the file
(foreach lay lays
(if (setq vpOverrides (entget (tblobjname "LAYER" lay)))
(progn
(setq vpColor (safe-convert (cdr (assoc 62 vpOverrides)) 0))
(setq vpLineWeight (safe-convert (cdr (assoc 370 vpOverrides)) 0))
(setq vpLineType (or (cdr (assoc 6 vpOverrides)) "ByLayer"))
(setq vpPlotStyle (or (cdr (assoc 390 vpOverrides)) ""))
(setq vpTransparency (safe-convert (cdr (assoc 440 vpOverrides)) 0))
(setq vpFreeze (if (= (logand (cdr (assoc 70 vpOverrides)) 1) 1) "Yes" "No"))
(write-line
(strcat
lay "\t"
(itoa vpColor) "\t"
(itoa vpLineWeight) "\t"
vpLineType "\t"
vpPlotStyle "\t"
(itoa vpTransparency) "\t"
vpFreeze
)
file
))
(princ (strcat "\nError: vpOverrides is nil for layer " lay))
)
)
;; Close the file
(close file)
;; Display completion message
(princ "\nThe \"VP\" settings have been saved as VPSettings.txt in C:\\Temp.\n")
(princ)
)
;; Display message after LISP is loaded
(princ "\nType \"SVP3\" to save viewport layer properties.\n")
(princ)
both attached versions give an error
Sorry for the newbie questions.
Thanks for your help
lays are layouts, not layers.... (tblobjname "LAYER" lay) ??
also noticed that zero response of getkword is nil, not "".
But I never got stringp error, so there is probably more.
The attached revised SVP3.lsp should work for non xref layers.
FYI: The only override value I could not find any lisp function to retrieve is the Plotstyle
That's just my luck. That is the VP Override param I was targeting
retrieving
Thank you, I will give it a look to see if it can help
Holy cow, that is fast. Literally, in the blink of an ey,e all the data pops up.
Ok, onto to the next phase I will see if I can apply those settings to a viewport in another drawing.
Thank You for your help
unfortunately I'm encountering the same issue with your Plotstyle solution as before where it's only getting the first one in the layer list but not the rest
(setq vpPlotStyle (cdr (assoc 3 (entget (cdr (assoc 330 (entget (cdr (assoc 344 rec)))))))))
Oh, I completely forgot about that one... It's some old code I had lying around... you should have kicked me when I reposted it... ![]()
So getting at that VP Plotstyle override is still a mystery that has yet to be solved. @timothy_crouse we can’t help you with that one. The layerstate functions is still the better solution where you can use those functions to save, export and import.
excellent...now you're talking @ВeekeeCZ !!!
I've updated the code one more time to sort all layers & place xref layers afterwards
@timothy_crouse take it away...
I think I have the code you provided to save the settings properly converted to save the override properties with RGB formatted colors vs the original ACI format.
But when I try to load the RGB formatted values using LVPRGB I get an error:
Command: LVPRGB
; error: bad argument type: stringp 0
If you have time please give the LVPRGB code review and give me a tip or two.
definitely have a good start, thank you for your help and time
Best Regards
-Tim C.
Can't find what you're looking for? Ask the community or share your knowledge.