# Thread: Get coordinate of polyline by VBA

1. ## Get coordinate of polyline by VBA

Registered forum members do not see this ad.

Hi,
I've just started learning VBA in CAD.I wanna select polyline onscreen and then get its coordinate(of every vertex).But I don't know how to do it.
Thanks for any help.
Bye

2. Something like this?

Code:
```Sub Example_Coordinates&#40;&#41;

Dim Bound As Double

'Makes a selectionset.
'On Error Resume Next
Set Selection = ThisDrawing.SelectionSets.Item&#40;"Select polyline."&#41;
If Err Then
Err.Clear
Else
Selection.Clear
End If

'Select the polyline.
Selection.SelectOnScreen

For Each Obj In Selection

If Obj.ObjectName = "AcDbPolyline" Then

Set Poly = Obj
On Error Resume Next

Bound = UBound&#40;Poly.Coordinates&#41;

x = 0
y = 1

For i = 0 To Bound / 2

MsgBox "X= " & Poly.Coordinates&#40;x&#41; & "  Y= " & Poly.Coordinates&#40;y&#41;
If Err Then Err.Clear

x = x + 2
y = y + 2

Next

End If

Next Obj

End Sub```
I hope this helps you!

Greetings

Sub Example_Coordinates&#40;&#41;
without the sub xx()

how to run this code?

4. Try to change &#40; by ( and &#41; by ) in code

5. Originally Posted by vitaminm

Sub Example_Coordinates&#40;&#41;
without the sub xx()

how to run this code?
Try another one instead,type vbarun in the command line,
select "demo" sub

HTML Code:
```Option Explicit
Function PolyCoords(oEnt As AcadEntity) As Variant
Dim cnt As Integer
Dim i As Integer
Dim j As Integer
Dim iStep As Integer
Dim varPt As Variant
Dim dblCoords() As Double
Dim dblVert() As Double

If TypeOf oEnt Is AcadLWPolyline Then
iStep = 2
ElseIf TypeOf oEnt Is Acad3DPolyline Or _
iStep = 3
End If
dblCoords = oEnt.Coordinates

ReDim ptsArr(0 To (UBound(dblCoords) + 1) \ iStep - 1, 0 To iStep - 1) As Double
For i = 0 To (UBound(dblCoords) + 1) \ iStep - 1
For j = 0 To iStep - 1
ptsArr(i, j) = dblCoords(cnt)
Debug.Print ptsArr(i, j)
cnt = cnt + 1
Next
Next
PolyCoords = ptsArr
End Function

Sub demo()
Dim pts As Variant
Dim varPt As Variant
ThisDrawing.Utility.GetEntity oEnt, varPt, vbCr & "Select polyline"
If Not TypeOf oEnt Is AcadLWPolyline And _
Not TypeOf oEnt Is Acad3DPolyline And _
Not TypeOf oEnt Is AcadPolyline Then
MsgBox "Method is not applicable for this entity type"
Exit Sub
End If
pts = PolyCoords(oEnt)
End Sub```

~'J'~

6. Originally Posted by ketxu
Try to change &#40; by ( and &#41; by ) in code
Ya, tq, it woks now!

7. Registered forum members do not see this ad.

I’m a bit discouraged, I started to learn acad-vba since a few days too & I wanted to understand Steven Bastiaanse’s example which seems simple.
Why #40 & #41 , what does it mean.
Set Selection = ThisDrawing.SelectionSets.Item &#40;”Select polyline” & #41; ???
(ThisDrawing.selectionsets) expected object or method isn’t it?

Thanks for helping me

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts