The .NET example does not work in dialog form.

The .NET example does not work in dialog form.

Anonymous
Not applicable
952 Views
4 Replies
Message 1 of 5

The .NET example does not work in dialog form.

Anonymous
Not applicable

Hi everyone,

I am going to run a sample code that generates polylines in the following environment.

 

[Environment]

VS 2019 communication, ACAD 2018, .NET Framework 4.7.2

[sample code]

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-NET/files/GUI...

 

I installed objectARX 2018 to add acax22ENU.tlb and axdb22enu.tlb to the reference in Solution Explorer, and added also AcCorMGD.dll, AcCui.dll, AcDbMgd.dll, and AcMgd.dll.

Then I inserted the create polyline example code in the event of button1_Click, which is causing a problem from here.

 

Private void button1_Click(object sender, EventArgs e)

{

    Document acDoc = Application.DocumentManager.MdiActiveDocument; // issue. Application is an ambiguous reference between Autodesk.AutoCAD.ApplicationServices.Application and System.Windows.Forms.Application. 

 

So, I avoided the syntax error by explicitly changing it like this:

    Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

 

However, when I run the application and press button1, error occurs that "Common Language Runtime detected an invalid program." in the modified line.

 

I would appreciate if you let me know where I made the mistake.

0 Likes
Accepted solutions (1)
953 Views
4 Replies
Replies (4)
Message 2 of 5

kerry_w_brown
Advisor
Advisor

Perhaps try something like this :-

 

in your using statements add :

using _cadApp = Autodesk.AutoCAD.ApplicationServices.Application;

in your code body use :

var doc = _cadApp.DocumentManager.MdiActiveDocument;

Regards,

 

 

 

 


// 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 3 of 5

Anonymous
Not applicable

Thank you for your help.

I modified the code as you suggested.

 

[statements add]
using _cadApp = Autodesk.AutoCAD.ApplicationServices.Application;

 

[code - before]
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
[code - after]
var acDoc = _cadApp.DocumentManager.MdiActiveDocument;

 

However, the Common Language Runtime error will still occur when running the application.
There is still a problem in the acDoc variable creation line.

0 Likes
Message 4 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You did not describe the context the "dialog form" is created.

I suspect it's a Windows form executable application (exe).

The AutoCAD API (accoremgd.dll acdbmgd.dll, acmgd.dll libraries) can only be used in AutoCAD application context, i.e. from a dll netloaded in AutoCAD (see this topic).

You should start from here.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 5

Anonymous
Not applicable

That's right.
I was looking at the example code with a windows form executable application.
Thank you for suggesting the right way.

0 Likes