Navigating the realm of AutoCAD programming requires both precision and flexibility. A crucial tool that offers both is the initget function in AutoLISP. This function is essential for guiding and constraining user inputs, ensuring a smoother interactive experience. In this tutorial, we will delve into a practical application of the initget function, helping you grasp its utility in AutoCAD scripting.
AutoLISP Tutorial 17: Mastering the initget Function
ObjectiveThe initget function in AutoLISP provides a mechanism to set constraints for user input. It ensures that specific conditions are met before proceeding with the rest of the code. In this exercise, you will be crafting a program that demonstrates the use of initget in conjunction with selecting entities in AutoCAD. |
Task Description:
You are tasked with creating a program that alters the entity selection process for the erase command. Here’s the behavior you’ll need to capture:
- The program should initially prompt the user to select an entity or press Enter to use a crossing window for selection.
- If the user directly selects an entity, the program should erase it.
- However, if the user presses Enter without selecting an entity, the program should prompt the user to specify two points. These two points will define the corners of a crossing window.
- All entities within the crossing window should be erased.
- Regardless of the path taken, the program should ensure that the original “pickbox” system variable is restored at the end.
Hints:
- You’ll need to handle the size of the pickbox, which affects the entity selection process.
- Remember, initget is your primary tool in this exercise to set constraints for the user’s input.
Deliverable:
By the end of this task, you should have a program that can selectively erase entities based on user input while demonstrating the functionality of the initget function.
Assessment:
Your program will be assessed on its ability to follow the specified behavior, use of the initget function, and preservation of the original system settings.
AutoLISP Tutorial 17: Exploring the initget Function – Solution Breakdown
The initget function plays a crucial role in AutoLISP by setting specific constraints for user input. By incorporating this function into your program, you can influence and guide the user’s interactions, ensuring smoother and more controlled operations. In this tutorial, we will dissect a program that demonstrates the initget function in relation to selecting and erasing entities in AutoCAD.
Complete Program:
(defun C:TUTO17 ( / svpb pnt1 pnt2 ) (setq svpb (getvar “pickbox”)) (setvar “pickbox” 15) (setq pnt1 (getpoint “\nSelect entity or press Enter for crossing window:” )) (if (eq pnt1 nil) (progn (initget 1) (setq pnt1 (getpoint “Enter first point of crossing window”)) (initget 33) (setq pnt2 (getcorner pnt1 “\nEnter second corner of crossing window: “)) (command “ERASE” “c” pnt1 pnt2 “”) ) (command “ERASE” pnt1 “”) ) (setvar “pickbox” svpb) (princ) ) |
Program Breakdown:
(setq svpb (getvar “pickbox”))
This line of code gets the current value of the “pickbox” system variable and stores it in the svpb variable. The pickbox controls the size of the selection box used to pick entities.
(setvar “pickbox” 15)
Here, the size of the pickbox is set to 15 to make entity selection easier for the user.
(setq pnt1 (getpoint “\nSelect entity or press Enter for crossing window:” ))
The user is prompted to either select an entity or press Enter. If an entity is selected, its point is stored in pnt1. If Enter is pressed without selection, pnt1 will be nil.
(if (eq pnt1 nil)
This line checks if pnt1 is nil, meaning the user pressed Enter without selecting an entity.
- (initget 1)
Prepares for the next getpoint function by ensuring no default or pre-selected point is returned. - (setq pnt1 (getpoint “Enter first point of crossing window”))
The user is prompted to select the first corner of the crossing window. - (initget 33)
Ensures that the next point specified by the user is different from pnt1. - (setq pnt2 (getcorner pnt1 “\nEnter second corner of crossing window: “))
The user is prompted to select the opposite corner of the crossing window. - (command “ERASE” “c” pnt1 pnt2 “”)
The entities enclosed within the specified crossing window are erased.
(command “ERASE” pnt1 “”)
If the user initially selected an entity, this line erases that entity.
(setvar “pickbox” svpb)
Restores the original size of the pickbox.
(princ)
This command terminates the function without leaving any stray message in the command line.
Conclusion:
With the power of the initget function at your disposal, you’re better equipped to streamline user interactions and maintain control over the input flow. As you continue your journey in AutoLISP programming, remember the significance of user experience. Harnessing functions like initget can make your programs more user-friendly and error-resistant. Keep experimenting, and soon, the intricate world of AutoLISP will unfold its wonders to you!