Connection Between AutoCAD with VB.NET & how to make GUI for passing values

Connection Between AutoCAD with VB.NET & how to make GUI for passing values

Anonymous
Not applicable
4,334 Views
16 Replies
Message 1 of 17

Connection Between AutoCAD with VB.NET & how to make GUI for passing values

Anonymous
Not applicable

Hello,

            my question is how to connect vb.net with AutoCAD 2016 through .NET GUI and passing the parameters or commands to AutoCAD through VB.NET GUI. Actually I run some code to create the Instance of AutoCAD but it gives error. i have send this error as attachment. Please give the solution ASAP. 

 

Thanx 

Mohit

0 Likes
4,335 Views
16 Replies
Replies (16)
Message 2 of 17

_gile
Consultant
Consultant

Hi,

 

In the picture you provide, there's not any VB.net, it's C# language using COM interop.

 

So, I'm afraid it's impossible to quickly give you a solution as it looks like you're missing some basics.

 

Is there any reason you use COM Interop instead of the .NET API (e.g. standalone application)?

Did you reference the COM libraries corresponding to the AutoCAD version (2016) ?

Was there an instance of AutoCAD running when you clicked the button?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 17

Anonymous
Not applicable

Thanx Gile,

                 Actually I send my problem fastley. Ya this is C# language. I add two refrence file that is AutoCAD and AXDBLib. 

"AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application");" on this line there is a exception.. Have you understood my problem. I can check in the task manager there is no other instance is open.

 

Thanx 

Mohit

0 Likes
Message 4 of 17

Alexander.Rivilis
Mentor
Mentor

@Anonymous

My recommendations:

1. Download and install ObjectARX SDK appropriate version: http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=785550

2. Add reference to Autodesk.AutoCAD.Interop.dll and Autodesk.AutoCAD.Interop.Common.dll from ObjectARX SDK \inc-Win32 or \inc-x64 directory dependent on bitness (32 or 64) of your application. Also set Copy Local to false for those dll's.

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 5 of 17

_gile
Consultant
Consultant

 

AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application");

Marshal.GetActiveObject() method throw an exception if there's none running process of the application.

 

 

Typically, we try to get a running process, and try to start a new one if it failed.

 

try // try to get an already running AutoCAD process
{
    AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application");
}
catch
{
    try // try to start a new AutoCAD process
    {
	AcadApp = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("AutoCAD.Application")); 
}
catch
{
MessageBox.Show("Unable to start AutoCAD.");
return;
}
}

 

 Anyway, you didn't reply to my questions...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 17

Anonymous
Not applicable
I have added your code but there is error on the below line
AcadApp = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("AutoCAD.Application"));
SystemArgumentException
Thanx
Mohit






 

 

0 Likes
Message 7 of 17

Anonymous
Not applicable

Dear Alexander,

                        Thanx for your reply, i am download ObjectARX SDK and add both dlls in the program. and set Copyto local in false. But it will give same error. 

 

Thanx 

Mohit

0 Likes
Message 8 of 17

Anonymous
Not applicable

Dear Gile,

              I have run this code and getting some exception.please see the attachment.

 

AcadApp = DirectCast(Marshal.GetActiveObject("AutoCAD.Application"), Autodesk.AutoCAD.Interop.AcadApplication)


Dim blockObj As Autodesk.AutoCAD.Interop.Common.AcadBlock
Dim insertionPnt(0 To 2) As Double
insertionPnt(0) = 0.0# : insertionPnt(1) = 0.0# : insertionPnt(2) = 0.0#

' Insert the block
Dim blockRefObj As Autodesk.AutoCAD.Interop.Common.AcadBlockReference
insertionPnt(0) = 2.0# : insertionPnt(1) = 2.0# : insertionPnt(2) = 0
blockRefObj = AcadApp.ActiveDocument.ModelSpace.b(insertionPnt, "D:\Pjojects\Dol Power circuit Programming\CircuitModel\ACB.dxf", 1.0#, 1.0#, 1.0#, 0)

 

thanx

Mohit

0 Likes
Message 9 of 17

_gile
Consultant
Consultant

Hi,

 


MDUTTA a écrit :

' Insert the block
Dim blockRefObj As Autodesk.AutoCAD.Interop.Common.AcadBlockReference
insertionPnt(0) = 2.0# : insertionPnt(1) = 2.0# : insertionPnt(2) = 0
blockRefObj = AcadApp.ActiveDocument.ModelSpace.b(insertionPnt, "D:\Pjojects\Dol Power circuit Programming\CircuitModel\ACB.dxf", 1.0#, 1.0#, 1.0#, 0)


As far as I know (but I'm not very cumfortable with VB), the ModelSpace object do not have a 'b' method.

To insert a block reference, you have to use the InsertBlock method.

 

blockRefObj = AcadApp.ActiveDocument.ModelSpace.InsertBlock(insertionPnt, "D:\Pjojects\Dol Power circuit Programming\CircuitModel\ACB.dxf", 1.0#, 1.0#, 1.0#, 0)

I find it odd that Visual Studio did not report this error at edition time (i.e. before compiling), it may be a special feature of VB ...

 

As I (and some others) recommend to those who start learning AutoCAD .NET customization : learn C# instead of VB and avoid, as much as possible, using COM.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 17

Anonymous
Not applicable

yes but this gives exception. what can i do

0 Likes
Message 11 of 17

Anonymous
Not applicable

Then please tell the C# code for reinserting the block.

 

Thanx

Mohit

0 Likes
Message 12 of 17

_gile
Consultant
Consultant

It seems to me you cannot use the InsertBlock method wit a dxf file, it have to be a dwg file.

As you're using COM, maybe you can try with SendCommand and the native INSERT command but it should be asynchronous.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 13 of 17

Anonymous
Not applicable

please see the attachment i am using dxf or dwg file both gives exception

0 Likes
Message 14 of 17

_gile
Consultant
Consultant

What if you try to "manually" insert this dwg in a drawing using the INSERT command and Browse option?

 

Don't you have the same "Self Reference" error?

 

Isn't your "ACB.dwg" file containing a block named "ACB"?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 15 of 17

Anonymous
Not applicable

Manually i use INSERT command and it is sucessfully inserted but through vb program it will give the self reference error.


Yes the block name is ACB

 

Thanx

Mohit

0 Likes
Message 16 of 17

_gile
Consultant
Consultant

Sorry,

 

I do not understand, as you're using VB with COM Interop, which is closed to VBA, perhaps you'd have more chance in the Visual Basic Customization forum.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 17 of 17

Juergen_Becker
Advocate
Advocate

Hi Folks,

 

what I don't understand is:

Why you are using AutoCAD as a COM-Object?

Is it necessary to develop a program as a Windows program?

Why you don't develop this prorgam within the AutoCAD enviroment?

 

Regards Jürgen

 

I hope my tip helps. If so then give me kudos and mark the tip as a solution.
Thanks.

Jürgen A. Becker
Building Services

Development and Support
Autodesk Forge Spezialist


CAD-Becker.de
https://www.CAD-Becker.de

0 Likes