You are on page 1of 19

CVEV 118/698 AutoCAD VBA

Lecture 1
Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar

Programming for AutoCAD

AutoCAD Development System (ADS):

Script file (accessed through the command line). Menu programming system. DIESEL (menu statement language).

LISt Processing (LISP) language:

For AutoCAD, AutoLISP then VisualLISP (AutoCAD 2000).

Visual Basic for Applications (VBA):

ActiveX technology.

Object ARX:
ObjectARX programming environment provides an objectoriented C++ application programming interface.


AutoLISP runs from command line or menu system. VBA is more powerful in terms of userinteraction and dialog box interfaces. VBA is also more powerful in linking to other Windows programs, I.e. opening applications, exchanging data.


VBA is run from inside the AutoCAD memory pool; thus, the communication is faster. At runtime, VB does not take the monopole of the AutoCAD activity, I.e. you can still manipulate a drawing using the ADS while the VB runs. VB is more powerful in terms of database manipulation. VBA does not have a form load procedure while VB does, I.e. forms need to be re-initialized in VBA. VBA is a subset of VB. Not all controls and properties found in VB are available in VBA.

Getting Started

The VBA IDE is accessed through the AutoCAD Tools menu Macro Visual Basic Editor, or Alt+F11. Main Features:
Project Explorer: tree diagram showing modules, forms and AutoCAD Objects in use. Properties Window. User Form Windows. Code Window. Object Browser: tree structure display used to shop for any AutoCAD/VBA object or method (bala TVA).


The Object Oriented Concept

VBA is an object oriented programming language.

Objects are entities characterized by:

Methods: subroutines and functions used to manipulate the object. Properties: set of data associated with the object. Methods act directly on those properties.

Example: Object Line (acLine) Properties Coordinates of start point Method Set [] StartPoint used to change those coordinates.

The Object Model

Entities, I.e. lines, Arcs, Shapes, Solids, etc

Tree structure that shows the hierarchy of objects in the application. Made of: collections of objects and objects. The AutoCAD Application is at the base of the trunk. Ascending procedure required to access an entity.
Preferences Collections Objects Paper Space Model Space


Other Collections


Active Document DocumentS AutoCAD Application


Using the Object Model

After declaration, each entity has to be assigned a certain location in the Object Model, in order to be manipulated:
Dim Circ as AcadCircle set Circ = ThisDrawing.ModelSpace.Addcircle()

It is required to trace this location in an ascending manner, I.e from the trunk up to the concerned branch. Later, any entity can be manipulated either directly by its reference name, or by retracing its location:
Circ.Color = acBlue


ThisDrawing.ModelSpace.Item(0).Color = acBlue

About ThisDrawing

ThisDrawing is a reference name for the active document in the application. Although the AutoCAD Application is closer in level to the roots of the Object Model, it can be manipulated from the active document, I.e:


This is not really in contradiction with the regular procedure, for Application in this case is a not really the AutoCAD Application, but only a pointer to it.

Application Pointer

Active Document DocumentS AutoCAD Application

Points to

ThisDrawing and Friends

Sample Code:
Dim oAcad as AcadApplication Dim oDocument AcadDocument Dim oPref as AcadPreferences Set oAcad = GetObject( , AutoCAD.Application) set oDocument = oAcad.ActiveDocument set oPref = oAcad.Preferences

Equivalent Syntaxes:
oDocument. ThisDrawing. oAcad. ThisDrawing.Application. oPref. ThisDrawing.Application.Preferences.

Entity Collection Objects

A Collection Object is a set of things all collected into one object. The Collection Object provides a mechanism by which several items, data, or other objects, can be linked together & referenced as a singular item: a Selection Set. A Selection Set can contain any number of entity objects, of any type (lines, blocks, etc.) referencing even more objects. Generally, 3 methods are associated with collections, providing the basic functions needed to Add, Remove or Access items in a collection.

Selection Sets Collection

thisDrawing Selection Sets Collection Selection Sets Entity Objects Selection Set Collection Hierarchy

In the AutoCAD drawing object model, the storage place for all selection sets is called Selection Sets Collection. Selection Sets are referenced by name (string type) inside the collection.

Dim S1 as AcadSelectionSet Set S1 = thisDrawing.SelectionSets.Add(S1)

Building a Collection of Entity Objects

Methods to add objects to the selection set: AddItems: appends an entity object to the selection set Select: adds entity objects using any of the valid selection mechanisms such as the previous selection set or all entity objects in the drawing, in a box, in a fence, and so forth. SelectAtPoint: adds any entity objects found at a given point to the selection set. SelectByPolygon: adds entity objects found inside a polygon area (fence). SelectOnScreen: operator selection of objects to add to the selection set.

Building a Collection of Entity Objects - Examples

Select an object & add it to a selection set:

Dim S2 As AcadSelectionSet Set S2 = ThisDrawing.SelectionSets.Add(S2) AppActivate ThisDrawing.Application.Caption S2.SelectOnScreen

Create a point object & add it to the selection set:

Dim Apoint As AcadPoint Dim PNT(0 to 2) as Double PNT(0) = 0# : PNT(1) = 0# : PNT(2) = 0# Set Apoint = ThisDrawing.ModelSpace.AddPoint(PNT) Dim varApoint(0) As AcadPoint Set varApoint(0) = Apoint S2.AddItems(varApoint)

Note: The AddItems() method requires that we supply an array of object references, and it will not accept just one single object.

Accessing Selection Set Members

Retrieving selection set members is done using an offset value into the selection set. The 1st member of the selection is at an offset of 0 & the last member is at an offset of (number of objects in the set 1). When iterating through a selection set, the easiest programming strategy is to use the For-Each-Next loop structure. The item() method is best if you know exactly where the object to be manipulated resides.

Accessing Selection Set Members - Example

The following program will get the entity type and layer of the 1st object in the entity list. It will then change the layers of matching entity types in the remainder of the selection set.
Dim MatchIt As Object Dim Ent As Object Set MatchIt = S2.Item(0) For each Ent in S2 If ((MatchIt.EntityType = Ent.EntityType))_ And (MatchIt.Layer <> Ent.Layer)) Then Ent.Layer = MatchIt.Layer Ent.Update End If Next End

More Selection Set Methods and Properties


Clear: clears the selection set (selection set still exists). Delete: deletes the selection set from the collection, linkage to the set is broken, and any reference to the set will result in an error. Erase: deletes the items from the drawing. Selection set still exists but without any entity objects in it. Highlight: highlights the objects in the set (to show the operator what items have been selected) RemoveItems: removes one or more entity objects from the selection set (objects are still on the drawing though). Update: regenerates the entity objects on the screen.
Count: Number of entity objects in the selection set. Name: Name of the selection set.


Whats Next

More Objects More methods