Accessing Symbol Tables with autolisp in AutoCAD

Certain information about each drawing is contained in symbol tables. Specifically, there are tables that contain the properties of all layers, linetypes, named views, text styles, block definitions, UCSs, and viewports within the drawing.

Explanation: the (tblnext) function

(tblnext table_name [T])

The (tblnext) function is used to scan an entire symbol table. For example: (tblnext “layer”).

Valid tables that can be scanned include:

Each time the (tblnext) function is executed, it returns information on the next item in the given table. If the optional argument T is set (any non-nil character will do), the (tblnext) function starts at the beginning of the table again. The properties of each item in the table are returned in the form of an association list.

Explanation: the (tblsearch) function

Another function, (tblsearch), will search a table for a particular entry:

(tblsearch table_name symbol)

(tblsearch) will search the table table_name for the entrysymbol and will return the association list which defines the entry.

Example

Command: (tblnext “layer” T)
((0 . “LAYER”) (2 . “0”) (70 . 0) (62 . 7)(6 . “CONTINUOUS”))

Command: (tblsearch “layer” “object”)
((0 . “LAYER”) (2 . “OBJECT “) (70 . 0) (62 . 0)(6 . “DASHED”))

Additional Example

You may want to use a specific layer in a routine. If the layer does not exist, your program will crash. Therefore, you could do a table search to see if the layer exists, make it current if it does, and if it does not, create it:

See also  Setting Up an AutoLISP Routine

(if (tblsearch “layer” “keynote”)
     (setvar “clayer” “keynote”)
     (progn (command “-layer” … create the layer…)
                   (setvar “clayer” “keynote”)
     ); _end progn
): _end if

  • Information obtained from the symbol access tables using (tblnext) and (tblsearch) is READ-ONLY and cannot be changed directly through AutoLISP.

PRACTICE

Create a Bill of Materials function that gives a count and a list of all the block names in the drawing. Test it on BLOX.DWG. Estimated time for completion: 10 minutes.

Solution

(defun c:BOM ()
(setq BLK-INFO (tblnext “BLOCK” T))
(while BLK-INFO
(setq BLK-NAME (cdr (assoc 2 BLK-INFO)))
(setq SS1 (ssget “X” (list
(cons 0 “INSERT”)
(cons 2 BLK-NAME)
)
)
)
(if SS1
(setq COUNT (sslength SS1))
(setq COUNT 0)
)
(prompt “\n”)
(prompt (itoa COUNT))
(prompt ” “)
(prompt BLK-NAME)
(setq BLK-INFO (tblnext “BLOCK”))
)
(princ)
)

  • A complete solution to this exercise is on your class disk as BOM-A.LSP.

Tip: Layouts and AutoLISP
There is a new function in AutoLISP for AutoCAD that will list the layouts in your drawing. They are not stored in a table.

Command: (layoutlist)
(“Architectural Plan” “Electrical Plan” “Lighting Plan”)

Back to top button