Metadata-Version: 2.5
Name: cadbuildr-bom
Version: 0.2.0
Summary: Bills of materials for cadbuildr assemblies: one BOM contract (part numbers, stock + cut length, mass, vendors, cost), exports, rollups, cut lists and stock nesting
Project-URL: Homepage, https://github.com/cadbuildr/bom
Author: Cadbuildr Team
License-Expression: MIT
License-File: LICENSE
Requires-Python: <4.0.0,>=3.10.0
Requires-Dist: cadbuildr-foundation<0.4.0,>=0.3.0
Provides-Extra: pdf
Requires-Dist: fpdf2>=2.7; extra == 'pdf'
Provides-Extra: stdlib
Requires-Dist: cadbuildr-stdlib<0.6.0,>=0.5.0; extra == 'stdlib'
Provides-Extra: xlsx
Requires-Dist: openpyxl>=3.1; extra == 'xlsx'
Description-Content-Type: text/markdown

# CADbuildr BOM

## Summary

Bills of materials for [`cadbuildr-foundation`](https://pypi.org/project/cadbuildr-foundation/)
assemblies. One **common BOM contract** every CADbuildr library feeds
(part number, standard, material, stock + cut length, mass, vendors, cost),
one builder, and what procurement and fabrication need from the result:
costing (unit price, quantity breaks, material and process estimates,
roll-ups per sub-assembly), vendor data and a procurement view (orders per
vendor rounded to pack sizes), cut-list optimisation (1-D bars, 2-D sheets,
waste report), checks, and exports (CSV, XLSX, Markdown, HTML, JSON with a
schema, PDF).

## Quickstart

```bash
pip install cadbuildr-bom                 # core: no third-party dependency
pip install "cadbuildr-bom[xlsx,pdf]"     # styled XLSX (openpyxl), PDF (fpdf2)
pip install "cadbuildr-bom[stdlib]"       # stdlib parts + material prices
```

```python
from cadbuildr.bom import build_bom, PriceBook, VendorRef, set_bom

bom = build_bom(MyMachine())               # cadbuildr-bom@2
print(bom.to_markdown())                   # also to_csv(), to_json(), to_xlsx(), to_html(), to_pdf()
bom.totals()                               # {"mass_kg", "cost": {"EUR": ...}, "unknown_mass": [...], ...}
bom.rollup_tree()                          # totals per sub-assembly, nested like the model

# costing: declared prices, then estimates for what you make
bom = bom.priced(PriceBook.from_csv(open("prices.csv").read()))
bom = bom.costed(rates={"laser": 90, "saw": 35}, currency="EUR", fx={"USD": 0.92})

# procurement: order per vendor, rounded up to packs, price breaks applied
print(bom.procurement(builds=10).to_markdown())
bom.issues()                               # unpriced lines, missing SKUs, missing materials...

# cut lists
print(bom.nest({"2020 aluminium extrusion": [1000, 2000]}, kerf=3).to_text())        # 1-D bars
print(bom.nest_sheets({"Birch plywood 18 mm": (2500, 1250)}, kerf=3).to_text())     # 2-D sheets
bom.to_html("bom.html", sheets=bom.nest_sheets({"Birch plywood 18 mm": (2500, 1250)}))
```

## Catalogue

| Feature | API | Notes |
|---|---|---|
| BOM contract | `BomInfo`, `set_bom`, `register_producer`, `@part(bom=...)`, `__bom__` | merged in order with provenance (`sources`) |
| Build | `build_bom(obj)` → `Bom` | groups by part number, vendor SKU, stock + cut size, or geometry |
| Unit price + breaks | `VendorRef(unit_cost, currency, pack_size, price_breaks, lead_time_days)`, `PriceBook` | breaks are `(min_packs, pack_price)` |
| Material cost | `Bom.costed(material_prices=...)`, `material_price()` | mass × price/kg: custom → `cadbuildr-stdlib` → cited local table |
| Process time | `BomInfo(process, process_time_h, setup_time_h)` + `costed(rates=...)` | optional estimates; rates passed in |
| Currency | `Bom.converted(currency, fx)` | rates passed in, never fetched |
| Roll-ups | `totals()`, `rollup(by)`, `rollup_tree()` | per category / procurement / sub-assembly |
| Vendor links | `VendorRef(vendor, sku, url, lead_time_days)`, `with_links(...)` | identical items merge across the tree |
| Procurement view | `Bom.procurement(builds)` → `Procurement` | per vendor, pack rounding, lead time, cart CSV |
| 1-D nesting | `nest_cuts`, `nest_plan`, `Bom.nest` | FFD + exact branch and bound (≤ 16 pieces), kerf, trim, several stock lengths |
| 2-D nesting | `nest_panels`, `Bom.nest_sheets`, `Sheet` | guillotine shelf heuristic, kerf, margin, grain |
| Waste report | `NestingPlan.waste_report()`, `SheetPlan.waste_report()` | waste %, FFD gap |
| Checks | `bom_issues`, `BomChecks` (`__checks__`), `attach_bom_checks` | foundation check issues |
| Exports | `export(fmt)`: `csv`, `md`, `json`, `xlsx`, `html`, `pdf` | `Bom.from_json` round-trips; `load_schema(2)` |

### Two schemas

| Function | Schema | What it is |
|---|---|---|
| `extract_bom(obj)` / `bom_to_csv` | `cadbuildr-bom@1` | grouped parts by (name, material, geometry hash) with stable ids. Output of the `bom@1` cloud task; unchanged. |
| `build_bom(obj)` → `Bom` | `cadbuildr-bom@2` | the @1 grouping plus the contract fields; purchased parts group by **part number**, stock pieces by (stock, cut length). |

JSON schemas ship in the package: `cadbuildr.bom.load_schema(1)` / `load_schema(2)`
(`cadbuildr/bom/schemas/cadbuildr-bom-{1,2}.schema.json`).

### The contract: where BOM data comes from

`BomInfo` is the one record. `build_bom` merges, in order (later wins, `None`
never erases):

1. **geometry**: part name, paint material, placement-free geometry hash;
2. **library**: the `LibraryRef` stamp of `@part` / `@assembly` instances
   (item id, category, standard, configuration, inputs);
3. **decorator**: `@part(..., bom={"part_number": "ISO4762-{size}x{length:g}"})`,
   templates over the inputs;
4. **producer**: `register_producer("stdlib:HexNut", fn)` adapters. The
   stdlib fasteners, bearings, rods, extrusions, motors and pulleys ship
   here (`cadbuildr.bom.producers.stdlib`);
5. **method**: the component's own `__bom__()`;
6. **explicit**: `set_bom(part, stock="45x45 beech", cut_length=720)` in project code.

Structure: `set_bom(asm, part_number=..., explode=False)` makes a bought
sub-assembly one line; `exclude=True` drops reference geometry;
`add_bom_line(asm, "Wood glue", quantity=30, unit="ml")` adds non-geometric
lines; `ref="R1"` collects reference designators.

### Library items (viewer tab "BOM")

| Item | Kind | What |
|---|---|---|
| `bom:PurchasedPart` | part | box / cylinder envelope of a bought component carrying part number, vendor, SKU, price, mass. Anchors `bottom`, `top`, `center` (`PlaneFace`). |
| `bom:SheetPanel` | part | panel of birch plywood (6.5 / 12 / 18 mm, 2500 × 1250 sheets, 680 kg/m³ per the [WISA specification](https://www.wisaplywood.com/specifying-wisa/sizes-thicknesses-and-weights/)) with cut size and grain; feeds the panel list and sheet nesting. Anchors `bottom`, `top`, `center`, `edge_a`, `edge_b`. |
| `bom:add_bom_line` | operation | non-geometric line on an assembly (consumables, labour, kits). |
| `bom:Bookshelf` | assembly | worked example: stock boards with cut lengths, a hardware line, glue; anchors + `connect`, no transforms; `check()` includes the BOM checks. |

```bash
python -m cadbuildr.foundation.library manifest cadbuildr.bom
```

### Cloud task

On CADbuildr cloud, `extract_bom` powers the `bom@1` task: run it against
any project version and `bom.json` + `bom.csv` land in the project's
artifact repository (`create_task_run(task_type="bom")`).

## Tags

cad, python, library, bom, bill-of-materials, cut-list, nesting, purchasing, open-source

## Status

yellow

## Guidelines

- `cadbuildr-bom@1` is a load-bearing contract (fan-out enumerator for
  per-part tasks): never change its fields or ids. New fields go in `@2`.
- Row ids must be stable across re-runs and commits: nothing that depends on
  the environment (library source sha, paint counters, placement) may reach
  an id or a geometry hash.
- Fail loudly: bad BOM data raises `BomContractError` naming the component
  and the field; impossible nesting raises `CutTooLongError` /
  `PanelTooLargeError` naming the piece; a missing optional package raises
  `OptionalDependencyError` naming it.
- No third-party runtime dependency in the core: openpyxl (XLSX styling),
  fpdf2 (PDF) and cadbuildr-stdlib (material prices) are optional extras.
- Never invent prices, SKUs or exchange rates: they come from the model, a
  price book or the caller. Estimates are marked (`cost_source="estimate"`,
  `mass_source="estimate"`) and every default price table cites its source.
- Checks live in a `__checks__` hook (`BomChecks`); never override `check()`.
- Producers for another library are keyed by library item id and never
  import that library. When the library carries the data itself (decorator
  `bom=` or `__bom__`), delete its producer here.
- Units: mm, kg, ISO 4217 currency codes.

## Dependencies

### Upstream

- `cadbuildr-foundation>=0.3.0,<0.4.0` (component tree, library registry and
  stamps, interfaces, `__checks__`)
- optional: `cadbuildr-stdlib>=0.5.0,<0.6.0` (stdlib producers, material
  prices), `openpyxl`, `fpdf2`

### Downstream

- CADbuildr cloud: the `bom@1` task and the curated library registry
- Libraries adopting the contract: `cadbuildr-stdlib` (via producers today),
  `cadbuildr-frames`, `cadbuildr-tslot`, `cadbuildr-electronics`,
  `cadbuildr-woodworking`
