<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Creating a New Instance of AutoCAD .NET and working with Documents in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/creating-a-new-instance-of-autocad-net-and-working-with/m-p/3760039#M51460</link>
    <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Imports Autodesk.AutoCAD.ApplicationServices&lt;BR /&gt;&lt;BR /&gt;Public Class Class2&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sub Open(ByVal strFileName)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If FileExists(strFileName) Then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim dm As DocumentCollection = Application.DocumentManager&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dm.Open(strFileName)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Function FileExists(ByVal FileFullPath As String) As Boolean&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim f As New IO.FileInfo(FileFullPath)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Return f.Exists&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Function&lt;BR /&gt;&lt;BR /&gt;End Class&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 23 Jan 2013 20:16:05 GMT</pubDate>
    <dc:creator>arcticad</dc:creator>
    <dc:date>2013-01-23T20:16:05Z</dc:date>
    <item>
      <title>Creating a New Instance of AutoCAD .NET and working with Documents</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-a-new-instance-of-autocad-net-and-working-with/m-p/3759772#M51458</link>
      <description>&lt;P&gt;Hey all!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've run into to some trouble when trying to create an instance of AutoCAD.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In fact, the behavior is strange,&amp;nbsp;and it leads to believe it's not from creating an instance! &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyways, I'm coming straight from VB6, so I used:&lt;/P&gt;&lt;DIV&gt;&lt;STRONG&gt;Dim&amp;nbsp;rpDoc As AcadApplication&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;Set rpDoc =CreateObject("AutoCAD.Application")&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;And this worked fine...&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Now with moving to VB.NET I'm having some real issues.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Here I'm creating the instance inside a module:&lt;BR /&gt;&lt;STRONG&gt;Imports Autodesk.AutoCAD.Interop&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;P&gt;&lt;STRONG&gt;Public acadApp As AcadApplication = New AcadApplication&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Inside the main Form, in a button1_Click event,&amp;nbsp; I have this:&lt;/P&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;acadApp.Documents.Item("Drawing1.dwg").Close(False)&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;acadApp.Visible = True&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;acadDoc = acadApp.Documents.Open(Me.FileName) 'OpenAcadFile(Me.FileName)&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The program crashes the first time it tries to use "acadApp", and I this error:&lt;/DIV&gt;&lt;DIV&gt;And sometimes I will see a "acad.exe" in processes in Task Manager&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Project1.exe&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;Addition information: Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Also, I'd like to know what references I need.&amp;nbsp; After doing lots of internet browsing, I came&lt;/DIV&gt;&lt;DIV&gt;up with a whole bunch of different entries I am currenlty using:&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;AcMgd&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;AcDbMgd&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;AutoCAD 2012 Type Library&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;AutoCAD/ObjectDBX Common 16.0 Type Library&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;Autodesk.AutoCAD.Interop.Common&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;So.. If you have any advice or know why I am&amp;nbsp;receiving the error, it'd be greatly appreciated!&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;PS: The errors are also attached to this post.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks much, and God Bless!&lt;/DIV&gt;&lt;DIV&gt;-Nick.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Wed, 23 Jan 2013 16:15:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-a-new-instance-of-autocad-net-and-working-with/m-p/3759772#M51458</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2013-01-23T16:15:16Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a New Instance of AutoCAD .NET and working with Documents</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-a-new-instance-of-autocad-net-and-working-with/m-p/3759952#M51459</link>
      <description>&lt;PRE&gt;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 &amp;lt; 10 And acadDoc Is Nothing)
            On Error Resume Next
            acadApp = Nothing
            acadApp = Marshal.GetActiveObject("AutoCAD.Application.18.1")

            If Err.Number &amp;gt; 0 Then
                Err.Clear()

                'acadApp = CreateObject("AutoCAD.Application.18")
                acadApp = New AcadApplicationClass

                If Err.Number &amp;gt; 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 &amp;gt; 0 Then
                Err.Clear()
                If tries &amp;lt; 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&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Well, I got something that worked!&lt;/P&gt;&lt;P&gt;Unfortunately though,&amp;nbsp;it requires that I make several calls to obtain the instance of AcadApplication.&lt;/P&gt;&lt;P&gt;Not sure why but the 3rd iteration is always successful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please though, I would like to step away from this code, so I am still looking for better solutions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jan 2013 18:44:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-a-new-instance-of-autocad-net-and-working-with/m-p/3759952#M51459</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2013-01-23T18:44:22Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a New Instance of AutoCAD .NET and working with Documents</title>
      <link>https://forums.autodesk.com/t5/net-forum/creating-a-new-instance-of-autocad-net-and-working-with/m-p/3760039#M51460</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Imports Autodesk.AutoCAD.ApplicationServices&lt;BR /&gt;&lt;BR /&gt;Public Class Class2&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sub Open(ByVal strFileName)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If FileExists(strFileName) Then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim dm As DocumentCollection = Application.DocumentManager&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dm.Open(strFileName)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Function FileExists(ByVal FileFullPath As String) As Boolean&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim f As New IO.FileInfo(FileFullPath)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Return f.Exists&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Function&lt;BR /&gt;&lt;BR /&gt;End Class&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jan 2013 20:16:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/creating-a-new-instance-of-autocad-net-and-working-with/m-p/3760039#M51460</guid>
      <dc:creator>arcticad</dc:creator>
      <dc:date>2013-01-23T20:16:05Z</dc:date>
    </item>
  </channel>
</rss>

