Get All Viewports in All Layouts

Get All Viewports in All Layouts

CodeDing
Advisor Advisor
3,182 Views
6 Replies
Message 1 of 7

Get All Viewports in All Layouts

CodeDing
Advisor
Advisor

I'm confused, maybe someone can help.

 

I was trying to create a function to help another OP On This Post.

 

In short, this function should merely return all viewport enames from all layout tabs (EXCLUDING the "paper space" viewport.. [Viewport 1])

(defun GetViewports ( / ssVP vpList cnt)
  (if (setq ssVP (ssget "_X" '((0 . "VIEWPORT")
                                (-4 . "<AND")
                                  (-4 . "<NOT") (410 . "Model") (-4 . "NOT>")
                                  (-4 . ">") (69 . 1)
                                (-4 . "AND>")
                              )));list/ssget/setq
    (repeat (setq cnt (sslength ssVP))
      (setq vpList (cons (ssname ssVP (setq cnt (1- cnt))) vpList))
    );repeat
  );if
);defun

 

BUT, I'm getting inconsistent results and cannot explain why?

I start a new drawing, which has 2 layouts by default (each with 1 viewport by default).

I am also defaulted to Model space. <-- This is important! do not change tabs yet!

image.png

When I run my function, only 1 viewport ename is returned (the viewport on Layout1), when I should get 2 viewports returned!

Command: (getviewports)
(<Entity name: 1ea74fc1bf0>)

 

Now, ONLY when I go to my last layout (Layout2), does my function return correctly...

image.png

Command: (getviewports)
(<Entity name: 1ea74fc1bf0> <Entity name: 1ea74fbf890>)

 

ALSO, if I add another Layout (do not switch to this layout yet!) and run it again, I still only get 2 viewports...

image.png

Command: (getviewports)
(<Entity name: 1ea74fc1bf0> <Entity name: 1ea74fbf890>)

 

And ONLY when I switch to Layout3 does my function finally return my 3rd layout...

image.png

Command: (getviewports)
(<Entity name: 1ea74fc1bf0> <Entity name: 1ea74fbf890> <Entity name: 1ea74fbf9e0>)

 

Any thoughts?! I have tried changing the 'ssget' call, to no avail. It just seems like a bug or something?

 

Best,

~DD

0 Likes
3,183 Views
6 Replies
Replies (6)
Message 2 of 7

Jonathan3891
Advisor
Advisor

I remember trying to do something similar with the exact results your getting. 

I had to use the "LAYOUTLIST" function to get it to work.

 

(defun GetViewports ( / ssVP vpList cnt)
  (foreach X (layoutlist)
  (if (setq ssVP (ssget "_X" '((0 . "VIEWPORT")
                                (-4 . "<AND")
                                  (-4 . "<NOT") (410 . "Model") (-4 . "NOT>")
                                  (-4 . ">") (69 . 1)
                                (-4 . "AND>")
                              )));list/ssget/setq
    (repeat (setq cnt (sslength ssVP))
      (setq vpList (cons (ssname ssVP (setq cnt (1- cnt))) vpList))
    );repeat
  );if
    )
);defun

 


Jonathan Norton
Blog | Linkedin
0 Likes
Message 3 of 7

cadffm
Consultant
Consultant

Because of..

Create a new Layout means not there is viewport,

Acad creates this paperspace viewport  by initializing the layout and this is the first time when the layout is current!

You can see this from user view too.

 

Create a new layout, set current, create one more but stay in the last created.

Use PUBLISH and see the layout list, last(?) Column..

The same you can check with lisp now.

 

Next step, set the last new layout current, check it again.

 

Understood?

Sebastian

0 Likes
Message 4 of 7

CodeDing
Advisor
Advisor

@Jonathan3891 ,

 

Thank you for trying to help. However, when I run your suggested edit in my method, it appears to work for one or two times, then it becomes inconsistent and the length of the returned list varies, especially when I add or move to new layouts. Which is weird, especially since all of the variables are declared as local. Does this happen for you also?

 

@cadffm ,

 

Yes it makes sense and I am starting to see how it affects my code.

 

 

The "easy" solution seems to loop to each layout sheet then retrieve the viewports on that sheet and append them to a single selection set, but that seems unnecessarily slow.

...Is there a method that someone can think of that will return the viewports I desire? PREFERABLY without having to activate each layout sheet to retrieve them? 

 

Best,

~DD

0 Likes
Message 5 of 7

pbejse
Mentor
Mentor

@CodeDing wrote:

...Is there a method that someone can think of that will return the viewports I desire? PREFERABLY without having to activate each layout sheet to retrieve them? 


 

The behaviour is the same even with ActiveX, Does it perform the same way even with an already titled drawing?  havent really tried to investigate that condtion. 

 

what about polygonal viewports?

 

BTW:  you can also write the filter as:

(ssget "_X" '((0 . "VIEWPORT")(410 . "~Model")(-4 . ">")(69 . 1)))

 

 

 

0 Likes
Message 6 of 7

CodeDing
Advisor
Advisor

@pbejse ,

 


Does it perform the same way even with an already titled drawing?


Yes

 


what about polygonal viewports?


Same issue

 


BTW:  you can also write the filter as:

 

(ssget "_X" '((0 . "VIEWPORT")(410 . "~Model")(-4 . ">")(69 . 1)))

 


Thank you LOL, I was hoping there was a better way. I always thought those special characters applied to numbers I guess. That's much cleaner!

 

Best,

~DD

0 Likes
Message 7 of 7

john.uhden
Mentor
Mentor

I have some old code much like yours.

The only difference is that I used (-4 . "/=")(69 . 1) instead of (-4 . ">")(69 . 1)

I dunno, they should both be the same, but it has always worked.

John F. Uhden

0 Likes