Whether or not you have it tell them anything more than to repeat a "Select...whatever...." prompt
is up to you, and what it's used on doesn't really matter, but if you don't want it to crap out on
them if they miss, you're pretty much going to have to have some kind of a (while (not ... ))
situation [*including* the (not) part -- not just the (while) part]. In other words, as long as it
*doesn't* get something it can apply (entget) to, it needs to loop back and ask again. As you have
it, if the User misses, the (setq) is going return nil, and that's what's going to crash the
routine, because the (while) function will give up [what it's checking for returned nil, and that
means skip to the end]. You need something that returns *other than* nil if they *miss*, and that's
what the (not) function does in the things I posted --
(entsel) returns nil to (setq) because nothing was selected;
(setq) therefore assigns nil to the variable, and also returns nil to (not);
(not) therefore returns T to (while), because what it was checking for returned nil;
(while) therefore "goes ahead" to the next step within itself, which is to ask them to try again,
and loops back for another shot.
But when they *do* succeed in picking something, here's what happens:
(setq) gets from (entsel) something *other than nil*, and also returns that to (not);
(not) therefore, having gotten a non-nil response, returns nil to (while);
(while) therefore jumps to its end, bypassing the try-again part;
and all the *processing* of the selected object happens *after* the end of the (while) loop, *not*
within it.
Your (while) functions are set up in a way that makes them fundamentally no different than (if)
functions [with (progn) heading up the 'then' arguments so they can do more than one thing], because
there's no "loop" opportunity. But (while) is made to check on something that might return nil or
otherwise, *and* if that check is satisfied, when it gets to the end, there's some point in going
back and checking it again, hoping it will then be nil, so the (while) loop can end. So yours may
work if they hit something [as long as it's the right kind of thing, and so on], but as with an (if)
function with no 'else' argument, when they miss, nothing happens. And [again if they hit
something], I suspect the (while) loops get to the end, go back as (while) functions are meant to
do, and ask the User to select something all over again unnecessarily, because once you've done what
you want to do, *there's nothing to "nil out" what (while) is checking for* so that the surrounding
routine can move on. The (not) functions nested inside the (while) functions in my suggestions are
what do that.
--
Kent Cooper
snkunz wrote...
I've tried a few variations to what you've shown here but I must not be getting it quite right.
I'll post the program that I am working on. Not really concerned with it telling them what they did
wrong since the function is intended only to be used on text anyway
(defun c:textswap2 (/ source CopyTargetName CopyTargetList b en)
(setvar "cmdecho" 0)
(prompt "\nSelect text you wish to copy: ")
(eln)
(setq objecttype (cdr (assoc 0 el)))
(if (= objecttype "MULTILEADER")
(multileaderswap)(originalswap))
(if en
(progn
(redraw en 4)
(setq en nil)
) ;end progn
) ;end if
(if (not CopyTargetName)
(progn
(princ "\nProgram Complete!")
(princ)
) ;end progn
) ;end if
(setvar "cmdecho" 1)
(princ)
) ;end function textswap2
(defun multileaderswap ()
(setq source (assoc 304 el))
(redraw (setq en (cdr (assoc -1 el))) 3)
(while (setq CopyTargetName (car (entsel "\nSelect text to copy to: ")))
(setq CopyTargetList (entget CopyTargetName)
b (assoc 304 CopyTargetList)
) ;end setq
(setq CopyTargetList (subst source b CopyTargetList))
(entmod CopyTargetList)
) ;end while
);end function multileaderswap
(defun originalswap ()
(setq source (assoc 1 el))
(redraw (setq en (cdr (assoc -1 el))) 3)
(while (setq CopyTargetName (car (entsel "\nSelect text to copy to: ")));end setq
(setq CopyTargetList (entget CopyTargetName)
b (assoc 1 CopyTargetList)
) ;end setq
(setq CopyTargetList (subst source b CopyTargetList))
(entmod CopyTargetList)
) ;end while
);end function originalswap