Getting Started in VBA

#1
If you have the professional version and want to get started in VBA, simply type vba at the command line and the VBA IDE (Integrated Development Environment) will open.

You will see three windows :

Top Left Window – Project Window
This is just like Windows Explorer and will show each drawing name that is open and a special project called CommomProjects that will be opened every time by default.

Bottom Left Window – Properties Window
This is used to set properties for events and forms (Dialog Boxes to CAD users).

Right Window (may be blank) – Code Window
This is where we type in the code that is compiled then executed when we Run our program.

To get started select Insert -> Module from the pull down menu and the Code Window will open with the words “Option Explicit”.
Type in the following Code

Sub Test() - then press Enter on the keyboard.

This will create a subroutine named Test. Note how the IDE places End Sub automatically.

Between the Sub Test() and the End Sub type in the following code:
MsgBox “This is my first Test in VBA”

To run this Subroutine, place your flashing cursor in the Code then Run -> RunSub/User Form from the pull down menu.

The drawing will display and a message will appear.

If the message does not show then check your code, it should look like the following :

Sub Test()
MsgBox “This is my first Test in VBA”
End Sub

You will notice some other information on your screen when you type in MsgBox. It is the automatic assistance to help you complete all the information for the MsgBox object.

Next we will discuss how to add user interactions – Stay tuned!

#2
Thanks for the tutorial. I've been wanting to get more into VBA but I found the hel file a bit confusing for someone first getting started. Thanks again

#3
Jason,

Follow me and I will make you an expert in VBA for IntelliCAD in no time!

My next post will start tomorrow morning Australian Time (that is) on how to interact with the user in a simple manner to obtain information and then use it to create an entity.

I will also show this in Lisp so you will be able to compare.


------------------
Regards
John Finlay

#4
First lets understand a few things like Dim and Set.

Dim is used to dimension a variable or in real terms define the type of variable such as a String, Double, Object or Integer etc.

Set is used to Set a pointer the an object. Once the pointer is set to an object we can manipulate the object properties such as its layer , color or highlight it.

When you start a module VBA inserts Option Explicit at the start of the module and this just means that all variables and pointers to objects must be Explicitly Defined using the Dim statement. If you don’t VBA will call an error to warn you prior to running your code.

We will now attempt to draw a circle by getting the user to select a point and then have the user input the radius – I know its nothing great but it’s a start.

As before type in VBA and insert a module.

Type in the following code:

Sub CircleTest()
' define all variables to their respective types
Dim CenPnt As Point
Dim RadSize As Double
Dim objCircle As IntelliCAD.Circle

' if intellicad encounters an error just keep going to the next line of code
On Error Resume Next

' set a pointer to the CenPnt point object by using the GetPoint method in VBA
Set CenPnt = IntelliCAD.ActiveDocument.Utility.GetPoint(, "Locate Circle Center Point")

' using an input box get the circle radius from the user
RadSize = InputBox("Input Size of Circle", "My First Input")

' set a pointer to the objCircle circle object by using the AddCircle method in VBA
Set objCircle = IntelliCAD.ActiveDocument.ModelSpace.AddCircle(CenPnt, RadSize)
' now that an object pointer is set we can update the object and display it on the screen
objCircle.Update

' just in case our circle is very small or too large we can zoom the extents
‘ of the drawing to display our circle
IntelliCAD.ActiveDocument.ActiveViewport.ZoomExtents


End Sub


Locate your cursor in the Sub and select F5 to run the Subroutine. You should be asked to Locate Circle Center Point – select point on screen – then - an input box will ask you to Input Size of Circle – then – you will be returned to VBA IDE. Select ICAD and you should see the circle.

Look at the code after GetPoint and you will see a comma after the first parenthesis, this ignores the first point that is used for rubber banding. Just like the circle command in IntelliCAD allows you to select a center point and then point to the radius with a rubber band display to assist the user.

What we did was not earth shattering however, we can now get a point from the user and get information using an input box then create an entity.

Here is the same program in Lisp for you to compare

(defun c:CircleTest ( / SPnt CircleRad )

(Setvar "CMDECHO" "Off" ); stops the commands echo to command line

; INPUT INFORMATION
(setq SPnt (getpoint "\nSelect Circle Center Point")); Start Point
(setq CircleRad (getdist "\nInput the Radius of Circle")) ; Radius of circle

; DRAW THE CIRCLE
(command "Circle" SPnt CircleRad )

; ZOOM TO EXTENTS
(command "Zoom" "E" )

(princ ); close the function quietly

); end defun circletest

Next we will take a look at the difference between a subroutine and a function in VBA and how to create some tools that will keep our general code in modules to a minimum – Stay Tuned!


------------------
Regards
John Finlay

#5
The difference between a subroutine and a function is easy to understand :

Subroutines are used to code your program and do not return anything.

Functions on the other hand are used to return information.

We have used a Sub to code our program and now I will show you how to use a Function:

As before open ICAD type VBA and enter to open the IDE, This time in the Project window (upper left) highlight CommonProjects and select Insert -> Module from Pull Down menu. We will be using the CommonProjects.vbi file to store our tools because it is available every time we open ICAD and we will want to use our programming tools over and over.

You will see in the Properties window (lower left) the Name of the Module eg. Module1 and we will change this name to Tools by editing the Module1 name . This will be reflected in the Project Window.

One of the problems with VBA is that pi is not a stored variable like it is in Lisp so we will create a Function that will return pi when we need it because that’s what functions do – they return information to our subroutine code.

Type in the following code

Public Function pi() As Double
pi = Atn(1) * 4
End Function

Note we used the word Public prior to Function because we want to use pi any time it is needed. We can make our Functions Private so that they are only used within the Module where they reside. This is called scope and pertains to variables as well.

We also set up the Function to return the value of pi as a Double and as with all Functions we must make the function name equal to the returning calculation. So if we create a Function named Fred we must use code like Fred = ????? and in our case we used pi = Atn(1) * 4 which is the Arc Tan of 1 multiplied by 4. Arc Tan is a built-in VBA trigonometry method.

Save our work by highlighting CommonProjects in the Project window and selecting save.

Now we need to know how to use our pi Function.

Select a drawing name in the Project window and insert a module in the drawing.

Type in the following code:

Sub TestPi()
MsgBox "pi = " & Tools.pi
End Sub

When we place our cursor in the Sub and choose F5 a message box appears in the drawing telling us what pi equals.

Now every time we want to use pi in our equations we simply use Tools.pi

We now know the difference between a subroutine and a function, how to scope our function (Public or Private) and how to enforce the returning variable type (this case pi is a Double).

Next we will create a special Layer Tool that will loop through the Layers and if it is not found, it will create it, set the color and linetype. This will introduce us to VBA looping and some other VBA built-in methods. I will show you how to use this in our CircleTest subroutine to set the layer, color and linetype of the new circle. – Hope you are keeping up with me and I am not going too fast – got a query – let me know.


------------------
Regards
John Finlay

#6
Once again open a the VBA IDE, select the + sign adjacent to Modules to show our Tools module and double click on Tools to open the code window. You should see our pi function we created.

Type in or copy and paste the code below:

Public Sub MakeLayer(MLayer As String, MColor As Integer, MLineType As String)
' This routine will create a layer when it is not in drawing with color and linetype

On Error Resume Next

' Dimension the variables
Dim LayerFlag As Integer
Dim objMLayer As IntelliCAD.Layer

'Make sure the LayerFlag is set to 0
LayerFlag = 0

' Loop through each Layer
For Each objMLayer In IntelliCAD.ActiveDocument.Layers
'Compare the name to our MLayer string. if it exists set the LayerFlag to 1
If StrComp(objMLayer.Name, MLayer, vbTextCompare) = 0 Then LayerFlag = 1
Next objMLayer

' If the LayerFlag is 0 then no layer name exists for our MLayer string and we must make it.
If LayerFlag = 0 Then
Set objMLayer = IntelliCAD.ActiveDocument.Layers.Add(MLayer)
objMLayer.Color = MColour
objMLayer.Linetype = MLineType
End If

End Sub

Once again you will note we have used “Public” – only this time it is a Sub because we don’t need anything to be returned. We are now using variables between the parenthesis of our Sub and these are called arguments – when we use this Sub these arguments will be displayed to remind us of the type of variables required.

Error, Dimensioning and setting the LayerFlag to 0 shouldn’t need further explanation.

We use For Each Next to loop through each layer and an If Then statement to compare each layer.
It is the If Then that needs further explanation – StrComp stands for String Compare and is a built-in VBA function that compares two strings and returns an Integer based on what it finds. In our case if it returns 0 there has been a match and the layer exists Then we set our LayerFlag = 1.

After comparing all the Layer Names with the supplied string Mlayer and no matches exist then LayerFlag = 0 and we must create the layer name. This is where the If LayerFlag = 0 Then comes into play and we use ICAD’s Layers.Add to add the layer name Mlayer and once the layer pointer is set to objMLayer, we set the other properties such as color and linetype.

Notice the difference between the way I have used If Then, The first is on one line and the other is multiple lines that has an End If on a separate line. The End If must be used when multiple actions occur from the If Then statement.

Now to use our Layer Tool we can insert code into our CircleTest subroutine between setting our objCircle and then updating it – Full code Below:

Sub CircleTest()
' define all variables to their respective types
Dim CenPnt As Point
Dim RadSize As Double
Dim objCircle As IntelliCAD.Circle

' if intellicad encounters an error just keep going to the next line of code
On Error Resume Next

' set a pointer to the CenPnt point object by using the GetPoint method in VBA
Set CenPnt = IntelliCAD.ActiveDocument.Utility.GetPoint(, "Locate Circle Centre Point")

' using an input box get the circle radius from the user
RadSize = InputBox("Input Size of Circle", "My First Input")

' set a pointer to the objCircle circle object by using the AddCircle method in VBA
Set objCircle = IntelliCAD.ActiveDocument.ModelSpace.AddCircle(CenPnt, RadSize)

‘********* Start New Code **********************

' make a layer from our Tools.MakeLayer
Tools.MakeLayer "Circles", 2, "Continuous"

' Change the property of the circle object to the new layer
objCircle.Layer = "Circles"

‘********* End New Code **********************

' now that an object pointer is set we can update the object and display it on the screen
objCircle.Update

' just in case our circle is very small we can zoom the the extents of the drawing to display our circle
IntelliCAD.ActiveDocument.ActiveViewport.ZoomExtents

End Sub

Now we have Layers under our complete control I will show you how to create a Function Tool that draws rectangles with a small amount of Code – great if you want to draw a window with mullions or a door with multiple glass panes or even a door frame – well after all drawing lines is what its all about and it helps if what we program is useful.

As we build up our programming tools that we know work well, we can then put applications together in a very short time (RDS). This collating of tools is called encapsulation where the bulk of our working code is already written as tools in Functions and Subroutines.

I will start a new topic for the rectangle tool called Getting Starting in VBA 2

------------------
Regards
John Finlay

[This message has been edited by John Finlay (edited 08-01-2001).]

[This message has been edited by John Finlay (edited 08-02-2001).]

#7
I have been following your tutorials and they are very helpful. I have always wanted to learn VBA but have been to afraid to try it. I do have a couple questions, hope they don't sound to stupid.

I noticed that most things begin with capitals, is this neccesary? I know in lisp it doesn't matter.

obj is in small letters, is that because it is just part of the assigned name objCircle which is set equal to the circle command?

Last one,

I do not understand why the RadSize (mixed capitals) is set to double. The circle command's default is radius so it seems that if this was doubled it would be wrong but my module is working right. Unless of course Double does not mean double in size.

Guess I need to go look at the help, but you just make things easier to understand.

Thanks
Lynn

#8
Went back and read the Help so I know that the letters have to be capitals now. I am still a bit confused on the double. I found where double means double-precision so I am wrong about it doubling the radius but could you give a little more detailed explaination? Am I wrong in thinking that this means accuracy?

Lynn

[This message has been edited by Lynn T (edited 08-07-2001).]

#9
Hello Lynn,

I believe John is out of town this week and may not be available to answer your question. I am sure he will respond to your post as soon as he returns.

Thank you,

Melodie

#10
Lynn,

Good questions and I trust my answers help:

Yes “Double” relates to accuracy that Lisp developers don’t have to worry about and I have outlined the VBA data types for storing variables below:

There are four basic Data Types:

1/ Strings = “Text in these quotes are called Strings”

2/ Numbers
Integers = Small whole numbers
Long = Large whole numbers (Not in Lisp)
Single = Single precision floating numbers (Not in Lisp)
Double = Double precision floating numbers
Currency = Money numbers. (Not in Lisp)

3/ Special
Boolean = 1 or 0 (True or False)
Byte = 0 to 255 used for color or character values
Date = Date and Times

4/ Variant = A Variant can holds any data type above.

OK, you may ask - why not use Variants for all our data needs?

A Variant uses a lot of memory (RAM) while an Integer uses very little, also VBA must convert the Variant to the required data prior to calculations or string manipulations and this slows code execution.

CAD vendors decided to use Doubles for x, y and z values for points and input for sizes of drawing entities, to ensure accuracy and compliance with Lisp and C.

This is the reason I used the Double when I declared the variable.

As far as naming conventions I use obj in front of variables to remind me that this is an object so, objCircle is the variable name of my circle object and it can be easily distinguished in the code.

Variable names cannot contain spaces and I use upper and lower case to make my code easier to read. CircleRad is easier to read than circlerad and you can also use these conventions in Lisp.



------------------
Regards
John Finlay

#11
John,
I work with Autocad during the day and trying to learn Intellicad at night. Are there major differences in VBA in intellicad and in Autocad ? Also are there any books that you reccomend that I could get to help me on my way of using VBA ?

Thanks

#12
tooldesigner,

Yes there are subtle differences between ACAD VBA and ICAD VBA and below is a short (but not exhaustive) list:

1/ ACAD uses a fixed array for holding and manipulating its x,y,z values for points where ICAD has a Point Object (ICAD is much better).

2/ ACAD calls its active drawing ThisDrawing where ICAD calls it ThisDocument (ICAD is more in line with VBA standards).

3/ The ACAD and ICAD object models are very similar however, ACAD has more objects exposed such as toolbars and solids (ACAD is much better).

4/ Not all the objects work correctly in ICAD such as the dimension objects where I believe ACAD has only a few not working as well as could be expected (ACAD is better)

5/ Nearly all Methods in both ACAD and ICAD work in exactly the same way therefore porting code between them is easy.

6/ ACAD is slower releasing updates than ICAD and you will notice enhancements in the VBA code in ICAD that don’t work in ACAD because it uses an earlier version of the VBA SDK (Software Developers Kit).

7/ ACAD has more events than ICAD.


------------------
Regards
John Finlay
Don't want to post a question - email me direct on john@acecad.com.au

#13
tooldesigner,

There are no books on ICAD VBA that I am aware off.

You can use books on ACAD VBA as a starting base and just be aware of the issues I raised in my previous post on the differences.

I use AutoCAD 2000 VBA Programmer's Reference by Joe Sutphin from Wrox Publications. It is more for the advanced user however, if you follow my posts on this forum to get you started the book will provide you with additional reading on specific topics.

Another good book is (and don’t laugh) VBA for Dummies Quick Reference by Paul Litwin. This is a handy reference to all the VBA commands is one simple spiral bound (lays flat) book and saves me from searching through VBA help to find the commands.


------------------
Regards
John Finlay
Don't want to post a question - email me direct on john@acecad.com.au

#14
I am going through your VBA pages. I was wondering when on the last part when we draw the circle and make and change the layer of the circle is there a way of making the color of the layer different ? I was also wondering if you can post some more easy VBA codes so I can totally understand what is going on within them. Also I want to say thank you for taking your time and writing these instructions for us.

#15
Tooldesigner,

Take a look at the tool named MakeLayer subroutine and you will see MColor As Integer.

When I used the MakeLayer tool in the CircleTest subroutine as below

********* Start New Code **********************
' make a layer from our Tools.MakeLayer
Tools.MakeLayer "Circles", 2, "Continuous"
' Change the property of the circle object to the new layer
objCircle.Layer = "Circles"
‘********* End New Code **********************

MLayer = “Circles”
MColor = 2
MlineType = “Continuous”

I have set the default layer color number = 2 “Yellow” and you can set any color number you want that is up to 255.

If you want to set the color of the circle entity rather than accepting the default layer color – set the Color rather than the Layer property using:


' make a layer from our Tools.MakeLayer
Tools.MakeLayer "Circles", 2, "Continuous"
' Change the property of the circle object to the new layer
objCircle.Layer = "Circles"

********* Start New Code **********************
' Change the property of the circle object to the new color
objCircle.Color = 5

‘********* End New Code **********************

The circle will be on layer “Circles” and in the ICAD explorer the default color will be yellow however, the circle color will be Blue and not set ByLayer.

Any object can have its color changed using object.Color = ColorNumber

The primary colors have been set as constants which just represent colors 1 to 7 so you may see vicBlue in which the color Blue has been set equal to the number 5. It is a lot easier to set colors using vicRed, vicYellow etc. rather than remembering the numbers and this is why its used.

As you can appreciate it is hard for me to keep everyone happy because of the differing experiences of users on an open forum, especially some users with extensive Lisp knowledge.

I can post simpler VBA code and will start a new thread “VBA The Basics”, is there any specific things you want to see in the VBA code?


------------------
Regards
John Finlay
Don't want to post a question - email me direct on john@acecad.com.au