Alright, please try the following program which should change the layer of selected blocks based on the closest / nearest polyline's layer found.
(defun c:test (/ int sel ent get pos pts ins loc srt)
;;----------------------------------------------------;;
;; Author : Tharwat Al Choufi ;;
;; website: https://autolispprograms.wordpress.com ;;
;;----------------------------------------------------;;
(and
(princ "\nSelect Polylines & Texts : ")
(setq int -1
sel (ssget "_:L" '((0 . "INSERT,LWPOLYLINE")))
)
(while (setq int (1+ int)
ent (ssname sel int)
)
(setq get (entget ent)
pos (cdr (assoc 10 get))
)
(or (and (= (cdr (assoc 0 get)) "LWPOLYLINE")
(setq pts (cons (list (cdr (assoc 10 (reverse get)))
ent
(assoc 8 get)
)
pts
)
)
)
(setq ins (cons (list pos get) ins))
)
)
(while (and ins pts)
(and (setq pos (car ins)
loc (car pos)
)
(setq srt
(car (vl-sort
pts
(function
(lambda (j k)
(< (distance
loc
(vlax-curve-getclosestpointto (cadr j) loc)
)
(distance
loc
(vlax-curve-getclosestpointto (cadr k) loc)
)
)
)
)
)
)
)
(entmod (subst (caddr srt) (assoc 8 (cadr pos)) (cadr pos)))
(setq ins (cdr ins))
)
)
)
(princ)
) (vl-load-com)