Macro to Create Text in AutoCAD Drawing

Join online Course on AutoCAD VBA Programming

In this post, I have explained ‘How to create Text objects in AutoCAD using VBA’. The same macro, with a simple modification can be used to insert Current Date and Time in AutoCAD drawings.

VBA code for creating Text in AutoCAD

Following is the VBA code to create a Text object ‘XL n CAD’ of height ’10’ and rotation angle ’25°’ at the point ‘300,450,0’.

Sub CreateText()
Dim XLnCADText As AcadText
Dim InsPoint(0 To 2) As Double
Dim Height As Double
Dim Content As String
InsPoint(0) = 300#: InsPoint(1) = 450#: InsPoint(2) = 0
Height = 10
Content = "XL n CAD"
Set XLnCADText = ThisDrawing.ModelSpace.AddText(Content, InsPoint, Height)
XLnCADText.Rotation = (25 * 3.14) / 180
ZoomAll
End Sub

Explanation of the Code

Sub CreateText() - Line 1

CreateText is the name of the Macro

Dim XLnCADText As AcadMText - Line 2

When a variable is declared as AcadText, it can be used to refer Text objects in AutoCAD. Here, the variable XLnCADText is declared as AcadText.

Dim InsPoint(0 To 2) As Double - Line 3

A variable InsPoint will be created as an Array of size 3.

Dim Height As Double - Line 4

A variable Height will be created which will be used to store the height of the text object to be created.

Dim Content As String - Line 5

Content is the variable to store the content of the text object to be created.

InsPoint(0) = 300#: InsPoint(1) = 450#: InsPoint(2) = 0 - Line 6

x, y and z co-ordinates for the insertion point of the text object are defined in this line. x co-ordinate is 300, y co-ordinate is 450 and z co-ordinate is 0.

Height = 10 - Line 7

Height of text is specified as 10.

Content = "XL n CAD" - Line 8

XL n CAD is the text to be created and is stored in the variable Content

Set XLnCADText = ThisDrawing.ModelSpace.AddText(Content, InsPoint, Height) - Line 9

This statement creates a Text object of specified Content (XL n CAD) at InsPoint (300,450,0) with the specified Height (10).

XLnCADText.Rotation = (25 * 3.14) / 180 - Line 10

This statement is to rotate the newly created text at 25°. Rotation angle is multiplied by 180°π as the same should be provided as Radians.

ZoomAll - Line 11

ZoomAll method will readjust the drawing area to display the newly created text.

End Sub - Line 12

End Sub means, end of the Sub procedure otherwise end of the Macro.

Output

The code explained above when executed will create a Text object XL n CAD of height 10 and rotation angle 25° at the point 300,450,0.

Macro to insert Current Date into AutoCAD Drawing

Following is the VBA code to insert the Current Date as a Text object of height 15 into the point 200,300,0 of an AutoCAD drawing.

Sub AddCurrentDate()
Dim XLnCADText As AcadText
Dim InsPoint(0 To 2) As Double
Dim Height As Double
Dim Content As String
InsPoint(0) = 200#: InsPoint(1) = 300#: InsPoint(2) = 0
Height = 15
Set XLnCADText = ThisDrawing.ModelSpace.AddText(Date, InsPoint, Height)
ZoomAll
End Sub

Join online Course on AutoCAD VBA Programming

Programming in AutoCAD
Programming in AutoCAD

Macros in AutoCAD

Scripting in AutoCAD

Tips & Tricks in AutoCAD

Command Alias in AutoCAD

Complete list of AutoCAD Commands