Draw a Circle in Excel Vba
Introduction
The idea of cartoon objects in AutoCAD by using Excel data has been analyzed several times in this blog. Nosotros accept seen how to draw polylines, 3D polylines, and so, today we will learn how to draw circles. To tell y'all the truth, the code below was developed as a response to the various requests that I accept received the terminal few weeks from web log readers, also every bit from Youtube users.
The lawmaking is based on the AddCircle method. According to AutoCAD VBA help, the structure of this method is the post-obit:
RetVal = object.AddCircle(Centre, Radius)
Where:
RetVal: Circle object. The newly created Circle object.
Object: ModelSpace Collection, PaperSpace Collection, Block – the objects this method applies to.
Centre: Variant (iii-element array of doubles); input-only. The 3D WCS coordinates specifying the circle'south middle.
Radius: Double; input-just. The radius of the circle. Must be a positive number.
Remarks: This circumvolve is created on the XY plane of the WCS.
The sample workbook that you will find in the Downloads department below requires two main user inputs: the coordinates of the circumvolve center (in X, Y, Z) and the circle radius. Then, by clicking the "Depict Circle(south)" push button the circles are being fatigued either in the active drawing (if AutoCAD is already lunched), or in a new drawing. If you run the workbook with the sample information, 5 circles will exist drawn in AutoCAD, which will look like the Olympic Games logo (a small tribute to the Winter Olympics that finished the previous calendar month).
VBA code to depict circles in AutoCAD from Excel
The lawmaking is actually a loop; almost half of it is used for initializing the AutoCAD object, equally well as the active/new cartoon.
Pick Explicit Sub DrawCircles() '-------------------------------------------------------------------------------------------------- 'Draws circles in AutoCAD using information - circumvolve center coordinates and circumvolve radius - from Excel. 'The code uses late binding, and then no reference to external AutoCAD (blazon) library is required. 'It goes without saying that AutoCAD must exist installed at your estimator before running this lawmaking. 'Written by: Christos Samaras 'Date: 04/03/2014 'e-mail: [e-mail protected] 'site: http://www.myengineeringworld.net '-------------------------------------------------------------------------------------------------- 'Declaring the necessary variables. Dim acadApp As Object Dim acadDoc As Object Dim acadCircle Every bit Object Dim LastRow As Long Dim i As Long Dim CircleCenter(0 To two) As Double Dim CircleRadius As Double 'Actuate the coordinates sheet and find the last row. With Sheets("Coordinates") .Activate LastRow = .Cells(.Rows.Count, "A").Stop(xlUp).Row End With 'Check if there are coordinates for at least 1 circle. If LastRow < two Then MsgBox "At that place are no coordinates to draw a circle!", vbCritical, "Circle Centre Error" Exit Sub End If 'Check if AutoCAD awarding is open up. On Fault Resume Next Gear up acadApp = GetObject(, "AutoCAD.Application") 'If AutoCAD is not opened create a new instance and make information technology visible. If acadApp Is Nothing And then Set acadApp = CreateObject("AutoCAD.Application") acadApp.Visible = True End If 'Cheque (again) if there is an AutoCAD object. If acadApp Is Nothing Then MsgBox "Sorry, it was impossible to start AutoCAD!", vbCritical, "AutoCAD Mistake" Get out Sub End If On Error GoTo 0 'If there is no active drawing create a new one. On Error Resume Adjacent Prepare acadDoc = acadApp.ActiveDocument If acadDoc Is Nothing Then Set acadDoc = acadApp.Documents.Add End If On Fault GoTo 0 'Check if the agile space is paper space and alter it to model infinite. If acadDoc.ActiveSpace = 0 Then '0 = acPaperSpace in early on binding acadDoc.ActiveSpace = 1 '1 = acModelSpace in early bounden Cease If 'Loop through all the coordinates/radius and draw the corresponding circle(s). With Sheets("Coordinates") For i = 2 To LastRow 'Prepare the circle radius. CircleRadius = .Range("D" & i).Value 'If the circumvolve radius is greater than 0, get the circle centre and draw the circle. If CircleRadius > 0 And so '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) Terminate If Next i Cease With 'Zoom in to the drawing area. acadApp.ZoomExtents 'Release the objects. Gear up acadCircle = Nothing Ready acadDoc = Nothing Prepare acadApp = Zero 'Inform the user near the process. MsgBox "The circumvolve(due south) was/were successfully drawn in AutoCAD!", vbInformation, "Finished" Cease Sub
Note that if you have AutoCAD 2010 or a newer version, you lot will accept to download and install the VBA module, otherwise the code volition probably neglect.
- AutoCAD 2010 VBA module 32-scrap
- AutoCAD 2010 VBA module 64-fleck
- AutoCAD 2011 VBA module 32-fleck
- AutoCAD 2011 VBA module 64-bit
- AutoCAD 2012 VBA module 32-flake
- AutoCAD 2012 VBA module 64-bit
- AutoCAD 2013 VBA module 32-fleck
- AutoCAD 2013 VBA module 64-bit
- AutoCAD 2014 VBA module 32-flake
- AutoCAD 2014 VBA module 64-bit
- AutoCAD 2015 VBA module 32-bit
- AutoCAD 2015 VBA module 64-scrap
All links were copied from Autodesk's website.
Sit-in video
The short video beneath 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 tin can be opened with Excel 2007 or newer. Please enable macros before using it.
Read too
Add Text In AutoCAD Using Excel & VBA
Cartoon Points In AutoCAD Using Excel & VBA
Insert Blocks In AutoCAD Using Excel & VBA
Send AutoCAD Commands From Excel & VBA
Page last modified: October one, 2021
Source: https://myengineeringworld.net/2014/03/circles-autocad-excel-vba.html
0 Response to "Draw a Circle in Excel Vba"
Post a Comment