Tutorials

AutoLISP Tutorial 13 Prompt: AutoLISP Text Entry with Conditional Checks

Welcome to this tutorial on AutoLISP! Today, we will delve into a program that highlights the power of conditional checks. This specific program will guide you on how to prompt users to specify text and its location within their drawing. By the end of this tutorial, you’ll understand the significance of the if, progn, and while functions and how to utilize them effectively in your AutoLISP scripting journey.

AutoLISP Tutorial 13 Prompt: AutoLISP Text Entry with Conditional Checks

Objective

In this tutorial, you will design a program that prompts users to define a location on their drawing where they’d like to place a text. If the user fails to select a location, the program will remind them to specify a starting location. However, if a location is selected, the program will then ask for the text content they’d like to place at that location.

Instructions:

  1. Start by setting up your program to temporarily disable the command echo in the AutoCAD command line to make the user interface cleaner.
  2. Prompt the user to select a starting location for their text.
  3. Check if the user actually selected a location:
    • If no location is chosen, remind the user that they must specify a starting location.
    • If a location is chosen, prompt them to enter the text they wish to place at that location and then place that text at the specified location in their drawing.
  4. Ensure that after your program runs, the command echo setting is restored to its original state.

Note:
Remember to always test your program after completion to ensure it works as expected. You should see the text placed at the selected location or receive an appropriate message if no location was chosen.


Tutorial Answer: Implementing Conditional Text Entry in AutoLISP

The aim of this tutorial is to walk you through a program written in AutoLISP that leverages conditional checks, specifically using if, progn, and while functions. The program will request the user to specify a location for text placement in their drawing. If no location is selected, the program alerts the user. If a location is specified, it will then prompt for text content to place at that location. Let’s dive into the code.

Complete Program:

(defun C:tuto13 ( / pnt1 txt1)
(setvar “cmdecho” 0)
(setq pnt1 (getpoint “\nSelect the text start location: “))
(if (= pnt1 nil)
(princ “You must specify the text start location: “)
(progn
(setq txt1 (getstring T “\nEnter the text: “))
(command “TEXT” pnt1 “” “” txt1)
)
)
(setvar “cmdecho” 1)
(princ)
)

 


Line-by-line Explanation:

(defun C:tuto13 ( / pnt1 txt1): This line defines a new function named C:tuto13. pnt1 and txt1 are local variables, which means they are only accessible within this function.

(setvar “cmdecho” 0): This line disables command echoing in AutoCAD, making the user interface cleaner.

(setq pnt1 (getpoint “\nSelect the text start location: “)): Prompt the user to select a starting point for the text and store the returned value in pnt1.

(if (= pnt1 nil): Check if pnt1 is nil, meaning the user didn’t select a location.

(princ “You must specify the text start location: “): If no location is chosen, this message will display to the user.

(progn: If a location is chosen (the else part of the if condition), this block of code will execute.

(setq txt1 (getstring T “\nEnter the text: “)): Prompt the user to input the desired text, storing the result in txt1.

(command “TEXT” pnt1 “” “” txt1): This command places the text stored in txt1 at the location stored in pnt1.

(setvar “cmdecho” 1): Re-enable command echoing in AutoCAD, restoring the original setting.

(princ): This line ends the function, ensuring that no extraneous information is returned to the command line.


Conclusion:

Through this tutorial, you have seen how to incorporate conditional checks using the if and progn functions in AutoLISP. This allows for user input validation and interaction, enhancing the user experience. As you progress in AutoLISP, you’ll find that such conditional structures are foundational in creating robust and user-friendly applications. Keep practicing, and soon, implementing such logic will become second nature!