System Crash enabling command in python

I am having problems enabling this command in python.
I don’t understand why my system crashes
Versión 7 SR37 (7.37.24107.15001, 2024-04-16)
thanks in advance

Blockquote
“”“Provides a scripting component.
Inputs:
C: Curbe
x: The x Snap variable
y: The y Snap variable
P: Plan on redraw Curve wwith Snap
Output:
a: The a output variable”“”

Blockquote
author = “Jordi Nebot”
version = “2024.07.20”
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
print “0”
def generate_stepped_curve(curve, plane, x_step, y_step):
# Convertir la curva de entrada a una curva de Rhino.Geometry
curve = rs.coercecurve(curve)
plane = rs.coerceplane(plane)
print “A”
# Inicializar lista de puntos para la nueva curva escalonada
points =

# Obtener el primer punto de la curva en el plano
current_point = plane.ClosestPoint(curve.PointAtStart)
print "AA"
# adaptamos al snap XY
nX = round(current_point.X / x_step)
nY = round(current_point.Y / y_step)
current_point = rg.Point3d(nX * x_step, nY * y_step, current_point.Z)
print "AAA"
points.append(current_point)
print "B"
fer = True
i = 0
#while fer:
while True:
    i=i+1
    print i
    # Obtener el parámetro actual en la curva basado en el punto actual
    success, parameter = curve.ClosestPoint(current_point)
    if not success:
        fer = False
        break
    
    # Punto actual en la curva
    point_on_curve = curve.PointAt(parameter)

    # Calcular los siguientes puntos en base a los pasos x_step y y_step desde el origen del plano
    next_Hpt = rg.Point3d(current_point.X + x_step, current_point.Y, current_point.Z)
    befo_Hpt = rg.Point3d(current_point.X - x_step, current_point.Y, current_point.Z)
    next_Vpt = rg.Point3d(current_point.X, current_point.Y + y_step, current_point.Z)
    befo_Vpt = rg.Point3d(current_point.X, current_point.Y - y_step, current_point.Z)
    
    # Proyectar los puntos al plano
    next_Hpt = plane.PointAt(next_Hpt.X, next_Hpt.Y)
    befo_Hpt = plane.PointAt(befo_Hpt.X, befo_Hpt.Y)
    next_Vpt = plane.PointAt(next_Vpt.X, next_Vpt.Y)
    befo_Vpt = plane.PointAt(befo_Vpt.X, befo_Vpt.Y)
    
    # Puntos cercanos a la curva
    next_HptCPt = curve.ClosestPoint(next_Hpt)[1]
    next_HptCP = curve.PointAt(next_HptCPt)
    befo_HptCPt = curve.ClosestPoint(befo_Hpt)[1]
    befo_HptCP = curve.PointAt(befo_HptCPt)
    next_VptCPt = curve.ClosestPoint(next_Vpt)[1]
    next_VptCP = curve.PointAt(next_VptCPt)
    befo_VptCPt = curve.ClosestPoint(befo_Vpt)[1]
    befo_VptCP = curve.PointAt(befo_VptCPt)

    # Determinar cuál de los 4 puntos está más cerca de la curva
    distance_to = 1000000
    if distance_to > next_HptCP.DistanceTo(next_Hpt) and next_Hpt != current_point:
        next_point = next_Hpt
        distance_to = next_HptCP.DistanceTo(next_Hpt)
    if distance_to > befo_HptCP.DistanceTo(befo_Hpt) and befo_Hpt != current_point:
        next_point = befo_Hpt
        distance_to = befo_HptCP.DistanceTo(befo_Hpt)
    if distance_to > next_VptCP.DistanceTo(next_Vpt) and next_Vpt != current_point:
        next_point = next_Vpt
        distance_to = next_VptCP.DistanceTo(next_Vpt)
    if distance_to > befo_VptCP.DistanceTo(befo_Vpt) and befo_Vpt != current_point:
        next_point = befo_Vpt
        distance_to = befo_VptCP.DistanceTo(befo_Vpt)
    
    points.append(next_point)
    current_point = next_point
    
    # Verificar si hemos llegado al final de la curva
    if parameter >= curve.Domain[1]:
        fer = False
        break

# Crear la polilínea escalonada a partir de los puntos generados
stepped_curve = rg.Polyline(points).ToNurbsCurve()

return stepped_curve

Blockquote# Obtener entradas desde Grasshopper
#curve_A = rs.coercecurve(C)
#plane_P = rs.coerceplane(P)
print “00”
curve_A = C
plane_P = P
x_step = x
y_step = y
print “000”

#Generar la curva escalonada
stepped_curve = generate_stepped_curve(curve_A, plane_P, x_step, y_step)

#Salida para Grasshopper
a = stepped_curve

SnappingCurve.gh (9.1 KB)

always be careful with While True loops,

most likely the condition never turns false. See if you can implement an escape (e.g. if i>100)

Thanks Gijs,
I thought about it, but in test mode, I don’t see any of the control prints

well, the issue you post here is that your system crashes, and the if i> … break solves it
So the next question is, what problem are you trying to solve? It’s not clear to me after briefly looking at the code.

The objective is to retrace a curve following the snap rule (x value, y value), according to a given plane

The next point will be one of the 4 possible points depending on the minimum distance to curve C.

Is something like this your goal?

exactly !!

SnappingCurve.gh (14.5 KB)

Note that the above is a bit of a hack, I am quite sure someone has figured this out in script before.

Thanks Gijs !!
Now I understand the : (e.g. if i>100)
if i>1000:
break
It will be a great help to fine-tune my script, which I see goes back to the same point.
I hope to find it soon
Thanks for the basic Grasshopper path.
…Although for my level it is a much higher use of Cull and Series
I hope to leave the solution here soon.

Hello again Gijs!
I have attached an updated routine, I need to adjust the change in plane orientation (later…). I think it works well, the route obtained is similar to yours, only differences in points where the distance is the same for the two possible paths.
The rotation of the Snap XY orientation does not work because I suppose I do not do the conversion well, or in a timely manner.
Thank you so much!!
SnappingCurve-v2.gh (35.8 KB)

Dear Gijs,
I have finished the routine of redrawing a curve by snapping x and y. You can find my solution attached. There are some minor bugs, especially in extreme situations: when a possible point is close to the curve at two points, …I would say it works in 90% of the cases.
SnappingCurve-v21.gh (39.0 KB)

Dear Gijs,
I have made some improvements to my GHpython, and I have allowed myself to adapt your GH by achieving a variation of the Sanp in X and Y, and moving the origin of the generating plane and its angle.
It has been a very interesting exercise.
Thanks again.
SnappingCurve-v30.gh (64.9 KB)