Author Topic: VB.NET - Open an Existing Dwg from Stand-Alone exe  (Read 11953 times)

0 Members and 2 Guests are viewing this topic.

Bill Tillman

  • Guest
VB.NET - Open an Existing Dwg from Stand-Alone exe
« on: August 17, 2012, 09:53:19 AM »
I'm running VB 2010 Express with AutoCAD 2013. I need to find a way to code a stand-alone exe which will check for an existing instance of AutoCAD already running and then open an existing drawing file. I think I have most of it down but the trouble is when AutoCAD is already running. I either end up with a Open new drawing with the Select Template dialog window showing or it throws an exception which says I need to use New, but that doesn't resolve the problem. If AutoCAD is not open, this works pretty good so far. RenderMan posted some code previously which got me off to a good start. I have the references "AutoCAD 2013 Type Library" and "AutoCAD/ObjectDBX Common 19 Library" loaded for this.

Code: [Select]
Imports AutoCAD
Imports AutoCAD.AcadDocumentClass

Module Module1

    Sub Main()

        Dim NovellPath As String = "\\my_novell_path\dwg\"
        Dim strFileName As String = NovellPath & "my_dwgfile.dwg"

        Dim vAcadApp As AutoCAD.AcadApplication
        Dim vAcadDoc As AutoCAD.AcadDocument
        Try
            'Look for an existing process if availabe getObject
            If Process.GetProcessesByName("acad").Length > 0 Then
                vAcadApp = CType(GetObject(, "Acad.Application"), AcadApplication)
                'Else createObject
            Else
                vAcadApp = New AutoCAD.AcadApplication
                vAcadApp.Visible = True
                vAcadApp.WindowState = AutoCAD.AcWindowState.acMax
                vAcadDoc = vAcadApp.Documents.Open(strFileName, False)

            End If
            vAcadApp.Open(strFileName, True)

        Catch ex As Exception

        End Try
    End Sub
End Module

BlackBox

  • King Gator
  • Posts: 3770
Re: VB.NET - Open an Existing Dwg from Stand-Alone exe
« Reply #1 on: August 17, 2012, 10:36:47 AM »
Doesn't the Open() Method need to be invoked upon the Document Collection Object, and not the Application Object?

Also, you might consider doing 'something' with the Exception(s) you Catch (and look for any Exceptions for Methods you invoke elsewhere in your code as well)... Pseudo example:

Code - vb.net: [Select]
  1. ' <snip>
  2.  
  3.             Catch ex As Autodesk.AutoCAD.Runtime.Exception
  4.                 ed.WriteMessage(vbLf & "AutoCAD error: {0}" & vbLf, ex.Message)
  5.                 Return Nothing
  6.  
  7.             Catch ex As System.Exception
  8.                 ed.WriteMessage(vbLf & "System error: {0}" & vbLf, ex.Message)
  9.                 Return Nothing
  10.  
  11.             End Try
  12.  
  13. ' <snip>
  14.  
"How we think determines what we do, and what we do determines what we get."

n.yuan

  • Bull Frog
  • Posts: 348
Re: VB.NET - Open an Existing Dwg from Stand-Alone exe
« Reply #2 on: August 17, 2012, 10:42:33 AM »
Doesn't the Open() Method need to be invoked upon the Document Collection Object, and not the Application Object?

Also, you might consider doing 'something' with the Exception(s) you Catch (and look for any Exceptions for Methods you invoke elsewhere in your code as well)... Pseudo example:

Code - vb.net: [Select]
  1. ' <snip>
  2.  
  3.             Catch ex As Autodesk.AutoCAD.Runtime.Exception
  4.                 ed.WriteMessage(vbLf & "AutoCAD error: {0}" & vbLf, ex.Message)
  5.                 Return Nothing
  6.  
  7.             Catch ex As System.Exception
  8.                 ed.WriteMessage(vbLf & "System error: {0}" & vbLf, ex.Message)
  9.                 Return Nothing
  10.  
  11.             End Try
  12.  
  13. ' <snip>
  14.  

The OP is doing standalone EXE, thus cannot use Autodesk.AutoCAD.Runtime namespace, nor can Editor.WriteMessage() be used.

Bill Tillman

  • Guest
Re: VB.NET - Open an Existing Dwg from Stand-Alone exe
« Reply #3 on: August 17, 2012, 11:02:31 AM »
Yes indeed, this one is to be a stand-alone. I think I found something on a post from several years ago on the Autodesk Forum. I have adjusted it for my needs here....and the original one had a GoTo in it. If I recall correctly, the GoTo statement is bad practice. I recall taking a C++ class way back in 1992 and the teacher told the first thing he was going to do was to take away our GoTo. Here is the code I've tested and it seems to be working like I need it...sans some additional error handling. I only needed the AutoCAD 2013 Type Libraries for this to work. This will eventually be in a WAF so the user can select which of about a dozen different templates we have available.

Code: [Select]
Imports AutoCAD
Imports AutoCAD.AcadDocumentClass

Module Module1
    Sub Main()
        Dim vAcadApp, vNewFile As Object
        Dim strNovellPath As String = "\\my_novell_path\dwg\"
        Dim strFileName As String = "drawing templates(xx).dwg"

        On Error Resume Next
        vAcadApp = GetObject(, "autocad.Application")
        If Err.Number <> 0 Then
            Err.Clear()
            vAcadApp = CreateObject("autocad.Application")
        End If

        vAcadApp.Visible = True
        Err.Clear()
        vNewFile = vAcadApp.Documents.Open(strNovellPath & strFileName, False)

    End Sub

End Module

This will open the existing drawing I'm wanting and if AutoCAD is already running it uses that instance of it, even if there are other drawings open in that instance, this one will be come the Active drawing. If AutoCAD is not running it will start it up and then open the requested drawing. I would appreciate anyone helping me to add the correct error handling in this. I'm still new to a lot of this .NET format.

Bill Tillman

  • Guest
Re: VB.NET - Open an Existing Dwg from Stand-Alone exe
« Reply #4 on: August 17, 2012, 11:11:10 AM »
Doesn't the Open() Method need to be invoked upon the Document Collection Object, and not the Application Object?

I think you're right on that RenderMan....that's the problem. The docs available for .NET are sorely lacking and most of the stuff I'm doing in automation has never been done before that's why everyone thinks I'm a whack job. But what I did find is that DocumentCollection is now DocumentCollectionExtension and .Open did not work when I tried to add to it. Perhaps I read the .NET migration tables wrong. And that is another stumbling block to all this. So many of the posts and articles I read which are sometimes months or even years ago written are now obselete. Heck the code in the new 2013 .NET Developer's Guide won't even work unless you make major changes to the syntax based on all the recent changes. Maybe I'm am a whack job after all for trying to learn this stuff.... :lol:

HYPERPICS

  • Guest
Re: VB.NET - Open an Existing Dwg from Stand-Alone exe
« Reply #5 on: August 23, 2012, 04:23:08 PM »
Heck the code in the new 2013 .NET Developer's Guide won't even work unless you make major changes to the syntax based on all the recent changes.

Which code samples are you having problems with in the .NET Developer's Guide?

The following is similar to what I have used in the past to start AutoCAD.

Code - vb.net: [Select]
  1. <CommandMethod("LaunchAcad")> _
  2. Public Sub LaunchAcad()
  3.     Dim AppComObj As AcadApplication = Nothing
  4.     Dim ProgId As String = "AutoCAD.Application.19"
  5.  
  6.     Try
  7.         ' Get a running instance of AutoCAD
  8.         AppComObj = GetObject(, ProgId)
  9.     Catch ex As System.Exception
  10.         Try
  11.             ' Create a new instance of AutoCAD
  12.             AppComObj = CreateObject(ProgId)
  13.         Catch ex2 As Exception
  14.             MsgBox(ex2.Message)
  15.  
  16.             Exit Sub
  17.         End Try
  18.     End Try
  19.  
  20.     ' Display the application and return the name and version
  21.     AppComObj.Visible = True
  22.     MsgBox("Now running " & AppComObj.Name & " version " & AppComObj.Version)
  23.  
  24.     ' Get the active document
  25.     Dim DocComObj As AcadDocument = AppComObj.ActiveDocument
  26.  
  27.     MsgBox(DocComObj.Name)
  28. End Sub