In looking through this Topic again, one thing I notice about both accepted solutions above is that they force linetype generation on, whether or not it was on for a given Polyline. If you want to preserve the linetype generation on/off status of Polylines, this does that, though it still only closes with a zero-length segment, leaving the coincident start/end vertices:
(vl-load-com); if needed
(defun C:CLPC
; = Closed-Looking Polyline Close [keeping coincident start/end vertices, with 0-length segment]
(/ ss n pl pldata)
(if (setq ss (ssget "_:L" '((0 . "lwpolyline") (-4 . "<NOT") (-4 . "&") (70 . 1) (-4 . "NOT>"))))
(repeat (setq n (sslength ss))
(setq
pl (ssname ss (setq n (1- n)))
pldata (entget pl)
); setq
(if (equal (vlax-curve-getStartPoint pl) (vlax-curve-getEndPoint pl) 1e-6)
(entmod (subst (cons 70 (1+ (cdr (assoc 70 pldata)))) (assoc 70 pldata) pldata))
); if [start/end at same place]
); repeat
); if [found unclosed Polylines]
(princ)
); defun
[It also uses a different approach to some things, principally that it's not necessary to extract a list of all vertices in order to compare whether the start and end ones are in the same place.]
But I keep mentioning the zero-length closing segment and coincident start-end vertices issue [see Message 7], because there have been several topics raised here in which some routine didn't work right on Polylines, and it turned out the reason was that they contained coincident vertices. They can definitely cause problems with certain operations. If you want to take Polylines that look closed but are not really, and close them without keeping both vertices at the start/end location, and without a zero-length closing segment, this [a modification of something from the Topic linked at Message 7] will do that:
(vl-load-com); if needed
(defun C:CLPCE ; = Closed-Looking Polyline Close Eliminating coincident start/end vertices
(/ ss n pl pldata)
(if (setq ss (ssget "_:L" '((0 . "lwpolyline") (-4 . "<NOT") (-4 . "&") (70 . 1) (-4 . "NOT>"))))
(repeat (setq n (sslength ss))
(setq
pl (ssname ss (setq n (1- n)))
pldata (entget pl)
); setq
(if (equal (vlax-curve-getStartPoint pl) (vlax-curve-getEndPoint pl) 1e-6)
(progn ; then - edit off last segment, and close
(command "_.pedit" pl "_edit"); end command
(repeat (- (cdr (assoc 90 pldata)) 2) (command "_next")); move to next-to-last vertex
(command "_break" "_next" "_go" "_eXit" "_close" "")
); progn
); if [start/end at same place]
); repeat
); if [found unclosed Polylines]
(princ)
); defun
Kent Cooper, AIA