TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: MeasureUp on August 30, 2012, 08:09:59 PM

Title: While with Initget
Post by: MeasureUp on August 30, 2012, 08:09:59 PM
Hi All,
I wrote this to draw "line" or "polyline".
Could you please help me on these questions:
1) The lines in red doesn't work. What is wrong? (i.e. it doesn't pass the call to draw "line" when click enter key without "L" or"P" input.)
2) Is the line in green necessary?
3) Is there anyother thing not making sense or incorrect?

Thanks in advance.

Code: [Select]
(defun c:TEST (/ LineType)
(initget "L P")
(while (setq LineType
(getkword "\n<L>ine or Polyline: ")
   ); end of setq
   [color=red](if (not LineType)
   (setq LineType "L")
   )[/color]; end of if
   (if (= LineType "L")
   (command "._line" pause pause)
   ); end of if
   (while (eq (getvar "cmdactive") 1)
  (command pause)
   ); end of while
   
   (if (= LineType "P")
   (command "._pline" pause pause)
   ); end of if
   (while (eq (getvar "cmdactive") 1)
  (command PAUSE)
   ); end of while
   
   [color=green](initget "L P")[/color]
); end of while
(princ)
)

PS. I can't show the texts in color in the code. Can someone point me out? Thanks.
Title: Re: While with Initget
Post by: CAB on August 30, 2012, 09:54:18 PM
Maybe this:
Code: [Select]
(defun c:test (/ linetype)
  (while (or (initget "L P")
             (setq linetype (getkword "\nLine or Polyline: ")))
    (cond
      ((= linetype "P") (command "._pline" pause pause))
      ((command "._line" pause pause))
    )
    (while (eq (getvar "cmdactive") 1) (command pause))
  )
  (princ)
)
Title: Re: While with Initget
Post by: MeasureUp on August 30, 2012, 10:09:36 PM
Thanks CAB.
But I can't get the meaning of this line:
Code: [Select]
(or (initget "L P")
             (setq linetype (getkword "\nLine or Polyline: ")))
At this point, I can't draw "line" by default by simply hitting the enter key without any letter inputs.
Title: Re: While with Initget
Post by: CAB on August 31, 2012, 12:13:42 AM
Hitting ENTER only is your exit option.
If you want to hit ENTER to draw a line, how do you want to signal the exit from the loop?


Off to bed, see you tomorrow.  ZZzzzz...
Title: Re: While with Initget
Post by: MeasureUp on August 31, 2012, 12:30:16 AM
What I mean is on my original code this should allow me to draw "line" without type in "L":
Code: [Select]
(if (not LineType)
(setq LineType "L")
)
unfortunatly, it doesn't work what I expected.

Thanks.
Title: Re: While with Initget
Post by: BlackBox on August 31, 2012, 12:52:35 AM
Posting from my iPhone....

Code: [Select]
(if
  (and
    (not (initget "Line Polyline"))
    (or (setq option (getkword "\nSelect an option [Line/Polyline]<Line>: "))
      (setq option "Line")
    )
  )
  (prompt (strcat "\nYou entered " option))
  (prompt "\n** No option entered **")
)
Title: Re: While with Initget
Post by: irneb on August 31, 2012, 02:18:48 AM
Actually I prefer not using the and/or when doing something like this. I use progn instead ... seeing as the test should have nothing to do with the return value of initget. Not that it's wrong to do so, it's just more clear when reading the code:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test  (/ LineType)
  2.   (while (and (progn (initget "Line Polyline Exit") ;Initialize the keywords
  3.                      ;;Ask for the keywords + a default
  4.                      (or (setq LineType (getkword "Select an option [Line/Polyline/Exit] <Line>: "))
  5.                          (setq LineType "Line"))) ;If Enter/Space for default - set the value of default
  6.               (not (eq LineType "Exit"))) ;Allow for an exit condition to stop the routine
  7.     (cond ((eq LineType "Line") ;Is keyword Line
  8.            (command "._Line")
  9.            (while (> (getvar "CmdActive") 0) (command pause)))
  10.           ((eq LineType "Polyline") ;Is keyword Polyline
  11.            (command "._PLine")
  12.            (while (> (getvar "CmdActive") 0) (command pause)))))
  13.   (princ))
I've done some further alterations, like using cond instead of multiple ifs, simply added the while pause, and added an Exit keyword else the user has no option but to press Esc to stop. But that's off-topic I think.
Title: Re: While with Initget
Post by: MeasureUp on August 31, 2012, 02:38:11 AM
Actually I prefer not using the and/or when doing something like this. I use progn instead ... seeing as the test should have nothing to do with the return value of initget. Not that it's wrong to do so, it's just more clear when reading the code:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test  (/ LineType)
  2.   (while (and (progn (initget "Line Polyline Exit") ;Initialize the keywords
  3.                      ;;Ask for the keywords + a default
  4.                      (or (setq LineType (getkword "Select an option [Line/Polyline/Exit] <Line>: "))
  5.                          (setq LineType "Line"))) ;If Enter/Space for default - set the value of default
  6.               (not (eq LineType "Exit"))) ;Allow for an exit condition to stop the routine
  7.     (cond ((eq LineType "Line") ;Is keyword Line
  8.            (command "._Line")
  9.            (while (> (getvar "CmdActive") 0) (command pause)))
  10.           ((eq LineType "Polyline") ;Is keyword Polyline
  11.            (command "._PLine")
  12.            (while (> (getvar "CmdActive") 0) (command pause)))))
  13.   (princ))
I've done some further alterations, like using cond instead of multiple ifs, simply added the while pause, and added an Exit keyword else the user has no option but to press Esc to stop. But that's off-topic I think.
Many thanks to irneb.
It works.
But just one more question:
Is there any way to modify your code to press the enter key to exit?
With your one I have to enter "E" to exit.
Title: Re: While with Initget
Post by: irneb on August 31, 2012, 02:52:43 AM
Yes very easily. There are 2 way to go about that:

Option 1:
Change the "default" to be Exit instead of Line
Code - Auto/Visual Lisp: [Select]
  1. ;;...
  2.   (while (progn (initget "Line Polyline Exit") ;Initialize the keywords
  3.                      ;;Ask for the keywords + a default
  4.                      (or (setq LineType (getkword "Select an option [Line/Polyline/Exit] <Exit>: "))
  5.                          (setq LineType "Exit"))) ;If Enter/Space for default - set the value of default
  6.               (not (eq LineType "Exit"))) ;Allow for an exit condition to stop the routine
  7. ;;...

Option 2:
Since the getkword function returns nil if you press Enter/Space, use that feature to stop the while loop directly. This actually makes the condition portion a lot simpler
Code - Auto/Visual Lisp: [Select]
  1. ;;...
  2.   (while (progn (initget "Line Polyline Exit") ;Initialize the keywords
  3.                 ;;Ask for the keywords + a default
  4.                 (setq LineType (getkword "Select an option [Line/Polyline/Exit] <Exit>: ")))
  5. ;;...
Title: Re: While with Initget
Post by: MeasureUp on August 31, 2012, 03:01:14 AM
Thanks again, irneb.
Now I can see the conflict in logical.
I can't set the default for drawing "line" and "Exit" at the same time by pressing the "enter" key.
Am I right?
Title: Re: While with Initget
Post by: irneb on August 31, 2012, 03:39:35 AM
Uhm ... well ... yes  :|

AFAICT acad can't read minds ... yet!  :lmao:
Title: Re: While with Initget
Post by: BlackBox on August 31, 2012, 07:44:29 AM
 :lmao:
Title: Re: While with Initget
Post by: Lee Mac on August 31, 2012, 08:14:33 AM
Here is how I might approach it:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / lt )
  2.     (while
  3.         (progn
  4.             (initget "Line Polyline Exit")
  5.             (/= "Exit" (setq lt (getkword "\nLine or Polyline? [Line/Polyline/Exit] <Line>: ")))
  6.         )
  7.         (if (or (= "Line" lt) (null lt))
  8.             (command "_.line")
  9.             (command "_.pline")
  10.         )
  11.         (while (= 1 (logand 1 (getvar 'cmdactive)))
  12.             (command pause)
  13.         )
  14.     )
  15.     (princ)
  16. )

EDIT: Oops! I didn't read all the posts - it seems the OP now wants the 'Exit' to be default...

So maybe:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / lt )
  2.     (while
  3.         (progn
  4.             (initget "Line Polyline Exit")
  5.             (member (setq lt (getkword "\nChoose [Line/Polyline/Exit] <Exit>: ")) '("Line" "Polyline"))
  6.         )
  7.         (if (or (= "Line" lt) (null lt))
  8.             (command "_.line")
  9.             (command "_.pline")
  10.         )
  11.         (while (= 1 (logand 1 (getvar 'cmdactive)))
  12.             (command pause)
  13.         )
  14.     )
  15.     (princ)
  16. )
Title: Re: While with Initget
Post by: CAB on August 31, 2012, 08:27:39 AM
EDIT: Oops! I didn't read all the posts - it seems the OP now wants the 'Exit' to be default...

No  he wants LINE to be the default until he wants to exit and then he wants EXIT to be the default.  :evil:
Title: Re: While with Initget
Post by: irneb on August 31, 2012, 08:31:02 AM
:lmao:
Yeah!!! I know! Funny as hell!  :pissed:

Anyhow, I hope I didn't confuse you too much RM  :lmao: .
Title: Re: While with Initget
Post by: CAB on August 31, 2012, 08:32:56 AM
Another variation.
Code: [Select]
(defun c:test ( / lt )
       (while
           (cond
             ((initget "Line Pline Exit"))
             ((not (member (setq lt (getkword "\nLine or Pline? [Line/Polyline/Exit] <Exit>: "))'("Line" "Pline" "Exit"))) nil)
             (t
               (command (strcat "_." lt))
               (while (= 1 (logand 1 (getvar 'cmdactive)))
                 (command pause)
               ) t)
           )
       )
       (princ)
    )

edit: should have tested it  :-(
Title: Re: While with Initget
Post by: irneb on August 31, 2012, 08:37:27 AM
Good one there CAB. Though shouldn't it be
Code: [Select]
(= "Exit" ...perhaps? Else it issues an exit command.

Edit ... just say you edited it!  :kewl:


Perhaps
Code: [Select]
(member (...) '("Exit" nil))
Title: Re: While with Initget
Post by: CAB on August 31, 2012, 08:40:53 AM
That one is better, off to get my second cup  8-)
Title: Re: While with Initget
Post by: Lee Mac on August 31, 2012, 08:42:12 AM
EDIT: Oops! I didn't read all the posts - it seems the OP now wants the 'Exit' to be default...

No  he wants LINE to be the default until he wants to exit and then he wants EXIT to be the default.  :evil:

 :-D
Title: Re: While with Initget
Post by: irneb on August 31, 2012, 08:49:02 AM
Actually I'm with him on this: The best interface is no interface (http://www.cooper.com/journal/2012/08/the-best-interface-is-no-interface.html/).

Unless they can get some decent brainwave translator going I don't think it's possible though!
Title: Re: While with Initget
Post by: CAB on August 31, 2012, 08:59:47 AM
The typical scenario I use is ENTER goes to your common choice and ESCape is the exit key.  8-)
Title: Re: While with Initget
Post by: BlackBox on August 31, 2012, 09:42:28 AM
:lmao:
Yeah!!! I know! Funny as hell!  :pissed:

Anyhow, I hope I didn't confuse you too much RM  :lmao: .

Not at all, my friend... Just ribbing you; Happy Friday!  :angel:

Actually I'm with him on this: The best interface is no interface (http://www.cooper.com/journal/2012/08/the-best-interface-is-no-interface.html/).

Unless they can get some decent brainwave translator going I don't think it's possible though!

BTW - Thanks for posting a link to this article; I've only skimmed it, but it looks pretty interesting.

Cheers! :beer:
Title: Re: While with Initget
Post by: irneb on August 31, 2012, 10:36:58 AM
Not at all, my friend... Just ribbing you; Happy Friday!  :angel:
Gathered as much  :laugh: . Cheers! I'm off for the weekend  :mrgreen:
Title: Re: While with Initget
Post by: CAB on August 31, 2012, 11:38:31 AM
Actually I'm with him on this: The best interface is no interface (http://www.cooper.com/journal/2012/08/the-best-interface-is-no-interface.html/).

Unless they can get some decent brainwave translator going I don't think it's possible though!
An interesting read. Thanks  8-)
Title: Re: While with Initget
Post by: MeasureUp on September 04, 2012, 12:33:44 AM
Nice explanation.
Thanks for everyone's help.
Title: Re: While with Initget
Post by: chlh_jd on September 06, 2012, 09:20:29 AM
Is this whether you prefer it  :-D
Code: [Select]
(defun c:test  (/ lt)
  ;; Edited from Alan's code
  (while
    (cond
      ((initget "Line Pline "))
      ((member (setq lt
      (getpoint
"\nLine or Pline? [Line/Polyline] <Select Point For Line / exit>: "))
       '("Line" "Pline"))
       (command (strcat "_." lt))
       (while (= 1 (logand 1 (getvar 'cmdactive)))
(command pause)
)
       t
       )
      ((vl-consp lt);_Select point , run default command "LINE"
       (command "_.Line")
       (command lt)
       (while (= 1 (logand 1 (getvar 'cmdactive)))
(command pause)
)
       t)
      )
    )
  (princ)
  )