Parts of a VBA Project
A VBA project can be as simple as one class module called ThisDrawing and
three lines of code, or as complex as a project that might include UserForm
and both standard and class modules. The solution that is being developed
determines the type of modules that will be a part of the final project. The
following sections outline what the different modules in a project are and
what is contained in each module.Standard code module
Standard code modules are used to store procedures, data types, variables,
and functions that are accessed by other modules in the project. Code
modules should be used to hold reusable code that can be used in future
projects or called from multiple places in a project. This concept is called
modular programming. It helps to avoid redundant code and also gives you
the ability to reuse the code in other, similar projects.
Class code module
Class code modules are used to store procedures and properties that define
a user-defined class. A user-defined class allows you to create your own
object for storing information or manipulating other objects, similar to the
way AutoCAD uses objects. All AutoCAD VBA projects contain a special class
module called ThisDrawing. The ThisDrawing class represents the object
that defines the current drawing in the AutoCAD session. Although you can
create your own classes with VBA, you donít often need to do it.
Procedures (subroutine or function)
A procedure is a block, or group, of code that is given a name so it can be
referenced easily in a project. The two different types of procedures are
sub and function. A sub, or subroutine, is a type of procedure that does not
return a result; a function returns a single result after the procedure has
completed all tasks and computations. Procedures to manipulate objects or
return information about an object can be found throughout the AutoCAD
Object Library. Some examples of procedures are
Function Add2WholeNumbers (Num1 as Integer, Num2 as Integer)
As Integer
Add2WholeNumbers = Num1 + Num2
End Function
Sub EchoValue (Value as Variant)
MsgBox Cstr(Value)
End Function
Hereís another example of using the previous two custom procedures
Add2WholeNumbers and EchoValue:
EchoValue Add2WholeNumbers(1, 3)Declaring variables
A variable is a way to store data in memory for use later, to hold the value
of an argument that is passed into a procedure, or to capture the returning
Dim statement
value of a function. To use a variable, you should first use the
to dimension (or declare) the variable as a specific data type. Declaring the
variable as a specific type helps the editor to check the data integrity of the
code when it is being executed and debugged.
Variables can be defined locally within a given procedure or module and
globally at a module level. For most data types, you only need to dimension
the variable and assign the value that you want stored with it. Some exam-
ples of variables are
Dim strValue as String
Dim vVar
Data types
Data types are used to describe what type of data a variable holds; only a
single data type can be assigned to a variable at one time. By default, when
Dim statement, or when it is defined with
a variable is not declared with the
the statement but no data type, it is declared as a variant. Variants are often
used to capture return values and as arguments for a procedure, in some
cases based on the type of information that is being passed in.
Table 4-1 shows some of the commonly used data types in VBA.
Table 4-1Common Data Types
Data TypeBrief DescriptionExamples
BooleanA true or false value.True
IntegerAny whole number without decimal places.10
ñ32,768 to 32,767
DoubleAny large number with decimal places and is accurate 1.0, 0.23451, .5
up to 16 digits.
1.80 x 10308 to ñ4.94 x 10-324 for neg.; 4.94 x 10ñ324 to
1.80 x 10308 for positive values
StringAny number of alphanumeric characters enclosed in ìHelloî
double quotes.
Range is from 0 to approximately 2 billion characters.
ObjectA valid COM object.objLine
VariantAny numeric range up to a double, a text string, or 183.59Assigning a value to a variable
The equal sign (=) is used for value assignment to a variable. The value on
the right is assigned to the variable on the left. If an object is being returned
Set statement must be used in conjunction
to be stored in a variable, the
with the equal (=) sign. Some examples of value assignment are
String value assignment
Dim strValue as String
strValue = Hello out there
Object value assignment
Dim acObjLine as AcadLine
Dim dPT1(0 to 2) as Double, dPT2(0 to 2) as Double
C
dPT1(0) = 0: dPT1(1) = 0: dPT1(2) = 0
dPT2(0) = 5: dPT2(1) = 5: dPT2(2) = 0
Set acObjLine = ThisDrawing.ModelSpace.AddLine(dPT1, dPT2)
The basics of working with objects
You may encounter objects throughout the use of the AutoCAD Object Library
and when working with UserForms and controls. Objects come in two different
distinct types: objects and collections.
Objects exist as individuals and as part of a group containing many objects
of the same kind. The group of objects is referred to as a collection. The first
object that you encountered when you opened the Visual Basic Editor is
ThisDrawing, which is an example of an individual object. ThisDrawing rep-
resents an object of the data type AcadDocument. ThisDrawing is an object
that represents the current drawing and is part of a collection that contains
all open drawings in the application called AcadDocuments, which is part of
the AcadApplication object.
Now that you are familiar with one type of object, you must know how to inter-
act with it. After you have a reference to an object, you use a dot to access its
properties or methods. At times, you are accessing an object below the cur-
rent object that you are referencing. The following syntax represents how to
access the properties and methods of an individual object and not one that is
part of a collection. The syntax shows a space before and after the dot for
readability, but there are no spaces on either side of the dot when writing the
line in a code window:
Object .
ThisDrawing.FullName
ThisDrawing.ModelSpace.AddLine dPT1, dPT2If you need to access an object that is part of a collection, you need to do a
little more work. Objects contained in a collection have to be accessed in a
slightly different way. Objects contained in a collection are indexed and are
accessible by using their unique names or a number in most cases. The
index is the order in which the items are added to the collection and is simi-
lar to a street address for a house. All houses have unique street numbers,
so they can get their mail; indexes for collections work the same way. Most
collections begin with an index of 0; however, some start with an index of 1.
The collection works the same whether the first item of the collection starts
at 0 or 1. (Really itís what the programmer felt like using that day for the first
itemís index.) Here are a few examples of how to access a layer from the
Layers collection in a drawing.
ThisDrawing.ActiveLayer = ThisDrawing.Layers(0)
ThisDrawing.ActiveLayer = ThisDrawing.Layers(1)
As mentioned earlier, the Set statement must be used to store a reference to
the object in a variable:
Dim objLayer As AcadLayer
Set objLayer = ThisDrawing.Layers(0)
Adding comments
A comment is a line or series of lines in a code module that do not get exe-
cuted. A comment can be used to identify what is taking place in a procedure
or may consist of a general note at the top of the module that includes gen-
eral revision and author information. An apostrophe () or single quote
mark is placed at the front of a text string to designate it as a comment. By
default, a comment appears in the color green within the Visual Basic Editor.
An example comment is
Close out application
Introducing the AutoCAD Object Model
The AutoCAD Object Model is the organizational structure that is used to
navigate the Object Library. The AutoCAD Object Model (see Figure 4-11) is
very large; only a small portion is shown here.
Object
Model.
Bo
Cha
At the top of the AutoCAD Object Model is the Application; below that are
the Preferences and Documents collections. You can identify that they are
collections because their names end with s. Just below the Documents col-
lection are the Document objects; this is the level that ThisDrawing repre-
sents in the object model. To give you an idea of what each Document is
made up of, the following properties and methods, and many others, are
part of the Document object: ActiveLayer: Property to set and return the current layer. FullName: Property to return the drawingís name with path. Blocks: Method returns a reference to the AcadBlocks collection. SaveAs: Method used to save the drawing with a different name or
location.
These steps show you how to open the AutoCAD Object Model Map in the
AutoCAD Online Help system:
1. From the Help menu in the AutoCAD, choose Additional Resources
Developer.
2. Click the Contents tab and expand ActiveX and VBA Reference.
3. Select Object Model just below ActiveX and VBA Reference on the
Contents tab.
Create a basic VBA project
If youíve come this far, you should be ready to build a small VBA project to
add a line in the current drawing.

Tidak ada komentar:
Posting Komentar