.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Question: Using AutoCAD 2012 through an external EXE

18 REPLIES 18
SOLVED
Reply
Message 1 of 19
vbNinja
3216 Views, 18 Replies

Question: Using AutoCAD 2012 through an external EXE

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

18 REPLIES 18
Message 2 of 19
Mike.Wohletz
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..

Message 3 of 19
vbNinja
in reply to: Mike.Wohletz

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

Message 4 of 19
vbNinja
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.

Message 5 of 19
Mike.Wohletz
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

 

Message 6 of 19
vbNinja
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

Message 7 of 19
chiefbraincloud
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
Message 8 of 19
vbNinja
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.

Message 9 of 19
chiefbraincloud
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
Message 10 of 19
vbNinja
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

Message 11 of 19
Mike.Wohletz
in reply to: vbNinja

Just curious if this is currently the only version of AutoCAD installed on this machine? If so has it ever had any other version installed on it?

 

Message 12 of 19
vbNinja
in reply to: Mike.Wohletz

Hi

No, this the only version that I have ever installed on this machine.

Message 13 of 19
chiefbraincloud
in reply to: vbNinja

I'm not sure.  Is it possible that the invisible AutoCAD session is trying to get user input, like asking the user to select the template file for the new drawing or something?

 

Would it be possible for you to attach the smallest version of your code that still has the error so that I can try it here?

Dave O.                                                                  Sig-Logos32.png
Message 14 of 19
vbNinja
in reply to: chiefbraincloud

Hi

Actually I don't think it does, anyway I attached the code you asked for, the exception occures at line 63.

Thanks

Message 15 of 19
Mike.Wohletz
in reply to: vbNinja

It is working every time for me, the only thing that I did see that I would change is cleaning up after accessing the Acad Object. I added this to the end of what you have. 

 

  Finally
                Try
                    acApp.Application.Quit()
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(acApp)
                    GC.Collect()
                    GC.WaitForPendingFinalizers()
                Catch ex2 As Exception
                    MsgBox(ex2.Message)
                End Try

 Please note that if this was going to run several times a minute I would not call the garbage collector on it every time. 

 

Message 16 of 19
vbNinja
in reply to: Mike.Wohletz

Hi

I don't know what to do. although the sleep function somehow solved this problem too, but I am convinced that this solution is not the best way handle this problem.

 

Thank you all for your support.

Message 17 of 19
vbNinja
in reply to: vbNinja

Hi

I want to provide some information about my system that my be useful.

I'm Running:

Windows 7 Home Premium (64-bit edition)

Microsoft Visual Studio 2010 Ultimate

AutoCAD 2012 Structural Detailing (64-bit version)

and I have AutoCAD VBA Enabler installed too

 

can these information help?

Message 18 of 19
chiefbraincloud
in reply to: vbNinja

Well, I am able to run your code (with my message filter code) without incident.

 

I am developing on WinXP 32 bit, with AutoCAD 2012, Visual Studio 2010 Professional.

 

I also built the program (with "Any CPU" as the Target platform) and ran it on a Windows 7 Professional 64 bit, with AutoCAD 2012 64 bit.

 

Neither my dev machine nor the users machine has the VBA enabler installed, but I can't imagine that being the problem.

 

Might I suggest you try what I did and see what happens.  To run your code, I had to create a form and stick a button on it, but then I added a ListBox control, and added a line to the ListBox in each of the message filter functions, and as status updates as the code went through your process.  I have attached my modified version.  Try adding a ListBox to your form and running this new code, to see if you get any messages in the ListBox.

Dave O.                                                                  Sig-Logos32.png
Message 19 of 19
vbNinja
in reply to: chiefbraincloud

Hi

Actually your code worked without any error, here is a summarized version of it's output (I replaced the listbox with a textbox in order to be able to copy from it):


Pending: (336 times)
AutoCAD Started:18.2s (LMS Tech)
Pending: (2 times)
Retry
Pending: (48 times)
Circle Drawn
Pending: (3 times)
Line Drawn
Pending: (2 times)
Doc Closed
Pending: (5 times)
Cad Closed

 

I tested it repeatedly and it worked every time, that is weird because your code is not different from my code.

I can't concentrate right now, I will test your code thoroughly tomorrow and I'll post any problem that might occur.


Thank you very much


 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost