AutoCAD VBA : Drawing Automation

Submitted by CAAadmin on Tue, 07/19/2016 - 10:52

 

cad vba

 

 

 

 

 

 

 

 

 

AutoCAD Drawing automation techniques using VBA (Visual Basic for Applications)

AutoCAD VBA Introduction

VBA Stands for Visual Basic for Applications, where program snippets used to control the applications instead of commands/controls. You can use VBA Editor to add new codes, modify existing codes. All codes are written in familiar programming language VB and since this website deals with VBA for AutoCAD, Excel and Solidworks, it is recommended that you refer VB code tutorials (there are many, available online) before using this website.

VBA Editor comes with AutoCAD 2007 version as preinstalled, if you are using later versions; you should install VBA editor before beginning the projects.

To open VBA Editor, go to Tools -> Macro -> Visual Basic Editor as shown below

autocad vba

 

 

 

 

 

 

 

 

Or simply press ALT + F11

The newly opened window should be something like this,

autocad vba

 

 

 

 

 

 

To begin with your first vba macro, go to Insert -> Module

vba programmin autocad

 

 

 

 

 

 

Then type Sub module1 () and press Enter (module1 is the name of your new module, it can be anything like drawline, yourname etc without any spaces)

As you can see that “End Sub” has been appeared just below Sub module1 (), thus defining the boundary of your AutoCAD macro.

Now, you can start typing your codes between module name and end sub, ie, Sub module1 () and End Sub, as shown below

Sub module1()

'your codes goes here

'your codes goes here

'your codes goes here

End Sub

To discuss about “How to use a VBA module inside an AutoCAD drawing”, I will be using a simple line drawing program to draw a line from start point to end point.

The example program is as shown below,

Example 1 : Draw a line using AutoCAD VBA

 

'Set Start point and End Point Variables

Dim Startpoint(0 To 2) As Double

Dim Endpoint(0 To 2) As Double

 'Start Coordinates XYZ values

Startpoint(0) = 100: Startpoint(1) = 100: Startpoint(2) = 0

 'End Coordinates XYZ values

Endpoint(0) = 200: Endpoint(1) = 200: Endpoint(2) = 0

 'Line object in to drawing space

ThisDrawing.ModelSpace.AddLine Startpoint(), Endpoint()

 

This programs uses 2 variables called Startpoint and Endoint to store the values of X,Y,Z coordinates of start point and end point of the line to be drawn, X1, Y1, and Z1 belongs to Startpoint where as X2,Y2 and Z2 belongs to Endpoint.

In this example, X1 = 100; Y1 = 100 and Z1 = 0 (z value is 0 for 2D drawings) and X2 = 200; Y2 = 100 and Z2 = 0.

Just copy and paste the above program snippet in between, Sub module1 () and End Sub

And final code will be something like below mentioned

 

Sub module1()

On Error Resume Next         ‘ To bypass run time error occurred (if any)

'Set Start point and End Point Variables

Dim Startpoint(0 To 2) As Double

Dim Endpoint(0 To 2) As Double

 'Start Coordinates XYZ values

Startpoint(0) = 100: Startpoint(1) = 100: Startpoint(2) = 0

 'End Coordinates XYZ values

Endpoint(0) = 200: Endpoint(1) = 200: Endpoint(2) = 0

 'Line object in to drawing space

ThisDrawing.ModelSpace.AddLine Startpoint(), Endpoint()

End Sub

(See below screenshot)

vba tutorial

 

 

 

 

 

 

 

 

Now you can save your macro using Save Button or by Ctrl + S,

You have created your first macro to create a line with coordinates (100, 100, 0 and 200, 200, 0).

To see it in action, you have to load it to the drawing from where you have saved your module

Just go to Tools-> Macro -> Macros

And click Run Button, and the output should be something like below

cad

 

 

 

 

 

 

 

 

 

Now you have created a line object.

 

Loading the Macro

Once you close the drawing after saving all objects, the macro wouldn’t be loaded automatically while you reopen the drawing. In order to do that, just go to Tools -> Macro-> Load Project and navigate the file browser to the saved module (in this case, module1.dvb)

 

Tags