Hello all, new user here, and I have what I hope is a simple (stupid?) question.
I'm working on a tool to convert dwg's from one applications format to another, mainly by reading Attributes/Xdata and writing those data back to the symbol in another Attribute/XData based on custom rules.
We use a WCF Service to communicate with AutoCAD, which seems to work pretty good most times, but now to my problem.
I write for example Attribute data to the symbol calling the following function:
public void SetSymbolData(string handle, string tag, string value)
{
if (host.Document != null && host.Document.Name != null)
{
using (DocumentLock docLock = host.Document.LockDocument())
{
long ln = Convert.ToInt64(handle, 16);
Handle hn = new Handle(ln);
ObjectId id = new ObjectId();
if (host.Document.Database.TryGetObjectId(hn, out id))
{
using (Transaction tr = host.Document.Database.TransactionManager.StartTransaction())
{
BlockReference blk = tr.GetObject(id, OpenMode.ForWrite) as BlockReference;
if (blk != null)
{
foreach (ObjectId arId in blk.AttributeCollection)
{
DBObject obj = tr.GetObject(arId, OpenMode.ForRead);
AttributeReference ar = obj as AttributeReference;
if (ar != null)
{
if (ar.Tag == tag)
{
ar.UpgradeOpen();
ar.TextString = value;
break;
}
}
}
AttributeReference attRef = new AttributeReference();
attRef.Tag = tag;
attRef.TextString = value;
blk.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
tr.Commit();
}
}
}
}
}
}
private IdesAcadServiceHost host
{
get
{
return OperationContext.Current.Host as IdesAcadServiceHost;
}
}
Sometimes this works beautifully, not a problem at all... But then sometimes it crashes giving me an eLockViolation when the Document Lock disposes. It seems to me that I get this error whenever I move the mouse over the drawing...
It might be that I'm way off in how I should do this, but I'm hoping that you might have som pointers for me.