Jump to content

Recommended Posts

Posted (edited)

Need help to set "rScale" variable between 0 and 1

How can I limit user to enter real number that greater 0 but less than or equal 1 ?

 

Quote

(setq rScale (getreal (strcat "\nEnter scale ratio: ")))

 

Thanks!

 

Edited by phuynh
Posted

One way.

 

(setq time t)
(while time
  (setq x (getreal "\nEnter value between 0.0 and 1.0:  "))
  (if (and (> x 0.0) (< x 1.0))
    (setq time nil)
  )
)

 

Posted

Thank you for reply,

 

It 's work great!

 

Thanks,

Posted

Here's another... Not tested as it's typed away from CAD.

 

(while

    (null (< 0.0 (setq rScale (getreal "\nSpecify value: ")) 1.0))

    (princ "\nValue is not between 0 and 1")

    )

  • Like 1
Posted

I always like to give the user a way to exit without forcing an error with Esc, e.g.:

(while
    (and
        (setq x (getreal "\nSpecify a number between 0 and 1: "))
        (not (<= 0.0 x 1.0))
    )
    (princ "\nNumber must be between 0 and 1.")
)
(if x
    ;; do stuff
)

 

  • Like 1
Posted

Jonathan,

You code also work great!

 

Thanks!

 

Posted

Lee Mac,

 

I am big fan of you! and I learn lot from you and community.

 

I work on lisp code that set up axonometry (dimetric) view point in 3d space.

 

it is work but it sloppy, can you help?

 

 

Posted

Try this

;;Get a real number x lying the closed interval [a b]
;; (_getreal 0 1 0.5 "\nSpecify a number between 0 and 1 <0.5>: ")
;; (_getreal 0 1 nil "\nSpecify a number between 0 and 1: ")
(defun _getreal (a b default mesg / x)
;;  (if (not default) (initget 1)) ;;Optional
 (setq x (vl-catch-all-apply (function getreal) (list mesg)))
 (cond
  ( (and default (not (numberp x))) default) ;;<Enter>, <Space>, <Esc>
  ( (and default (numberp x) (or (< x a) (> x b))) (_getreal a b default mesg))
  ( (and
     (not default)
     (or (not (numberp x)) (and (numberp x) (or (< x a) (> x b))))
    )
    (_getreal a b nil mesg)
  )
  (T x)
 )
) ;;_getreal

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...