- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I cannot seem to find a solution to getting the blockreference when inside an AttributeModifiedEventHandler. When using StartTransaction() I get a NullReferenceException and when using StartOpenCloseTransaction() I get a 'eWasOpenforWrite' error.
Please see below for further info and the event handler code.
Double clicking a dynamic block and selecting "OK" inside the enhanced attribute editor gives a NullReferenceException at:
using (Transaction transaction1 = transactionManager.StartTransaction())
Copying the dynamic block works however as expected.
If I change: StartTransaction() to:
using (var transaction1 = transactionManager.StartOpenCloseTransaction())
The attribute editor then works, but copying the block gives an 'eWasOpenForWrite' error at:
var blockReference = transaction1.GetObject(id, OpenMode.ForRead) as BlockReference;
Below is the full EventHandler.
private void AttributeModifiedEventHandler(object sender, Autodesk.AutoCAD.DatabaseServices.ObjectEventArgs args)
{
var transactionManager = currentDatabase.TransactionManager;
AttributeReference attributeRef;
string CadHandle = "";
bool updateDB = false;
if (args.DBObject is AttributeReference)
{
attributeRef = (AttributeReference)args.DBObject;
using (Transaction transaction1 = transactionManager.StartTransaction())
{
var blockTable = transaction1.GetObject(currentDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
var blockTableRecord = transaction1.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId id in blockTableRecord)
{
// check if the current ObjectId is a block reference one
if (id.ObjectClass.DxfName == "INSERT")
{
var blockReference = transaction1.GetObject(id, OpenMode.ForRead) as BlockReference;
if (blockReference.Id == attributeRef.OwnerId)
{
CadHandle = blockReference.Handle.ToString();
}
};
}
transaction1.Commit();
}
}
}
Any help anyone can give is extremely appreciated!
Thank you.
Solved! Go to Solution.