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;
}
Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr