Community
I have some VB.NET code which checks for a running instance of AutoCAD and uses it. Trouble is several of our users like to open up 4 even 5 instances of AutoCAD when they are working. So, in that case, is there a way to filter through all five instances to find the particular drawing?
If you know the path of the drawing, you can use Marshal.BindToMoniker:
object acadDoc = Marshal.BindToMoniker("Path of the dwg"); // Calling the GetVariable method to read the system variable DWGNAME with late binding var dwgName = (string)acadDoc.GetType().InvokeMember("GetVariable", BindingFlags.InvokeMethod, null, acadDoc, new[] {"DWGNAME"});
The drawing will be opened if it's not loaded. To test if the drawing is opened, you can read the ROT (Running Object Table):
static bool DwgIsOpen(string path) { IRunningObjectTable rot = null; IEnumMoniker monikerEnumerator = null; IBindCtx bindCtx = null; try { Marshal.ThrowExceptionForHR(GetRunningObjectTable(0, out rot)); rot.EnumRunning(out monikerEnumerator); var pNumFetched = new IntPtr(); var monikers = new IMoniker[1]; Marshal.ThrowExceptionForHR(CreateBindCtx(0, out bindCtx)); while (monikerEnumerator.Next(1, monikers, pNumFetched) == 0) { IMoniker moniker = monikers[0]; string displayName; moniker.GetDisplayName(bindCtx, null, out displayName); if (displayName.Equals(path, StringComparison.OrdinalIgnoreCase)) return true; } } finally { if (rot != null) Marshal.ReleaseComObject(rot); if (monikerEnumerator != null) Marshal.ReleaseComObject(monikerEnumerator); if (bindCtx != null) Marshal.ReleaseComObject(bindCtx); } return false; }