Join adjacent lines with same direction

Hi,
I have a polygon, with 5 lines. Two of the adjacent lines have same direction. I want to make an algorithm that joins every n numbers of adjacent + same direction lines.

Here for example line 2 and 3 should join and in result I get 0,1,2,3,4 lines. The most important thing is that the order of lines remain same. Order could shift but should remain in sequence.

I was thinking in terms of booleans and supply them to join lines.

Hope it helps.

# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
def direc(curve):
	p1=curve.StartPoint
	p2=curve.EndPoint
	vec=Vector.ByTwoPoints(p1,p2)
	vec=vec.Normalized()
	return vec
# Place your code below this line
lines=IN[0]

#to get same direction curves
pline=PolyCurve.ByJoinedCurves(lines)
lines=pline.Explode()

#collect points with angle
points=[]
for i in range(len(lines)):
	if i==len(lines)-1:
		j=0
	else:
		j=i+1
	l1=lines[i]
	d1=direc(l1)
	l2=lines[j]
	point=l2.StartPoint
	d2=direc(l2)
	if not d1.IsAlmostEqualTo(d2):
		points.append(point)

#make new organized lines
newlines=[]
for i in range(len(points)):
	if i==len(points)-1:
		j=0
	else:
		j=i+1
	p1=points[i]
	p2=points[j]
	line=Line.ByStartPointEndPoint(p1,p2)
	newlines.append(line)
# Assign your output to the OUT variable.
OUT = newlines
3 Likes

crn1 = pgn1.Corners();
crn2 = List.ShiftIndices(crn1,[1,0]);
drn1 = Vector.ByTwoPoints(crn2[0],crn2[1]);
drn2 = List.ShiftIndices(drn1,[1,0]);
bln1 = drn2[0].IsAlmostEqualTo(drn2[1]);
bln2 = List.ShiftIndices(bln1,-1);
crn3 = List.FilterByBoolMask(crn1,bln2)["out"];
pgn2 = Polygon.ByPoints(crn3);

rmvColnrPnts.dyn (12.0 KB)

3 Likes

Hello, here is another possibility

cordially
christian.stan
9 juillet forum anglais.dyn (23.2 KB)

2 Likes

Hello,
with itertools.groupby Python library

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
from itertools import groupby

plg = IN[0]
new_pt = []

for key_, group_ in groupby(plg.Curves(), key = lambda l : l.Direction):
    # get the first point of curve from each group
    new_pt.append(next(group_).StartPoint)
    
newplg = Polygon.ByPoints(new_pt)
OUT = newplg, newplg.Points
4 Likes

Thanks guys, I am trying these methods now. I started with @c.poupin method since it seemed more efficent.
I am getting this error. I am feeding an 8 sided polygon.

Hello @hassan.orion

because you probably use an old version of Dynamo
try this version

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
from itertools import groupby

plg = IN[0]
new_pt = []

for key_, group_ in groupby(plg.Curves(), key = lambda l : l.CoordinateSystemAtParameter(0.5).YAxis):
    # get the first point of curve from each group
    new_pt.append(next(group_).StartPoint)
    
newplg = Polygon.ByPoints(new_pt)
OUT = newplg, newplg.Points
1 Like

I think my post needs a bit more clarification.

My method of creating polygon is

  1. join lines if they are adjacent and unidirectional
  2. find their intersection points
  3. trim lines through these points
  4. Rejoin into polygon.

Since I am moving lines, I have to extend them.They do not end at intersection points like it does in a regular polygon.In the given image, you could see the problem I am facing. I have reached stage C i.e, I can trim lines at intersection, but I lack moving from stage A to B.

Main problem:
I trim lines with a list of points. For that to work, I need a list of lines / polyline( incase of joined lines) that has the same order as point list. When I make polylines, they are shifted at the end of the list, hence the list structure does not correspond. (I am using SuperCurve node of bimorphe to join). Example

Trimming point list :p1, p2
Line list required : Polyline2, crv3
Line list i get : crv3, Polyline2.

Or - Is there any way to extract the index structure from one list and impost it to another list ? I would also like to include here that when lines are combined, the index structure also changes. For example.

present structure
0 [crv1, crv2]
1[crv3, crv4, crv5]
2[crv6]

required
0[pl1]
1[pl2]
2[crv6]