Message 1 of 2
Commit BlockTableRecord with erased objects

Not applicable
01-25-2008
02:17 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello good people, I am in need of your wisdom.
I have a BlockTableRecord that is referenced several times in a drawing. I wish to update the contents of this BlockTableRecord and therefore I Erase all objects in it and append new ones.
But when I commit the transaction I get one System.NullReferenceException for each erased object?
Here is some code recreating the exception:
using AcDb = Autodesk.AutoCAD.DatabaseServices;
AcDb.Database db = AcDb.HostApplicationServices.WorkingDatabase;
AcDb.ObjectId myBlockReferenceId = AcDb.ObjectId.Null;
using (AcDb.Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
AcDb.BlockTable bt = (AcDb.BlockTable)trans.GetObject(db.BlockTableId, AcDb.OpenMode.ForWrite);
AcDb.BlockTableRecord modelspace = (AcDb.BlockTableRecord)trans.GetObject(bt[AcDb.BlockTableRecord.ModelSpace], AcDb.OpenMode.ForWrite);
AcDb.BlockTableRecord myBlock = new AcDb.BlockTableRecord();
myBlock.Name = "MyTestBlock";
bt.Add(myBlock);
trans.AddNewlyCreatedDBObject(myBlock, true);
AcDb.MText myText = new AcDb.MText();
myBlock.AppendEntity(myText);
trans.AddNewlyCreatedDBObject(myText, true);
AcDb.BlockReference myBlockReference = new AcDb.BlockReference(new Autodesk.AutoCAD.Geometry.Point3d(), myBlock.ObjectId);
modelspace.AppendEntity(myBlockReference);
trans.AddNewlyCreatedDBObject(myBlockReference, true);
myBlockReferenceId = myBlockReference.ObjectId;
trans.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception)
{
trans.Abort();
}
}
using (AcDb.Transaction trans = AcDb.HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
try
{
AcDb.BlockReference myBlockReference = trans.GetObject(myBlockReferenceId, AcDb.OpenMode.ForRead) as AcDb.BlockReference;
AcDb.BlockTableRecord myBlock = trans.GetObject(myBlockReference.BlockTableRecord, AcDb.OpenMode.ForWrite) as AcDb.BlockTableRecord;
foreach (AcDb.ObjectId objId in myBlock)
{
AcDb.DBObject myObj = trans.GetObject(objId, AcDb.OpenMode.ForWrite);
myObj.Erase(true);
}
trans.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception)
{
trans.Abort();
}
}
}
Am I doing something the wrong way or should I not bother with this exception?
Could it be related to what is discussed in this thread:
http://discussion.autodesk.com/thread.jspa?messageID=4915935
Thanks
/Jan-Eric
I have a BlockTableRecord that is referenced several times in a drawing. I wish to update the contents of this BlockTableRecord and therefore I Erase all objects in it and append new ones.
But when I commit the transaction I get one System.NullReferenceException for each erased object?
Here is some code recreating the exception:
using AcDb = Autodesk.AutoCAD.DatabaseServices;
AcDb.Database db = AcDb.HostApplicationServices.WorkingDatabase;
AcDb.ObjectId myBlockReferenceId = AcDb.ObjectId.Null;
using (AcDb.Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
AcDb.BlockTable bt = (AcDb.BlockTable)trans.GetObject(db.BlockTableId, AcDb.OpenMode.ForWrite);
AcDb.BlockTableRecord modelspace = (AcDb.BlockTableRecord)trans.GetObject(bt[AcDb.BlockTableRecord.ModelSpace], AcDb.OpenMode.ForWrite);
AcDb.BlockTableRecord myBlock = new AcDb.BlockTableRecord();
myBlock.Name = "MyTestBlock";
bt.Add(myBlock);
trans.AddNewlyCreatedDBObject(myBlock, true);
AcDb.MText myText = new AcDb.MText();
myBlock.AppendEntity(myText);
trans.AddNewlyCreatedDBObject(myText, true);
AcDb.BlockReference myBlockReference = new AcDb.BlockReference(new Autodesk.AutoCAD.Geometry.Point3d(), myBlock.ObjectId);
modelspace.AppendEntity(myBlockReference);
trans.AddNewlyCreatedDBObject(myBlockReference, true);
myBlockReferenceId = myBlockReference.ObjectId;
trans.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception)
{
trans.Abort();
}
}
using (AcDb.Transaction trans = AcDb.HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
try
{
AcDb.BlockReference myBlockReference = trans.GetObject(myBlockReferenceId, AcDb.OpenMode.ForRead) as AcDb.BlockReference;
AcDb.BlockTableRecord myBlock = trans.GetObject(myBlockReference.BlockTableRecord, AcDb.OpenMode.ForWrite) as AcDb.BlockTableRecord;
foreach (AcDb.ObjectId objId in myBlock)
{
AcDb.DBObject myObj = trans.GetObject(objId, AcDb.OpenMode.ForWrite);
myObj.Erase(true);
}
trans.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception)
{
trans.Abort();
}
}
}
Am I doing something the wrong way or should I not bother with this exception?
Could it be related to what is discussed in this thread:
http://discussion.autodesk.com/thread.jspa?messageID=4915935
Thanks
/Jan-Eric