copy

Wednesday, 14 May 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 lunched), 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 does NOT work with AutoCAD LT. AutoCAD LT does NOT support VBA. Use a full AutoCAD version instead.



VBA code



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:        xristos.samaras@gmail.com
    '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 the 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.



Did you like this post? If yes, then share it with your friends. Thank you!


Categories:


Mechanical Engineer (Ph.D. cand.), M.Sc. Cranfield University, Dipl.-Ing. Aristotle University, Thessaloniki - Greece.
Communication: e-mail, Facebook, Twitter, Google+ and Linkedin. More info