kcl-lib 0.2.144

KittyCAD Language implementation and tools
Documentation
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/// Definitions of standard holes that could be drilled or cut into solids.

@settings(defaultLengthUnit = mm, kclVersion = 1.0)

// Tops


holeTypeSimple = 0
holeTypeCounterbore = 1
holeTypeCountersink = 2

/// A hole top with no decoration.
@(feature_tree = false)
export fn simple() {
  return { feature = holeTypeSimple }
}

/// Cut a straight vertical counterbore at the top of the hole.
/// Typically used when a fastener (e.g. the head cap on a screw)
/// needs to sit flush with the solid's surface.
/// ```kcl,legacySketch
/// // Model a cube
/// cubeLen = 20
/// bigCube = startSketchOn(XY)
///   |> startProfile(at = [-cubeLen / 2, -cubeLen / 2 + 10])
///   |> line(end = [cubeLen, 0], tag = $a)
///   |> line(end = [0, cubeLen], tag = $b)
///   |> line(end = [-cubeLen, 0], tag = $c)
///   |> line(end = [0, -cubeLen], tag = $d)
///   |> close()
///   |> extrude(length = cubeLen, symmetric = true)
///   |> translate(x = 5)
///
/// // Add a hole to the cube.
/// // It'll have a drilled end, and a counterbore (vertical hole that emerges from a larger hole)
/// bigCube
///   |> hole::hole(
///        face = a,
///        cutAt = [0, 5],
///        holeBottom = hole::drill(pointAngle = 110deg),
///        holeBody = hole::blind(depth = 5, diameter = 8),
///        holeType = hole::counterbore(diameter = 12, depth = 3.5),
///      )
/// ```
@(feature_tree = false)
export fn counterbore(diameter: number(Length), depth: number(Length)) {
  return {
    diameter = diameter,
    depth = depth,
    feature = holeTypeCounterbore
  }
}

/// Cut an angled countersink at the top of the hole.
/// Typically used when a conical screw head has to sit flush
/// with the surface being cut into.
/// ```kcl,legacySketch
/// // Model a cube
/// cubeLen = 20
/// bigCube = startSketchOn(XY)
///   |> startProfile(at = [-cubeLen / 2, -cubeLen / 2 + 10])
///   |> line(end = [cubeLen, 0], tag = $a)
///   |> line(end = [0, cubeLen], tag = $b)
///   |> line(end = [-cubeLen, 0], tag = $c)
///   |> line(end = [0, -cubeLen], tag = $d)
///   |> close()
///   |> extrude(length = cubeLen, symmetric = true)
///   |> translate(x = 5)
///
/// // Add a hole to the cube.
/// // It'll have a drilled end, and a countersink (angled tip at the start).
/// bigCube
///   |> hole::hole(
///        face = a,
///        cutAt = [0, 5],
///        holeBottom = hole::drill(pointAngle = 110deg),
///        holeBody = hole::blind(depth = 5, diameter = 8),
///        holeType = hole::countersink(diameter = 14, angle = 100deg),
///      )
/// ```
@(feature_tree = false)
export fn countersink(diameter: number(Length), angle: number(Angle)) {
  return {
    diameter = diameter,
    angle = angle,
    feature = holeTypeCountersink
  }
}

// Bodies


/// The hole has the given blind depth.
@(feature_tree = false)
export fn blind(depth: number(Length), diameter: number(Length)) {
  return {
    blindDepth = depth,
    diameter = diameter
  }
}

// Ends


/// End the hole in an angle, like the end of a drill.
/// ```kcl,legacySketch
/// // Sketch a cube, so we have something to drill into.
/// cubeLen = 20
/// bigCube = startSketchOn(XY)
///   |> startProfile(at = [-cubeLen / 2, -cubeLen / 2 + 10])
///   |> line(end = [cubeLen, 0], tag = $a)
///   |> line(end = [0, cubeLen], tag = $b)
///   |> line(end = [-cubeLen, 0], tag = $c)
///   |> line(end = [0, -cubeLen], tag = $d)
///   |> close()
///   |> extrude(length = cubeLen, symmetric = true)
/// 
/// // Add a hole with a very pointy drilled bottom.
/// bigCube
///   |> hole::hole(
///        face = a,
///        cutAt = [0, 0],
///        holeBottom = hole::drill(pointAngle = 25deg),
///        holeBody = hole::blind(depth = 1, diameter = 8),
///        holeType = hole::simple(),
///      )
/// ```
@(feature_tree = false)
export fn drill(pointAngle: number(Angle)) {
  return { drillBitAngle = pointAngle }
}

/// End the hole flat.
/// ```kcl,legacySketch
/// // Sketch a cube, so we have something to drill into.
/// cubeLen = 20
/// bigCube = startSketchOn(XY)
///   |> startProfile(at = [-cubeLen / 2, -cubeLen / 2 + 10])
///   |> line(end = [cubeLen, 0], tag = $a)
///   |> line(end = [0, cubeLen], tag = $b)
///   |> line(end = [-cubeLen, 0], tag = $c)
///   |> line(end = [0, -cubeLen], tag = $d)
///   |> close()
///   |> extrude(length = cubeLen, symmetric = true)
/// 
/// // Add a hole with a flat bottom.
/// bigCube
///   |> hole::hole(
///        face = a,
///        cutAt = [0, 0],
///        holeBottom = hole::flat(),
///        holeBody = hole::blind(depth = 2, diameter = 8),
///        holeType = hole::simple(),
///      )
/// ```
@(feature_tree = false)
export fn flat() {
  return { drillBitAngle = 180deg }
}

// Defining the hole


@(feature_tree = false)
fn drawTop(@partialHoleProfile, body, holeType) {
  prof = if holeType.feature == holeTypeSimple {
    partialHoleProfile
      |> xLine(length = body.diameter / 2)
  } else if holeType.feature == holeTypeCounterbore {
    lip = (holeType.diameter - body.diameter) / 2
    partialHoleProfile
      |> yLine(length = holeType.depth)
      |> xLine(length = holeType.diameter / 2)
      |> yLine(length = -holeType.depth)
      |> xLine(length = -lip)
  } else if holeType.feature == holeTypeCountersink {
    theta = holeType.angle / 2
    alpha = 90 - theta
    tr = holeType.diameter / 2
    br = body.diameter / 2
    assert(tr, isGreaterThan = br, error = "Diameter of the countersink must be greater than diameter of the hole's body")
    // Ugly trigonometry that I calculated with pen and paper
    up = (tr - br) / tan(theta)
    b = br / tan(theta)
    diag = up/cos(theta)
    partialHoleProfile
      |> yLine(length = up)
      |> xLine(length = tr)
      |> angledLine(length = diag, angle = 180 + alpha)
  } else {
    assert(holeType.feature, isLessThanOrEqual = holeTypeCountersink, error = "unknown holeType feature")
  }
  return prof
}

@(feature_tree = false)
fn holeTypeHeight(body, holeType) {
  height = if holeType.feature == holeTypeSimple {
    0
  } else if holeType.feature == holeTypeCounterbore {
    holeType.depth
  } else if holeType.feature == holeTypeCountersink {
    t = holeType.angle / 2
    tr = holeType.diameter / 2
    br = body.diameter / 2
    // Ugly trigonometry that I calculated with pen and paper
    up = (tr - br) / tan(t)
    up
  } else {
    assert(holeType.feature, isLessThanOrEqual = holeTypeCountersink, error = "unknown holeType feature")
  }
  return height
}

// Actually defining a hole


/// Profile for the end of the hole. Only really intended for advanced uses.
/// You can use this and `revolveHole` to do tricky custom hole cutting.
@(feature_tree = false)
fn sketchHoleProfile(@sketchOn, holeBottom, holeBody, holeType, shift) {
  body = holeBody
  bottom = holeBottom
  // Calculate the drill bit height
  radius = body.diameter / 2
  // if drill bit angle is exactly 180 degrees, it causes a divide by zero
  // (because tan(theta = 90) = 0).
  // So, if it would be 180deg, change it to be 179.99deg.
  theta = if abs(bottom.drillBitAngle - 180deg) < 0.01deg {
    179.99deg / 2
  } else {
    bottom.drillBitAngle / 2
  }
  drillHeight = radius / tan(theta)

  // Calculate total model height from holeType to bottom
  hBody = body.blindDepth
  hBott = drillHeight
  totalHeight = hBody + hBott + holeTypeHeight(body, holeType)
  sk = startProfile(sketchOn, at = [shift, -totalHeight])
    // Drill profile
    |> yLine(length = drillHeight)
    // Body depth
    |> yLine(length = body.blindDepth, tag = $d)
    // Top
    |> drawTop(body, holeType)
    // Body depth
    |> yLine(length = -body.blindDepth)
    // // Drill profile
    |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
    |> close()
  return { profile = sk, axis = d }
}

/// Given a hole profile, revolve it into a 3D hole solid geometry.
/// This can be subtracted from a solid to form a hole in it.
@(feature_tree = false)
fn revolveHole(@holeProfile, edge) {
  return holeProfile
    |> revolve(axis = edge)
    |> appearance(color = "#ff0000")
}

/// Build a hole's geometry from its top, bottom and depth.
/// Can be subtracted from a solid to cut a hole into it.
@(feature_tree = false)
fn holeGeometry(@solid, face, holeBottom, holeBody, holeType, cutAt) {
  intoSolid = startSketchOn(
    solid,
    normalToFace = face,
    alignAxis = -Y,
    normalOffset = cutAt[1],
  )
  toCut = sketchHoleProfile(
    intoSolid,
    holeBottom,
    holeBody,
    holeType,
    shift = -cutAt[0],
  )

  drilled = revolveHole(toCut.profile, edge = toCut.axis)
  return drilled
}

/// From the hole's parts (bottom, middle, top), cut the hole into the given solid,
/// at the given 2D position on the given face.
/// ```kcl,legacySketch

/// // Model a cube
/// cubeLen = 20
/// bigCube = startSketchOn(XY)
///   |> startProfile(at = [-cubeLen / 2, -cubeLen / 2 + 10])
///   |> line(end = [cubeLen, 0], tag = $a)
///   |> line(end = [0, cubeLen], tag = $b)
///   |> line(end = [-cubeLen, 0], tag = $c)
///   |> line(end = [0, -cubeLen], tag = $d)
///   |> close()
///   |> extrude(length = cubeLen, symmetric = true)
///   |> translate(x = 5)
///
/// // Add a hole to the cube.
/// // It'll have a drilled end, and a countersink (angled tip at the start).
/// bigCube
///   |> hole::hole(
///        face = a,
///        cutAt = [0, 5],
///        holeBottom = hole::drill(pointAngle = 110deg),
///        holeBody = hole::blind(depth = 5, diameter = 8),
///        holeType = hole::countersink(diameter = 14, angle = 100deg),
///      )
/// ```
/// ```kcl,legacySketch

/// // Model a cube
/// cubeLen = 20
/// bigCube = startSketchOn(XY)
///   |> startProfile(at = [-cubeLen / 2, -cubeLen / 2 + 10])
///   |> line(end = [cubeLen, 0], tag = $a)
///   |> line(end = [0, cubeLen], tag = $b)
///   |> line(end = [-cubeLen, 0], tag = $c)
///   |> line(end = [0, -cubeLen], tag = $d)
///   |> close()
///   |> extrude(length = cubeLen, symmetric = true)
///   |> translate(x = 5)
///
/// // Add a hole to the cube.
/// // It'll have a drilled end, and a counterbore (vertical hole that emerges from a larger hole)
/// bigCube
///   |> hole::hole(
///        face = a,
///        cutAt = [0, 5],
///        holeBottom = hole::drill(pointAngle = 110deg),
///        holeBody = hole::blind(depth = 5, diameter = 8),
///        holeType = hole::counterbore(diameter = 12, depth = 3.5),
///      )
/// ```
/// ```kcl,sketchSolve
/// blockProfile = sketch(on = XY) {
///   edge1 = line(start = [var 0mm, var 0mm], end = [var 8mm, var 0mm])
///   edge2 = line(start = [var 8mm, var 0mm], end = [var 8mm, var 6mm])
///   edge3 = line(start = [var 8mm, var 6mm], end = [var 0mm, var 6mm])
///   edge4 = line(start = [var 0mm, var 6mm], end = [var 0mm, var 0mm])
///   coincident([edge1.end, edge2.start])
///   coincident([edge2.end, edge3.start])
///   coincident([edge3.end, edge4.start])
///   coincident([edge4.end, edge1.start])
///   horizontal(edge1)
///   vertical(edge2)
///   horizontal(edge3)
///   vertical(edge4)
/// }
///
/// block = extrude(region(point = [4mm, 3mm], sketch = blockProfile), length = 6mm, tagEnd = $top)
/// drilledBlock = hole::hole(
///   block,
///   face = top,
///   cutAt = [4mm, 3mm],
///   holeBottom = hole::flat(),
///   holeBody = hole::blind(depth = 4mm, diameter = 2mm),
///   holeType = hole::simple(),
/// )
/// ```
@(feature_tree = true)
export fn hole(
  /// Which solid to add a hole to.
  @solid: Solid,
  /// Which face of the solid to add the hole to.
  /// Controls the orientation of the hole.
  face: TaggedFace,
  /// Define bottom feature of the hole. E.g. drilled or flat.
  holeBottom,
  /// Define the main length of the hole. E.g. a blind distance.
  holeBody,
  /// Define the top feature of the hole. E.g. countersink, counterbore, simple.
  holeType,
  /// Where to place the cut on the given face of the solid.
  /// Given as absolute coordinates in the global scene.
  cutAt: [number(Length); 2],
) {
  drilled = holeGeometry(solid, face, holeBottom, holeBody, holeType, cutAt)
  return subtract(solid, tools = [drilled])
}

/// From the hole's parts (bottom, middle, top), cut the hole into the given solid,
/// at each of the given 2D positions on the given face.
/// Basically like function `hole` but it takes multiple 2D positions in `cutsAt`.
/// ```kcl,legacySketch
/// // Sketch a solid
/// sketch001 = startSketchOn(XY)
/// profile001 = startProfile(sketch001, at = [-3.89, 1.95])
///   |> line(end = [0.63, -3.25])
///   |> xLine(length = 7.15)
///   |> line(end = [0.59, 3.2])
///   |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
///   |> close()
/// mySolid = extrude(profile001, length = 3, symmetric = true)
/// 
/// // Add three holes to it.
/// hole001 = hole::holes(
///   mySolid,
///   face = END,
///   cutsAt = [[0, 0], [0, 3], [1, 2]],
///   holeBottom = hole::drill(pointAngle = 110deg),
///   holeBody = hole::blind(depth = 2, diameter = 0.4),
///   holeType = hole::counterbore(diameter = 1, depth = 0.2),
/// )
/// ```
/// ```kcl,sketchSolve
/// blockProfile = sketch(on = XY) {
///   edge1 = line(start = [var 0mm, var 0mm], end = [var 10mm, var 0mm])
///   edge2 = line(start = [var 10mm, var 0mm], end = [var 10mm, var 6mm])
///   edge3 = line(start = [var 10mm, var 6mm], end = [var 0mm, var 6mm])
///   edge4 = line(start = [var 0mm, var 6mm], end = [var 0mm, var 0mm])
///   coincident([edge1.end, edge2.start])
///   coincident([edge2.end, edge3.start])
///   coincident([edge3.end, edge4.start])
///   coincident([edge4.end, edge1.start])
///   horizontal(edge1)
///   vertical(edge2)
///   horizontal(edge3)
///   vertical(edge4)
/// }
///
/// block = extrude(region(point = [5mm, 3mm], sketch = blockProfile), length = 6mm, tagEnd = $top)
/// drilledBlock = hole::holes(
///   block,
///   face = top,
///   cutsAt = [[2mm, 2mm], [5mm, 3mm], [8mm, 4mm]],
///   holeBottom = hole::drill(pointAngle = 90deg),
///   holeBody = hole::blind(depth = 3mm, diameter = 1.5mm),
///   holeType = hole::simple(),
/// )
/// ```
@(feature_tree = true)
export fn holes(
  /// Which solid to add a hole to.
  @solid: Solid,
  /// Which face of the solid to add the hole to.
  /// Controls the orientation of the hole.
  face: TaggedFace,
  /// Define bottom feature of the hole. E.g. drilled or flat.
  holeBottom,
  /// Define the main length of the hole. E.g. a blind distance.
  holeBody,
  /// Define the top feature of the hole. E.g. countersink, counterbore, simple.
  holeType,
  /// Where to place the holes, given as absolute coordinates in the global scene.
  cutsAt: [[number(Length); 2]],
) {
  drilled = map(cutsAt, f = fn(@cutAt) {
    return holeGeometry(solid, face, holeBottom, holeBody, holeType, cutAt)
  })
  return subtract(solid, tools = drilled)
}

/// Place the given holes in a line.
/// Basically like function `hole` but cuts multiple holes in a line.
/// Works like linear patterns.
@(feature_tree = false)
export fn holesLinear(
  /// Which solid to add a hole to.
  @solid: Solid,
  /// Which face of the solid to add the hole to.
  /// Controls the orientation of the hole.
  face: TaggedFace,
  /// Define bottom feature of the hole. E.g. drilled or flat.
  holeBottom,
  /// Define the main length of the hole. E.g. a blind distance.
  holeBody,
  /// Define the top feature of the hole. E.g. countersink, counterbore, simple.
  holeType,
  /// Where to place the first cut in the linear pattern,
  /// given as absolute coordinates in the global scene.
  cutAt: [number(Length); 2],
  /// How many holes to cut.
  instances: number(_),
  /// How far between each hole
  distance,
  /// Along which axis should the holes be cut?
  axis: Axis2d | Point2d,
) {
  drilled = holeGeometry(solid, face, holeBottom, holeBody, holeType, cutAt)
  |> patternLinear3d(instances, distance, axis)

  return subtract(solid, tools = drilled)
}