Author Topic: Useful (if) (cond) snippet...  (Read 797 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Useful (if) (cond) snippet...
« on: February 25, 2024, 09:21:27 AM »
Hi all,
During revisions of my lisps, I've noticed that (cond) is faster than (if) construction, but further more, when converting (if) into (cond), CAD don't like t parameter for else from (if), but rather function for evaluation...

So this :
(if expression
  then
  else
)

Is better like :
(cond
  ( expression
    then
  )
  ( (= t t) ; but not just t - it works faster although it evaluates function...
    else
  )
)

You can see this in function in this *.ZIP posted here : https://www.theswamp.org/index.php?topic=41837.msg618428#msg618428
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Useful (if) (cond) snippet...
« Reply #1 on: February 25, 2024, 10:41:47 PM »
Hi all,
During revisions of my lisps, I've noticed that (cond) is faster than (if) construction, but further more, when converting (if) into (cond), CAD don't like t parameter for else from (if), but rather function for evaluation...

So this :
(if expression
  then
  else
)

Is better like :
(cond
  ( expression
    then
  )
  ( (= t t) ; but not just t - it works faster although it evaluates function...
    else
  )
)

You can see this in function in this *.ZIP posted here : https://www.theswamp.org/index.php?topic=41837.msg618428#msg618428


I had trouble with your premise, so  :

Code - Auto/Visual Lisp: [Select]
  1. (setq x 5
  2.           y 10
  3. )
  4.  
  5. (BenchMark2     2500
  6.                         '((cond
  7.                            ((> x y) (setq a x))
  8.                            (t (setq a y))
  9.                           )
  10.                          
  11.                           (if
  12.                            (> x y)
  13.                            (setq a x)
  14.                            (setq a y)
  15.                           )
  16.                          )
  17. )
  18.  
Code: [Select]
_$
Benchmarking-V2 :Boundary=1200 [M.P.2005 <revised kdub 2005,2014>] ...................
Elapsed milliseconds for 65536 iteration(s)/ relative Timing :

    (COND ((> X Y) (SETQ A X)) (T (SETQ ...).....1703 / 1.1123 <slowest>: 0.02598572ms Per iteration
    (IF (> X Y) (SETQ A X) (SETQ A Y))...........1531 / 1.0000 <fastest>: 0.02336121ms Per iteration

 
_$
Benchmarking-V2 :Boundary=2500 [M.P.2005 <revised kdub 2005,2014>] ....................
Elapsed milliseconds for 131072 iteration(s)/ relative Timing :

    (COND ((> X Y) (SETQ A X)) (T (SETQ ...).....3359 / 1.1082 <slowest>: 0.02562714ms Per iteration
    (IF (> X Y) (SETQ A X) (SETQ A Y))...........3031 / 1.0000 <fastest>: 0.02312469ms Per iteration




Regards,
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: Useful (if) (cond) snippet...
« Reply #2 on: February 26, 2024, 10:49:26 AM »
@kdub_nz
Why don't you try it on real sample - DWG...
I am attaching my version of testing DWG with times produced with (cond) - (roof2d-new-new-new-nnn.lsp) version - they are faster then with (if)... If I didn't removed it from *.ZIP posted here and I think I didn't (roof2d-new-new-new-nn.lsp) : https://www.theswamp.org/index.php?topic=41837.msg618428#msg618428 you can compare it with (if) - (roof2d-new-new-new-nn.lsp)
« Last Edit: April 07, 2024, 10:08:46 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

dexus

  • Bull Frog
  • Posts: 204
Re: Useful (if) (cond) snippet...
« Reply #3 on: February 26, 2024, 11:19:14 AM »
For my version of AutoCAD if seems to be faster and in cond a simple t is faster than (= t t).
So I cant replicate the results you found.

What version are you using?
Weren't you using Bricscad before?

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: Useful (if) (cond) snippet...
« Reply #4 on: February 26, 2024, 11:24:22 AM »
I rarely use AutoCAD 2022 as it's slower with *.lsp files and BricsCAD V24 and BricsCAD V23 as they are faster with *.lsp files...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10624
Re: Useful (if) (cond) snippet...
« Reply #5 on: February 26, 2024, 12:16:14 PM »
Not sure it matters much because we are talking about milliseconds difference but, I built a few test cases which test a few different uses. I have this saved as: benchmark_IFvCOND.lsp

Code - Auto/Visual Lisp: [Select]
  1. (defun benchmark2 (boundary   statements /;{{{
  2.                    _lset      _rset      _tostring
  3.                    _eval      _princ     _main
  4.                   )
  5.   (defun _lset (text len fillchar / padding result)
  6.     (setq padding (list (ascii fillchar))
  7.           result  (vl-string->list text)
  8.     )
  9.     (while
  10.       (< (length (setq padding (append padding padding)))
  11.          len
  12.       )
  13.     )
  14.     (while
  15.       (< (length (setq result (append result padding))) len)
  16.     )
  17.     (substr (vl-list->string result) 1 len)
  18.   )
  19.   (defun _rset (text len fillchar / padding result)
  20.     (setq padding (list (ascii fillchar))
  21.           result  (vl-string->list text)
  22.     )
  23.     (while
  24.       (< (length (setq padding (append padding padding)))
  25.          len
  26.       )
  27.     )
  28.     (while
  29.       (< (length (setq result (append padding result))) len)
  30.     )
  31.     (substr (vl-list->string result)
  32.             (1+ (- (length result) len))
  33.     )
  34.   )
  35.   (defun _tostring (x / result)
  36.     (if
  37.       (< (strlen (setq result (vl-prin1-to-string x))) 40)
  38.        result
  39.        (strcat (substr result 1 36) "..." (chr 41))
  40.     )
  41.   )
  42.   (defun _eval (statement iterations / start)
  43.     (gc)
  44.     (setq start (getvar "millisecs"))
  45.     (repeat iterations (eval statement))
  46.     (- (getvar "millisecs") start)
  47.   )
  48.   (defun _princ (x)
  49.     (princ x)
  50.     (princ)
  51.     ;; forces screen update
  52.   )
  53.   (defun _main (statements /
  54.                 ;;boundary
  55.                 iterations timings    slowest    fastest
  56.                 lsetlen    rsetlen    index      count
  57.                )
  58.     (setq ;; boundary 750                       ; 1000
  59.           iterations
  60.            1
  61.     )
  62.     (_princ
  63.       (strcat "Benchmarking-V2 :Boundary="
  64.               (itoa boundary)
  65.               " [M.P.2005 <revised kdub 2005,2014>] ..."
  66.       )
  67.     )
  68.     (while (or (< (apply 'max
  69.                          (setq timings
  70.                                 (mapcar '(lambda (statement)
  71.                                            (_eval statement iterations)
  72.                                          )
  73.                                         statements
  74.                                 )
  75.                          )
  76.                   )
  77.                   boundary
  78.                )
  79.                (< (apply 'min timings) boundary)
  80.            )
  81.       (setq iterations (* 2 iterations))
  82.       (_princ ".")
  83.     )
  84.     (_princ (strcat "\nElapsed milliseconds for "
  85.                     (itoa iterations)
  86.                     " iteration(s)"
  87.                     "/ relative Timing :\n\n"
  88.             )
  89.     )
  90.     (setq slowest (float (apply 'max timings))
  91.           fastest (float (apply 'min timings))
  92.     )
  93.     (setq
  94.       lsetlen (+ 5
  95.                  (apply
  96.                    'max
  97.                    (mapcar 'strlen
  98.                            (setq statements (mapcar '_tostring
  99.                                                     statements
  100.                                             )
  101.                            )
  102.                    )
  103.                  )
  104.               )
  105.     )
  106.     (setq
  107.       rsetlen (apply
  108.                 'max
  109.                 (mapcar '(lambda (ms) (strlen (itoa ms)))
  110.                         timings
  111.                 )
  112.               )
  113.     )
  114.     (setq index 0
  115.           count (length statements)
  116.     )
  117.     (foreach pair
  118.              (vl-sort (mapcar 'cons statements timings)
  119.                       '(lambda (a b) (> (cdr a) (cdr b)))
  120.              )
  121.       ((lambda (pair / ms)
  122.          (_princ
  123.            (strcat
  124.              "    "
  125.              (_lset (car pair) lsetlen ".")
  126.              (_rset (itoa (setq ms (cdr pair)))
  127.                     rsetlen
  128.                     "."
  129.              )
  130.              " / "
  131.              (rtos (/ ms fastest) 2 4)
  132.              (cond
  133.                ((eq 1 (setq index (1+ index)))
  134.                 (strcat " <slowest>: "
  135.                         (rtos (/ slowest iterations) 2 8)
  136.                         "ms Per iteration"
  137.                 )
  138.                )
  139.                ((eq index count)
  140.                 (strcat " <fastest>: "
  141.                         (rtos (/ fastest iterations) 2 8)
  142.                         "ms Per iteration"
  143.                 )
  144.                )
  145.                ("")
  146.              )
  147.              "\n"
  148.            )
  149.          )
  150.        )
  151.         pair
  152.       )
  153.     )
  154.     (princ)
  155.   )
  156.   (_main statements)
  157. );}}}
  158.  
  159.  
  160. (defun test_if-ret ( x y );{{{
  161.   ;; This test should return the greater than value
  162.   ;; using IF as a test.
  163.   ;;
  164.   ;; ARGS:
  165.   ;;    x       - a value
  166.   ;;    y       - a value
  167.   ;;
  168.   ;; RETURNS:
  169.   ;;    The greater value (X or Y)
  170.   ;;
  171.   ;; EX:
  172.   ;;    (test_if 5 10)
  173.   ;;    > 10
  174.   (if (> x y)
  175.     x
  176.     y));}}}
  177.  
  178. (defun test_cond-ret ( x y );{{{
  179.   ;; This test should return the greater than value
  180.   ;; using COND as a test.
  181.   ;;
  182.   ;; ARGS:
  183.   ;;    x       - a value
  184.   ;;    y       - a value
  185.   ;;
  186.   ;; RETURNS:
  187.   ;;    The greater value (X or Y)
  188.   ;;
  189.   ;; EX:
  190.   ;;    (test_if 5 10)
  191.   ;;    > 10
  192.   (cond
  193.     ((> x y) x)
  194.     (y)));}}}
  195.  
  196. (defun test_if-set-one ( x y / a);{{{
  197.   ;; This test should return create a variable to the greater than value
  198.   ;; using IF as a test.
  199.   ;;
  200.   ;; ARGS:
  201.   ;;    x       - a value
  202.   ;;    y       - a value
  203.   ;;
  204.   ;; RETURNS:
  205.   ;;    The greater value (X or Y)
  206.   ;;
  207.   ;; EX:
  208.   ;;    (test_if-set-one 5 10)
  209.   ;;    > 10
  210.   (if (> x y)
  211.     (setq a x)
  212.     (setq a y)));}}}
  213.  
  214. (defun test_cond-set-one (x y / a);{{{
  215.   ;; This test should return create a variable to the greater than value
  216.   ;; using COND as a test.
  217.   ;;
  218.   ;; ARGS:
  219.   ;;    x       - a value
  220.   ;;    y       - a value
  221.   ;;
  222.   ;; RETURNS:
  223.   ;;    The greater value (X or Y)
  224.   ;;
  225.   ;; EX:
  226.   ;;    (test_cond-set-one 5 10)
  227.   ;;    > 10
  228.   (setq a
  229.         (cond
  230.           ((> x y) x)
  231.           (y))) );}}}
  232.  
  233. (defun test_if-set-two (x y v);{{{
  234.   ;; This test should return create a variable to the greater than value
  235.   ;; using IF as a test.
  236.   ;;
  237.   ;; ARGS:
  238.   ;;    x       - a value
  239.   ;;    y       - a value
  240.   ;;    v       - a variable to create
  241.   ;;
  242.   ;; RETURNS:
  243.   ;;    The greater value (X or Y)
  244.   ;;
  245.   ;; EX:
  246.   ;;    (test_if-set-two x y 'a)
  247.   ;;    > 10
  248.   (if (> x y)
  249.     (set v x)
  250.     (set v y)));}}}
  251.  
  252. (defun test_cond-set-two (x y v);{{{
  253.   ;; This test should return create a variable to the greater than value
  254.   ;; using COND as a test.
  255.   ;;
  256.   ;; ARGS:
  257.   ;;    x       - a value
  258.   ;;    y       - a value
  259.   ;;    v       - a variable to create
  260.   ;;
  261.   ;; RETURNS:
  262.   ;;    The greater value (X or Y)
  263.   ;;
  264.   ;; EX:
  265.   ;;    (test_cond-set-two 10 5 'a)
  266.   ;;    > 10
  267.   (set v
  268.         (cond
  269.           ((> x y) x)
  270.           (y))) );}}}
  271.  
  272.  
  273. (setq x 5
  274.       y 10
  275.       )
  276. (defun reset ()
  277.   ;; -sets the vaiables defined in this test to NIL.
  278.   (setq a nil
  279.         x nil
  280.         y nil
  281.         functionlist nil
  282.         ExpectedResult nil)
  283.   )
  284. (defun cube (x) (* x x x))      ;| -cube a number |;
  285. (setq dx 0.00001)               ;| -something small |;
  286. (defun deriv (g)
  287.   ;; create a function which should be evaluated using some syntax trickery
  288.   ;; from SCHEME.
  289.   (eval
  290.     (list 'lambda (list 'x)
  291.           (list '/ (list '- (list g (list '+ 'x 'dx)) (list g 'x))
  292.                 'dx)
  293.           )
  294.     )
  295.   )
  296.  
  297.  
  298.  
  299.   ;;
  300.   ;; Testing
  301.   (set '#test
  302.        (lambda ( result expected )
  303.          (equal result expected)))
  304.   (set '#functioncheck
  305.        (lambda ( process expected )
  306.          (if (#test (eval process) expected)
  307.            (set 'functionlist (cons process functionlist)))))
  308.  
  309.   ;;
  310.   ;; Check the results and add the passed
  311.   ;; funcitons to the benhmark list
  312.  
  313.   (setq ExpectedResult 10)
  314.  
  315.   (#functioncheck '(test_if-ret x y)                                          ExpectedResult)
  316.   (#functioncheck '(test_cond-ret x y)                                        ExpectedResult)
  317.   (#functioncheck '(test_if-set-one x y)                                      ExpectedResult)
  318.   (#functioncheck '(test_cond-set-one x y)                                    ExpectedResult)
  319.   (#functioncheck '(test_if-set-two x y 'a)                                   ExpectedResult)
  320.   (#functioncheck '(test_cond-set-two x y 'a)                                 ExpectedResult)
  321.  
  322.   ;;
  323.   ;; Test the functions by making them do some evaluating of functions.
  324.   (setq ExpectedResult 300)
  325.  
  326.   (#functioncheck '(test_if-ret ((deriv 'cube) x) ((deriv 'cube) y))          ExpectedResult)
  327.   (#functioncheck '(test_cond-ret ((deriv 'cube) x) ((deriv 'cube) y))        ExpectedResult)
  328.   (#functioncheck '(test_if-set-one ((deriv 'cube) x) ((deriv 'cube) y))      ExpectedResult)
  329.   (#functioncheck '(test_cond-set-one ((deriv 'cube) x) ((deriv 'cube) y))    ExpectedResult)
  330.   (#functioncheck '(test_if-set-two ((deriv 'cube) x) ((deriv 'cube) y) 'a)   ExpectedResult)
  331.   (#functioncheck '(test_cond-set-two ((deriv 'cube) x) ((deriv 'cube) y) 'a) ExpectedResult)
  332.  
  333.   ;;
  334.   ;; Run the benchmark
  335.   (BenchMark2 2500 functionlist)
  336.  
  337.   ;;
  338.   ;; Rest all the variables created by this test.
  339.   (reset)
  340.   )
  341.  

Results:
Code: [Select]
Benchmarking-V2 :Boundary=2500 [M.P.2005 <revised kdub 2005,2014>] .....................
Elapsed milliseconds for 262144 iteration(s)/ relative Timing :

    (TEST_COND-RET ((DERIV (QUOTE CUBE))...).....17594 / 6.2214 <slowest>: 0.06711578ms Per iteration
    (TEST_IF-SET-TWO ((DERIV (QUOTE CUBE...).....16468 / 5.8232
    (TEST_COND-SET-ONE ((DERIV (QUOTE CU...).....16438 / 5.8126
    (TEST_COND-SET-TWO ((DERIV (QUOTE CU...).....16422 / 5.8069
    (TEST_IF-SET-ONE ((DERIV (QUOTE CUBE...).....16344 / 5.7793
    (TEST_IF-RET ((DERIV (QUOTE CUBE)) X...).....16187 / 5.7238
    (TEST_IF-SET-TWO X Y (QUOTE A))...............3141 / 1.1107
    (TEST_COND-SET-TWO X Y (QUOTE A)).............3078 / 1.0884
    (TEST_COND-SET-ONE X Y).......................2922 / 1.0332
    (TEST_COND-RET X Y)...........................2860 / 1.0113
    (TEST_IF-SET-ONE X Y).........................2859 / 1.0110
    (TEST_IF-RET X Y).............................2828 / 1.0000 <fastest>: 0.01078796ms Per iteration
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: Useful (if) (cond) snippet...
« Reply #6 on: February 26, 2024, 12:46:02 PM »
I get this in BricsCAD V23...

Code: [Select]
Benchmarking-V2 :Boundary=2500 [M.P.2005 <revised kdub 2005,2014>] ...........................
Elapsed milliseconds for 16777216 iteration(s)/ relative Timing :

    (TEST_COND-SET-TWO X Y 'A).....6094 / 1.2542 <slowest>: 0.00036323ms Per iteration
    (TEST_IF-SET-TWO X Y 'A).......6000 / 1.2348
    (TEST_COND-SET-ONE X Y)........5109 / 1.0515
    (TEST_IF-SET-ONE X Y)..........4984 / 1.0257
    (TEST_COND-RET X Y)............4890 / 1.0064
    (TEST_IF-RET X Y)..............4859 / 1 <fastest>: 0.00028962ms Per iteration

But on actuall real examples *.DWG it looks that milliseconds are smaller with (cond) than (if)...
See for yourself if you have a spare time...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Useful (if) (cond) snippet...
« Reply #7 on: February 26, 2024, 01:38:49 PM »
Regarding the (= T T) conditional statement usage :

Code: [Select]
10 Benchmarking-V2 :Boundary=2500 [M.P.2005 <revised kdub 2005,2014>] ....................
Elapsed milliseconds for 131072 iteration(s)/ relative Timing :

    (COND ((> X Y) (SETQ A X)) ((= T T) ...).....3562 / 1.1453 <slowest>: 0.02717590ms Per iteration
    (COND ((> X Y) (SETQ A X)) (T (SETQ ...).....3297 / 1.0601
    (IF (> X Y) (SETQ A X) (SETQ A Y))...........3110 / 1.0000 <fastest>: 0.02372742ms Per iteration


AND
Code: [Select]
_$

10 Benchmarking-V2 :Boundary=2500 [M.P.2005 <revised kdub 2005,2014>] ....................
Elapsed milliseconds for 131072 iteration(s)/ relative Timing :

    (COND ((> X Y) (SETQ A X)) ((= T T) ...).....3609 / 1.0497 <slowest>: 0.02753448ms Per iteration
    (COND ((> X Y) (SETQ A X)) (T (SETQ ...).....3438 / 1.0000 <fastest>: 0.02622986ms Per iteration

 

In VS 2023
Draw your own conclusions
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: Useful (if) (cond) snippet...
« Reply #8 on: February 26, 2024, 01:47:26 PM »
Yes according to benchmarks that is true, but I left (= t t) as everyone can simply change it with ctrl+H into just t...
And for twighlight zone, I suggest you that you draw something and compare "roof2d-new-new-new-nn.lsp" with "roof2d-new-new-new-nnn.lsp"... I left bunch of examples which routine had to pass to be sure it's working as desired... DWG is here : https://www.theswamp.org/index.php?topic=59312.msg619513#msg619513

Have a nice day to all and stay well...
M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10624
Re: Useful (if) (cond) snippet...
« Reply #9 on: February 26, 2024, 03:59:45 PM »
Given the slowest at 0.0275ms per iteration, and there being, maybe, a handful of iteration in real-world. I'd have to say my conclusion, is: "*meh* I choose readability."
Command: (/ 0.0275 1000000)
0.0000000275 [seconds per iteration]

I checked out that post (not the drawing) and that code you made is over my head; there are zero comments in it, so I don't know what most of the code does. Sorry.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Useful (if) (cond) snippet...
« Reply #10 on: February 26, 2024, 06:21:56 PM »
Marco, If you want to demonstrate why you believe your deduction is consistantly true please provide examples.

Don't expect ( me ) to pull apart your code and your specific drawing to test for myself.

If the run-time of your code is significantly and noticibly different perhaps look at the code structure for each variation.
I'm guessing there is more at play here than the simple choice between using if and cond.

Regards,

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: Useful (if) (cond) snippet...
« Reply #11 on: February 27, 2024, 09:42:35 AM »
I checked out that post (not the drawing) and that code you made is over my head; there are zero comments in it, so I don't know what most of the code does. Sorry.

Look John,
while coding I've done numerious checkings on various examples to make sure it'll pass through all that may user run during his/her examples/work... So I really didn't care of describing what particular line do - I thought it's obvious especially with that (if) version with just few (cond)s where they are neccesity... As you see, I also have difficulty to go through it again and comment it, but I think that with good formatting which is my standard way of coding I assume that everyone can find a way to understand what it is doing at that particular paragraph... All open brackets have their closing one at the same column, so it is nicely written and without preetify implemented in BricsCAD BLADE... To be honest and to understand what it does you have to download and *.DWG posted here : https://www.theswamp.org/index.php?topic=59312.msg619513#msg619513
DWG is also good and for checking theorehm that (cond) is faster than (if) no matter what results of benchmarking showed...
Anyway, thanks for interest in this challenge - perhaps that's why everyone step aside when trying to make his/her own version... I offered my and mod. chlh_jd's one, but without downloading testing *.DWG you can only play around and realize that there is part of true when I assumed (cond) faster than (if)...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10624
Re: Useful (if) (cond) snippet...
« Reply #12 on: February 27, 2024, 12:24:04 PM »
Im sorry, I don't mean any offence, it's just that it is a little cryptic and I find it hard to follow.

Take the first helper funcion (not the error handler of course). I just don't understand the objectives behind the seemingly over complication in it.

Code - Auto/Visual Lisp: [Select]
  1. (defun vl-load nil
  2.   (or cad
  3.         (setq cad (vlax-get-acad-object))
  4.         (progn
  5.           (vl-load-com)
  6.           (setq cad (vlax-get-acad-object))
  7.           )
  8.         )
  9.       )
  10.   (or doc (setq doc (vla-get-activedocument cad)))
  11.   (or alo (setq alo (vla-get-activelayout doc)))
  12.   (or spc (setq spc (vla-get-block alo)))
  13.   )

ALSO: I used my text editor to list all the instances of `cad`, `doc` etc and the only place they are used is the following line:
...

(or (and cad doc alo spc) (vl-load))

Besides the point that OR and AND are logical functions it makes very little sense; in that, if you want/need the objects then get them (you have to release and localize the varaibles anyways) so why not just define the variables and release and null them in your error handler?

So, for an example about why I say cryptic:

Code - Auto/Visual Lisp: [Select]
  1. ((lambda ()
  2.    ;; get the `doc` object.
  3.  
  4.    ;; This will check the DOC variable and if it NOT nil, return it otherwise
  5.    ;; if it is nil then define it.
  6.    (or doc
  7.        (setq doc
  8.    ; ... code
  9.  
  10.    (vlax-release-object doc)            ; -release the object (OOPS!).
  11.  
  12.    (or doc (vla-get-activelayout doc))  ; -what does OR do for you?
  13.                                         ;  COND makes "more" sense but
  14.                                         ;  still doesnt protect you from
  15.                                         ;  errors like this.
  16.    )
  17.  )

OR is stupid! It returns T or NIL.

You could build a function like below...

Code - Auto/Visual Lisp: [Select]
  1. (defun vl-load ()
  2.   ;; Gets the object model.
  3.   ;; Defines the follwoing variables: cad doc alo
  4.   ;;
  5.   ;; EX:
  6.   ;;    (vl-load)
  7.         doc (vla-get-activedocument cad)
  8.         alo (vla-get-activelayout doc)
  9.         )
  10.   )


But what is wrong with simply creating the variables.

Code - Auto/Visual Lisp: [Select]
  1.       doc (vla-get-activedocument cad)
  2.       ; ...
  3.       )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Re: Useful (if) (cond) snippet...
« Reply #13 on: February 27, 2024, 01:06:53 PM »
@John,
cad, doc, alo, spc are supposed to be global variables - that's why (vl-load) sub...
(or (all of them are global) - no need to do (vl-load) - or returns t imidiately and finish that line...
and opposite
(or (none of them are global) - or returns nil and proceed to (vl-load) where they are assigned as globals...
This construction I used in my LISP template example posted here : https://www.theswamp.org/index.php?topic=57865.0

But in those lisps I didn't wanted to use all defined in template, just simplification of things, so that's why (vl-load)...
I imagine you have 2 polygonal LWPOLYLINES... Firstly globals are defined, and secondly on second LWPOLYLINE, they are already there globals, and process is just slightly faster... IMHO there is no need to release them... When session is over, they are released anyway...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube