From f41f900cd599853abd162d1bcb6308e7b0074af7 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 9 Nov 2026 12:39:34 -0500 Subject: [PATCH 1/2] Add sourced mechanical properties to the filament schema An optional `mechanical` array on filament.json (schemas/mechanical_schema.json): one entry per published value (tensile, flexural, impact, heat deflection) with its print direction, unit as published, standard, condition, test-bar details and the sustainability-style source fields. Follows #768: data-quality checks (sources, value in quote, unit fits the property), CSV/SQLite columns, key order and tracker stripping in style_data, WebUI display and editor, manual section and tests. The number-in-quote check also accepts thousands separators (7,250 psi). Refs #892 Co-Authored-By: Claude Opus 5.5 --- docs/manual.md | 51 ++++ ofd/builder/exporters/csv_exporter.py | 1 + ofd/builder/exporters/sqlite_exporter.py | 3 +- ofd/scripts/style_data.py | 11 + ofd/validation/data_quality.py | 112 +++++++- schemas/filament_schema.json | 5 + schemas/mechanical_schema.json | 260 ++++++++++++++++++ tests/test_data_quality.py | 94 +++++++ tests/test_mechanical_schema.py | 214 ++++++++++++++ .../entity/MechanicalDisplay.svelte | 156 +++++++++++ .../__tests__/MechanicalDisplay.test.ts | 52 ++++ webui/src/lib/components/entity/index.ts | 1 + .../form-fields/MechanicalEditor.svelte | 148 ++++++++++ .../form-fields/SourcedEntryFields.svelte | 52 +++- .../__tests__/MechanicalEditor.test.ts | 78 ++++++ .../__tests__/MechanicalEditorHarness.svelte | 10 + webui/src/lib/components/form-fields/index.ts | 1 + .../lib/components/forms/FilamentForm.svelte | 50 +++- webui/src/lib/server/cloudValidator.ts | 25 +- webui/src/lib/services/schemaService.ts | 6 +- webui/src/lib/types/changes.ts | 7 +- webui/src/lib/types/database.ts | 61 ++++ .../lib/utils/__tests__/dataQuality.test.ts | 126 ++++++++- .../lib/utils/__tests__/mechanical.test.ts | 132 +++++++++ webui/src/lib/utils/dataQuality.ts | 115 +++++++- webui/src/lib/utils/mechanical.ts | 170 ++++++++++++ .../routes/api/schemas/mechanical/+server.ts | 3 + .../[material]/[filament]/+page.svelte | 13 +- 28 files changed, 1915 insertions(+), 42 deletions(-) create mode 100644 schemas/mechanical_schema.json create mode 100644 tests/test_mechanical_schema.py create mode 100644 webui/src/lib/components/entity/MechanicalDisplay.svelte create mode 100644 webui/src/lib/components/entity/__tests__/MechanicalDisplay.test.ts create mode 100644 webui/src/lib/components/form-fields/MechanicalEditor.svelte create mode 100644 webui/src/lib/components/form-fields/__tests__/MechanicalEditor.test.ts create mode 100644 webui/src/lib/components/form-fields/__tests__/MechanicalEditorHarness.svelte create mode 100644 webui/src/lib/utils/__tests__/mechanical.test.ts create mode 100644 webui/src/lib/utils/mechanical.ts create mode 100644 webui/src/routes/api/schemas/mechanical/+server.ts diff --git a/docs/manual.md b/docs/manual.md index 9f48a9ee4a..3825f995b0 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -121,6 +121,7 @@ A filament represents a **product line** (e.g. "Silk PLA", "Glow PLA"), **not a - Lifecycle: `discontinued` (boolean) - Slicer integration: `slicer_ids` (PrusaSlicer / BambuStudio / OrcaSlicer / Cura native IDs) and `slicer_settings` (per-slicer profile names, IDs, and temperature overrides) - Sustainability: `sustainability` — sourced recycled / bio-based content, carbon footprints and relative claims. See [🌱 Sustainability data](#-sustainability-data); every figure needs a source. + - Mechanical data: `mechanical` — sourced tensile, flexural, impact and heat deflection values from the data sheet, per print direction. See [🔩 Mechanical data](#-mechanical-data); every value needs a source. ## 🎨 Adding a Variant A variant represents a single colour or finish of a filament. @@ -253,6 +254,56 @@ Sustainability figures are used to calculate the CO₂ footprint of prints, and Checked a brand and found nothing? Record that in [`docs/sustainability-sources.md`](sustainability-sources.md). A negative result is useful too. +## 🔩 Mechanical data +Data sheets give stiffness, strength, impact and heat values, often **per print direction**: a part is weaker across its layers than along them. `mechanical` keeps those values with their source, so CAD, simulation and slicer tools can use them. + +### The rules +1. **Copy, never compute.** Enter each value in the unit the sheet uses: a sheet in psi stays in psi, and `3.86 GPa` is never entered as `3860 MPa`. Never average, convert or estimate. +2. **Every entry needs a source**: `source_url`, `source_quote` and `retrieved`, as for [sustainability data](#-sustainability-data). The quote is the sheet's row, verbatim, and must contain the value (and `plus_minus`, if set). `5.85` matches `5,85`, and `7250` matches `7,250`. +3. **Primary sources first**: the brand's own data sheet (TDS) or product page. Mark a retailer copy `source_type: "secondary"`. +4. **One entry per value**: one for X-Y, one for Z, one for dry and one for conditioned. If the sheet gives a range or a footnote, quote it and explain it in `notes`. +5. **No generic values in `data/`.** Typical values for a material type (PLA, PETG...) are not a brand's data. + +`ofd validate` checks the sources, the value in the quote, dates in the future and that the unit fits the property (errors). It warns about a print direction on moulded bars and about repeated entries. + +### Where it goes +`mechanical` is an array on `filament.json`. Variants don't override it: a data sheet covers the product line. + +```json +"mechanical": [ + { + "property": "tensile_modulus", "direction": "XY", + "value": 3860, "plus_minus": 230, "unit": "MPa", + "standard": "ISO 527", "condition": "dry", + "specimen": {"process": "printed", "infill_percent": 100, "print_temperature": 290, + "conditioning": "annealed and dried at 80 °C for 12 h"}, + "source_type": "primary", + "source_url": "https://store.bblcdn.com/7166b20bc68f4c87b32c972b856eeb4b.pdf", + "source_quote": "Young's Modulus (X-Y) ISO 527, GB/T 1040 3860 ± 230 MPa", + "retrieved": "2026-11-08" + } +] +``` + +The sheet's Z row (`2180 ± 130 MPa`) is a second entry with `"direction": "Z"` and its own quote. + +### Field guide +| Field | Values | Meaning | +|---|---|---| +| `property` | `tensile_modulus`, `tensile_strength`, `yield_strength`, `elongation_at_break`, `flexural_modulus`, `flexural_strength`, `impact_strength`, `impact_strength_notched`, `heat_deflection_045`, `heat_deflection_18`, `vicat_softening` | What was measured. `heat_deflection_045` / `_18` are HDT at 0.45 / 1.8 MPa. Young's modulus is `tensile_modulus` | +| `direction` | `XY`, `XZ`, `Z`, `unknown` | `XY`: bars printed flat (along the layers). `XZ`: on edge. `Z`: upright (across the layers). `unknown` when the sheet gives none, and for moulded bars | +| `value`, `plus_minus` | numbers | As written. `plus_minus` is the "±" spread, or `null` | +| `unit` | `MPa`, `GPa`, `psi`, `ksi`, `%`, `kJ/m2`, `J/m`, `ft-lb/in`, `°C`, `°F` | Must fit the property: moduli and strengths in `MPa`, `GPa`, `psi` or `ksi`; elongation in `%`; impact in `kJ/m2` (ISO 179 / 180), `J/m` or `ft-lb/in` (ASTM D256); heat in `°C` or `°F` | +| `standard` | free text or `null` | **Only** when the sheet names one, e.g. "ISO 527" or "ASTM D638" | +| `condition` | `dry`, `conditioned` | Moisture state at test time, when the sheet gives it (common for nylons) | +| `specimen` | object | How the bars were made: `process` (`printed`, `moulded`, `unknown`), `printer`, `nozzle_diameter`, `layer_height`, `infill_percent`, `perimeters`, `raster_angle`, `print_temperature`, `bed_temperature`, `chamber_temperature`, `print_speed`, `conditioning` (e.g. "annealed at 80 °C for 12 h"). Leave out what the sheet doesn't say | + +### Valid sources +- ✅ The brand's technical data sheet (TDS) or product page +- ✅ A retailer copy of the brand's sheet, **only** when the original is gone (`source_type: "secondary"`) +- ❌ Your own test results, or values from a review or forum +- ❌ Values you converted, averaged or read off a chart + ## 🏪 Adding a Store Stores are referenced by `purchase_links[].store_id` and live under `/stores`. diff --git a/ofd/builder/exporters/csv_exporter.py b/ofd/builder/exporters/csv_exporter.py index e25a3c7374..348cb41e15 100644 --- a/ofd/builder/exporters/csv_exporter.py +++ b/ofd/builder/exporters/csv_exporter.py @@ -56,6 +56,7 @@ "enclosure", "discontinued", "sustainability", + "mechanical", ], "variant": [ "id", diff --git a/ofd/builder/exporters/sqlite_exporter.py b/ofd/builder/exporters/sqlite_exporter.py index 0bf2529a8a..28620d7a8e 100644 --- a/ofd/builder/exporters/sqlite_exporter.py +++ b/ofd/builder/exporters/sqlite_exporter.py @@ -93,7 +93,8 @@ safety_sheet_url TEXT, discontinued INTEGER NOT NULL DEFAULT 0, slicer_settings TEXT, -- JSON - sustainability TEXT -- JSON (see schemas/sustainability_schema.json) + sustainability TEXT, -- JSON (see schemas/sustainability_schema.json) + mechanical TEXT -- JSON (see schemas/mechanical_schema.json) ); CREATE INDEX IF NOT EXISTS ix_filament_brand ON filament(brand_id); CREATE INDEX IF NOT EXISTS ix_filament_material ON filament(material_id); diff --git a/ofd/scripts/style_data.py b/ofd/scripts/style_data.py index a0788efa4c..cd2c9e8149 100644 --- a/ofd/scripts/style_data.py +++ b/ofd/scripts/style_data.py @@ -255,6 +255,7 @@ def sanitize_data(data: Any, schema_name: str) -> tuple[Any, list[str]]: - sizes: default sizes[].diameter from 0 to 1.75; sanitize nested purchase_links and container_details dicts. - filament/variant: sanitize the sourced entries inside ``sustainability``. + - filament: sanitize each sourced entry in ``mechanical``. """ changes: list[str] = [] @@ -285,6 +286,7 @@ def sanitize_data(data: Any, schema_name: str) -> tuple[Any, list[str]]: _sanitize_dict(data, changes) _sanitize_sustainability(data.get("sustainability"), changes) + _sanitize_mechanical(data.get("mechanical"), changes) return data, changes return data, changes @@ -303,6 +305,15 @@ def _sanitize_sustainability(sustainability: Any, changes: list[str]) -> None: _sanitize_dict(item, changes, f"sustainability.{section}[{i}]") +def _sanitize_mechanical(mechanical: Any, changes: list[str]) -> None: + """Sanitize each sourced entry in a filament's ``mechanical`` array.""" + if not isinstance(mechanical, list): + return + for i, item in enumerate(mechanical): + if isinstance(item, dict): + _sanitize_dict(item, changes, f"mechanical[{i}]") + + def _sanitize_dict(data: dict[str, Any], changes: list[str], prefix: str = "") -> None: """Sanitize string fields in a dict in-place.""" dot = f"{prefix}." if prefix else "" diff --git a/ofd/validation/data_quality.py b/ofd/validation/data_quality.py index d46c1b93d0..44e81ff5ac 100644 --- a/ofd/validation/data_quality.py +++ b/ofd/validation/data_quality.py @@ -33,6 +33,11 @@ ``traits.recycled`` with no recycled content only WARNs, and only once the filament or variant has a sustainability block. +Mechanical data (``mechanical`` on filaments) follows the same sourcing rules: a +missing source field, a value or ``plus_minus`` absent from its ``source_quote``, a +``retrieved`` date in the future and a unit that does not fit the property are +ERRORs. A print direction on moulded test bars and a repeated entry only WARN. + A WARNING here is advice, not a failure: ``ofd validate`` still exits 0 and reports "All validations passed (N warnings)". Reviewers should not describe a warning as something that breaks the build or makes data unreachable. @@ -693,10 +698,15 @@ def _number_pattern(value) -> re.Pattern: """Match ``value`` as written in prose: ``5.85`` or ``5,85``, trailing zeros allowed. The digit boundaries stop ``5.85`` from matching inside ``15.85`` or ``5.851``. + Thousands may be grouped with a comma or a space, as data sheets in psi do: + ``7250`` also matches ``7,250`` and ``7 250``. """ text = repr(float(value)).rstrip("0").rstrip(".") int_part, _, frac = text.partition(".") - number = re.escape(int_part) + (f"[.,]{frac}0*" if frac else r"(?:[.,]0+)?") + head = len(int_part) % 3 or 3 + groups = [int_part[:head]] + [int_part[i : i + 3] for i in range(head, len(int_part), 3)] + integer = re.escape(groups[0]) + "".join(f"[,\u00a0\u202f ]?{g}" for g in groups[1:]) + number = integer + (f"[.,]{frac}0*" if frac else r"(?:[.,]0+)?") return re.compile(rf"(? list[ValidationError]: """One sourced entry: sources present, percentages in range, dates not in the future, and every number present verbatim in ``source_quote``.""" @@ -722,7 +737,7 @@ def _check_sourced_entry( errors.append( _err( ValidationLevel.Error, - f"{path} is missing {', '.join(missing)}. Every sustainability figure " + f"{path} is missing {', '.join(missing)}. Every {kind} figure " "needs a source URL, a verbatim quote and the date it was retrieved.", rel, ) @@ -845,6 +860,96 @@ def _check_recycled_trait( return [] +# --- Rules: mechanical data --------------------------------------------------- +# +# Mechanical values are copied from the brand's data sheet like sustainability figures: +# in the unit the sheet uses, with the quote that shows them. See +# docs/manual.md#-mechanical-data. + +_PRESSURE_UNITS = frozenset({"MPa", "GPa", "psi", "ksi"}) +_IMPACT_UNITS = frozenset({"kJ/m2", "J/m", "ft-lb/in"}) +_TEMPERATURE_UNITS = frozenset({"°C", "°F"}) + +# Units that fit each property. +_MECHANICAL_UNITS = { + "tensile_modulus": _PRESSURE_UNITS, + "tensile_strength": _PRESSURE_UNITS, + "yield_strength": _PRESSURE_UNITS, + "flexural_modulus": _PRESSURE_UNITS, + "flexural_strength": _PRESSURE_UNITS, + "elongation_at_break": frozenset({"%"}), + "impact_strength": _IMPACT_UNITS, + "impact_strength_notched": _IMPACT_UNITS, + "heat_deflection_045": _TEMPERATURE_UNITS, + "heat_deflection_18": _TEMPERATURE_UNITS, + "vicat_softening": _TEMPERATURE_UNITS, +} + +_PRINT_DIRECTIONS = {"XY", "XZ", "Z"} + + +def _check_mechanical(data: dict, file_path: Path, base: Path) -> list[ValidationError]: + """Every entry in a filament's ``mechanical`` array: sourced like a sustainability + figure, a unit that fits the property, no print direction on moulded bars, and no + repeats.""" + entries = data.get("mechanical") + if not isinstance(entries, list): + return [] + rel = _rel(file_path, base) + errors: list[ValidationError] = [] + seen: dict[tuple, int] = {} + for i, entry in enumerate(entries): + if not isinstance(entry, dict): + continue + path = f"mechanical[{i}]" + errors.extend( + _check_sourced_entry( + path, entry, ("value", "plus_minus"), file_path, base, kind="mechanical" + ) + ) + + prop, unit = entry.get("property"), entry.get("unit") + allowed = _MECHANICAL_UNITS.get(prop) + if allowed and isinstance(unit, str) and unit not in allowed: + errors.append( + _err( + ValidationLevel.Error, + f"{path}.unit is {unit!r}, which does not fit {prop}; use one of " + f"{', '.join(sorted(allowed))}, as the source writes it.", + rel, + ) + ) + + specimen = entry.get("specimen") + process = specimen.get("process") if isinstance(specimen, dict) else None + direction = entry.get("direction") + if process == "moulded" and direction in _PRINT_DIRECTIONS: + errors.append( + _err( + ValidationLevel.Warning, + f"{path}.direction is {direction}, but the test bars were moulded; " + "moulded bars have no print direction (use unknown).", + rel, + ) + ) + + standard = entry.get("standard") + standard = standard.strip() or None if isinstance(standard, str) else standard + key = (prop, direction, entry.get("condition"), standard, unit) + if key in seen: + errors.append( + _err( + ValidationLevel.Warning, + f"{path} repeats mechanical[{seen[key]}] (same property, direction, " + "condition, standard and unit).", + rel, + ) + ) + else: + seen[key] = i + return errors + + # --- Entry point -------------------------------------------------------------- @@ -888,6 +993,7 @@ def check_data_quality(data_dir) -> list[ValidationError]: errors.extend(_check_inverted_ranges(filament_data, filament_file, base)) errors.extend(_check_orphan_filament(filament_dir, base)) errors.extend(_check_sustainability(filament_data, filament_file, base)) + errors.extend(_check_mechanical(filament_data, filament_file, base)) name = filament_data.get("name") if isinstance(name, str) and name: diff --git a/schemas/filament_schema.json b/schemas/filament_schema.json index 52ae4125ec..00affb2fcf 100644 --- a/schemas/filament_schema.json +++ b/schemas/filament_schema.json @@ -326,6 +326,11 @@ "title": "Sustainability", "description": "Sourced sustainability data (recycled and bio-based content, carbon footprints, relative claims). Every number must be copied verbatim from a cited source." }, + "mechanical": { + "$ref": "./mechanical_schema.json", + "title": "Mechanical Properties", + "description": "Sourced mechanical properties (tensile, flexural, impact, heat deflection) per print direction, from the brand's data sheet. Every value must be copied verbatim from a cited source." + }, "slicer_ids": { "type": "object", "title": "Slicer IDs", diff --git a/schemas/mechanical_schema.json b/schemas/mechanical_schema.json new file mode 100644 index 0000000000..feb37e0629 --- /dev/null +++ b/schemas/mechanical_schema.json @@ -0,0 +1,260 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema", + "$id": "./mechanical_schema.json", + "title": "Mechanical Properties", + "description": "Sourced mechanical properties from the brand's data sheet. Copy, never compute: every value must appear verbatim in source_quote, in the unit the source uses. One entry per value, per print direction and condition.", + "type": "array", + "items": { + "type": "object", + "title": "Mechanical Property", + "description": "One published value, copied verbatim from the source", + "properties": { + "property": { + "type": "string", + "title": "Property", + "description": "What was measured. heat_deflection_045 / _18 are HDT at 0.45 / 1.8 MPa load.", + "enum": [ + "tensile_modulus", + "tensile_strength", + "yield_strength", + "elongation_at_break", + "flexural_modulus", + "flexural_strength", + "impact_strength", + "impact_strength_notched", + "heat_deflection_045", + "heat_deflection_18", + "vicat_softening" + ] + }, + "direction": { + "type": "string", + "title": "Direction", + "description": "Print direction of the test bars: XY = flat, along the layers; XZ = on edge; Z = upright, across the layers. unknown when the source gives none (always for moulded bars).", + "enum": [ + "XY", + "XZ", + "Z", + "unknown" + ] + }, + "value": { + "type": "number", + "title": "Value", + "description": "The value exactly as published, in unit" + }, + "plus_minus": { + "type": [ + "number", + "null" + ], + "minimum": 0, + "title": "±", + "description": "Spread the source gives as ±, in unit. null when it gives none." + }, + "unit": { + "type": "string", + "title": "Unit", + "description": "Unit as published. Never convert: a sheet in psi stays in psi.", + "enum": [ + "MPa", + "GPa", + "psi", + "ksi", + "%", + "kJ/m2", + "J/m", + "ft-lb/in", + "°C", + "°F" + ] + }, + "standard": { + "type": [ + "string", + "null" + ], + "maxLength": 200, + "title": "Standard", + "description": "Test standard named by the source, e.g. ISO 527 or ASTM D638. Only if the source names one." + }, + "condition": { + "type": "string", + "title": "Condition", + "description": "Moisture state at test time when the source gives it: dry or conditioned (e.g. 23 °C / 50% RH). Most often given for nylons.", + "enum": [ + "dry", + "conditioned" + ] + }, + "specimen": { + "type": "object", + "title": "Specimen", + "description": "How the test bars were made, as stated by the source. Leave out what the source doesn't say.", + "properties": { + "process": { + "type": "string", + "title": "Process", + "description": "printed = 3D printed bars; moulded = injection or compression moulded bars (no print direction)", + "enum": [ + "printed", + "moulded", + "unknown" + ] + }, + "printer": { + "type": [ + "string", + "null" + ], + "maxLength": 200, + "title": "Printer", + "description": "Printer named by the source" + }, + "nozzle_diameter": { + "type": [ + "number", + "null" + ], + "exclusiveMinimum": 0, + "title": "Nozzle Diameter", + "description": "Nozzle diameter in mm" + }, + "layer_height": { + "type": [ + "number", + "null" + ], + "exclusiveMinimum": 0, + "title": "Layer Height", + "description": "Layer height in mm" + }, + "infill_percent": { + "type": [ + "number", + "null" + ], + "minimum": 0, + "maximum": 100, + "title": "Infill %", + "description": "Infill in percent (0-100)" + }, + "perimeters": { + "type": [ + "integer", + "null" + ], + "minimum": 0, + "title": "Perimeters", + "description": "Number of walls / shells" + }, + "raster_angle": { + "type": [ + "string", + "null" + ], + "maxLength": 100, + "title": "Raster Angle", + "description": "Infill direction as written, e.g. ±45°" + }, + "print_temperature": { + "type": [ + "number", + "null" + ], + "title": "Print Temperature", + "description": "Nozzle temperature in °C" + }, + "bed_temperature": { + "type": [ + "number", + "null" + ], + "title": "Bed Temperature", + "description": "Bed temperature in °C" + }, + "chamber_temperature": { + "type": [ + "number", + "null" + ], + "title": "Chamber Temperature", + "description": "Chamber temperature in °C" + }, + "print_speed": { + "type": [ + "number", + "null" + ], + "exclusiveMinimum": 0, + "title": "Print Speed", + "description": "Print speed in mm/s" + }, + "conditioning": { + "type": [ + "string", + "null" + ], + "maxLength": 500, + "title": "Conditioning", + "description": "Drying, annealing or moisture conditioning before the test, as written, e.g. annealed at 80 °C for 12 h" + } + }, + "additionalProperties": false + }, + "source_type": { + "type": "string", + "title": "Source Type", + "description": "primary = the brand's own data sheet (TDS), product page or PDF. secondary = a retailer or press copy of the brand's sheet, only when the original can't be reached.", + "enum": [ + "primary", + "secondary" + ] + }, + "source_url": { + "type": "string", + "title": "Source URL", + "description": "Where the figure is published", + "format": "uri", + "pattern": "^https?://", + "maxLength": 1000, + "x-placeholder": "https://example.com/..." + }, + "source_quote": { + "type": "string", + "title": "Source Quote", + "description": "Verbatim text from the source. Must contain the value (and plus_minus, if set) exactly as written.", + "minLength": 1, + "maxLength": 2000 + }, + "retrieved": { + "type": "string", + "format": "date", + "pattern": "^\\d{4}-\\d{2}-\\d{2}$", + "title": "Retrieved", + "description": "Date the source was read (YYYY-MM-DD)" + }, + "notes": { + "type": [ + "string", + "null" + ], + "maxLength": 1000, + "title": "Notes", + "description": "Caveats, such as a range the source gives, conflicting sheets, or anything a reader needs to interpret the value" + } + }, + "additionalProperties": false, + "required": [ + "property", + "direction", + "value", + "unit", + "source_type", + "source_url", + "source_quote", + "retrieved" + ] + }, + "minItems": 1 +} \ No newline at end of file diff --git a/tests/test_data_quality.py b/tests/test_data_quality.py index 16f281a35b..ebd58becbe 100644 --- a/tests/test_data_quality.py +++ b/tests/test_data_quality.py @@ -850,3 +850,97 @@ def test_container_details_are_checked(tmp_path): errors = sustainability_errors(tmp_path, sizes=sizes) found = messages(errors, "container_details.recycled_pct is 90") assert len(found) == 1 + + +# --- Mechanical data -------------------------------------------------------------- +# +# Values from a brand's data sheet follow the same sourcing rules: copy, never compute. + +SHEET = { + "source_type": "primary", + "source_url": "https://store.bblcdn.com/7166b20bc68f4c87b32c972b856eeb4b.pdf", + "retrieved": "2026-11-08", +} + + +def mech(prop, direction, value, unit, quote, **extra): + return { + "property": prop, + "direction": direction, + "value": value, + "unit": unit, + **SHEET, + "source_quote": quote, + **extra, + } + + +def modulus_xy(**extra): + quote = "Young's Modulus (X-Y) ISO 527, GB/T 1040 3860 ± 230 MPa" + return mech("tensile_modulus", "XY", 3860, "MPa", quote, plus_minus=230, **extra) + + +def mechanical_errors(tmp_path, *entries): + data = build( + tmp_path / "data", + filament_extra={"mechanical": list(entries)}, + variants=[{"id": "black", "name": "Black"}], + ) + return check_data_quality(data) + + +def test_sourced_mechanical_entry_passes(tmp_path): + quote = "Young's Modulus (Z) ISO 527, GB/T 1040 2180 ± 130 MPa" + z = mech("tensile_modulus", "Z", 2180, "MPa", quote, plus_minus=130) + assert mechanical_errors(tmp_path, modulus_xy(), z) == [] + + +def test_mechanical_value_missing_from_quote_is_an_error(tmp_path): + entry = modulus_xy(source_quote="Young's Modulus (X-Y) 3.86 GPa") + found = messages(mechanical_errors(tmp_path, entry), "does not contain that number") + assert len(found) == 2 # value and plus_minus + assert found[0].level.value == "ERROR" + + +def test_mechanical_psi_with_thousands_separator_passes(tmp_path): + entry = mech("tensile_strength", "XY", 7250, "psi", "Tensile Strength ASTM D638 7,250 psi") + assert mechanical_errors(tmp_path, entry) == [] + + +def test_mechanical_missing_source_is_an_error(tmp_path): + entry = modulus_xy() + del entry["source_url"] + found = messages(mechanical_errors(tmp_path, entry), "is missing source_url") + assert len(found) == 1 + assert "Every mechanical figure" in found[0].message + + +def test_mechanical_future_retrieved_is_an_error(tmp_path): + errors = mechanical_errors(tmp_path, modulus_xy(retrieved="2999-01-01")) + assert len(messages(errors, "is in the future")) == 1 + + +def test_mechanical_unit_that_does_not_fit_is_an_error(tmp_path): + entry = mech("elongation_at_break", "XY", 2.6, "MPa", "Elongation at Break (X-Y) 2.6 MPa") + found = messages(mechanical_errors(tmp_path, entry), "does not fit elongation_at_break") + assert len(found) == 1 + assert found[0].level.value == "ERROR" + + +def test_mechanical_direction_on_moulded_bars_warns(tmp_path): + entry = modulus_xy(specimen={"process": "moulded"}) + found = messages(mechanical_errors(tmp_path, entry), "moulded bars have no print direction") + assert len(found) == 1 + assert found[0].level.value == "WARNING" + + +def test_mechanical_repeat_warns(tmp_path): + found = messages(mechanical_errors(tmp_path, modulus_xy(), modulus_xy()), "repeats") + assert len(found) == 1 + assert found[0].level.value == "WARNING" + + +def test_mechanical_same_property_other_standard_is_not_a_repeat(tmp_path): + iso = modulus_xy(standard="ISO 527") + astm = modulus_xy(standard="ASTM D638") + assert messages(mechanical_errors(tmp_path, iso, astm), "repeats") == [] diff --git a/tests/test_mechanical_schema.py b/tests/test_mechanical_schema.py new file mode 100644 index 0000000000..12132f30d1 --- /dev/null +++ b/tests/test_mechanical_schema.py @@ -0,0 +1,214 @@ +"""Mechanical schema, its cross-file $ref, and how the exporters and styler carry it. + +`mechanical` lives in schemas/mechanical_schema.json and is pulled into the filament +schema with a cross-file `$ref`, like `sustainability`, so every validator that reads +the schemas must resolve that reference. +""" + +import csv +import json +import shutil +import sqlite3 +from pathlib import Path + +import ofd_validator +import pytest +from jsonschema import Draft7Validator + +from ofd.builder.exporters import export_csv, export_sqlite +from ofd.builder.models import Database +from ofd.scripts.export_data import SchemaLoader +from ofd.scripts.style_data import build_key_order_map, sanitize_data, sort_json_keys + +ROOT = Path(__file__).resolve().parent.parent +SCHEMAS = ROOT / "schemas" + +PAHT_CF_SHEET = "https://store.bblcdn.com/7166b20bc68f4c87b32c972b856eeb4b.pdf" + +PAHT_CF_MODULUS_XY = { + "property": "tensile_modulus", + "direction": "XY", + "value": 3860, + "plus_minus": 230, + "unit": "MPa", + "standard": "ISO 527", + "condition": "dry", + "specimen": { + "process": "printed", + "infill_percent": 100, + "print_temperature": 290, + "conditioning": "annealed and dried at 80 °C for 12 h", + }, + "source_type": "primary", + "source_url": PAHT_CF_SHEET, + "source_quote": "Young's Modulus (X-Y) ISO 527, GB/T 1040 3860 ± 230 MPa", + "retrieved": "2026-11-08", + "notes": None, +} + + +def validator(name): + loader = SchemaLoader(SCHEMAS) + return Draft7Validator(loader.get(name), registry=loader.registry) + + +def filament(*mechanical): + return { + "id": "paht_cf", + "name": "PAHT-CF", + "density": 1.06, + "diameter_tolerance": 0.02, + "mechanical": list(mechanical), + } + + +def error_messages(name, doc): + return [e.message for e in validator(name).iter_errors(doc)] + + +def test_full_entry_is_valid(): + assert error_messages("filament", filament(PAHT_CF_MODULUS_XY)) == [] + + +def test_minimal_entry_is_valid(): + entry = { + "property": "heat_deflection_045", + "direction": "unknown", + "value": 194, + "unit": "°C", + "source_type": "primary", + "source_url": PAHT_CF_SHEET, + "source_quote": "Heat Deflection Temperature (0.45 MPa) ISO 75 194 °C", + "retrieved": "2026-11-08", + } + assert error_messages("filament", filament(entry)) == [] + + +def test_unknown_enum_value_is_rejected(): + entry = {**PAHT_CF_MODULUS_XY, "direction": "sideways"} + assert error_messages("filament", filament(entry)) + + +def test_unknown_key_is_rejected(): + entry = {**PAHT_CF_MODULUS_XY, "value_converted_gpa": 3.86} + assert error_messages("filament", filament(entry)) + + +def test_unknown_specimen_key_is_rejected(): + entry = {**PAHT_CF_MODULUS_XY, "specimen": {"process": "printed", "colour": "black"}} + assert error_messages("filament", filament(entry)) + + +def test_missing_source_is_rejected(): + entry = {k: v for k, v in PAHT_CF_MODULUS_XY.items() if k != "source_quote"} + assert error_messages("filament", filament(entry)) + + +def test_empty_array_is_rejected(): + # Leave the field out when there is no data; an empty array says nothing. + assert error_messages("filament", filament()) + + +def test_variant_has_no_mechanical_field(): + variant = { + "id": "black", + "name": "Black", + "color_hex": "#000000", + "mechanical": [PAHT_CF_MODULUS_XY], + } + assert error_messages("variant", variant) + + +def test_ofd_validator_resolves_the_cross_file_ref(tmp_path): + """The Rust validator must find mechanical_schema.json for the `$ref`.""" + src = ROOT / "data" / "bambu_lab" + if not (src / "PA6" / "paht_cf").exists(): + pytest.skip("bambu_lab PAHT-CF not in data/") + data = tmp_path / "data" / "bambu_lab" + data.mkdir(parents=True) + for f in src.iterdir(): + if f.is_file(): + shutil.copy2(f, data / f.name) + (data / "PA6").mkdir() + shutil.copy2(src / "PA6" / "material.json", data / "PA6" / "material.json") + shutil.copytree(src / "PA6" / "paht_cf", data / "PA6" / "paht_cf") + + fil = data / "PA6" / "paht_cf" / "filament.json" + doc = json.loads(fil.read_text(encoding="utf-8")) + doc["mechanical"] = [PAHT_CF_MODULUS_XY] + fil.write_text(json.dumps(doc), encoding="utf-8") + + def schema_errors(): + result = ofd_validator.validate_json_files( + str(tmp_path / "data"), str(ROOT / "stores"), str(SCHEMAS) + ) + return [e.message for e in result.errors if "chema" in e.message] + + assert schema_errors() == [] + + doc["mechanical"][0]["direction"] = "sideways" + fil.write_text(json.dumps(doc), encoding="utf-8") + assert any("sideways" in m for m in schema_errors()) + + +def make_db(): + db = Database() + db.brands = [ + { + "id": "b1", + "name": "Acme", + "slug": "acme", + "origin": "US", + "website": "https://a.co", + "logo_name": "acme.png", + } + ] + db.materials = [ + {"id": "m1", "brand_id": "b1", "material": "PA6", "slug": "PA6", "material_class": "FFF"} + ] + db.filaments = [ + { + "id": "f1", + "brand_id": "b1", + "material_id": "m1", + "name": "PAHT-CF", + "slug": "paht_cf", + "material": "PA6", + "density": 1.06, + "diameter_tolerance": 0.02, + "discontinued": False, + "mechanical": [PAHT_CF_MODULUS_XY], + } + ] + return db + + +def test_sqlite_stores_mechanical_as_json(tmp_path): + export_sqlite(make_db(), str(tmp_path), "v", "now") + conn = sqlite3.connect(tmp_path / "sqlite" / "filaments.db") + (mech,) = conn.execute("SELECT mechanical FROM filament").fetchone() + conn.close() + assert json.loads(mech)[0]["value"] == 3860 + + +def test_csv_has_mechanical_column(tmp_path): + export_csv(make_db(), str(tmp_path), "v", "now") + with open(next((tmp_path / "csv").glob("filament*.csv")), encoding="utf-8") as f: + row = next(csv.DictReader(f)) + assert json.loads(row["mechanical"])[0]["specimen"]["infill_percent"] == 100 + + +def test_style_orders_entry_and_specimen_keys(): + info = build_key_order_map(SCHEMAS)["filament"] + entry = dict(reversed(list(PAHT_CF_MODULUS_XY.items()))) + entry["specimen"] = dict(reversed(list(entry["specimen"].items()))) + styled = sort_json_keys(filament(entry), info, set())["mechanical"][0] + assert list(styled) == list(PAHT_CF_MODULUS_XY) + assert list(styled["specimen"]) == list(PAHT_CF_MODULUS_XY["specimen"]) + + +def test_style_strips_tracking_from_source_url(): + entry = {**PAHT_CF_MODULUS_XY, "source_url": PAHT_CF_SHEET + "?utm_source=newsletter"} + data, changes = sanitize_data(filament(entry), "filament") + assert data["mechanical"][0]["source_url"] == PAHT_CF_SHEET + assert any("mechanical[0]" in c for c in changes) diff --git a/webui/src/lib/components/entity/MechanicalDisplay.svelte b/webui/src/lib/components/entity/MechanicalDisplay.svelte new file mode 100644 index 0000000000..19ee325a32 --- /dev/null +++ b/webui/src/lib/components/entity/MechanicalDisplay.svelte @@ -0,0 +1,156 @@ + + +
+
+ + + + + {#each table.directions as direction (direction)} + + {/each} + + + + {#each table.rows as row, r (r)} + + + {#each table.directions as direction (direction)} + + {/each} + + {/each} + +
Property + {DIRECTION_LABELS[direction]} +
+
{propertyLabel(row.property)}
+ {#if row.standard || row.condition} +
+ {rowDetail(row.standard, row.condition)} +
+ {/if} +
+ {#each row.cells[direction] ?? [] as entry, k (k)} +
+ + {formatMechanicalValue(entry)} + + {#if entry.specimen?.process === 'moulded'} + (moulded) + {/if} +
+ {:else} + – + {/each} +
+
+ +
+ Sources and test details + {#each sources as { url, entries, secondary, retrieved } (url)} +
+ + + {url} + + + + {secondary ? '(secondary), ' : ''}retrieved {retrieved} + +
    + {#each entries as entry, k (k)} +
  • + + {propertyLabel(entry.property)}{entry.direction === 'unknown' + ? '' + : ` ${entry.direction}`}: {formatMechanicalValue(entry)} + +
    + “{entry.source_quote}” +
    + {#if specimenSummary(entry.specimen)} +

    {specimenSummary(entry.specimen)}

    + {/if} + {#if entry.notes}

    {entry.notes}

    {/if} +
  • + {/each} +
+
+ {/each} +
+
diff --git a/webui/src/lib/components/entity/__tests__/MechanicalDisplay.test.ts b/webui/src/lib/components/entity/__tests__/MechanicalDisplay.test.ts new file mode 100644 index 0000000000..3e42ed9083 --- /dev/null +++ b/webui/src/lib/components/entity/__tests__/MechanicalDisplay.test.ts @@ -0,0 +1,52 @@ +import { describe, it, expect } from 'vitest'; +import { render, screen } from '@testing-library/svelte'; +import type { MechanicalProperty } from '$lib/types/database'; +import MechanicalDisplay from '../MechanicalDisplay.svelte'; + +const SOURCE = { + source_type: 'primary' as const, + source_url: 'https://store.bblcdn.com/7166b20bc68f4c87b32c972b856eeb4b.pdf', + retrieved: '2026-11-08' +}; + +const mechanical: MechanicalProperty[] = [ + { + property: 'tensile_modulus', + direction: 'XY', + value: 3860, + plus_minus: 230, + unit: 'MPa', + standard: 'ISO 527', + ...SOURCE, + source_quote: "Young's Modulus (X-Y) ISO 527, GB/T 1040 3860 ± 230 MPa" + }, + { + property: 'tensile_modulus', + direction: 'Z', + value: 2350, + plus_minus: 140, + unit: 'MPa', + standard: 'ISO 527', + ...SOURCE, + source_quote: "Young's Modulus (Z) ISO 527, GB/T 1040 2350 ± 140 MPa" + } +]; + +describe('MechanicalDisplay', () => { + it('shows directions side by side, as published, linked to the source', () => { + render(MechanicalDisplay, { mechanical }); + const headers = screen.getAllByRole('columnheader').map((th) => th.textContent?.trim()); + expect(headers).toEqual(['Property', 'XY', 'Z']); + expect(screen.getAllByRole('row')).toHaveLength(2); + const xy = screen.getByRole('link', { name: '3860 ± 230 MPa' }); + expect(xy.getAttribute('href')).toBe(SOURCE.source_url); + expect(xy.getAttribute('title')).toContain('3860 ± 230 MPa'); + expect(screen.getByRole('link', { name: '2350 ± 140 MPa' })).toBeTruthy(); + expect(screen.getByText('ISO 527')).toBeTruthy(); + }); + + it('lists one data sheet once under its sources', () => { + render(MechanicalDisplay, { mechanical }); + expect(screen.getAllByRole('link', { name: SOURCE.source_url })).toHaveLength(1); + }); +}); diff --git a/webui/src/lib/components/entity/index.ts b/webui/src/lib/components/entity/index.ts index b350f56cdb..93c174e252 100644 --- a/webui/src/lib/components/entity/index.ts +++ b/webui/src/lib/components/entity/index.ts @@ -8,3 +8,4 @@ export { default as SubmittedBanner } from './SubmittedBanner.svelte'; export { default as InFlightHint } from './InFlightHint.svelte'; export { default as OrcaDownloadButton } from './OrcaDownloadButton.svelte'; export { default as SustainabilityDisplay } from './SustainabilityDisplay.svelte'; +export { default as MechanicalDisplay } from './MechanicalDisplay.svelte'; diff --git a/webui/src/lib/components/form-fields/MechanicalEditor.svelte b/webui/src/lib/components/form-fields/MechanicalEditor.svelte new file mode 100644 index 0000000000..e80684abf9 --- /dev/null +++ b/webui/src/lib/components/form-fields/MechanicalEditor.svelte @@ -0,0 +1,148 @@ + + +
+

+ Copy each value exactly as the data sheet writes it, in its unit, and paste the line it appears + in. Add one entry per print direction. Never convert units or average directions. +

+ + {#each value as entry, i (entry)} +
+
+

+ + {i + 1}. {entry.property ? propertyLabel(entry.property) : itemTitle} + {#if entry.direction && entry.direction !== 'unknown'}({entry.direction}){/if} +

+ +
+ + + {#if specimen} + {#if entry.specimen} +
+
+ + {specimen.title ?? 'Specimen'} + {#if specimen.description}{/if} + + +
+ +
+ {:else} + + {/if} + {/if} +
+ {/each} + + +
diff --git a/webui/src/lib/components/form-fields/SourcedEntryFields.svelte b/webui/src/lib/components/form-fields/SourcedEntryFields.svelte index 26398f2185..0cf9e51b1d 100644 --- a/webui/src/lib/components/form-fields/SourcedEntryFields.svelte +++ b/webui/src/lib/components/form-fields/SourcedEntryFields.svelte @@ -1,13 +1,14 @@ + + +
{JSON.stringify(value)}
diff --git a/webui/src/lib/components/form-fields/index.ts b/webui/src/lib/components/form-fields/index.ts index 79023183aa..118fdf6e43 100644 --- a/webui/src/lib/components/form-fields/index.ts +++ b/webui/src/lib/components/form-fields/index.ts @@ -20,3 +20,4 @@ export { default as PurchaseLinksSection } from './PurchaseLinksSection.svelte'; export { default as SizeCard } from './SizeCard.svelte'; export { default as SourcedEntryFields } from './SourcedEntryFields.svelte'; export { default as SustainabilityEditor } from './SustainabilityEditor.svelte'; +export { default as MechanicalEditor } from './MechanicalEditor.svelte'; diff --git a/webui/src/lib/components/forms/FilamentForm.svelte b/webui/src/lib/components/forms/FilamentForm.svelte index cf05428a55..dc7abea82e 100644 --- a/webui/src/lib/components/forms/FilamentForm.svelte +++ b/webui/src/lib/components/forms/FilamentForm.svelte @@ -1,7 +1,7 @@