Introduction
The idea of drawing objects in AutoCAD by using Excel data has been analyzed several times in this blog. We have seen how to draw polylines, 3D polylines, so, today we will learn how to draw circles. To tell you the truth, the code below was developed as a response to the various requests that I have received the last few weeks from blog readers, as well as from Youtube users.
The code is based on the AddCircle method. According to AutoCAD VBA help, the structure of this method is the following:
RetVal = object.AddCircle(Center, Radius)
Where:
RetVal: Circle object. The newly created Circle object.
Object: ModelSpace Collection, PaperSpace Collection, Block – the objects this method applies to.
Center: Variant (three-element array of doubles); input-only. The 3D WCS coordinates specifying the circle’s center.
Radius: Double; input-only. The radius of the circle. Must be a positive number.
Remarks: This circle is created on the XY plane of the WCS.
The sample workbook that you will find in the Downloads section below requires two main user inputs: the coordinates of the circle center (in X, Y, Z) and the circle radius. Then, by clicking the “Draw Circle(s)” button the circles are being drawn either in the active drawing (if AutoCAD is already lunched), or in a new drawing. If you run the workbook with the sample data, 5 circles will be drawn in AutoCAD, which will look like the Olympic Games logo (a small tribute to the Winter Olympics that finished the previous month).
VBA code to draw circles in AutoCAD from Excel
The code is actually a loop; almost half of it is used for initializing the AutoCAD object, as well as the active/new drawing.
Option Explicit
Sub DrawCircles()
'--------------------------------------------------------------------------------------------------
'Draws circles in AutoCAD using data - circle center coordinates and circle radius - from Excel.
'The code uses late binding, so no reference to external AutoCAD (type) library is required.
'It goes without saying that AutoCAD must be installed at your computer before running this code.
'Written by: Christos Samaras
'Date: 04/03/2014
'e-mail: [email protected]
'site: http://www.myengineeringworld.net
'--------------------------------------------------------------------------------------------------
'Declaring the necessary variables.
Dim acadApp As Object
Dim acadDoc As Object
Dim acadCircle As Object
Dim LastRow As Long
Dim i As Long
Dim CircleCenter(0 To 2) As Double
Dim CircleRadius As Double
'Activate the coordinates sheet and find the last row.
With Sheets("Coordinates")
.Activate
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'Check if there are coordinates for at least one circle.
If LastRow < 2 Then
MsgBox "There are no coordinates to draw a circle!", vbCritical, "Circle Center Error"
Exit Sub
End If
'Check if AutoCAD application is open.
On Error Resume Next
Set acadApp = GetObject(, "AutoCAD.Application")
'If AutoCAD is not opened create a new instance and make it visible.
If acadApp Is Nothing Then
Set acadApp = CreateObject("AutoCAD.Application")
acadApp.Visible = True
End If
'Check (again) if there is an AutoCAD object.
If acadApp Is Nothing Then
MsgBox "Sorry, it was impossible to start AutoCAD!", vbCritical, "AutoCAD Error"
Exit Sub
End If
On Error GoTo 0
'If there is no active drawing create a new one.
On Error Resume Next
Set acadDoc = acadApp.ActiveDocument
If acadDoc Is Nothing Then
Set acadDoc = acadApp.Documents.Add
End If
On Error GoTo 0
'Check if the active space is paper space and change it to model space.
If acadDoc.ActiveSpace = 0 Then '0 = acPaperSpace in early binding
acadDoc.ActiveSpace = 1 '1 = acModelSpace in early binding
End If
'Loop through all the coordinates/radius and draw the corresponding circle(s).
With Sheets("Coordinates")
For i = 2 To LastRow
'Set the circle radius.
CircleRadius = .Range("D" & i).Value
'If the circle radius is greater than 0, get the circle center and draw the circle.
If CircleRadius > 0 Then
'Set the circle centert.
CircleCenter(0) = .Range("A" & i).Value
CircleCenter(1) = .Range("B" & i).Value
CircleCenter(2) = .Range("C" & i).Value
'Draw the circle.
Set acadCircle = acadDoc.ModelSpace.AddCircle(CircleCenter, CircleRadius)
End If
Next i
End With
'Zoom in to the drawing area.
acadApp.ZoomExtents
'Release the objects.
Set acadCircle = Nothing
Set acadDoc = Nothing
Set acadApp = Nothing
'Inform the user about the process.
MsgBox "The circle(s) was/were successfully drawn in AutoCAD!", vbInformation, "Finished"
End Sub
Note that if you have AutoCAD 2010 or a newer version, you will have to download and install the VBA module, otherwise the code will probably fail.
- AutoCAD 2010 VBA module 32-bit
- AutoCAD 2010 VBA module 64-bit
- AutoCAD 2011 VBA module 32-bit
- AutoCAD 2011 VBA module 64-bit
- AutoCAD 2012 VBA module 32-bit
- AutoCAD 2012 VBA module 64-bit
- AutoCAD 2013 VBA module 32-bit
- AutoCAD 2013 VBA module 64-bit
- AutoCAD 2014 VBA module 32-bit
- AutoCAD 2014 VBA module 64-bit
- AutoCAD 2015 VBA module 32-bit
- AutoCAD 2015 VBA module 64-bit
All links were copied from Autodesk‘s website.
Demonstration video
The short video below demonstrates the result of the above VBA code; 5 circles are drawn in a new AutoCAD drawing based on sample data from the Excel workbook.
Downloads
The file can be opened with Excel 2007 or newer. Please enable macros before using it.
Read also
Add Text In AutoCAD Using Excel & VBA
Drawing Points In AutoCAD Using Excel & VBA
Insert Blocks In AutoCAD Using Excel & VBA
Send AutoCAD Commands From Excel & VBA