-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbracket.js
More file actions
189 lines (169 loc) · 4.55 KB
/
bracket.js
File metadata and controls
189 lines (169 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/** @import * as Replicad from 'replicad' */
export const defaultParams = {
gridSpacingInMm: 40,
widthInGrids: 2,
topLengthInGrids: 1,
bottomLengthInGrids: 1,
fastenerDiameterInMm: 8,
wallThicknessInMm: 1.6,
roundRadiusInMm: 5,
innerFilletInMm: 5,
kerfInMm: 1,
}
/**
* @param {typeof defaultParams} params
*/
export default function main(params) {
const { combineFinderFilters, EdgeFinder } = replicad
const {
gridSpacingInMm,
widthInGrids,
topLengthInGrids,
bottomLengthInGrids,
fastenerDiameterInMm,
wallThicknessInMm,
roundRadiusInMm,
innerFilletInMm,
kerfInMm,
} = params
const sideOptions = {
gridSpacingInMm,
widthInGrids,
fastenerDiameterInMm,
roundRadiusInMm,
kerfInMm,
}
const bottom = /** @type {Replicad.Solid} */ (
drawSide({
...sideOptions,
lengthInGrids: bottomLengthInGrids,
})
.sketchOnPlane('XZ')
.extrude(wallThicknessInMm)
.rotate(90, [0, 0, 0], [0, 1, 0])
.translateY(wallThicknessInMm)
)
const top = /** @type {Replicad.Solid} */ (
drawSide({ ...sideOptions, lengthInGrids: topLengthInGrids })
.sketchOnPlane('XZ')
.extrude(wallThicknessInMm)
.rotate(90, [0, 0, 0], [0, 1, 0])
.rotate(90, [0, 0, 0], [0, 0, 1])
)
const [filters] = combineFinderFilters([
{
// @ts-ignore
filter: new EdgeFinder().containsPoint([
wallThicknessInMm,
wallThicknessInMm,
(1 / 2) * widthInGrids * gridSpacingInMm,
]),
radius: innerFilletInMm,
},
{
// @ts-ignore
filter: new EdgeFinder(),
radius: (1 / 3) * wallThicknessInMm,
},
])
const bracket = top.fuse(bottom).fillet(filters)
return {
shape: bracket,
}
}
/**
* @param {object} options
* @param {number} options.gridSpacingInMm
* @param {number} options.widthInGrids
* @param {number} options.lengthInGrids
* @param {number} options.fastenerDiameterInMm
* @param {number} options.roundRadiusInMm
* @param {number} options.kerfInMm
* @returns {import('replicad').Drawing}
*/
function drawSide(options) {
const {
gridSpacingInMm,
widthInGrids,
lengthInGrids,
fastenerDiameterInMm,
roundRadiusInMm,
kerfInMm,
} = options
const widthInMm = widthInGrids * gridSpacingInMm - 2 * kerfInMm
const lengthInMm = lengthInGrids * gridSpacingInMm - kerfInMm
let side = drawRoundedRectangleWithStraightBack(widthInMm, lengthInMm, roundRadiusInMm)
for (const widthIndex of range(widthInGrids)) {
for (const lengthIndex of range(lengthInGrids)) {
const fastenerCut = drawHexihole({
radius: (1 / 2) * fastenerDiameterInMm,
orientation: 'flat-bottom',
}).translate([
-(1 / 2 + widthIndex) * gridSpacingInMm + kerfInMm,
(1 / 2 + lengthIndex) * gridSpacingInMm - kerfInMm,
])
side = side.cut(fastenerCut)
}
}
return side
}
/**
* @param {number} width
* @param {number} height
* @param {number} r
* @returns {import('replicad').Drawing}
*/
function drawRoundedRectangleWithStraightBack(width, height, r) {
const { draw } = replicad
return draw()
.vLine(height - r)
.tangentArc(-r, r)
.hLine(-width + 2 * r)
.tangentArc(-r, -r)
.vLine(-height + r)
.close()
}
/**
* @param {object} options
* @param {number} options.radius
* @param {'v-bottom' | 'flat-bottom'} options.orientation
*/
function drawHexihole(options) {
// https://en.wikipedia.org/wiki/Hexagon#Regular_hexagon
// The common length of the sides equals the radius of the circumscribed circle or circumcircle,
// which equals (2/sqrt(3)) times the apothem (radius of the inscribed circle).
const { radius: inscribedRadius, orientation } = options
const circumscribedRadius = inscribedRadius * (2 / Math.sqrt(3))
return drawHexagon({ radius: circumscribedRadius, orientation })
}
/**
* @param {object} options
* @param {number} options.radius
* @param {'v-bottom' | 'flat-bottom'} options.orientation
*/
function drawHexagon(options) {
const { draw } = replicad
const { radius, orientation = 'v-bottom' } = options
/** @type {[number, number]} */
let startPoint
/** @type {number} */
let startAngle
if (orientation === 'v-bottom') {
startPoint = [0, -radius]
startAngle = 30
} else {
startPoint = [radius, 0]
startAngle = 120
}
let pen = draw().movePointerTo(startPoint)
for (const sideIndex of range(6)) {
pen = pen.polarLine(radius, sideIndex * 60 + startAngle)
}
return pen.close()
}
/**
* @param {number} end
*/
function range(end) {
return Array.from(Array(end).keys())
}