.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

.NET Code to Get a Particular Running Instance of AutoCAD

1 REPLY 1
Reply
Message 1 of 2
btillman
638 Views, 1 Reply

.NET Code to Get a Particular Running Instance of AutoCAD

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?

1 REPLY 1
Message 2 of 2
FRFR1426
in reply to: btillman

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost