How to Automate AutoCAD Civil 3D using Visual Basic

Khan Mohammad Hekmat
2 min readSep 23, 2023

Automating AutoCAD Civil 3D using Visual Basic for Applications (VBA) involves writing VBA code to interact with the Civil 3D application through its object model. The object model provides access to various components and functionality within Civil 3D. Here are the general steps to automate Civil 3D using VBA:

  1. Enable VBA in AutoCAD Civil 3D:
  • Open AutoCAD Civil 3D.
  • Go to the “Tools” menu.
  • Select “Macro” and then “Visual Basic Editor” to open the VBA editor.
  • If VBA is not enabled, you may need to install it separately, as it might not be included in all versions of AutoCAD.
  1. Create a New VBA Project:
  • In the VBA editor, go to “Insert” and choose “Module” to create a new module.
  • This module will contain your VBA code.
  1. Write VBA Code:
  • In the module, you can start writing your VBA code. You’ll use the Civil 3D object model to access and manipulate Civil 3D elements and perform tasks.
  • Here’s a simple example of VBA code to automate a task like creating a new Civil 3D alignment:

vbaCopy code

Sub CreateAlignment() Dim acadApp As Object Set acadApp = GetObject(, “AutoCAD.Application”) If acadApp Is Nothing Then MsgBox “AutoCAD is not running.” Exit Sub End If Dim civil3dApp As Object Set civil3dApp = acadApp.GetInterfaceObject(“AeccXUiLand.AeccApplication.10.0”) If civil3dApp Is Nothing Then MsgBox “Civil 3D is not running.” Exit Sub End If ‘ Create a new alignment Dim alignment As Object Set alignment = civil3dApp.ActiveDocument.ParcelSet.AddAlignment(“MyAlignment”) ‘ Additional code to configure the alignment ‘ Save and close the document civil3dApp.ActiveDocument.Save civil3dApp.ActiveDocument.Close End Sub

This code demonstrates how to create a new alignment in Civil 3D using VBA. You can extend it to perform more complex tasks.

  1. Debug and Test:
  • Debug your code as needed to ensure it works correctly.
  • You can use the VBA editor’s debugging tools to step through your code, set breakpoints, and inspect variables.
  1. Run the VBA Macro:
  • To run your VBA macro, you can either use the “Run” button in the VBA editor or create a button or menu item within AutoCAD Civil 3D that triggers your macro.
  1. Save and Share:
  • Save your VBA project and code for future use.
  • You can also share your VBA code with others who use Civil 3D.

Please note that VBA may not be available in all versions of Civil 3D, and its use has been on the decline in recent years in favor of .NET-based customization. If your version of Civil 3D does not support VBA or if you prefer a more modern approach to customization, you might consider using .NET languages like C# or VB.NET to develop Civil 3D plugins and add-ins.

--

--

Khan Mohammad Hekmat

I am a Civil Engineer and AutoCAD specialist with over 20 years of experience. I live in Washington DC metropolitan area and work full time as Civil Engineer.