Author Topic: Turn all layout viewports ON-OFF in a drawing  (Read 681 times)

0 Members and 1 Guest are viewing this topic.

Ndruu

  • Mosquito
  • Posts: 6
Turn all layout viewports ON-OFF in a drawing
« on: February 07, 2024, 11:02:34 AM »
We have relatively big files with a lot of xrefs and a whole lot of Layouts with 3-5 viewports (. Switching between Layouts is painfully slow in these drawings. That's why I came up with the idea to turn the viewports OFF, when we don't need them, so switching can be sped up.

I have this little code to do the job for me, but it only works in some cases. You can even run it from Model space.
But it only works if you create the viewports in the same session as when you run the lisp.

Try it: Draw something in Model space, create multiple Layouts then make a viewport on each one.
Run the code AVPOFF and it will turn all viewports off on all Layouts. Save the drawing, close and reopen it and run the code AVPON. It will only turn on those viewports that were on the last activated Layout.

According to the Autocad help file:
DXF 68 || Viewport status field: || -1 = On, but is fully off screen, or is one of the viewports that is not active because the $MAXACTVP count is currently being exceeded. // 0 = Off // <positive value > = On and active. The value indicates the order of stacking for the viewports, where 1 is the active viewport, 2 is the next, and so forth

DXF 69 || Viewport ID || Couldn't find any further description...

The problem is Viewport ID resets to 0 every time you close the drawing.  :idiot2: You have to cycle through all layouts to get the true Viewport ID values.

I would like to do this job without alternating through paperspaces. Can it be done?

Code: [Select]
(defun c:avpoff (/ ss i d1 d2 d3 d4)
  (vl-load-com)
  (if
    (setq ss (ssget "_X"
    (list (cons -4 "<AND")
  (cons 0 "VIEWPORT")
  (cons -4 "!=")
  (cons 69 1)
  (cons -4 "!=")
  (cons 68 0)
  (cons -4 "AND>")
    )
     )
    )
     (progn
       (setq i 0)
       (repeat (sslength ss)
(vla-put-viewporton
   (vlax-ename->vla-object (ssname ss i))
   :vlax-false
)
(setq i (1+ i))
       )
     )
  )
  (setvar 'qaflags 0)
  (princ "\nDone! All viewports are inactive!")
  (princ)
)

(defun c:avpon (/ ss1 i1 d1 d2 d3 d4)
  (vl-load-com)
  (if
    (setq ss1 (ssget "_X"
     (list (cons -4 "<AND")
   (cons 0 "VIEWPORT")
   (cons -4 "!=")
   (cons 69 1)
   (cons -4 "<")
   (cons 68 1)
   (cons -4 "AND>")
     )
      )
    )
     (progn
       (setq i1 0)
       (repeat (sslength ss1)
(vla-put-viewporton
   (vlax-ename->vla-object (ssname ss1 i1))
   :vlax-true
)
(setq i1 (1+ i1))
       )
     )
  )
  (princ "\nDone! All viewports are active!")
  (princ)
)

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Turn all layout viewports ON-OFF in a drawing
« Reply #1 on: February 07, 2024, 03:26:30 PM »
Try using this to get the VPORTS:
Code - Auto/Visual Lisp: [Select]
  1. (setq ss (ssget "_X" '((0 . "VIEWPORT") (-4 . "!=") (69 . 1))))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Rod

  • Newt
  • Posts: 185
Re: Turn all layout viewports ON-OFF in a drawing
« Reply #2 on: February 07, 2024, 04:01:27 PM »
FYI Turning off the layer the viewports are on will still be slow when switching between layouts. But freezing the layer will make a big difference.
"All models are wrong, some models are useful" - George Box

Ndruu

  • Mosquito
  • Posts: 6
Re: Turn all layout viewports ON-OFF in a drawing
« Reply #3 on: February 07, 2024, 06:29:45 PM »
Try using this to get the VPORTS:
Code - Auto/Visual Lisp: [Select]
  1. (setq ss (ssget "_X" '((0 . "VIEWPORT") (-4 . "!=") (69 . 1))))

Thank you ronjonp! Unfortunately it didn't work, it still gives me the same "Automation Error. Not in paperspace" message.
The last break point in the code is at this line:
Code: [Select]
(vla-put-viewporton (vlax-ename->vla-object (ssname ss i)) :vlax-false)


FYI Turning off the layer the viewports are on will still be slow when switching between layouts. But freezing the layer will make a big difference.

Rod: Thank you too! I tried it and I could not measure any difference. Switching from one layout to another took 33s and another 25s to regenerate. I tried it on a drawing that has 380k objects, 48 xrefs in it.

PKENEWELL

  • Bull Frog
  • Posts: 320
Re: Turn all layout viewports ON-OFF in a drawing
« Reply #4 on: February 08, 2024, 11:09:46 AM »
According to Autodesk help, you should use the (vla-Display) method to control the display of a viewport on or off:
Code - Auto/Visual Lisp: [Select]
  1. (vla-Display (vlax-ename->vla-object (ssname ss i)) :vlax-true)
  2.  
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

Ndruu

  • Mosquito
  • Posts: 6
Re: Turn all layout viewports ON-OFF in a drawing
« Reply #5 on: May 24, 2024, 12:04:26 PM »
Thank you all for the great suggestions and sorry for the late reply!
Unfortunately none of it worked for me hundred percent, but after some digging and fiddling I found a way to do it.
Thanks to Andrzej Kalinowski who wrote a description of his app https://autolisps.blogspot.com/p/viewporttools.html

To get the correct Viewport ID, you have to cycle through all layouts, that is unavoidable.
But the process takes more time as the number of layers grow in the drawing.
So the key is to freeze all the layers and then go through each layout. After that you can thaw back the layers with LAYERP.

Final simple code (no error trapping):

Code - Auto/Visual Lisp: [Select]
  1. (defun c:avpoff (/ old-ctab ss count ent llc i)
  2.   (setq old-ctab (getvar "ctab"))
  3.   (setq llc (length (layoutlist)))
  4.   (setq llc0 llc)
  5.   (command "-Layer" "freeze" "*" "")
  6.   (acet-ui-progress-init
  7.     (strcat "In progress ( Nr. of layouts " (rtos llc 2 0) " )")
  8.     llc
  9.   )
  10.   (setq i 0)
  11.   (foreach layout (layoutlist)
  12.     (if
  13.       (and (not (= (strcat layout) "MODEL"))
  14.            (setq ss (ssget "x" (list '(0 . "VIEWPORT") (cons 410 layout))))
  15.            (setvar 'ctab layout)
  16.       )
  17.        (progn
  18.          (setq count -1)
  19.          (setq i (1+ i))
  20.          (while (< (setq count (1+ count)) (1- (sslength ss)))
  21.            (setq ent (vlax-ename->vla-object (ssname ss count)))
  22.            (vla-display ent :vlax-false)
  23.            (vla-put-viewporton ent :vlax-false)
  24.            (acet-ui-progress-safe i)
  25.          )                              ;while
  26.        )
  27.     )                                   ;if
  28.   )                                     ;foreach
  29.   (acet-ui-progress-done)
  30.   (setvar 'ctab old-ctab)
  31.   (command "layerp")
  32.   (princ)
  33.   (setvar 'modemacro "")
  34.   (princ "\nDone!")
  35.   (alert "\nAll viewports are ON")
  36.   (princ)
  37. )
  38.  
  39. (defun c:avpon  (/ old-ctab ss count ent llc i)
  40.   (setq old-ctab (getvar "ctab"))
  41.   (setq llc (length (layoutlist)))
  42.   (setq old-layer (getvar 'clayer))
  43.   (command "-Layer" "freeze" "*" "")
  44.   (acet-ui-progress-init
  45.     (strcat "In progress ( Nr. of layouts: "
  46.             (rtos llc 2 0)
  47.             " )"
  48.     )
  49.     llc
  50.   )
  51.   (setq i 0)
  52.   (foreach layout (layoutlist)
  53.     (if
  54.       (and (not (= (strcat layout) "MODEL"))
  55.            (setq ss (ssget "x" (list '(0 . "VIEWPORT") (cons 410 layout))))
  56.            (setvar 'ctab layout)
  57.       )
  58.        (progn
  59.          (setq count -1)
  60.          (setq i (1+ i))
  61.          (while (< (setq count (1+ count)) (1- (sslength ss)))
  62.            (setq ent (vlax-ename->vla-object (ssname ss count)))
  63.            (vla-display ent :vlax-false)
  64.            (vla-put-viewporton ent :vlax-true)
  65.            (acet-ui-progress-safe i)
  66.          )                              ;while
  67.        )
  68.     )                                   ;if
  69.   )                                     ;foreach
  70.   (acet-ui-progress-done)
  71.   (setvar 'ctab old-ctab)
  72.   (command "layerp")
  73.   (princ)
  74.   (setvar 'modemacro "")
  75.   (princ "\nDone!")
  76.   (alert "\nAll viewports are ON")
  77.   (princ)
  78. )