To update a code example for use with VB, you must first reference the AutoCAD type library.
To do this in VB, select the References option from the Project menu to launch the References dialog box. From the References dialog box, choose the type library for AutoCAD, and then click OK.
Next, in the code example, replace all references to ThisDrawing with a user-specified variable referencing the active document. To do this, define a variable for the AutoCAD application (acadApp) and for the current document (acadDoc). Then, set the application variable to the current AutoCAD application.
If AutoCAD is running, the VB GetObject function retrieves the AutoCAD Application object when you specify the AutoCAD version number. If AutoCAD is not running, an error occurs that (in this example) is trapped, then cleared. The CreateObject function then attempts to create an AutoCAD Application object. If it succeeds, AutoCAD is started; if it fails, a message box displays a description of the error.
When running multiple sessions of AutoCAD, the GetObject function will return the first instance of AutoCAD in the Windows Running Object Table. See the Microsoft VBA documentation on the Running Object Table (ROT) and the GetObject function for more information on verifying the session returned by GetObject.
You must set the AutoCAD application's Visible property to TRUE in order to display the AutoCAD drawing window.
If GetObject creates a new instance of AutoCAD (that is, AutoCAD was not already running when you issued GetObject), failure to set Visible to TRUE results in an invisible AutoCAD application; AutoCAD will not even appear on the Windows taskbar.
The following code example uses the Clear and Description properties of Err. If your coding environment does not support these properties, you will need to modify the example appropriately:
Sub Ch2_ConnectToAcad() Dim acadApp As AcadApplication On Error Resume Next Set acadApp = GetObject(, "AutoCAD.Application.20.1") If Err Then Err.Clear Set acadApp = CreateObject("AutoCAD.Application.20.1") If Err Then MsgBox Err.Description Exit Sub End If End If MsgBox "Now running " + acadApp.Name + _ " version " + acadApp.Version End Sub
Next, set the document variable to the Document object in the AutoCAD application. The Document object is returned by the ActiveDocument property of the Application object.
Dim acadDoc as AcadDocument Set acadDoc = acadApp.ActiveDocument
From this point on, use the acadDoc variable to reference the current AutoCAD drawing.
The following code example demonstrates creating a line in both VBA and VB.
Creating a line using VBA:
Sub Ch2_AddLineVBA() ' This example adds a line ' in model space Dim lineObj As AcadLine Dim startPoint(0 To 2) As Double Dim endPoint(0 To 2) As Double ' Define the start and end ' points for the line startPoint(0) = 1 startPoint(1) = 1 startPoint(2) = 0 endPoint(0) = 5 endPoint(1) = 5 endPoint(2) = 0 ' Create the line in model space Set lineObj = ThisDrawing. _ ModelSpace.AddLine _ (startPoint, endPoint) ' Zoom in on the newly created line ZoomAll End Sub
Creating a line using VB:
Sub Ch2_AddLineVB() On Error Resume Next ' Connect to the AutoCAD application Dim acadApp As AcadApplication Set acadApp = GetObject _ (, "AutoCAD.Application.20.1") If Err Then Err.Clear Set acadApp = CreateObject _ ("AutoCAD.Application.20.1") If Err Then MsgBox Err.Description Exit Sub End If End If ' Connect to the AutoCAD drawing Dim acadDoc As AcadDocument Set acadDoc = acadApp.ActiveDocument ' Establish the endpoints of the line Dim lineObj As AcadLine Dim startPoint(0 To 2) As Double Dim endPoint(0 To 2) As Double startPoint(0) = 1 startPoint(1) = 1 startPoint(2) = 0 endPoint(0) = 5 endPoint(1) = 5 endPoint(2) = 0 ' Create a Line object in model space Set lineObj = acadDoc.ModelSpace.AddLine _ (startPoint, endPoint) ZoomAll acadApp.visible = True End Sub