XL n CAD

Excel isn’t just a tool – it's a way of Life!

Macros (VBA for AutoCAD)

Introduction to VBA for AutoCAD

VBA stands for Visual Basic for Applications. It is a custom version of the Visual Basic programming language that has powered AutoCAD Macros since the mid-2000s. This programming interface of AutoCAD will let you,

  • Create and Execute Macros (explained in the next section). Almost everything that the user can do in AutoCAD from the user interface can be done by writing code in VBA for AutoCAD.
  • Integrate AutoCAD with other applications such as Microsoft Excel, Word, PowerPoint, Outlook, Notepad etc.

What is a Macro ?

Macros are codes written in VBA (Visual Basic for Applications) which helps to reduce the manual effort required for a job. Macros save your time and headaches by automating common, repetitive tasks. They reduce the possibility of human error that increases with many, repetitive keystrokes and tasks. The most important feature of an AutoCAD macro is, it can be saved for future use, shared among multiple users.

In a layman’s language, a macro is a recording of your routine steps in AutoCAD that you can replay using a single button

The best part is You don’t have to be a programmer or know Visual Basic Applications (VBA) to use Macros. You just need to copy the codes shared here and hit the run button to execute them. Save the file as DVB file for future use.


How to use codes shared here?

  • Step 1: Copy the Code shared here.
  • Step 2: Click on Tools Tab in the Menu bar of AutoCAD.
  • Click on Macros, then Visual Basic Editor. ALT + F11 is the Shortcut for Visual Basic Editor.
  • Step 3: Right click on the tree view and Insert a Module.
  • Step 4: Paste the Code given here into a module.
  • Step 5: Close the editor and Click on Macros in Developer Tab. You will get a dialogue box with the list of Macros ready for execution. Click on Run button to execute the macro.

Ready to use Macros


Extract X, Y, Z Coordinates of Points in an AutoCAD Drawing


Extract X, Y, Z Coordinates of Points in an AutoCAD Drawing
Sub ExtractDetailsOfAutoCADPoints()
Open “E:ExtractedDetails.Txt” For Output As 1
Dim X, Y, Z As Double
Dim XLnCADObject As AcadObject
Dim XLnCADPoint As AcadPoint
For Each XLnCADObject In ThisDrawing.ModelSpace
If XLnCADObject.ObjectName = “AcDbPoint” Then
Set XLnCADPoint = XLnCADObject
X = XLnCADPoint.Coordinates(0)
Y = XLnCADPoint.Coordinates(1)
Z = XLnCADPoint.Coordinates(2)
Print #1, X; Y; Z; “” & XLnCADPoint.Layer
End If
Next
Close (1)
End Sub
Video explaining this code in detail is given below


Extract X, Y, Z Coordinates of Blocks in an AutoCAD Drawing


Sub ExtractDetailsOfAutoCADBlocks()
Open “E:ExtractDetailsOfAutoCADBlockExtractedDetails.txt” For Output As 1
Dim X, y, z As Double
Dim XLnCADObject As AcadObject
Dim XLnCADBlock As AcadBlockReference
Dim XLnCADSelection As AcadSelectionSet
MsgBox “Select Objects”, , “XL n CAD”
On Error Resume Next
Set XLnCADSelection = ThisDrawing.SelectionSets.Add(“XLnCAD_SelectionSet”)
Set XLnCADSelection = ThisDrawing.SelectionSets(“XLnCAD_SelectionSet”)
XLnCADSelection.SelectOnScreen
For Each XLnCADObject In ThisDrawing.SelectionSets(“XLnCAD_SelectionSet”)
If XLnCADObject.ObjectName = “AcDbBlockReference” Then
Set XLnCADBlock = XLnCADObject
X = XLnCADBlock.InsertionPoint(0)
y = XLnCADBlock.InsertionPoint(1)
z = XLnCADBlock.InsertionPoint(2)
Print #1, X; y; z; XLnCADObject.Name&; ” ” & XLnCADBlock.Layer
End If
Next
Close (1)
XLnCADSelection.Clear
End Sub


Extract details Text Objects in an AutoCAD Drawing


Sub ExtractDetailsOfText()
Open “E:ExtractedDetails.txt” For Output As 1
Dim x As Double, y As Double, z As Double
Dim XLnCADObject As AcadObject
Dim XLnCADText As AcadText
Dim XLnCADSelection As AcadSelectionSet
MsgBox “Select Objects”, , “XL n CAD”
On Error Resume Next
Set XLnCADSelection = ThisDrawing.SelectionSets.Add(“XLnCAD_SelectionSet”)
Set XLnCADSelection = ThisDrawing.SelectionSets(“XLnCAD_SelectionSet”)
XLnCADSelection.SelectOnScreen
For Each XLnCADObject In ThisDrawing.SelectionSets(“XLnCAD_SelectionSet”)
If XLnCADObject.ObjectName = “AcDbText” Then
Set XLnCADText = XLnCADObject
x = XLnCADText.InsertionPoint(0)
y = XLnCADText.InsertionPoint(1)
z = XLnCADText.InsertionPoint(2)
Print #1, x; y; z; XLnCADObject.TextString
End If
Next
Close (1)
XLnCADSelection.Clear
End Sub


Extract Coordinates of 2D Polyline (LW Polyline)


Dim XLnCADObject As AcadLWPolyline
Dim i As Integer
Dim x As Double, y As Double
Open “E:ExtractCoordinatesOfALWPolylineLWPolylineCordinates.txt” For Output As #1
Dim basePnt As Variant
ThisDrawing.Utility.GetEntity XLnCADObject, basePnt, “Select a Light Weight Polyline”
Dim Cords As Variant
Cords = XLnCADObject.Coordinates
For i = LBound(Cords) To UBound(Cords) Step 2
x = Format(XLnCADObject.Coordinates(i), “0.000”)
y = Format(XLnCADObject.Coordinates(i + 1), “0.000”)
Print #1, x; y
Next i
Dim length As Double
length = Format(XLnCADObject.length, “0.000”)
Print #1, vbCrLf; “Length of the selected LW Polyline is “; length
Print #1, vbCrLf; “Number of vertices of the selected polyline is “; (UBound(Cords) + 1) / 2
Close (1)
End Sub


Extract Coordinates of a 3D Polyline


Sub ThreeDPolylineCords()
Dim XLnCADObject As Acad3DPolyline
Dim i As Integer
Dim x As Double, y As Double, z As Double
Open “E:3DPolylineCordinates.txt” For Output As #1
Dim basePnt As Variant
ThisDrawing.Utility.GetEntity XLnCADObject, basePnt, “Select a 3D Polyline”
Dim Cords As Variant
Cords = XLnCADObject.Coordinates
For i = LBound(Cords) To UBound(Cords) Step 3
x = Format(XLnCADObject.Coordinates(i), “0.000”)
y = Format(XLnCADObject.Coordinates(i + 1), “0.000”)
z = Format(XLnCADObject.Coordinates(i + 2), “0.000”)
Print #1, x; y; z
Next i
Dim length As Double
length = Format(XLnCADObject.length, “0.000”)
Print #1, vbCrLf; “Length of the selected LW Polyline is “; length
Print #1, vbCrLf; “Number of vertices of the selected polyline is “; (UBound(Cords) + 1) / 2
Close (1)
End Sub


Extract Area of Circles


Sub ExtractAreaOfCircles_1()
Open “E:DetailsOfCircles.txt” For Output As 1
Dim XLnCADObject As AcadObject
Dim XLnCADCircle As AcadCircle
For Each XLnCADObject In ThisDrawing.ModelSpace
If XLnCADObject.ObjectName = “AcDbCircle” Then
Set XLnCADCircle = XLnCADObject
X = XLnCADCircle.Center(0)
Y = XLnCADCircle.Center(1)
Print #1, “X,Y-” & X; “,” & Y
Print #1, “Area-” & XLnCADCircle.Area; “”
End If
Next
Close (1)
Dim retval As Variant
retval = Shell(“Notepad.exe ” & “E:DetailsOfCircles.txt”, vbNormalFocus)
End Sub


Extract Area of Polygons and Polylines


Sub ExtractAreaOfPolygonsAndPlines_1()
Open “E:DetailsOfPolygonsAndPlines.txt” For Output As 1
Dim XLnCADObject As AcadObject
Dim XLnCADPline As AcadLWPolyline
For Each XLnCADObject In ThisDrawing.ModelSpace
If XLnCADObject.ObjectName = “AcDbPolyline” Then
Set XLnCADPline = XLnCADObject
X = XLnCADPline.Coordinates(0)
Y = XLnCADPline.Coordinates(1)
Print #1, “X,Y-” & X; “,” & Y
Print #1, “Area-” & XLnCADPline.Area; “”
End If
Next
Close (1)
End Sub


Things to remember while using Macros

  • VBA is no longer installed by default in AutoCAD. To download VBA pack for AutoCAD, visit https://www.autodesk.com/vba-download.
  • We cannot Undo an action performed using Macro. Make sure that you have a backup of the data before executing the macro.
  • For future use the AutoCAD macro as DVB file.
  • Always fill in the description of the macro when creating one. This will help you understand your own work if you have to revisit it weeks or months later.

One thought on “Macros (VBA for AutoCAD)

  1. Hola Ajay Anand, con respecto a la extracción de coordenadas de bloques, quisiera poder extraer las coordenadas de cada bloque aplicando un click a cada bloque manualmente y que se valla insertando sus coordenadas en el excel de tal forma que pueda tener las coordenadas de los bloques pero en orden personalizado.
    Te agradezco me puedas ayudar con esta macro. Gracias

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.