Drawing Points In AutoCAD Using Excel & VBA

Share this

May 13, 2014

Drawing Points In AutoCAD Using Excel VBA


Introduction


During the previous weekend, a blog reader (Denis) sent me an email asking me if it is possible to draw points in AutoCAD from Excel. The answer is quite straightforward and is YES. The proposed solution involves the AddPoint method:

RetVal = object.AddPoint(Point)

Where:
RetVal: Point object. The newly created Point object.
Object: ModelSpace Collection, PaperSpace Collection, Block – the objects this method applies to.
Point: Variant (three-element array of doubles); input-only. The coordinates of the point to be created.

The sample workbook that you will find in the Downloads section below requires three main user inputs: the coordinates of the point (in X, Y, Z), the point type (select from a dropdown list) and the point size. Then, by clicking the “Add Point(s)” button the points are created either in the active drawing (if AutoCAD is already launched), or in a newly created drawing. The picture below summarizes the available point types (you will find the picture also in the sample workbook).

AutoCAD Point Types

Note: I would like to clarify that all the VBA codes presented in this blog do NOT work with AutoCAD LT. AutoCAD LT does NOT support VBA. Use a full AutoCAD version instead.

 


VBA code to draw points in AutoCAD from Excel


The code is actually a loop; most of the code is used for initializing the AutoCAD object, as well as the active/new drawing.

Option Explicit
 
Sub DrawPoints()
 
    '--------------------------------------------------------------------------------------------------
    'Draws points in AutoCAD using data - point coordinates, type and size - 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:          14/05/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 Point(0 To 2)   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 point.
    If LastRow < 2 Then
        MsgBox "There are no coordinates to draw a point!", vbCritical, "Point Coordinates 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 and draw the corresponding point(s).
    With Sheets("Coordinates")
 
        'Set the point type.
        acadDoc.SetVariable "PDMODE", .Range("E1").Value
 
        'Set the point size.
        acadDoc.SetVariable "PDSIZE", .Range("G1").Value
 
        'Loop through all the coordinates.
        For i = 2 To LastRow
 
            'Set the point coordinates.
            Point(0) = .Range("A" & i).Value
            Point(1) = .Range("B" & i).Value
            Point(2) = .Range("C" & i).Value
 
            'Draw the point.
            acadDoc.ModelSpace.addpoint (Point)
 
        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 point(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.

All links were copied from Autodesk‘s website.

 


Demonstration video


The short video below demonstrates the result of the above VBA code; a “happy face” consisted of 10 points will be drawn in a new AutoCAD drawing.

 


Downloads


Download

The file can be opened with Excel 2007 or newer. Please enable macros before using it.

 


Read also


Drawing Circles In AutoCAD Using Excel & VBA
Add Text In AutoCAD Using Excel & VBA
Insert Blocks In AutoCAD Using Excel & VBA
Add Dimensions In AutoCAD Using Excel & VBA
Send AutoCAD Commands From Excel & VBA

Page last modified: March 2, 2020

Christos Samaras

Hi, I am Christos, a Mechanical Engineer by profession (Ph.D.) and a Software Developer by obsession (10+ years of experience)! I founded this site back in 2011 intending to provide solutions to various engineering and programming problems.

Christos E. Samaras

  • Hi Tim,

    The short answer is that you have to use the InsertBlock method.
    Check this article for more information.

    Best Regards,
    Christos

  • Hi, Ferion,

    I answered your other comment as well.
    Regarding books, you can check my recommended list here.

    Check the section about AutoCAD and VBA.

    Best Regards,
    Christos

  • Hi, John,

    Yes, I think it is possible although it is not easy to do it.
    However, since you will already have selected the points in AutoCAD, it will be much easier to send the coordinates to Excel directly from AutoCAD/VBA.

    Best Regards,
    Christos

  • John Smith says:

    Hi!!! It is possible to get the coordinates of selected points in Autocad using Excel VBA only? Could you please help me? Many many thanks in advance

  • Ferion Nishani says:

    Christos. How are you able to create this VBA Code that works with AutoCAD. What language is it? and where would I be able to learn it? Do you get it from AutoCAD Macros? or LISPs? or the Auto Desk website? Do you have a book that you recommend I should read? Anything would be helpful as I am starting off with the basics and want to learn how to write this code.

  • Hey Great information thanks!

    If i was going to insert blocks that I have already created instead of the points using a similar approach how would i go about doing that?

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    Add Content Block
    >