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;
}
}
class IdesAcadServiceHost : ServiceHost
{
public Document Document { get; set; }
public IdesAcadServiceHost(Type serviceType, Uri baseAddress, Document doc)
: base(serviceType, baseAddress)
{
Document = doc;
}
}
serviceHost = new IdesAcadServiceHost(typeof(IdesAcadService), baseAddress, AcadApp.DocumentManager.MdiActiveDocument);
"System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: eLockViolation (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
Autodesk.AutoCAD.Runtime.Exception: eLockViolation
at Autodesk.AutoCAD.ApplicationServices.DocumentLock.~DocumentLock()
at Autodesk.AutoCAD.ApplicationServices.DocumentLock.Dispose()
at Ides.Providers.AutoCAD.Common.Services.IdesAcadService.SetSymbolData(String handle, Int32 type, String tag, String value)
at SyncInvokeSetSymbolData(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet))."
I've been trying a few different overloads of LockDocument, both ProtectedAutoWrite and ExclusiveWrite, but so far no luck :/
I'm calling from an external application, so the lock is definately needed. The thing that is so weird is that we only get the exception when AutoCAD is handling mouse movement...
I'm going to try to make a new simple application to see if I can replicate the problem, if so I could upload a sample project...
You cannot just simply assume the external calling to the Acad hosted services can always lock the document whenever it wants (well, unless you are absolutely the external app is the only thing touches the AutoCAD session, once at a time). Even AutoSaving called by AutoCAD would reject your WCF client's locking document request.
So you mean that the lock I make from my external application will be overriden by "internal" locks? This is what it seems like since the errors I get are when I try to dispose my lock.