Author Topic: GetX With Default  (Read 9762 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
GetX With Default
« on: October 18, 2010, 01:06:09 PM »
This code is from a little while ago, written as a generic function to get user input with a 'remembered' default.

I would like to share it here and open the floor to suggestions for improvement or perhaps alternatives that you guys use for the same purpose  :-)

Code: [Select]
(defun LM:GetXWithDefault ( _function _prompt _symbol _default / _toString )
  ;; © 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) )
        ( x )
      )
    )
  )
 
  (set _symbol
    (
      (lambda ( input ) (if (or (not input) (eq "" input)) (eval _symbol) input))
      (_function (strcat _prompt "<" (_toString (set _symbol (cond ( (eval _symbol) ) ( _default )))) "> : "))
    )
  )
)

Example:
Code: [Select]
(defun c:test nil
  (LM:GetXWithDefault getreal "\nEnter Number " '*num* 1.0)
)

« Last Edit: October 18, 2010, 01:14:08 PM by Lee Mac »

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: GetX With Default
« Reply #1 on: October 18, 2010, 01:21:50 PM »
Practical application: Not the greatest idea (I thought about it myself). Off the top of my head; You will have a problem with name spaces i would imagine.

Academically, I agree with the principle.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: GetX With Default
« Reply #2 on: October 18, 2010, 01:32:18 PM »
Thanks for your input Se7en - I considered it for use when multiple prompts are issued in a program, each perhaps using different functions/prompts.

When you speak about problems with namespaces, would this take effect if the application were to be compiled with a separate namespace, or were you talking in general?

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: GetX With Default
« Reply #3 on: October 18, 2010, 02:24:52 PM »
Compiled. but i never researched it. You will have to do that yourself.

As always, I am always cautious but this is how i would/did it (this is in my notes on the subject, however i never applied or used it).

Code: [Select]
(defun aif ( var expr iffalse )
    ;; anaphoric-if
    ;;
    ;; ported to AutoLisp from ?comonLisp?
    ;; I dont know the whole extents of the CL`aif' function, im only
    ;; working from one small definition.
    ;;
    ;; EX:
    ;;     (aif 'a (getint "\nEnter new value [1]: ") 1)
    ;;
    ;; OR (if you want to recall `a'...)
    ;; (aif 'a
    ;;      (getint
    ;;        (strcat
    ;;         "\nEnter new value [" (if a (rtos a 2 0) (rtos 1 2 0)) "]: "))
    ;;      (if a a 1) )
    ;;
    (set var (cond (expr) (iffalse))) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: GetX With Default
« Reply #4 on: October 18, 2010, 08:28:43 PM »
Sorry I am am still a bit busy but I wanted to tell you to check the way your procedure evaluates. -i.e. Try to initiate a call and cancel it in the mid of the prompt. ...You may end up with a nasty (hard to track down) bug.

Quote
Command: (LM:GetXWithDefault getreal "\nEnter Number " '*num* 1.0)

Enter Number <1.0000> : *Cancel*
; error: Function cancelled

Command: !*num*
1.0

Command: (aif 'a (getint "\nEnter new value [1]: ") 1)

Enter new value [1]: *Cancel*
; error: Function cancelled

Command: !a
nil
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: GetX With Default
« Reply #5 on: October 18, 2010, 09:45:38 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.
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: GetX With Default
« Reply #6 on: October 18, 2010, 10:37:22 PM »
Ah yes, good times they were. I learned a lot from those conversations.  8-)

http://www.theswamp.org/index.php?topic=6992.0
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.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetX With Default
« Reply #7 on: October 19, 2010, 12:35:37 AM »
A limit would be any additional arguments (basepoint for getdist, T to allow strings with spaces for getstring, etc.).
Granted, this could easily be avoided by appending a list with the message and eval'ing it.

eg.
Code: [Select]
(eval (append (list getstring T) (list (strcat _prompt "blah blah"))))
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: GetX With Default
« Reply #8 on: October 19, 2010, 09:21:18 AM »
@alanjt
I'm not sure i follow what you mean. Are you referring to this structure of function call?
As in: "(LM:GetXWithDefault getreal "\nEnter Number " '*num* 1.0)"

If so, i highly recommend using my method (where the "procedure call" is its own entity in itself).
(aif 'a (getint "\nEnter new value [1]: ") 1)


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 #9 on: October 19, 2010, 09:34:02 AM »
I'm not disagreeing with you.
I was just pointing out a hole in Lee's routine.

eg.
Code: [Select]
(LM:GetXWithDefault getstring "\nSpecify string " '*string* "something")With the above, one could only type wordswithoutspaces. Any option for hidden arguments is removed.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: GetX With Default
« Reply #10 on: October 19, 2010, 09:41:29 AM »
Yes, okay. I understood you then.
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 #11 on: October 19, 2010, 09:47:01 AM »
Yes, okay. I understood you then.
I like your method, very interesting approach.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: GetX With Default
« Reply #12 on: October 19, 2010, 10:21:27 AM »
Yes, okay. I understood you then.
I like your method, very interesting approach.

*lol* you would be the first (Thank you).

If you like that, then you may like this (?related?).

Code: [Select]
(defun let ( bindings body / replace )
  ;; let
  ;; evaluate process with localized variables.
  ;;
  ;; Ported to AutoLisp By: John (7)
  ;;
  ;; Explination of Let:
  ;; [ http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3.2 ]
  ;;
  ;;
  ;; Synopsis: let <bindings> <body>
  ;;
  ;;  (let '((a (+ 1 (* x y)))
  ;;         (b (- 1 y)))
  ;;     '(+ (* x (square a))
  ;;         (* y b)
  ;;         (* a b)))
  ;;  ~~>
  ;;  ((LAMBDA nil (+ (* X (SQUARE (+ 1 (* X Y))))
  ;;  (* Y (- 1 Y))
  ;;  (* (+ 1 (* X Y)) (- 1 Y)))))
  ;;
  ;;  ==> Evaluated value
  ;;
  ;; EX1:
  ;;  (setq x 5)
  ;;  (+ (let '((x 3))
  ;;          '(+ x (* x 10)))
  ;;   x)
  ;; 
  ;;  ==> 38
  ;; 
  ;; EX2:
  ;;  (setq x 2 y 4)
  ;;  (let '((x 3)
  ;;         (y (+ x 2)))
  ;;       '(* x y))
  ;; 
  ;;  ==> 12
  ;;
  ;; EX3:
  ;;
  ;;  ( (lambda ( / let )
  ;;    (defun let (bindings body)
  ;;      (eval
  ;;        (cons (append (list 'lambda (mapcar 'car bindings)) body)
  ;;              (mapcar 'cadr bindings))) )
  ;;
  ;;    (let
  ;;      '((ss-vp
  ;;          (ssget "x"
  ;;                 (list '(0 . "VIEWPORT")
  ;;                       '(-4 . "/=")
  ;;                       '(68 . 1)
  ;;                       (cons 410 (getvar "ctab"))))))
  ;;      '((if (null ss-vp)
  ;;          (progn
  ;;            (alert "\n No VIEWPORTS in current layout")
  ;;            (princ) )
  ;;          ss-vp))
  ;;      )
  ;;    )
  ;; )
  ;;
  ;; ==>
    (eval
        (cons (append (list 'lambda (mapcar 'car bindings)) body)
              (mapcar 'cadr bindings))) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: GetX With Default
« Reply #13 on: October 19, 2010, 10:22:39 AM »
I should mention that these are purely academic and not really practical.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: GetX With Default
« Reply #14 on: October 19, 2010, 12:37:07 PM »
Sorry I am am still a bit busy but I wanted to tell you to check the way your procedure evaluates. -i.e. Try to initiate a call and cancel it in the mid of the prompt. ...You may end up with a nasty (hard to track down) bug.

Quote
Command: (LM:GetXWithDefault getreal "\nEnter Number " '*num* 1.0)

Enter Number <1.0000> : *Cancel*
; error: Function cancelled

Command: !*num*
1.0

Command: (aif 'a (getint "\nEnter new value [1]: ") 1)

Enter new value [1]: *Cancel*
; error: Function cancelled

Command: !a
nil

True, the default symbol is set to the first-time default upon evaluation, but, I don't believe this is an issue as the default symbol will have some value whatever the user presses, (other than Esc). But thanks for pointing it out.

Lee Mac

  • Seagull
  • Posts: 12913
  • 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: 10634
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: 12913
  • 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: 12913
  • 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: 10634
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: 12913
  • 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: 10634
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

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetX With Default
« Reply #30 on: October 20, 2010, 01:58:26 PM »
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)
Interesting. Sucks there are strange little differences.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox