Author Topic: While with Initget  (Read 6838 times)

0 Members and 1 Guest are viewing this topic.

MeasureUp

  • Bull Frog
  • Posts: 462
While with Initget
« 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.
« Last Edit: August 30, 2012, 09:32:23 PM by MeasureUp »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: While with Initget
« Reply #1 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)
)
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.

MeasureUp

  • Bull Frog
  • Posts: 462
Re: While with Initget
« Reply #2 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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: While with Initget
« Reply #3 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...
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.

MeasureUp

  • Bull Frog
  • Posts: 462
Re: While with Initget
« Reply #4 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.

BlackBox

  • King Gator
  • Posts: 3770
Re: While with Initget
« Reply #5 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 **")
)
"How we think determines what we do, and what we do determines what we get."

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: While with Initget
« Reply #6 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.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

MeasureUp

  • Bull Frog
  • Posts: 462
Re: While with Initget
« Reply #7 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.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: While with Initget
« Reply #8 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. ;;...
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

MeasureUp

  • Bull Frog
  • Posts: 462
Re: While with Initget
« Reply #9 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?

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: While with Initget
« Reply #10 on: August 31, 2012, 03:39:35 AM »
Uhm ... well ... yes  :|

AFAICT acad can't read minds ... yet!  :lmao:
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

BlackBox

  • King Gator
  • Posts: 3770
Re: While with Initget
« Reply #11 on: August 31, 2012, 07:44:29 AM »
 :lmao:
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: While with Initget
« Reply #12 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. )
« Last Edit: August 31, 2012, 08:19:16 AM by Lee Mac »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: While with Initget
« Reply #13 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:
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.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: While with Initget
« Reply #14 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: .
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.