Lessons

User Interaction: Input

Today, we’re diving into one of the most essential components of programming — User Interaction. Specifically, we’re going to focus on input functions in AutoLISP that allow programs to communicate effectively with users. These functions pave the way for more interactive and dynamic CAD designs, making your scripts more user-friendly and versatile.

Learning Outcomes

Upon completing this lesson, you will be able to:

  • Understand the significance of user input in AutoLISP scripts.
  • Employ various input functions to collect different types of data from the user.
  • Design interactive CAD routines by effectively combining these functions.

Let’s Get Interactive: Introduction to Input Functions

getangle

Definition: This function prompts the user for an angle input, usually in relation to a base point.

Syntax:

(getangle [base_point] [prompt])

Examples:

  1. Basic angle input:
(getangle “\nEnter an angle: “)

Prompts the user with “Enter an angle:”.

  1. Angle input with a base point:
(getangle ‘(0 0) “\nEnter an angle from the origin: “)

Requests an angle in relation to the origin (0,0).

  1. Using the result to draw a line:
(setq ang (getangle “\nEnter line angle: “))
(command “line” “0,0” (polar ‘(0 0) ang 100))

Draws a 100-unit long line at the specified angle from (0,0).


getcorner

Definition: Prompts the user to select a rectangular area’s opposite corner, given the first corner.

Syntax:

(getcorner point [prompt])

Examples:

  1. Basic corner input:
(getcorner ‘(0 0) “\nSelect the opposite corner: “)

Asks the user to select the opposite corner after providing (0,0) as the first corner.

  1. Creating a rectangle using the corners:
(setq pt1 ‘(0 0))
(setq pt2 (getcorner pt1 “\nSelect opposite corner: “))
(command “rectangle” pt1 pt2)

Draws a rectangle using the two specified corners.

  1. Verifying if the selected point lies in the positive X and Y:
(setq pt1 ‘(0 0))
(setq pt2 (getcorner pt1))
(if (and (> (car pt2) 0) (> (cadr pt2) 0))
(alert “Point is in positive X and Y”)
(alert “Point is not in positive X and Y”))

Displays a message based on the quadrant of pt2.


getdist

Definition: This function prompts the user for a distance.

Syntax:

(getdist [base_point] [prompt])

Examples:

  1. Basic distance input:
(getdist “\nEnter a distance: “)

Prompts the user with “Enter a distance:”.

  1. Distance input with a base point:
(getdist ‘(0 0) “\nEnter distance from origin: “)

Asks for a distance in relation to the origin (0,0).

  1. Using the result to offset a line:
(setq dist (getdist “\nEnter offset distance: “))
(command “offset” dist “Select object: ” “”)

Offsets a selected line by the specified distance.


getkword

Definition: This function prompts the user to enter one of the specified keywords.

Syntax:

(getkword “prompt/keyword1/keyword2/…”)

Examples:

  1. Basic keyword input:
(getkword “\nChoose an option/Yes/No/Cancel”)

Prompts the user with three options: Yes, No, and Cancel.

  1. Using the keyword to execute different actions:
(setq kw (getkword “\nDraw/Line/Circle”))
(cond
((= kw “Line”) (command “line”))
((= kw “Circle”) (command “circle”)))

Draws a line or circle based on the user’s choice.

  1. Nested keyword selection:
(setq shape (getkword “\nSelect shape/Rectangle/Circle”))
(cond
((= shape “Rectangle”) (setq type (getkword “\nType/Solid/Hollow”)))
((= shape “Circle”) (setq type (getkword “\nType/Filled/Outline”))))

Asks the user to select a shape and then its type.


getpoint

Definition: This function prompts the user for a point.

Syntax:

(getpoint [base_point] [prompt])

Examples:

  1. Basic point input:
(getpoint “\nSelect a point: “)

Prompts the user to choose a point.

  1. Point input relative to a base point:
(getpoint ‘(0 0) “\nSelect a point relative to the origin: “)

Asks for a point in relation to the origin (0,0).

  1. Drawing a line using two points:
(setq pt1 (getpoint “\nSelect start point: “))
(setq pt2 (getpoint pt1 “\nSelect end point: “))
(command “line” pt1 pt2)

Draws a line between two selected points.


getreal

Definition: This function prompts the user for a real number.

Syntax:

(getreal [prompt])

Examples:

  1. Basic real number input:
(getreal “\nEnter a number: “)

Prompts the user with “Enter a number:”.

  1. Using the number to define a circle’s radius:
(setq rad (getreal “\nEnter circle’s radius: “))
(command “circle” “0,0” rad)

Draws a circle with the specified radius at (0,0).

  1. Calculating area of a square:
(setq side (getreal “\nEnter side length of square: “))
(* side side)

Returns the area of the square based on the side length provided.


getstring

Definition: This function prompts the user for a string.

Syntax:

(getstring [allow_spaces] [prompt])

Examples:

  1. Basic string input:
(getstring T “\nEnter your name: “)

Prompts the user to enter their name and allows spaces.

  1. Using the string in a message:
(setq name (getstring T “\nEnter your name: “))
(alert (strcat “Hello, ” name “!”))

Displays a greeting message with the provided name.

  1. Checking if a specific word is in the input:
(setq sentence (getstring T “\nEnter a sentence: “))
(if (vl-string-search “AutoLISP” sentence)
(alert “You mentioned AutoLISP!”)
(alert “No mention of AutoLISP.”))

Displays a message based on whether “AutoLISP” is in the input.


getint

Definition: Prompts the user for an integer.

Syntax:

(getint [prompt])

Examples:

  1. Basic integer input:
(getint “\nEnter an age: “)

Prompts the user to enter their age.

  1. Using the integer to check for adult/minor:
(setq age (getint “\nEnter your age: “))
(if (< age 18)
(alert “You are a minor.”)
(alert “You are an adult.”))

Displays a message based on the age provided.

  1. Calculating years to reach a milestone:
(setq current_age (getint “\nEnter your current age: “))
(setq milestone 50)
(- milestone current_age)

Returns the number of years left to reach the milestone age of 50.


Conclusion

User interaction is at the heart of any application. By mastering these input functions in AutoLISP, you can create dynamic and interactive CAD designs that cater to specific user needs and preferences. Regular practice and experimentation with these functions will enhance your proficiency and make your scripts more user-centric. Remember, the key to becoming an expert is constant learning and application. Stay curious and keep coding!