I have an old SWAP.LSP routine I don't know who made, but I'm making some modifications to it so It doesn't cancel out when you miss selecting an object. Only problem is that now it won't end. I have to hit escape to get out which make it difficult to use in other routines. Here is the code I have so far.
(defun C:MSWAP (/ YN) (initget "Yes No") (setq YN (getkword "\nSwap titleblock text? [Yes/No](Yes): ")) (if (= "No" YN) (and (setq YN nil) (princ "\nDone")) (while (/= "No" YN) (C:SWAP)) (setq YN nil)) (princ "\nDone") ) ;;----------------------------------------------- (defun c:swap (/ l1 lll1 ll1 lin1 line1 l2 lll2 ll2 line2 med1 med2) (setq l1 (nentsel "\nSelect First Line of text: ")) (while (= l1 nil)(setq l1 (nentsel "\nNo object selected: Select First Line of text: "))) (setq lll1 (car l1)) (redraw lll1 3) (setq ll1 (entget (car l1))) (setq lin1 (cdr (assoc 0 ll1))) (setq line1 (cdr (assoc 1 ll1))) (if (= line1 nil) (exit)) (setq l2 (nentsel "\nSelect Second Line of text: ")) (if (= l2 nil) (while (= l2 nil) (setq l2 (nentsel "\nNo object selected: Select Second Line of text: ")))) (while (/= l2 nil) (setq lll2 (car l2)) (redraw lll2 3) (setq ll2 (entget (car l2))) (setq lin2 (cdr (assoc 0 ll2))) (setq line2 (cdr (assoc 1 ll2))) (if (= line2 nil) (exit)) (setq med1 ll1) (setq med1 (subst (cons 1 line2) (assoc 1 med1) med1)) (entmod med1) (setq med2 ll2) (setq med2 (subst (cons 1 line1) (assoc 1 med2) med2)) (entmod med2) (setq l2 nil) (princ) ) (princ) )
Solved! Go to Solution.
Solved by Lee_Mac. Go to Solution.
I actually just accomplished this same task earlier today.
Here's how I did it....
I located the line of code that was accepting user input, the spot that they could "miss" at.
I put an (if in front of it, and i made EVERYTHING that followed....as the "then" part of the (if) conditional, it's own subroutine.
That way I could call it very simply AS LONG AS A VALID SELECTION WAS MADE.
Additionally, I was able to put an "ELSE" statement onto the if ...(princ "\n Missed, try again or ESC out")
As well as (C:MYROUTINE) following the conditional statement, so that the routine would re-initialize if the else fired, almost in a repeating loop that must be successful or cancelled out of.
I hope you're able to repeat these steps as it was quite simple!
That's what I did to get my loops started correctly on the existing SWAP.LSP. Only problem is that I can't have it use the escape key to exit. If I do my other routines will cancel out before they atre finnished running.
I would suggest the following:
(defun c:swap ( / e1 e2 ) (while (and (setq e1 (select-text "\nSelect 1st line of text <exit>: ")) (setq e2 (select-text "\nSelect 2nd line of text <exit>: ")) ) (setq e1 (entget e1) e2 (entget e2) ) (mapcar '(lambda ( a b ) (entmod (subst (assoc 1 a) (assoc 1 b) b))) (list e1 e2) (list e2 e1) ) ) (princ) ) (defun select-text ( msg / ent ) (while (progn (setvar 'errno 0) (setq ent (car (nentsel msg))) (cond ( (= 7 (getvar 'errno)) (princ "\nMissed, try again.") ) ( (null ent) nil) ( (not (wcmatch (cdr (assoc 0 (entget ent))) "ATTRIB,*TEXT")) (princ "\nPlease select either Text, MText or an Attribute.") ) ) ) ) ent ) (princ)
(defun C:MSWAP (/ YN) (while (or (initget "Yes No") (not (setq YN (getkword "\nSwap titleblock text? [Yes/No] <Yes>: "))) (= YN "Yes") );; or (C:SWAP) );; while (princ "\nDone") );; MSWAP
EDIT: Toooo slooowww...
HTH
Henrique
Worked like a charm. Thank you again Lee. You are truely the Mac...lol.
Lee would you be so kind as to comment your routine?
I see a few different methods here, would like to understand the logic in yours, specifically the
( (null ent) nil)
area.
Thanks 🙂
@gccdaemon wrote:Worked like a charm. Thank you again Lee. You are truely the Mac...lol.
Excellent - thank you Andrew
@Anonymous wrote:Lee would you be so kind as to comment your routine?
I see a few different methods here, would like to understand the logic in yours, specifically the
( (null ent) nil)area.
Thanks 🙂
I'm afraid I don't currently have time to comment the entire code, but to respond to your specific question:
The expression (null ent) is testing whether there is a non-nil value assigned to the symbol 'ent' (i.e. whether the nentsel function has returned a non-nil value); if the symbol ent evaluates to nil, this expression will return T.
By returning T, the test condition for the cond expression is validated, causing the remaining expressions within the condition to be evaluated - in this case, a single symbol 'nil', which of course evaluates to nil.
The cond expression then returns the last expression evaluated, returning nil to the progn expression, which in turn returns nil to the while loop, exiting the selection prompt loop.
Finally, since I have already checked whether the ent symbol is null, I can be confident that the function will return nil should the user right-click at the selection prompt.
Lee
Hi, thanks for sharing your code. I used it, works great!!, but when I try to swap field text, it dont work acurate. Some times it change the field to static text other it just swap 1 of 2 field inside the text and the remaning field are changed to static. There is a way to swap the text even if it have fields on it?
Thanks you
Can't find what you're looking for? Ask the community or share your knowledge.