How easily to access the model space using the AutoCAD .net API?

Now you want to access the modelspace ObjectId.

The problem with the way the AutoCAD .net API is structured is that involves a lot of painful plumbing. Just a lot of useless information which hinders the process of doing what you originally set out to do.

Document doc = Application.DocumentManager.CurrentDocument;
Database db = doc.Database;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
    BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
    BlockTableRecord modelSpace = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;           
    tr.Commit(); // Don't forget to commit or abort the transaction!
}

That’s a lot of effort.

You can actually eliminate two lines by using SymbolUtilityServices:

Document doc = Application.DocumentManager.CurrentDocument;
Database db = doc.Database;

using (Transaction tr = db.TransactionManager.StartTransaction())
{   
    ObjectId id = SymbolUtilityServices.GetBlockModelSpaceId(db);    
}

It beautifully saves you a few lines of code.

Perhaps I’ll write a library that can access all the goodies and eliminate the plumbing. Perhaps making use of the dynamic value type to eliminate all the jazz associated with opening and closing transactions etc.

Anyways, hope you learned something.

Written on February 17, 2017