Question

I'm trying to open all .dwg files in a folder with AutoCad and run a previous written script. In order to do that, I built the following code:

 Dim myapp As New Autodesk.AutoCAD.Interop.AcadApplication
 Dim docMgr As AutoCAD.Interop.AcadDocuments = myapp.Documents

 docMgr.Open(File.FullName, False)

Can anyone help me understand why it just doesn't work?

First, I was getting that "RPC_E_CALL_REJECTED" error. But I inserted a handle to read the isQuiescent state and now I just run .Open when AutoCad is idle but stills, Visual Studio is returning me an error without any number.

The COM Details Exceptions is: -2147418113

Does anybody know the correct way to simple open an existing file and run a script in AutoCad? I don't know, I just followed the AutoDest instruction at their webpage and I thought that it would be easy :(

Was it helpful?

Solution

I always implement IMessageFilter when working with AutoCAD Interop objects based on Kean Walmsley's suggestion here: http://adndevblog.typepad.com/autocad/2012/05/cannot-instantiate-autocad-2010-from-an-external-net-application-after-installing-update-1.html

// IMessageFilter Interface
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000016-0000-0000-C000-000000000046")]
public interface IMessageFilter
{
    [PreserveSig]
    int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);

    [PreserveSig]
    int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);

    [PreserveSig]
    int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
}

For a form, it would look something like this:

public partial class Form1 : Form, IMessageFilter
{
    public Form1()
    {
        InitializeComponent();
        CoRegisterMessageFilter(this, null);
    }

    int IMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
    {
        // SERVERCALL_ISHANDLED
        return 0;
    }

    int IMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
    {
        // Retry in a second
        return 1000;
    }

    int IMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
    {
        // PENDINGMSG_WAITNOPROCESS
        return 1;
    }

    [DllImport("ole32.dll")]
    private static extern int CoRegisterMessageFilter(IMessageFilter lpMessageFilter, IMessageFilter lplpMessageFilter); 

    ...


}

It seems to be basically telling those COM exceptions to just shut up and wait for the AutoCAD COM object to be ready.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top