• The princ function sends a string after formatting any contol characters. E.g. (princ “\”this is quoted\””) will show as “this is quoted” on the command line.
  • The prin1 function does the same without formatting. E.g. (prin1 “\”this is quoted\””) will show as “\”this is quoted\””. This is usually used when sending to a file so you can later use the read function to get the same value. Also say you have a list of strings, it’ll send the text to the file so a later read would produce the same list of strings – princ will ommit the quotes and the read would fail.
  • The print function does the same as the prin1, but prefixes the string with a newline “\n” (which is formatted). This so that you can send a control code string to a new line in a file in one instruction, otherwise you’d have to do a princ for the new line, then a prin1 for the data.
  • There’s also the prompt function which does exactly the same as princ with 2 caveats:
    • It can’t send to a file (only the command line)
    • It has to have some text to send to the command line. The princ can be used without any string – this is usually wanted when you end a defun with (princ) so nothing is displayed on the command line. Otherwise the last line of the defun will basically issue a prin1 with its value.

AutoLISP programs often need to inform the user of an error or request for input.

Messages that are displayed should try and not interrupt the flow of a command, and when they do the text displayed should be short and precise as to what the problem is or the input that is being requested. AutoLISP offers the following functions to display messages to the user:

  • prompt – Displays string at the AutoCAD Command prompt.
  • princ – Displays a value at the AutoCAD Command prompt or to an open file. Strings are displayed without the enclosing quotation marks.
  • prin1 – Displays a value at the AutoCAD Command prompt or to an open file. Strings are enclosed in quotation marks.
  • print – Displays a value at the AutoCAD Command prompt or to an open file, but a blank line is placed before the value and a space is placed after the value. Strings are enclosed in quotation marks.
  • alert – Displays a dialog box containing an error or warning message.
  • terpri – Prints a newline to the AutoCAD Command prompt.

The write-char, write-line, getXXX, and entsel functions can also display messages at the AutoCAD Command prompt.

When entered from the Visual LISP Console window prompt, the prompt function displays a message (a string) in the AutoCAD Command window and returns nil to the Visual LISP Console window. The princ, prin1, and print functions all display a value (not necessarily a string) in the AutoCAD Command prompt and returns the value to the Visual LISP Console window.

Note: Visual LISP is available on Windows only.

The following examples demonstrate the differences between the basic output functions and how they handle the same string of text.

(setq str "The \"allowable\" tolerance is \261 \274\"")
(prompt str)
outputs The "allowable" tolerance is 1/4"
returns nil

(princ str)
outputs The "allowable" tolerance is 1/4"
returns "The \"allowable\" tolerance is 1/4\""

(prin1 str)
outputs "The \"allowable\" tolerance is 1/4""
returns "The \"allowable\" tolerance is 1/4\""

(print str)
outputs<blank line> "The \"allowable\" tolerance is 1/4""<space>
returns "The \"allowable\" tolerance is 1/4\""

prin1
(prin1 [expr [file-desc]])
Arguments
expr A string or AutoLISP expression. Only the specified expr is
printed; no newline or space is included.
file-desc A file descriptor for a file opened for writing.
Return Values
The value of the evaluated expr. If called with no arguments, prin1 returns a
null symbol.
Returns the UCS 3D point at a specified angle and distance from a point
Prints an expression to the command line or writes an expression to an open file
142 | AutoLISP Reference
Used as the last expression in a function, prin1 without arguments results in
a blank line printing when the function completes, allowing the function to
exit “quietly.”
Examples
Command: (setq a 123 b '(a))
(A)
Command: (prin1 'a)
AA
The previous command printed A and returned A.
Command: (prin1 a)
123123
The previous command printed 123 and returned 123.
Command: (prin1 b)
(A)(A)
The previous command printed (A) and returned (A).
Each preceding example is displayed on the screen because no file-desc was
specified. Assuming that f is a valid file-descriptor for a file opened for writing,
the following function call writes a string to that file and returns the
string:
Command: (prin1 "Hello" f)
"Hello"
If expr is a string containing control characters, prin1 expands these characters
with a leading \, as shown in the following table:
Control codes
Code Description
\\ \ character
\" " character
\e Escape character
\n Newline character
\r Return character
\t TAB character
\nnn Character whose octal code is nnn
princ | 143
The following example shows how to use control characters:
Command: (prin1 (chr 2))
"\002""\002"
SEE ALSO “Displaying Messages” in the Visual LISP Developer’s Guide.
princ
(princ [expr [file-desc]])
This function is the same as prin1, except control characters in expr are
printed without expansion. In general, prin1 is designed to print expressions
in a way that is compatible with load, while princ prints them in a way that
is readable by functions such as read-line.
Arguments
expr A string or AutoLISP expression. Only the specified expr is
printed; no newline or space is included.
file-desc A file descriptor for a file opened for writing.
Return Values
The value of the evaluated expr. If called with no arguments, princ returns a
null symbol.
SEE ALSO “Displaying Messages” in the Visual LISP Developer’s Guide.
print
(print [expr [file-desc]])
This function is the same as prin1, except it prints a newline character before
expr, and prints a space following expr.
Arguments
expr A string or AutoLISP expression. Only the specified expr is
printed; no newline or space is included.
Prints an expression to the command line, or writes an expression to an open file
Prints an expression to the command line, or writes an expression to an open file
144 | AutoLISP Reference
file-desc A file descriptor for a file opened for writing.
Return Values
The value of the evaluated expr. If called with no arguments, print returns a
null symbol.
Advertisements