Autocad Application Object in Visual Studio

The root or the trunk of the autocad COM object model is the AcadApplication object. It has to be obtained first to get anything else. If Autocad is running and you know it is running it can be obtained with GetObject.

        Dim app As AcadApplication
        app = CType(GetObject(, "Autocad.Application.22"), AcadApplication)

Autocad.Application.22 is a version specific program ID. you can also use a version independent string, Autocad.Application, but if you have more than one autocad version loaded, it will load the last one used and saved, probably.

here is the Autocad Application object Help page.

the clickable object model is easier to use as a reference base.

I cannot tell that any updates have been made for a long time so the exact year does not matter.

the easiest reference of all and its exactly the same material are the ActiveX API documentation found locally in these folders:

%ProgramFiles%\Common Files\Autodesk Shared\acad_aag.chm
%ProgramFiles%\Common Files\Autodesk Shared\acadauto.chm

In Visual Studio with Strict Typing On I added the Ctype function to get rid of the error.

‘app = GetObject(, “Autocad.Application.22”)
‘Option Strict On disallows implicit conversions from ‘Object’ to ‘AcadApplication’

In autocad VBA help, the root object is “ThisDrawing.Application”. This is because ThisDrawing is provided by the implementation as always available. ThisDrawing is AcadApplication.ActiveDocument. You can run all the sample code without changing that part of it by declaring an AcadDocument variable called ThisDrawing.

        Dim ThisDrawing As AcadDocument
        ThisDrawing = app.ActiveDocument

The only subtle difference, is that your ThisDrawing will not automatically follow a shifting ActiveDocument. Its only current if the ActiveDocument is not changed.

In the clickable object model, objects are noted with an oval, collections are noted with a rectangle. To get to the drawing objects, the path is Application – Documents – Document. ActiveDocument is a property of the Application, a shortcut to the main drawing entities.

here is an Application test file where i demo’ed many of the application methods and properties. most of the actual code is pasted from the help, going thru the application page and clicking on the method or property. i commented out much of it after i ran it once.

 Sub App_Test1()

        Dim app As AcadApplication
        app = CType(GetObject(, "Autocad.Application.22"), AcadApplication)

        Dim ThisDrawing As AcadDocument
        ThisDrawing = app.ActiveDocument

        Debug.Print("AcadApplication Name: " & app.Name)
        Debug.Print("AcadApplication FullName: " & app.FullName)
        Debug.Print("AcadApplication Path: " & app.Path)
        Debug.Print("AcadApplication Version: " & app.Version)

        Debug.Print("AcadApplication HWND: " & app.HWND)
        Debug.Print("AcadApplication LocaleId: " & app.LocaleId)

        Debug.Print("AcadApplication Caption: " & app.Caption)
        Debug.Print("AcadApplication Height: " & app.Height)
        Debug.Print("AcadApplication Width: " & app.Width)
        Debug.Print("AcadApplication WindowLeft: " & app.WindowLeft)
        Debug.Print("AcadApplication WindowTop: " & app.WindowTop)
        Debug.Print("AcadApplication WindowState: " & app.WindowState)

        Debug.Print("AcadApplication Visible: " & app.Visible)

        'GetInterfaceObject
        'not New AutoCAD.AcadAcCmColor
        Dim col As AcadAcCmColor
        'col = app.GetInterfaceObject("AutoCAD.AcCmColor.22")
        'Option Strict On disallows implicit conversions from 'Object' to 'AcadAcCmColor'
        col = CType(app.GetInterfaceObject("AutoCAD.AcCmColor.22"), AcadAcCmColor)
        Debug.Print("GetInterfaceObject ColorMethod: " & col.ColorMethod.ToString())

        'Quit
        'Closes the drawing file And exits the AutoCAD application. 
        'app.Quit()

        'ZoomWindow
        'Dim pt1() As Double
        'Dim pt2() As Double
        'pt1 = Pt(1.3, 7.8, 0)
        'pt2 = Pt(13.7, -2.6, 0)
        'app.ZoomWindow(pt1, pt2)

        'ZoomScaled
        'Dim scalefactor As Double
        ''help exampe - Dim scaletype As Integer
        'Dim scaletype As AcZoomScaleType
        'scalefactor = 2
        'scaletype = AcZoomScaleType.acZoomScaledRelative
        'app.ZoomScaled(scalefactor, AcZoomScaleType.acZoomScaledRelative)
        'app.ZoomScaled(scalefactor, scaletype)

        'ZoomAll
        'ZoomExtents
        'ZoomPickWindow
        'ZoomPrevious
        app.ZoomExtents()
        app.ZoomPickWindow()
        app.ZoomAll()
        app.ZoomPrevious()

        'ZoomCenter
        'Dim zcenter() As Double
        'Dim magnification As Double
        'zcenter = Pt(3, 3, 0)
        'magnification = 10
        'app.ZoomCenter(zcenter, magnification)

        'Update
        app.Update()


        '' This example uses MenuBar to obtain a reference to the AutoCAD File menu.
        '' It then creates a new menu item and inserts it at the bottom of the File menu.
        '' The menu item will be automatically removed when AutoCAD is restarted
        'Dim menu As AcadPopupMenu, newMenuItem As AcadPopupMenuItem
        'Dim openMacro As String
        '' Use MenuBar property to obtain reference to the AutoCAD File menu
        'Menu = ThisDrawing.Application.MenuBar.Item("&File")
        '' Add a menu item to the new menu and
        '' assign an Open macro (VBA equivalent of: "ESC ESC _open ")
        'openMacro = Chr(3) & Chr(3) & Chr(95) & "open" & Chr(32)
        '' Add a menu separator
        'Menu.AddSeparator(menu.Count + 1)
        '' Add new menu item to File menu
        'newMenuItem = menu.AddMenuItem(menu.Count + 1, "NEW MENU ITEM", openMacro)
        'MsgBox("A new menu item has been added to the File menu!")


        ' This example uses MenuGroups to obtain a reference to the AutoCAD main menu.
        ' It then creates a new menu called TestMenu and inserts a menu item
        ' into it. The menu is then displayed on the menu bar.
        ' The menu item will be automatically removed when AutoCAD is restarted
        'Dim currMenuGroup As AcadMenuGroup
        'Dim newMenu As AcadPopupMenu
        '' Use MenuGroups property to obtain reference to main AutoCAD menu
        'currMenuGroup = ThisDrawing.Application.MenuGroups.Item("ACAD")
        '' Create the new menu in this group
        'newMenu = currMenuGroup.Menus.Add("TestMenu")
        '' Add a menu item to the new menu and
        '' assign an Open macro (VBA equivalent of: "ESC ESC _open ")
        'openMacro = Chr(3) & Chr(3) & Chr(95) & "open" & Chr(32)
        'newMenuItem = newMenu.AddMenuItem(newMenu.Count + 1, "Open", openMacro)
        '' Display the menu on the menu bar
        'newMenu.InsertInMenuBar(ThisDrawing.Application.MenuBar.Count + 1)
        'MsgBox("A new menu has been added to the ACAD menu system!")

        'Preferences
        ' This example returns the current setting of
        ' LogFilePath from the preferences object.
        Dim preferences As AcadPreferences
        preferences = ThisDrawing.Application.Preferences
        ' Retrieve the current LogFilePath value
        MsgBox("The current value for LogFilePath is " & preferences.Files.LogFilePath, , "Preferences Example")

        'Documents
        ' This example obtains a reference to the Documents collection
        ' and displays information about the loaded documents.
        Dim Document As AcadDocument
        Dim msg As String
        msg = vbCrLf

        '' Cycle through the Documents collection and retrieve the names
        '' of the loaded documents
        For Each Document In app.Documents
            msg = msg & Document.Name & vbCrLf
        Next

        '' Display loaded document information
        If app.Documents.Count > 0 Then
            MsgBox("The loaded documents are: " & msg)
        Else
            MsgBox("There are no loaded documents!")
        End If

        ' This example reads and displays the current window state of the AutoCAD application.
        Dim CurrentState As String = ""
        ' Use the "WindowState" variable to determine the window state of AutoCAD
        Select Case app.WindowState
            Case AcWindowState.acMin : CurrentState = "Minimized"
            Case AcWindowState.acMax : CurrentState = "Maximized"
            Case AcWindowState.acNorm : CurrentState = "Normal Size"
        End Select

        ' Display window state
        MsgBox("AutoCAD is now: " & CurrentState)

    End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.