AutoCAD Architecture Automation Concepts [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Up a level to ..
Understanding ActiveX Automation in AutoCAD Architecture
AutoCAD Architecture Automation Concepts

Topics in this section:

  • AEC Objects
  • Derivation of AEC Objects

Using ActiveX Automation, you can develop custom applications for AutoCAD Architecture (ACA) using Visual Basic for Applications (VBA). You can also exchange data with other applications that support ActiveX Automation, such as Microsoft Access and Microsoft Excel. For example, you can extract data from an AutoCAD Architecture schedule table and load it into an Excel spreadsheet.

At the present time, there are some limitations as to what you can do using VBA and ActiveX Automation. For example, you can use VBA to determine what style is applied to a wall, and you can assign a different style to that wall, but you cannot change the properties of a wall style or create a new style. The following list summarizes the capabilities and limitations of ActiveX Automation in AutoCAD Architecture:

  • Walls, doors, and windows (but not window assemblies) are almost fully exposed, and most of the things you can do with them interactively can be done using Automation (changing wall styles is an exception).
  • Slabs, for floors, are partially exposed. You can modify many properties of existing slabs, but you can't create valid new ones. Most properties set through styles are not exposed.
  • You cannot add roof edges and roof faces, limiting your ability to add new roofs to a drawing.
  • Stairs and railings are partially exposed. Most of the properties that are set through styles are not available, but many of the properties set directly in the object are exposed and can be modified.
  • Curtain wall use is very limited, as most of its properties are not exposed.
  • Structural member objects are mostly, but not completely, exposed. You cannot create new structural member styles using just Automation.
  • Schedules are mostly exposed and most schedule features are available through Automation.

The Reference sections of AutoCAD Architecture Developer Documentation indicate what you can and cannot do with each AutoCAD Architecture object.

More AutoCAD Architecture features will be exposed in later releases of AutoCAD Architecture.


AEC Objects [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

AutoCAD Architecture Automation Concepts
AEC Objects

An object is the main building block of the AutoCAD Architecture Automation interface. Each object exposes a specific part of AutoCAD Architecture. Automation objects may or may not represent visible drawing objects. Visible drawing objects, such as doors, walls, and windows, are sometimes referred to as entities in order to distinguish them from non-visible Automation objects. All entities are objects, but not all objects are entities. An example of an object that is not visible is the door style. A door style defines the component dimensions and other properties of a door. In AutoCAD Architecture Automation, a door style is exposed as an AecDoorStyle object. You can't see this object, but you can set its properties, such as door stop depth, and the change will affect the appearance of any door the style is applied to.

The Reference sections in AutoCAD Architecture Developer Documentation list and describe the objects comprising the AutoCAD Architecture object model.


Derivation of AEC Objects [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

AutoCAD Architecture Automation Concepts
Derivation of AEC Objects

Most of the AEC objects have their roots in the AutoCAD object model. For example, the following diagram illustrates how AEC entity objects are derived from AutoCAD objects:

The base class for all AEC graphical objects is AecEntity. AecEntity is derived from the AutoCAD AcadEntity object, and provides most of the internal connections to AutoCAD. From the AecEntity object, you can access properties common to all AEC and AutoCAD entities, such as Layer and Linetype. AecEntity also provides methods common to AutoCAD and AEC entities, such as Move and Copy.

Building on AecEntity, the AecGeo object adds the location, rotation, and anchoring capabilities that are common to all AEC entities. All AEC entities are derived from AecGeo. For example, you can think of a stair object (AecStair) as an AecGeo object with additional properties and methods required to describe stairs.

AEC styles are stored in collections derived from the AecDictionary object. AecDictionary is derived from AcadObject, as the following diagram illustrates:

In addition to styles, the cleanup groups collection (AecCleanupGroups) and the structural member component shape definitions collection (AecsShapeDefinitions) are also derived from AecDictionary.

The member objects of the AecDictionary collections have their roots in AcadObject too. Their base class, AecDictRecord, is derived from AecObject, which in turn is derived from AcadObject, as shown in the following illustration:

Anchor objects also have their roots in AcadObject, as shown in the following diagram:

The base anchor object, from which all other anchor objects are derived, is AecAnchor. AecAnchor is itself derived from AecObject, which in turn is derived from AcadObject.

As you might expect, the AutoCAD Architecture preferences object (AecArchBasePreferences) has its roots in the AutoCADPreferences object:

Not all AEC objects can trace their roots to AutoCAD objects. Layout nodes, mass groups, profiles, roof edges, and most of the structural objects, among others, are derived from base classes unique to AEC applications.


Understanding the AutoCAD Architecture Object Model [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Understanding ActiveX Automation in AutoCAD Architecture
Understanding the AutoCAD Architecture Object Model

The AutoCAD Architecture object model has its roots in the AutoCAD object model (see the AutoCAD ActiveX and VBA Developer's Guide for a description of the AutoCAD object model). It includes all of the elements of the AutoCAD object model and builds on many of these elements.

The AutoCAD Architecture object model is more complex than the AutoCAD object model. The implementation of the overall object model breaks it down into several smaller sub-models, each defined in its own set of type libraries. For example, objects that are common to all Autodesk AEC applications are represented as one hierarchy of objects, while objects that are specific to AutoCAD Architecture are defined in a separate hierarchy. This modular approach enables parts of the object model to be used in multiple Autodesk AEC applications.

Topics in this section:

  • Overview of the Object Model
  • Referencing the AutoCAD Architecture Type Libraries
  • Setting Up the VBA Environment


Overview of the Object Model [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Understanding the AutoCAD Architecture Object Model
Overview of the Object Model

Although the AutoCAD Architecture object model is large and somewhat complex, the general structure of the model is fairly simple. The following diagram illustrates the basic structure of the object model:

As the diagram shows, AecArchBaseApplication is the base application object for AutoCAD Architecture. Navigating from AecArchBaseApplication, you can access all AutoCAD Architecture objects. Anchor objects are accessed through the objects they anchor.


Referencing the AutoCAD Architecture Type Libraries [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Understanding the AutoCAD Architecture Object Model
Referencing the AutoCAD Architecture Type Libraries

There are four type libraries that are typically required for AutoCAD Architecture VBA applications. The first two define the objects common to all Autodesk AEC applications:

  • AEC Base Object Library (AecXBase)

Contains objects that are common to all Autodesk AEC products.

  • AEC Base Application Library (AecXUIBase)

Contains application option settings common to all Autodesk AEC products.

The object model diagram in the AEC Base Automation Reference illustrates the contents of the AecBase type libraries.

The other required libraries define the objects specific to AutoCAD Architecture:

  • AEC Architectural Object Library (AecXArchBase)

Contains objects that are specific to Autodesk AutoCAD Architecture (for example, doors, walls, and windows).

  • AEC Architectural Application Library (AecXUIArchBase)

Contains AEC application option settings, and any application option settings specific to AutoCAD Architecture.

The object model diagram in the AEC AutoCAD Architecture Automation Reference illustrates the contents of the AecArchBase type libraries.

Two additional type libraries complete the AutoCAD Architecture object model, although you can omit them from your VBA applications if you do not need to reference the objects they define:

  • AEC Schedule Object Library (AecXSchedule)

Contains schedules and property sets. The object model diagram in the AEC Schedule Automation Reference illustrates the contents of this type library.

  • AEC Structural Object Library (AecsXBase)

Contains structural member objects used to represent beams and columns. The object model diagram in the AEC Structural Automation Reference illustrates the contents of this type library.

AutoCAD Architecture also supplies the following specialized type libraries:

  • Structural Catalog Style Generator Library (AecsXStyleGen)

Used to generate styles from XML tables.

  • Area Calculation Base Object Library (AecXAreaCalculationBase)

Contains German area calculation objects.

  • AEC D/A/CH Base Object Library (AecXArchDACHBase)

Contains dimensions and dimension style objects specific to Germany, Austria and Switzerland.

  • AEC DcContent Type Library (AecDcArch)

Used by AutoCAD Design Center.


To reference the AutoCAD Architecture type libraries [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Referencing the AutoCAD Architecture Type Libraries
Setting Up the VBA Environment

When you start the VBA editor and open a new project in AutoCAD Architecture, none of the AutoCAD Architecture type libraries are referenced by default. The only type libraries that are referenced by default are two that are required by VBA for all VBA applications, and the AutoCAD type library. Before you write any code to work with the AutoCAD Architecture object model, you should set a reference to the type libraries that define AutoCAD Architecture objects.

To reference the AutoCAD Architecture type libraries

  1. In the VBA IDE, from the Tools menu, select References.
  2. In the list of available references, scroll until you see the references whose name begins with "AEC."
  3. To work with all AutoCAD Architecture objects, select the following references:
  • AEC Architectural Application Library
  • AEC Architectural Object Library
  • AEC Base Application Library
  • AEC Base Object Library
  • AEC Schedule Object Library
  • AEC Structural Object Library
  4. Click OK.


Accessing AutoCAD Architecture Objects [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Understanding ActiveX Automation in AutoCAD Architecture
Accessing AutoCAD Architecture Objects

AecArchBaseApplication is the base application object for AutoCAD Architecture. Ultimately, all drawings and all drawing objects are accessed via AecArchBaseApplication.

Topics in this section:

  • Accessing AutoCAD Architecture Drawings
  • Accessing the Objects in a Drawing
  • Creating AutoCAD Architecture Entity Objects
  • Accessing AutoCAD Architecture Options
  • Writing Routines for Multiple AEC Applications


Accessing AutoCAD Architecture Drawings [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Accessing AutoCAD Architecture Objects
Accessing AutoCAD Architecture Drawings

You access AutoCAD Architecture drawings through AecArchBaseApplication. For example, the following code accesses the current drawing:

  Dim doc As AecArchBaseDocument
  Set doc = AecArchBaseApplication.ActiveDocument

You can access any open drawing through the application's AecArchBaseDocuments collection. You can also open a drawing by adding it to the AecArchBaseDocuments collection. Use the Documents property of AecArchBaseApplication to access the documents collection, as in the following example:

Sub List_or_Open_Drawings()

  Dim docs As AecArchBaseDocuments

  Dim doc As AecArchBaseDocument

  Dim msg As String

  Set docs = AecArchBaseApplication.Documents

  ' If there are no open documents, then create a new one

  ' using the Imperial-Intl drawing template.

  If docs.count = 0 Then

      Set doc = docs.Add("AEC Arch (Imperial - Intl).dwt")

      msg = "There were no open documents," & _

        " so I opened a new one for you." & vbCrLf

      msg = msg & " It is named " & doc.Name

      MsgBox (msg)

      Exit Sub

  Else

      msg = "There are " & docs.count & _

      " open drawings. They are:" & vbCrLf

  End If

  ' Cycle through the documents and list their names.

  For Each doc In docs

      msg = msg & "  " & doc.Name & " (" & doc.FullName & ")" & vbCrLf

  Next

  ' Display the results.

  MsgBox (msg)

End Sub

Note:  If you tried running this example and received the compile error, "User-defined type not defined," you probably forgot to reference the AEC type libraries.


Accessing the Objects in a Drawing [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Accessing AutoCAD Architecture Objects
Accessing the Objects in a Drawing

To access all of the graphical and most of the nongraphical objects in an AutoCAD Architecture drawing, begin by setting a reference to the drawing's AecArchBaseDocument object.

Access to the graphical objects, such as doors, stairs, and windows, is provided through the ModelSpace and PaperSpace collections. Other than the fact that you are starting with AecArchBaseDocument instead of ThisDrawing, you access AutoCAD Architecture entities the same as you do AutoCAD entities. The following example lists all the AEC entities in model space:

Public Sub List_Aec_Objects_In_Modelspace()
  Dim object As AcadObject
  Dim doc As AecArchBaseDocument
  Dim msg As String

  Set doc = AecArchBaseApplication.ActiveDocument

  msg = "Model space objects in the current drawing: " & vbCrLf

  For Each object In doc.ModelSpace
     ' Only list the object if it is an AEC entity
     If TypeOf object Is AecEntity Then
        msg = msg & "  " & object.ObjectName & vbCrLf
     End If
  Next

  MsgBox (msg)

End Sub

Access to drawing styles is provided through collections named for the type of style. For example, door styles are stored in the AecDoorStyles collection, and window styles are stored in the AecWindowsStyles collection. The following code loops through the AecDoorStyles collection and lists all the door styles in the current drawing:

Sub List_Doorstyles()

  Dim doc As AecArchBaseDocument

  Dim doorstyles As AecDoorStyles

  Dim doorstyle As AecDoorStyle

  Dim styleInfo As String

  ' Access the DoorStyles collection through the Document object

  Set doc = AecArchBaseApplication.ActiveDocument

  Set doorstyles = doc.doorstyles

  ' Loop through the door styles collection and list the name of each

  For Each doorstyle In doorstyles

     styleInfo = styleInfo & doorstyle.Name & vbCrLf

  Next

  MsgBox ("There are " & doorstyles.count & _

    " door styles. They are: " & vbCrLf & styleInfo)

End Sub

Anchor objects are one category of object that is not accessed through AecArchBaseDocument. An anchor object is only created in the drawing database when you attach it to an object, and anchors are always accessed through the objects they anchor. The following topic includes sample code that accesses an anchor object.


Determining if a Selected Entity is an AEC Object [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Accessing the Objects in a Drawing
Determining if a Selected Entity is an AEC Object

The greatest common denominator for all AutoCAD Architecture entities is the AecGeo object. Derived from the AecEntity object, AecGeo adds the location, rotation, and anchoring capabilities that are common to all AEC entities. You can use AecGeo to determine if a selected drawing object is an AEC entity (as opposed to an AutoCAD entity, for example). Although you can also use AecEntity to test this, using AecGeo adds the capability of checking the object's location and rotation, and determining if the object is anchored to anything. For example, the following VBA code prompts the user to select a drawing object, tests to see if the selection is an AEC entity, then determines whether there is an anchor attached to the object and what type of anchor it is:

Sub Identify_Object_Anchor()
  Dim ent As AcadEntity
  Dim geo As AecGeo
  Dim pt As Variant
  Dim anchor As AecAnchor

  ' Prompt user to select a drawing object.
  ThisDrawing.Utility.GetEntity ent, pt, "Selected anchored object"

  ' If the selected object is an AutoCAD Architecture object, get its anchor.

  If TypeOf ent Is AecGeo Then

     Set geo = ent

     ' If you try to get the object's anchor, and it is not anchored

     ' to anything, an error is raised. You need to ignore this.

     On Error Resume Next

     Set anchor = geo.GetAnchor

     On Error GoTo 0

     If anchor Is Nothing Then

        MsgBox ("The selected object is not anchored")

     Else

        MsgBox ("Entity is anchored with an " & _

               anchor.ObjectName & " object")

  End If

  Else

       MsgBox ("AEC Entity not selected")

  End If

End Sub


Creating AutoCAD Architecture Entity Objects [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Accessing AutoCAD Architecture Objects
Creating AutoCAD Architecture Entity Objects

AEC entities are implemented as AutoCAD custom objects and are created using the AddCustomObject method. For example, the following code prompts users for the start and end points of a wall, then creates a wall object:

Sub Add_Wall()

  Dim wall As AecWall

  Dim startpt As Variant

  Dim endpt As Variant

  'Get the points

  startpt = ThisDrawing.Utility.GetPoint(, "Enter wall start point:")

  endpt = ThisDrawing.Utility.GetPoint(startpt, "Enter wall end point:")

  'Create the wall, then set its start and end points

  Set wall = ThisDrawing.ModelSpace.AddCustomObject("AecWall")

  wall.StartPoint = startpt

  wall.EndPoint = endpt

End Sub

The approach is the same for all AEC entities. For properties whose value you do not specify, AutoCAD Architecture sets defaults according to the current drawing units, using values stored in the Windows registry. If you do not specify a style for the object, the Standard style is used.


Accessing AutoCAD Architecture Options [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Accessing AutoCAD Architecture Objects
Accessing AutoCAD Architecture Options

Some AutoCAD Architecture application options are stored with each drawing, and some options are stored in the Windows registry. Access to options stored in the drawing is provided through the AecArchBaseDatabasePreferences object. These include the options set by selecting Drawing Setup from the AutoCAD Architecture Desktop menu, and some of the options set from the Tools menu Options dialog box. Other options set in the Options dialog box are stored in the Windows registry, and you access them through the AecArchBasePreferences object.

Note: Currently, none of the AutoCAD Architecture-specific options set using the Options dialog box are exposed through ActiveX. For all practical purposes, AecArchBasePreferences is identical to the AcadPreferences object in the AutoCAD object model. However, since this could change in the future, it is recommended that you use AecArchBasePreferences to reference options stored in the registry.

Access AecArchBaseDatabasePreferences through the Preferences property of the Document (AecArchBaseDocument) object, as in the following example.

Dim dwgSetup As AecArchBaseDatabasePreferences
Dim doc As AecArchBaseDocument
Set doc = AecArchBaseApplication.ActiveDocument
Set dwgSetup = doc.Preferences

You can make the code more compact if you won't be needing to access anything else through the Document object:

Dim dwgSetup As AecArchBaseDatabasePreferences
Set dwgSetup = AecArchBaseApplication.ActiveDocument.Preferences

To access AecArchBasePreferences, use the Preferences property of the Application object (AecArchBaseApplication), as in the following example:

Dim regOptions As AecArchBasePreferences
Set regOptions = AecArchBaseApplication.Preferences


Writing Routines for Multiple AEC Applications [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Accessing AutoCAD Architecture Objects
Writing Routines for Multiple AEC Applications

AecBaseApplication is the base application object for all Autodesk AEC applications (currently, AutoCAD Architecture and Land Development Desktop). AutoCAD Architecture's AecArchBaseApplication object is derived from AecBaseApplication and is always available in the AutoCAD Architecture environment, so you can access all AutoCAD Architecture objects and their methods and properties from AecArchBaseApplication. However, it's possible that you might want to write a routine that could be used in any Autodesk AEC application. In that case, you need to access objects through the common application object, AecBaseApplication, instead of through AecArchBaseApplication, which only exists in AutoCAD Architecture. Such a routine would not recognize objects specific to AutoCAD Architecture (such as doors and door styles), but would work with any objects defined in the common (AecXBase and AecXUIBase) type libraries.

To use AecBaseApplication in your VBA code, you must first initialize the object. The following code demonstrates how to access a style collection through AecBaseApplication:

Sub Use_AecBaseApplication()

 ' Declare a new application object, and then initialize it

  Dim app As New AecBaseApplication

  app.Init ThisDrawing.Application

  ' Access the current document.

  Dim doc As AecBaseDocument

  Set doc = app.ActiveDocument

  ' Determine how many layer key styles are defined in this drawing

  Dim layerkeyStyles As AecLayerKeyStyles

  Set layerKeyStyles = doc.LayerKeyStyles

  MsgBox ("Number of Layer Key Styles in this drawing is: " & _

    layerkeyStyles.count)

End Sub

Some of the examples in the AEC Automation References that demonstrate the use of methods and properties common to all AEC applications use AecBaseApplication to access objects.

Similarly, to access structural member objects without assuming the presence of the AutoCAD Architecture environment, access those objects through the AecsBaseDatabase object, which is also derived from AecBaseDatabase. Remember that the AEC Structural Object Library must be referenced for the following code to work:

Sub Use_AecsBaseDatabase()

  Dim memstyles As AecsMemberStyles

  Dim Memstyle As AecsMemberStyle

  Dim aecsDB As New AecsBaseDatabase

  Dim i As Integer

  Dim MsgInfo As String

  ' Initialize the database before accessing it

  aecsDB.Init ThisDrawing.Database

  Set memstyles = aecsDB.MemberStyles

  i = 0

  For Each Memstyle In memstyles

     MsgInfo = MsgInfo & "   " & memstyles.Item(i).Name & vbCrLf

     i = i + 1

  Next

  MsgBox ("There are " & memstyles.count & _

    " Member styles. They are: " & vbCrLf & MsgInfo)

End Sub


Using Visual Basic to Program AutoCAD Architecture [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Understanding ActiveX Automation in AutoCAD Architecture
Using Visual Basic to Program AutoCAD Architecture

AutoCAD Architecture Developer Documentation is written for the VBA programming language. The programming examples assume that you are running under VBA in AutoCAD Architecture. This section introduces the subject of using Visual Basic (VB) to connect with and customize AutoCAD Architecture. For suggestions on finding more information on this subject, see Other Sources of Information.

Topics in this section:

  • Converting VBA Code to VB


Modifying the VBA code for use in Visual Basic [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Using Visual Basic to Program AutoCAD Architecture
Converting VBA Code to VB

The following points summarize what you need to do differently when coding an application to run under Visual Basic, instead of under Visual Basic for Applications:

  • You must explicitly set a reference to the AutoCAD type library, as well as the AutoCAD Architecture type libraries. (In VBA under AutoCAD Architecture, the AutoCAD type library is automatically referenced by default.)
  • You must explicitly connect to, and initialize, the AutoCAD Architecture application object.
  • You cannot use the ThisDrawing object, which only exists in VBA.

In addition, you must set the application's Visible property to TRUE if you create a new instance of AutoCAD Architecture, in order for the drawing window to display.

For an example that takes existing VBA code and converts it to run under VB, begin with the following VBA code. The routine combines two code samples shown earlier in this chapter. It first checks to see if there are any AutoCAD Architecture drawings open. If there are open drawings, it lists the drawing names; if there are no open drawings, it opens a new drawing, prompts the user to enter start and end points for a wall, and then adds the wall to the drawing:

Sub VBA_To_VB_Example()

Dim docs As AecArchBaseDocuments

Dim doc As AecArchBaseDocument

Dim msg As String

' Get the Documents collection

Set docs = AecArchBaseApplication.Documents

' If there are no open documents, then create a new one and

' draw a wall in it.

If docs.count = 0 Then        'There are no open drawings

    Set doc = docs.Add("AEC Arch (Imperial - Intl).dwt")

    msg = "No open documents, so I opened " & doc.Name & vbCrLf

    ' Now add a wall.

    Dim wall As AecWall

    Dim startpt As Variant

    Dim endpt As Variant

    'Prompt user for wall insertion points.

    startpt = ThisDrawing.Utility.GetPoint(, "Enter wall start point: ")

    endpt = ThisDrawing.Utility.GetPoint(startpt, "Enter wall end point: ")

    Set wall = ThisDrawing.ModelSpace.AddCustomObject("AecWall")

    'Set the start and end points.

    wall.StartPoint = startpt

    wall.EndPoint = endpt

    msg = msg & "I even added a wall for you!"

Else                         'There are already open drawings

    msg = "There are " & docs.count & _

      " open drawings. They are:" & vbCrLf

    ' Cycle through the documents and display their names

    For Each doc In docs

        msg = msg & "  " & doc.Name & " (" & doc.FullName & ")" & vbCrLf

    Next

End If

' Display the results.

MsgBox (msg)

End Sub

To modify the VBA code to run in Visual Basic, begin by opening VB and create a new Standard EXE project. Then add a new Module to the project and copy the VBA code into the module's code window. The instructions that follow were tested in Visual Basic 6.0; for the completed code, see Revised VB Code.

Modifying the VBA code for use in Visual Basic

  1. Set references to the AutoCAD and AutoCAD Architecture type libraries.

From the VB Project menu, select References. In the References dialog box, select at least the following items:

  • AutoCAD Type Library

  • AEC Architectural Application Library

  • AEC Architectural Object Library

  • AEC Base Application Library

  • AEC Base Object Library

(Actually, the sample code will run without the AEC Base libraries, but it's unlikely that you could omit them from a real application.)

  2. Change the procedure name to "Main."

This example does not use the Form object, which is where you usually start VB programs from. Using a procedure named "Main" is another way of starting a VB application.

  3. From the Project menu, choose Properties ("Project1 Properties"), and from the General tab, change Startup Object to Sub Main.

This tells VB to use the procedure you've named Main when you issue the Run command.

  4. Connect to AutoCAD Architecture using the following code:

   Dim acadApp As AcadApplication

   Set acadApp = GetObject(, "AutoCAD.Application.15")

   If Err.Number <> 0 Then

       MsgBox ("AutoCAD Architecture is not running. Please wait while it starts.")

       Set acadApp = CreateObject("AutoCAD.Application.15")

       If Err.Number <> 0 Then

           MsgBox ("Unable to create AutoCAD Architecture session. " & Err.Description)

         Exit Sub

       End If

   End If

AutoCAD Architecture is implemented as a COM server in an AutoCAD process, so the first thing you need to do is connect to AutoCAD. The code first tries to use the VB GetObject function to connect to a running instance of AutoCAD. If GetObject returns an error, it is assumed that there is no AutoCAD application currently running. CreateObject is then used to open a new instance of AutoCAD

Note:  CreateObject is used for simplicity in this example, but it is far from foolproof. CreateObject starts the version of AutoCAD that was most recently run on the computer. If something other than AutoCAD Architecture (for example, AutoCAD 2002 or Autodesk Mechanical Desktop) was run last, the code described in this procedure will not run correctly. In order to insure that AutoCAD Architecture is the AutoCAD application you start, you must use a method other than CreateObject to start it. One approach you might take is to obtain the file location of the application you want from the Windows registry, and then invoke the application using the Visual Basic Shell function.

Note:  When running multiple sessions of AutoCAD, the GetObject function will return the first instance of AutoCAD in the Windows Running Object Table. See the Microsoft Visual Basic documentation on the Running Object Table (ROT) and the GetObject function for more information on verifying the session returned by GetObject.

  5. Test to see if the application you've connected to is AutoCAD Architecture, and not some other application based on AutoCAD. The following code tests by checking whether the caption in the application's title bar contains the words "AutoCAD Architecture:"

   ' Check the title bar caption to see whether this is AutoCAD Architecture

   Dim window_caption As String

   Dim found_pos As Long

   window_caption = acadApp.Caption

   found_pos = InStr(1, window_caption, "AutoCAD Architecture", vbTextCompare)

   If found_pos Then               ' Yes, it's AutoCAD Architecture

   Else                            ' No, it's NOT AutoCAD Architecture!

      MsgBox ("AutoCAD application is not AutoCAD Architecture")

      Set acadApp = Nothing

      Exit Sub

   End If

If the open AutoCAD-based application is not AutoCAD Architecture, a warning is issued and the procedure is exited.

  6. Obtain and initialize the AutoCAD Architecture application object:

   Dim AutoCAD ArchitectureApp As AecArchBaseApplication

   Set AutoCAD ArchitectureApp = acadApp.GetInterfaceObject("AecX.AecArchBaseApplication")

   If Err.Number <> 0 Then

      MsgBox "An error occurred initializing AutoCAD Architecture!"

      Exit Sub

   End If

   AutoCAD ArchitectureApp.Init acadApp             ' Initialize AutoCAD Architecture

The AutoCAD Architecture application object (AecArchBaseApplication) provides access to AutoCAD Architecture drawings and drawing objects.

  7. Access the Documents collection through the AecArchBaseApplication object you obtained:

   Set docs = AutoCAD ArchitectureApp.Documents

Use this code in place of the original line of code used to access the Documents collection in VBA, which reads as follows:

   Set docs = AecArchBaseApplication.Documents

  8. Set the  application's Visible property to TRUE in order to display the AutoCAD Architecture drawing window.

   acadApp.Visible = True

If AutoCAD Architecture is already running and you connect to it with GetObject, you can assume that the application is already visible. But if you start a new instance of AutoCAD Architecture using CreateObject, you need to set Visible to True, otherwise AutoCAD Architecture will be invisible; it will not even appear on the Windows taskbar.

  9. Replace all references to ThisDrawing with a variable referencing the active document.

The example code only refers to ThisDrawing during the procedure to add a wall to a new drawing. In this procedure, it already sets a variable to reference the active document:

   Set doc = docs.Add("AEC Arch (Imperial - Intl).dwt")

Replace ThisDrawing with doc in the following lines of code:

   startpt = ThisDrawing.Utility.GetPoint(, "Enter wall start point: ")

   endpt = ThisDrawing.Utility.GetPoint(startpt, "Enter wall end point: ")

   Set wall = ThisDrawing.ModelSpace.AddCustomObject("AecWall")


Revised VB Code [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Converting VBA Code to VB
Revised VB Code

The full version of the revised code contains some additional processing that is not described in Modifying the VBA code for use in Visual Basic. The additional code adds logic to open a new drawing and draw a wall if a new instance of AutoCAD Architecture was just created using CreateObject; the original code only adds a wall when there are no drawings currently open. None of this code is required to make the code run in VB. However, the full version also contains additional error-trapping and error-checking that is required for the program to run successfully.

Public Sub Main()

  ' From VB, list all open AutoCAD Architecture drawings, or open a new drawing if
  ' none are currently open.

  Dim acadApp As AcadApplication
  Dim AutoCAD ArchitectureApp As AecArchBaseApplication
  Dim docs As AecArchBaseDocuments
  Dim doc As AecArchBaseDocument

  Dim msg As String
  Dim new_session As Boolean
  Dim window_caption As String
  Dim found_pos As Long

  new_session = False

  'Allow program to continue if GetObject fails
  On Error Resume Next

  ' If a version of AutoCAD is already running, connect to it,
  ' otherwise create a new session (and hope it's AutoCAD Architecture!)

  Set acadApp = GetObject(, "AutoCAD.Application.15")
  If Err.Number <> 0 Then
      Err.Clear               ' Reset err.number
      MsgBox ("AutoCAD Architecture is not running. Please wait while I try to start it")
      Set acadApp = CreateObject("AutoCAD.Application.15")
      If Err.Number <> 0 Then
          MsgBox ("Unable to create AutoCAD Architecture session. " & Err.Description)
          Exit Sub
      Else
          new_session = True   ' Indicates a new session was started
      End If
  End If

  ' Check the window caption to see whether this is AutoCAD Architecture
  window_caption = acadApp.Caption
  found_pos = InStr(1, window_caption, "AutoCAD Architecture", vbTextCompare)
  If found_pos Then            'It IS AutoCAD Architecture!
  Else                         'It's NOT AutoCAD Architecture!
      MsgBox ("AutoCAD application is not AutoCAD Architecture")
      Set acadApp = Nothing
      Exit Sub
  End If

  ' Get the AutoCAD Architecture application object, and then initialize it
  Set AutoCAD ArchitectureApp = acadApp.GetInterfaceObject("AecX.AecArchBaseApplication")
  If Err.Number <> 0 Then
      MsgBox "An error occurred initializing AutoCAD Architecture!"
      Exit Sub
  End If

  On Error GoTo 0         ' enable error handling

  ' Initialize the AutoCAD Architecture application
  AutoCAD ArchitectureApp.Init acadApp

  Set docs = AutoCAD ArchitectureApp.Documents

  ' If there are no open documents, or if this is a new
  ' session of AutoCAD Architecture, then open a new drawing.
  If (docs.Count = 0 Or new_session) Then
      acadApp.Visible = True       ' Show the drawing window

      Set doc = docs.Add("AEC Arch (Imperial - Intl).dwt")
      msg = "No open documents, so I opened " & doc.Name & vbCrLf
      ' Now add a wall to it
      Dim wall As AecWall
      Dim startpt As Variant
      Dim endpt As Variant

     'Get the insertion points from the user

     ' Note: replaces "ThisDrawing.Utility.GetPoint"

     startpt = doc.Utility.GetPoint(, "Enter wall start point: ")

      endpt = doc.Utility.GetPoint(startpt, "Enter wall end point: ")

     'Create a new wall in the drawing

     ' Note: replaces "ThisDrawing.Modelspace..."

     Set wall = doc.ModelSpace.AddCustomObject("AecWall")

     'Set the start and end point

     wall.StartPoint = startpt

     wall.EndPoint = endpt

     msg = msg & " I even added a wall for you!"

  Else                            ' There are drawings already open

      msg = "There are " & docs.Count & _

        " open drawings. They are:" & vbCrLf

      ' Cycle through the documents and display their names

      For Each doc In docs

         msg = msg & "  " & doc.Name & " (" & doc.FullName & ")" & vbCrLf

      Next

  End If

  ' Display results

  MsgBox (msg)

End Sub


Other Sources of Information [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Understanding ActiveX Automation in AutoCAD Architecture
Other Sources of Information

In addition to the Help files that come with AutoCAD Architecture, Autodesk also delivers information about ActiveX Automation programming on the World Wide Web, where it provides web sites, discussion groups, and courseware. You can also find third-party web sites and books devoted to the topic of VB and VBA programming for AutoCAD.


AutoCAD Help Files [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Other Sources of Information
AutoCAD Help Files

The set of Help files that comes with AutoCAD Architecture includes documentation on using ActiveX Automation in AutoCAD. Anyone who has never programmed AutoCAD or an AutoCAD-based application using VBA should read at least chapters 1 through 4, and chapter 11, in the following document:

  • AutoCAD ActiveX and VBA Developer's Guide

You can also refer to the AutoCAD ActiveX and VBA Developer's Guide for information about, and an example of, using VBA to interact with other Windows applications that support ActiveX Automation.

The following Help files also contain information about using Automation with AutoCAD:

  • AutoCAD ActiveX and VBA Reference

A reference for all objects, methods, properties, and events in the AutoCAD object model.

  • Visual LISP Developer's Guide

This book contains information about how to use ActiveX Automation with Visual LISP (not encouraged!).

You can access all of these books through the AutoCAD Architecture Help menu, or by opening the file named acad_dev.chm in the AutoCAD Architecture Help directory.


Internet Resources [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Other Sources of Information
Internet Resources

Autodesk provides web sites and discussion groups that provide information on programming in VBA using ActiveX Automation. In addition, you can find third-party web sites by entering "AutoCAD VBA" or "AutoCAD VB" in any search tool.

Note: Web sites change frequently, so some of the URLs and links noted here may be obsolete. In that event, try using one of the web search sites to find the information.


Autodesk Developer Center [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Internet Resources
Autodesk Developer Center

The Autodesk Developer Center is a web site with resources for people using any of the various APIs to customize Autodesk products. The Developer Center includes a knowledge base containing API information, discussion groups, and tips and tools for developers. Membership in the Autodesk Developer Network (ADN) is required to use this site. Visit http://www.autodesk.com/adn for information about ADN and the Autodesk Developer Center.


Discussion Groups [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Internet Resources
Discussion Groups

A variety of discussion groups for Autodesk products, including API discussion groups, can be accessed on the web at http://discussion.autodesk.com. Select Autodesk Architectual Desktop from the product list on that web page. The majority of postings on the autodesk.aec.arch-desktop.customization discussion group are related to ActiveX Automation in AutoCAD Architecture.


Courseware [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Other Sources of Information
Courseware

Autodesk offers the following courseware for learning about ActiveX Automation in AutoCAD Architecture:

  • AutoCAD Architecture COM API eLearning Labs

This course consists of nine modules with step-by-step instructions on using ActiveX Automation with AutoCAD Architecture. The courseware was developed by the Developer Technical Services group at Autodesk, and is presented using Autodesk Learning Assistance (ALA). There is a small charge for each module, and modules can be purchased separately. Go to the Autodesk e-Learning Site for more information about this course.


Third-Party Books [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Other Sources of Information
Third-Party Books

There are no third-party books devoted to ActiveX Automation and AutoCAD Architecture, but there are a number of books about using Automation with AutoCAD (and more are published every year). The two most widely read are the following:

  • AutoCAD 2000 VBA Programmer's Reference, by Joe Sutphin. Wrox Press Inc, ISBN 1861002564.
  • Using Visual Basic with AutoCAD 2000, by Andrew Roe. Delmar Publishers, ISBN 0766820912.

Search the World Wide Web for other books on AutoCAD VBA.


Understanding ActiveX Automation in AutoCAD Architecture [Understanding ActiveX Automation in AutoCAD Architecture: ADD]

Understanding ActiveX Automation in AutoCAD Architecture

This document provides an overview on how to use ActiveX Automation in Visual Basic for Applications (VBA) to write custom applications for AutoCAD Architecture. It describes the AutoCAD Architecture object model and introduces concepts you need to understand in order to query, modify, or add AutoCAD Architecture objects using VBA. It also contains information to help you get started coding Visual Basic (VB) applications to interact with AutoCAD Architecture.

This document assumes that you already know how to use VBA, are familiar with ActiveX Automation concepts, and understand the AutoCAD object model. For information on where to learn about these topics, see Other Sources of Information.

The terms "ActiveX Automation" and "Automation" are used interchangeably in this document.

Topics in this Document:

  • AutoCAD Architecture Automation Concepts
  • Understanding the AutoCAD Architecture Object Model
  • Accessing AutoCAD Architecture Objects
  • Using Visual Basic to Program AutoCAD Architecture
  • Other Sources of Information