Polylines


Geometric definition


A polyline is a sequence of ordered and connected line segments. Since a line is defined by two points, a polyline can also be seen as a list of successively connected points:

How to add


RhinoCommons


Using RhinoCommons, we can construct a polyline as follows:

import Rhino
import scriptcontext as sc
import math

# points from which the polyline needs to be created
point_A = Rhino.Geometry.Point3d(1, 1, 1)
point_B = Rhino.Geometry.Point3d(2, 2 ,2)
point_C = Rhino.Geometry.Point3d(2, 1 ,0)

# put the points in a list
pts = []
pts.append(point_A)
pts.append(point_B)
pts.append(point_C)

# instantiate the polyline
my_polyline = Rhino.Geometry.Polyline(pts)

# add the object to the workspace
sc.doc.Objects.AddPolyline(my_polyline)


import Rhino
import scriptcontext as sc

# Parameters to create the polygon
my_circle = Rhino.Geometry.Circle(10)
number_edges = 8

# Creating a regular n-gon inscribed in a circle
my_octogon = Rhino.Geometry.Polyline.CreateInscribedPolygon(my_circle,number_edges)

sc.doc.Objects.AddPolyline(my_octogon)


RhinoScripSyntax


import rhinoscriptsyntax as rs

# Creating a list of points:
for i in range (10):
    pts.append(rs.AddPoint((i, i+1, i+2)))

# Creating and adding the polyline to the workspace
rs.AddPolyline(pts)

Main methods and properties


Methods


Among the main methods we have:

# Will add a point of coordinates (x, y, z) at the end of the list of points of the polyline:
x = 1
y = 2
z = 3

my_polyline.Add(x, y, z)

# Will compute the wheighted average of the polyline's segment centers:
my_polyline.CenterPoint()

# Will retrieve the point at the length parameter t in the polyline (t=0.0 means beginning of first segment, t=5.25 means first fourth of the fifth segment...)
length_parameter = 5 + 1/3
my_polyline.PointAt(length_parameter)

# Will merge all colinear segments, given an angular tolerance:
tol = sc.doc.ModelAngleToleranceRadians
include_seam = False
my_polyline.MergeColinearSegments(tol,include_seam)


Properties


Among the main propoerties we have:

# Will evaluate wether the polyline is closed or not:
is_closed = my_polyline.IsClosed
print("is closed : ",is_closed)

# Will evaluate how many segments the polyline contains:
segments = my_polyline.SegmentCount
print("number of segments : ",segments)

# Will measure the length of the polyline
length = my_polyline.Length
print("length of polyline : ",length)


🛠 Exercise



01: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍

Solution:

01: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍