Here is another way, slight difference in selecting the boundary and blatant copy of pkenewell error.
Using ssget "+.:E:S" in case you later want to add more filters than just a polyline.
Put the units type (-units command) as a variable to reset it after the LISP has run
Moved the end of an if statement (if POLY) to the end so that if no polyline is selected the LISP will just stop.
(defun C:BS ( / clayer vars old MySS Poly SS obj cc d i s )
(vl-load-com)
(defun trap (msg)
(if (not (wcmatch (strcase msg T) "*break*,*cancel*,*quit*,*exit*"))
(princ (strcat "\nError: " msg "\n"))
(princ "\nProgram Aborted.\n")
)
(if (and vars vals)(mapcar '(lambda (x y) (setvar x y)) vars vals))
(setq *error* temperr)
(vl-cmdf "._undo" "_end")
(princ)
) ; end defun
;; https://www.afralisp.net/visual-lisp/tutorials/error-trapping.php
(setq temperr *error*)
(setq *error* trap)
(vl-cmdf "._undo" "_begin") ;;Start Undo
(setq clayer (getvar "clayer")) ;;Get current Layer
(setq vars '("CMDECHO" "DIMLFAC" "DIMLUNIT" "DIMDEC" "DIMTXT" "LUNITS"))
(setq old (mapcar 'getvar vars)) ;;get old variables
(mapcar 'setvar vars '(0 1 4 4 0.5 4)) ;;set variables above according to list values
;; (setvar "cmdecho" 0) ; see above
;; (setvar "dimlfac" 1) ; see above
;; (setvar "dimlunit" 4); see above
;; (setvar "dimdec" 4) ; see above
;; (setvar "dimtxt" 0.5); see above
;; (vl-cmdf "-UNITS" 4 "" "" "" "" "") ; see above
(vl-cmdf "_.ZOOM" "E" "_.zoom" ".9x")
(vl-cmdf "-overkill" "all" "" "P" "N" "")
(initcommandversion)(command "_.join" "_All" "")
(while (= (setq MySS (ssget "_+.:E:S" (list (cons 0 "*POLYLINE")) )) nil)
(princ "\nOh No!! that is not a polyline")
) ; end while
;; (if (setq Poly (car (entsel "\nSelect Boundary: ")))
(if (setq Poly (ssname MySS 0))
(progn
(vl-cmdf "-Layer" "M" "PIECE" "C" "40" "" "")
(vla-put-layer (vlax-ename->vla-object Poly) "PIECE")
(vla-put-color (vlax-ename->vla-object Poly) 256)
;; ) ; end progn ; moved to end of LISP
;; ) ; end if ; moved to end of LISP
(setvar "clayer" clayer)
(vl-cmdf "_.layer" "F" "PIECE" "")
(if (setq SS (ssget "_A" (LIST '(0 . "CIRCLE,POLYLINE,LWPOLYLINE,ARC"))))
(progn
(vl-cmdf "-Layer" "M" "HOLE" "C" "140" "" "")
(foreach obj (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS))))
(vla-put-layer obj "HOLE")
(vla-put-color obj 256)
) ; end foreach
) ; end prog
) ; end if
(vla-Explode (vlax-ename->vla-object Poly))
(vla-Delete (vlax-ename->vla-object Poly))
(if (setq cc (ssget "_A" (LIST '(0 . "CIRCLE") '(40 . 0.0))))
(progn
(setq d 0.5) ; default circle radius
;; (and (setq d 1.00)
;; (setq d (/ d 2.))
;; ) ; end and
(repeat (setq i (sslength cc))
(setpropertyvalue (ssname cc (setq i (1- i))) "Radius" d)
) ; end repeat
(foreach obj (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex cc))))
(vl-cmdf "-Layer" "M" "ERROR" "C" "1" "" "")
(vla-put-layer obj "ERROR")
(vla-put-color obj 256)
) ; end foreach
) ; end progn
) ; end if
(and (setq s (ssget "_X" '((0 . "POINT,TEXT"))))
(vl-cmdf "_.erase" s "")
) ; end and
(vl-cmdf "-purge" "a" "" "n")
(vl-cmdf "-Layer" "M" "MARKER" "C" "240" "" "")
(vl-cmdf "-Layer" "T" "PIECE" "")
(vl-cmdf "-Layer" "U" "HOLE" "")
(vl-cmdf "-Layer" "M" "HOLE" "C" "140" "" "")
(vl-cmdf "-Layer" "M" "0" "C" "7" "" "")
) ; end progn
(progn
(princ "\nNo suitable polyline selected")
)
) ; end if Poly exists
(setvar "cmdecho" 1)
(vl-cmdf "._undo" "_end")
(mapcar 'setvar vars old)
(princ)
)