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)
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)
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.
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).