Author Topic: GetX With Default  (Read 9744 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12910
  • London, England
Re: GetX With Default
« Reply #15 on: October 19, 2010, 12:38:55 PM »

It's really interesting to see your evolution Lee.
Reminds be of the phases I went through 15 or 20 years ago ; and similar growth by several others here and elsewhere over the years.

and those phases ARE a good thing, 'cause they provide a solid conceptual base to work from.

You'll probably change your mind about do-all getx functions in favour of purpose specific bomb-proof methods for each type.

case in point : adding initget and keyword options to each oh the get options.

I recall a couple of conversations between CAB and myself that were particularly productive in that regard.

Thanks Kerry - I do like to look back at some of my posts on here and 1) cringe at the code formatting 2) wonder at the magnitude of things I'd do differently now...

I'm seeing though, as this thread progresses that perhaps this can't be described as a 'generic' function, but rather perhaps a time saver for those simple prompts...

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetX With Default
« Reply #16 on: October 19, 2010, 12:43:09 PM »
Quickly scratched this out. It will work with getstring (having or not having optional argument filled), but I couldn't figure out how to get it to eval a list with a point list within. Oh well, it wasn't for a practical purpose and I don't know the solution (not that it matters).

Code: [Select]
(defun foo (var exprlst iffalse)
  (set var
       (cond (((lambda (result) (cond ((not (member result (list nil ""))) result)))
                (eval
                  (mapcar
                    (function
                      (lambda (x)
                        (cond ((eq (type x) 'STR)
                               (strcat x
                                       " <"
                                       (cond ((eq 'STR (type iffalse)) iffalse)
                                             ((eq getangle (car exprlst)) (angtos iffalse))
                                             ((eq 'REAL (type iffalse)) (rtos iffalse))
                                             ((eq 'INT (type iffalse)) (itoa iffalse))
                                       )
                                       ">: "
                               )
                              )
                              ((listp x) x)
                              (x)
                        )
                      )
                    )
                    exprlst
                  )
                )
              )
             )
             (iffalse)
       )
  )
)

works fine:
Code: [Select]
(foo 'str (list getstring T "\nSpecify string") "DOG")
errors on the added point:
Code: [Select]
(foo 'ang (list getangle '(0 0 0) "\nSpecify angle") pi)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: GetX With Default
« Reply #17 on: October 19, 2010, 01:09:34 PM »
I don't think i understand. -i.e. In both of these functions the "SYMBOL" (or "VAR") is set (even if the user escapes out of the prompt). That's *not* a problem?



EDIT: My apologies to alanjt.
« Last Edit: October 19, 2010, 02:14:42 PM by Se7en »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12910
  • London, England
Re: GetX With Default
« Reply #18 on: October 19, 2010, 01:13:08 PM »
I don't think i understand. -i.e. In both of these functions the "SYMBOL" (or "VAR") is set (even if the user escapes out of the prompt). That's *not* a problem?

I can't speak for Alan's, but in my case, the _symbol is intended to be a 'remembered' default (hence global), and so, setting it to the default at the first time of running is not an issue, whatever the user does.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetX With Default
« Reply #19 on: October 19, 2010, 01:22:09 PM »
I don't think i understand. -i.e. In both of these functions the "SYMBOL" (or "VAR") is set (even if the user escapes out of the prompt). That's *not* a problem?
For my post, it will only set a variable if the sub completes. If the users escapes, nothing is set - modeled after your submission, I was just playing around and taking it a step further (adding the <*> prompt).
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12910
  • London, England
Re: GetX With Default
« Reply #20 on: October 19, 2010, 01:52:20 PM »
To account for the extra args and initget etc, I suppose...

Code: [Select]
(defun LM:GetXWithDefault ( _function _prompt _symbol _default _initget _args / _toString ) (vl-load-com)
  ;; © Lee Mac 2010
 
  (setq _toString
    (lambda ( x )
      (cond
        ( (eq getangle _function) (angtos x) )
        ( (eq 'REAL (type x))       (rtos x) )
        ( (eq 'INT  (type x))       (itoa x) )
        ( (vl-princ-to-string x) )
      )
    )
  )

  (if _initget (apply 'initget _initget))
 
  (set _symbol
    (
      (lambda ( input ) (if (or (not input) (eq "" input)) (eval _symbol) input))
      (apply '_function
        (append _args
          (list
            (strcat _prompt "<"
              (_toString
                (set _symbol
                  (cond
                    ( (eval _symbol) ) ( _default )
                  )
                )
              )
              "> : "
            )
          )
        )
      )
    )
  )
)

Code: [Select]
(LM:GetXWithDefault getstring "\nEnter a String " '*str* "Lee Mac" nil '(T))

Code: [Select]
(LM:GetXWithDefault getkword "\nEnter an Option [Alpha/Beta/Gamma] " '*opt* "Alpha" '("Alpha Beta Gamma") nil)
But yeah, this is getting a tad messy...
« Last Edit: October 19, 2010, 02:02:47 PM by Lee Mac »

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: GetX With Default
« Reply #21 on: October 19, 2010, 02:14:00 PM »
I don't think i understand. -i.e. In both of these functions the "SYMBOL" (or "VAR") is set (even if the user escapes out of the prompt). That's *not* a problem?
For my post, it will only set a variable if the sub completes. If the users escapes, nothing is set - modeled after your submission, I was just playing around and taking it a step further (adding the <*> prompt).

Oh sorry. Yes you are right; i was mistaken to include you in that. Sorry 'bout that.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetX With Default
« Reply #22 on: October 19, 2010, 02:23:01 PM »
I don't think i understand. -i.e. In both of these functions the "SYMBOL" (or "VAR") is set (even if the user escapes out of the prompt). That's *not* a problem?
For my post, it will only set a variable if the sub completes. If the users escapes, nothing is set - modeled after your submission, I was just playing around and taking it a step further (adding the <*> prompt).

Oh sorry. Yes you are right; i was mistaken to include you in that. Sorry 'bout that.
No biggie. I am curious if anyone can tell me what would have to be done to my post to account for a point filling the optional argument for getangle/getdist.

But yeah, this is getting a tad messy...
Holy arguments Batman!
 If one was going to take the full route, I would think my solution would be the easiest (barring someone could fix my stated problem).

BTW, you shouldn't need to worry with initget since you don't have a while function, it will apply it to the first input function (the only one to be evaluated in the subroutine).
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: GetX With Default
« Reply #23 on: October 19, 2010, 06:39:22 PM »
I am curious if anyone can tell me what would have to be done to my post to account for a point filling the optional argument for getangle/getdist.

Using your existing code, this works in Bricscad (note: 2 additional quotes):
Code: [Select]
(foo 'ang (list 'getangle ''(0 0 0) "\nSpecify angle") pi)
Alternatively (basically your code but using apply instead of eval):
Code: [Select]
(defun kg:foo (var exprlst iffalse)
  (set var
       (cond (((lambda (result) (cond ((not (member result (list nil ""))) result)))
                (apply
                  (car exprlst)
                  (mapcar
                    (function
                      (lambda (x)
                        (cond ((eq (type x) 'STR)
                               (strcat x
                                       " <"
                                       (cond ((eq 'STR (type iffalse)) iffalse)
                                             ((eq getangle (car exprlst)) (angtos iffalse))
                                             ((eq 'REAL (type iffalse)) (rtos iffalse))
                                             ((eq 'INT (type iffalse)) (itoa iffalse))
                                       )
                                       ">: "
                               )
                              )
                              ((listp x) x)
                              (x)
                        )
                      )
                    )
                    (cdr exprlst)
                  )
                )
              )
             )
             (iffalse)
       )
  )
)
Code: [Select]
(kg:foo 'ang (list getangle '(0 0 0) "\nSpecify angle") pi)
BTW, this line is redundant:
Code: [Select]
                             ((listp x) x)
« Last Edit: October 19, 2010, 06:43:37 PM by roy_043 »

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetX With Default
« Reply #24 on: October 19, 2010, 09:35:29 PM »
I am curious if anyone can tell me what would have to be done to my post to account for a point filling the optional argument for getangle/getdist.


BTW, this line is redundant:
Code: [Select]
                             ((listp x) x)
I know it's redundant. I was having trouble with the list and was trying to modify it, after nothing worked, I just removed the 'changes' and left it as just a return.
I tried apply before, but never succeeded, but I just realized what I was doing wrong. Give this a try...

Code: [Select]
(defun AT:foo (var exprlst iffalse)
  (set var
       (cond (((lambda (result) (cond ((not (member result (list nil ""))) result)))
                (apply (quote (car exprlst))
                       (mapcar
                         (function
                           (lambda (x)
                             (if (eq (type x) 'STR)
                               (strcat x
                                       " <"
                                       (cond ((eq getangle (car exprlst)) (angtos iffalse))
                                             ((eq 'REAL (type iffalse)) (rtos iffalse))
                                             ((eq 'INT (type iffalse)) (itoa iffalse))
                                             (iffalse)
                                       )
                                       ">: "
                               )
                               x
                             )
                           )
                         )
                         (cdr exprlst)
                       )
                )
              )
             )
             (iffalse)
       )
  )
)

eg.
Code: [Select]
(AT:foo 'str (list getstring T "\nSpecify string") "DOG")

(AT:foo 'ang (list getangle '(0 0 0) "\nSpecify angle") pi)

Thanks for the push. :)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: GetX With Default
« Reply #25 on: October 19, 2010, 10:40:30 PM »

Credit where it's due :)

"Maximising AutoCAD"
and "Inside AutoLisp"
and "The AutoCAD Professional's API Toolkit"
were indespensible in the days when there wasn't an internet to search


Quote

;*  AutoLISP Files for Maximizing AutoCAD Volume II - IL DISK Version 11.00

;*   (c) 1987, 1988, 1989, 1991 New Riders Publishing. All Rights Reserved.

;*       Developed by Rustin Gesner, Patrick Haessly and Joseph Smith

;*
< ... snip ....>


From Inside AutoLisp Chapter 5

Code: [Select]

;; These functions are freeware courtesy of the author's of "Inside AutoLisp"
;; for rel. 10 published by New Riders Publications. [ Rusty Gesner (et al) 1989 ]
;; This credit must accompany all copies of these function.

;;* UANGLE User interface angle function
;;* BIT (1 for no null, 0 for none) and
;;* KWD key word ("" for none) are same as for INITGET.
;;* MSG is the prompt string, to which a default real in rads is added as
;;* <DEF> (nil for none), and a : is added.
;;* BPT is base point (nil for none).
;;*
(defun uangle (bit kwd msg def bpt / inp)
  (if def
    (setq msg (strcat "\n"
                      msg
                      " <"
                      (if (eq (type def) 'str)
                        def
                        (angtos def)
                      )
                      ">: "
              )
          bit (* 2 (fix (/ bit 2)))
    )
    (setq msg (strcat "\n" msg ": "))
  )
  (initget bit kwd)
  (setq inp (if bpt
              (getangle msg bpt)
              (getangle msg)
            )
  )
  (if inp
    inp
    def
  )
)


;;* UDIST User interface function
;;* BIT (0 for none) and
;;* KWD key word ("" for none) are same as for INITGET.
;;* MSG is the prompt string, to which a default real is added as
;;* <DEF> (nil for none), and a : is added.
;;* BPT is base point (nil for none).
;;*
(defun udist (bit kwd msg def bpt / inp)
  (if def
    (setq msg (strcat "\n" msg " <" (rtos def) ">: ")
          bit (* 2 (fix (/ bit 2)))
    )
    (setq msg (strcat "\n" msg ": "))
  )
  (initget bit kwd)
  (setq inp (if bpt
              (getdist msg bpt)
              (getdist msg)
            )
  )
  (if inp
    inp
    def
  )
)


;;* UINT User interface function
;;* BIT (0 for none) and
;;* KWD key word ("" for none) are same as for INITGET.
;;* MSG is the prompt string, to which a default real is added as
;;* <DEF> (nil for none), and a : is added.
;;*
(defun uint (bit kwd msg def / inp)
  (if def
    (setq msg (strcat "\n" msg " <" (rtos def 2 0) ">: ")
          bit (* 2 (fix (/ bit 2)))
    )
    (setq msg (strcat "\n" msg ": "))
  )
  (initget bit kwd)
  (setq inp (getint msg))
  (if inp
    inp
    def
  )
)


;;* UKWORD User key word. DEF, if any, must match one of the KWD strings
;;* BIT (1 for no null, 0 for none) and
;;* KWD key word ("" for none) are same as for INITGET.
;;* MSG is the prompt string, to which a default string is added as
;;* <DEF> (nil or "" for none), and a : is added.
;;*
(defun ukword (bit kwd msg def / inp)
  (if (and def (/= def ""))
    (setq msg (strcat "\n" msg " <" def ">: ")
          bit (* 2 (fix (/ bit 2)))
    )
  )
  (initget bit kwd)
  (setq inp (getkword msg))
  (if inp
    inp
    def
  )
)

;;* UPOINT User interface point function
;;* BIT (1 for no null, 0 for none) and
;;* KWD key word ("" for none) are same as for INITGET.
;;* MSG is the prompt string, to which a default point variable is added as
;;* <DEF> (nil for none), and a : is added.
;;* BPT is base point(nil for none).
;;*
(defun upoint (bit kwd msg def bpt / inp)
  (if def
    (setq pts (strcat (rtos (car def))
                      ","
                      (rtos (cadr def))
                      (if (and (caddr def) (= 0 (getvar "FLATLAND")))
                        (strcat "," (rtos (caddr def)))
                        ""
                      )
              )
          msg (strcat "\n" msg " <" pts ">: ")
          bit (* 2 (fix (/ bit 2)))
    )
    (setq msg (strcat "\n" msg ": "))
  )
  (initget bit kwd)
  (setq inp (if bpt
              (getpoint msg bpt)
              (getpoint msg)
            )
  )
  (if inp
    inp
    def
  )
)



;;* UREAL User interface real function
;;* BIT (0 for none) and
;;* KWD key word ("" for none) are same as for INITGET.
;;* MSG is the prompt string, to which a default real is added as
;;* <DEF> (nil for none), and a : is added.
;;*
(defun ureal (bit kwd msg def / inp)
  (if def
    (setq msg (strcat "\n" msg " <" (rtos def 2) ">: ")
          bit (* 2 (fix (/ bit 2)))
    )
    (setq msg (strcat "\n" msg ": "))
  )
  (initget bit kwd)
  (setq inp (getreal msg))
  (if inp
    inp
    def
  )
)


;;* USTR User interface string
;;* If BIT=1 no null "" input allowed, 0 for none, BIT ignored if DEF present.
;;* MSG is the prompt string, to which a default string is added as
;;* <DEF> (nil or "" for none), and a : is added.
;;* If SPFLAG T, spaces are allowed in string.
;;*
(defun ustr (bit msg def spflag / inp nval)
  (if (and def (/= def ""))
    (setq msg (strcat "\n" msg " <" def ">: ")
          inp (getstring msg spflag)
          inp (if (= inp "")
                def
                inp
              )
    )
    (progn (setq msg (strcat "\n" msg ": "))
           (if (= bit 1)
             (while (= "" (setq inp (getstring msg spflag))))
             (setq inp (getstring msg spflag))
           )
    )
  )
  inp
)
« Last Edit: October 19, 2010, 10:44:17 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: GetX With Default
« Reply #26 on: October 20, 2010, 04:25:38 AM »
Strange... (or perhaps not):
If I try AT:foo in Bricscad it doesn't work.
I have to change one line of code.
Old:
Code: [Select]
                (apply (quote (car exprlst))New:
Code: [Select]
                (apply (car exprlst)

Lee Mac

  • Seagull
  • Posts: 12910
  • London, England
Re: GetX With Default
« Reply #27 on: October 20, 2010, 08:40:40 AM »
Quote

;*  AutoLISP Files for Maximizing AutoCAD Volume II - IL DISK Version 11.00

;*   (c) 1987, 1988, 1989, 1991 New Riders Publishing. All Rights Reserved.

;*       Developed by Rustin Gesner, Patrick Haessly and Joseph Smith

;*
< ... snip ....>


Thanks Kerry - I thought this topic must've been covered somewhere already - it seems there is very little that hasn't already been done by someone.. I suppose my function is just an attempt to combine all of those.

David Bethel

  • Swamp Rat
  • Posts: 656
Re: GetX With Default
« Reply #28 on: October 20, 2010, 08:47:42 AM »

Credit where it's due :)

"Maximising AutoCAD"
and "Inside AutoLisp"
and "The AutoCAD Professional's API Toolkit"
were indespensible in the days when there wasn't an internet to search


Quote


Isn't that the truth!  -David

R12 Dos - A2K

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: GetX With Default
« Reply #29 on: October 20, 2010, 09:25:45 AM »
Thanks Kerry - I thought this topic must've been covered somewhere already - it seems there is very little that hasn't already been done by someone.. I suppose my function is just an attempt to combine all of those.

As Kerry already said, a lot of us did the same thing (*my* functions are/were called getpoin7, entse7, etc., etc.). Combining `all into one' is what I would call a library type function.

I don't use library functions, I develop more self contained type. You will too.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org