OK here's the 2nd try, changes marked in red:
Code:
(setq L2P:Tol 10.0) ;Global var to save the tolerance
;;; Command to match line ends to closest point
(defun c:Lines2Points (/ ss tol n en ed lines points)
(command "_UNDO""_BEgin")
(prompt "\nSelect the lines & points to match: ")
(setq ss (ssget '((0 . "LINE,POINT"))) ;Get a selection set from user
n 0.0 ;Initialize counter to zero
) ;_ end of setq
;; Step through entire selection set
(while (and ss (< n (sslength ss)))
(setq en (ssname ss n) ;Get entity's ename
ed (entget en) ;And its DXF data
) ;_ end of setq
(cond
((= "LINE" (cdr (assoc 0 ed))) ;If it's a line
(setq lines (cons ed lines)) ;Add it to the lines list
)
((= "POINT" (cdr (assoc 0 ed))) ;If it's a point
(setq points (cons (cdr (assoc 10 ed)) points)) ;Add the point's XYZ to the points list
)
) ;_ end of cond
(setq n (+ n 1.0)) ;Increment counter
) ;_ end of while
(prompt (strcat "\nMatching " (itoa (length lines)) " lines to " (itoa (length points)) " points."))
(if (setq tol (getdist (strcat "\nWhat's the maximum tolerance distance <" (rtos L2P:Tol) ">: "))) ;Ask user for tolerance
(setq L2P:Tol tol) ;User specified new distance - set default for next time
(setq tol L2P:Tol) ;User pressed Enter - get default from last time
) ;_ end of if
(prompt "\nContinue to modify lines ... ")
;; Step through the lines list
(foreach ed lines
;; Removed and condition and split start- and endpoints into 2 sepearate if statements
(if (setq n (FindClosest (cdr (assoc 10 ed)) points tol)) ;Get closest point to start
(setq ed (subst (cons 10 n) (assoc 10 ed) ed)) ;New start point
) ;_ end of if
(if (setq en (FindClosest (cdr (assoc 11 ed)) points tol)) ;Get closest point to end
(setq ed (subst (cons 11 en) (assoc 11 ed) ed)) ;New end point
) ;_ end of if
(entmod ed) ;Change the line
) ;_ end of foreach
(prompt "done.")
(command "_UNDO" "_End")
(princ)
) ;_ end of defun
;;; Helper function to find the closest point
(defun FindClosest (pt lst dist / pt0 pt1 dt)
(setq pt0 nil) ;Initializa the found point to nil
;; Removed calculate 1st point, return nil if not found
(foreach pt1 lst ;Step through points list
(if (< (setq dt (distance pt pt1)) dist) ;If distance is less
(setq dist dt ;Set new minimal distance
pt0 pt1 ;Set new closest point
) ;_ end of setq
) ;_ end of if
) ;_ end of foreach
pt0 ;Return the calculated closest point
) ;_ end of defun
Asks user for tolerance after selection. Defaults to 10.0, thereafter the tolerance defaults to previous. Also added some prompts to show totals & progress. As well as wraped the entire command in an UNDO group, so it can be undone in one step.