Join Lines with extend line

It’s likely there is a better way to solve the larger problem this is a part of, but this Python will take a PolyCurve and join it to an adjacent PolyCurve by drawing the line from the end point of the first Polycurve to the closest point at parameter 0 or 1 of the second PolyCurve. It also has an optional boolean input to close the entire curve as needed, and will work with a series of PolyCurveas well.

Careful selection of the curves to make PolyCurve, or use of the GroupCurves node (from Archi-Lab) or your own method thereof should make this workable.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import PolyCurve, Geometry, Curve, Line

inCrvs = IN[0] #the curves from the Dynamo environment
closePoly = IN[1] #boolean indicated if the polycurve should be closed
joinedCurve = PolyCurve.ByJoinedCurves([inCrvs.pop()]) #take one curve out of the inCrvs list and convert it to a PolyCurve

while inCrvs: #while there are items in the inCrvs list
    endPnt = Curve.PointAtParameter(joinedCurve,1) #get the end point of the 
    dists = [Geometry.DistanceTo(endPnt,i) for i in inCrvs] #get the distance from the end of the initial curve to each curve still in the inCrvs list
    next = [x for y,x in sorted(zip(dists,inCrvs))][0] #gest the next curve from the in crvs list based on the shorted distance to the end point
    nextPnts = [ Curve.PointAtParameter(next,i) for i in [0,1] ] #get the start and end point from the next curve
    pntDists = [Geometry.DistanceTo(endPnt,i) for i in nextPnts] #get the distance from the end point of the init curve to the start and end point of the next cruve 
    nextPnt = [x for y,x in sorted(zip(pntDists,nextPnts))][0] #get the cloest of the next curves end points to the init curves endpoint
    newLine = Line.ByBestFitThroughPoints([endPnt,nextPnt]) #build a line from the closest end point of teh next curve to teh end point of the init curve
    joinedCurve = PolyCurve.ByJoinedCurves([joinedCurve,newLine,next]) #build a new polycurve from the init curve, next curve, and new line
    inCrvs.remove(next) #remove the next curve from the list of inCrvs

if closePoly: joinedCurve = PolyCurve.CloseWithLine(joinedCurve)

OUT = joinedCurve #returns the joined polycuve to the Dynamo environment
4 Likes