- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have this extension method that adds attributes to a BlockReference. Its's a variation of Giles' extension posted here.
/// <summary>
/// Extension method that adds <see cref="AttributeReference"/>'s to a <see cref="BlockReference"/>.
/// AttributeReference properties are taken from the block being inserted.
/// </summary>
/// <returns>Void.</returns>
public static void AddAttributeReferences(this BlockReference target)
{
if (target == null)
throw new ArgumentNullException("target");
Transaction tr = target.Database.TransactionManager.TopTransaction;
if (tr == null)
throw new AcRx.Exception(ErrorStatus.NoActiveTransactions);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(target.BlockTableRecord, OpenMode.ForRead);
if (btr.HasAttributeDefinitions)
{
foreach (AttributeDefinition attDef in btr.AttributeDefinitions())
{
if (!attDef.Constant)
{
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, target.BlockTransform);
attRef.Position = attDef.Position.TransformBy(target.BlockTransform);
attRef.TextString = attDef.TextString;
target.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
}
It is being called by a sub that inserts a block. If I comment out the line to add the attributes, the block inserts fine. If I try to run AppendAttributeReferences(), it runs without error, but then I get this unhandled error in acad.exe when the calling sub trys to commit the transaction. Here is the stack trace.
System.InvalidOperationException: Operation is not valid due to the current state of the object.
at Autodesk.AutoCAD.DatabaseServices.Transaction.CheckTopTransaction()
at Autodesk.AutoCAD.DatabaseServices.Transaction.DeleteUnmanagedObject()
at Autodesk.AutoCAD.Runtime.DisposableWrapper.!DisposableWrapper()
at Autodesk.AutoCAD.Runtime.DisposableWrapper.Dispose(Boolean )
at Autodesk.AutoCAD.Runtime.DisposableWrapper.Dispose()
at TID_.TID_Commands.TitleBlockInsert()
at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)
at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)
at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)
at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()
Ed
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.
Solved! Go to Solution.