Chamfer two untouched curves with python

Hi, i’m trying to make a script for chamfer with sharp corner between two untouched curves (Linear Curve), so my thought is to first extend the curves with recursion until they touch and have the intersection parameter to split and keep the wanted curves.

So far i’m trying to get the CurveCurveIntersection() point with no luck, i’m a starter in python so maybe i’m doing something wrong with recursion, the code is this:

import rhinoscriptsyntax as rs

if crvA and crvB:

def extCrv(d):
	extCrvA = rs.ExtendCurveLength(crvA, 0, 2, d)
	extCrvB = rs.ExtendCurveLength(crvB, 0, 2, d)
	intPnt = rs.CurveCurveIntersection(extCrvA, extCrvB, tolerance=0)
	lenCrvA = rs.CurveLength(extCrvA)
	lenCrvB = rs.CurveLength(extCrvB)
	if intPnt == None:
	res = [extCrvA, extCrvB, intPnt, lenCrvA, lenCrvB]
	return res

crv = extCrv(0)

The problem is that the crv output give me the two correct extended curves but not the intersection point (“null”), and for demonstration purposes, the curve lengths are wrong to.
I’m not sure if recursion is the right way to do it because i try loops and if statements and can’t find the way.
It seems that the intersection point and lengths are not updating to the last recursion operation but why does the curves geometry are?

Can anyone help with the right way to code it?
Thanks! (10.1 KB)

Hi @constructoradicons,

There’s no need for recursion here, if I understand you correctly! You can simply use rs.LineLineIntersection() to find the intersection point of two lines. This only works for lines though and only if both lines are not parallel. Parallel lines have no intersection!

import rhinoscriptsyntax as rs

def extend_lines(line_a, line_b):
    """Extends two lines to a common intersection point.
      line_a: A first line to extend.
      line_b: A second line to extend.
      The extended lines, or None is both input lines are parallel.
    # Intersect both lines
    intersect = rs.LineLineIntersection(line_a, line_b)
    if intersect != None:
        # Get the intersection points on both lines
        int_pt_a = rs.AddPoint(intersect[0])
        int_pt_b = rs.AddPoint(intersect[1])
        # Find the line a end point that is not extended
        dist_a1 = rs.Distance(int_pt_a, rs.CurveStartPoint(line_a))
        dist_a2 = rs.Distance(int_pt_a, rs.CurveEndPoint(line_a))
        if dist_a1 > dist_a2:
            start_pt_a = rs.CurveStartPoint(line_a)
            start_pt_a = rs.CurveEndPoint(line_a)
        # Find the line b end point that is not extended
        dist_b1 = rs.Distance(int_pt_b, rs.CurveStartPoint(line_b))
        dist_b2 = rs.Distance(int_pt_b, rs.CurveEndPoint(line_b))
        if dist_b1 > dist_b2:
            start_pt_b = rs.CurveStartPoint(line_b)
            start_pt_b = rs.CurveEndPoint(line_b)
        # Construct the new lines
        ext_line_a = rs.AddLine(start_pt_a, int_pt_a)
        ext_line_b = rs.AddLine(start_pt_b, int_pt_b)
        return ext_line_a, ext_line_b
Lines = extend_lines(LineA, LineB) (3.8 KB)

This might be off topic, but I think that you don’t really understand what recursion refers to!

Thanks a lot for the reply!
I’m sorry for my bad example, I was trying to set the intersection of curves instead of lines because I was considering curved chamfers by arc or smooth extensions intersecting, anyway this code help me a lot due to finding the end point line that is no extended, didn’t figured that out.
Best regards!

This is two similar ones I wrote before, but I am not satisfied! Maybe it can help you a little! (5.1 KB) (10.5 KB)

Thanks @zhangvip0755, can’t work with the file because i’m in mac and there’s no ghpythonlib support here but i’ll try the other file, have to help with something.
Best regards!

Hi @constructoradicons,

Here’s a new Python script that can chamfer Nurbs curves.
It currently only works with curves that aren’t intersecting, though. It works similarly to the Rhino chamfer command, which first extends and than chamfers the extended curves.


Extending smooth, by line, or arc is also implemented. It is by default set to arc, but can be changed in the script.
The reason why the script currently doesn’t work for curves that are intersecting is, because the extensions are achieved by gradually extending both curves, until they intersect. However, if the input curves already intersect in the first place, no extension happens and thus no chamfering.
This should be pretty straightforward to implement though. I’m just too tired to come up with a watertight logic.

Furthermore, the script works on Rhino for Mac! I’m a Mac user too. :slight_smile: (10.1 KB)

Wow that’s a great code man… thanks so much for all that teaching, I apologize for my delayed reply but have to be so thankful for your support.
My deal was to make the chamfer curves with a sharp touch and even work with touched input curves but still, it’s a great extra component for my library.
Really appreciate it, regards!

1 Like