Lessons

Dictionaries, Tables & Records

Welcome to another exciting lesson on AutoLISP programming! Today, we’ll dive into Dictionaries, Tables, & Records, critical constructs that allow us to manage structured data within AutoCAD efficiently.

Learning Outcomes

By the end of this lesson, you’ll be able to:

  • Understand the significance of dictionaries, tables, and records in AutoLISP.
  • Create, manipulate, search, and manage dictionaries.
  • Work with table records in AutoCAD.
  • Understand the importance of registered application names and how to manage them.

Dictionaries: Your Key to Structured Data

In AutoLISP, dictionaries are fundamental data structures that allow you to store data in key-value pairs. This means each value in a dictionary is associated with a unique key.

dictadd

Definition: Adds a new entry to a dictionary.

Syntax:

(dictadd dictionary key object)

Examples:

  1. Creating a new dictionary and adding an entry:
(setq myDict (namedobjdict))
(dictadd myDict “FirstKey” “FirstValue”)
  1. Adding another entry to the existing dictionary:
(dictadd myDict “SecondKey” “SecondValue”)

dictnext

Definition: Retrieves the next key from a dictionary.

Syntax:

(dictnext dictionary [key])

Examples:

  1. Iterating through all keys in a dictionary:
(setq key (dictnext myDict))
(while key
(print key)
(setq key (dictnext myDict key))
)
  1. Retrieving the key following a specific key:
(setq subsequentKey (dictnext myDict “FirstKey”))

dictremove

Definition: Removes a key-value pair from a dictionary.

Syntax:

(dictremove dictionary key)

Examples:

  1. Removing an entry from the dictionary:
(dictremove myDict “FirstKey”)
  1. Trying to remove a non-existent key (nothing happens):
(dictremove myDict “NonExistentKey”)

dictrename

Definition: Renames a key within a dictionary.

Syntax:

(dictrename dictionary oldkey newkey)

Examples:

  1. Renaming a key:
(dictrename myDict “SecondKey” “RenamedKey”)
  1. Renaming a non-existent key (results in an error):
(dictrename myDict “FakeKey” “AnotherKey”)

dictsearch

Definition: Searches for a key in a dictionary and retrieves its value.

Syntax:

(dictsearch dictionary key)

Examples:

  1. Searching for a key’s value:
(setq keyValue (dictsearch myDict “RenamedKey”))
  1. Search for a non-existent key:
(setq nonExistent (dictsearch myDict “FakeKey”))

namedobjdict

Definition: Returns the named object dictionary of the current drawing.

Syntax:

(namedobjdict)

Examples:

  1. Accessing the main named object dictionary:
(setq mainDictionary (namedobjdict))
  1. Adding a custom dictionary:
(dictadd (namedobjdict) “CustomDictionary” nil)

Working with Tables

Tables in AutoCAD are important constructs that allow structured data storage, like layers, linetypes, and more.

tblnext

Definition: Retrieves the next symbol table entry.

Syntax:

(tblnext tablename [start])

Examples:

  1. Getting the next layer in the layers table:
(setq nextLayer (tblnext “LAYER”))
  1. Iterating over all layers:
(setq layer (tblnext “LAYER”))
(while layer
(print layer)
(setq layer (tblnext “LAYER” layer))
)

tblobjname

Definition: Retrieves the name of a table given a specified type and name.

Syntax:

(tblobjname type name)

Examples:

  1. Getting the layer table’s name for “Layer1”:
(setq layerName (tblobjname “LAYER” “Layer1”))
  1. Retrieving the linetype table’s name for “Dashed”:
(setq lineTypeName (tblobjname “LTYPE” “Dashed”))

tblsearch

Definition: Searches for an entry in a symbol table.

Syntax:

(tblsearch tablename entryname [flag])

Examples:

  1. Searching for a layer named “Layer1”:
(setq searchLayer (tblsearch “LAYER” “Layer1”))
  1. Looking for a linetype named “Dotted”:
(setq searchLinetype (tblsearch “LTYPE” “Dotted”))

Registered Application Names

regapp

Definition: Registers a new application name in the drawing.

Syntax:

(regapp appname)

Examples:

  1. Registering a new application:
(regapp “NewAppName”)
  1. Registering another application:
(regapp “AnotherAppName”)

In conclusion, Dictionaries, Tables, & Records are versatile tools in AutoLISP that allow structured data management in AutoCAD. Mastering these will provide a solid foundation for more advanced AutoLISP programming. Practice, practice, practice, and soon, you’ll be creating powerful routines with ease!