Get viewport VLA object from a VPORT number

Get viewport VLA object from a VPORT number

EATREVITPOOPCAD
Collaborator Collaborator
1,546 Views
9 Replies
Message 1 of 10

Get viewport VLA object from a VPORT number

EATREVITPOOPCAD
Collaborator
Collaborator

How can I get a VLA object of a viewport using a selection such as 

 

(vl-load-com)
(setq vp  (entsel "\n Select Viewport :"))
(setq obj (vlax-ename->vla-object (car vp)))

 

 

However, how can I get vla-object of a viewport using CVPORT number? In my case it would be always number 2, because each layout only has one viewport. So I am ok with getting the first viewport, or all the viewports on the layout also... I need to select it automatically without the user.

The definition of insanity is doing the same thing over and over again and expecting different results
0 Likes
Accepted solutions (1)
1,547 Views
9 Replies
Replies (9)
Message 2 of 10

paullimapa
Mentor
Mentor
Accepted solution

actually in pspace, cvport # is never 2

only Model tab uses cvport of 2

the pspace itself has cvport # of 1

then any vports created will be 3 and above

so to get all vports in current layout use this filter list:

 

(setq ssvp (ssget"x"(list '(0 . "VIEWPORT") (cons 410 (getvar"ctab"))'(-4 . ">=")'(69 . 2))))

 

Now if ssvp is not nil which means you have pspace vports you can proceed with cycling through the selection set to get the entity to convert to vla object like this:

 

(foreach ent (setq entlst(mapcar 'cadr (ssnamex ssvp))) ; loop through all entties from ssvp converted to list 
 (if (= 'ename (type ent)) ; confirm is entity
    (progn
      (setq obj (vlax-ename->vla-object ent)) ; convert ent to vla obj
      ; ....do rest of your code
    ) ; progn
 ) ; if entity
) ; foreach loop

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 3 of 10

Moshe-A
Mentor
Mentor

@paullimapa  hi,

 


@paullimapa wrote:

actually in pspace, cvport # is never 2

only Model tab uses cvport of 2

the pspace itself has cvport # of 1

then any vports created will be 3 and above

this is not exactly true, the first viewport id is 2 and the next is 3, at least on my AutoCAD 😀

 

 

 

 

Message 4 of 10

paullimapa
Mentor
Mentor

you are correct. I guess I must have deleted the first vport because when I was testing it showed the only vport I had as #3. 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 5 of 10

Moshe-A
Mentor
Mentor

@paullimapa ,

 

And it's correct that cvport is 2 when you call it from model space so i do not understand the conflict?!

i would assign 0 to model space 🤣

 

 

Message 6 of 10

komondormrex
Mentor
Mentor

to get vla-object of layout with id 2 in paperspace Layout2

 

 

(vlax-ename->vla-object (ssname (ssget "_x" (list (cons 0 "viewport") (cons 410 (setq p_space "Layout2")) (cons 69 2))) 0))

 

Message 7 of 10

paullimapa
Mentor
Mentor

Yes don’t know why Autodesk chose to introduce this kind of conflict. This means any code written will always need to check tilemode value as well and not just cvport number to confirm you’re actually in Model tab... face palm


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 8 of 10

EATREVITPOOPCAD
Collaborator
Collaborator

@paullimapa That was it, thank you! Appreciate quick responses everyone... This community is awesome.

 

@komondormrex I actually got an error running your code

Exception has occurred.
bad argument type: lselsetp nil
The definition of insanity is doing the same thing over and over again and expecting different results
0 Likes
Message 9 of 10

paullimapa
Mentor
Mentor

Glad to have helped.... cheers!!!

ps: I think you get the error when you don’t have a cvport = 2 since @komondormrex  coded  specifically for that 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 10 of 10

Sea-Haven
Mentor
Mentor

Something like this

 

(command "._PSPACE")
(setq sss (ssget "_+.:E:S" (list (cons 0 "Viewport"))))
(setq cspace (cdr (assoc 69 (entget (ssname sss 0)))))
0 Likes