Convert the VBA Code to VB
From AutoCAD ActiveX
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.
Connect to AutoCAD from Visual Basic 6
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.17")If Err ThenErr.ClearSet acadApp = CreateObject("AutoCAD.Application.17")If Err ThenMsgBox Err.DescriptionExit SubEnd IfEnd IfMsgBox "Now running " + acadApp.Name + _" version " + acadApp.VersionEnd 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.
VBA versus VB Comparison Code Example
The following code example demonstrates creating a line in both VBA and VB.
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 linestartPoint(0) = 1startPoint(1) = 1startPoint(2) = 0endPoint(0) = 5endPoint(1) = 5endPoint(2) = 0' Create the line in model spaceSet lineObj = ThisDrawing. _ModelSpace.AddLine _(startPoint, endPoint)' Zoom in on the newly created lineZoomAllEnd Sub
Sub Ch2_AddLineVB()
On Error Resume Next
' Connect to the AutoCAD applicationDim acadApp As AcadApplicationSet acadApp = GetObject _(, "AutoCAD.Application.17")If Err ThenErr.ClearSet acadApp = CreateObject _("AutoCAD.Application.17")If Err ThenMsgBox Err.DescriptionExit SubEnd IfEnd If' Connect to the AutoCAD drawingDim acadDoc As AcadDocumentSet acadDoc = acadApp.ActiveDocument' Establish the endpoints of the lineDim lineObj As AcadLineDim startPoint(0 To 2) As DoubleDim endPoint(0 To 2) As DoublestartPoint(0) = 1startPoint(1) = 1startPoint(2) = 0endPoint(0) = 5endPoint(1) = 5endPoint(2) = 0' Create a Line object in model spaceSet lineObj = acadDoc.ModelSpace.AddLine _(startPoint, endPoint)ZoomAllacadApp.visible = TrueEnd Sub