Error System.InvalidProgramException while using AutoCAD 2013 .Net API

Error System.InvalidProgramException while using AutoCAD 2013 .Net API

Anonymous
Not applicable
2,135 Views
11 Replies
Message 1 of 12

Error System.InvalidProgramException while using AutoCAD 2013 .Net API

Anonymous
Not applicable

pic1.jpgpic2.jpg

Intending to develop an application in Visual C# .NET 2012 that will generate a .dwg file dynamically from the program.
In the system, we have AutoCAD 2013 already installed.

In .NET environment we've already installed AutoCAD 2013 .Net API from NuGet packages.

 

0 Likes
Accepted solutions (1)
2,136 Views
11 Replies
Replies (11)
Message 2 of 12

norman.yuan
Mentor
Mentor

While you did not mention what type of application you are doing, from the picture showed, I can see there is a Program.cs. From this, I guess you are developing a stand-alone EXE (console, or Win Form/WSP exe). If so, the exception of "InvalidProgram" is expected: you CANNOT use AutoCAD .NET API assemblies in EXE app (i.e. outside AutoCAD process).

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 12

Anonymous
Not applicable

Thanks for your prompt response. Yes, you are right. I'm trying to create a AutoCAD .dwg file from .NET console application. Please suggest how can I create .dwg file programmatically from .NET environment using C#.

0 Likes
Message 4 of 12

norman.yuan
Mentor
Mentor

Depending on the business workflow of your requirement, there could be different way to create a drawing. In general, you need to run AutoCAD to create a drawing (unless you use RealDwg, or use Autodesk's cloud services).

 

If you choose to automate AutoCAD froom external EXE, you would use AutoCAD COM API to automate an AutoCAD session to create/open/manipulate a drawing file. Since this is a external app that has to communicate with a running AutoCAD, the process is slow, and AutoCAD has to be running anyway. So, it might as well be better simply just write AutoCAD add-in code to do the drawing creating/manipulating work, this way you can use AutoCAD .NET API (the main topic of this forum). Or, you can write AutoCAD .NET API code as AutoCAD add-in and using an EXE to drive AutoCAD core console to run the add-in code...How to do it really depends on your specific business workflow.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 12

Anonymous
Not applicable

Sorry for delayed reply.


<Quote>
So, it might as well be better simply just write AutoCAD add-in code to do the drawing creating/manipulating work, this way you can use AutoCAD .NET API (the main topic of this forum). Or, you can write AutoCAD .NET API code as AutoCAD add-in and using an EXE to drive AutoCAD core console to run the add-in code
<Unquote>

 

From the above, what I understood is
1. For the first solution, we need to run AutoCAD in background, and write AutoCAD add-in code.
2. For the second solution, we need to write AutoCAD .NET API code. For this AutoCAD need not to be run in background.

 

If so, please let us know the details of the second solution. Do we require any SDK for the same? Will RealDWG be the solution for the second one?

0 Likes
Message 6 of 12

_gile
Consultant
Consultant

Hi,

 

You should have a look at the AutoCAD Managed .NET Developer's Guide.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 12

Anonymous
Not applicable

Thanks for the reference. What I understood it is required to have Object ARX (mainly Real DWG) to develop .NET application which will create / edit .dwg file. please confirm my understanding.

0 Likes
Message 8 of 12

kerry_w_brown
Advisor
Advisor
post redacted.

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 9 of 12

_gile
Consultant
Consultant

biswassum a écrit :

Thanks for the reference. What I understood it is required to have Object ARX (mainly Real DWG) to develop .NET application which will create / edit .dwg file. please confirm my understanding.


Assuming you already have AutoCAD installed, you do not need Real DWG.

 

As Norman said, you have to choose if you want to build an out-of-process EXE application or an in-process add-in DLL (typically an AutoCAD custom command).

 

For an out-of-process EXE, you have to use the COM API (ActiveX).

 

For an in-process DLL, as explained in this topic, you should install the ObjectARX SDK so that you can reference the AutoCAD .NET API DLL files that come with the SDK instead of those that are found in the install directory of AutoCAD. You do not need need to install the Managed .NET project wizard, you'd rather start a new project from scratch.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 12

Anonymous
Not applicable

Thanks for the help. We have few concern describing below.

1) we have autocad 2013
2) Visual Studio 2015
3) Extracted objectARX for autocad 2018
4) All the above applied for 32 bit

now we've created .net console application to open an autocad application instance with a single line in the modelspace.
we took all dll referencs from objectARX.

now the problem is that ACadApplication instance is not created during runtime.

we are attaching our code for your reference

 

Code

 

AcadApplication AcadApp = default(AcadApplication);
            //WriteToCsv(data, @"D:\ExcelInputOutput\ExcelInputOutput\Output\ModifiedData.csv", ",");

            try
            {
                AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application");
            }
            catch
            {
                try
                {
                    AcadApp = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID("AutoCAD.Application"), true);
                }
                catch {
                    Console.WriteLine("AutoCAD Not Loaded....");
                }
            }

 

Here AcadApp is returning Null during runtime. Do we have any version related issue?

 

Please help.

 

0 Likes
Message 11 of 12

_gile
Consultant
Consultant
Accepted solution

Hi,


biswassum a écrit :

Thanks for the help. We have few concern describing below.

1) we have autocad 2013
2) Visual Studio 2015
3) Extracted objectARX for autocad 2018
4) All the above applied for 32 bit

now we've created .net console application to open an autocad application instance with a single line in the modelspace.
we took all dll referencs from objectARX.

now the problem is that ACadApplication instance is not created during runtime.

 


 

It looks like you did not attentively read the linked topics I provided (specially this one).

 

I'd try a last time.

 

Assuming you try to build a standalone out-of-process executable targetting AutoCAD 2013 32 bits,

you have to use COM API (you cannot use the .NET API out-of-process) so you do not need to reference the managed .NET libraries, but you must reference the AutoCAD Interop libraries corresponding to the AutoCAD version you target:

C:\Program Files (x86)\Common Files\Autodesk Shared\acax19enu.tlb

C:\Program Files (x86)\Common Files\Autodesk Shared\axdb19enu.tlb



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 12 of 12

Anonymous
Not applicable

Thanks for the help. Now our desired result is executing.

0 Likes