Tutorials

Tutorial Exercise 5 : Displaying Text and Values with AutoLISP

Welcome to this brief tutorial on the basics of AutoLISP, focusing on the princ function. For anyone who’s ever wondered about how to communicate with AutoCAD users via the command line, or simply display messages, you’re in the right place. By the end of this tutorial, you’ll understand how to effectively use the princ function in a straightforward and concise program.

Objective

To create an AutoLISP program that introduces the princ function for displaying both numerical and string values to the AutoCAD command line.

Description:
In this exercise, you will be tasked with developing an AutoLISP program named testprinc1. This program is designed to help you understand the usage of the princ function, a crucial function for outputting information to the command line.

Steps:

  1. Initialize two variables:
    • A numerical value named num1.
    • A string named str1.
  2. Display the numerical value to the AutoCAD command line.
  3. Display the string on a new line.
  4. Conclude the output by displaying the phrase “The End” on the next line.

Tips:

  • Remember to maintain readability in your outputs. Including newline characters (\n) between outputs can make your program’s outputs clearer.
  • The princ function is versatile, allowing for the display of both numerical and string values.

End Goal:
Once your program is executed, the AutoCAD command line should display the numerical value, followed by the string, and then the phrase “The End”, each on their respective lines.


Detailed Solution to the testprinc1 Exercise

Introduction:
In AutoCAD, having the ability to interactively communicate with the user via the command line is invaluable. The princ function allows us to achieve just that by displaying messages and variable values. In this solution, we will walk through the construction of the testprinc1 program, a simple yet illustrative example of how to harness the capabilities of the princ function in AutoLISP.

The Complete Program:

(defun testprinc1 ( )
(setq num1 5
str1 “Testing this program”
)
(princ num1)
(princ “\n”)(princ str1)
(princ “\nThe End”)
(princ)
)

Line-by-Line Explanation:

  • (defun testprinc1 ( )
    This line begins the definition of the function named testprinc1. The parentheses following the function name indicate that this function doesn’t accept any arguments.
  • (setq num1 5
    str1 “Testing this program”
    )
    These lines set the values of two variables. num1 is assigned the numeric value 5, and str1 is given the string “Testing this program”.
  • (princ num1)
    This line utilizes the princ function to display the value stored in the num1 variable, which is 5.
  • (princ “\n”)(princ str1)
    These commands are responsible for displaying a newline character (to move to the next line) and then displaying the value stored in the str1 variable, which is the string “Testing this program”.
  • (princ “\nThe End”)
    This line prints the string “The End” on a new line.
  • (princ)
    Finally, this line invokes the princ function without any arguments, ensuring the function exits cleanly without returning any trailing characters or values.

Conclusion:
Congratulations on completing this tutorial! You’ve now gained foundational knowledge of using the princ function in AutoLISP, a vital tool for crafting interactive scripts in AutoCAD. As with any skill, regular practice and continued learning will enhance your proficiency. We encourage you to experiment and create new programs building on what you’ve learned here.