Loop that crash my computer

I have a list of point PT, Newlist of Point Clovknot[(i,j)] is select some of them by distance to test point.
in the Clovknot[(i,j)]. I run the loop to create lines. but when I add line 55. the computer crash. Does anyone know how to solve the problem.

question.pdf (143.9 KB)

import rhinoscriptsyntax as rs
def Main():
Pt = {}
Distance =
squknot = {}
SQCurve =
ISuqKnot =
JSuqKnot =
Clovknot = {}
IClovknot =
JClovknot =
DistClovknot = {}
TestPt = rs.GetObject(“select test point”, rs.filter.point)

for i in range(15):
    for j in range(15):
        x = i*2
        y = j*3
        z=0
        Pt[(i,j)] = (x,y,z)
    
for i in range (15):
    for j in range(15):
        if i-1>0:
            Distance = rs.Distance( Pt[(i,j)], Pt[(i-1),j]  )
            if Distance >1:
                Clovknot[(i,j)] = Pt[(i,j)]
                IClovknot.append(i)
                JClovknot.append(j)
                DistClovknot[(i,j)] = rs.Distance(Clovknot[(i,j)], TestPt)
                
IMAXClovknot = max(IClovknot)
IMIMClovknot = min(IClovknot)
JMAXClovknot = max(JClovknot) 
JMIMClovknot = min(JClovknot)

#get the index of the smallest value
DistClovknotLIST = zip(DistClovknot.values(),DistClovknot.keys())
DistClovknotLIST.sort()
#the I and J value of the distance,
ClosestPtIJ = DistClovknotLIST[0][1]

CLOVECurve = CLOVKNOT(Clovknot,ClosestPtIJ,IClovknot,JClovknot,IMAXClovknot,IMIMClovknot,JMAXClovknot,JMIMClovknot)

def CLOVKNOT(POINT,CLOSTPT,ICLOVknot,JCLOVknot,IMAX,IMIM,JMAX,JMIM):
# get the I,J value of the startpoint
i = CLOSTPT[0]
j = CLOSTPT[1]
Curve =
if i >= IMIM and j >= JMIM and i <= IMAX and j<= JMAX:

    if i-1 in ICLOVknot and j-1 in JCLOVknot:
        NewPtIJ2 = (i-1,j-1)
        Curve.append(rs.AddLine( POINT[(i,j)],POINT[(i-1,j-1)] ))
        CLOVKNOT(POINT,NewPtIJ2,ICLOVknot,JCLOVknot,IMAX,IMIM,JMAX,JMIM) #  **crash happen when add this line**
    
    if i+1 in ICLOVknot and j+1 in JCLOVknot:
        NewPtIJ = (i+1,j+1)
        Curve.append(rs.AddLine( POINT[(i,j)],POINT[(i+1,j+1)] ))
        CLOVKNOT(POINT,NewPtIJ,ICLOVknot,JCLOVknot,IMAX,IMIM,JMAX,JMIM) 

Main()

It looks like the function CLOVKNOT calls itself over and over leading to infinite recursion. This will give you a stackoverflow and a crash.

I have a similar problem,

If I try something really simple as rotating a line for an infinite time :

import rhinoscriptsyntax as rs

line = rs.GetObject(“line”,4)
start = rs.CurveStartPoint(line)
t = 0
while 1 :
…line = rs.RotateObject(line,start,1)
…t+=1
…print t

I got a crash around 692.

Therefore, if I replace this while loop by a very long for loop such as :

for i in range(10000):
…line = rs.RotateObject(line,start,1)
…print i

it also freezes around 700 (more like 750, but I am not sure this is the point) but still compute the 10000 and prints “9999” with the line at the correct angle after a few seconds.

…Of course, my point isn’t to create an infinite loop, more something like “as long as this line isn’t perpendicular with this one, rotate it a little bit”. But the smaller the rotation, the bigger chances it crashes.

Does anyone have an explanation? a Solution?

Thanks,
Sylvain

(If this post is in a wrong place I can create a new one…)

This has been discussed in a few threads like this one… There are some explanations from Steve in there.

In your case, since you didn’t disable the redraw - the processor is able to calculate way faster than the display is able to react. So the display update info is getting stored in a buffer somewhere and that is filling up. Also as you are using rhinoscriptsyntax, this is actually working on the document object itself, not virtual geometry, which is much slower - my guess is it’s also constantly trying to write to the file.

I would first disable the redraw and see if that helps. Then maybe I would try to re-engineer the script so that it is not rotating an object in the file, but rather using some math calculations to figure out the angle to be rotated, then rotate the geometry only once.

–Mitch

Hi Sylvain
Try the below version , it’s ok:

import rhinoscriptsyntax as rs
import Rhino

def rot(line,start):
    
    for i in range (10000):
        rs.RotateObject(line,start,1)
        print i    
        Rhino.RhinoApp.Wait()
line=rs.GetObject("get line")
start=rs.CurveStartPoint(line)
rot(line,start)

Ciao Vittorio

Many thanks to both of you !

Good to know, any chance to slow down the calculation? (that’s not what the RhinoApp.Wait() from @vittorio’s script is doing? not sure to understand the “Pauses to keep Windows message pump alive so views will update and windows will repaint.” from the rhinocommon.chm ).

My goal was to try a processing like approach (setup & draw) with python and rhino. The non iterative would have work for this example but my final problem (bending some beams in an odd way) seem to be a non linear system… I am actually not sure, maybe it’ll worth the time to figure it out once for all!

indeed! thanks :slight_smile:

edit : even works with an infinite while loop and an escape instead of a for loop!
Sylvain.