Try this, a few changes to the selection codes and a slight change to the polyline selection 'ssget'
(defun c:foo (/ selset offset_value loops hatch layer)
;;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/get-list-of-all-layers-in-lisp/td-p/822262
(defun Table (s / d r)
(while (setq d (tblnext s (null d)))(setq r (cons (cdr (assoc 2 d)) r)) )
) ; end defun
(defun LM:lst->str ( lst del ) ;;Lee Mac
(if (cdr lst)
(strcat (car lst) del (LM:lst->str (cdr lst) del))
(car lst)
)
)
(defun LM:ListIntersection ( l1 l2 )
(vl-remove-if-not '(lambda ( x ) (member x l2)) l1)
)
;;;List of layers
(setq LayerList '("Layer1" "Layer2") ) ; List of layers
;;;
(if (= (type (LM:ListIntersection LayerList (Table "layer")) ) 'LIST) ; check if layers exist in drawing
(progn
(if (= (setq selset ; Selection set of closed polylines on layers
(ssget (list
(cons 0 "*POLYLINE") (cons 8 (LM:lst->str LayerList ","))
'(-4 . "<OR") '(70 . 1) '(70 . 129) '(-4 . "OR>")
)) ; end ssget, list
) ; end setq
nil) ; end =
(progn
(princ "No Lines selected") ; no suitable polylines
)
(progn
(initget "1 2")
(setq offset_value
(cond ((getkword "\nSelect offset value [1 - OPTION1/2 - OPTION2] <1> : ")) ; get option
(t "1")
) ;_ end of cond
) ; end setq
(vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
(setq layer (vla-add (vla-get-layers adoc) "HATCH LAYER"))
(vla-put-color layer 253) ; change color
(vla-put-lineweight layer aclnwt005) ; change lineweight
(setq offset_value (cond ((= (atoi offset_value) 1) 0.5)
((= (atoi offset_value) 2) 2.0)
) ;_ end of cond
loops
(mapcar (function (lambda (ent)
(vl-sort (mapcar (function (lambda (x)
(setq x (car (vlax-safearray->list (vlax-variant-value x))))
(vla-put-layer x (vla-get-name layer))
x
) ;_ end of lambda
) ;_ end of function
(list (vla-offset (vlax-ename->vla-object ent) offset_value)
(vla-offset (vlax-ename->vla-object ent) (- offset_value))
) ;_ end of list
) ;_ end of mapcar
(function (lambda (a b) (> (vla-get-area a) (vla-get-area b))))
) ;_ end of vl-sort
) ;_ end of lambda
) ;_ end of function
((lambda (/ tab item)
(repeat (setq tab nil
item (sslength selset)
) ;_ end setq
(setq tab (cons (ssname selset (setq item (1- item))) tab))
) ;_ end of repeat
) ;_ end of lambda
)
) ;_ end of mapcar
) ;_ end of setq
(foreach item loops
(setq hatch (vla-addhatch (vla-get-modelspace adoc)
achatchpatterntypepredefined
"ANSI31"
:vlax-false
achatchobject
) ;_ end of vla-AddHatch
) ;_ end of setq
(vla-appendouterloop hatch
(vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list (car item)))
) ;_ end of vla-appendouterloop
(vla-appendinnerloop hatch
(vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list (cadr item)))
) ;_ end of vla-appendinnerloop
(vla-evaluate hatch)
(vla-update hatch)
(vla-put-patternscale hatch 0.1) ; change hatch scale
(vla-put-layer hatch (vla-get-name layer))
) ;_ end of foreach
(vla-endundomark adoc)
) ; end progn ssget
)
) ; end progn Layers Exist
(progn
(alert "No layers exist in the drawing...")
) ; end progn
) ;_ end if Layers exist
(command "_Setbylayer" (ssget "X") "" "_Yes" "_Yes")
(princ)
)