AutoCAD macro to Extract Coordinates of 2D Polyline (LW Polyline)

Extract Coordinates of 2D Polyline

Join online Course on AutoCAD VBA Programming

Following is the VBA code to extract Coordinates of Polyline in an AutoCAD Drawing

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

Join online Course on Scripting in AutoCAD

Macros (VBA for AutoCAD)

Scripting in AutoCAD

Tips & Tricks in AutoCAD