.NET Code to Get a Particular Running Instance of AutoCAD

.NET Code to Get a Particular Running Instance of AutoCAD

btillman
Advocate Advocate
836 Views
1 Reply
Message 1 of 2

.NET Code to Get a Particular Running Instance of AutoCAD

btillman
Advocate
Advocate

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?

0 Likes
837 Views
1 Reply
Reply (1)
Message 2 of 2

FRFR1426
Collaborator
Collaborator

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
0 Likes