SymbolTable is the abstract base class for all symbol tables in the AutoCAD database, such as BlockTable, LayerTable, LinetypeTable, etc. It acts as a specialized dictionary that maps string names (keys) to ObjectIds of SymbolTableRecords.
Autodesk.AutoCAD.DatabaseServices
System.Object
└─ RXObject
└─ DBObject
└─ SymbolTable
├─ BlockTable
├─ LayerTable
├─ LinetypeTable
├─ TextStyleTable
├─ DimStyleTable
├─ RegAppTable
├─ UcsTable
├─ ViewTable
└─ ViewportTable
| Property | Type | Description |
|---|---|---|
this[string] |
ObjectId |
Indexer to get record ID by name. |
Count |
int |
Number of records in the table. |
| Method | Return Type | Description |
|---|---|---|
Has(string) |
bool |
Checks if a record with the given name exists. |
Has(ObjectId) |
bool |
Checks if a record with the given ID exists in this table. |
Add(SymbolTableRecord) |
ObjectId |
Adds a new record to the table. |
GetEnumerator() |
SymbolTableEnumerator |
Returns enumerator for ObjectIds. |
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Open a derived table (e.g., LayerTable) as SymbolTable
SymbolTable table = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as SymbolTable;
if (table.Has("MyLayer"))
{
// Layer exists
}
tr.Commit();
}using (Transaction tr = db.TransactionManager.StartTransaction())
{
// 1. Open Table for Write
SymbolTable table = tr.GetObject(db.LayerTableId, OpenMode.ForWrite) as SymbolTable;
// 2. Create Record (Derived type)
LayerTableRecord newRec = new LayerTableRecord();
newRec.Name = "NewLayer";
// 3. Add to Table
// Add() returns the new ObjectId
ObjectId newId = table.Add(newRec);
// 4. Add to Transaction
tr.AddNewlyCreatedDBObject(newRec, true);
tr.Commit();
}using (Transaction tr = db.TransactionManager.StartTransaction())
{
SymbolTable table = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as SymbolTable;
// Enumerate ObjectIds
foreach (ObjectId id in table)
{
// Open each record
SymbolTableRecord rec = tr.GetObject(id, OpenMode.ForRead) as SymbolTableRecord;
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\nFound: {rec.Name}");
}
tr.Commit();
}ObjectId id = table["MyLayer"];
// Note: Throws KeyNotFoundException if "MyLayer" doesn't exist.
// Use Has() first if unsure.// Symbol table keys are generally case-insensitive in AutoCAD
if (table.Has("mylayer") && table.Has("MYLAYER"))
{
// These refer to the same record
}// SymbolTable does NOT have a Remove() method directly.
// You must open the Record itself and call Erase().
using (Transaction tr = db.TransactionManager.StartTransaction())
{
SymbolTable table = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as SymbolTable;
if (table.Has("LayerToDelete"))
{
ObjectId id = table["LayerToDelete"];
DBObject rec = tr.GetObject(id, OpenMode.ForWrite);
rec.Erase(); // This removes it from the table
}
tr.Commit();
}// Since SymbolTable implements IEnumerable, use Cast<ObjectId>()
var allIds = table.Cast<ObjectId>().ToList();// Any record added to this table will have this table as its OwnerID
if (record.OwnerId == table.ObjectId)
{
// Record belongs to this table
}- Always use Transactions: Use
AddNewlyCreatedDBObjectimmediately aftertable.Add(). - Check
Has(): Checks are cheap; exceptions from invalid indexers are expensive. - Erase vs Remove: Remember there is no
Remove()method on the table. YouErase()the record. - Base Class Usage: Use
SymbolTablereference when writing generic code that handles any table (Layers, Linetypes, etc.) identically.
- SymbolTableRecord - The items contained in the table.
- BlockTable - Specific implementation.
- LayerTable - Specific implementation.