Code Red > AutoLISP (Vanilla / Visual)

edited: Swap Block routine using ((cond (T...success

(1/1)

jlogan02:
My first attempt at ((cond...(T

Using this discussion from CAB and Lee Mac...


--- Code - Auto/Visual Lisp: ---(cond ((= 1 0)   none of this will get executed   because the conditions is false ) ; end cond 1...  ...(T    This is often placed at the last position    in a condition statement and will be executed    if all the prior conditions are false    if any one of the above conditions are true     this will not be executed  ) ; end cond 4) ; end cond stmt
That last line in the discussion about (T?
If my first condition is false, anything following (T will execute. It took me forever to get it to work. I need to pay attention to parenthesis use.
I was missing the double paren at ((setq after (cond

Thanks for the help from 2008 CAB and Lee. Good stuff..


--- Code - Auto/Visual Lisp: ---(command "_layer" "MAKE" "TBLK_BORD_LINES" "COLOR" "WHITE" "" "UNLOCK" "TBLK_BORD_LINES,TBLK_BORD_TEXT" "")   (cond          ((setq blk1 (ssget "x" '((2 . "TBLK_BORD_CTL"))))                        (repeat (setq n (sslength blk1))                         (setq edata (entget (ssname blk1 (setq n (1- n)))))                         (entmod (subst '(2 . "TBLK_BORD_MAX") '(2 . "TBLK_BORD_CTL") edata))                  );; repeat           );;cond        (T          (setq blk2 (ssget "x" '((2 . "TBLK_BORD_MAX"))))                  (repeat (setq n1 (sslength blk2))                    (setq edata1 (entget (ssname blk2 (setq n1 (1- n1)))))                    (entmod (subst '(2 . "TBLK_BORD_CTL") '(2 . "TBLK_BORD_MAX") edata1))             );;repeat    );;T        );;cond    (command "_LAYER" "LOCK" "TBLK_BORD_LINES,TBLK_BORD_TEXT""")(princ) );;defun
Any suggestions are welcome...

J. Logan



Lee Mac:
Note that the use of T in the final cond condition is not strictly necessary and is only typically included to provide a default branch should the test expressions for all other conditions fail to be validated.

For your particular example, I would suggest excluding the T from your second condition since if the second ssget expression returns nil, the second condition will continue to be evaluated and the subsequent sslength expression will return an error.

Instead, I would suggest something like the following:

--- Code - Auto/Visual Lisp: ---(cond    (   (setq s (ssget "_X" '((2 . "TBLK_BORD_CTL"))))        (repeat (setq i (sslength s))            (entmod (subst '(2 . "TBLK_BORD_MAX") '(2 . "TBLK_BORD_CTL") (entget (ssname s (setq i (1- i))))))        )    )    (   (setq s (ssget "_X" '((2 . "TBLK_BORD_MAX"))))        (repeat (setq i (sslength s))            (entmod (subst '(2 . "TBLK_BORD_CTL") '(2 . "TBLK_BORD_MAX") (entget (ssname s (setq i (1- i))))))        )    )    (   (princ "\nNeither \"TBLK_BORD_CTL\" nor \"TBLK_BORD_MAX\" blocks were found.")))

jlogan02:
It's funny you say that.

Just a bit ago I popped into a drawing to test in an existing drawing and it only had the CTL border but not the MAX. Fail!!!

To guard against this, should I add checking for and insertion of the borders if they don't exist before hand?

and

Are you saying that in reality the T branch would really be used in an instance where there were more than one (cond and all of them might fail?

J. Logan

Navigation

[0] Message Index

Go to full version