draw lines from polyline points

draw lines from polyline points

stefanveurink68AXD
Advocate Advocate
3,079 Views
5 Replies
Message 1 of 6

draw lines from polyline points

stefanveurink68AXD
Advocate
Advocate

Dear Friends, 


I am trying to write a macro but i can't really get started, i hope your answer to this question might get me going. 

 

What i want is when i got a horizontal line, en select a polyline above it, to draw a vertical line from every coordinate on the polyline to that horizontal line, in other words perpendicular to that horizontal line. 

 

If someone could show me how to loop trough coordinates of the selected polyline i think i can figure the rest out myself. For your information, the above isn't my final goal to reach but i guess when i can do the following the other stuff of what i want follows from the method to do the above. 

 

Thanks. 

0 Likes
Accepted solutions (1)
3,080 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor

When you say "every coordinate on the polyline", I assume you mean every vertex of the polyline. Also,you did not say, but I assume by "polyline", you mean Lightweight Polyline (AcadLWPolyline).

 

So, your issue is to get the geometric point (coordinate) of each vertex of the polyline, so that you can draw a line from each of these points to the said horizontal line perpendicularly.

 

Getting polyline's vertex coordinate is rather simple: just use Coordinate and/or Coordinates property AcadLWPolyline. You can look it up in AutoCAD VBA's document/code sample by: open VBA editor->open Object Browser->select AcadLWPolyline on left side -> select Coordinate or Coordinates on right side->click "?" on top of Object Browser window. This would lead you to the VBA document on AcadLWPolyline and its properties/methods with code samples.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 6

stefanveurink68AXD
Advocate
Advocate

Ok thanks, i found the obj.coordinates function allready on the internet but didn't know about the object browser, this is a very helpfull tool. 

 

However, the part i don't understand is how to loop through all those coordinates from vertices of the selected pline.  i cant say for each coordinate in 0 to ??? because i don't know the number of coordinates on forehand and next to that i want to treat the x coordinate (coordinate(0)) different than the Y (coordinate(1)) and Z (coordinate(2)). in some way i need to loop through the vertices and change the coordinates or something like that?

 

Anyways i also got some different bugs allready but these must be solveable by myself. Some explanation about the loop trough the coordinates from the different vertexes however would be very helpful. 

 

0 Likes
Message 4 of 6

Ed__Jobe
Mentor
Mentor

First, the AcadLWPolyline is a 2D entity. There is no Z element of the coordinate. The whole entity has an Elevation property. Second, the Coordinates property returns an array, so you can use the UBOUND function to find the upper bound of the array then loop for a count of int To UBound -1. For sample code on editing the vertices, see the help topic for the Coordinates property.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 6

stefanveurink68AXD
Advocate
Advocate

Ok so what i got is: 

 

-----

Sub coordinaten()

Dim olwp As AcadLWPolyline

Dim ip As Variant

Dim startpunt(0 To 2) As Double
Dim eindpunt(0 To 2) As Double
Dim t As Long

Dim u As Long
Dim v As AcadLine
Dim w As Variant

 

ThisDrawing.Utility.GetEntity olwp, ip, " selecteer polylijn"

 

w = UBound(olwp.Coordinates)

 

For t = 0 To (w - 1)
u = t * 2

startpunt(0) = olwp.Coordinates(u)
startpunt(1) = olwp.Coordinates(u + 1)

eindpunt(0) = olwp.Coordinates(u)
eindpunt(1) = 0

Set v = ThisDrawing.ModelSpace.AddLine(startpunt, eindpunt)


Next

End Sub

 

It seems to work (the lines i was looking for do appear), but i still get an error that its out of range, probably cause of wrong use of the ubound function. 

----

 

anybody sees the problem? cause i can't stand this last little problem. 

 

thanks anyways!

0 Likes
Message 6 of 6

norman.yuan
Mentor
Mentor
Accepted solution

Your code inside the For ... Next is wrong. Every 2 elements in the Coordinates array is the coordinate (X, Y values) of a vertex of the polyline. So, you can set "Step 2" in the For ... loop to easily parse the coordinate array into coordinate for each vertex.

 

The code should look like (it works with my test):

Option Explicit

Public Sub DoWork()

    Dim i As Integer
    Dim pl As AcadLWPolyline
    Dim ent As AcadEntity
    Dim pt As Variant
    Dim line As AcadLine
    
    On Error Resume Next
    
    ThisDrawing.Utility.GetEntity ent, pt, vbCr & "Select polyline:"
    If ent Is Nothing Then Exit Sub
    If TypeOf ent Is AcadLWPolyline Then
        Set pl = ent
    Else
        MsgBox "Selected entity is not polyline!"
        Exit Sub
    End If
    
    On Error GoTo 0
    
    Dim startPt(0 To 2) As Double
    Dim endPt(0 To 2) As Double
    
    For i = 0 To UBound(pl.Coordinates) Step 2
        
        startPt(0) = pl.Coordinates(i): startPt(1) = pl.Coordinates(i + 1): startPt(2) = 0#
        endPt(0) = pl.Coordinates(i): endPt(1) = 0#: startPt(2) = 0#
        
        Set line = ThisDrawing.ModelSpace.AddLine(startPt, endPt)
        line.Update
        
    Next
    
End Sub

Please use the "</>" button from the toolbar (above the text entry window) to post code, so that it is easy to read.

 

HTH

 

 

 

Norman Yuan

Drive CAD With Code

EESignature