Offset surface until intersect loop

I am trying to offset a planar surface until it reaches an intersection with a set of curves. The while loop (line 287) seems to get stuck after one offset iteration and after letting it sit for a very long time I see no advancement.

testMRIcombine.py (10.5 KB)

Hi @yfhudak,

Python code, like most other programming languages, is read from top to bottom by the compiler, while executing the code.
You have put your exit variable cintersectlist = rs.CurveSurfaceIntersection(scurvelist, cptplstart), above the while loop, and thus the intersection check is only called once, even before the while loop runs for the first time.
This means that cintersectlist always remains None, is never updated, and since your while loop relies on it not being None to stop looping, it never stops, and your script gets stuck in an infinite loop,

This should work better:

cintersectlist = None

while cintersectlist is None:
    rs.OffsetSurface(cptplstart,dsi)
    cintersectlist = rs.CurveSurfaceIntersection(scurvelist, cptplstart)

Before the while loop, cintersectlist is None, since it has not been checked yet. Each iteration the while loop runs, it first checks if cintersectlist is still None, before performing any operations. Inside the loop, you first offset and then you check for intersections and update your variable that lives outside.

1 Like

Hi @diff-arch, thank you for the reply. I tried your suggested code but I am getting the same behavior.

You’re welcome!

There seem to be two more problems, it’s hard to tell though, since you haven’t provided any files to test the script with.
One issue, is that the dsi variable currently still remains constant at 1 during the looping, which again causes an infinite loop! At every iteration, the surface is offset by 1 and never intersects the curve. In order to remedy this, you need to increment dsi at the end of the while loop by a desired value (e.g. 1).

Also, in order to definitely prevent infinite loops during the development phase of your script, it is always wise to introduce some sort of counter that exits the loop at a given maximum iterations.

dsi = 1 # start offset distance
cintersectlist = None

max_count = 15 # maximum number of allowed iterations
count = 0 # current iteration count

while cintersectlist is None and count < max_count:
    rs.OffsetSurface(cptplstart, dsi)
    cintersectlist = rs.CurveSurfaceIntersection(scurvelist, cptplstart)
    dsi += 1 # increment dsi by a desired value (e.g. 1)
    count += 1 # increment count by 1

Furthermore, an entirely different issue might still arise. Since, surfaces are offset in their normal direction, it could be that your surface is offset in the wrong direction, and thus never intersects the curve.
If this is an issue, you can simply get a single surface normal vector and construct the vector between a surface point and its closest point on the curve. If the angle between both vectors is smaller than 90° or bigger than 270° (in radians), your surface is good to go, but if not you need to flip it, before offsetting.