Tutorials

AutoLISP Tutorial 6 : Displaying and Combining Values

Welcome to tutorial 6 on variable assignments and displaying messages in AutoLISP! One of the core functionalities in programming is the ability to store information in variables and present it in a user-friendly manner. AutoLISP, being an integral part of AutoCAD, offers tools to make this possible directly within your CAD environment. By the end of this tutorial, you’ll know how to assign values to variables, combine them, and present them clearly to the end-user.

Objective

Develop a basic understanding of variable assignment and string concatenation using the princ function in AutoLISP.

Assignment Brief:

  1. Variable Assignment: You’ll be setting up three variables. Two of these variables will store numerical values, and one will store a descriptive string.
  2. Message Display: Using the princ function, you’ll display a series of messages to the command line. Each message will provide information about the assigned variables.
  3. Numerical Operation: In one of your messages, you’re required to display the sum of the two numerical values. You will compute this sum within the princ function itself.
  4. String Concatenation: While constructing your messages, pay attention to combining string literals with variables. Also, be aware of how you display a string with quotation marks inside of it.
  5. Directory Information: Finally, display a message indicating a default directory. Make sure you correctly use the backslash character.

Remember, your objective is to familiarize yourself with assigning values to variables, displaying those values, and performing basic operations with them. This will aid in your understanding of how to create informative and user-friendly feedback in your AutoCAD scripts.


AutoLISP Tutorial Solution: Displaying and Combining Values

In this solution, we will walk through the process of defining variables, using the princ function to display messages on the AutoCAD command line, and conducting basic operations with these variables. The objective is to understand how variable assignments work and how we can utilize them to generate user-friendly messages.


Program:

(defun testprinc2 ( )
(setq num1 25
num2 92.85
str1 “The value for num1 is: ”
)
(princ str1)(princ num1)
(princ “\nThe value for \”num2\” is: “)(princ num2)
(princ “\nThe sum of num1 and num2 is: ” ) (princ (+ num1 num2))
(princ “\nThe default directory is C:\\ACAD\\DRAW”)
(princ)
)

Explanation:

(setq num1 25 num2 92.85 str1 “The value for num1 is: “):

Here, we’re setting up three variables. num1 and num2 store numerical values, while str1 stores a string.

(princ str1)(princ num1):

Using the princ function, we display the string stored in str1 followed by the value stored in num1. This will output “The value for num1 is: 25”.

(princ “\nThe value for \”num2\” is: “)(princ num2):

We’re displaying a string indicating the value of num2. Notice the use of escape characters (\”) to include the quotation marks around “num2”. The value of num2 is then displayed, resulting in “The value for “num2″ is: 92.85”.

(princ “\nThe sum of num1 and num2 is: ” ) (princ (+ num1 num2)):

Here, we combine a string with a calculated value. The string introduces the sum of num1 and num2, and then we use the princ function again to calculate and display the actual sum.

(princ “\nThe default directory is C:\\ACAD\\DRAW”):

A message is displayed indicating a default directory. The double backslashes (\\) are used to escape the backslash character in the directory path.

(princ):

The final princ function with no arguments is commonly used to exit the function cleanly, ensuring no stray values are returned to the AutoCAD command line.

By understanding each line of this program, you’ll be better equipped to use variable assignments and the princ function to generate informative messages in AutoCAD. This will be a foundational skill as you continue to develop more complex AutoLISP programs.


Conclusion

Congratulations on completing the tutorial! You’ve now learned the basics of assigning values to variables and using them to generate meaningful messages in AutoCAD. This is a foundational skill that will serve as a building block for more complex AutoLISP programs. As you continue to explore the vast world of AutoLISP programming, always remember that clear communication with the end-user is as important as the technical intricacies of your code. Keep practicing, and soon, you’ll be creating powerful and user-friendly tools for AutoCAD.