Author Topic: How to avoid direction of picking points?  (Read 6162 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
How to avoid direction of picking points?
« on: June 10, 2012, 07:49:35 AM »
How to make always left point as pt and right point as p2 even I picked from right to left?
Code: [Select]
(and (setq p1 (getpoint "\nPick first point."))
     (setq p2 (getpoint p1 "\nPick second point."))   
   )

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: How to avoid direction of picking points?
« Reply #1 on: June 10, 2012, 08:04:56 AM »

(if (< (car p2) (car p1))
    (mapcar 'set '(p1 p2) '(p2 p1))
)


Code - Auto/Visual Lisp: [Select]
  1. (if (< (car p2) (car p1))
  2.     (mapcar 'set '(p1 p2) (list p2 p1))
  3. )

Or

Code - Auto/Visual Lisp: [Select]
  1. (if (< (car p2) (car p1))
  2.     (setq tmp p1 p1 p2 p2 tmp)
  3. )
« Last Edit: June 10, 2012, 09:18:58 AM by Lee Mac »

pBe

  • Bull Frog
  • Posts: 402
Re: How to avoid direction of picking points?
« Reply #2 on: June 10, 2012, 08:17:58 AM »
Code - Auto/Visual Lisp: [Select]
  1. (and (setq pk nil  p1 (getpoint "\nPick first point."))
  2.      (while (and (not pk) (setq p2 (getpoint p1 "\nPick second point.")))
  3.                 (if (< (car p1) (car p2))
  4.                         (setq pk T) (princ "\nOut of bounds:")
  5.                         )(princ)
  6.                 )
  7.      )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to avoid direction of picking points?
« Reply #3 on: June 10, 2012, 08:44:24 AM »
Code - Auto/Visual Lisp: [Select]
  1. (if (< (car p2) (car p1))
  2.     (mapcar 'set '(p1 p2) '(p2 p1))
  3. )
Just a slight mod on your code:
Code - Auto/Visual Lisp: [Select]
  1. (if (< (car p2) (car p1))
  2.   (mapcar 'set '(p1 p2) (list p2 p1)))
Otherwise P1 equals the symbol P2, and P2 equals the symbol P1. Even if you then use eval, you simply get:
Code - Auto/Visual Lisp: [Select]
  1. (eval p1) ;Returns P1
  2. (eval (eval p1)) ;Returns P2
  3. (eval p2) ;Returns P2
  4. (eval (eval p2)) ;Returns P1
You've effectively removed the actual point values and replaced them with symbols.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: How to avoid direction of picking points?
« Reply #4 on: June 10, 2012, 09:17:59 AM »
Oops! Yes, that's what I meant to write - thanks Irneb  :-)

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: How to avoid direction of picking points?
« Reply #5 on: June 10, 2012, 09:43:16 AM »
...
Code - Auto/Visual Lisp: [Select]
  1. (if (< (car p2) (car p1))
  2.     (setq tmp p1 p1 p2 p2 tmp)
  3. )
...

perfect but with little bit change
Code - Auto/Visual Lisp: [Select]
  1. (if (< (car p2) (car p1))
  2.                (setq tmp p1 p1 p2 p2 tmp )
  3.               (setq p1 p1 p2 p2 )
  4.              )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to avoid direction of picking points?
« Reply #6 on: June 10, 2012, 09:44:25 AM »
No prob ... we all make mistakes ... me quite a lot!  :-[ You had the idea, I just fixed a minor typo!

I think this is one of those cases where using mapcar in a different manner than it's "supposed" to be used makes for more elegant code. I'm trying to imagine how to do this with foreach instead, but the code becomes convoluted since you first need to combine 2 lists in order to use them both together in foreach. Something like this:
Code - Auto/Visual Lisp: [Select]
  1. (if (< (car p2) (car p1))
  2.   (foreach item (mapcar 'list '(p1 p2) (list p2 p1))
  3.     (apply 'set item)))
"Supposedly" more correct, but sooo much more typing, and gaining nothing in return! :ugly:

Cudos for the most elegant and most "functional" version!
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to avoid direction of picking points?
« Reply #7 on: June 10, 2012, 09:45:52 AM »
perfect but with little bit change
Code - Auto/Visual Lisp: [Select]
  1. (if (< (car p2) (car p1))
  2.           (setq tmp p1 p1 p2 p2 tmp )
  3.          (setq p1 p1 p2 p2 )
  4.         )
You don't strictly need to do that, p1 already equals the most left pick-point in the else portion. No need to set it to it's own value again.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: How to avoid direction of picking points?
« Reply #8 on: June 10, 2012, 10:01:20 AM »
Cudos for the most elegant and most "functional" version!

Thanks, but I'm afraid I can't take all the credit for that solution  :-)

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: How to avoid direction of picking points?
« Reply #9 on: June 10, 2012, 10:10:53 AM »
...
You don't strictly need to do that
...
Try this will give an error
Code: [Select]
(and (setq p1 (getpoint "\nPick first point."))
     (setq p2 (getpoint p1 "\nPick second point."))
     (if (< (car p2) (car p1))
       (setq tmp p1 p1 p2 p2 tmp)
       (setq p1 p1 p2 p2)
       )
     (setq *L* (cond ((getint (strcat "\nWhat is HIGH Point <" (itoa (setq *L* (cond (*L*)
    (160)))) ">: " ))) (*L*)))
     (setq *c* (cond ((getint (strcat "\nWhat is LOW Point <" (itoa (setq *c* (cond (*c*) (35)))) ">: " ))) (*c*)))
     (setq step 1000)
     (setq dist (distance p1 p2))
     (setq #pt (fix (/ (/ dist step) 2)))
     (setq ang (angle p1 p2))
     (setq pang (+ ang (/ pi 2.)))
     (setq idx 1)
     (setq mp (list (/ (+ (car p1) (car p2)) 2)
    (/ (+ (cadr p1) (cadr p2)) 2))))

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: How to avoid direction of picking points?
« Reply #10 on: June 10, 2012, 10:12:25 AM »
Any way
Thanks all for the flood of information I got ;)

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: How to avoid direction of picking points?
« Reply #11 on: June 10, 2012, 10:13:24 AM »
Tip: Learn how to use multiple:

Code - Auto/Visual Lisp: [Select]
  1. (if <test expression>
  2.     <then expression>
  3.     <else expression>
  4. )

Over a single:

Code - Auto/Visual Lisp: [Select]
  1.     <expression>
  2.     <expression>
  3.     <expression>
  4.     ....
  5. )

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: How to avoid direction of picking points?
« Reply #12 on: June 10, 2012, 10:23:59 AM »
Tip: Learn how to use multiple:
...[/code]

And in previous reply is a test expression of IF function
Code: [Select]
(if (and (setq p1 (getpoint "\nPick first point."))
     (setq p2 (getpoint p1 "\nPick second point."))
     (if (< (car p2) (car p1))
       (setq tmp p1 p1 p2 p2 tmp)
       (setq p1 p1 p2 p2)
       )
     (setq *L* (cond ((getint (strcat "\nWhat is HIGH Point <" (itoa (setq *L* (cond (*L*)
    (160)))) ">: " ))) (*L*)))
     (setq *c* (cond ((getint (strcat "\nWhat is LOW Point <" (itoa (setq *c* (cond (*c*) (35)))) ">: " ))) (*c*)))
     (setq step 1000)
     (setq dist (distance p1 p2))
     (setq #pt (fix (/ (/ dist step) 2)))
     (setq ang (angle p1 p2))
     (setq pang (+ ang (/ pi 2.)))
     (setq idx 1)
     (setq mp (list (/ (+ (car p1) (car p2)) 2)
    (/ (+ (cadr p1) (cadr p2)) 2))))

      (progn
)
    )
« Last Edit: June 10, 2012, 10:29:01 AM by HasanCAD »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to avoid direction of picking points?
« Reply #13 on: June 10, 2012, 10:31:56 AM »
The point is you don't need to check if you've got the points swapped around correctly in order to continue. There's 2 ways of going about it without needing to reset variables to their already set values.

Option 1:
Code - Auto/Visual Lisp: [Select]
  1. (if (and (setq p1 (getpoint "\nPick first point."))
  2.          (setq p2 (getpoint p1 "\nPick second point."))
  3.          (if (< (car p2) (car p1))
  4.            (setq tmp p1
  5.                  p1  p2
  6.                  p2  tmp)
  7.            t)
  8.          (setq *L* (cond ((getint (strcat "\nWhat is HIGH Point <"
  9.                                           (itoa (setq *L* (cond (*L*)
  10.                                                                 (160))))
  11.                                           ">: ")))
  12.                          (*L*)))
  13.          ...
  14.          )
  15.   (progn ...
  16.          ))
Or a slightly more "correct" Option 2:
Code - Auto/Visual Lisp: [Select]
  1. (if (and (setq p1 (getpoint "\nPick first point."))
  2.          (setq p2 (getpoint p1 "\nPick second point."))
  3.          (progn (if (< (car p2) (car p1))
  4.                   (setq tmp p1
  5.                         p1  p2
  6.                         p2  tmp))
  7.                 (setq *L* (cond ((getint (strcat "\nWhat is HIGH Point <"
  8.                                                  (itoa (setq *L* (cond (*L*)
  9.                                                                        (160))))
  10.                                                  ">: ")))
  11.                                 (*L*)))
  12.          )
  13.          ...
  14.          )
  15.   (progn ...
  16.          ))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to avoid direction of picking points?
« Reply #14 on: June 10, 2012, 12:01:25 PM »
Late to the party.
Code: [Select]
  (if (< (/ pi 2) (angle p1 p2) (* pi 1.5))
    (mapcar 'set '(p1 p2) (list p2 p1)))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.