Terry, I think I confused you again. I was not using the flat length (chord length), but rather the distance across the flats - what you actually measure with a caliper across flats. You can call it the inscribed circle diameter otherwise.
Thanks Terry. Reason I did it with the distance across flats is that usually that would be the known parameter as dictated by standard wrench sizes. I can see though where the shaft diameter would be rather convenient. Anyway not a problem to derive one from another. What I do like in your approach is, as I mentioned, the option to do this in multiple passes.
Yes, the math with arcs is going to be painful. Interesting how it will work out.
Terry, thanks to your idea of utilizing the toolpath display I was able to debug the code and now have a perfect working version. It works very well. With my approach the minimal number of flats is 3. See attached final code. I have also cleaned it up a bit and added some more comments for readability. Works very well here and I am pleased.
By the way. Changed my code to work in degrees mode so that toolpath would display more meaningfully. See attached. It didn't occur to me previously, but this way the debugging is easier. Interestingly the toolpath produced by my code consists of almost straight lines, while yours of curved lines. So based on the fact that mine did produce flats somehow, I would assume that there is a problem with yours .