LISP that inserts different block based on user input

LISP that inserts different block based on user input

BL_Apex
Enthusiast Enthusiast
857 Views
6 Replies
Message 1 of 7

LISP that inserts different block based on user input

BL_Apex
Enthusiast
Enthusiast

I'm trying to create a tool that will insert the correct sized block based on a number input from the user. Example:

 

User runs the LISP

User is prompted to input a number

  • If user specified number is less than 250, then insert Block A
  • If user specified number is 250 or greater and less than 500, then insert Block B
  • If user specified number is 500 or greater, then insert Block C

Note: The number the user is inputting may be a decimal

 

I know how to do basic If statements but I'm not sure how to handle a list of conditions or work with ranges of numbers.

 

Any help is appreciated!

0 Likes
Accepted solutions (2)
858 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

@BL_Apex wrote:

.... I'm not sure how to handle a list of conditions or work with ranges of numbers.

....


Your terminology is right on.  This is a perfect application of the (cond) function [read about it].  For example:

(setq num (getreal "\nThe number: "))

(command "_.insert"

  (cond ; determine Block name from numerical range

    ((< num 250) "BlockA")

    ((< num 500) "BlockB")

    ("BlockC")

  ); cond

  .... on to insertion point, etc.....

Kent Cooper, AIA
Message 3 of 7

pbejse
Mentor
Mentor
Accepted solution

@BL_Apex wrote:

I'm trying to create a tool that will insert the correct sized block based on a number input from the user. Example:

 

User runs the LISP

User is prompted to input a number

  • If user specified number is less than 250, then insert Block A
  • If user specified number is 250 or greater and less than 500, then insert Block B
  • If user specified number is 500 or greater, then insert Block C

Note: The number the user is inputting may be a decimal

 

(defun c:demo (/ d)
(initget 6)
  (princ
    (Strcat "\nBlock to insert: "
	    (cond
	      ((not (setq d (getreal "\nEnter number:"))))	      
	      ((< d 250) "Block A")
	      ((<= 250 d 500) "Block B")
	      ((> d 500) "Block C")
	    )
    )
  )(princ)
)

 

HTH

Message 4 of 7

BL_Apex
Enthusiast
Enthusiast

@Kent1Cooper @pbejse thank you both for the working solutions! I knew it was simple I just couldn't get it formatted correctly.

 

In the event that I wanted to have a maximum value, let's say 2000, is there a way that the "otherwise" part of the statement could be set up to read "Invalid input. Enter number between 0 and 2000" on the command line and  prompt the user for another input?

0 Likes
Message 5 of 7

Kent1Cooper
Consultant
Consultant

....

In the event that I wanted to have a maximum value, let's say 2000, is there a way that the "otherwise" part of the statement could be set up to read "Invalid input. Enter number between 0 and 2000" on the command line and  prompt the user for another input?


The asking again part is much easier if that check and prompt are in the getting-the-number part, rather than a none-of-the-above condition when already in the Insert command:

 

(while

  (and

    (not (initget 4)); no negative [0 permitted] {wrapped in (not) because (initget) returns nil}

    (setq num (getreal "\nNumber between 0 and 2000: "))

    (> num 2000)

  ); and

  (prompt "\nInvalid input. Enter number between 0 and 2000")

); while

(command "_.insert"

  (cond ; determine Block name from numerical range

    ((< num 250) "BlockA")

    ((< num 500) "BlockB")

    ("BlockC")

  ); cond

  .... on to insertion point, etc.....

Kent Cooper, AIA
Message 6 of 7

BL_Apex
Enthusiast
Enthusiast

Excellent. Makes perfect sense.

0 Likes
Message 7 of 7

pbejse
Mentor
Mentor

@BL_Apex wrote:

 

In the event that I wanted to have a maximum value, let's say 2000, is there a way that the "otherwise" part of the statement could be set up to read "Invalid input. Enter number between 0 and 2000" on the command line and  prompt the user for another input?


(defun BlockNameIsValid ( lmt  / d)
(initget 5)
    (cond
      ((not (setq d (getreal (strcat "\nEnter number between 0 and "
				     (rtos lmt 2 0) ": "))))
       )
      ((< d 250) 	"Block A")
      ((<= 250 d 500) 	"Block B")
      ((<  500 d lmt) 	"Block C")
      (t (alert "Invalid input") (BlockNameIsValid lmt))
    )
  )

Usage

(BlockNameIsValid 2000)

Enter number between 0 and 2000

 

(BlockNameIsValid 5000)

Enter number between 0 and 5000

 

HTH

 

0 Likes