Polyline to list of points and vectors

hello guys,

I think it is a basic question but i am struggling.

I am trying to import a set of lines ad polylines from rhino to python.

i would like to extract the point coordinates from those lines and transform the segments into vectors

Anyone could help me on this ?

rs.PolylineVertices() will get you a list of the points. But how do you want to create the vectors? From the start point to the next point, and so on for each polyline segment? That can be done in a loop.

I found navigating all the line/curve types to be tricky. Here is a sample to get started:

import scriptcontext as sc
import rhinoscriptsyntax as rs
import Rhino as R

guids = rs.GetObjects('select objects')

if guids:
    for guid in guids:
        # get the geometry
        geo = rs.coercegeometry(guid)
        # just to see what type of object it is
        # could use to filter if needed.
        object_type = geo.ObjectType
        print 'this line is a {} type'.format(object_type)
        # nice function to get a polyline from all valid line types
        ok, polyline = geo.TryGetPolyline()
        if ok:
            # GetSegments gives array of lines
            for line in polyline.GetSegments():
                # lines have a vector property
                vector = line.Direction
                print 'vector for this segment is {}'.format(vector)

Well, I have something maybe a bit simpler…

import rhinoscriptsyntax as rs

objID=rs.GetObject("Select a polyline to process",4)
if objID and rs.IsPolyline(objID):
    verts=rs.PolylineVertices(objID)
    vectors=[]
    for i in range(1,len(verts)):
        vectors.append(verts[i]-verts[i-1])
for i in range(len(vectors)):
    print "Segment {} vector:  {}".format(i,vectors[i])

thanks a lot !

what doses mean ? vectors=

vectors=[] creates an empty list, which is then filled one item at a time in the loop via vectors.append(…)

Thank you !

i am trying to comput the closest point from the polyline myself as an exercise :

i would like to determine the closest point from each segment by computing each vector equations and resolving a dot product = 0