And this version put the text in the middle of the polyline and makes the width the same as the counter.
(defun c:numberpolylines ( / txthght sspolylines acount ssent txtpt )
(vl-load-com)
;;https://autocadtips1.com/2011/10/21/autolisp-mid-point-of-entire-polyline/
(defun MidPoly ( ename / entl en oname param hLen MidPt )
(setq entl (entget ename)
en (cdr (assoc 0 entl))
) ;end setq
(setq oname (vlax-ename->vla-object ename)
param (vlax-curve-getEndParam oname)
hlen (* (vlax-curve-getDistAtParam oname param) 0.5)
MidPt (vlax-curve-getPointAtDist oname hLen)
) ;end setq
(vlax-release-object oname)
MidPt
) ;end defun
(defun createtext ( MyText TextPoint textheight / ) ;; a sub routine to create text.
(entmake (list
'(0 . "TEXT")
'(100 . "AcDbEntity")
'(8 . "0")
'(100 . "AcDbText")
(cons 10 TextPoint)
(cons 40 textheight)
(cons 1 MyText)
'(50 . 0.0)
'(41 . 1.0)
'(51 . 0.0)
;; (cons 7 font)
'(71 . 0)
'(72 . 0)
'(11 0.0 0.0 0.0)
'(210 0.0 0.0 1.0)
'(100 . "AcDbText")
'(73 . 0)
));end list, entmake
) ;;end sub routine
(setq txthght 2.5) ;; text height - can calculate this later if needed to make it relative to polyline lengths and so on
(setq sspolylines (ssget '((0 . "LWPOLYLINE")) )) ;get selection set, filter it to only LWPolyline types
(setq acount 0) ;;just a counter
(while (< acount (sslength sspolylines)) ;; do a loop for the length of the selection set (sslength)
(setq ssent (ssname sspolylines acount )) ;;get the nth entity details in the selection set
(setq TxtPt (MidPoly ssent)) ;;calls MidPoly routine and gets the point result as TxtPt
(setq txtpt (mapcar '+ (list 0 (/ (+ acount txthght) 2) 0) txtpt )) ;; offset txtpt for text position by x:0, y: half text height + line width, z:0
(setq acount (+ acount 1)) ;;increase count by 1. Increased here to make the displayed text start at 1
(command "pedit" ssent "w" acount "") ;;adjusts line width to the count
(createtext (rtos acount) txtpt txthght) ;; run subroutine to create text
)
(princ) ;; exit silently
)
So over to you now, how you want to use this or amend to suit what you want