Author Topic: Why is this code ca not detect viewport in new file?  (Read 2375 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Why is this code ca not detect viewport in new file?
« on: November 02, 2015, 02:24:10 PM »
I added this code in startup.
It is working with saved files but with new file can not detect viewports
Code - Auto/Visual Lisp: [Select]
  1. (setq ViewPortTrgtLyr "0") ;Change Target Layer
  2. (setq ViewPortcnt 0)
  3. (if (setq ss (ssget "_X" '((0 . "VIEWPORT"))))
  4.   (while
  5.     (setq ViewPortcntent (ssname ss ViewPortcnt))
  6.     (setq ViewPortcntententVL (vlax-ename->vla-object ViewPortcntent))
  7.     (vla-put-layer ViewPortcntententVL ViewPortTrgtLyr)
  8.     (vla-put-color ViewPortcntententVL 256)
  9.     (vla-put-displaylocked ViewPortcntententVL :vlax-true)
  10.     (setq ViewPortcnt (1+ ViewPortcnt))
  11.     )
  12.   )
  13. )

Thanks
« Last Edit: November 05, 2015, 01:56:56 AM by HasanCAD »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Why is this code ca not detect viewport in new vile?
« Reply #1 on: November 02, 2015, 03:47:48 PM »
This is because your new files do not contain "VIEWPORT" entities.
You may think they do because when you activate a PS layout there is a VP. But this VP has been newly created (notice how any existing MS geometry always fits perfectly inside this VP).

A simple test:
Open an existing dwg with 2 layouts each with 1 VP on the layout:
Code: [Select]
(sslength (ssget "_X" '((0 . "VIEWPORT")))) => 4Create a new layout:
Code: [Select]
(sslength (ssget "_X" '((0 . "VIEWPORT")))) => 4Activate the new layout:
Code: [Select]
(sslength (ssget "_X" '((0 . "VIEWPORT")))) => 6

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Why is this code ca not detect viewport in new vile?
« Reply #2 on: November 02, 2015, 05:35:47 PM »
Perhaps use a vlr-miscellaneous-reactor with callback function triggered on the layoutswitched event.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Why is this code ca not detect viewport in new vile?
« Reply #3 on: November 03, 2015, 02:51:18 AM »
So is this good solution
Code - Auto/Visual Lisp: [Select]
  1. (setq ViewPortTrgtLyr "0") ;Change Target Layer
  2. (setq ViewPortcnt 0)
  3. (if (= (getvar 'dwgtitled) 1)
  4.   (if(> (setq ss (sslength (ssget "_X" '((0 . "VIEWPORT"))))) 0 )
  5.     (while
  6.       (setq ViewPortcntent (ssname ss ViewPortcnt))
  7.       (setq ViewPortcntententVL (vlax-ename->vla-object ViewPortcntent))
  8.       (vla-put-layer ViewPortcntententVL ViewPortTrgtLyr)
  9.       (vla-put-color ViewPortcntententVL 256)
  10.       (vla-put-displaylocked ViewPortcntententVL :vlax-true)
  11.       (setq ViewPortcnt (1+ ViewPortcnt))
  12.       )
  13.     )
  14.   (setq ss 0)
  15.   )
  16. (princ (strcat (rtos ssl 2 0) " Viewports Moved to Layer " ViewPortTrgtLyr ))

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Why is this code ca not detect viewport in new vile?
« Reply #4 on: November 03, 2015, 02:57:03 AM »
Perhaps use a vlr-miscellaneous-reactor with callback function triggered on the layoutswitched event.
:?  :roll:
In fact this high level of coding is greater than my poor level
:wink:  :-D

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Why is this code ca not detect viewport in new vile?
« Reply #5 on: November 03, 2015, 03:52:15 AM »
So is this good solution...
I don't think your code works.
FWIW: My suggestion:
Code - Auto/Visual Lisp: [Select]
  1. (setq ViewPortTrgtLyr "0") ; Change Target Layer.
  2. (if (setq ss (ssget "_X" '((0 . "VIEWPORT") (-4 . "<NOT") (69 . 1) (-4 . "NOT>")))) ; Skip the main PS VP.
  3.   (progn
  4.     (setq ViewPortCnt 0)
  5.     (repeat (sslength ss)
  6.       (setq ViewPortEnt (ssname ss ViewPortCnt))
  7.       (setq ViewPortObj (vlax-ename->vla-object ViewPortEnt))
  8.       (vla-put-layer ViewPortObj ViewPortTrgtLyr)
  9.       (vla-put-color ViewPortObj 256)
  10.       (vla-put-displaylocked ViewPortObj :vlax-true)
  11.       (setq ViewPortCnt (1+ ViewPortCnt))
  12.     )
  13.     (princ (strcat "\n" (itoa ViewPortCnt) " Viewports Moved to Layer " ViewPortTrgtLyr))
  14.   )
  15.   (princ "\nNo Viewports ")
  16. )

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Why is this code ca not detect viewport in new vile?
« Reply #6 on: November 03, 2015, 04:33:42 AM »
Suppose the new file is very light
What about this
Code - Auto/Visual Lisp: [Select]
  1. (if (= (getvar 'dwgtitled) 0)
  2.   (progn
  3.     (setq Ctab (getvar "CTAB"))
  4.     (foreach Layout (layoutlist) (vl-cmdf "LAYOUT" "S" Layout))
  5.     (setvar "CTAB" Ctab)))
  6.       (setq ViewPortTrgtLyr "0") ;Change Target Layer
  7.       (setq ViewPortcnt 0)
  8.      (setq ss (ssget "_X" '((0 . "VIEWPORT") (-4 . "<NOT") (69 . 1) (-4 . "NOT>")))) ; Skip the main PS VP.      
  9.       )
  10.   (progn
  11.     (setq ViewPortCnt 0)
  12.     (repeat (sslength ss)
  13.       (setq ViewPortcntent (ssname ss ViewPortcnt))
  14.       (setq ViewPortcntententVL (vlax-ename->vla-object ViewPortcntent))
  15.       (vla-put-layer ViewPortcntententVL ViewPortTrgtLyr)
  16.       (vla-put-color ViewPortcntententVL 256)
  17.       (vla-put-displaylocked ViewPortcntententVL :vlax-true)
  18.       (setq ViewPortcnt (1+ ViewPortcnt))
  19.       )
  20.     (princ (strcat "\n" (itoa ViewPortCnt) " Viewports Moved to Layer " ViewPortTrgtLyr))
  21.     )
  22.   (princ "\nNo Viewports ")
  23.   )


Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Why is this code ca not detect viewport in new vile?
« Reply #7 on: November 03, 2015, 05:30:28 AM »
Perhaps use a vlr-miscellaneous-reactor with callback function triggered on the layoutswitched event.
:?  :roll:
In fact this high level of coding is greater than my poor level
:wink:  :-D

Try something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun fixvports ( a b / i o s )
  2.     (if (setq s (ssget "_X" '((0 . "VIEWPORT") (-4 . "<>") (69 . 1))))
  3.         (repeat (setq i (sslength s))
  4.             (if (vlax-write-enabled-p (setq o (vlax-ename->vla-object (ssname s (setq i (1- i))))))
  5.                 (progn
  6.                     (vla-put-layer o "0")
  7.                     (vla-put-color o acbylayer)
  8.                     (vla-put-displaylocked o :vlax-true)
  9.                 )
  10.             )
  11.         )
  12.     )
  13.     (princ)
  14. )
  15. (   (lambda nil (vl-load-com)
  16.             (if (= "fixvports-reactor" (vlr-data rtr))
  17.                 (vlr-remove rtr)
  18.             )
  19.         )
  20.         (fixvports nil nil)
  21.         (vlr-miscellaneous-reactor "fixvports-reactor"
  22.            '((:vlr-layoutswitched . fixvports))
  23.         )
  24.         (princ)
  25.     )
  26. )

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Why is this code ca not detect viewport in new file?
« Reply #8 on: November 05, 2015, 02:35:28 AM »
Thanks Lee