errors while build and run old vs c# project while referencing full autocad 2019

errors while build and run old vs c# project while referencing full autocad 2019

Anonymous
Not applicable
3,340 Views
15 Replies
Message 1 of 16

errors while build and run old vs c# project while referencing full autocad 2019

Anonymous
Not applicable

Hello,

I am trying to fix a bug in old 2003 vs project created a long time ago in my company. The project references an old autocan version (not sure which version. If it is important I will find it  in one of the following days when someone will be in he office).

At my computer at home I have Visual Studio Community 2017 and I installed an autocad 2019 to test if I can build the old vs project.

I get  about 100 errors of un-recognized type or namespace such as Interop, AcadApplication, AcadDocument, AcadEntity, etc...

I do see that some autocad includes succeed, for example:

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Geometry;

 

I tried to find a solution in google and in the forums here and in VS.

I understood that it is probably related to adding references to the project, so I entered the COM part of the Reference Manager and added all the autocad 2019 Type Library, all the autocad Map MPolygon Type Library and all autocad/ObjectDBX Common 23.0 Type Library.

After getting help in the main Forum, I uninstalled the LT version and downloaded and installed the full version.

I added a reference to the: AutoCAD Focus Control for VBA Type Library, but all the references are still not recognized.

 

Help will be very appriciated.

0 Likes
Accepted solutions (1)
3,341 Views
15 Replies
Replies (15)
Message 2 of 16

norman.yuan
Mentor
Mentor

Firstly, you did not say the VS2003 project is what type of app (DLL project, to be run inside AutoCAD, either using AutoCAD API, or using AutoCAD COM API and running with AutoCAD VBA; or EXE project, automating AutoCAD via COM API). Which AutoCAD version then probably is not important here, because you now are using AutoCAD 2019, you need to set references to Acad2019 .NET assemblies anyway, whether using .NET APIs, or COM APIs. However, if that old VS project was targeting AutoCAD2005 or older, it definitely used AutoCAD COM API, not .NET API (thus the references you listed were not used then); but you still need to tell it is DLL or EXE app.

 

Once you make it clear the project is a DLL or EXE, using AutoCAD .NET APIs or COM APIs, which references you need would be easy/simple to decide.

 

I  am not sure why AutoCAD Focus Contrl for VBA is relevant here: it is only needed if in VBA code if modeless form is used and have nothing to do with  code in VS (C# or VB.NET), using AutoCAD .NET API or AutoCAD COM API.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 16

Anonymous
Not applicable
Hi Norman,
Thank you. I'm new to all of this.
If I understand correctly what you wrote, then it is a c# exe project using
autocad API, probably the com API because it is old.
VBA isn't needed. Somebody answered my question in a different forum here
and told me to install the full autocad instead of the LT one, and that was
the only additional reference that I could add.

0 Likes
Message 4 of 16

norman.yuan
Mentor
Mentor

Full AutoCAD is required for any AutoCAD programming, be it VBA, or .NET API, DLL or EXE COM automation.

 

Since you are using AutoCAD 2019, make sure your VisualStudio2017 has .NET 4.7x development components installed (depending on the OS, it is not installed by default, as as with my Win7 box), because AutoCAD 2019 uses .NET 4.7, thus, your VS project should target .NET 4.7.

 

For setting reference to AutoCAD COM interop API, you can either use VS's adding references dialog's COM Tab, find the AutoCAD 2019 type librarry and ObjectDBX Common type library, click OK so that VS would generate the interop assemblies for you.

 

However, the recommended approach is to directly set reference to the interop assemblies coming with AutoCAD installation (Autodesk.AutoCAD.Interop.dll and Autodesk.AutoCAD.Interop.Common.dll), and also make sure they are not be set to "Embedded" and "Copy Local" set to False.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 16

Anonymous
Not applicable

I realy appriciate your help.

I was looking all over the Visual Studio 2017 for the place to target the .Net 4.7x development components but couldn't find them.

It should be in the "Tools->Choose Toolbox Items"? If yes, I din't find it there.

0 Likes
Message 6 of 16

norman.yuan
Mentor
Mentor

I use full VS2017, thus not sure if what I say applies to your VS2017 Community version.

 

In your project, go to its "Properties" and on the "Application" tab, the "Target Framework" should be set to .NET Framework 4.7xx. Actually, if you start a new solution, the new solution dialog box should give you chance to choose targeting MS .NET framework. If 4.7x is available in .NET framework dropdown list, you are good to go.

 

If 4.7x version is not listed in the dropdown list, it means your VS installation does not include .NET 4.7 development components. If it is this case, you need to run VIsual Studio Installer again, and then choose "Modify"->"Individual components", then make sure .NET framework 4.7xs are checked.

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 16

Anonymous
Not applicable

Thank you very much for the help.

I have run the VS installation and added the .NET 4.7 development components from the list. My computer then needed to reboot, and then I have changed the Target Framework of the project through Properties->Application tab as you explained.

The errors are still there.

Is there something else I need to do?

0 Likes
Message 8 of 16

norman.yuan
Mentor
Mentor

Now that you have the project target .NET 4.7x, it is very likely the errors are due to the referenced interop assemblies are not correct. 

 

Have you removed the old references to AutoCAD COM interop assemblies and re-added with ones from AutoCAD2019. As I previously said, you can either use the ones that comes with AutoCAD installation - Autodesk.AutoCAD.Interop.dll and Autodesk.AutoCAD.Interop.Common.dll; or you can go through "References" dialog box->COM tab->AutoCAD 2019 type library and AUtoCAD 2019 DBX Common Type library, so that VS will generate the COM interop assemblies for you. The former is recommended, but make sure to set "Copy Local" to False. For the latter, make sure "Copy Local" is set to True. In both cases, do not embed the interop assemblies into he project (VS default when added COM interop references). Also not, for the former, the code namespace is Autodesk.AutoCAD.Interop[/Common], but if the interop assemblies are generated by VS, the namespace is likely be AutoCAD and AXDBLib.

 

So, you need to examine the references set in your project, and better yet, remove existing ones and reset them. Then examine the "using [namespace]" direction on top of each code file. I bet if you have massive errors, it is likely just a simple namespace issue.

 

You may want to post pictures showing the actual error, if you still cannot solve it.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 9 of 16

Anonymous
Not applicable

 

1) As you recommended I removed the two refernces.

2) I wanted to add back the references as you wrote, but in the list in the COM tab there is a list of about 10 of each. Do I need all or only one of each?

3) I added one of each, but got mixed up of your explanation:

a. The AutoCAD reference name is Interop.AutoCAD and is locked for editing.

b. The AutoCAD reference "Copy Local" is "True". I changed it to "False". Is this what you wrote?

c. The AutoCAD reference "Embed Interop Types" is "True". I changed it to "False".

d. The AXDBLib reference name is Interop.AXDBLib and is locked for editing.

e. The AXDBLib reference "Copy Local" is "True". To leave it that way? Is this what you wrote?

f. The AXDBLib reference "Embed Interop Types" is "True". I changed it to "False".

4) The namespace written in the code is using "Autodesk.AutoCAD.Interop.Common" for example, but it doesn't recognize the using "Interop"

I'm adding a screenshot too.

0 Likes
Message 10 of 16

Anonymous
Not applicable

One more thing:

On Monday you wrote:

"However, the recommended approach is to directly set reference to the interop assemblies coming with AutoCAD installation (Autodesk.AutoCAD.Interop.dll and Autodesk.AutoCAD.Interop.Common.dll), and also make sure they are not be set to "Embedded" and "Copy Local" set to False."

And today you also worte about it.

I tried to find where I should do this but didn't find.

Can you please elaborate where this can be done?

Thank you very much

0 Likes
Message 11 of 16

norman.yuan
Mentor
Mentor

As my last reply said, it is recommended to set reference to the COM interop assemlies provided by Autodesk, which comes with AutoCAd installation, rather than letting VS generates them (by using Reference dialog box->COM tab).

 

Do to it, you open "Add References" dialog box, clock "Browse..." button, navigate to AutoCAD installation C:\Program files\Autodesk\AutoCAD 2019\", find both Autodesk.AutoCAD.Interop.dll and Autodesk.AutoCAD.Interop.Common.dll. That's it.

 

These 2 interop assemblies contains namespace Autodesk.AutoCAD.Interop.[Common]. Make sure the 2 assemblies are set "Copy Local" to False, "Embed Interop Types" to False.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 12 of 16

Anonymous
Not applicable

Thank you very much. The last explanation was very helpfull.

After adding the dll references as you explained, all the errors were gone.

But now I get an exception while running the code. Maybe you can help me with this too. It looks related.

In the following line of code:

m_acadApp = new Autodesk.AutoCAD.Interop.AcadApplication();

The application throws an exception of type: System.Runtime.InteropServices.COMException.

Once I got it with: CO_E_SERVER_EXEC_FAILURE, and once with: REGDB_E_CLASSNOTREG.

 

In my Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.1 folder I have found some System.Runtime dll files, but I didn't find one like System.Runtime.InteropServices.COMException.

 

Please help me with this one too

0 Likes
Message 13 of 16

norman.yuan
Mentor
Mentor
Accepted solution

Well, it looks like the code itself it now OK for compiling with correct references. However, you really have not given many details on the application, except for it is an EXE app.

 

Is it a regular desktop EXE app run by a user, which in turn automates AutoCAD (another application that must run in the same computer) to do something? Or the app somehow runs as some sort of unattended server app?

 

If it is former, there is very few reason for that line of code not working, it the user, who runs the application, can also run AutoCAD 2019 in the same computer. If it is latter, I'd say, forget the app, it is not worth your effort doing things that way.

 

Again, not knowing more of your app's details (what it does, how it runs...), cannot suggest more. But keep in mind: using EXE to automate AutoCAD, a heavy desktop app, is hardly a good business solution in most cases.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 14 of 16

Anonymous
Not applicable

Hi again,

This is an internal desktop application (not on server) which is run in our office in order to extact specific information from dwg drawings into our database for further use.

It runs AutoCAD with a folder full of DWG drawings and collects the relevant information from all drawings.

 

Do you have some more ideas about the problem?

Thank you very much for the help until now.

0 Likes
Message 15 of 16

norman.yuan
Mentor
Mentor

Well, I am really not sure what to suggest here with the very limited information.

 

With the computer that runs your application, you may want to confirm this:

1. Run AutoCAD (full AutoCAD, or AutoCAD vertical, such as Acad Map, Acad Civil3d, ..., but not AutoCAD LT!) as CAD user while you are logged into this computer.

2. If 1 is OK, you run your EXE application. If the code is correct, at least this line of code should work:

 

m_acadApp = new Autodesk.AutoCAD.Interop.AcadApplication();

 

If not, either the AutoCAD reference is not the same version as the AutoCAD installed in this computer, or the AutoCAD installation is somehow messed up that you may need to reinstall.

 

Again, unless I know the full details about what are in the computer and see the details of the EXE project, it is really not much to say. How the other hand, for that specific line of code not working, it should not hard to figure out why if someone has access to the computer and to your project code.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 16 of 16

Anonymous
Not applicable

Thank you for all your help.

I probably found the problem. I downloaded a full AutoCAD trial version which should have worked for a month for evaluating, but after three days the trial ended and when I tried to open the application it wrote that the trial expired.

I have no idea why it happened.

0 Likes