Valued Contributor
Posts: 55
Registered: ‎01-23-2013
Message 1 of 3 (1,125 Views)

Creating a New Instance of AutoCAD .NET and working with Documents

1125 Views, 2 Replies
01-23-2013 08:03 AM

Hey all!


I've run into to some trouble when trying to create an instance of AutoCAD.


In fact, the behavior is strange, and it leads to believe it's not from creating an instance! :smileytongue:



Anyways, I'm coming straight from VB6, so I used:

Dim rpDoc As AcadApplication
Set rpDoc =CreateObject("AutoCAD.Application")
And this worked fine...
Now with moving to VB.NET I'm having some real issues.
Here I'm creating the instance inside a module:
Imports Autodesk.AutoCAD.Interop

Public acadApp As AcadApplication = New AcadApplication


Inside the main Form, in a button1_Click event,  I have this:

acadApp.Visible = True
acadDoc = acadApp.Documents.Open(Me.FileName) 'OpenAcadFile(Me.FileName)
The program crashes the first time it tries to use "acadApp", and I this error:
And sometimes I will see a "acad.exe" in processes in Task Manager
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Project1.exe
Addition information: Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))
Also, I'd like to know what references I need.  After doing lots of internet browsing, I came
up with a whole bunch of different entries I am currenlty using:
AutoCAD 2012 Type Library
AutoCAD/ObjectDBX Common 16.0 Type Library
So.. If you have any advice or know why I am receiving the error, it'd be greatly appreciated!
PS: The errors are also attached to this post.
Thanks much, and God Bless!
Valued Contributor
Posts: 55
Registered: ‎01-23-2013
Message 2 of 3 (1,100 Views)

Re: Creating a New Instance of AutoCAD .NET and working with Documents

01-23-2013 10:43 AM in reply to: Millerni456
Imports Autodesk.AutoCAD.Interop

Imports Autodesk.AutoCAD.Interop
Imports System.IO
Imports System.Runtime.InteropServices

Module modMain
    'Define Global AutoCAD variables.
    Public acadApp As AcadApplication 'System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application") 'New AcadApplication
    Public acadDoc As AcadDocument
    Public selectionSet As AcadSelectionSet

    Public Sub ConnectToAutoCAD()
        Dim tries As Integer
        tries = 0
        acadDoc = Nothing
        Do While (tries < 10 And acadDoc Is Nothing)
            On Error Resume Next
            acadApp = Nothing
            acadApp = Marshal.GetActiveObject("AutoCAD.Application.18.1")

            If Err.Number > 0 Then

                'acadApp = CreateObject("AutoCAD.Application.18")
                acadApp = New AcadApplicationClass

                If Err.Number > 0 Then

                    MsgBox("Instance of 'AutoCAD.Application' could not be created.")
                    Exit Sub
                End If
            End If

            'Open the AutoCAD file
            acadDoc = acadApp.Documents.Open("C:\Test.dwg")

            If Err.Number > 0 Then
                If tries < 10 Then
                    tries += 1
                End If
            End If

        'Close the Drawing1.dwg that always pops up.
        acadApp.Visible = True
    End Sub
End Module

 Well, I got something that worked!

Unfortunately though, it requires that I make several calls to obtain the instance of AcadApplication.

Not sure why but the 3rd iteration is always successful.


Please though, I would like to step away from this code, so I am still looking for better solutions.



*Expert Elite*
Posts: 1,285
Registered: ‎06-21-2004
Message 3 of 3 (1,091 Views)

Re: Creating a New Instance of AutoCAD .NET and working with Documents

01-23-2013 12:16 PM in reply to: Millerni456

You won't need to connect to AutoCAD if you are working with a DLL file, You already have a connection with the document manager. You should only use COM when you have an external application linking into the instance of AutoCAD.


Imports Autodesk.AutoCAD.ApplicationServices

Public Class Class2

    Sub Open(ByVal strFileName)
        If FileExists(strFileName) Then
            Dim dm As DocumentCollection = Application.DocumentManager
        End If

    End Sub

    Function FileExists(ByVal FileFullPath As String) As Boolean
        Dim f As New IO.FileInfo(FileFullPath)
        Return f.Exists
    End Function

End Class



(defun botsbuildbots() (botsbuildbots))
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.