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

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

2 REPLIES 2
Reply
Message 1 of 3
Millerni456
2982 Views, 2 Replies

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

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! 😛

 

 

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.
 
2 REPLIES 2
Message 2 of 3
Millerni456
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!

Message 3 of 3
arcticad
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))

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