Sie sind auf Seite 1von 15

CVEV 118/698

AutoCAD VBA
Lecture 2
Prof. Mounir Mabsout
Elsa Sulukdjian
Walid El Asmar

Application Methods

The application is accessed either by a reference


name chosen by the programmer upon declaration
or by the ThisDrawing.Application. syntax.

Be cautious when using application methods for


they can affect the whole AutoCAD window and all
its documents.

Methods:
Quit: exits drawing and drawing editor.
RunMacro: runs VBA application.
Update: updates the object on the display.
Zoom many methods available for zooming

Application Properties

Many of the properties are read-only to


keep the system integrity level high.

Properties:

FullName: returns the complete path and file name.


Caption: displays the caption above the primary
window of AutoCAD (read-only property).
ActiveDocument: points to the active document
(equivalent to ThisDrawing..
Documents: provides access to the collection of
documents opened in the AutoCAD application.
Preferences: provides access to drawing setup.
Etc.

Application Properties
Example
Dim oAcad As AcadApplication
Dim oDoc As AcadDocument
Set oAcad = GetObject(, "AutoCAD.Application")
Set oDoc = oAcad.ActiveDocument
oAcad.Preferences.Display.CursorSize = 5
oAcad.Documents.Open (strDocumentName)
oDoc.Activate

Document Methods

Methods:

New: creates a new document.


Save: saves changes to the current drawing.
Open: opens an existing drawing in single
document mode. For multiple document
mode, use it with Documents collection, I.e.
ThisDrawing.Application.Documents.Open(Name)

Close: closes the drawing.


Activate: makes the document active.

Layers

Layers is a collection directly linked


to the Document object (see Object
Model).
Although this can be done at the individual
level of the object, it is advised to manage
all color, line types, etc. settings from the
layers of the Layers collection.
Changing those settings can be done
directly using a single layers declaration
name or by referring to its position in the
Object Model.

Layers Example
Dim LayFrame As AcadLayer
Set LayFrame = ThisDrawing.Documents.Add(LayFrame)
LayFrame.Color = acBlue
set LayFrame = ThisDrawing.ActiveLayer
For i = 0 To ThisDrawing.Layers.Count 1
If LayFrame = ThisDrawing.Layers.Item(i) Then
Exit For
End If
Next i
ThisDrawing.Layers.Item(i).Linetype = StrLineTypeName

Model Space & Paper


Space

Model Space and Paper Space are the


Primary Spaces of the AutoCAD
application.
Model Space: a geometric model
placed in a 3D coordinate space.
Paper Space: used for creating a
finished layout for printing or plotting.
Objects are only made visible when
stored on either of the two Primary
Spaces Collections.

Model Space & Paper


Space (contd)

Referencing and adding objects in model space


and paper space can be done in similar ways as for
regular collections (Item( ), Count, Delete,
Add*,etc).
More methods are supported by the two primary
spaces, mainly functions for adding entities to the
collections.
However, the Remove function is not available;
deleting an object from the primary spaces is done
using the Erase method.
Unlike regular Selection Sets, upon exit of the VBA
application, the information stored in the primary
spaces collections is kept as a database for the
drawing.

Utility Object Functions

The Utility object is directly linked to the


Document object in the object model.

It contains functions only, I.e. no data


properties.

It provides with many functions to convert


data from one format to another.

It also provides with tools to support user


input and interaction via the traditional
AutoCAD interface methodologies (I.e.
Command Line).

Utility Object Functions

Some Conversion Methods:

RealToString / StringToReal
AngleToReal /AngleToString
PolarPoint: returns point variant

determined relative to an existing point.

Some User Interactive Methods:

Promt (No return)


GetReal / GetInteger / GetString / GetAngle
GetPoint (selection on screen, return variant)
GetEntity (selection of object on screen,
returns object)

Utility Object Functions


(Contd)

Dim i As Integer : Dim Nlines As Integer : Dim strNum As String


Dim dblStPnt(0 To 2) As Double : Dim dblEnPnt(0 To 2) As Double
Dim acLine() As AcadLine
With ThisDrawing.Utility
Nlines = .GetInteger("Enter number of lines to draw:")
ReDim acLine(Nlines - 1) As AcadLine

For i = 1 To Nlines
strNum = .RealToString(i, acDecimal, 0)
dblStPnt(0) = .GetReal("Enter st pnt x for line " + strNum +":")
dblStPnt(1) = .GetReal("Enter st pnt y for line " + strNum +":") : dblStPnt(2)
= 0#
dblEnPnt(0) = .GetReal("Enter End pnt x for line " + strNum +":")
dblEnPnt(1) = .GetReal("Enter End pnt y for line " + strNum +":") :
dblEnPnt(2) = 0#
Set acLine(Nlines - 1) = ThisDrawing.ModelSpace.AddLine(dblStPnt,
dblEnPnt)
Next i
End With

Error Handler

User interaction increases the risk of


having errors at run time
The user could for example select
nothing, while requested to select an
object . This might cause a crash in
the application.
For this reason, it is necessary to set
up an error handler in the code,
where user input is requested.

Error Handler
Dim dblPnt(0 To 2) As Double
Dim objSelect As Object
Again:
On Error Resume Next
ThisDrawing.Utility.GetEntity objSelect, dblPnt, "Pick
Object:"
If Err.Number <> 0 Then
Err.Clear
MsgBox "No entity was selected"
GoTo Again
End If

Whats Next

VBA lab
Filtering the selection set
The True Story of PaperSpace

Das könnte Ihnen auch gefallen