Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Text Swap LSP Help

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
gccdaemon
1056 Views, 11 Replies

Text Swap LSP Help

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)
)

 

Andrew Ingram
Civil 3D x64 2019
Win 10 x64 Pro
Intel Xeon E5-1620
32 GB Ram
11 REPLIES 11
Message 2 of 12
bhull1985
in reply to: gccdaemon

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!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 3 of 12
gccdaemon
in reply to: bhull1985

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.

Andrew Ingram
Civil 3D x64 2019
Win 10 x64 Pro
Intel Xeon E5-1620
32 GB Ram
Message 4 of 12
Lee_Mac
in reply to: gccdaemon

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)

 

Message 5 of 12
hmsilva
in reply to: gccdaemon

(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

EESignature

Message 6 of 12
gccdaemon
in reply to: Lee_Mac

Worked like a charm. Thank you again Lee. You are truely the Mac...lol.

Andrew Ingram
Civil 3D x64 2019
Win 10 x64 Pro
Intel Xeon E5-1620
32 GB Ram
Message 7 of 12
bhull1985
in reply to: Lee_Mac

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 🙂

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 8 of 12
stevor
in reply to: bhull1985

Not lee here, but until then: "The usual style guide is to use NOT for logical negation and NULL for checking if a list is empty." http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/not-amp-maybe-null-function-bug-or-pe... and is done towards the beginning of a COND state test.
S
Message 9 of 12
bhull1985
in reply to: stevor

Thanks Stevor, I'll study it now.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 10 of 12
Lee_Mac
in reply to: gccdaemon


@gccdaemon wrote:

Worked like a charm. Thank you again Lee. You are truely the Mac...lol.


Excellent - thank you Andrew Smiley Very Happy

Message 11 of 12
Lee_Mac
in reply to: bhull1985


@bhull1985 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

Message 12 of 12
jhonatan.barreda
in reply to: Lee_Mac

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.

Post to forums  

Autodesk Design & Make Report

”Boost