64ba396c05
* claim: #4784 * Part properties: one table of keys, each with its scope, check and label (#4784) SolidCraft's part-property keys (project, part number, revision, author, organization, description, licence) were declared once but listed again by hand in each command, error, dialog and window. They are now one table, metadata::KEYS: each row has the key, where it may be set (Scope: the design, a component or both), how its value is checked and the message id of its label. A new key is one row. Metadata::set is told whose metadata it changes (Owner::Design or Owner::Component), and a key is taken where its scope covers that owner. is_well_known, both set commands, document.metadata, component.metadata, the refusals, the Design Properties dialog and the Bill of Materials window read the table; the hand-written lists are gone (WELL_KNOWN, the label match whose fallback named any unknown key "License", the window's three labels). metadata::BOM_KEYS names, in column order, the keys a bill of materials lists for each part: component.metadata returns them, component.metadata.set takes each as a parameter of its own and the window edits them; a test keeps each one a row of the table that a component takes. Free properties use the keys' text rule (metadata::check_text) instead of a copy of it. The params docs point to document.metadata's properties instead of repeating the keys. Every key accepted before still is, on the design and on a component. Two things change: - a refused key or a full metadata on a component says "part property" ("`bogus` is not a part property: …", "part properties are limited to …"), not "design property"; - changing the design's own properties no longer evaluates the model. The fields that change no geometry are named once, Document::with_part_properties_of: the design's metadata, and each component's metadata and free properties. It compares them first and copies the design only when one differs, as before. The shortcut is for a session's own edits only. A command that gives the session another design (doc.open, file.new, doc.recover: through Session::replace_design, which now counts them) is refreshed in full, however little the new design differs from the one before, so its joints are solved and its occurrence state pruned. Before, opening a design that differed from the open one only in render settings or component properties skipped that; with the design's properties in the list it would have for two revisions of one part too. Tests: the table (one row per key, the dialog's order); the bill of materials' keys (rows a component takes, in column order); each scope on each owner, and refusals that name the owner (the empty key, an unknown key, a 256-byte key shown cut); the text check at 4096 bytes, line breaks, tabs and control characters; hostile keys and values refused without a change; only-part-property changes in each scope, and changes beside them (units, a component's name, an added, removed or swapped component); document.metadata.set, remove, undo and redo evaluate nothing; opening a revision that differs only in its author solves its joint; the component wording, the shortcuts and the limits; the dialog's and the window's labels from the table. What the tests can't reach: every row is Scope::Both, so a known key refused for its owner (the wrong-owner branch of check) can't happen yet. It becomes live with the first single-scope row, bom_type, a component-only key. Left for later (not this topic): - Metadata::others() ignores scope, so a component-only key stored on the design would be neither a dialog field nor listed under other tools' data. It matters from the first single-scope key (bom_type). - assembly.bom's rows, its CSV columns and the window's grid headers still name the three keys by hand; the bill of materials module's rewrite will read BOM_KEYS. - The bill of materials types (P1, #5107, into drawings) still use WELL_KNOWN and the two-argument set: once this is on main, bom_type is rebuilt on it as one KEYS row with Scope::Component, set through Owner::Component. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
403 lines
22 KiB
Rust
403 lines
22 KiB
Rust
use serde_json::{Value, json};
|
|
|
|
use crate::Session;
|
|
|
|
fn run(s: &mut Session, id: &str, p: Value) -> Value {
|
|
match s.execute(id, &p) {
|
|
Ok(v) => v,
|
|
Err(e) => panic!("{id} {p}: {e}"),
|
|
}
|
|
}
|
|
|
|
/// A folder of its own for one test's files.
|
|
fn scratch(name: &str) -> std::path::PathBuf {
|
|
let dir = std::env::temp_dir().join(format!("solidcraft-bom-{}-{name}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
dir
|
|
}
|
|
|
|
/// A plate placed three times, and a hinge subassembly (no bodies of its own) placed twice
|
|
/// that holds one pin: the plate has 3 occurrences, the pin 2 (one in each hinge).
|
|
fn assembly() -> Session {
|
|
let mut s = Session::default();
|
|
run(&mut s, "component.create", json!({"name": "Plate"}));
|
|
run(&mut s, "solid.box", json!({"length": 40, "width": 20, "height": 3}));
|
|
run(&mut s, "component.activate", json!({"component": 0}));
|
|
run(&mut s, "component.create", json!({"name": "Hinge", "activate": false}));
|
|
run(&mut s, "component.create", json!({"name": "Pin", "parent": "Hinge"}));
|
|
run(&mut s, "solid.cylinder", json!({"diameter": 4, "height": 30}));
|
|
run(&mut s, "component.activate", json!({"component": 0}));
|
|
for x in [50, 100] {
|
|
run(&mut s, "occurrence.copy", json!({"component": "Plate", "translate": [x, 0, 0]}));
|
|
}
|
|
run(&mut s, "occurrence.copy", json!({"component": "Hinge", "translate": [0, 50, 0]}));
|
|
run(&mut s, "component.metadata.set", json!({"component": "Plate", "part_number": "PL-100", "description": "Plate, 3 mm", "revision": "B"}));
|
|
run(
|
|
&mut s,
|
|
"component.metadata.set",
|
|
json!({"component": "Pin", "part_number": "PN-040", "description": "Pin 4 x 30", "properties": {"finish": "zinc"}}),
|
|
);
|
|
s
|
|
}
|
|
|
|
fn summary(r: &Value) -> Vec<(String, String, u64)> {
|
|
r["rows"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|r| (r["part_number"].as_str().unwrap().to_string(), r["definition"].as_str().unwrap().to_string(), r["count"].as_u64().unwrap()))
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn flat_counts_parts_through_subassemblies_and_top_counts_children() {
|
|
let mut s = assembly();
|
|
let undo = s.undo.len();
|
|
let flat = run(&mut s, "assembly.bom", json!({}));
|
|
assert_eq!(summary(&flat), [("PL-100".into(), "Plate".into(), 3), ("PN-040".into(), "Pin".into(), 2)], "{flat}");
|
|
assert_eq!(flat["total_occurrences"], 5);
|
|
assert_eq!(flat["rows"][0]["item"], 1);
|
|
assert_eq!(flat["rows"][0]["description"], "Plate, 3 mm");
|
|
assert_eq!(flat["rows"][0]["revision"], "B");
|
|
assert_eq!(flat["rows"][1]["properties"], json!({"finish": "zinc"}));
|
|
|
|
let top = run(&mut s, "assembly.bom", json!({"levels": "top"}));
|
|
assert_eq!(summary(&top), [("PL-100".into(), "Plate".into(), 3), (String::new(), "Hinge".into(), 2)], "{top}");
|
|
assert_eq!(top["total_occurrences"], 5);
|
|
|
|
// One subassembly's own bill of materials.
|
|
let hinge = run(&mut s, "assembly.bom", json!({"component": "Hinge"}));
|
|
assert_eq!(summary(&hinge), [("PN-040".into(), "Pin".into(), 1)]);
|
|
assert_eq!(s.undo.len(), undo, "the bill of materials only reads");
|
|
}
|
|
|
|
#[test]
|
|
fn equal_part_numbers_share_a_row_unless_grouped_by_definition() {
|
|
let mut s = assembly();
|
|
// An independent copy keeps the part properties, so it is the same part.
|
|
run(&mut s, "component.paste_new", json!({"component": "Plate", "translate": [0, -60, 0]}));
|
|
let r = run(&mut s, "assembly.bom", json!({}));
|
|
let plate = &r["rows"][0];
|
|
assert_eq!((plate["part_number"].as_str(), plate["count"].as_u64()), (Some("PL-100"), Some(4)), "{r}");
|
|
assert_eq!(plate["components"].as_array().map(Vec::len), Some(2));
|
|
let by_def = run(&mut s, "assembly.bom", json!({"group_by": ["definition"]}));
|
|
let counts: Vec<u64> =
|
|
by_def["rows"].as_array().unwrap().iter().filter(|r| r["part_number"] == "PL-100").map(|r| r["count"].as_u64().unwrap()).collect();
|
|
assert_eq!(counts, [3, 1], "{by_def}");
|
|
// Without a part number every definition is its own row, after the numbered ones.
|
|
run(&mut s, "component.create", json!({"name": "Spacer"}));
|
|
run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5}));
|
|
run(&mut s, "component.activate", json!({"component": 0}));
|
|
let r = run(&mut s, "assembly.bom", json!({}));
|
|
let last = r["rows"].as_array().unwrap().last().cloned().unwrap();
|
|
assert_eq!((last["part_number"].as_str(), last["definition"].as_str()), (Some(""), Some("Spacer")));
|
|
}
|
|
|
|
#[test]
|
|
fn part_properties_survive_save_and_open_and_undo_in_one_step() {
|
|
let dir = scratch("roundtrip");
|
|
let mut s = assembly();
|
|
let undo = s.undo.len();
|
|
let r = run(&mut s, "component.metadata.set", json!({"component": "Pin", "revision": "C", "properties": {"finish": null, "material": "1.4301"}}));
|
|
assert_eq!(r["changed"], json!(["revision", "properties.finish", "properties.material"]));
|
|
assert_eq!(s.undo.len(), undo + 1, "one undo step");
|
|
// Setting the same values again changes nothing.
|
|
let r = run(&mut s, "component.metadata.set", json!({"component": "Pin", "revision": "C"}));
|
|
assert_eq!(r["changed"], json!([]));
|
|
assert_eq!(s.undo.len(), undo + 1);
|
|
for (file, format) in [("bom.json", "json"), ("bom.solidcraft", "container")] {
|
|
let path = dir.join(file);
|
|
run(&mut s, "file.save_as", json!({"path": path, "format": format}));
|
|
let mut back = Session::default();
|
|
run(&mut back, "doc.open", json!({"path": path}));
|
|
let pin = run(&mut back, "component.metadata", json!({"component": "Pin"}));
|
|
assert_eq!((pin["part_number"].as_str(), pin["revision"].as_str()), (Some("PN-040"), Some("C")), "{format}: {pin}");
|
|
assert_eq!(pin["properties"], json!({"material": "1.4301"}), "{format}");
|
|
assert_eq!(summary(&run(&mut back, "assembly.bom", json!({}))), summary(&run(&mut s, "assembly.bom", json!({}))), "{format}");
|
|
}
|
|
run(&mut s, "edit.undo", json!({}));
|
|
let pin = run(&mut s, "component.metadata", json!({"component": "Pin"}));
|
|
assert_eq!((pin["revision"].as_str(), &pin["properties"]), (None, &json!({"finish": "zinc"})));
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn inserted_design_carries_its_part_properties_into_the_bom() {
|
|
let dir = scratch("insert-design");
|
|
let source = dir.join("plate.solidcraft");
|
|
let metadata = json!({
|
|
"part_number": "PL-6624", "description": "Mounting plate", "revision": "B",
|
|
"author": "Example author", "license": "CC0-1.0", "com.example.catalog": {"stock": 12}
|
|
});
|
|
let mut part = Session::default();
|
|
run(&mut part, "solid.box", json!({"length": 40, "width": 20, "height": 3}));
|
|
run(&mut part, "document.metadata.set", json!({"values": metadata}));
|
|
run(&mut part, "file.save", json!({"path": source}));
|
|
|
|
for reuse in [false, true] {
|
|
let mut s = Session::default();
|
|
run(&mut s, "document.metadata.set", json!({"values": {"part_number": "ASM-1", "description": "Assembly"}}));
|
|
let own_metadata = s.doc.metadata.clone();
|
|
let undo = s.undo.len();
|
|
let inserted = run(&mut s, "component.insert_design", json!({"path": source, "name": "Plate", "reuse": reuse}));
|
|
let component = inserted["component"].as_u64().unwrap();
|
|
assert_eq!(run(&mut s, "component.metadata", json!({"component": component}))["metadata"], metadata);
|
|
assert_eq!(s.doc.metadata, own_metadata, "the assembly keeps its own properties");
|
|
let bom = run(&mut s, "assembly.bom", json!({}));
|
|
assert_eq!(bom["rows"][0]["part_number"], "PL-6624");
|
|
assert_eq!(bom["rows"][0]["description"], "Mounting plate");
|
|
assert_eq!(bom["rows"][0]["revision"], "B");
|
|
assert_eq!(bom["rows"][0]["count"], 1);
|
|
assert_eq!(s.undo.len(), undo + 1, "insertion including properties is one undo step");
|
|
run(&mut s, "edit.undo", json!({}));
|
|
assert!(s.doc.components.is_empty());
|
|
assert!(s.doc.links.is_empty());
|
|
run(&mut s, "edit.redo", json!({}));
|
|
assert_eq!(run(&mut s, "component.metadata", json!({"component": component}))["metadata"], metadata);
|
|
|
|
for (file, format) in [("assembly.json", "json"), ("assembly.solidcraft", "container")] {
|
|
let path = dir.join(file);
|
|
run(&mut s, "file.save_as", json!({"path": path, "format": format}));
|
|
let mut back = Session::default();
|
|
run(&mut back, "doc.open", json!({"path": path}));
|
|
assert_eq!(run(&mut back, "component.metadata", json!({"component": component}))["metadata"], metadata, "{format}");
|
|
assert_eq!(run(&mut back, "assembly.bom", json!({}))["rows"], bom["rows"], "{format}");
|
|
}
|
|
|
|
// Reusing the link shares its definition, including locally edited part properties.
|
|
run(&mut s, "component.metadata.set", json!({"component": component, "description": "Assembly-specific description"}));
|
|
run(&mut s, "component.insert_design", json!({"path": source, "reuse": true, "translate": [50, 0, 0]}));
|
|
let bom = run(&mut s, "assembly.bom", json!({}));
|
|
assert_eq!(bom["rows"].as_array().unwrap().len(), 1);
|
|
assert_eq!(bom["rows"][0]["count"], 2);
|
|
assert_eq!(bom["rows"][0]["part_number"], "PL-6624");
|
|
assert_eq!(bom["rows"][0]["description"], "Assembly-specific description");
|
|
}
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn csv_quotes_every_field_and_json_writes_the_rows() {
|
|
let dir = scratch("export");
|
|
let mut s = assembly();
|
|
run(&mut s, "component.metadata.set", json!({"component": "Pin", "description": "Pin \"short\", 4 mm"}));
|
|
let csv = dir.join("bom.csv");
|
|
let r = run(&mut s, "assembly.bom", json!({"path": csv}));
|
|
assert_eq!(r["format"], "csv");
|
|
let text = std::fs::read_to_string(&csv).unwrap();
|
|
assert_eq!(
|
|
text,
|
|
"\"item\",\"part_number\",\"description\",\"revision\",\"count\",\"definition\"\n\
|
|
\"1\",\"PL-100\",\"Plate, 3 mm\",\"B\",\"3\",\"Plate\"\n\
|
|
\"2\",\"PN-040\",\"Pin \"\"short\"\", 4 mm\",\"\",\"2\",\"Pin\"\n"
|
|
);
|
|
let json_path = dir.join("bom.json");
|
|
run(&mut s, "assembly.bom", json!({"path": json_path, "levels": "top"}));
|
|
let back: Value = serde_json::from_str(&std::fs::read_to_string(&json_path).unwrap()).unwrap();
|
|
assert_eq!(back["total_occurrences"], 5);
|
|
assert_eq!(back["rows"][1]["definition"], "Hinge");
|
|
assert!(s.execute("assembly.bom", &json!({"path": dir.join("bom.txt")})).is_err(), "unknown extension");
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn standard_parts_carry_a_part_number_that_follows_a_resize() {
|
|
let mut s = Session::default();
|
|
let a = run(&mut s, "parts.insert", json!({"family": "socket_head_cap_screw", "size": "M6", "length": 20}));
|
|
run(&mut s, "parts.insert", json!({"family": "socket_head_cap_screw", "size": "M6", "length": 20, "point": [30, 0, 0]}));
|
|
run(&mut s, "parts.insert", json!({"family": "bearing", "size": "608", "point": [60, 0, 0]}));
|
|
let r = run(&mut s, "assembly.bom", json!({}));
|
|
let rows: Vec<(String, String, u64)> = r["rows"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|r| (r["part_number"].as_str().unwrap().into(), r["description"].as_str().unwrap().into(), r["count"].as_u64().unwrap()))
|
|
.collect();
|
|
assert_eq!(
|
|
rows,
|
|
[("608".into(), "Deep groove ball bearing (envelope) 608".into(), 1), ("ISO 4762 M6x20".into(), "Socket head cap screw M6x20".into(), 2)]
|
|
);
|
|
let screws = r["rows"].as_array().unwrap().iter().find(|r| r["count"] == 2).cloned().unwrap();
|
|
assert_eq!(screws["definition"], "SHCS M6x20", "equal names are named once");
|
|
assert_eq!(screws["components"].as_array().map(Vec::len), Some(2));
|
|
// A resize renumbers the part; a description someone wrote stays theirs.
|
|
let comp = a["component"].as_u64().unwrap();
|
|
run(&mut s, "component.metadata.set", json!({"component": comp, "description": "Cap screw, A2"}));
|
|
run(&mut s, "parts.resize", json!({"component": comp, "size": "M8", "length": 25}));
|
|
let m = run(&mut s, "component.metadata", json!({"component": comp}));
|
|
assert_eq!((m["part_number"].as_str(), m["description"].as_str()), (Some("ISO 4762 M8x25"), Some("Cap screw, A2")));
|
|
}
|
|
|
|
#[test]
|
|
fn hostile_part_properties_are_refused_without_a_change() {
|
|
let mut s = assembly();
|
|
let undo = s.undo.len();
|
|
for p in [
|
|
json!({"component": "Pin"}),
|
|
json!({"component": 0, "part_number": "X"}),
|
|
json!({"component": "Nope", "part_number": "X"}),
|
|
json!({"component": "Pin", "part_number": 7}),
|
|
json!({"component": "Pin", "part_number": "ok", "description": "bell\u{7}"}),
|
|
json!({"component": "Pin", "properties": {"": "x"}}),
|
|
json!({"component": "Pin", "properties": {"finish": ["x"]}}),
|
|
json!({"component": "Pin", "properties": {"finish": "x".repeat(5000)}}),
|
|
json!({"component": "Pin", "values": {"bogus": "x"}}),
|
|
json!({"component": "Pin", "values": "x"}),
|
|
] {
|
|
assert!(s.execute("component.metadata.set", &p).is_err(), "{p}");
|
|
}
|
|
assert_eq!(s.undo.len(), undo);
|
|
let pin = run(&mut s, "component.metadata", json!({"component": "Pin"}));
|
|
assert_eq!(pin["part_number"], "PN-040", "all or nothing");
|
|
for p in [json!({"levels": "deep"}), json!({"group_by": ["colour"]}), json!({"group_by": 3}), json!({"component": "Nope"})] {
|
|
assert!(s.execute("assembly.bom", &p).is_err(), "{p}");
|
|
}
|
|
// The root reads as the design's own properties.
|
|
run(&mut s, "document.metadata.set", json!({"key": "part_number", "value": "ASM-1"}));
|
|
assert_eq!(run(&mut s, "component.metadata", json!({"component": 0}))["part_number"], "ASM-1");
|
|
}
|
|
|
|
#[test]
|
|
fn a_components_refusals_speak_of_part_properties() {
|
|
let mut s = assembly();
|
|
let undo = s.undo.len();
|
|
let refusal = |s: &mut Session, p: Value| s.execute("component.metadata.set", &p).unwrap_err().to_string();
|
|
let bogus = refusal(&mut s, json!({"component": "Pin", "values": {"bogus": "x"}}));
|
|
assert!(
|
|
bogus.ends_with(
|
|
"`bogus` is not a part property: SolidCraft's are project, part_number, revision, author, organization, description, \
|
|
license; another tool's data goes under a reverse-DNS key such as `com.example.tool`"
|
|
),
|
|
"{bogus}"
|
|
);
|
|
assert!(!bogus.contains("design property"), "{bogus}");
|
|
assert!(refusal(&mut s, json!({"component": "Pin", "values": {"": "x"}})).contains("`` is not a part property"));
|
|
// Nothing to change: the same list as before the table.
|
|
assert!(refusal(&mut s, json!({"component": "Pin"})).ends_with(
|
|
"give part_number, description, revision, values or properties (project, part_number, revision, author, organization, \
|
|
description, license)"
|
|
));
|
|
// Only the bill of materials' keys are parameters of their own; the others go in `values`.
|
|
assert!(refusal(&mut s, json!({"component": "Pin", "author": "Jane"})).contains("give part_number, description, revision"));
|
|
let long = refusal(&mut s, json!({"component": "Pin", "properties": {"finish": "x".repeat(5000)}}));
|
|
assert!(long.ends_with("property `finish` is longer than 4096 bytes"), "{long}");
|
|
let bell = refusal(&mut s, json!({"component": "Pin", "properties": {"finish": "a\u{7}"}}));
|
|
assert!(bell.ends_with("property `finish` has a control character"), "{bell}");
|
|
assert_eq!(s.undo.len(), undo, "nothing changed");
|
|
// The design keeps its own words.
|
|
let design = s.execute("document.metadata.set", &json!({"key": "bogus", "value": "x"})).unwrap_err().to_string();
|
|
assert!(design.contains("`bogus` is not a design property"), "{design}");
|
|
}
|
|
|
|
#[test]
|
|
fn every_key_a_component_took_before_is_still_taken() {
|
|
let mut s = assembly();
|
|
let keys = ["project", "part_number", "revision", "author", "organization", "description", "license"];
|
|
let mut values: serde_json::Map<String, Value> = keys.iter().map(|k| (k.to_string(), json!(format!("{k} text")))).collect();
|
|
values.insert("com.acme.shop".into(), json!({"id": 7}));
|
|
let r = run(&mut s, "component.metadata.set", json!({"component": "Plate", "values": values}));
|
|
assert_eq!(r["changed"].as_array().map(Vec::len), Some(keys.len() + 1), "{r}");
|
|
let plate = run(&mut s, "component.metadata", json!({"component": "Plate"}));
|
|
for k in keys {
|
|
assert_eq!(plate["metadata"][k], format!("{k} text"), "{k}");
|
|
}
|
|
for k in ["part_number", "description", "revision"] {
|
|
assert_eq!(plate[k], format!("{k} text"), "the bill of materials' field {k}: {plate}");
|
|
}
|
|
assert!(plate.get("author").is_none(), "the others stay in metadata: {plate}");
|
|
// The shortcuts and the root read the same fields.
|
|
run(&mut s, "component.metadata.set", json!({"component": "Pin", "part_number": "", "description": "Pin", "revision": "D"}));
|
|
let pin = run(&mut s, "component.metadata", json!({"component": "Pin"}));
|
|
assert_eq!((&pin["part_number"], &pin["description"], &pin["revision"]), (&Value::Null, &json!("Pin"), &json!("D")), "{pin}");
|
|
let root = run(&mut s, "component.metadata", json!({"component": 0}));
|
|
assert_eq!((&root["component"], &root["part_number"], &root["properties"]), (&json!(0), &Value::Null, &json!({})), "{root}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_component_refused_past_its_limits_says_part_properties() {
|
|
let mut s = assembly();
|
|
// The pin holds a part number and a description: room for this many more keys.
|
|
let room = solidcraft_doc::metadata::MAX_KEYS - 2;
|
|
let values: serde_json::Map<String, Value> = (0..room).map(|i| (format!("org.k{i}"), json!({}))).collect();
|
|
run(&mut s, "component.metadata.set", json!({"component": "Pin", "values": values}));
|
|
let over = s.execute("component.metadata.set", &json!({"component": "Pin", "values": {"org.one_more": {}}})).unwrap_err().to_string();
|
|
assert!(over.ends_with("part properties are limited to 256 keys and 256 KiB"), "{over}");
|
|
}
|
|
|
|
/// Two hex bolts, each with a washer and a nut placed inside it, and a third bolt alone.
|
|
fn bolts() -> Session {
|
|
let mut s = Session::default();
|
|
for x in [0, 30] {
|
|
let bolt = run(&mut s, "parts.insert", json!({"family": "hex_bolt", "size": "M6", "length": 30, "point": [x, 0, 0], "direction": [0, 0, 1]}));
|
|
run(&mut s, "component.activate", json!({"component": bolt["component"]}));
|
|
run(&mut s, "parts.insert", json!({"family": "washer", "size": "M6", "point": [x, 0, -2], "direction": [0, 0, -1]}));
|
|
run(&mut s, "parts.insert", json!({"family": "hex_nut", "size": "M6", "point": [x, 0, -4], "direction": [0, 0, -1]}));
|
|
run(&mut s, "component.activate", json!({"component": 0}));
|
|
}
|
|
run(&mut s, "parts.insert", json!({"family": "hex_bolt", "size": "M6", "length": 30, "point": [60, 0, 0], "direction": [0, 0, 1]}));
|
|
s
|
|
}
|
|
|
|
#[test]
|
|
fn group_children_keeps_a_bolts_washer_and_nut_in_its_row() {
|
|
let mut s = bolts();
|
|
let plain = run(&mut s, "assembly.bom", json!({}));
|
|
let numbers: Vec<(String, u64)> = summary(&plain).into_iter().map(|(pn, _, n)| (pn, n)).collect();
|
|
assert_eq!(numbers, [("ISO 4017 M6x30".into(), 3), ("ISO 4032 M6".into(), 2), ("ISO 7089 M6".into(), 2)], "{plain}");
|
|
|
|
let r = run(&mut s, "assembly.bom", json!({"group_children": true}));
|
|
let rows = r["rows"].as_array().unwrap();
|
|
assert_eq!(rows.len(), 2, "the bolts with a nut and the bolt alone: {r}");
|
|
let kit = &rows[0];
|
|
assert_eq!((kit["part_number"].as_str(), kit["count"].as_u64()), (Some("ISO 4017 M6x30"), Some(2)), "{r}");
|
|
let children: Vec<(&str, &str, u64, u64)> = kit["children"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|c| (c["item"].as_str().unwrap(), c["part_number"].as_str().unwrap(), c["each"].as_u64().unwrap(), c["count"].as_u64().unwrap()))
|
|
.collect();
|
|
assert_eq!(children, [("1.1", "ISO 4032 M6", 1, 2), ("1.2", "ISO 7089 M6", 1, 2)]);
|
|
assert_eq!((rows[1]["count"].as_u64(), rows[1].get("children")), (Some(1), None));
|
|
assert_eq!(r["total_occurrences"], plain["total_occurrences"], "the same parts, folded");
|
|
|
|
let dir = scratch("kit");
|
|
let path = dir.join("bom.csv");
|
|
run(&mut s, "assembly.bom", json!({"group_children": true, "path": path.to_str().unwrap()}));
|
|
let text = std::fs::read_to_string(&path).unwrap();
|
|
let items: Vec<&str> = text.lines().skip(1).map(|l| l.split(',').next().unwrap()).collect();
|
|
assert_eq!(items, ["\"1\"", "\"1.1\"", "\"1.2\"", "\"2\""], "{text}");
|
|
assert!(s.execute("assembly.bom", &json!({"group_children": "yes"})).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn changing_part_properties_does_not_evaluate_the_model_again() {
|
|
let mut s = bolts();
|
|
s.evaluations = 0;
|
|
run(&mut s, "component.metadata.set", json!({"component": 1, "description": "Bolt", "properties": {"finish": "zinc"}}));
|
|
run(&mut s, "edit.undo", json!({}));
|
|
run(&mut s, "edit.redo", json!({}));
|
|
assert_eq!(s.evaluations, 0, "part properties change no geometry");
|
|
assert_eq!(run(&mut s, "component.metadata", json!({"component": 1}))["description"], "Bolt");
|
|
// A geometric edit after it finds every other feature's earlier result.
|
|
let first = run(&mut s, "assembly.bom", json!({}))["rows"][1]["components"][0].clone();
|
|
run(&mut s, "parts.resize", json!({"component": first, "size": "M8"}));
|
|
run(&mut s, "parts.resize", json!({"component": first, "size": "M6"}));
|
|
// Anything else still evaluates.
|
|
run(&mut s, "occurrence.copy", json!({"component": 1, "translate": [0, 40, 0]}));
|
|
assert!(s.evaluations > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn body_owners_match_each_bodys_component() {
|
|
for s in [assembly(), bolts()] {
|
|
let st = s.model.state();
|
|
let each: std::collections::HashSet<u64> = st.bodies.iter().map(|b| s.doc.model_body_component(b)).collect();
|
|
assert_eq!(s.doc.body_owners(&st), each);
|
|
assert!(each.len() > 1);
|
|
// The same answers body by body, in the state's order.
|
|
let in_order: Vec<u64> = st.bodies.iter().map(|b| s.doc.model_body_component(b)).collect();
|
|
assert_eq!(s.doc.model_body_components(&st), in_order);
|
|
}
|
|
}
|