;;; Select (activate) viewport inside another viewport in one click
;;; Created by cwake
;;; Saved from: https://www.cadtutor.net/forum/topic/59734-alternate-method-for-activating-floating-viewports/

(defun c:vv ( /
; Functions
gvpbs insidep1 insidep2 vlax-list->3D-point
; Variables
doc vps pt vp
)

(vl-load-com)
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
;;******************************************************************
;; Local Functions
;;******************************************************************
(defun gvpbs ( / vps i el cb bdys ctr v )
(if (setq vps (ssget
"_X"
(list
'(0 . "VIEWPORT")
'(-4 . ">")
'(69 . 1)
(cons 410 (getvar "CTAB"))
)
)
)
(progn
(repeat (setq i (sslength vps))
(setq el (entget (ssname vps (setq i (1- i)))))
(if (setq cb (cdr (assoc 340 el)))
(setq bdys (cons (cons cb (cdr (assoc 69 el))) bdys))
(progn
(setq ctr (cdr (assoc 10 el))
v (list
(/ (cdr (assoc 40 el)) 2.)
(/ (cdr (assoc 41 el)) 2.)
0.0
)
bdys (cons
(cons
(list
(mapcar '- ctr v)
(mapcar '+ ctr v)
)
(cdr (assoc 69 el))
)
bdys
)
)
)
)
)
bdys
)
)
)

;;******************************************************************
;; from [url]http://www.cadtutor.net/forum/showthread.php?36221-How-to-determine-if-a-point-is-within-a-boundary[/url]
;; ============ Insidep.lsp ===============
;;
;; MAIN FUNCTION DESCRIPTION:
;; Will determine whether a point lies
;; inside or outside an object.
;;
;; FUNCTION: insidep
;; ARGUMENTS:
;; Point to be tested.
;; Object Ename or VLA-Object
;;
;; FUNCTION: vlax-list->3D-point
;; ARGUMENTS:
;; List to be converted.
;; Flag to determine x or y.
;;
;; OBJECT COMPATIBILITY:
;; Everything except Viewport/Polygon Mesh.
;;
;; AUTHOR:
;; Copyright (c) 2009, Lee McDonnell
;; (Contact Lee Mac, CADTutor.net)
;;
;; PLATFORMS:
;; No Restrictions,
;; only tested in ACAD 2004.
;;
;; ========================================

(defun insidep1 ( pt Obj / Tol ang spc flag int lin xV yV )
(or (eq 'VLA-OBJECT (type Obj))
(setq Obj (vlax-ename->vla-object Obj)))

(setq Tol (/ pi 6) ; Uncertainty
ang 0.0 flag T)

(setq spc (if (zerop (vla-get-activespace doc))
(if (= (vla-get-mspace doc) :vlax-true)
(vla-get-modelspace doc)
(vla-get-paperspace doc))
(vla-get-modelspace doc)))

(while (and (< ang (* 2 pi)) flag)
(setq flag (and
(setq int
(vlax-invoke
(setq lin
(vla-addLine spc
(vlax-3D-point pt)
(vlax-3D-point
(polar pt ang
(if (vlax-property-available-p Obj 'length)
(vla-get-length Obj) 1.0)))))
'IntersectWith Obj
acExtendThisEntity))
(3D-point int T) '3D-point int nil) '<))
(or (<= (car xV) (car pt) (last xV))
(3D-point (lst flag)
(if lst
(cons ((if flag car cadr) lst)
(vlax-list->3D-point (cdddr lst) flag)
)
)
)

;;******************************************************************
(defun insidep2 ( pt lst )
(vl-every
(function
(lambda ( a )
(apply ' (getvar "CVPORT") 1) (vla-put-mspace doc :vlax-false)))
((null (setq pt (getpoint "\nSelect point inside a viewport: "))))
(T
(setq pt (trans pt 1 0))
(if (setq vp (vl-some
(function
(lambda ( x )
(if
(apply
(if (= (type (car x)) 'ENAME)
'insidep1
'insidep2
)
(list pt (car x))
)
(cdr x)
)
)
)
vps
)
)
(progn
(vla-put-mspace doc :vlax-true)
(setvar "CVPORT" vp)
)
(princ "\nPoint is not within a viewport.")
)
)
)
(princ)
)

(c:vv)