.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Question: Using AutoCAD 2012 through an external EXE
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi
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?
Thank you
Solved! Go to Solution.
Re: Question: Using AutoCAD 2012 through an external EXE
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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
Try
' we will connect to a running instance of AutoCAD is we have one.
AcApp = System.Runtime.InteropServices.Marshal.GetActiveOb
Catch ex As Exception
End Try
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
End If
'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
If My.Computer.FileSystem.FileExists("c:\someDocument
Dim acDoc As AcadDocument = AcApp.Documents.Open("c:\someDocument.dwg", False)
'we will do what we want with this document
acDoc.SaveAs("C:\savedDocument.dwg")
If AcAppWasStarted Then ' if the application started AutoCAD then lets let the application close it.
acDoc.Close()
AcApp.Quit()
End If
End If
End Sub
If this does not solve your problem then please post your code on how you are trying to save the drawing..
Re: Question: Using AutoCAD 2012 through an external EXE
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Thank you very much Mike.Wohletz, That solved my problem, I feel I can't thank you enough.
Re: Question: Using AutoCAD 2012 through an external EXE
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi
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.
Re: Question: Using AutoCAD 2012 through an external EXE
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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
Re: Question: Using AutoCAD 2012 through an external EXE
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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?
Thank you
Re: Question: Using AutoCAD 2012 through an external EXE
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.

Re: Question: Using AutoCAD 2012 through an external EXE
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi
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:
System.Threading.Thread.Sleep(4000)
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.
Re: Question: Using AutoCAD 2012 through an external EXE
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.

Re: Question: Using AutoCAD 2012 through an external EXE
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi
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?
Thanks



