Formatting Strings with Control Codes with autolisp in AutoCAD

Some simple formatting of text can be handled through the use of expanded ASCII codes or control codes. We have already seen one of these, “\n”, which generates a new line. Here are some others that may be useful.

Note that multiple control codes can be used within the same string.

  • The control codes must be within the double quotes.
  • The control codes must be lowercase.

Example

Command: (prompt “This text \nis on \nthree lines with a \ttab.”)
This text

is on
three lines with a                     tab.nil

Translating ASCII Codes

Two other functions will help you work with specialty ASCII characters.

(ascii string)

Changes the string to the appropriate ASCII character as an integer.

(chr integer)

Changes the integer to the appropriate ASCII string.
Command: (ascii “N”)
78
Command: (chr 78)
“N”

PRACTICE

Open the drawing file BLOX.DWG and then edit the LISP file BOM.LSP so that there is a title showing “Number” and “Name”, the prompts are joined together with (strcat), and there are tabs between the information so that the returned information is easier to read.

(defun c:BOM ()
(setq BLK-INFO (tblnext “BLOCK” T))
(prompt “\n NUMBER \t\tNAME”)
(prompt “\n——————–“)
(while BLK-INFO
(setq BLK-NM (cdr (assoc 2 BLK-INFO)))
(setq SS1 (ssget “X”
(list (cons 0 “INSERT”)
(cons 2 BLK-NM)
) ;_ end of list
) ;_ end of ssget
) ;_ end of setq
(if SS1
(setq COUNT (sslength SS1))
(setq COUNT 0) ) ;_ end of if

(prompt (strcat “\n \t” (itoa COUNT) “\t \t” BLK- NM))
(setq BLK-INFO (tblnext “BLOCK”))
) ;_ end of while
(princ)
) ;_ end of defun

See also  Every List is Evaluated in the Same Specific Manner in Autolisp
Back to top button