Navigating through the vast capabilities of AutoLISP, we often stumble upon functions that significantly simplify our tasks. One such function is the getkword function, which offers users predefined choices to select from, ensuring a more controlled input and outcome. This tutorial is used to demonstrate the getkword function.
Tutorial 18: Exploring the getkword Function in AutoLISP
ObjectiveIn this exercise, we will develop an AutoLISP program to illustrate the usage of the getkword function. The getkword function is a powerful tool that prompts users for a keyword input, allowing a controlled set of responses. By the end of this exercise, you should be able to harness the function to provide users with a clear set of options and capture their choice. |
Requirements:
- Begin by setting the command echo off to ensure a clean command line experience.
- Use the initget function to specify a set of keyword options. For this task, the keyword options will be:
- FPlan
- FDimension
- FLayout
- FRooms
- Prompt the user to select one of the specified layer names using getkword. The possible choices are:
- FP (which corresponds to FPlan)
- FD (corresponding to FDimension)
- FL (corresponding to FLayout)
- FR (corresponding to FRooms)
- After capturing the user’s input, use a command to match the chosen layer name.
- Conclude by turning the command echo back on and exit the function gracefully.
Considerations:
While crafting your program, pay attention to the user’s experience. Ensure that the keyword choices are clear and that the program can handle the selected keyword correctly.
AutoLISP Tutorial 18: Understanding the getkword Function
In this detailed breakdown, we will explore a simple AutoLISP program that leverages the power of the getkword function. This function is crucial when you want to present the user with a list of predefined choices and have them select one. The user’s choice can then be used to drive subsequent logic in your program. Let’s dive in!
The Program:
(defun C:tuto18 ( / layn) (setvar “cmdecho” 0) (initget 1 “FPlan FDimension FLayout FRooms”) (setq layn (getkword “\nEnter layer name [FP,FD,FL,FR]: “)) (command “-LAYER” “m” layn “”) (setvar “cmdecho” 1) (princ) ) |
Explanation:
(defun C:tuto18 ( / layn)
This line defines a new function named tuto18. The / layn within the parentheses denotes a local variable for this function.
(setvar “cmdecho” 0)
This command turns off the command line echoing in AutoCAD. This is done to ensure that the user gets a clean command prompt experience.
(initget 1 “FPlan FDimension FLayout FRooms”)
initget initializes allowable input for the subsequent getkword function. The number 1 ensures the user can’t enter any value other than the options provided.
(setq layn (getkword “\nEnter layer name [FP,FD,FL,FR]: “))
This line prompts the user to enter a layer name with specific keyword options (FP, FD, FL, FR). The chosen keyword is then stored in the variable layn.
(command “-LAYER” “m” layn “”)
This command uses the layer name chosen by the user (stored in layn) and matches it in the current drawing. It essentially makes the chosen layer the current layer in the drawing.
(setvar “cmdecho” 1)
This command turns the command echoing back on.
(princ)
This function is used to gracefully exit the program without leaving any trailing characters in the command line.
Conclusion:
Through this tutorial, we’ve seen how the getkword function can be effectively used to prompt the user for specific keyword inputs, and how those choices can be incorporated into the logic of an AutoLISP program. The ability to provide users with set choices makes for a more controlled and streamlined experience, ensuring that the program behaves as expected. Always consider the clarity of your prompts and the flow of your program to ensure a smooth user experience.