Python - Flip Referenced Curve?

Hello,

I have a python script I am working on that checks the orientation of a curve and flips it either clockwise or counterclockwise depending on a boolean value.

Everything is working except I can’t get the actual curves themselves flipped.

I realized the .reverse() method actually returns True or False if a curve was flipped but how do I actually flip the curve itself?

I found this topic but I am specifically trying to flip referenced curves in GH that may or may not have GUIDs from the Rhino model.

Do I flip the domain based on the true/false value?

I feel like I’m overlooking a really obvious fact. Thanks for the help!

Graph Space:

Code:

def orient_curves_consistently(C, B):
    Fc = []
    O = []
    F = []

    if C is not None:
        for curve in C:
            orientation = str(curve.ClosedCurveOrientation())
            O.append(orientation)

            #If B is True Flip All Curves CounterClockwise
            if B: 
                if orientation == "Clockwise":
                    reversed_curve = curve.Reverse()
                    Fc.append(reversed_curve)
                    F.append(True)  # Curve was flipped
                else:
                    Fc.append(curve)
                    F.append(False)  # Curve was not flipped
            else:
                if orientation == "CounterClockwise":
                    reversed_curve = curve.Reverse()
                    Fc.append(reversed_curve)
                    F.append(True)  # Curve was flipped
                else:
                    Fc.append(curve)
                    F.append(False)  # Curve was not flipped

    return Fc, O, F

if __name__ == "__main__":
    Fc, O, F = orient_curves_consistently(C, B)

20230823_Flip_Referenced_Curves_01a.gh (11.9 KB)

1 Like

The curve is reversed in place, it does not return a new curve. If you need both you can duplicate the curve first.

2 Likes

That makes more sense, thank you I’ll attempt that fix

I guess you’ve got this already. I usually use do this without Python, as shown in the white group below. The gray group shows the start tangent vector (direction) of each curve, before or after flipping.


Flip_Referenced_Curves_2023Aug22a.gh (19.2 KB)

I fixed your Python like this:

def orient_curves_consistently(C, B):
    Fc = []
    O = []
    F = []

    if C is not None:
        for curve in C:
            orientation = str(curve.ClosedCurveOrientation())
            O.append(orientation)

            flip = (orientation == "Clockwise") == B
            F.append(flip)
            if flip:
                curve.Reverse()
            Fc.append(curve)

    return Fc, O, F
2 Likes

Thank you so much @Joseph_Oster ,

That works flawlessly and while I did create a cluster definition that orients curves by a guide circle (idea I got from your post) and that worked great, I was hoping to have the function wrapped in a single node and be able to do some more info related stuff with the output.

Much appreciated!

I neglected to mention that Flip Curve may fail unless the circle is created using the same plane as the curves you want to flip. And that non-planar curves are a different ball game. Perhaps self-intersecting curves as well, like figure 8s? Nothing wrong with Python.

1 Like

Good points to consider, thank you!

For my current use case I’m mostly ensuring consistent CounterClockwise orientation prior to executing various offset operations.

It appears in Rhino that the orientation is derived from the direction you draw the curve in (for polylines at least). So I realized myself, and others, accidentally draw orientation mismatched curves here and there and I’d like to “accept the user error” and then just account for it by applying a uniform orientation to them. (As this script now achieves)

1 Like

Yep! But we don’t always know how curves are drawn so this kind of conditional flipping is common. Typically I use one guide curve circle for each curve I want to test / flip. If I don’t know the base plane for each curve, I divide each curve into three points and use them to create a plane for the circle.

1 Like

Oh nice, a 3 pt plane is a good idea for that. Appreciate the knowledge share!