Tutorials

AutoLISP Tutorial 15: the use of the not function

This program demonstrates the use of the not function. Here, we’ll delve into the nuances of user interaction in AutoCAD through a simple but illustrative AutoLISP program. We’ll harness the power of the not function to make our program responsive to different user inputs, ensuring a user-friendly experience even when users might provide unexpected responses. No prior experience with the not function is required, as we’ll break things down step by step.


AutoLISP Tutorial 15: the use of the not function

Objective

Create an AutoLISP program that uses conditional statements to guide user input when placing text in an AutoCAD drawing.

Instructions:

  1. Initial Setup:
    • Disable the command echo in the beginning.
    • Initialize a variable to control a loop.
  2. User Input:
    • Prompt the user to specify the start point of the text.
    • If the user presses ‘Enter’ without selecting a point, exit the loop and finish the program.
  3. Text Entry:
    • If the user provides a valid start point, prompt them to enter the text.
    • If the user presses ‘Enter’ without providing any text, set the start point to null and exit the loop. Do not place any text in this scenario.
    • If the user provides a valid text entry, place the text at the specified point in the drawing.
  4. Program Closure:
    • After exiting the loop, enable the command echo.
    • Finish the program without leaving any message on the command line.

Remember, the key is to understand the flow of user interaction and how the program reacts to different scenarios. Test your program multiple times with various inputs to ensure it works as expected.


AutoLISP Tutorial Solution

In this tutorial solution, we will walk through the creation of an AutoLISP program that uses the not function to guide the user when placing text in an AutoCAD drawing. The program has features such as disabling command echoes for a cleaner user experience and providing mechanisms to handle different user input scenarios.

The Program:

(defun C:tuto15 (/ num1 num2 pnt1 txt1 stp1)
(setvar “cmdecho” 0)
(setq stp1 0)
(while (= stp1 0)
(setq pnt1 (getpoint “\nSpecify start point of text: “))
(If (not pnt1)
(setq stp1 1)
(progn
(setq txt1 (getstring T “\nEnter text: “))
(if (= txt1 “”)
(progn
(setq pnt1 nil)
(setq stp1 1)
)
(command “TEXT” pnt1 “” “” txt1)
)
)
)
)
(setvar “cmdecho” 1)
(princ)
)

 


Line by Line Explanation:

(defun C:tuto15 (/ num1 num2 pnt1 txt1 stp1):

This line begins the definition of the function named tuto15. The local variables num1, num2, pnt1, txt1, and stp1 are declared.

(setvar “cmdecho” 0):

This line disables the command echo, providing a cleaner command line experience for the user.

(setq stp1 0):

Initializes the loop control variable stp1 to 0.

(while (= stp1 0):

This line starts a loop that continues until stp1 is set to 1.

(setq pnt1 (getpoint “\nSpecify start point of text: “)):

Prompts the user to specify a start point for the text. The response is stored in pnt1.

(If (not pnt1):

Checks if the user pressed ‘Enter’ without specifying a point (meaning pnt1 is null).

(setq stp1 1):

If the user did not specify a point, the loop control variable stp1 is set to 1 to exit the loop.

(setq txt1 (getstring T “\nEnter text: “)):

If the user provided a point, this line prompts the user to input text.

(if (= txt1 “”):

Checks if the user pressed ‘Enter’ without providing any text.

(setq pnt1 nil) and (setq stp1 1):

If no text was provided, the start point pnt1 is nullified, and the loop is set to exit.

(command “TEXT” pnt1 “” “” txt1):

If valid text was entered, this line places the text at the specified point in the drawing.

(setvar “cmdecho” 1):

This line enables the command echo back on.

(princ):

This line finishes the function silently without any message on the command line.

I hope this detailed explanation provides a clear understanding of how the AutoLISP program functions. It’s crucial to always consider different user scenarios and how your program should respond to ensure a smooth and intuitive user experience.


Conclusion:

Congratulations on completing this tutorial! You’ve now seen firsthand how to utilize the not function in AutoLISP to enhance user interactions within AutoCAD. By incorporating such functions, you’re better equipped to handle various user responses, ensuring your program remains robust and user-friendly. As you continue to explore AutoLISP, remember that the key to a great program often lies in anticipating and elegantly handling all potential user actions.