Grasshopper python command combining two if commands

Hi There,

I have a phyton command in grasshopper that is getting a list of y coordinates, x coordinates, and z coordinates. The command is looping through y and x coordinate values and checks ( if y[i] > y[i-1]+3 or x[i]> x[i-1]+3 ) then it adds A to the list. And if not it writes (‘G1’+ X[i]+ Y[i]+ E)

But I want the command to also loop through the z coordinates and check if each z coordinate value is bigger than its previous one (if z[i] > z[i-1] and i>0) Then (if true) I want it to add the followings to the list:

1- ‘; announce new layer; ’

2- ‘;—‘

3- ‘M756 S’ + L

4- ‘M790 ;execute any new layer actions’

5- ‘;—‘

6- ‘G1 Z’+ Z[i] + ‘F’ + f + ‘; move to next layer (1)’

7- ‘G1’+ ‘X’+ X[i] + ‘Y’+ y[i]+ ‘F’+ f+ ‘; move to first perimeter point’

And if not I want it to continue writing (‘G1’+ X[i]+ Y[i]+ E) (Which was doing before)

but I don’t know how to add the if command of Z to the if command that I already have got, (Or how to combine them)

Here is a picture:

And here is my file:
Question-x,y,z.gh (15.8 KB)

So the final result would be something like this:

I am stuck, I would really appreciate if someone could help me.

Is this what you were trying to do?

I also included a method where you find the X, Y and Z of the point within Python, just so its a little cleaner (with less wires to deal with).

I’m still pretty new to python, so I dont know if this is the most efficient way to do it.

Question-x,y,z_Rev2.gh (25.9 KB)

1 Like

Thanks for your reply, Yes that’s what I am trying to do.
The code was good, but it seems to have a problem.
As the Z coordinate value changes from 0 to 1.15, it works alright and inserts the following to the list:

1- ‘; announce new layer; ’

2- ‘;—‘

3- ‘M756 S’ + L

4- ‘M790 ;execute any new layer actions’

5- ‘;—‘

6- ‘G1 Z’+ Z[i] + ‘F’ + f + ‘; move to next layer (1)’

7- ‘G1’+ ‘X’+ X[i] + ‘Y’+ y[i]+ ‘F’+ f+ ‘; move to first perimeter point’

However, after that, it keeps inserting them to the list even though the z coordinate value is not changing and is still on 1.15 (After index number 26)

Here is the file:
Error.gh (16.5 KB)

It seems to have been a tolerance issue. Although the Z values seem to be matching at 3 decimal places, at some point there must have been a very small difference. Rounding the Z values before comparing them seems to have fixed it.

Error_Fixed__Hopefully.gh (21.3 KB)

1 Like

Thanks, yeah that seems to work, now. :blush: :blush:

Also, do you know any way that I can have the 3D points in order based on Z value?
For example, it first prints all the (x(i) Y(i) z(0)) in the list first, then all the (x(i) Y(i) z(1.15)) and then all the (x(i) Y(i) z(2.3)).

But what currently it is doing, is that it prints all the (x(i), Y(i), z(0)) of curve 1 then (x(i), Y(i), z(1.15)) of curve 1 then (x(i), Y(i), z(2.3)) of curve 1
Then it starts printing the (x(i), Y(i), z(0)) of curve 2 then (x(i), Y(i), z(1.15)) of curve 2 then (x(i), Y(i), z(2.3)) of curve 2

See my reply here for two general methods for sorting in Python based on keys (you’ll want method 1 likely):

Also, note that a “safe” method for checking if two values (or points) are identical, is to subtract the two and check that the result is smaller than some acceptable tolerance value (for points, calculate the distance between the two and check this is smaller than your tolerance).

2 Likes

Thanks for your reply,

Can I ask which grasshopper function is the highlighted function? (in the image)

1 Like

Move

SortedCurves = sorted(curvess, key=lambda crv: crv.PointAt(0).Z)

Am I doing It wrong?

SortingPolylines.gh (11.1 KB)

Think it was just a mispelled variable name:

180829_SortingPolylines_Fixed.gh (10.4 KB)