Solving this involves letting the algorithm analyze the rest of the set and implementing a little more code to filter the results.
For this reason, I decided to emphasize my initial approach.
Forgive me if I'm a bit long-winded in my explanation, but I believe that for those of us who work in the world of cartography, topography and geographic information systems, these resources are of interest.
Therefore, I attach below the results of my approach, which are as follows:
- Create a selection set, as limited as possible, of closed polylines that will need to be fully analyzed
- Analyze all the perimeters and store those that enclose the reference point
- Select and return the one with the smallest area
The result, given the limited scope of the analysis (closed polylines), has allowed the required code to be considerably less than expected.
Below is the code:
;***************************************
;****** G L A V C V S F E C I T ******
(defun selR (p / r s l lt en ex d cj i n o r4 f)
(defun r4 (p e / r is f)
(foreach l '(1.5708 3.141592 4.71239)
(setq r (vla-addline i (vlax-3d-point p) (vlax-3d-point (polar p l d))))
(if (setq is (safearray-value (variant-value (vla-intersectWith r e acextendnone))))
(setq f (cons (= (rem (length is) 2) 1) f))
)
(vla-delete r)
)
f
)
(setq en (getvar "extmin") ex (getvar "extmax") n -1
d (max (- (car ex) (car en)) (- (cadr ex) (cadr en)))
)
(if (setq cj
(ssget"_X"
(list
'(0 . "LWP*")
'(-4 . "&=") '(70 . 1)
(cons 410 (if (= 1 (getvar 'cvport)) (getvar 'ctab) "Model"))
'(-4 . "<and")
'(-4 . ">,>,*")
(list 10 (car p) (- (cadr p) 50.0) 0.0)
'(-4 . ">,<,*")
(list 10 (car p) (+ (cadr p) 50.0) 0.0)
'(-4 . "and>")
)
)
)
(while (setq e (ssname cj (setq n (1+ n))))
(setq lt nil)
(if (not r)
(setq r (vla-addline (setq i (vlax-ename->vla-object (cdr (assoc 330 (entget e))))) (vlax-3d-point p) (vlax-3d-point (polar p 0 d))))
)
(if (setq l (safearray-value (variant-value (vla-intersectWith r (setq o (vlax-ename->vla-object e)) acextendnone))))
(if (= (rem (length (setq l (while l (setq lt (cons (list (nth 0 l) (nth 1 l) (nth 2 l)) lt) l (cdddr l) lt lt)))) 2) 1)
(if (and (car (setq f (r4 p o))) (cadr f) (caddr f))
(setq s (if s (if (< (vla-get-area o) (vla-get-area s)) o) o))
)
)
)
)
)
(if r (vla-delete r))
(if s (sssetfirst nil (ssadd (vlax-vla-object->ename s))))
s
)