Visual Basic for Applications in IntelliCAD
Part 4
John McIver
The fourth in a series of articles introducing custom programming of
IntelliCAD and AutoCAD using Visual Basic for Applications (VBA)
This article describes the
manipulation of layers in VBA, and present a function to create or set drawing
layers.
This will also mark the beginning of the creation of a library of standard
utility functions, to make general VBA programming in IntelliCAD quicker and
more convenient.
The functions and examples presented here are included in a single VBA
project. The screen for this, when running, is shown in Figures 1 and 2, while
the files themselves comprise either the executable code (which contains the
program code), in the file,
CommonProjects.Vbi
or the individual source code files,
UserForm1.Frm UserForm1.Frx Module1.Bas
Note that if you want to use the "CommonProjects.Vbi" file you will
have to copy this into your IntelliCAD directory. There may already be a file of
this name in the directory, so you should rename the existing file. It might be
dangerous to delete it as it may be part of some other application.
Getting the Current Layer Name
We'll start by just getting the current layer name (the name which is the
currently active drawing layer) and displaying that on the screen. This function
has been presented previously, but makes a good point from which to expand on
the subject of drawing layers.
'----------------------------------------------------------
'
Display the current Layer name
'----------------------------------------------------------
Private Sub
CommandButton2_Click() Dim LayerName As String
LayerName = Doc.ActiveLayer.Name
MsgBox "Layer: " + LayerName, vbInformation End Sub
'----------------------------------------------------------
The function operation is very simple. We first define a variable, called
"LayerName", as a string. This will be used to store the actual layer
name we get from VBA. We then use the "ActiveLayer" function and
request its "Name" property. This returns us the name of the current
drawing layer, which we store in the "LayerName" variable. Finally we
display this on the computer screen using the standard VBA "MsgBox"
(Message Box) command.

Our example "Layer Function" program's dialogue box, open on the
IntelliCAD screen.
Getting an Entity Layer Name
While it's nice enough to be able to get the currently active drawing layer,
it would be far more useful to get a display of the drawing layer for any
entity. That is, if you were to pick an entity, then its layer would be
displayed. The following function will perform this task.
'----------------------------------------------------------
'
Pick an entity and display the layer name
'----------------------------------------------------------
Private Sub
CommandButton6_Click() Dim PickObj As IntelliCAD.Entity Dim Pt1 As
IntelliCAD.Point Dim Ename As String UserForm1.Hide ' hide the form
Doc.Utility.GetEntity PickObj, Pt1, "Pick an Object"
Ename = PickObj.EntityName
MsgBox "Name: " & Ename & vbCrLf & _
"Layer: " & PickObj.Layer
UserForm1.Show ' redisplay the form End Sub
'----------------------------------------------------------
Its operation should be fairly clear. We need to perform two basic
operations. We first need to pick an object on the screen, which is done using
the "GetEntity" function, then we need to get its layer name, which is
accessed using the "Layer" property of the selected object.

A closer view of the "Layer Function" dialogue box.
The "GetEntity" function requires three parameters. The first is an
entity object (called "PickObj" here), which is returned by the
function and stores the entity information for the particular item picked. The
second is a pick point, which is the location of your pick on the screen. This
is returned by the function, and stored in the "Pt1" variable here.
The third parameter is a prompt string, which will be displayed when the
function executes.
We now have the entity data for an item which was selected on screen, and
would like to get its name and layer. The name is extracted using the "EntityName"
property of the selected object (PickObj). This value is stored in a variable
called "Ename". Note that the variables "Ename", "PickObj"
and "Pt1" have been previously defined at the start of this function.
Like the entity name, the entity layer is a property of the selected object (PickObj),
so this is easily accessed by requesting "PickObj.Layer". The entity
name was obtained as a separate operation, whereas the layer name was obtained
as part of the message box display function. This data can be accessed in
various ways. If required, the message box function could just as easily have
been rewritten as,
MsgBox "Name: " & PickObj.EntityName & vbCrLf & _
"Layer: " & PickObj.Layer
Note that the underscore character (_) at the end of the first line here is a
line continuation character, included simply to make displaying the program code
a little neater. Also note the pair of commands,
UserForm1.Hide ' hide the form
UserForm1.Show ' redisplay the form
are used here to allow interactivity with the user. The form must be
suppressed while the user selects an entity on the screen, to stop it getting in
the way, then redisplayed after the selection has been made. This procedure has
been described more fully in a previous article.
Also, when trying this function, it's a good idea to first open an existing
drawing, which contains a number of entities on different layers. A new drawing
will not provide anything for you to test it on. Alternatively, create a test
drawing comprising several lines, each on a different drawing layer.
Creating a New Layer
Having presented some ways to interrogate a drawing for information about
existing layers, we now need to discuss how to create new layers. To add a new
layer to a drawing requires a single command, as shown below.
'----------------------------------------------------------
'
Create a New Layer called "TestLayer1"
'----------------------------------------------------------
Private Sub
CommandButton3_Click() Dim Lyr As IntelliCAD.Layer
Set Lyr = Doc.Layers.Add("TestLayer1") Lyr.Color =
vicYellow End Sub
'----------------------------------------------------------
Here, we first need to define a layer object (called "Lyr" here) as
an IntelliCAD Layer entity. We then use the "Layers.Add" function to
create a new layer, this layer having the name defined in the single function
parameter (the name "TestLayer1" here). Once the layer has been
created we can manipulate its various properties by accessing and changing them,
as shown in the last line of the function. Here we set the layer colour to be
yellow, by setting the "Color" property of the "Lyr" layer
entity to be "vicYellow".
Note that "vicYellow" is a predefined constant in VBA which
contains the numeric value for yellow. It's easier to use (and remember) the
"vicYellow" variable name than have to define it as a hexadecimal
number.
A Routine to Create or Set Layers
One of the most useful AutoLisp functions I ever wrote was a routine to
create or set drawing layers. This operated by looking for a particular drawing
layer. If it existed then it was made the current drawing layer. If it didn't
exist then it was created. When I moved to VBA this was one of the first
functions I set out to create, and this is the function I shall now present.
The function is called "SetLayer" and its use is shown below.
'----------------------------------------------------------
'
Create a new layer (using the SetLayer function)
'----------------------------------------------------------
Private Sub
CommandButton4_Click() SetLayer "FLANGE", 1, "CONTINUOUS"
End Sub
'----------------------------------------------------------
The function is called with three parameters. The first is the layer name,
the second the layer colour and the third the line type for this layer. The
function will look to see if a layer with the specified name exists. If it does
then that layer will be set to be the current drawing layer. If the layer does
not exist then a new layer of that name will be created, using the specified
colour and line type. This function's best feature is its flexibility. You don't
need to keep track of layer names as it will take care of itself.
The following are the functions making up "SetLayer" and are
modified from code obtained from various other sources, so I don't claim any
ownership of this. I've simply adapted the functions to my specific
requirements. There are three separate functions involved; the main "SetLayer"
function, a function to check whether a layer exists and a function to set the
required linetype. The main "SetLayer" function is as follows.
'----------------------------------------------------------
'
Function to set an existing layer or create a new drawing layer
'----------------------------------------------------------
Sub
SetLayer(LayerName As String, _ LayerColour As Integer, _ LayerLineType As
String)
Dim NewLayer As IntelliCAD.Layer
Dim RetLayer As Object
Set RetLayer = Doc.ActiveLayer
' Return current layer
' If the layer does not exist then create it
If LayerExist(LayerName) = False Then
Set NewLayer =
Doc.Layers.Add(LayerName)
' Create Layer
NewLayer.Color = LayerColour
NewLayer.Linetype = LayerLineType
' Linetype must already exist
Set CurrentLayer
= NewLayer
End If
' The layer must now exist, so set it to be the current layer
Doc.ActiveLayer = CurrentLayer
End Sub
'----------------------------------------------------------
This function operates as follows. It first checks to see if the requested
layer name already exists (using a function which will be described a little
later). If it does not exist then a new layer is created using the "Layers.Add"
function. It then has a colour assigned to it, and a line type (again using a
function which will be described later). If the layer does exist then it is
simply set to be the current drawing layer.
The function used to check whether the requested layer exists is as follows.
'----------------------------------------------------------
'
Function to determine if a layer exists
'----------------------------------------------------------
Function LayerExist(ByVal LayerName As String) As Boolean
Dim
Element As Object
LayerExist = False
For Each Element In Doc.Layers
If
Element.Name = UCase(LayerName) Then
LayerExist = True
Set CurrentLayer =
Element
Exit For
End If
Next
End Function
'----------------------------------------------------------
This is called with the specified layer name. A flag called "LayerExist"
is initially set to be false, assuming the layer does not exist. We then move
through each entry in the complete set (collection) of drawing layers, comparing
their name to that of the layer we want. If we find a match then we set the flag
to be true, set the layer to be the current drawing layer and exit the function.
If we don't find a match then the function will exit after all the existing
layer names have been checked, with the flag still set to be false.
Finally, the third function we need is a function to set the layer line type,
shown below. If you don't need line types other than "CONTINUOUS" then
this can be ignored and the appropriate line in the "SetLayer"
function removed. Although this function should be able to load a new linetype,
it is safer to have all the line types you may want to use already loaded into
the drawing. It's best to have these preloaded in your prototype (template)
drawing file, so this routine will always work correctly.
'----------------------------------------------------------
'
Function to load a new line type into the drawing
'----------------------------------------------------------
Sub
SetLineType(LineTypeName As String)
Dim entry As IntelliCAD.Linetype
Dim found
As Boolean
found = False
For Each entry In ThisDrawing.Linetypes
If
StrComp(entry.Name, LineTypeName, 1) = 0 Then
found = True
Exit For
End If
Next
If Not (found) Then
ThisDrawing.Linetypes.Load LineTypeName,
"ICAD.LIN"
End If
End Sub
'----------------------------------------------------------
As with the function to check if a layer exists, this operates in a similar
way. It sets a flag called "found" to initially be false, then
progresses through all the linetypes in the drawing. If a match is found the
flag is set to be true then the function exits. The linetype is actually set in
the "SetLayer" function. This routine only checks whether it must load
a new linetype. If no match is found the function attempts to load the requested
linetype from the "ICAD.LIN" linetype file, then exits, for the new
linetype to then be set in the "SetLayer" function.
As this is a generally useful function it should be included in a program
module (MODULE1.BAS for example), as a public function, where it can be accessed
from anywhere in your application.
The Sample Application
The code examples provided with this article illustrate the functions
described above. However, the "SetLayer" code example does a little
more than just display the function operation. We've included an example where
you can choose a layer name and a layer colour from two list boxes, then create
a new layer using these values with the "SetLayer" command. This is a
better guide to how you may want to employ this function in your own code, and
also a good illustration of the flexibility of the "SetLayer" command
itself.
Finding More About the Commands Available in IntelliCAD VBA
Following the previous article in this series, which dealt with object snap
modes, the question was asked, "How did I know about all the OSNAP variable
names?". There is very little documentation available for VBA in
IntelliCAD, so what reference sources exist to help you find which commands and
functions are available to you?
There is a degree of help available in the IntelliCAD HELP files. I'm basing
this series on Cadopia's version of IntelliCAD, so this may vary slightly from
some other versions. If you access the HELP from the IntelliCAD drawing screen,
and search through this using the Index, there are examples and information in
there about how to use specific VBA commands.
Unfortunately, this assistance does not extend to the HELP system within the
VBA Editor screen. In a typical Visual Basic Editor it would be possible to
highlight a single word or command, then press the <F1> key and have an
appropriate screen of information about that command displayed. In VBA here
though, this would generally give you a message that there was no information
available for the particular command.
To get a complete overview of the commands available for IntelliCAD within
VBA, you will need to look at the "Object Browser", accessed from
within the VBA Editor screen. This is opened from one of the central icons on
the top menu bar.

Showing how to access the "Object Browser" icon from within the VBA
Editor screen.
When selected, a dialogue box will appear displaying a list of all the
functions available to you within VBA.

The open "Object Browser" dialogue box, showing the contents of all
the available programming libraries.
This is typically a long list, so we need to isolate only those commands
relevant to IntelliCAD. This is done by selecting the list box in the top left
of the Object Browser dialogue box and choosing "IntelliCAD" (Figure
5). Normally all the available function libraries are displayed, but we can
limit the functions shown to only a single library, which is what we've done
here.

Showing how to select only the "IntelliCAD" function library in the
"Object Browser". We are only interested in the commands related to
IntelliCAD, so we need to remove all the other options from the display.
The purpose of the Object Browser is to allow you to see all the functions
and associated items which are present in a particular library, and which you
can access. For example, if we consider the case of our previous article, where
I needed to find all the snap modes provided within VBA, and the constant names
they had been given, then this can be done with the Object Browser (Figure 6).
By selecting "ObjectSnapMode" in the left column I am then presented
with all the available options in the right column.

The list of available Object Snap modes, accessed and displayed using the
"Object Browser".
Although the Object Browser is not really an on-line help system, it does
give an overview of the full range of functions available to you, and a general
guide as to how they may work. So, at worst, you can determine a particular
operation is available, but you may have to experiment a bit in order to get it
to work properly.
The Object Browser does have one additional feature. It will offer a template
as to how a particular function will operate. For example, if you select
"Document" in the left column, the right column will display all the
options available. If you then select, for example, the "GetVariable"
function in the right column (Figure 7), the area at the bottom of the dialogue
box will display the parameters which need to be passed to this function, their
type and the value which would be returned. In this case, the "GetVariable"
function will need to have a variable name passed to it, in the form of a
string, while the returned value will be the value of the chosen system
variable.

Another example of using the "Object Browser". Here we have found the
"GetVariable" function for an IntelliCAD document. Note that the
format of the function is displayed in the box at the bottom of the screen. This
shows it requires a string variable to be passed to it (the variable name) and
returns a system variable value. With just a bit of guesswork you can generally
find out how most of the IntelliCAD VBA functions operate.
Conclusion
In this article we've addressed several issues related to handling layers in
IntelliCAD's VBA. We've also described a few ways to get a little more
information about the functions which are available to you. In the next article
we hope to present a simple programming application, to show how everything fits
together.

|
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!
|
|
|
|
|