Here's some code excerpted from an old function of mine for labeling tax maps. Maybe there's something in it that will help guide you to success...
;;------------------------------------------------------------
;; Function to get the absolutely last entity in the database:
;;
(defun @entlast ( / e elast)
(setq elast (entlast))
(while (setq e (entnext elast))
(setq elast e)
)
elast
)
(defun @str2num (txt / nlist1 nlist2 c char txtlen num)
(setq nlist1 '("1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "-")
nlist2 '("1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "." "-")
c 1
txtlen (strlen txt)
num ""
)
(while (and (not (vl-position (substr txt c 1) nlist1))
(<= c txtlen))
(setq c (1+ c))
)
(while (vl-position (setq char (substr txt c 1)) nlist2)
(setq num (strcat num char) c (1+ c))
)
(if (/= num "")
(atof num)
)
)
;;---------------------------------------------------------------
;; Function to determine if an item (point, VLA-Object, or ENAME)
;; is within a closed polyline boundary.
;;
(defun @within (item poly / p ilist)
(cond
((= (type item) 'ENAME)
(setq p (cdr (assoc 10 (entget item))))
)
((= (type item) 'VLA-OBJECT)
(setq p (cdr (assoc 10 (entget (vlax-vla-object->ename item)))))
)
((and (listp item)
(> (vl-list-length item) 2)
(setq item (vl-remove-if-not 'numberp item))
(> (length item) 1))
(setq p item)
)
)
(if (and p ray)(vlax-put ray "Basepoint" p))
(if (= (type poly) 'ENAME)
(setq poly (vlax-ename->vla-object poly))
)
(and
p
(= (type poly) 'VLA-OBJECT)
(setq ilist (vlax-invoke ray "IntersectWith" poly acExtendNone))
(= (rem (/ (length ilist) 3) 2) 1)
)
)
(defun @get_items_within (etypes layers poly / ss ssl i items)
(if (setq ss (ssget "X" (list (cons 0 etypes)(cons 8 layers)'(410 . "Model"))))
(progn
(setq i 0.0 ssl (sslength ss))
(while (< i ssl)
(setq e (ssname ss i)
i (1+ i)
)
(if (@within e poly)(setq items (cons e items)))
)
)
)
items
)
;;---------------------------------------------------------------
;; Function to create a boundary polyline from LINES ans ARCs
;; given the internal point (point, VLA-Object, or ENAME),
;; a comma-delimited string of the layers to use to select the lines, and arcs,
;; a string to use as the layer of the new object,
;; and a string (text) to describe the boundary.
;;
(defun @bpoly (node layers layer text / p ss e elast ent)
(cond
((= (type node) 'ENAME)
(setq p (cdr (assoc 10 (entget node))))
)
((= (type item) 'VLA-OBJECT)
(setq p (cdr (assoc 10 (entget (vlax-vla-object->ename node)))))
)
((and (listp node)
(> (vl-list-length node) 2)
(setq node (vl-remove-if-not 'numberp node))
(> (length node) 1))
(setq p node)
)
)
(if
(and p
(= (type layers) 'STR)
(= (type layer) 'STR)
(setq ss (ssget "X" (list '(0 . "LINE,ARC")(cons 8 layers))))
(setq elast (@entlast))
(setq ucs (not (command "_.UCS" "_Origin" (getvar "viewctr"))))
(if (= (type text) 'STR)(princ (strcat "\nCreating boundary for " text " ...")) 1)
(not (command "_.boundary" "_A" "_I" "_N" "" "_O" "_P" "_B" "_N" ss "" "" (trans p 0 1) ""))
(setq ucs (command "_.UCS" "_P") e (entlast))
(or (not (equal e elast))(prompt " Failed."))
)
(progn
(if text (princ " Okay."))
(setq ent (entget e))
(entmod (subst (cons 8 layer)(assoc 8 ent) ent))
(vlax-ename->vla-object e)
)
)
)
i don't think you need all the functions, but I know that you do need @entlast and @bpoly. I think the UCS has a lot to do with its working properly.