There are two main parts to this project. One is the function that makes a list of triangles that a gear is made of, and the other function converts that to an STL. The triangles -> STL part was easy, just look up the format on wikipedia, and find out that the only hard part (calculating a normal) was really easy with the library i’m using.
The main function was the code that generated the gear. The way I do this is by first making a list of points around the gear that the teeth are made of (these are the points between the pokey triangle bits in the image below). Then I iterate over this list and get each sliding window of two points. For each of these, I make two triangles for the top (tooth and inner part), two for the bottom, two for the left side of the tooth (it’s a quad, which is two triangles), and two for the right side.
My first iteration I had some issues, because I didn’t understand how the rotation function in the library i was using worked (i even filed a bug, and then realized that how i thought it worked doesn’t even make sense). I also was calling angleTo, which it turns out is the absolute value of the angle and I really should be calling angleToSigned. Eventually it looked mostly okay, except some triangles were being shown only from the opposite direction as I wanted. I realized this was due to the fact that my triangles weren’t following the right-hand rule, where the vertices needed to be listed in counter-clockwise order. After doing that it was almost entirely wrong, which it turns out was because I thought Z was up, when in fact it was down. Finally I got a reasonable solid, and after tweaking some settings i got the below gear (60T, inner radius 1, outer radius .1, thickness 0.2).