diff --git a/assets/locales/pending/parts-several-holes.json b/assets/locales/pending/parts-several-holes.json new file mode 100644 index 000000000..462061ac8 --- /dev/null +++ b/assets/locales/pending/parts-several-holes.json @@ -0,0 +1,46 @@ +{ + "en": { + "parts.holes": "Holes (optional)", + "parts.placement_hint": "pick one or more holes to seat a part in each, or OK places it at the origin" + }, + "ar": { + "parts.holes": "الثقوب (اختياري)", + "parts.placement_hint": "اختر ثقبًا أو أكثر لتثبيت قطعة في كل منها، أو اضغط موافق لوضعها عند نقطة الأصل" + }, + "de": { + "parts.holes": "Bohrungen (optional)", + "parts.placement_hint": "Wählen Sie eine oder mehrere Bohrungen, um in jede ein Teil einzusetzen, oder OK platziert es im Ursprung" + }, + "es": { + "parts.holes": "Agujeros (opcional)", + "parts.placement_hint": "elija uno o más agujeros para asentar una pieza en cada uno, o Aceptar la coloca en el origen" + }, + "fr": { + "parts.holes": "Trous (facultatif)", + "parts.placement_hint": "choisissez un ou plusieurs trous pour y loger une pièce chacun, ou OK la place à l'origine" + }, + "he": { + "parts.holes": "חורים (אופציונלי)", + "parts.placement_hint": "בחרו חור אחד או יותר כדי להושיב חלק בכל אחד, או לחצו אישור כדי למקם אותו בראשית" + }, + "it": { + "parts.holes": "Fori (facoltativo)", + "parts.placement_hint": "scegli uno o più fori per alloggiare una parte in ciascuno, oppure OK la colloca nell'origine" + }, + "ja": { + "parts.holes": "穴 (省略可)", + "parts.placement_hint": "1 つ以上の穴を選ぶとそれぞれに部品を取り付けます。選ばずに OK を押すと原点に配置します" + }, + "nl": { + "parts.holes": "Gaten (optioneel)", + "parts.placement_hint": "kies een of meer gaten om in elk een onderdeel te plaatsen, of OK plaatst het in de oorsprong" + }, + "vi": { + "parts.holes": "Lỗ (tùy chọn)", + "parts.placement_hint": "chọn một hoặc nhiều lỗ để lắp một chi tiết vào mỗi lỗ, hoặc nhấn OK để đặt tại gốc tọa độ" + }, + "zh": { + "parts.holes": "孔(可选)", + "parts.placement_hint": "选择一个或多个孔,在每个孔中各装一个零件;或按确定将其放在原点" + } +} diff --git a/crates/engine/src/cmd/parts.rs b/crates/engine/src/cmd/parts.rs index 95aae7da9..0a651c013 100644 --- a/crates/engine/src/cmd/parts.rs +++ b/crates/engine/src/cmd/parts.rs @@ -2,7 +2,8 @@ //! the dimensions in the public standards tables (no vendor geometry). Each part is inserted as //! its own component, built by ordinary features (so it stays parametric and editable), with a //! cosmetic thread where it has one; at a hole it gets a rigid joint that puts its axis on the -//! hole's and seats it on the face. +//! hole's and seats it on the face. Inserted at several holes it is still one component, placed +//! once per hole, each placement with its own joint. //! //! Part frame: the seating plane is z = 0 (the underside of a head, the bottom of a nut or //! washer, the top of a set screw or pin); the shank runs down −z. @@ -18,13 +19,15 @@ pub static COMMANDS: &[CommandSpec] = &[ CommandSpec::new("parts.library", "Standard Parts", library).icon("library") .noundo() .params("family? → families (name, standard, sizes, lengths) and, with family, each size's dimensions (mm)"), - CommandSpec::new("parts.fastener", "Insert Fastener", insert).at("SOLID", "INSERT").icon("fastener").params( + CommandSpec::new("parts.fastener", "Insert Fastener", insert_atomic).at("SOLID", "INSERT").icon("fastener").params( "the Insert Part dialog's command (same as parts.insert): family, size, length?, at?|point?+direction?, name?", ), - CommandSpec::new("parts.insert", "Insert Part", insert).icon("fastener").params( + CommandSpec::new("parts.insert", "Insert Part", insert_atomic).icon("fastener").params( "family (socket_head_cap_screw | button_head_cap_screw | hex_bolt | hex_nut | washer | set_screw | dowel_pin | bearing); size (\"M6\", or \"608\" for bearings); \ length? (mm; screws, bolts, set screws, pins; default a standard length near 2.5 d); \ - at?: [x,y,z] on a hole's rim or wall (the part seats on the hole's face with a rigid joint) | point?: [x,y,z] with direction?: [x,y,z] (no joint); name?", + at?: [x,y,z] on a hole's rim or wall (the part seats on the hole's face with a rigid joint), or a list of them (one component, \ + placed and seated once per hole) | point?: [x,y,z] with direction?: [x,y,z] (no joint); name? → component, occurrence, joint?, \ + and with at: occurrences, joints (one per hole, in pick order)", ), CommandSpec::new("parts.resize", "Change Part Size", resize_atomic).icon("scale") .params("component: id|name of an inserted part; size?; length? — rebuilds it (joints keep it seated)"), @@ -514,18 +517,40 @@ fn length_param(p: &Value, cmd: &str) -> Result> { } } +/// Building the part and seating it runs many feature and joint commands: as in +/// [`resize_atomic`], they run on a scratch session and the insert is one undo step. +fn insert_atomic(s: &mut Session, p: &Value) -> Result { + let mut computed = s.scratch(); + let result = insert(&mut computed, p)?; + computed.undo.clear(); + computed.active_component = s.active_component; + computed.active_occurrence_path = s.active_occurrence_path.clone(); + computed.active_sketch = s.active_sketch.filter(|id| computed.doc.feature(*id).is_some()); + s.accept_computed(computed); + Ok(result) +} + fn insert(s: &mut Session, p: &Value) -> Result { let cmd = "parts.insert (Insert Fastener)"; let family = str_(p, "family").ok_or_else(|| bad(cmd, "`family` is required (parts.library)"))?.to_string(); let size = str_(p, "size").ok_or_else(|| bad(cmd, "`size` is required"))?.to_string(); let sh = shape(&family, &size, length_param(p, cmd)?).map_err(|e| bad(cmd, e))?; // A pick on a hole's rim also touches its end face: step onto the wall just inside the rim. - let at = p.get("at").and_then(vec3).map(|a| hole_wall(s, a).unwrap_or(a)); - // The occurrence the hole belongs to, found before the part exists. - let host = match at { - Some(a) => Some(super::joints::occurrence_at(s, a).ok_or_else(|| bad(cmd, "no body at `at` (pick a hole's rim or wall)"))?), - None => None, - }; + // Each hole's occurrence is found before the part exists, so a pick off every body fails the + // insert before anything is made. + let mut holes = Vec::new(); + let mut seats: Vec<(u64, Vec3)> = Vec::new(); + for a in hole_picks(p, cmd)? { + let (a, seat) = hole_wall(s, a).unwrap_or((a, a)); + let host = super::joints::occurrence_at(s, a).ok_or_else(|| bad(cmd, "no body at `at` (pick a hole's rim or wall)"))?; + // A rim is drawn as more than one edge, and one click on it picks them all: the picks + // of one rim seat one part. + if seats.iter().any(|(h, q)| *h == host && (*q - seat).len() < 1e-3) { + continue; + } + seats.push((host, seat)); + holes.push((a, host)); + } let name = str_(p, "name").map(str::to_string).unwrap_or_else(|| part_name(&family, &size, &sh)); let active = s.active_component; // Hardware is fitted to other parts: it starts free even as the design's first part (#2804). @@ -536,15 +561,29 @@ fn insert(s: &mut Session, p: &Value) -> Result { set_identity(s, comp, identity(&family, &size, &sh), None); run(s, "component.activate", json!({"component": active}))?; let mut out = json!({"component": comp, "occurrence": occ, "name": name}); - if let (Some(a), Some(h)) = (at, host) { - // Seat it: its axis on the hole's, its seating plane on the hole's face, the shank going - // into the hole (the joint mates the part's −z with the face's outward z). - let j = run( - s, - "joint.create", - json!({"type": "rigid", "a": {"occurrence": h, "circle": a}, "b": {"occurrence": occ, "point": [0, 0, 0], "z": [0, 0, -1]}, "name": format!("{name} seat")}), - )?; - out["joint"] = j["joint"].clone(); + if !holes.is_empty() { + // The first hole has the placement the component came with; each further hole places the + // same part again. All are made before any is seated, while each is still at the part's + // own origin, where the joint below takes the part's seating point to be. + let mut occurrences = vec![json!(occ)]; + for _ in 1..holes.len() { + occurrences.push(run(s, "occurrence.copy", json!({"component": comp}))?["occurrence"].clone()); + } + let mut joints = Vec::new(); + for (i, ((a, h), o)) in holes.into_iter().zip(&occurrences).enumerate() { + // Seat it: its axis on the hole's, its seating plane on the hole's face, the shank + // going into the hole (the joint mates the part's −z with the face's outward z). + let seat = if i == 0 { format!("{name} seat") } else { format!("{name} seat {}", i + 1) }; + let j = run( + s, + "joint.create", + json!({"type": "rigid", "a": {"occurrence": h, "circle": a}, "b": {"occurrence": o, "point": [0, 0, 0], "z": [0, 0, -1]}, "name": seat}), + )?; + joints.push(j["joint"].clone()); + } + out["joint"] = joints.first().cloned().unwrap_or(Value::Null); + out["occurrences"] = Value::Array(occurrences); + out["joints"] = Value::Array(joints); } else if let Some(pt) = p.get("point").and_then(vec3) { // Placed at a point, its +z along `direction`. let dir = p.get("direction").and_then(vec3).and_then(|d| d.normalized()).unwrap_or(Vec3::Z); @@ -563,8 +602,28 @@ fn insert(s: &mut Session, p: &Value) -> Result { Ok(out) } -/// A point on the cylindrical wall of the hole at `a`, a little in from the picked rim. -fn hole_wall(s: &Session, a: Vec3) -> Option { +/// The most holes one insert seats a part in. +const MAX_HOLES: usize = 1000; + +/// The hole picks in `at`: one point, or a list of them. +fn hole_picks(p: &Value, cmd: &str) -> Result> { + let finite = |a: Vec3| a.x.is_finite() && a.y.is_finite() && a.z.is_finite(); + let one = |v: &Value| vec3(v).filter(|a| finite(*a)).ok_or_else(|| bad(cmd, "`at` must be [x,y,z] or a list of them")); + match p.get("at") { + None | Some(Value::Null) => Ok(Vec::new()), + Some(Value::Array(list)) if list.first().is_some_and(Value::is_array) || list.is_empty() => { + if list.len() > MAX_HOLES { + return Err(bad(cmd, format!("`at` takes at most {MAX_HOLES} holes"))); + } + list.iter().map(one).collect() + } + Some(v) => Ok(vec![one(v)?]), + } +} + +/// A point on the cylindrical wall of the hole at `a`, a little in from the picked rim, and the +/// point on the hole's axis level with it (the same for every pick on one rim). +fn hole_wall(s: &Session, a: Vec3) -> Option<(Vec3, Vec3)> { let st = s.model.state(); let dirs = [Vec3::Z * -1.0, Vec3::Z, Vec3::X, Vec3::X * -1.0, Vec3::Y, Vec3::Y * -1.0]; for step in [0.2, 0.5, 1.0, 2.0] { @@ -576,7 +635,7 @@ fn hole_wall(s: &Session, a: Vec3) -> Option { let along = v.dot(c.axis); let on_wall = ((v - c.axis * along).len() - c.radius).abs() < 1e-3 && along > c.start && along < c.end; if on_wall { - return Some(q); + return Some((q, c.axis_point + c.axis * along)); } } } @@ -800,6 +859,54 @@ mod tests { assert!(s.model.state().threads.iter().any(|t| t.designation == "M8" && (t.pitch - 1.25).abs() < 1e-9)); } + /// Several holes in one insert: one component placed and seated once per hole, in one undo + /// step (#9341); two picks of one rim are one hole; a pick off every body fails the whole + /// insert. + #[test] + fn one_insert_seats_a_screw_in_each_hole() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": 10, "body_name": "Plate"})); + let centres = [(8.0, 8.0), (32.0, 8.0), (32.0, 22.0), (8.0, 22.0)]; + for (x, y) in centres { + run(&mut s, "solid.hole", json!({"position": [x, y, 10], "diameter": 3.4})); + } + let (comps, occs, undo) = (s.doc.components.len(), s.doc.occurrences.len(), s.undo.len()); + // A hole off the plate fails the insert, and nothing is made. + let off = json!({"family": "socket_head_cap_screw", "size": "M3", "length": 8, "at": [[9.7, 8, 10], [100, 100, 100]]}); + assert!(s.execute("parts.insert", &off).is_err()); + assert_eq!((s.doc.components.len(), s.doc.occurrences.len()), (comps, occs)); + // Each rim picked at the top face. + // The first hole is picked twice, on both halves of its rim, as one click on a rim picks + // every edge of it: it still gets one screw. + let mut at: Vec<_> = centres.iter().map(|(x, y)| json!([x + 1.7, y, 10])).collect(); + at.insert(1, json!([centres[0].0 - 1.7, centres[0].1, 10])); + let r = run(&mut s, "parts.fastener", json!({"family": "socket_head_cap_screw", "size": "M3", "length": 8, "at": at})); + assert_eq!(s.doc.components.len(), comps + 1, "one part: {r}"); + let comp = r["component"].as_u64().unwrap_or(0); + let placed: Vec = r["occurrences"].as_array().into_iter().flatten().filter_map(Value::as_u64).collect(); + assert_eq!(placed.len(), 4, "{r}"); + assert!(placed.iter().all(|o| s.doc.occurrences.iter().any(|x| x.id == *o && x.component == comp)), "{r}"); + assert_eq!(r["joints"].as_array().map(|j| j.iter().filter(|j| j.is_number()).count()), Some(4), "{r}"); + assert_eq!(r["joint"], r["joints"][0]); + // Each placement sits in its own hole: the head flush on the top face, the shank down + // the hole's axis. + let st = s.world_state(); + let mut seated: Vec<(f64, f64)> = Vec::new(); + for b in st.bodies.iter().filter(|b| s.doc.model_body_component(b) == comp) { + let (lo, hi) = (b.mesh().bounds().min, b.mesh().bounds().max); + assert!((hi.z - 13.0).abs() < 1e-6 && (lo.z - 2.0).abs() < 1e-6, "{lo:?} {hi:?}"); + seated.push(((lo.x + hi.x) / 2.0, (lo.y + hi.y) / 2.0)); + } + assert_eq!(seated.len(), 4, "{seated:?}"); + for (x, y) in centres { + assert!(seated.iter().any(|c| (c.0 - x).abs() < 1e-6 && (c.1 - y).abs() < 1e-6), "no screw at ({x}, {y}): {seated:?}"); + } + // One undo takes them all away. + assert_eq!(s.undo.len(), undo + 1); + run(&mut s, "edit.undo", json!({})); + assert_eq!((s.doc.components.len(), s.doc.occurrences.len()), (comps, occs)); + } + #[test] fn every_sketch_of_a_standard_part_is_fully_defined() { for (family, size, resized) in [ diff --git a/crates/engine/src/frames.rs b/crates/engine/src/frames.rs index 48b5b269b..22194765e 100644 --- a/crates/engine/src/frames.rs +++ b/crates/engine/src/frames.rs @@ -90,7 +90,7 @@ fn table(id: &str) -> &'static [(&'static str, K)] { "sketch.project_to_surface" | "sketch.isoparametric_curve" => &[("face", Here)], "sketch.intersection_curve" => &[("a", Here), ("b", Here)], "inspect.curvature_comb" | "inspect.minimum_radius" | "inspect.isocurve" => &[("edges", Picks), ("faces", Picks)], - "parts.insert" | "parts.fastener" => &[("at", Pick), ("point", Make), ("direction", Dir)], + "parts.insert" | "parts.fastener" => &[("at", Picks), ("point", Make), ("direction", Dir)], "appearance.assign" => &[("faces", Picks), ("bodies", Body), ("body", Body)], "solid.move" => &[("bodies", Body), ("translate", Delta), ("axis", Dir), ("origin", Moved)], "solid.combine" => &[("target", Body), ("tools", Body)], diff --git a/crates/ui-egui/src/dialogs_parts.rs b/crates/ui-egui/src/dialogs_parts.rs index c07b7bb67..d6f0c4b59 100644 --- a/crates/ui-egui/src/dialogs_parts.rs +++ b/crates/ui-egui/src/dialogs_parts.rs @@ -1,7 +1,8 @@ //! Insert Part: the standard parts library (screws, bolts, nuts, washers, set screws, pins, //! bearings) by family, size and length, with the size's table values and a picture of the part. -//! Picking a hole (its wall or rim) seats the part in it with a rigid joint; without one the part -//! goes at the origin. The live preview shows it placed. +//! Picking a hole (its wall or rim) seats the part in it with a rigid joint, and picking several +//! seats the same part in each; without one the part goes at the origin. The live preview shows it +//! placed. use crate::ui_kit::{choice_row, text_row}; use egui::vec2; @@ -80,7 +81,7 @@ pub fn start(_app: &SolveApp, id: &str) -> Option<(Kind, Vec)> { matches!(id, "parts.fastener" | "parts.insert" | "parts.library").then(|| { ( Kind::Part(Pt { family: 0, size: 0, length: None, name: String::new(), picture: None, lib: None }), - vec![SelInput::new("Hole (optional)", FACES | EDGES, false)], + vec![SelInput::new("parts.holes", FACES | EDGES, true)], ) }) } @@ -89,20 +90,29 @@ fn library(app: &mut SolveApp) -> Vec { app.session.execute("parts.library", &json!({})).map(|v| families(&v)).unwrap_or_default() } -/// The insert command's parameters for the picks. -pub fn insert_params(lib: &[Family], k: &Pt, hole: Option<&Sel>) -> Result { +/// The insert command's parameters for the hole picks: one part seated in each. +pub fn insert_params(lib: &[Family], k: &Pt, holes: &[Sel]) -> Result { let f = lib.get(k.family).ok_or("the parts library is empty")?; let (size, _) = f.sizes.get(k.size).ok_or("pick a size")?; let mut p = json!({"family": f.id, "size": size}); if let Some(l) = k.length.and_then(|i| f.lengths.as_ref().and_then(|ls| ls.get(i))) { p["length"] = json!(l); } - match hole { - Some(Sel::Face { point, .. } | Sel::Edge { point, .. }) => p["at"] = json!([point.x, point.y, point.z]), - _ => { + let at: Vec = holes + .iter() + .filter_map(|h| match h { + Sel::Face { point, .. } | Sel::Edge { point, .. } => Some(json!([point.x, point.y, point.z])), + _ => None, + }) + .collect(); + match at.len() { + 0 => { p["point"] = json!([0.0, 0.0, 0.0]); p["direction"] = json!([0.0, 0.0, 1.0]); } + // One hole keeps the single pick's form. + 1 => p["at"] = at.into_iter().next().unwrap_or(Value::Null), + _ => p["at"] = Value::Array(at), } if !k.name.trim().is_empty() { p["name"] = json!(k.name.trim()); @@ -183,7 +193,7 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Pt, inputs: &[SelInpu let key = (k.family, k.size, k.length); let tex_id = egui::Id::new("sc_part_picture"); if k.picture != Some(key) { - let tex = insert_params(&lib, k, None) + let tex = insert_params(&lib, k, &[]) .ok() .and_then(|p| picture(&p)) .map(|img| ui.ctx().load_texture("sc_part_picture", img, egui::TextureOptions::LINEAR)); @@ -220,11 +230,7 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Pt, inputs: &[SelInpu } if inputs.first().is_some_and(|i| i.items.is_empty()) { ui.vertical(|ui| { - crate::ui_kit::text( - ui, - crate::i18n::tr_label("pick a hole to seat it, or OK places it at the origin"), - crate::ui_kit::Text::Note("parts_placement"), - ); + crate::ui_kit::text(ui, crate::i18n::tr("parts.placement_hint"), crate::ui_kit::Text::Note("parts_placement")); }); } false @@ -236,7 +242,7 @@ pub fn commands(app: &SolveApp, k: &Pt, inputs: &[SelInput]) -> Result l.clone(), None => app.session.scratch().execute("parts.library", &json!({})).map(|v| families(&v)).unwrap_or_default(), }; - let p = insert_params(&lib, k, inputs.first().and_then(|i| i.items.first()))?; + let p = insert_params(&lib, k, inputs.first().map_or(&[][..], |i| &i.items))?; // The toolbar's command when the engine has it (same parameters). let id = if solidcraft_engine::find_command("parts.fastener").is_some() { "parts.fastener" } else { "parts.insert" }; Ok(vec![(id.into(), p)]) @@ -312,20 +318,25 @@ mod tests { assert_eq!(default_length(&l[0], 99, &ls), 0, "so does a size that is not there"); } - /// A hole pick seats the part (`at`); without one it goes to the origin along Z. + /// A hole pick seats the part (`at`), several picks seat it in each hole (#9341); without one + /// it goes to the origin along Z. #[test] fn insert_params_for_picks() { let l = lib(); let k = Pt { family: 0, size: 1, length: Some(2), name: String::new(), picture: None, lib: None }; let hole = Sel::Face { body: "Body1".into(), index: 3, point: Vec3::new(5.0, 0.0, 10.0) }; - let p = insert_params(&l, &k, Some(&hole)).unwrap(); + let p = insert_params(&l, &k, std::slice::from_ref(&hole)).unwrap(); assert_eq!(p, json!({"family": "socket_head_cap_screw", "size": "M6", "length": 20.0, "at": [5.0, 0.0, 10.0]})); + let rim = Sel::Edge { body: "Body1".into(), index: 7, point: Vec3::new(25.0, 0.0, 10.0) }; + let p = insert_params(&l, &k, &[hole, rim]).unwrap(); + assert_eq!(p["at"], json!([[5.0, 0.0, 10.0], [25.0, 0.0, 10.0]])); + assert!(p.get("point").is_none()); let k = Pt { family: 1, size: 0, length: None, name: "Front bearing".into(), picture: None, lib: None }; - let p = insert_params(&l, &k, None).unwrap(); + let p = insert_params(&l, &k, &[]).unwrap(); assert_eq!(p["point"], json!([0.0, 0.0, 0.0])); assert_eq!(p["name"], "Front bearing"); assert!(p.get("length").is_none()); - assert!(insert_params(&[], &k, None).is_err()); + assert!(insert_params(&[], &k, &[]).is_err()); } /// Every dimension the library gives for a size is labelled in words, in each family.