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 OutcomesBy the end of this lesson, you’ll be able to:
|
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:
- Creating a new dictionary and adding an entry:
(setq myDict (namedobjdict)) (dictadd myDict “FirstKey” “FirstValue”) |
- 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:
- Iterating through all keys in a dictionary:
(setq key (dictnext myDict)) (while key (print key) (setq key (dictnext myDict key)) ) |
- 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:
- Removing an entry from the dictionary:
(dictremove myDict “FirstKey”) |
- 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:
- Renaming a key:
(dictrename myDict “SecondKey” “RenamedKey”) |
- 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:
- Searching for a key’s value:
(setq keyValue (dictsearch myDict “RenamedKey”)) |
- 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:
- Accessing the main named object dictionary:
(setq mainDictionary (namedobjdict)) |
- 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:
- Getting the next layer in the layers table:
(setq nextLayer (tblnext “LAYER”)) |
- 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:
- Getting the layer table’s name for “Layer1”:
(setq layerName (tblobjname “LAYER” “Layer1”)) |
- 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:
- Searching for a layer named “Layer1”:
(setq searchLayer (tblsearch “LAYER” “Layer1”)) |
- 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:
- Registering a new application:
(regapp “NewAppName”) |
- 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!