formacad 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/LICENSE +200 -0
  2. package/README.md +98 -2
  3. package/build/cli/src/forma.d.ts +2 -0
  4. package/build/cli/src/forma.js +546 -0
  5. package/build/cli/src/forma.js.map +1 -0
  6. package/build/cli/src/mcp.d.ts +1 -0
  7. package/build/cli/src/mcp.js +495 -0
  8. package/build/cli/src/mcp.js.map +1 -0
  9. package/build/cli/src/preview.d.ts +31 -0
  10. package/build/cli/src/preview.js +427 -0
  11. package/build/cli/src/preview.js.map +1 -0
  12. package/build/core/fonts/Roboto-Bold.ttf +0 -0
  13. package/build/core/fonts/Roboto-Regular.ttf +0 -0
  14. package/build/core/src/bevel.d.ts +41 -0
  15. package/build/core/src/bevel.js +548 -0
  16. package/build/core/src/bevel.js.map +1 -0
  17. package/build/core/src/difflib.d.ts +3 -0
  18. package/build/core/src/difflib.js +94 -0
  19. package/build/core/src/difflib.js.map +1 -0
  20. package/build/core/src/errors.d.ts +9 -0
  21. package/build/core/src/errors.js +14 -0
  22. package/build/core/src/errors.js.map +1 -0
  23. package/build/core/src/export2d.d.ts +10 -0
  24. package/build/core/src/export2d.js +127 -0
  25. package/build/core/src/export2d.js.map +1 -0
  26. package/build/core/src/geometry.d.ts +93 -0
  27. package/build/core/src/geometry.js +946 -0
  28. package/build/core/src/geometry.js.map +1 -0
  29. package/build/core/src/gltf.d.ts +6 -0
  30. package/build/core/src/gltf.js +154 -0
  31. package/build/core/src/gltf.js.map +1 -0
  32. package/build/core/src/host.d.ts +28 -0
  33. package/build/core/src/host.js +113 -0
  34. package/build/core/src/host.js.map +1 -0
  35. package/build/core/src/importers.d.ts +12 -0
  36. package/build/core/src/importers.js +1025 -0
  37. package/build/core/src/importers.js.map +1 -0
  38. package/build/core/src/index.d.ts +92 -0
  39. package/build/core/src/index.js +227 -0
  40. package/build/core/src/index.js.map +1 -0
  41. package/build/core/src/inflate.d.ts +15 -0
  42. package/build/core/src/inflate.js +230 -0
  43. package/build/core/src/inflate.js.map +1 -0
  44. package/build/core/src/interp.d.ts +178 -0
  45. package/build/core/src/interp.js +3012 -0
  46. package/build/core/src/interp.js.map +1 -0
  47. package/build/core/src/lexer.d.ts +14 -0
  48. package/build/core/src/lexer.js +217 -0
  49. package/build/core/src/lexer.js.map +1 -0
  50. package/build/core/src/manifold.d.ts +26 -0
  51. package/build/core/src/manifold.js +687 -0
  52. package/build/core/src/manifold.js.map +1 -0
  53. package/build/core/src/modeling.d.ts +18 -0
  54. package/build/core/src/modeling.js +483 -0
  55. package/build/core/src/modeling.js.map +1 -0
  56. package/build/core/src/parser.d.ts +46 -0
  57. package/build/core/src/parser.js +478 -0
  58. package/build/core/src/parser.js.map +1 -0
  59. package/build/core/src/scad/colors.d.ts +1 -0
  60. package/build/core/src/scad/colors.js +37 -0
  61. package/build/core/src/scad/colors.js.map +1 -0
  62. package/build/core/src/scad/convert.d.ts +33 -0
  63. package/build/core/src/scad/convert.js +2842 -0
  64. package/build/core/src/scad/convert.js.map +1 -0
  65. package/build/core/src/scad/parser.d.ts +239 -0
  66. package/build/core/src/scad/parser.js +578 -0
  67. package/build/core/src/scad/parser.js.map +1 -0
  68. package/build/core/src/scad/sources.d.ts +39 -0
  69. package/build/core/src/scad/sources.js +171 -0
  70. package/build/core/src/scad/sources.js.map +1 -0
  71. package/build/core/src/std.d.ts +3 -0
  72. package/build/core/src/std.js +47 -0
  73. package/build/core/src/std.js.map +1 -0
  74. package/build/core/src/surface.d.ts +21 -0
  75. package/build/core/src/surface.js +253 -0
  76. package/build/core/src/surface.js.map +1 -0
  77. package/build/core/src/threemf.d.ts +4 -0
  78. package/build/core/src/threemf.js +127 -0
  79. package/build/core/src/threemf.js.map +1 -0
  80. package/build/core/src/ttf.d.ts +39 -0
  81. package/build/core/src/ttf.js +473 -0
  82. package/build/core/src/ttf.js.map +1 -0
  83. package/build/core/src/values.d.ts +146 -0
  84. package/build/core/src/values.js +365 -0
  85. package/build/core/src/values.js.map +1 -0
  86. package/build/core/src/xml.d.ts +13 -0
  87. package/build/core/src/xml.js +152 -0
  88. package/build/core/src/xml.js.map +1 -0
  89. package/build/core/std/electronics.forma +120 -0
  90. package/build/core/std/enclosures.forma +76 -0
  91. package/build/core/std/fasteners.forma +170 -0
  92. package/build/core/std/gridfinity.forma +182 -0
  93. package/build/core/std/joints.forma +120 -0
  94. package/build/core/std/mechanics.forma +276 -0
  95. package/build/core/std/patterns.forma +138 -0
  96. package/build/core/std/threads.forma +152 -0
  97. package/package.json +59 -4
@@ -0,0 +1,182 @@
1
+ // std/gridfinity.forma — Modular storage systems: Gridfinity bins & stepped baseplates, IKEA Skadis hooks, DIN rail clips.
2
+ // Usage: import "std/gridfinity.forma" as gf
3
+
4
+ let pitch = 42mm
5
+ let unitHeight = 7mm
6
+
7
+ // Single 42x42mm Gridfinity stepped base foot with optional 6x2mm corner magnet pockets.
8
+ // Matches Baseplate() stepped 45-degree cavity with 0.2mm radial clearance.
9
+ component BaseFoot(magnets: Bool = true) {
10
+ Difference {
11
+ Union {
12
+ Box(width: 36.6mm, depth: 36.6mm, height: 2.6mm)
13
+ .fillet(r: 1.6mm, edges: .vertical)
14
+ .chamfer(0.8mm, edges: .bottom)
15
+ Hull {
16
+ Box(width: 36.6mm, depth: 36.6mm, height: 0.2mm)
17
+ .fillet(r: 1.6mm, edges: .vertical)
18
+ .at(z: 2.5mm)
19
+ Box(width: 40.9mm, depth: 40.9mm, height: 0.2mm)
20
+ .fillet(r: 3.75mm, edges: .vertical)
21
+ .at(z: 4.55mm)
22
+ }
23
+ }
24
+ if magnets {
25
+ for ix in 0..<2 {
26
+ for iy in 0..<2 {
27
+ Cylinder(d: 6.2mm, h: 2.2mm)
28
+ .at(x: (ix == 0 ? -1 : 1) * 13mm, y: (iy == 0 ? -1 : 1) * 13mm, z: -0.1mm)
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
34
+
35
+ // Gridfinity stepped 45-degree baseplate (x by y units)
36
+ component Baseplate(x: Number = 2, y: Number = 2) {
37
+ assert(x >= 1 && y >= 1, "Baseplate x and y must be at least 1")
38
+ let w = x * pitch
39
+ let d = y * pitch
40
+ let h = 4.65mm
41
+ Difference {
42
+ Box(width: w, depth: d, height: h).fillet(r: 4mm, edges: .vertical)
43
+ for ix in 0..<x {
44
+ for iy in 0..<y {
45
+ let cx = (ix - (x - 1) / 2) * pitch
46
+ let cy = (iy - (y - 1) / 2) * pitch
47
+ Union {
48
+ Hull {
49
+ Box(width: 35.6mm, depth: 35.6mm, height: 0.2mm)
50
+ .fillet(r: 1.2mm, edges: .vertical)
51
+ .at(z: -0.2mm)
52
+ Box(width: 37.0mm, depth: 37.0mm, height: 1.9mm)
53
+ .fillet(r: 1.8mm, edges: .vertical)
54
+ .at(z: 0.7mm)
55
+ }
56
+ Hull {
57
+ Box(width: 37.0mm, depth: 37.0mm, height: 0.2mm)
58
+ .fillet(r: 1.8mm, edges: .vertical)
59
+ .at(z: 2.4mm)
60
+ Box(width: 41.5mm, depth: 41.5mm, height: 0.2mm)
61
+ .fillet(r: 3.9mm, edges: .vertical)
62
+ .at(z: h)
63
+ }
64
+ }.at(x: cx, y: cy)
65
+ }
66
+ }
67
+ }
68
+ }
69
+
70
+ // Parametric Gridfinity storage bin with optional stacking lip, dividers, label shelf, and magnet pockets
71
+ component Bin(
72
+ x: Number = 1,
73
+ y: Number = 1,
74
+ z: Number = 3,
75
+ wall: Length = 1.6mm,
76
+ magnets: Bool = true,
77
+ dividersX: Number = 0,
78
+ dividersY: Number = 0,
79
+ stackingLip: Bool = true,
80
+ label: Bool = false
81
+ ) {
82
+ assert(x >= 1 && y >= 1 && z >= 2, "Bin requires x >= 1, y >= 1, z >= 2")
83
+ assert(wall >= 1mm, "Bin wall should be at least 1mm")
84
+ let outerW = x * pitch - 0.5mm
85
+ let outerD = y * pitch - 0.5mm
86
+ let footH = 4.75mm
87
+ let bodyH = z * unitHeight - footH
88
+ let innerW = outerW - 2 * wall
89
+ let innerD = outerD - 2 * wall
90
+ let floorZ = footH + 1.8mm
91
+ let lipH = stackingLip ? 3.2mm : 0mm
92
+
93
+ Union {
94
+ for ix in 0..<x {
95
+ for iy in 0..<y {
96
+ BaseFoot(magnets: magnets).at(
97
+ x: (ix - (x - 1) / 2) * pitch,
98
+ y: (iy - (y - 1) / 2) * pitch
99
+ )
100
+ }
101
+ }
102
+ Difference {
103
+ Box(width: outerW, depth: outerD, height: bodyH + lipH)
104
+ .fillet(r: 3.75mm, edges: .vertical)
105
+ .chamfer(0.5mm, edges: .top)
106
+ .at(z: footH)
107
+ // Main inner cavity
108
+ Box(width: innerW, depth: innerD, height: bodyH + lipH)
109
+ .fillet(r: 2.2mm, edges: .vertical)
110
+ .at(z: floorZ)
111
+ // Stepped stacking lip recess at top
112
+ if stackingLip {
113
+ Hull {
114
+ Box(width: innerW, depth: innerD, height: 0.2mm)
115
+ .fillet(r: 2.2mm, edges: .vertical)
116
+ .at(z: footH + bodyH - 0.2mm)
117
+ Box(width: outerW - 1.6mm, depth: outerD - 1.6mm, height: 0.2mm)
118
+ .fillet(r: 3.0mm, edges: .vertical)
119
+ .at(z: footH + bodyH + lipH)
120
+ }
121
+ }
122
+ }
123
+ // Optional internal dividers
124
+ let divH = bodyH - 2.4mm
125
+ if dividersX > 0 {
126
+ for i in 1...dividersX {
127
+ let dx = -innerW / 2 + i * (innerW / (dividersX + 1))
128
+ Box(width: 1.2mm, depth: innerD + 0.4mm, height: divH).at(x: dx, z: floorZ)
129
+ }
130
+ }
131
+ if dividersY > 0 {
132
+ for i in 1...dividersY {
133
+ let dy = -innerD / 2 + i * (innerD / (dividersY + 1))
134
+ Box(width: innerW + 0.4mm, depth: 1.2mm, height: divH).at(y: dy, z: floorZ)
135
+ }
136
+ }
137
+ // Optional 45-degree overhang-free label shelf on the +Y inner wall
138
+ if label {
139
+ let shelfD = 10mm
140
+ let shelfZ = footH + bodyH - 1.5mm
141
+ Hull {
142
+ Box(width: innerW, depth: shelfD, height: 1.4mm).at(y: innerD / 2 - shelfD / 2, z: shelfZ)
143
+ Box(width: innerW, depth: 1mm, height: 1mm).at(y: innerD / 2 - 0.5mm, z: shelfZ - shelfD)
144
+ }
145
+ }
146
+ }
147
+ }
148
+
149
+ // IKEA Skadis pegboard hook printed flat on its side in XY for maximum layer-line strength and zero supports
150
+ component SkadisHook(reach: Length = 22mm, thick: Length = 4.6mm) {
151
+ assert(reach >= 8mm && thick >= 3mm, "SkadisHook reach >= 8mm and thick >= 3mm")
152
+ // Continuous 2D profile in XY (back peg enters -X, drops -Y; front hook extends +X and curves +Y)
153
+ Polygon(points: [
154
+ [-8.5mm, 4mm],
155
+ [-5.3mm, 4mm],
156
+ [-5.3mm, 9.5mm],
157
+ [0mm, 9.5mm],
158
+ [0mm, 0mm],
159
+ [reach + 4mm, 0mm],
160
+ [reach + 4mm, 9mm],
161
+ [reach, 9mm],
162
+ [reach, 4.5mm],
163
+ [3.2mm, 4.5mm],
164
+ [3.2mm, 18mm],
165
+ [0mm, 18mm],
166
+ [0mm, 14mm],
167
+ [-8.5mm, 14mm]
168
+ ]).extrude(height: thick).chamfer(0.4mm, edges: .horizontal)
169
+ }
170
+
171
+ // Flexure snap-clip for standard 35mm top-hat DIN rail (EN 60715) with spring arm & release tab
172
+ component DinRailClip(width: Length = 15mm, thick: Length = 3mm) {
173
+ assert(width > 0mm && thick >= 2mm, "DinRailClip width > 0 and thick >= 2mm")
174
+ // Fixed hook on -X, flexible cantilevered latch + release tab on +X
175
+ Polygon(points: [
176
+ [-21mm, 0mm], [25mm, 0mm], [25mm, 2.2mm], [21.5mm, 2.2mm],
177
+ [21.5mm, 9.5mm], [16.5mm, 9.5mm], [16.5mm, 7.0mm], [18.5mm, 6.2mm],
178
+ [18.5mm, thick], [14mm, thick], [14mm, 1.5mm], [19.2mm, 1.5mm],
179
+ [19.2mm, thick + 0.4mm], [-18mm, thick + 0.4mm], [-18mm, 6.5mm],
180
+ [-16.5mm, 6.5mm], [-16.5mm, 9mm], [-21mm, 9mm]
181
+ ]).extrude(height: width)
182
+ }
@@ -0,0 +1,120 @@
1
+ // std/joints.forma — Screwless 3D-printable joints: tapered snap-fits, dovetails, print-in-place hinges, and alignment pins.
2
+ // Usage: import "std/joints.forma" as joints
3
+
4
+ // Tapered cantilever snap-fit clip with root reinforcement.
5
+ // Set flat: true to print lying in XY so continuous perimeter loops run along the beam (5x stronger in FDM).
6
+ component SnapFit(width: Length = 6mm, length: Length = 14mm, thick: Length = 2.0mm, hook: Length = 1.2mm, flat: Bool = false) {
7
+ assert(length > hook * 3 && thick >= 1mm, "SnapFit length must exceed 3 * hook")
8
+ let tipThick = thick * 0.55
9
+ let rootPad = thick * 0.45
10
+ let profile = Polygon(points: [
11
+ [-thick - rootPad, 0mm],
12
+ [rootPad, 0mm],
13
+ [0mm, rootPad * 1.4],
14
+ [0mm, length - hook * 2.2],
15
+ [hook, length - hook * 1.6],
16
+ [0mm, length],
17
+ [-tipThick, length],
18
+ [-thick, rootPad * 1.4]
19
+ ])
20
+ if flat {
21
+ profile.extrude(height: width)
22
+ } else {
23
+ profile.extrude(height: width, center: true)
24
+ .rotate(y: -90deg)
25
+ .rotate(z: 90deg)
26
+ }
27
+ }
28
+
29
+ // Matching receiver slot cutter for SnapFit() (use inside Difference)
30
+ component SnapSlot(width: Length = 6mm, catchH: Length = 3.2mm, depth: Length = 4mm, clearance: Length = 0.25mm) {
31
+ Box(width: width + 2 * clearance, depth: depth + 1mm, height: catchH + 2 * clearance)
32
+ }
33
+
34
+ // Dovetail male pin (centered in X, protruding along +Y from y = 0..depth)
35
+ component DovetailPin(width: Length = 12mm, depth: Length = 6mm, height: Length = 10mm, angle: Angle = 60deg, clearance: Length = 0.2mm) {
36
+ assert(angle > 30deg && angle < 85deg, "DovetailPin angle must be between 30deg and 85deg")
37
+ let flare = depth / tan(angle)
38
+ let w0 = width - 2 * clearance
39
+ let w1 = width + 2 * flare - 2 * clearance
40
+ Polygon(points: [
41
+ [-w0 / 2, 0mm],
42
+ [w0 / 2, 0mm],
43
+ [w1 / 2, depth - clearance],
44
+ [-w1 / 2, depth - clearance]
45
+ ]).extrude(height: height).chamfer(0.35mm, edges: .horizontal)
46
+ }
47
+
48
+ // Dovetail female slot cutter matching DovetailPin() (use inside Difference)
49
+ component DovetailSlot(width: Length = 12mm, depth: Length = 6mm, height: Length = 10mm, angle: Angle = 60deg, clearance: Length = 0.2mm) {
50
+ assert(angle > 30deg && angle < 85deg, "DovetailSlot angle must be between 30deg and 85deg")
51
+ let flare = depth / tan(angle)
52
+ let w0 = width + 2 * clearance
53
+ let w1 = width + 2 * flare + 2 * clearance
54
+ Polygon(points: [
55
+ [-w0 / 2, -0.5mm],
56
+ [w0 / 2, -0.5mm],
57
+ [w1 / 2, depth + clearance],
58
+ [-w1 / 2, depth + clearance]
59
+ ]).extrude(height: height + 1mm).at(z: -0.5mm)
60
+ }
61
+
62
+ // Print-in-place self-centering 45-degree double-cone knuckle hinge along the X axis.
63
+ // Flattened bottom gives solid build-plate adhesion; conical pins eliminate horizontal bridging.
64
+ component Hinge(length: Length = 36mm, d: Length = 8mm, leaf: Length = 12mm, thick: Length = 3mm, knuckles: Number = 3, clearance: Length = 0.35mm) {
65
+ assert(knuckles >= 3, "Hinge requires at least 3 knuckles")
66
+ assert(d >= 5mm && clearance >= 0.2mm, "Hinge d >= 5mm and clearance >= 0.2mm")
67
+ let segW = length / knuckles
68
+ let coneR = d * 0.32
69
+ let coneH = min(coneR, segW * 0.42)
70
+ let flatDrop = 0.4mm
71
+ let axisZ = d / 2 - flatDrop
72
+
73
+ Intersection {
74
+ Union {
75
+ for i in 0..<knuckles {
76
+ let x0 = -length / 2 + i * segW + clearance / 2
77
+ let w = segW - clearance
78
+ let side = (i % 2 == 0) ? 1 : -1
79
+ let isPinKnuckle = (i % 2 == 0)
80
+
81
+ if isPinKnuckle {
82
+ Union {
83
+ Cylinder(d: d, h: w).rotate(y: 90deg).at(x: x0, z: axisZ)
84
+ Box(width: w, depth: leaf, height: thick).at(x: x0 + w / 2, y: side * (leaf / 2 + d * 0.25))
85
+ if i > 0 {
86
+ Cone(r1: coneR, r2: 0.4mm, h: coneH).rotate(y: -90deg).at(x: x0 + 0.1mm, z: axisZ)
87
+ }
88
+ if i < knuckles - 1 {
89
+ Cone(r1: coneR, r2: 0.4mm, h: coneH).rotate(y: 90deg).at(x: x0 + w - 0.1mm, z: axisZ)
90
+ }
91
+ }
92
+ } else {
93
+ Difference {
94
+ Union {
95
+ Cylinder(d: d, h: w).rotate(y: 90deg).at(x: x0, z: axisZ)
96
+ Box(width: w, depth: leaf, height: thick).at(x: x0 + w / 2, y: side * (leaf / 2 + d * 0.25))
97
+ }
98
+ Cone(r1: coneR + clearance, r2: 0.4mm, h: coneH + clearance).rotate(y: 90deg).at(x: x0 - 0.1mm, z: axisZ)
99
+ Cone(r1: coneR + clearance, r2: 0.4mm, h: coneH + clearance).rotate(y: -90deg).at(x: x0 + w + 0.1mm, z: axisZ)
100
+ }
101
+ }
102
+ }
103
+ }
104
+ // Clip cleanly at z = 0 to create a flat bed-adhesion surface on every knuckle
105
+ Box(width: length + 4mm, depth: 2 * leaf + 2 * d + 4mm, height: d + 4mm)
106
+ }
107
+ }
108
+
109
+ // Alignment pin with tapered lead-in chamfer
110
+ component AlignmentPin(d: Length = 4mm, h: Length = 6mm) {
111
+ assert(d >= 1.5mm && h > 0mm, "AlignmentPin d >= 1.5mm and h > 0")
112
+ Cylinder(d: d, h: h).chamfer(min(0.6mm, d * 0.2), edges: .top)
113
+ }
114
+
115
+ // Matching alignment hole cutter with lead-in chamfer (use inside Difference)
116
+ component AlignmentHole(d: Length = 4mm, h: Length = 6mm, clearance: Length = 0.2mm) {
117
+ let effD = d + 2 * clearance
118
+ Cylinder(d: effD, h: h + 0.6mm).at(z: -0.1mm)
119
+ Cone(d1: effD, d2: effD + 0.8mm, h: 0.4mm).at(z: h - 0.3mm)
120
+ }
@@ -0,0 +1,276 @@
1
+ // std/mechanics.forma — Involute gears (spur, helical, herringbone, ring), racks, GT2 pulleys, bearings, shafts, and NEMA 17 mounts.
2
+ // Usage: import "std/mechanics.forma" as mech
3
+
4
+ // Pitch radius of a gear
5
+ func pitchRadius(teeth: Number, mod: Length = 2mm) -> Length {
6
+ assert(teeth >= 6, "gear must have at least 6 teeth")
7
+ assert(mod > 0mm, "gear module must be positive")
8
+ return teeth * mod / 2
9
+ }
10
+
11
+ // Exact center-to-center distance for two meshing external gears
12
+ func centerDistance(z1: Number, z2: Number, mod: Length = 2mm, backlash: Length = 0mm) -> Length {
13
+ (z1 + z2) * mod / 2 + backlash
14
+ }
15
+
16
+ // Phase rotation for the second meshing gear placed on +X so a tooth lands cleanly in a valley
17
+ func meshAngle(teeth: Number) -> Angle {
18
+ (teeth % 2 == 0) ? 180deg / teeth : 0deg
19
+ }
20
+
21
+ // Involute function inv(alpha) = tan(alpha) - alpha (in dimensionless radians)
22
+ func invAngle(a: Angle) -> Number {
23
+ tan(a) - (a / 180deg) * pi
24
+ }
25
+
26
+ // Polar angle delta (in radians) on an involute flank at radius r >= rBase
27
+ func invAtRadius(r: Length, rBase: Length) -> Number {
28
+ let ratio = r / rBase
29
+ let t = ratio <= 1 ? 0 : sqrt(ratio * ratio - 1)
30
+ return t - (atan(t) / 180deg) * pi
31
+ }
32
+
33
+ // 2D involute gear profile (external when internal: false, internal cutter when internal: true)
34
+ func gearPoints(teeth: Number, mod: Length, pressureAngle: Angle, backlash: Length, profileShift: Number, internal: Bool) -> List {
35
+ let pr = teeth * mod / 2
36
+ let rBase = pr * cos(pressureAngle)
37
+ let shift = profileShift * mod
38
+ let rRoot = max(0.5 * mod, pr - 1.25 * mod + shift)
39
+ let rTip = pr + 1.0 * mod + shift
40
+ let rStart = max(rRoot, rBase)
41
+ let stepRad = (2 * pi) / teeth
42
+ let invA = invAngle(a: pressureAngle)
43
+ // Backlash thins external teeth and widens internal tooth spaces
44
+ let blSign = internal ? -1 : 1
45
+ let halfPitch = (pi / 2 + 2 * profileShift * tan(pressureAngle) - blSign * (backlash / mod)) / teeth
46
+ let phiB = halfPitch + invA
47
+ let rootPad = min(0.04 * stepRad, max(0, stepRad / 2 - phiB - 0.01))
48
+
49
+ return stride(from: 0, to: teeth * 10, by: 1).map { k in
50
+ let i = floor(k / 10)
51
+ let s = k % 10
52
+ let cAngle = i * stepRad
53
+ let frac = (s >= 1 && s <= 4) ? (s - 1) / 3 : (s >= 5 && s <= 8) ? (8 - s) / 3 : 0
54
+ let r = (s == 0 || s == 9) ? rRoot : rStart + frac * (rTip - rStart)
55
+ let dTheta = (s == 0) ? -(phiB + rootPad)
56
+ : (s == 9) ? (phiB + rootPad)
57
+ : (s <= 4) ? -(phiB - invAtRadius(r: r, rBase: rBase))
58
+ : (phiB - invAtRadius(r: r, rBase: rBase))
59
+ let a = (cAngle + dTheta) * (180deg / pi)
60
+ return [r * cos(a), r * sin(a)]
61
+ }
62
+ }
63
+
64
+ // True involute spur, helical, or herringbone (double-helical) gear with FDM bore & elephant's-foot compensation
65
+ component SpurGear(
66
+ teeth: Number = 16,
67
+ mod: Length = 2mm,
68
+ thickness: Length = 6mm,
69
+ bore: Length = 5mm,
70
+ boreClearance: Length = 0.15mm,
71
+ elephantsFoot: Length = 0.3mm,
72
+ pressureAngle: Angle = 20deg,
73
+ helix: Angle = 0deg,
74
+ herringbone: Bool = false,
75
+ backlash: Length = 0.15mm,
76
+ profileShift: Number = 0,
77
+ hubD: Length = 0mm,
78
+ hubH: Length = 0mm
79
+ ) {
80
+ assert(teeth >= 6, "SpurGear requires at least 6 teeth")
81
+ assert(mod > 0mm && thickness > 0mm, "SpurGear mod and thickness must be positive")
82
+ let pr = pitchRadius(teeth: teeth, mod: mod)
83
+ let rRoot = pr - 1.25 * mod + profileShift * mod
84
+ let rTip = pr + 1.0 * mod + profileShift * mod
85
+ assert(bore < rRoot * 1.9, "SpurGear bore is too large for the root diameter")
86
+
87
+ let profile = Polygon(points: gearPoints(
88
+ teeth: teeth, mod: mod, pressureAngle: pressureAngle,
89
+ backlash: backlash, profileShift: profileShift, internal: false
90
+ ))
91
+
92
+ let rawGear = Union {
93
+ if herringbone && helix != 0deg {
94
+ let halfH = thickness / 2
95
+ let halfTwist = ((halfH * tan(helix)) / pr) * (180deg / pi)
96
+ let slices = max(6, ceil(abs(halfTwist) / 4deg))
97
+ let lower = profile.extrude(height: halfH, twist: halfTwist, slices: slices)
98
+ lower
99
+ lower.mirror(.z).at(z: thickness)
100
+ } else if helix != 0deg {
101
+ let twist = ((thickness * tan(helix)) / pr) * (180deg / pi)
102
+ let slices = max(8, ceil(abs(twist) / 4deg))
103
+ profile.extrude(height: thickness, twist: twist, slices: slices)
104
+ } else {
105
+ profile.extrude(height: thickness)
106
+ }
107
+ if hubD > bore && hubH > 0mm {
108
+ Cylinder(d: hubD, h: thickness + hubH).chamfer(0.4mm, edges: .top)
109
+ }
110
+ }
111
+
112
+ Difference {
113
+ if elephantsFoot > 0mm {
114
+ Intersection {
115
+ rawGear
116
+ Union {
117
+ Cone(r1: rTip - elephantsFoot, r2: rTip + 0.5mm, h: elephantsFoot + 0.5mm)
118
+ Cylinder(r: rTip + 0.5mm, h: thickness + hubH + 1mm).at(z: elephantsFoot)
119
+ }
120
+ }
121
+ } else {
122
+ rawGear
123
+ }
124
+ if bore > 0mm {
125
+ Cylinder(d: bore + boreClearance, h: thickness + hubH + 2mm).at(z: -1mm)
126
+ }
127
+ }
128
+ }
129
+
130
+ // Internal involute ring gear (for planetary gearboxes with matching SpurGear sun & planets)
131
+ component RingGear(
132
+ teeth: Number = 36,
133
+ mod: Length = 2mm,
134
+ thickness: Length = 6mm,
135
+ rim: Length = 4mm,
136
+ elephantsFoot: Length = 0.3mm,
137
+ pressureAngle: Angle = 20deg,
138
+ helix: Angle = 0deg,
139
+ herringbone: Bool = false,
140
+ backlash: Length = 0.2mm
141
+ ) {
142
+ assert(teeth >= 14, "RingGear requires at least 14 teeth")
143
+ assert(rim > 0mm && thickness > 0mm, "RingGear rim and thickness must be positive")
144
+ let pr = pitchRadius(teeth: teeth, mod: mod)
145
+ let outerR = pr + 1.25 * mod + rim
146
+ let innerR = pr - 1.0 * mod
147
+ let profile = Polygon(points: gearPoints(
148
+ teeth: teeth, mod: mod, pressureAngle: pressureAngle,
149
+ backlash: backlash, profileShift: 0, internal: true
150
+ ))
151
+
152
+ let cutH = thickness + 2mm
153
+ Difference {
154
+ Cylinder(r: outerR, h: thickness).chamfer(0.4mm, edges: .horizontal)
155
+ if herringbone && helix != 0deg {
156
+ let halfH = thickness / 2 + 1mm
157
+ let halfTwist = ((halfH * tan(helix)) / pr) * (180deg / pi)
158
+ let slices = max(6, ceil(abs(halfTwist) / 4deg))
159
+ let lower = profile.extrude(height: halfH, twist: halfTwist, slices: slices).at(z: -1mm)
160
+ Union {
161
+ lower
162
+ lower.mirror(.z).at(z: thickness)
163
+ }
164
+ } else if helix != 0deg {
165
+ let twist = ((cutH * tan(helix)) / pr) * (180deg / pi)
166
+ let slices = max(8, ceil(abs(twist) / 4deg))
167
+ profile.extrude(height: cutH, twist: twist, slices: slices).at(z: -1mm)
168
+ } else {
169
+ profile.extrude(height: cutH).at(z: -1mm)
170
+ }
171
+ if elephantsFoot > 0mm {
172
+ Cone(r1: innerR + elephantsFoot + 0.2mm, r2: innerR, h: elephantsFoot + 0.2mm).at(z: -0.1mm)
173
+ }
174
+ }
175
+ }
176
+
177
+ // Linear involute gear rack matching SpurGear(mod:, pressureAngle:)
178
+ // Pitch line sits at y = 1.25 * mod (back face at y = -back).
179
+ component GearRack(
180
+ teeth: Number = 10,
181
+ mod: Length = 2mm,
182
+ thickness: Length = 6mm,
183
+ back: Length = 5mm,
184
+ pressureAngle: Angle = 20deg,
185
+ backlash: Length = 0.15mm
186
+ ) {
187
+ assert(teeth >= 2 && mod > 0mm && back > 0mm, "GearRack parameters must be positive")
188
+ let pitch = pi * mod
189
+ let totalL = teeth * pitch
190
+ let add = 1.0 * mod
191
+ let ded = 1.25 * mod
192
+ let toothH = add + ded
193
+ let halfPitchW = pitch / 4 - backlash / 2
194
+ let halfRootW = halfPitchW + ded * tan(pressureAngle)
195
+ let halfTipW = max(0.2 * mod, halfPitchW - add * tan(pressureAngle))
196
+ Union {
197
+ Box(width: totalL, depth: back, height: thickness).at(y: -back / 2)
198
+ for i in 0..<teeth {
199
+ let x0 = -totalL / 2 + (i + 0.5) * pitch
200
+ Polygon(points: [
201
+ [x0 - halfRootW, -0.05mm],
202
+ [x0 + halfRootW, -0.05mm],
203
+ [x0 + halfTipW, toothH],
204
+ [x0 - halfTipW, toothH]
205
+ ]).extrude(height: thickness)
206
+ }
207
+ }
208
+ }
209
+
210
+ // GT2 2mm-pitch timing belt pulley with support-free 45-degree chamfered top flange and FDM bore clearance
211
+ component PulleyGT2(teeth: Number = 20, beltW: Length = 6.5mm, bore: Length = 5mm, boreClearance: Length = 0.15mm, flangeH: Length = 1.4mm) {
212
+ assert(teeth >= 12, "PulleyGT2 requires at least 12 teeth")
213
+ let pitch = 2mm
214
+ let pr = (teeth * pitch) / (2 * pi)
215
+ let rOut = pr - 0.254mm
216
+ let rIn = rOut - 0.75mm
217
+ let rFlange = rOut + 1.6mm
218
+ let rampH = rFlange - rOut
219
+ let step = 360deg / teeth
220
+ let pts = stride(from: 0, to: teeth * 6, by: 1).map { k in
221
+ let i = floor(k / 6)
222
+ let s = k % 6
223
+ let frac = s == 0 ? 0.00 : s == 1 ? 0.20 : s == 2 ? 0.32 : s == 3 ? 0.50 : s == 4 ? 0.68 : 0.80
224
+ let r = (s == 3) ? rIn : (s == 2 || s == 4) ? (rIn + 0.25mm) : rOut
225
+ let a = (i + frac) * step
226
+ return [r * cos(a), r * sin(a)]
227
+ }
228
+ let totalH = flangeH + beltW + rampH + flangeH
229
+ Difference {
230
+ Union {
231
+ Cylinder(r: rFlange, h: flangeH).chamfer(0.3mm, edges: .bottom)
232
+ Polygon(points: pts).extrude(height: beltW + 0.2mm).at(z: flangeH - 0.1mm)
233
+ // 45-degree overhang-free transition into upper flange
234
+ Cone(r1: rOut, r2: rFlange, h: rampH).at(z: flangeH + beltW)
235
+ Cylinder(r: rFlange, h: flangeH).chamfer(0.3mm, edges: .top).at(z: flangeH + beltW + rampH)
236
+ }
237
+ if bore > 0mm {
238
+ Cylinder(d: bore + boreClearance, h: totalH + 2mm).at(z: -1mm)
239
+ }
240
+ }
241
+ }
242
+
243
+ // Ball bearing reference model (default 608: 22mm OD x 8mm ID x 7mm W; also 625: 16x5x5, 6800: 19x10x5)
244
+ component Bearing(od: Length = 22mm, id: Length = 8mm, width: Length = 7mm) {
245
+ assert(od > id && width > 0mm, "Bearing od must exceed id")
246
+ Difference {
247
+ Cylinder(d: od, h: width).chamfer(0.4mm, edges: .horizontal)
248
+ Cylinder(d: id, h: width + 2mm).at(z: -1mm)
249
+ }
250
+ }
251
+
252
+ // Bearing press-fit seat cutter (use inside Difference)
253
+ component BearingSeat(od: Length = 22mm, width: Length = 7mm, clearance: Length = 0.15mm) {
254
+ Cylinder(d: od + 2 * clearance, h: width + 0.3mm).at(z: -0.1mm)
255
+ }
256
+
257
+ // D-shaft hole cutter (flat cut on +X side; use inside Difference)
258
+ component DShaftHole(d: Length = 5mm, flat: Length = 0.5mm, depth: Length = 10mm, clearance: Length = 0.2mm) {
259
+ assert(flat < d / 2, "DShaftHole flat must be less than radius")
260
+ let r = d / 2 + clearance
261
+ Difference {
262
+ Cylinder(r: r, h: depth + 2mm).at(z: -1mm)
263
+ Box(width: d, depth: d + 2mm, height: depth + 4mm).at(x: r - flat + d / 2, z: -2mm)
264
+ }
265
+ }
266
+
267
+ // NEMA 17 stepper motor faceplate cutout (31mm M3 bolt pattern + 22.6mm pilot boss)
268
+ component Nema17Mount(wall: Length = 5mm) {
269
+ Cylinder(d: 22.6mm, h: wall + 2mm, center: true)
270
+ for ix in 0..<2 {
271
+ for iy in 0..<2 {
272
+ Cylinder(d: 3.4mm, h: wall + 2mm, center: true)
273
+ .at(x: (ix == 0 ? -1 : 1) * 15.5mm, y: (iy == 0 ? -1 : 1) * 15.5mm)
274
+ }
275
+ }
276
+ }