Author Topic: Repeat until escape  (Read 1539 times)

0 Members and 1 Guest are viewing this topic.

jimlapier

  • Mosquito
  • Posts: 9
Repeat until escape
« on: April 11, 2017, 10:45:49 PM »
I've searched but didn't see anything overly obvious that helped:
I have a small routine for entering distances as text. The basic idea is:
User picks point A
User picks point B
Routine calculates distance and angle between the two
User picks point C (insertion point for text)
Routine passes the distance to the text command (set at right angle from before) and places the text at point C
User picks point D
Routine calculates distance between A and D
User picks E (insertion point for text)
Routine passes the distance to the text command
and so on, repeating (user picks next point, calculate between first point and latest, use the same angle from first step, then user places the text) until the user escapes out of the routine. There may be 10-30 points, give or take.
Here is what I have (it works for the first part):
Code - Auto/Visual Lisp: [Select]
  1. (defun c:cld (/ pt1 pt2 dis1 dis2 ang1 pt3)
  2.         (setq pt1 (getpoint "\nPick the start point of the centerline: "))
  3.         (setq pt2 (getpoint "\nPick the measurement point on the pool centerline: "))
  4.        
  5.         (setq dis1 (distance pt1 pt2))
  6.         (setq dis2 (rtos dis1 4 0))
  7.         (defun rtod (r) (* 180.0 (/ r pi)))
  8.         (setq ang1 (rtod(angle pt1 pt2)))
  9.         (cond ((< 90 ang1 269)
  10.                 (setq ang2 (+ ang1 180))
  11.                 )
  12.                 ((< 0 ang1 89)
  13.                 (setq ang2 (+ ang1 0))
  14.                 )
  15.                 ((< 270 ang1 360)
  16.                 (setq ang2 (+ ang1 0))
  17.                 )
  18.         )
  19.         (setq pt3 (getpoint "\nInsertion point for text: "))
  20.         (command "_text" pt3 ang2 dis2))
  21.         ;
  22.         ;END FIRST ARROW
  23.         ;

Now I just need to know if there is any easy way of picking a new pt2 and pt3 over and over until the user escapes. Any help / direction is appreciated.
Also, needs to be vanilla lisp only please, if possible. Thanks!

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Repeat until escape
« Reply #1 on: April 12, 2017, 12:52:31 AM »
Have a look at something like this :

Code - Auto/Visual Lisp: [Select]
  1. (defun c:dostuff (/ _rtod *error* pt1 pt2 dis1 dis2 ang1 ang2 pt3)
  2.   (defun _rtod (r) (* 180.0 (/ r pi)))
  3.   (defun *error* (msg / tmp)
  4.     ;;----- Cancel any Active Commands
  5.     (while (< 0 (getvar 'cmdactive)) (command-s nil))
  6.     (setvar 'menuecho 1)
  7.     ;;----- Display error message if applicable
  8.     (cond
  9.       ((not msg))
  10.       ((member
  11.          (strcase msg t)
  12.          '("console break" "function cancelled" "quit / exit abort")
  13.        )
  14.        (princ "\nFunction Cancelled.")
  15.       )
  16.       ((princ (strcat "\nApplication Error: "
  17.                       (itoa (getvar 'errno))
  18.                       " :- "
  19.                       msg
  20.               )
  21.        )
  22.        ;;----- Display backtrace
  23.        (vl-bt)
  24.       )
  25.     )
  26.     (setvar 'errno 0)
  27.   )
  28.   ;;--------------------
  29.   (setq pt1 (getpoint "\nPick the start point of the centerline [SPACE to EXIT]: "))
  30.   (while (and pt1
  31.               (setq pt2
  32.                      (getpoint
  33.                        pt1
  34.                        "\nPick the measurement point on the pool centerline  [SPACE to EXIT]: "
  35.                      )
  36.               )
  37.               (setq pt3 (getpoint "\nInsertion point for text  [SPACE to EXIT]: "))
  38.          )
  39.     (setq dis1 (distance pt1 pt2)
  40.           dis2 (rtos dis1 4 0)
  41.     )
  42.     ;;---
  43.     (if (not ang2)
  44.       (progn (setq ang1 (_rtod (angle pt1 pt2)))
  45.              ;; this should be checked ... what if ang1 is 90.000 or 270 or 269.3 or 89.6 ??
  46.              (cond ((< 90 ang1 269) (setq ang2 (+ ang1 180)))
  47.                    ((< 0 ang1 89) (setq ang2 (+ ang1 0)))
  48.                    ((< 270 ang1 360) (setq ang2 (+ ang1 0)))
  49.              )
  50.       )
  51.     )
  52.     ;;---
  53.     (command-s "_text" pt3 ang2 dis2)
  54.   )
  55.   (*error* nil)
  56.   (princ)
  57. )
  58. (prompt "\n 'DoStuff' to write selected distance to the screen.")
  59.  

BTW:
Well structured question Jim.
« Last Edit: April 12, 2017, 12:59:38 AM by kdub »
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.

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Repeat until escape
« Reply #2 on: April 12, 2017, 05:10:49 AM »
(initget) can be a help here as well

Code - Auto/Visual Lisp: [Select]
  1.  
  2.   (defun c:foo ()
  3.   (initget 1)
  4.   (setq pt1 (getpoint "\nStart Point:   "))
  5.   (while (setq pt2 (getpoint "\nMeasurement Point:   "))
  6.          (if (do_calculations,tests,etc)
  7.              (progn
  8.               (initget 1)
  9.               (setq pt3 (getpoint "\nText Point:   "))
  10.               (do_text)
  11.               )))
  12.   (prin1))


-David
R12 Dos - A2K

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Repeat until escape
« Reply #3 on: April 12, 2017, 10:58:15 AM »
Is there a way that the user does not have to keep picking points? That seems like the next logical step to automate if it's possible.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Repeat until escape
« Reply #4 on: April 12, 2017, 12:14:03 PM »
Enter to exist
Code: [Select]
  (setvar "errno" 0) ; must pre set the errno to 0
  (setq p1 nil)             
  (while
    (cond
      ((null p1)
       (setq p1 (getpoint "\nSelect first point."))
      )
      ((= (getvar "errno") 52) ; exit if user pressed ENTER
       nil ; exit loop
      )

    )
  )
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.