You could 'save' the existing current layer.. e.g.
(setq olay (getvar 'clayer))
Then reset it on exit .. e.g.
(setvar 'clayer olay)
For your request to set to Layer "Text" on exit, try following.
(defun C:DDP (/ cc1 cc2 range spcs spc)
; = DIVide Distance [not object] using Points, at user-specified Maximum spacing
(command "-layer" "s" "Rivet Hole" "")
(initget (if *DIVDPmax* 6 7)); no zero, no negative, no Enter on first use
(setq
*DIVDPmax* ; global variable with routine-specific name
(cond
( (getdist ; returns nil on Enter when permitted
(strcat
"\nMaximum Point spacing"
(if *DIVDPmax* (strcat " <" (rtos *DIVDPmax*) ">") "")
": "
); strcat
); getdist
); User-input condition
(*DIVDPmax*); prior value if present, on Enter
); cond & *DIVDPmax*
p1 (getpoint "\nOne end of range: ")
p2 (getpoint "\nOther end: ")
range (distance p1 p2)
spcs (+ (fix (/ range *DIVDPmax*)) (if (equal (rem range *DIVDPmax*) 0 1e-4) 0 1))
spc (/ range spcs)
); setq
(setvar 'lastpoint p1); kind of interesting that that's allowed...
(repeat (1- spcs)
(command "_.point" "_none" (polar (getvar 'lastpoint) (angle p1 p2) spc))
); repeat
;; Add the next 2 lines
(command "_.layer" "_M" "TEXT" ""); Case no Layer Text exists (nil if exists)
(command "_.layer" "_S" "TEXT" ""); Set to Layer Text
); defun
ECCAD