2

I was trying to load AutoCAD 2015 from .Net process so that I can send commands to the document to create/modify blocks.

I tried both of these approaches but none of them seems to work.

1st approach:

AcadApplication app = new AcadApplication();
app.Visible = true;

2nd approach:

var t = Type.GetTypeFromProgID("AutoCAD.Application", true);
dynamic obj = Activator.CreateInstance(t, true);

In both of the cases I am getting COM exception. Any help?

It's not a duplicate as mentioned in comments, I have tried both approaches mentioned in here.

COM exception -

Retrieving the COM class factory for component with CLSID {0B628DE4-07AD-4284-81CA-5B439F67C5E6} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

Community
  • 1
  • 1
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185

2 Answers2

2

I would recommend attempting to get an existing instance of AutoCAD first before creating a new instance each time your application runs.

Creating an instance each time is very expensive.

try
{
  GetAutoCAD();
}
catch (COMException cx)
{
    try
    {
        StartAutoCad();
    }
    catch(Exception ex)
    {
      Log.Error(ex);
      throw;
    }
}

void GetAutoCAD()
{
    // try to Get an instance
    _application = Marshal.GetActiveObject(_autocadClassId);
}

void StartAutoCad()
{
    var t = Type.GetTypeFromProgID(_autocadClassId, true);
    var obj = Activator.CreateInstance(t, true);
    _application = obj;
}
reckface
  • 5,678
  • 4
  • 36
  • 62
1

Finally I was able to make it run for me. (Posting here so on one had to waste time like I did)

Not sure what was an exact issue though. Strangely, running VS 2013 as normal user worked fine but in case I run it as an administrator, it always fails with above mentioned COM exception.

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • At first run, AutoCAD configures itself. May be it has never been run as Administrator. – Maxence Nov 30 '15 at 09:26
  • @Maxence - I run it as an administrator and then try to launch it from my code but still same issue. Does AutoCAD picks the configuration from first run only? I didn't run it as an admin after installation. – Rohit Vats Nov 30 '15 at 13:47
  • Your second approach is more flexible, I develop using the first approach, and then change the types to dynamic before deployment. That way I can keep up with new versions of AutoCAD using the string value "AutoCAD.Application" as a setting. – reckface Nov 30 '15 at 16:08