Welcome to CADVANTAGE’s No Nonsense, Quick Start, Step by Step tutorial for Visual Basic for Applications ( VBA ) in AutoCAD.

This tutorial is part of the CAD-CAM @ Home scheme currently available in the city of Aurangabad in India.

The tutorial is a Goodwill Gesture for all AutoCAD users who take pains to go out of way and learn new things.

To learn more about computers in general and CAD CAM CAE in particular as detailed below, you may enroll for the
CAD++ [ CAD @ Home ] program at :

CADVANTAGE ( where AutoCAD is taught FREE )
C 2 - 6, N 7, CIDCO,
Opp. Onkar Gas, Behind Swamikripa STD,
Aurangabad 431003
India
Ph : 0240 - 480767
Email : CadGuruCool@yahoo.com
Webpage : www.geocities.com/CadGuruCool



VBA Tutorial


Step 1


It is assumed that you are familiar with the basics of some version higher of AutoCAD 2000.

The code that we will undertake is tested in AutoCAD 2002 only, although it should work with versions down to R14.

The tutorial assumes no prior experience with VB or VBA but proper knowledge of AutoLISP is a must.

VBA is a language parallel to AutoLISP

To state this in techical terms -

All AutoLISP functions are mapped to thier VBA equvalents with similar names but with VB style syntax.

It is strongly recommended that you complete the AutoLISP tutorial first.

To avail the AutoLISP tutorial, go to :

www.geocities.com/CadGuruCool

and click Tutorials 4U


Step 2


We want to write a program in VBA that will draw the milled slot as shown in figure.

The program will ask for :

1) CT = Cutter Traverse,
2) CD = Cutter Diameter, and
3) P1 = Cutter Start Point.

Unlike in vanilla AutoLISP, we want to input the parameters via a dialog box as shown in the figure on the right.
dialog box

Step 3


To begin with, Kick AutoCAD .

He ! He ! you already know what is kicking, still ...

Right Click on the AutoCAD desktop icon and select Properties from the menu that appears as shown in figure on the right (top).

In the properties dialog box, make sure that the Target edit-box looks similar to the following :

"C:\Program Files\AutoCAD 2002\acad.exe" /nologo /t acad.dwt
as shown in the figure besides (bottom) .

The /nologo will save you wondering what will happen in 2003, while /t acad.dwt gets rid of the Startup dialog, although this has nothing to do with VBA.

Once in AutoCAD, press Alt F11 or Tools > Macro > Visual Basic Editor.



Step 4


In VBA, you can create dialog boxes and the buttons on it by drawing them as you would draw rectangles in AutoCAD.

In the VBA editor, click Insert > User Form .

Click on the dialog box (form) The toolbox for controls will appear as shown in figure.

Consider this as a toolbar for drawing various buttons on the form like the command button, edit box, text, etc.
Step 5

Note the white grips labeled 1, 2 & 3 and circled green as shown in figure.

Press and drag on these grips to resize the dialog box (ie. the form).

For this tutorial, the default size for the form is okay.


Step 6


To create the first button, click the CommandButton tool from the toolbox.

Press and drag at two points as shown in the figure to create the command button.

Initially the button is labeled CommandButton 1.

You can change the properties of the button just as you would change object properties in AutoCAD 2000+.

Click anywhere on the dialog box (ie. the form) and change the Caption property to Milled Slot as shown in the figure on the right ( bottom ).

Similarly, click the button you created just now and change its Caption property to Draw. To create a keyboard accelerator for the draw button, change the Accelerator property to D.

Also, change the Width, Height, Top, Left and the Font property to your liking.

Create another button and call it Cancel as shown in the figure on the right ( top ).



Step 7


If the toolbox tends to hide while you are working, just click anywhere on the dialog box (ie the form) and the toolbox will come back again.

Use the Label tool to create the two text lines -
Cutter Traverse ( CT ) and
Cutter Diameter ( CD ).

Use the TextBox tool to create the two Text boxes.

Use the Image tool to create the image at the top.

To put the picture in the image box, click on Picture in the properties area as shown in the figure.

Click the button with three dots as shown in figure circled red and select a file with extension bmp, jpg, gif, ico or cur.

Step 8


Now that the dialog box layout is complete, we will put some code to kick it.

Right click on the Cancel button.

A menu will appear.

Select View Code from the menu.

The code window will apepar and two lines will be already present

Type the word End between the two lines as shown in the figure.

The color will turn blue after you move the up or down arrow key.

The single word End is the command to cancel the program and close the dialog box.

Right click in the code window and select Hide from the menu. You will come back to the dialog box (ie. the form).

Step 9


At this stage, the dialog box is ready to be tested in AutoCAD.

Click the Run Sub/UserForm tool on the Standard toolbar.

AutoCAD will be activated and the dialog box will appear.

Right now you cannot do much with the dialog box, so click Cancel and return to VBA.
Step 10

As shown in the figure, right click on UserForm1 in the Project window.

A menu will appear.

Select View Code from the menu as shown in the figure.

Observe the two lists at the top of the Code Window.

The list on the left is the Object list. Pull down the list.

You will see the names of all the objects like buttons, edit-boxes, label, etc listed over here and also the form name will appear as UserForm1.

The list on the right is the Procedure list.

This list will show what all things you can do with a particular object.

For example, select CommandButton1 from the left list. Next, pull down the right list. You will see words like Click, DoubleClick, MouseDown, MouseUp, etc.

These words are possible things that can happen to the CommandButton1 and we can write code to execute when any of these things (events) from the right list occur.

We have already defined one such action in Step 8 ie. telling the CommandButton1 (the Cancel button) what to do when the user clicks it.


Step 11

Select TextBox1 from the left list and the item Change from the right list.

Two lines of code will appear for free and the cursor is positioned between the two lines.

This is the text box that will accept the Cutter Traverse . For this, we need a variable CT to store the value from the edit box.

Also, we want to store the Cutter Diameter in variable CD.

Press Ctrl Home to go to the top of the code window.

We will first declare that we need to use two variables in our program.

Add the following two lines to the beginning of the code :

Public CT As Double
Public CD As Double

Step 12

Using the arrow keys, come back to the TextBox1 code, where the lines look like :

Private Sub TextBox1_Change()

End Sub

Insert the middle line so that the complete code looks like :

Private Sub TextBox1_Change()
CT = Val(TextBox1.Text)
End Sub

Here, the word TextBox1.Text means the text entered by the user in the text box when using the program.

Thus, the value of the text is stored in the variable CT

Similarly, repeat the procedure for the other text box and the code for it will look like :

Private Sub TextBox2_Change()
CD = Val(TextBox2.Text)
End Sub

Step 13

Next, click UserForm1 from the left list and the item Initialize from the right list.

We want to have some default values in the edit-boxes when the program is used for the first time in this AutoCAD session.

Also, when the program is run the next time, last used values should appear in the edit boxes.

For both features, do the coding as shown in figure to the right.
Step 14

Now comes the meaty part of the code.

Select CommandButton1 from the left list and select Click from the right list.

Look at the initial part of the code that you have to key in as shown in the figure on the right.

We declare all variable that are required when we click the Draw button.

The dissection is as follows .

The word Dim is used to declare a variable.

The various types of variables that we have used are :

1) Variant - This can store almost any type of data,

2) Double - This can store real or floating numbers,

The declaration Pt1 ( 0 To 2 ) as Double means an array of Doubles consisting of three numbers for the x-, y- and z- coordinates of a point.

Don't confuse the word array with the AutoCAD array command. This is a completely different animal.

3) Object - We declare line1 and line2 as Object, while arc1 and arc2 as AcadArc

4) Byte - This is a small capacity integer data holder used in this program for the i counter




Step 16

Using the PolarPoint function, which is an equvalent of the Polar function of AutoLISP, a new point can be created relative to a known point.
Step 17

The rest of the code is as below :

A little consideration ( courtesy - R. S. Khurmi) will show that VBA code closely resembles AutoLISP.

It is strongly recommended (courtesy - InstallShield) that you complete the AutoLISP tutorial first. To avail the AutoLISP tutorial, go to : wwww.geocities.com/CadGuruCool and click Tutorials 4U

UserForm1.Hide
p1 = ThisDrawing.Utility.GetPoint(, "Click:")
For i = 0 To 2: pt1(i) = p1(i): Next i
p2 = ThisDrawing.Utility.PolarPoint(p1, (90 * 0.01745), (CD / 2))
For i = 0 To 2: pt2(i) = p2(i): Next i
p3 = ThisDrawing.Utility.PolarPoint(p1, (270 * 0.01745), (CD / 2))
For i = 0 To 2: pt3(i) = p3(i): Next i
p4 = ThisDrawing.Utility.PolarPoint(p2, 0, CT)
For i = 0 To 2: pt4(i) = p4(i): Next i
p5 = ThisDrawing.Utility.PolarPoint(p3, 0, CT)
For i = 0 To 2: pt5(i) = p5(i): Next i
Set line1 = ThisDrawing.ModelSpace.AddLine(pt2, pt4)
Set arc1 = ThisDrawing.ModelSpace.AddArc(pt3, (CD / 2), 90 , 270))
Set line2 = ThisDrawing.ModelSpace.AddLine(pt3, pt5)
Unload UserForm1
End Sub

Step 18

Want to dig deeper
into VBA ?

Call CADVANTAGE Now !!! Ph : 0240 - 480767 in Aurangabad.
Dig deep till you reach the other side of the globe

- Only at CADVANTAGE



FREE - Basic Course on AutoCAD and AutoLISP

FREE - Tons of free VBA code on CD

FREE - 400 pages spiral bound indeth study material.