Author Topic: edited: Swap Block routine using ((cond (T...success  (Read 154 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Newt
  • Posts: 96
edited: Swap Block routine using ((cond (T...success
« on: August 17, 2019, 03:16:11 PM »
My first attempt at ((cond...(T

Using this discussion from CAB and Lee Mac...

Code - Auto/Visual Lisp: [Select]
  1.  ((= 1 0)
  2.    none of this will get executed
  3.    because the conditions is false
  4.  ) ; end cond 1...
  5.  
  6.  ...(T
  7.     This is often placed at the last position
  8.     in a condition statement and will be executed
  9.     if all the prior conditions are false
  10.     if any one of the above conditions are true
  11.      this will not be executed
  12.   ) ; end cond 4
  13. ) ; 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: [Select]
  1. (command "_layer" "MAKE" "TBLK_BORD_LINES" "COLOR" "WHITE" "" "UNLOCK" "TBLK_BORD_LINES,TBLK_BORD_TEXT" "")
  2.    (cond
  3.           ((setq blk1 (ssget "x" '((2 . "TBLK_BORD_CTL"))))
  4.                         (repeat (setq n (sslength blk1))
  5.                          (setq edata (entget (ssname blk1 (setq n (1- n)))))
  6.                          (entmod (subst '(2 . "TBLK_BORD_MAX") '(2 . "TBLK_BORD_CTL") edata))
  7.                   );; repeat
  8.            );;cond
  9.         (T
  10.           (setq blk2 (ssget "x" '((2 . "TBLK_BORD_MAX"))))
  11.                   (repeat (setq n1 (sslength blk2))
  12.                     (setq edata1 (entget (ssname blk2 (setq n1 (1- n1)))))
  13.                     (entmod (subst '(2 . "TBLK_BORD_CTL") '(2 . "TBLK_BORD_MAX") edata1))
  14.              );;repeat
  15.     );;T
  16.         );;cond
  17.  
  18.   (command "_LAYER" "LOCK" "TBLK_BORD_LINES,TBLK_BORD_TEXT""")
  19.  
  20. );;defun

Any suggestions are welcome...

J. Logan



« Last Edit: August 17, 2019, 03:48:32 PM by jlogan02 »
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.

Lee Mac

  • Seagull
  • Posts: 12253
  • London, England
Re: edited: Swap Block routine using ((cond (T...success
« Reply #1 on: August 17, 2019, 03:54:46 PM »
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: [Select]
  1.     (   (setq s (ssget "_X" '((2 . "TBLK_BORD_CTL"))))
  2.         (repeat (setq i (sslength s))
  3.             (entmod (subst '(2 . "TBLK_BORD_MAX") '(2 . "TBLK_BORD_CTL") (entget (ssname s (setq i (1- i))))))
  4.         )
  5.     )
  6.     (   (setq s (ssget "_X" '((2 . "TBLK_BORD_MAX"))))
  7.         (repeat (setq i (sslength s))
  8.             (entmod (subst '(2 . "TBLK_BORD_CTL") '(2 . "TBLK_BORD_MAX") (entget (ssname s (setq i (1- i))))))
  9.         )
  10.     )
  11.     (   (princ "\nNeither \"TBLK_BORD_CTL\" nor \"TBLK_BORD_MAX\" blocks were found."))
  12. )

jlogan02

  • Newt
  • Posts: 96
Re: edited: Swap Block routine using ((cond (T...success
« Reply #2 on: August 17, 2019, 04:04:14 PM »
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
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.