0

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 :(

tshepang
  • 12,111
  • 21
  • 91
  • 136
Totalys
  • 445
  • 2
  • 10
  • 25
  • I have to mention though, if you're just running scripts on each drawing, LISP, the commandline switch /b, or even ScriptPro written by Viru on the AutoDesk website (http://labs.autodesk.com/utilities/ADN_plugins/catalog) might be simpler options. – Parrish Husband Oct 08 '13 at 18:52

1 Answers1

2

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.

Parrish Husband
  • 3,148
  • 18
  • 40
  • Hi Locke. Thank you for your Reply. After implementing this complex handler I could finaly whatch the handler being called several times until acad is ready and it runs the next command. It worked when I tried to open AutoCad and also to add some blank tamplate. But when I put: Dim docMgr As AutoCAD.Interop.AcadDocuments = myapp.Documents docMgr.open("filename", false) - It throughs an Unhandled COMException without an description. The code is the same as the first statment... – Totalys Oct 09 '13 at 16:29