cadbuildr-flatpack 0.1.0__py3-none-any.whl

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.
cadbuildr/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from pkgutil import extend_path
2
+
3
+ __path__ = extend_path(__path__, __name__)
@@ -0,0 +1,167 @@
1
+ """cadbuildr-flatpack: laser-cut and CNC-routed sheet construction.
2
+
3
+ Everything starts from a sheet and a machine:
4
+
5
+ * :class:`Sheet` / :data:`SHEETS` — material, nominal and **measured**
6
+ thickness, sheet size, minimum web; :class:`Machine` / :data:`MACHINES` —
7
+ laser kerf, router bit + dogbone / T-bone relief, default joint clearance.
8
+ * :class:`Panel` — one flat part; joints cut its edges and faces.
9
+ * Joints (library operations; each places a panel AND cuts both):
10
+ :func:`finger_joint`, :func:`slot_and_tab`, :func:`cross_halving`,
11
+ :func:`tslot_joint` (stdlib DIN 912 screws + DIN 934 nuts connected by
12
+ interface), :func:`butt_joint`, :func:`living_hinge`.
13
+ * Generators: :class:`FingerBox`, :class:`TSlotEnclosure`, :class:`EggCrate`,
14
+ :class:`HingeSample`.
15
+ * Output: :func:`flatten`, :func:`nest`, :func:`to_svg`, :func:`to_dxf`,
16
+ :func:`sheet_report`, :func:`bom_rows`.
17
+ * Validation: :func:`check_flatpack`, :func:`overlap_samples`.
18
+ """
19
+
20
+ from .errors import (
21
+ CornerConflict,
22
+ EdgeMismatch,
23
+ FeatureOverlap,
24
+ FitError,
25
+ FlatpackError,
26
+ NestingError,
27
+ NotPlaced,
28
+ TSlotError,
29
+ UnknownStock,
30
+ WebTooThin,
31
+ )
32
+ from .export import (
33
+ FlatPart,
34
+ SheetLayout,
35
+ bom_rows,
36
+ cut_time,
37
+ flatten,
38
+ nest,
39
+ panels_of,
40
+ sheet_report,
41
+ to_dxf,
42
+ to_svg,
43
+ )
44
+ from .calibration import KerfCoupon, coupon_clearances, kerf_from_measurement
45
+ from .generators import (
46
+ DrawerUnit,
47
+ EggCrate,
48
+ FingerBox,
49
+ HingeSample,
50
+ PhoneStand,
51
+ TSlotEnclosure,
52
+ finger_box,
53
+ )
54
+ from .joints import (
55
+ SCREW_LENGTHS,
56
+ butt_joint,
57
+ cross_halving,
58
+ dovetail_splice,
59
+ finger_count,
60
+ finger_joint,
61
+ living_hinge,
62
+ slot_and_tab,
63
+ stack_joint,
64
+ tslot_joint,
65
+ )
66
+ from .panel import EDGES, Panel, ShapedPanel, chamfered_rect, parse_points, rounded_rect
67
+ from .stock import MACHINES, SHEETS, Machine, Sheet, resolve_machine, resolve_sheet
68
+ from .validate import check_flatpack, check_panel, overlap_samples
69
+
70
+ LIBRARY_ID = "flatpack"
71
+ LIBRARY_NAME = "Flatpack (laser & CNC sheet parts)"
72
+ LIBRARY_TAB = "Flatpack"
73
+
74
+ TOOLBAR = {
75
+ "tab": "Flatpack",
76
+ "sections": [
77
+ {
78
+ "id": "panels",
79
+ "label": "Panels",
80
+ "items": ["Panel", "ShapedPanel", "KerfCoupon"],
81
+ },
82
+ {
83
+ "id": "joints",
84
+ "label": "Joints",
85
+ "items": [
86
+ "finger_joint",
87
+ "slot_and_tab",
88
+ "cross_halving",
89
+ "tslot_joint",
90
+ "butt_joint",
91
+ "dovetail_splice",
92
+ "stack_joint",
93
+ "living_hinge",
94
+ ],
95
+ },
96
+ {
97
+ "id": "generators",
98
+ "label": "Generators",
99
+ "items": [
100
+ "FingerBox",
101
+ "DrawerUnit",
102
+ "TSlotEnclosure",
103
+ "EggCrate",
104
+ "PhoneStand",
105
+ "HingeSample",
106
+ ],
107
+ },
108
+ ],
109
+ }
110
+
111
+ __all__ = [
112
+ "DrawerUnit",
113
+ "KerfCoupon",
114
+ "PhoneStand",
115
+ "ShapedPanel",
116
+ "chamfered_rect",
117
+ "coupon_clearances",
118
+ "cut_time",
119
+ "dovetail_splice",
120
+ "finger_box",
121
+ "kerf_from_measurement",
122
+ "parse_points",
123
+ "rounded_rect",
124
+ "stack_joint",
125
+ "CornerConflict",
126
+ "EDGES",
127
+ "EdgeMismatch",
128
+ "EggCrate",
129
+ "FeatureOverlap",
130
+ "FingerBox",
131
+ "FitError",
132
+ "FlatPart",
133
+ "FlatpackError",
134
+ "HingeSample",
135
+ "MACHINES",
136
+ "Machine",
137
+ "NestingError",
138
+ "NotPlaced",
139
+ "Panel",
140
+ "SCREW_LENGTHS",
141
+ "SHEETS",
142
+ "Sheet",
143
+ "SheetLayout",
144
+ "TSlotEnclosure",
145
+ "TSlotError",
146
+ "UnknownStock",
147
+ "WebTooThin",
148
+ "bom_rows",
149
+ "butt_joint",
150
+ "check_flatpack",
151
+ "check_panel",
152
+ "cross_halving",
153
+ "finger_count",
154
+ "finger_joint",
155
+ "flatten",
156
+ "living_hinge",
157
+ "nest",
158
+ "overlap_samples",
159
+ "panels_of",
160
+ "resolve_machine",
161
+ "resolve_sheet",
162
+ "sheet_report",
163
+ "slot_and_tab",
164
+ "to_dxf",
165
+ "to_svg",
166
+ "tslot_joint",
167
+ ]
@@ -0,0 +1,110 @@
1
+ """Calibrate a machine on a sheet: one coupon tells you the kerf and the fit.
2
+
3
+ Cut a :class:`KerfCoupon` (exported **without** kerf compensation):
4
+
5
+ * a comb of slots, each the sheet's measured thickness plus a clearance
6
+ (``-0.15 … +0.15`` mm by default, engraved under the slot). Push an
7
+ offcut of the same sheet into each; the slot that fits the way you want
8
+ (snug / press / slip) gives the clearance:
9
+ ``machine.with_clearance(c)``;
10
+ * a square hole ``hole`` mm wide. Measure the square that drops out:
11
+ ``kerf_from_measurement(hole, measured)`` is the kerf
12
+ (``machine.with_kerf(k)``).
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from typing import List
18
+
19
+ from cadbuildr.foundation.library import Num, OneOf, part
20
+
21
+ from . import geometry2d as g2
22
+ from .errors import FitError
23
+ from .panel import Panel
24
+ from .stock import DEFAULT_MACHINE, DEFAULT_SHEET, MACHINES, SHEETS
25
+
26
+
27
+ def coupon_clearances(slots: int, start: float, step: float) -> List[float]:
28
+ return [round(start + i * step, 4) for i in range(int(slots))]
29
+
30
+
31
+ def kerf_from_measurement(nominal: float, measured: float) -> float:
32
+ """Kerf from an uncompensated square: the drop is ``nominal - kerf`` wide."""
33
+ k = float(nominal) - float(measured)
34
+ if not 0.0 <= k < 1.0:
35
+ raise FitError(
36
+ f"kerf_from_measurement: a {nominal:g} mm square measured {measured:g} mm gives "
37
+ f"kerf {k:.3f} mm; measure the square that dropped out, not the hole"
38
+ )
39
+ return round(k, 4)
40
+
41
+
42
+ @part(
43
+ category="Calibration",
44
+ name="Kerf / fit coupon",
45
+ configurations={
46
+ "Laser, 3 mm ply": dict(sheet="Plywood 3 mm"),
47
+ "CNC, coarse steps": dict(machine="CNC 6 mm bit dogbone", step=0.1, start=-0.1),
48
+ },
49
+ )
50
+ class KerfCoupon(Panel):
51
+ """A comb of test slots (thickness + clearance) and a square kerf gauge.
52
+
53
+ Args:
54
+ sheet: the stock to calibrate.
55
+ thickness: measured thickness override (0 keeps the sheet's measured value).
56
+ machine: the machine profile to calibrate.
57
+ slots: number of test slots.
58
+ start: clearance of the first slot (mm, negative = press fit).
59
+ step: clearance step between slots (mm).
60
+ hole: size of the square kerf gauge (mm).
61
+ """
62
+
63
+ def __init__(
64
+ self,
65
+ sheet: OneOf[SHEETS] = DEFAULT_SHEET,
66
+ thickness: Num(min=0, max=40, step=0.05) = 0.0,
67
+ machine: OneOf[MACHINES] = DEFAULT_MACHINE,
68
+ slots: int = 7,
69
+ start: Num(min=-0.5, max=0.5, step=0.01) = -0.15,
70
+ step: Num(min=0.01, max=0.2, step=0.01) = 0.05,
71
+ hole: Num(min=5, max=50, step=1) = 20.0,
72
+ ):
73
+ from .stock import resolve_sheet
74
+
75
+ if slots < 2:
76
+ raise FitError("KerfCoupon: at least two slots")
77
+ t = resolve_sheet(sheet, thickness).thickness
78
+ depth = max(10.0, 2.0 * t)
79
+ pitch = t + 0.5 + max(8.0, 2.0 * t)
80
+ margin = 8.0
81
+ width = 2 * margin + slots * pitch + hole + margin
82
+ height = depth + hole + 3 * margin
83
+ self._setup(
84
+ g2.rect(0.0, 0.0, width, height),
85
+ sheet,
86
+ thickness,
87
+ machine,
88
+ "coupon",
89
+ names=("s", "e", "n", "w"),
90
+ )
91
+ self.compensate = False # calibration is cut as drawn
92
+ self.clearances = coupon_clearances(slots, start, step)
93
+ ops_rings = []
94
+ edge = self.edge("n")
95
+ for i, c in enumerate(self.clearances):
96
+ x = margin + (i + 0.5) * pitch
97
+ w = t + c
98
+ if w <= 0:
99
+ raise FitError(f"KerfCoupon: clearance {c:g} closes a {t:g} mm slot")
100
+ handle = edge.notch(
101
+ self.u_of("n", x + w / 2), self.u_of("n", x - w / 2), depth, "coupon"
102
+ )
103
+ ops_rings.extend(self.void_rings("n", handle))
104
+ self.engrave_text(f"{c:+.2f}", x, height - depth - 5.0, 3.0)
105
+ hx = width - margin - hole
106
+ ops_rings.append(self.hole_ring(g2.rect(hx, margin, hx + hole, margin + hole)))
107
+ self.engrave_text(f"{hole:g}", hx + hole / 2, margin + hole + 4.0, 3.0)
108
+ for op in self.cut_ops(ops_rings):
109
+ self.add_operation(op)
110
+ self.hole_size = float(hole)
@@ -0,0 +1,44 @@
1
+ """Named errors: every flatpack failure says what is wrong and how to fix it."""
2
+
3
+ from __future__ import annotations
4
+
5
+
6
+ class FlatpackError(ValueError):
7
+ """Base of every flatpack error."""
8
+
9
+
10
+ class UnknownStock(FlatpackError):
11
+ """A sheet or machine key that is not in the table."""
12
+
13
+
14
+ class EdgeMismatch(FlatpackError):
15
+ """Two edges a joint pairs do not fit (length, unknown edge name)."""
16
+
17
+
18
+ class FeatureOverlap(FlatpackError):
19
+ """Two cuts on the same edge overlap (two joints on one stretch of edge)."""
20
+
21
+
22
+ class CornerConflict(FlatpackError):
23
+ """Notches from two adjacent edges collide at a panel corner."""
24
+
25
+
26
+ class FitError(FlatpackError):
27
+ """A tab, finger or slot cannot be cut for the sheet / machine (too narrow,
28
+ too many fingers for the edge, interference beyond what the material takes)."""
29
+
30
+
31
+ class WebTooThin(FlatpackError):
32
+ """Material left between two cuts is thinner than the sheet's minimum web."""
33
+
34
+
35
+ class TSlotError(FlatpackError):
36
+ """A T-slot bolt joint that cannot be built (screw too short / too long)."""
37
+
38
+
39
+ class NotPlaced(FlatpackError):
40
+ """A joint between two panels neither of which is in the assembly yet."""
41
+
42
+
43
+ class NestingError(FlatpackError):
44
+ """A flat part larger than the sheet it must be nested on."""
@@ -0,0 +1 @@
1
+ """Runnable flatpack examples (shipped inside the package so tests and Bazel see them)."""
@@ -0,0 +1,56 @@
1
+ """Two flatpack builds and their cut files.
2
+
3
+ python examples/shelf_and_box.py [out_dir]
4
+
5
+ * a small shelf: a divider stands on a base through three tabs, a side is
6
+ finger-jointed to the base;
7
+ * a closed 3 mm plywood finger box, nested and exported as DXF + SVG.
8
+ """
9
+
10
+ import sys
11
+ from pathlib import Path
12
+
13
+ from cadbuildr.foundation import Assembly
14
+ from cadbuildr.flatpack import (
15
+ FingerBox,
16
+ Panel,
17
+ check_flatpack,
18
+ finger_joint,
19
+ nest,
20
+ sheet_report,
21
+ slot_and_tab,
22
+ to_dxf,
23
+ to_svg,
24
+ )
25
+
26
+
27
+ def shelf() -> Assembly:
28
+ asm = Assembly()
29
+ base = Panel(300, 200, label="base")
30
+ divider = Panel(200, 80, label="divider")
31
+ side = Panel(200, 80, label="side")
32
+ asm.ground(base)
33
+ slot_and_tab(asm, divider, "s", base, at=150, along="y", tabs=3)
34
+ finger_joint(asm, side, "s", base, "w")
35
+ return asm
36
+
37
+
38
+ def main(out: Path) -> None:
39
+ out.mkdir(parents=True, exist_ok=True)
40
+ for name, build in (
41
+ ("shelf", shelf()),
42
+ ("box", FingerBox(sheet="Plywood 3 mm", top="closed")),
43
+ ):
44
+ issues = check_flatpack(build)
45
+ sheets = nest(build)
46
+ (out / f"{name}.dxf").write_text(to_dxf(sheets))
47
+ (out / f"{name}.svg").write_text(to_svg(sheets))
48
+ report = sheet_report(sheets)
49
+ print(
50
+ f"{name}: {report['part_count']} parts on {report['sheet_count']} sheet(s), "
51
+ f"{report['cut_length_m']} m of cut, issues: {issues}"
52
+ )
53
+
54
+
55
+ if __name__ == "__main__":
56
+ main(Path(sys.argv[1] if len(sys.argv) > 1 else "flatpack_out"))