I'm working on a vb.net 2010 project and want to export a dwg file So I'm trying to run AutoCAD, insert drawings and finally save the dwg file at some location.
I found this page talking about this subject and it helped me to run autoCAD in the background and insert some drawings but when I try to save the file nothing happens, no file is saved anywhere and I can't make AutoCAD window appear.
Can anyone help me or is there an official tutorial about this or some website that talks about it clearly?
Solved! Go to Solution.
If you can't see the Application running when you start then it is because you did not tell it to be visible. The following is a sample of opening AutoCAD and saving the document.
Public Sub StartApplication()
Dim AcApp As AcadApplication = Nothing
Dim AcAppName As String = "AutoCAD.Application.18"
Dim AcAppWasStarted As Boolean = False
' we will connect to a running instance of AutoCAD is we have one.
AcApp = System.Runtime.InteropServices.Marshal.GetActiveOb
Catch ex As Exception
If AcApp Is Nothing Then 'if it was not running then lets start the application.
AcApp = CreateObject(AcAppName)
AcApp.Visible = True 'true is we want to see the application running
'Dim acDoc As AcadDocument = AcApp.ActiveDocument ' we will just get the active document
' if we wanted to open a document then it would be like this
Dim acDoc As AcadDocument = AcApp.Documents.Open("c:\someDocument.dwg", False)
'we will do what we want with this document
If AcAppWasStarted Then ' if the application started AutoCAD then lets let the application close it.
If this does not solve your problem then please post your code on how you are trying to save the drawing..
so I'm facing another problem, mostly when I run the code you wrote, it works, but sometimes a message box appears and it says "Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))"
why does it appear randomly and what should I do?
Thanks a lot.
That one can be tough to pinpoint without seeing what is going on, try adding this to the end of the code that I had posted and see if that fixes the problem.
'before we close we need to do some cleanup If Not AcApp Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComO
bject(AcApp) GC.Collect() GC.WaitForPendingFinalizers() End If
Actually the problem still exists, but here is I tried to do: I commented the acDoc.Close() and AcApp.Quit() out, then I opened AutoCAD manually and then ran my program, and it worked perfectly so is it possible that sometimes AutoCAD is not running properly?
This is a problem that appeared after AutoCAD 2010 Update 1. The problem, and the solution, is explained in this Through the Interface POST
You need to implement a reasonably simple iMessageFilter to retry the COM call if it is rejected.
I read your linked article and to be honest I don't know anything about C#, I tried to convert the code to vb.net using some website, but this produces errors that I don't know how to handle.
But I found a solution based on a theory that came up with, the theory says that AutoCAD is not ready to recieve commands whan I am trying to draw on it, so I need to wait for to get ready, but since I don't know how long should I wait, I assumed that 4 seconds is enough, so I insert this line:
right before the code responsible for drawing and actually it worked like a miracle.
Thank you chiefbraincloud and Mike.Wohletz for your help, I really appreciate it.
While it may seem to have solved your problem, waiting 4 seconds will NOT gaurauntee success every time, and it is possible to have a COM call rejected at other times besides when starting the program.
I have clipped out a VB version of the important code from the TTIF post and attached it here as a .txt file. See if you can follow the couple of comments I put in, and merge this code into your form code (or vice/versa). This code worked for me in the only COM code I have which is an installer that opens AutoCAD to create a new profile and workspace using the current ones as a starting point.
So I removed the sleep command and inserted your code and an exception occures at line (Dim doc As AcadDocument = acApp.ActiveDocument) and it says:
The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A(RPC_E_SERVERCALL_RETRYLATER))
what is the problem now?