copy

Thursday, 13 March 2014



Add Text In AutoCAD Using Excel & VBA


Introduction



The previous week I published a VBA code for drawing circles in AutoCAD using data from an Excel workbook. Since I received several email requests from various blog readers, I decided to continue the subject of AutoCAD automation from Excel. So, below you will find a VBA code that will help you “send” text strings from Excel to a specific location in an AutoCAD drawing. The code might be extremely useful for filling information in a drawing legend (watch the demonstration video for example).

The code is based on the AddText method. According to AutoCAD VBA help, the structure of this method is the following:

RetVal = object.AddText(TextString, InsertionPoint, Height)

Where:
RetVal: Text object. The newly created Text object.
Object: ModelSpace Collection, PaperSpace Collection, Block – the objects this method applies to.
TextString: String; input-only. The actual text to be displayed.
InsertionPoint: Variant (three-element array of doubles); input-only. The 3D WCS coordinates on the drawing where the text is placed.
Height: Double; input-only. The height of the text. Must be a positive number.

The sample workbook that you will find in the Downloads section below requires three main user inputs (as the AddText method), as well as four optional: the coordinates of the insertion point (in X, Y, Z) – in other words, where you want the newly created text to be inserted, the text height (like font size) and the text message that should be displayed in the drawing. Then, by clicking the “Add Text" button the text is inserted either in the active drawing (if AutoCAD is already lunched), or in a newly created drawing. The optional parameters give additional functionality, so you can adjust the alignment, the rotation angle, the width factor and the color of the text to be inserted. If you like to insert an Excel range as a table in AutoCAD then use the (freeware) ERTAT add-in.



VBA code



Similar to the circle case, almost half of the code is used to initialize the AutoCAD object, as well as the active/new drawing object.

Option Explicit

Sub AddText()

    '----------------------------------------------------------------------------------------------------------------
    'This macro adds text in AutoCAD using data - insertion point, text height and text message - from Excel.
    'Moreover, it provides some optional parameters, so that the user can adjust the alignment, the rotation angle,
    'the width factor and the color of the text to be inserted (using the Red, Green and Blue parameters).
    '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:          07/03/2014
    'Last Update:   16/03/2015
    'E-mail:        xristos.samaras@gmail.com
    'Site:          http://www.myengineeringworld.net
    '----------------------------------------------------------------------------------------------------------------
        
    'Declaring the necessary variables.
    Dim acadApp                 As Object
    Dim acadDoc                 As Object
    Dim acadText                As Object
    Dim acadColor               As Object
    Dim LastRow                 As Long
    Dim i                       As Long
    Dim InsertionPoint(0 To 2)  As Double
    Dim ZeroPoint(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 text message.
    If LastRow < 2 Then
        MsgBox "There are no coordinates for the insertion point!", vbCritical, "Insertion Point Error"
        Exit Sub
    End If
    
    'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
    On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
    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
             
    'Set the AcCmColor object (here acadColor) which represents colors.
    Set acadColor = acadApp.GetInterfaceObject("AutoCAD.AcCmColor." & Left(acadApp.Version, 2))
                
    'The point at the beginning of 3 axes.
    ZeroPoint(0) = 0
    ZeroPoint(1) = 0
    ZeroPoint(2) = 0
    
    'Loop through all the rows and add the corresponding text in AutoCAD.
    With Sheets("Coordinates")
        For i = 2 To LastRow
            'If the height and the message are not empty, add the text.
            If IsEmpty(.Range("D" & i)) = False And IsEmpty(.Range("E" & i)) = False Then
                'Set the insertion point.
                InsertionPoint(0) = .Range("A" & i).Value
                InsertionPoint(1) = .Range("B" & i).Value
                InsertionPoint(2) = .Range("C" & i).Value
                'Add the text.
                Set acadText = acadDoc.ModelSpace.AddText(.Range("E" & i), InsertionPoint, .Range("D" & i))
                'Align the text based on alignment selection. The default is left.
                If IsEmpty(.Range("F" & i)) = False Then
                    If UCase(.Range("F" & i)) = "CENTER" Then
                        acadText.Alignment = 1
                        acadText.Move ZeroPoint, InsertionPoint
                    End If
                    If UCase(.Range("F" & i)) = "RIGHT" Then
                        acadText.Alignment = 2
                        acadText.Move ZeroPoint, InsertionPoint
                    End If
                End If
                'If the rotation angle is not empty, rotate the text.
                '0.0174532925 is used to convert from degrees to radians.
                If IsEmpty(.Range("G" & i)) = False Then acadText.Rotate InsertionPoint, .Range("G" & i).Value * 0.0174532925
                'If the width factor is not empty, apply the width factor.
                If IsEmpty(.Range("H" & i)) = False Then acadText.ScaleFactor = .Range("H" & i)
                'Use the sheet data to change the color on each line of text (if the corresponding cells are not empty).
                If IsEmpty(.Range("I" & i)) = False And IsEmpty(.Range("J" & i)) = False And IsEmpty(.Range("K" & i)) = False Then
                    Call acadColor.SetRGB(.Range("I" & i), .Range("J" & i), .Range("K" & i))
                    'Change the color of text.
                    acadText.TrueColor = acadColor
                End If
            End If
        Next i
    End With
    
    'Zoom in to the drawing area.
    acadApp.ZoomExtents

    'Release the objects.
    Set acadText = Nothing
    Set acadDoc = Nothing
    Set acadApp = Nothing
    
    'Inform the user about the process.
    MsgBox "The text was successfully added 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. Seven lines of text are inserted in the legend of this sample drawing. Each line has different color, scale factor and alignment, while the rotation angle for all lines was set to zero.




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
Insert Blocks In AutoCAD Using Excel & VBA
Drawing Points In AutoCAD Using Excel & VBA
Excel Range To AutoCAD Table Add-In
Send AutoCAD Commands From Excel & VBA 

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