Visual Basic for Applications in IntelliCAD
Part 2
John McIver
The second in a series of articles introducing custom programming of IntelliCAD and AutoCAD using Visual Basic for Applications (VBA)
1. Introduction
We saw in the previous article how to set up VBA to run within IntelliCAD, and briefly discussed some of the benefits of using VBA. It's now time to look at some simple programming examples, to get a feel for how VBA can be used practically.
It is assumed that the reader of this article has at least a passing familiarity with Visual Basic and the VBA IDE (Integrated Development Environment). If you don't know how to create a Visual Basic form and add a button to it then it may be best to find this out before proceeding further here. The aim of this article is to describe the use of VBA in IntelliCAD, and not to present an introduction to Visual Basic.
The code for the examples here is presented in this article, but can also be accessed as a CommonProject file, or in
the source code linked from the Resource Center.
If the description here is a little confusing then you should download the "CommonProject.vbi" file, copy it to your IntelliCAD directory (the place on disk where the Icad.exe file is located) and the application can then be run, and the code viewed in the Visual Basic Editor (VBA IDE).
2. Setting Things Up
Before we can perform any practical programming we need to first set up our VBA program so it can communicate with the IntelliCAD drawing. We need to set an object so we can use it to easily reference the drawing later on.

A general view of the VBA Editor Screen showing the Project window on the right side of the screen, the test program form on the left and a code window in the middle of the screen.
For the purposes of this article we will create a test program which can be accessed by any IntelliCAD drawing, so we need to enter the VBA Editor (from the Visual Basic menu option on the TOOLS pull-down menu). Then we need to add a new form under the "CommonProjects" section in the Project window.
Display this form on the screen and add a button to it. Now we're ready to start. First, double-click on the form to display the code window for the "UserForm_Initialize" function. Within this you need to type the following,
'--------------------------------
' Set up drawing document
'--------------------------------
Private Sub UserForm_Initialize()
Set Doc = IntelliCAD.ActiveDocument
End Sub
The comment lines are provided here for convenience and don't need to be typed in. This line sets an object called "Doc" to represent an IntelliCAD document, which is effectively the IntelliCAD drawing file. The "UserForm_Initialize" function is called every time this form gets loaded, so every time you run the program this line of code will be executed, establishing a link between IntelliCAD and VBA.
We now need to define what the object "Doc" is. This is best done in a separate code module, as we will also need this module to provide a means to easily start the program from IntelliCAD. Create a module called MODULE1.BAS in the Editor.

The VBA Editor Screen showing the contents of MODULE1.BAS.
To this add an initial line to globally define the object "Doc" to be an IntelliCAD document. By globally defining this we can refer to "Doc" anywhere in our program and it will be understood to refer to the IntelliCAD drawing. That is,
Global Doc As IntelliCAD.Document
'--------------------------------
' Load a user form automatically
'--------------------------------
Sub LoadApp()
Load UserForm1
UserForm1.Show
End Sub
Below this line we define a subroutine called "LoadApp", as shown in the code above. This will be used later to make loading the application in IntelliCAD much more elegant. Effectively, when you call the "LoadApp" subroutine the user form called "UserForm1" will be loaded and displayed. This form is the form we will be placing our test functions on. This is the entire contents of the code module.
If we now return to our form (UserForm1) we can add a button to it, if we haven't already done so. We can call this button "Exit" and behind it we will add some code to close the program, as follows;
'--------------------------------
' Close the form
'--------------------------------
Private Sub
CommandButton1_Click()
End
End Sub
This makes up the core code needed in order to start doing useful things, so now we need to do something with it.
3. Accessing Some Drawing Information
The best, and simplest, starting point is to just extract some information from a drawing file and display it on the screen. The most basic data we can access is the name of the current drawing file. Create a new button on the form and give it the caption "Drawing Name". Also add a text box to the form, so we have somewhere to display the drawing name once we've extracted it. Behind the button type the following code;
'------------------------
' Get the current drawing name
'------------------------
Private Sub
CommandButton2_Click()
UserForm1.TextBox1.Text = "Drawing Name: " + Doc.Name
End Sub
That's all there is to it. When you run the program and select the button the drawing name will be displayed in the text box on the form. The variable "Doc.Name" references the drawing name (it gets the "Name" property from the "Doc" object). This is then concatenated with the text string "Drawing Name: " and assigned to the text box's "Text" property.
We can access the current drawing layer name in a similar way. Create another button on the form, label it "Current Layer Name" then type the following code behind the button.
'--------------------------------
' Display layer name
'--------------------------------
Private Sub
CommandButton3_Click()
Dim LayerName As String
LayerName = Doc.ActiveLayer.Name
MsgBox "Layer: " + LayerName
End Sub
When selected this button will display the current drawing layer name in a message box on the screen. We could perform this function in a single line of code, as before, but here we'll take things a bit slower. We first define a local string variable called "LayerName", so we can store the name somewhere. We may wish to use the value later. We then access the "ActiveLayer" and get its "Name" property, storing this in the "LayerName" string variable. Finally we display this using the Visual Basic "MsgBox" command.
Note that we defined the "Doc" object mainly to make life easier, and reduce the amount of typing required, and also the program complexity. We could have just as well obtained the current layer name using the command,
IntelliCAD.ActiveDocument.ActiveLayer.Name
instead of,
Doc.ActiveLayer.Name
It should be apparent that part of the trick in programming in VBA with IntelliCAD is in just knowing, or remembering, the properties available for the various functions. There is not a great deal of documentation available for VBA in IntelliCAD, so much programming comes down to trial-and-error, or finding and learning from other people's code examples. As you build up experience you'll also build up a library of useful code which can be easily reused in many situations.
4. Accessing a Variable
Having accessed the drawing and current layer names, we can now progress a little further. Now we will access a drawing variable. For convenience, we will seek to obtain the AutoCAD/IntelliCAD drawing version number. This is virtually the same as for accessing the layer name, but we're using a different VBA command. As before, create a new button on the form, label it "Version Number" and type the following code behind it.
'--------------------------------
' Display a Variable
'--------------------------------
Private Sub
CommandButton4_Click()
Dim Ver As String
Ver = Doc.GetVariable("ACADVER")
MsgBox "Version: " + Ver
End Sub
Again we set a string variable, to store the value we obtain. We now use the "GetVariable" function to extract the variable value we require. This value will be "ACADVER", which is the standard variable name you would use to access a variable from the IntelliCAD command line. We then display this value in a message box on screen. You could use this same function to display any drawing variable, simply by altering the variable name requested.
5. Drawing a Line
We should now be becoming more familiar with the basics of how to interact with the drawing file from within VBA. For this example, instead of extracting values from the drawing, we will progress to creating drawing entities, beginning with a single line. As before, create a new button on the form, label it "Draw Line" and type the following code behind it.
'--------------------------------
' Draw a Line
'--------------------------------
Private Sub
CommandButton5_Click()
Dim Lin As IntelliCAD.Line
Dim StartPt As IntelliCAD.Point
Dim EndPt As IntelliCAD.Point
Set StartPt = Library.CreatePoint(0, 10, 10)
Set EndPt = Library.CreatePoint(10, 10, 0)
Set Lin = Doc.ModelSpace.AddLine(StartPt, EndPt)
Lin.Update
End Sub
This operation is a little more complex. We need to define some input data, this being a start point and an end point for the line. We can then create the line itself. To integrate with IntelliCAD we need to define a variable called "StartPt" and another called "EndPt" as IntelliCAD "Point" objects. The variable names can be anything you like, but they must be defined as "IntelliCAD.Point" objects. We also need to define an IntelliCAD line object, called "Lin" here.
We then need to set the X, Y and Z coordinates of the start and end points. This is done using a general IntelliCAD Library function called "CreatePoint". We will be drawing a line between the points 0, 10, 0 and 10, 10, 0. Having defined these two points, we now wish to add a line entity to our drawing and we wish to add it in modelspace. So we use the "AddLine" method, to create a modelspace line, from the "StartPt" coordinate to the "EndPt" coordinate. Finally we need to "Update" the line object to make sure it gets displayed.
While this may sound a little messy just to draw a line between two points, it must be remembered we are writing a computer program, and the program can employ custom functions. We could adjust the code here to create a "DrawLine" function, and encase the above into a custom function which just requires 6 numbers to be submitted (2 sets of coordinates) in order to draw the line.
It should be mentioned here that how points are handled is the main difference between AutoCAD's implementation of VBA and IntelliCAD's VBA. In general, in VBA, AutoCAD stores 3D coordinates as values in an array;
pt(0) = X pt(1) = Y pt(2) = Z
while IntelliCAD explicitly defines them as properties of a value;
pt.x = X pt.y = Y pt.z = Z
Although mentioning this may be a bit premature, it is worth knowing that because of this AutoCAD and IntelliCAD VBA programs are not likely to be compatible with each other, even though the VBA programming interface they use is fundamentally the same.
6. Drawing a Circle
To draw a circle is virtually the same as drawing a line.
Create a new button, label it "Draw Circle" and type the following code behind it.
'--------------------------------
' Draw a Circle
'--------------------------------
Private Sub CommandButton6_Click()
Dim Circ As IntelliCAD.Circle
Dim CenPt As IntelliCAD.Point
Dim Rad As Double
Set CenPt = Library.CreatePoint(0, 10, 10)
Rad = 8#
Set Circ = Doc.ModelSpace.AddCircle(CenPt, Rad)
Circ.Update
End Sub
Based on our previous comments regarding the operation of the function to create a line, this code should be fairly clear to understand. We define a centre point and then add a circle to the drawing using the "AddCircle" function.
7. Running the Program
Having presented several simple examples of programming in IntelliCAD using VBA we now really only need to explain the relevance of the "LoadApp" function we included when we first created our core code. This relates to our closing remarks in the previous article, which can be found at,
http://www.cadinfo.net/icad/VBA-1.htm

The IntelliCAD Screen showing the Macro form used to load VBA functions. Note the "LoadApp" function is available for loading our test program.
To start a VBA function from the IntelliCAD command line, or from a menu, we use the "-VBARUN" command. This requires us to specify a macro name to be run. Here, "LoadApp" is the macro name. So when the "-VBARUN" command is entered, and the "LoadApp" macro name is provided, VBA will load that function. The function will then load the UserForm1 form, which will display our test program on the IntelliCAD screen. The program will persist until the "Exit" button is selected, at which point the program will shut down.

The IntelliCAD Screen showing the test program running. This is the output resulting from the "Get Drawing Name" button being used.
In the next article we will present some more VBA advanced operations which can be performed on an IntelliCAD drawing file.

|
Sponsored Links |
AnyDWG Offers DWG to PDF, DWG to DXF, PDF to DWG, DWG to JPG, PDF to DXF Converters
|
AutoDWG offers DWF to DWG, DWG to PDF , PDF to DWG,
DWG to Flash Converters, DWG Viewer.
|
eCampus.com
Get your stuff for College... Cheap!
Textbooks, Greek Gear, DVD's, University Clothing, Computers and MORE!
|
Access Your PC from Anywhere
Free Trial plus 10% Off!
|
|
|
|
|