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:
Public acadApp As AcadApplication = New AcadApplication
Inside the main Form, in a button1_Click event, I have this:
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.GetActiveO
bject("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.
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.
Public Class Class2
Sub Open(ByVal strFileName)
If FileExists(strFileName) Then
Dim dm As DocumentCollection = Application.DocumentManager
Function FileExists(ByVal FileFullPath As String) As Boolean
Dim f As New IO.FileInfo(FileFullPath)
Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register