You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Database class represents an AutoCAD drawing database. It is the central repository for all graphical and non-graphical objects in a drawing, including entities, symbol tables, and dictionaries.
Namespace
Autodesk.AutoCAD.DatabaseServices
Inheritance Hierarchy
System.Object
└─ RXObject
└─ DBObject
└─ Database
Key Properties
Symbol Table Properties
Property
Type
Description
BlockTableId
ObjectId
Gets the ObjectId of the BlockTable
LayerTableId
ObjectId
Gets the ObjectId of the LayerTable
LinetypeTableId
ObjectId
Gets the ObjectId of the LinetypeTable
TextStyleTableId
ObjectId
Gets the ObjectId of the TextStyleTable
DimStyleTableId
ObjectId
Gets the ObjectId of the DimStyleTable
UcsTableId
ObjectId
Gets the ObjectId of the UcsTable
ViewTableId
ObjectId
Gets the ObjectId of the ViewTable
ViewportTableId
ObjectId
Gets the ObjectId of the ViewportTable
RegAppTableId
ObjectId
Gets the ObjectId of the RegAppTable
Dictionary Properties
Property
Type
Description
NamedObjectsDictionaryId
ObjectId
Gets the Named Objects Dictionary (NOD)
GroupDictionaryId
ObjectId
Gets the Group Dictionary
MLStyleDictionaryId
ObjectId
Gets the Multiline Style Dictionary
LayoutDictionaryId
ObjectId
Gets the Layout Dictionary
PlotSettingsDictionaryId
ObjectId
Gets the Plot Settings Dictionary
ColorDictionaryId
ObjectId
Gets the Color Dictionary
MaterialDictionaryId
ObjectId
Gets the Material Dictionary
VisualStyleDictionaryId
ObjectId
Gets the Visual Style Dictionary
Current State Properties
Property
Type
Description
CurrentSpaceId
ObjectId
Gets the ObjectId of the current space (Model or Paper)
Clayer
ObjectId
Gets/sets the current layer
Celtype
ObjectId
Gets/sets the current linetype
Dimstyle
ObjectId
Gets/sets the current dimension style
Textstyle
ObjectId
Gets/sets the current text style
Cmaterial
ObjectId
Gets/sets the current material
Drawing Properties
Property
Type
Description
TileMode
bool
Gets/sets whether TileMode is active (Model Space)
Filename
string
Gets the full path of the drawing file
OriginalFileName
string
Gets the original filename before any saves
DwgVersion
DwgVersion
Gets the DWG file version
LastSavedAsVersion
DwgVersion
Gets the version last saved as
System Properties
Property
Type
Description
TransactionManager
TransactionManager
Gets the transaction manager for this database
ObjectContextManager
ObjectContextManager
Gets the object context manager
Dimscale
double
Gets/sets the dimension scale
Ltscale
double
Gets/sets the linetype scale
Key Methods
Method
Return Type
Description
ReadDwgFile(string, FileOpenMode, bool, string)
Database
Reads a DWG file into a new Database object
SaveAs(string, DwgVersion)
void
Saves the database to a file
Audit(bool, bool)
void
Audits the database for errors
Purge(ObjectIdCollection)
void
Purges unused objects
GetObjectId(bool, Handle, int)
ObjectId
Gets an ObjectId from a handle
GetAcadDatabase()
IntPtr
Gets a pointer to the underlying AcDbDatabase
Code Examples
Example 1: Accessing Symbol Tables
Documentdoc=Application.DocumentManager.MdiActiveDocument;Databasedb=doc.Database;using(Transactiontr=db.TransactionManager.StartTransaction()){// Access BlockTableBlockTablebt=tr.GetObject(db.BlockTableId,OpenMode.ForRead)asBlockTable;// Access LayerTableLayerTablelt=tr.GetObject(db.LayerTableId,OpenMode.ForRead)asLayerTable;// Access current spaceBlockTableRecordbtr=tr.GetObject(db.CurrentSpaceId,OpenMode.ForRead)asBlockTableRecord;tr.Commit();}
Example 2: Reading an External DWG File
using(DatabaseextDb=newDatabase(false,true)){extDb.ReadDwgFile("C:\\Drawings\\Sample.dwg",FileOpenMode.OpenForReadAndAllShare,true,"");using(Transactiontr=extDb.TransactionManager.StartTransaction()){BlockTablebt=tr.GetObject(extDb.BlockTableId,OpenMode.ForRead)asBlockTable;// Work with external databasetr.Commit();}}
Databasedb=Application.DocumentManager.MdiActiveDocument.Database;using(Transactiontr=db.TransactionManager.StartTransaction()){ObjectIdCollectionidsToPurge=newObjectIdCollection();// Get purgeable objectsdb.Purge(idsToPurge);if(idsToPurge.Count>0){// Purge the objectsforeach(ObjectIdidinidsToPurge){DBObjectobj=tr.GetObject(id,OpenMode.ForWrite);obj.Erase();}}tr.Commit();}
using(Transactiontr=db.TransactionManager.StartTransaction()){// Get the Named Objects Dictionary (NOD)DBDictionarynod=tr.GetObject(db.NamedObjectsDictionaryId,OpenMode.ForRead)asDBDictionary;ed.WriteMessage("\nNamed Objects Dictionary entries:");foreach(DBDictionaryEntryentryinnod){ed.WriteMessage($"\n{entry.Key}");}tr.Commit();}
Example 6: Accessing Group Dictionary
using(Transactiontr=db.TransactionManager.StartTransaction()){DBDictionarygroupDict=tr.GetObject(db.GroupDictionaryId,OpenMode.ForRead)asDBDictionary;ed.WriteMessage($"\nNumber of groups: {groupDict.Count}");foreach(DBDictionaryEntryentryingroupDict){Groupgroup=tr.GetObject(entry.Value,OpenMode.ForRead)asGroup;ed.WriteMessage($"\n Group: {entry.Key} - {group.Count} objects");}tr.Commit();}
Example 7: Accessing Layout Dictionary
using(Transactiontr=db.TransactionManager.StartTransaction()){DBDictionarylayoutDict=tr.GetObject(db.LayoutDictionaryId,OpenMode.ForRead)asDBDictionary;ed.WriteMessage("\nLayouts in drawing:");foreach(DBDictionaryEntryentryinlayoutDict){Layoutlayout=tr.GetObject(entry.Value,OpenMode.ForRead)asLayout;ed.WriteMessage($"\n{layout.LayoutName}");}tr.Commit();}
Example 8: Creating Entry in Named Objects Dictionary
using(Transactiontr=db.TransactionManager.StartTransaction()){DBDictionarynod=tr.GetObject(db.NamedObjectsDictionaryId,OpenMode.ForWrite)asDBDictionary;// Create a custom dictionarystringdictName="MyCustomDictionary";if(!nod.Contains(dictName)){DBDictionarycustomDict=newDBDictionary();nod.SetAt(dictName,customDict);tr.AddNewlyCreatedDBObject(customDict,true);ed.WriteMessage($"\nCreated custom dictionary: {dictName}");}tr.Commit();}
Working with TileMode (Model/Paper Space)
Databasedb=Application.DocumentManager.MdiActiveDocument.Database;if(db.TileMode){// Currently in Model Space}else{// Currently in Paper Space}
Accessing Model Space and Paper Space
using(Transactiontr=db.TransactionManager.StartTransaction()){BlockTablebt=tr.GetObject(db.BlockTableId,OpenMode.ForRead)asBlockTable;// Model SpaceBlockTableRecordmodelSpace=tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForRead)asBlockTableRecord;// Paper SpaceBlockTableRecordpaperSpace=tr.GetObject(bt[BlockTableRecord.PaperSpace],OpenMode.ForRead)asBlockTableRecord;tr.Commit();}