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 DBObject class is the fundamental base class for all objects stored in an AutoCAD database. It provides core functionality for object persistence, transactions, handles, and object lifecycle management. Every object in the drawing database—whether graphical (entities) or non-graphical (symbol table records, dictionaries, etc.)—derives from DBObject.
using(Transactiontr=db.TransactionManager.StartTransaction()){// Open for readDBObjectobj=tr.GetObject(objectId,OpenMode.ForRead);ed.WriteMessage($"\nRead enabled: {obj.IsReadEnabled}");ed.WriteMessage($"\nWrite enabled: {obj.IsWriteEnabled}");// Upgrade to writeobj.UpgradeOpen();ed.WriteMessage($"\nAfter upgrade - Write enabled: {obj.IsWriteEnabled}");// Make changes...if(objisEntityent){ent.ColorIndex=1;// Change to red}// Downgrade back to readobj.DowngradeOpen();ed.WriteMessage($"\nAfter downgrade - Write enabled: {obj.IsWriteEnabled}");tr.Commit();}
using(Transactiontr=db.TransactionManager.StartTransaction()){Entityent=tr.GetObject(entityId,OpenMode.ForWrite)asEntity;// Save original colorshortoriginalColor=ent.ColorIndex;ed.WriteMessage($"\nOriginal color: {originalColor}");// Make changesent.ColorIndex=1;// Reded.WriteMessage($"\nChanged color to: {ent.ColorIndex}");// Cancel changesent.Cancel();ed.WriteMessage($"\nAfter cancel: {ent.ColorIndex}");// Don't commit - changes are canceledtr.Abort();}
Example 7: Getting Runtime Class Information
using(Transactiontr=db.TransactionManager.StartTransaction()){DBObjectobj=tr.GetObject(objectId,OpenMode.ForRead);RXClassrxClass=obj.GetRXClass();ed.WriteMessage("\n=== Runtime Class Info ===");ed.WriteMessage($"\nClass Name: {rxClass.Name}");ed.WriteMessage($"\nDXF Name: {rxClass.DxfName}");ed.WriteMessage($"\nApp Name: {rxClass.AppName}");// Check class hierarchyRXClassparent=rxClass.MyParent;while(parent!=null){ed.WriteMessage($"\nParent: {parent.Name}");parent=parent.MyParent;}tr.Commit();}
Example 8: Iterating Through All Database Objects
using(Transactiontr=db.TransactionManager.StartTransaction()){// Iterate through all objects in the databaseDictionary<string,int>objectCounts=newDictionary<string,int>();// Get all entities in model spaceBlockTablebt=tr.GetObject(db.BlockTableId,OpenMode.ForRead)asBlockTable;BlockTableRecordmodelSpace=tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForRead)asBlockTableRecord;foreach(ObjectIdobjIdinmodelSpace){DBObjectobj=tr.GetObject(objId,OpenMode.ForRead);stringtypeName=obj.GetType().Name;if(objectCounts.ContainsKey(typeName))objectCounts[typeName]++;elseobjectCounts[typeName]=1;}ed.WriteMessage("\n=== Object Type Counts ===");foreach(varkvpinobjectCounts.OrderByDescending(x =>x.Value)){ed.WriteMessage($"\n{kvp.Key}: {kvp.Value}");}tr.Commit();}
Example 9: Comparing Objects by Handle
using(Transactiontr=db.TransactionManager.StartTransaction()){DBObjectobj1=tr.GetObject(objectId1,OpenMode.ForRead);DBObjectobj2=tr.GetObject(objectId2,OpenMode.ForRead);// Compare by ObjectIdboolsameById=obj1.ObjectId==obj2.ObjectId;// Compare by HandleboolsameByHandle=obj1.Handle==obj2.Handle;ed.WriteMessage($"\nSame by ObjectId: {sameById}");ed.WriteMessage($"\nSame by Handle: {sameByHandle}");ed.WriteMessage($"\nObj1 Handle: {obj1.Handle}");ed.WriteMessage($"\nObj2 Handle: {obj2.Handle}");tr.Commit();}
Example 10: Finding Object Owner
using(Transactiontr=db.TransactionManager.StartTransaction()){DBObjectobj=tr.GetObject(objectId,OpenMode.ForRead);ed.WriteMessage("\n=== Object Ownership Chain ===");ed.WriteMessage($"\nObject: {obj.GetType().Name} ({obj.Handle})");// Walk up the ownership chainObjectIdcurrentOwnerId=obj.OwnerId;intlevel=1;while(!currentOwnerId.IsNull){DBObjectowner=tr.GetObject(currentOwnerId,OpenMode.ForRead);ed.WriteMessage($"\n{newstring(' ',level*2)}Owner {level}: {owner.GetType().Name} ({owner.Handle})");currentOwnerId=owner.OwnerId;level++;// Prevent infinite loopsif(level>10)break;}tr.Commit();}
Object Lifecycle
Creation
Create new object instance
Add to container (BlockTableRecord, SymbolTable, Dictionary, etc.)
Call Transaction.AddNewlyCreatedDBObject()
Commit transaction
Modification
Open object with OpenMode.ForWrite
Modify properties
Commit transaction (or call Cancel() to revert)
Deletion
Open object with OpenMode.ForWrite
Call Erase() for soft delete
Commit transaction
Call Erase(false) to unerase if needed
Open Modes
Mode
Description
Usage
ForRead
Read-only access
Querying properties, no modifications
ForWrite
Read and write access
Modifying properties, erasing
ForNotify
Notification only
Reactor notifications
Best Practices
Always Use Transactions: Never work with DBObjects outside of transactions
Open with Correct Mode: Use ForRead when possible to avoid locking
Upgrade When Needed: Use UpgradeOpen() to change from read to write
Dispose Properly: Let transactions handle disposal, or use using statements
Check IsErased: Verify object hasn't been deleted before accessing
Handle Exceptions: Wrap database operations in try-catch blocks
Use Handles for Persistence: Handles persist across sessions, ObjectIds don't
Extension Dictionaries: Use for custom data storage on objects
Check Owner: Understand object ownership before erasing
Reactor Safety: Be careful with object reactors to avoid circular references
ObjectId vs Handle
Feature
ObjectId
Handle
Persistence
Session only
Permanent
Uniqueness
Per session
Per drawing
Speed
Fast
Slower (requires lookup)
Use Case
Runtime operations
Cross-session references
Common Derived Classes
Graphical Objects
Entity - Base for all graphical objects (lines, circles, text, etc.)
Curve - Base for curve entities
Non-Graphical Objects
SymbolTableRecord - Layer, block, linetype records, etc.