.NET

.NET

Reply
Valued Contributor
Millerni456
Posts: 55
Registered: ‎01-23-2013
Message 1 of 3 (987 Views)

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

987 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.Documents.Item("Drawing1.dwg").Close(False)
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:
AcMgd
AcDbMgd
AutoCAD 2012 Type Library
AutoCAD/ObjectDBX Common 16.0 Type Library
Autodesk.AutoCAD.Interop.Common
 
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!
-Nick.
 
Valued Contributor
Millerni456
Posts: 55
Registered: ‎01-23-2013
Message 2 of 3 (962 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
                Err.Clear()

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

                If Err.Number > 0 Then
                    Err.Clear()

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

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

            If Err.Number > 0 Then
                Err.Clear()
                If tries < 10 Then
                    tries += 1
                End If
            End If
        Loop

        'Close the Drawing1.dwg that always pops up.
        acadApp.Documents.Item("Drawing1.dwg").Close(False)
        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.

 

Thanks!

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 3 of 3 (953 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
            dm.Open(strFileName)
        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))
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 or visit the Installation and Licensing Forum to get help installing your software.