Author Topic: Using cond and exiting loops  (Read 355 times)

0 Members and 1 Guest are viewing this topic.

AcoCar

  • Mosquito
  • Posts: 8
Using cond and exiting loops
« on: January 17, 2022, 07:28:59 AM »
Hi guys, maybe this will be a noob question, but i'm trying to better handle cond function...I have specific problem, i need to put/move object on a certain position which need to meets some conditons (no intersections), im defining 8 positions around object which will be tested, and if one is position meet my condiotions i put my object there. i'm thinking about defining every position, and checking with COND function. Will COND function automaticly exit if one condition is met or will it check every condition no matter what. I'm trying to optimize my routine because it will be done on very large sets of data (100k + elements)
Here is my current routine for  checking 4 positions. Any advices will be helpful  :-)

Code: [Select]
    (if (< l0 2)
      (progn
(ssadd en razmesteno))
      (progn
    (setq a1 (mapcar '+ MaxPnt (list 0.0 odst 0.0)))
    (setq b1 (mapcar '+ MinPnt (list 0.0 odst 0.0)))
    (setq p1 (ssget "_C" a1 b1))
    (if (= p1 nil)
      (setq l1 0)
      (setq l1 (sslength p1)));prva pozicija

    (setq a2 (mapcar '+ MaxPnt (list odst 0.0 0.0)))
    (setq b2 (mapcar '+ MinPnt (list odst 0.0 0.0)))
    (setq p2 (ssget "_C" a2 b2))
    (if (= p2 nil)
      (setq l2 0)
      (setq l2 (sslength p2)));druga pozicija

    (setq a3 (mapcar '- MaxPnt (list odst 0.0 0.0)))
    (setq b3 (mapcar '- MinPnt (list odst 0.0 0.0)))
    (setq p3 (ssget "_C" a3 b3))
    (if (= p3 0)
      (setq p3 0)
      (setq l3 (sslength p3)));treca pozicija

    (setq a4 (mapcar '- MaxPnt (list odst 0.0 0.0)))
    (setq b4 (mapcar '- MinPnt (list odst 0.0 0.0)))
    (setq p4 (ssget "_C" a4 b4))
    (if (= p4 0)
      (setq p4 0)
      (setq l4 (sslength p4)));cetvrta pozicija
    );progn
      );if

    (if (> l0 1)
      (progn
    (cond
      ((and (>= l0 2) (< l1 2)) (entmake (list (cons 0 "TEXT")
       (cons 1 tekst)
       (cons 40 vis)
       (cons 41 width)
       (cons 8 "Razmesteno")
       (cons 10 b1)))
       (entdel en)
       );prvi uslov
      ((and (>= l0 2) (>= l1 2) (< l2 2)) (entmake (list (cons 0 "TEXT")
       (cons 1 tekst)
       (cons 40 vis)
       (cons 41 width)
       (cons 8 "Razmesteno")
       (cons 10 b2)))
        (entdel en)
       );drugi uslov
      ((and (>= l0 2) (>= l1 2) (>= l2 2) (< l3 2)) (entmake (list (cons 0 "TEXT")
       (cons 1 tekst)
       (cons 40 vis)
       (cons 41 width)
       (cons 8 "Razmesteno")
       (cons 10 b3)))
        (entdel en)
       );treci uslov
      ((and (>= l0 2) (>= l1 2) (>= l2 2) (>= l3 2) (< l4 2)) (entmake (list (cons 0 "TEXT")
       (cons 1 tekst)
       (cons 40 vis)
       (cons 41 width)
       (cons 8 "Razmesteno")
       (cons 10 b4)))
        (entdel en)
       );cetvrti uslov
     );cond
    );progn
);if

dexus

  • Newt
  • Posts: 75
Re: Using cond and exiting loops
« Reply #1 on: January 17, 2022, 08:43:34 AM »
Yes, cond stops the first condition that is true.
You can test if for yourself like this:
Code: [Select]
(setq check t)
(cond
  (check (princ "first"))
  (check (princ "second"))
)
This wil only print first, and not second.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: Using cond and exiting loops
« Reply #2 on: January 17, 2022, 12:11:54 PM »
It is only a sample…
Code: [Select]
(if (>= l0 2)
  (progn
    (cond
      ( (< l1 2)
         ... ;prvi uslov
      )
      ( (>= l1 2)
        (cond
          ( (< l2 2)
            ... ;drugi uslov
          )
          ( (>= l2 2)
            (if (< l3 2))
               ... ;treci uslov
              (if (and  (>= l3 2) (< l4 2))
                ...;cetvrti uslov
              )
            )
          )
        )
      )
    );cond
  );progn
);if