.NET

Reply
Contributor
vbNinja
Posts: 21
Registered: ‎03-18-2012
Message 1 of 19 (991 Views)
Accepted Solution

Question: Using AutoCAD 2012 through an external EXE

991 Views, 18 Replies
03-18-2012 07:42 AM

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

Valued Mentor
Mike.Wohletz
Posts: 375
Registered: ‎07-29-2008
Message 2 of 19 (982 Views)

Re: Question: Using AutoCAD 2012 through an external EXE

03-18-2012 09:54 AM in reply to: vbNinja

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.GetActiveObject(AcAppName)
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.dwg") Then
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..

Contributor
vbNinja
Posts: 21
Registered: ‎03-18-2012
Message 3 of 19 (977 Views)

Re: Question: Using AutoCAD 2012 through an external EXE

03-18-2012 10:14 AM in reply to: Mike.Wohletz

Thank you very much Mike.Wohletz, That solved my problem, I feel I can't thank you enough.

Contributor
vbNinja
Posts: 21
Registered: ‎03-18-2012
Message 4 of 19 (966 Views)

Re: Question: Using AutoCAD 2012 through an external EXE

03-18-2012 01:30 PM in reply to: vbNinja

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.

Valued Mentor
Mike.Wohletz
Posts: 375
Registered: ‎07-29-2008
Message 5 of 19 (954 Views)

Re: Question: Using AutoCAD 2012 through an external EXE

03-18-2012 03:55 PM in reply to: vbNinja

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.ReleaseComObject(AcApp)
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End If

 

Contributor
vbNinja
Posts: 21
Registered: ‎03-18-2012
Message 6 of 19 (939 Views)

Re: Question: Using AutoCAD 2012 through an external EXE

03-18-2012 11:12 PM in reply to: Mike.Wohletz

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

*Expert Elite*
chiefbraincloud
Posts: 752
Registered: ‎02-13-2008
Message 7 of 19 (912 Views)

Re: Question: Using AutoCAD 2012 through an external EXE

03-19-2012 07:29 PM in reply to: vbNinja

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.

Dave O.                                                                  Sig-Logos32.png
Contributor
vbNinja
Posts: 21
Registered: ‎03-18-2012
Message 8 of 19 (892 Views)

Re: Question: Using AutoCAD 2012 through an external EXE

03-20-2012 01:16 PM in reply to: chiefbraincloud

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.

*Expert Elite*
chiefbraincloud
Posts: 752
Registered: ‎02-13-2008
Message 9 of 19 (886 Views)

Re: Question: Using AutoCAD 2012 through an external EXE

03-20-2012 01:41 PM in reply to: vbNinja

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. 

 

Dave O.                                                                  Sig-Logos32.png
Contributor
vbNinja
Posts: 21
Registered: ‎03-18-2012
Message 10 of 19 (867 Views)

Re: Question: Using AutoCAD 2012 through an external EXE

03-21-2012 06:46 AM in reply to: chiefbraincloud

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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community