Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Loft and Surface Loft: two sections from one sketch (#8221) (#8222)

Jesper Wecffa6e1580a2
diff --git a/assets/locales/pending/loft-sections-one-sketch.json b/assets/locales/pending/loft-sections-one-sketch.json
new file mode 100644
index 0000000000..708b80eff4
--- /dev/null
+++ b/assets/locales/pending/loft-sections-one-sketch.json
@@ -0,0 +1,26 @@
+{
+ "en": {
+ "Sections": "Sections"
+ },
+ "ar": {
+ "Sections": "المقاطع"
+ },
+ "de": {
+ "Sections": "Querschnitte"
+ },
+ "he": {
+ "Sections": "חתכים"
+ },
+ "ja": {
+ "Sections": "セクション"
+ },
+ "ko": {
+ "Sections": "단면"
+ },
+ "vi": {
+ "Sections": "Các mặt cắt"
+ },
+ "zh": {
+ "Sections": "截面"
+ }
+}
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index d8a8678c84..0e323356df 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -4641,33 +4641,31 @@ fn dialog_commands(app: &SolveApp, d: &Dialog) -> Result<Vec<(String, Value)>, S
}
Kind::Loft { operation, rail_fit } => {
need(0, "profiles of two or more sketches")?;
- // One section per sketch, in the order the sketches were first picked; a sketch point
- // is a section of its own, an apex the loft closes to (#4830).
- let mut sections: Vec<(u64, Vec<usize>, Option<&str>)> = Vec::new();
+ // Each picked profile is a section, in pick order, two of them from one sketch
+ // included (#8221); a sketch point is a section of its own, an apex the loft closes
+ // to (#4830).
+ let mut sections: Vec<Value> = Vec::new();
+ let (mut profile_sketches, mut apexes) = (Vec::new(), 0);
for x in sels(d, 0) {
match x {
- Sel::Profile { sketch, index } => match sections.iter_mut().find(|(sk, _, apex)| sk == sketch && apex.is_none()) {
- Some((_, v, _)) => v.push(*index),
- None => sections.push((*sketch, vec![*index], None)),
- },
+ Sel::Profile { sketch, index } => {
+ sections.push(json!({"sketch": sketch, "profiles": [index]}));
+ profile_sketches.push(*sketch);
+ }
Sel::SketchPoint { id } => {
if let Some((sk, point)) = id.split_once(':').and_then(|(sk, point)| Some((sk.parse::<u64>().ok()?, point))) {
- sections.push((sk, Vec::new(), Some(point)));
+ sections.push(json!({"sketch": sk, "point": point}));
+ apexes += 1;
}
}
_ => {}
}
}
- if sections.len() < 2 {
+ profile_sketches.sort_unstable();
+ profile_sketches.dedup();
+ if profile_sketches.len() + apexes < 2 {
return Err("pick profiles in two or more sketches, or a profile and a sketch point".into());
}
- let sections: Vec<Value> = sections
- .into_iter()
- .map(|(sk, v, apex)| match apex {
- Some(point) => json!({"sketch": sk, "point": point}),
- None => json!({"sketch": sk, "profiles": v}),
- })
- .collect();
let mut p = json!({"sections": sections, "operation": OPS.get(*operation).copied().unwrap_or("new")});
crate::dialogs_sweep::rail_params(app, *rail_fit, [sels(d, 1), sels(d, 2)], &mut p)?;
("solid.loft", p)
diff --git a/crates/ui-egui/src/dialogs_surface.rs b/crates/ui-egui/src/dialogs_surface.rs
index fa2fbadaf0..fc26a53b50 100644
--- a/crates/ui-egui/src/dialogs_surface.rs
+++ b/crates/ui-egui/src/dialogs_surface.rs
@@ -173,7 +173,7 @@ fn inputs(k: &Surf) -> Vec<SelInput> {
}
Surf::Loft { .. } => {
let [rail, rail2] = crate::dialogs_sweep::rail_inputs();
- vec![SelInput::new("Sections (one sketch each)", PROFILES | CURVES, true), rail, rail2]
+ vec![SelInput::new("Sections", PROFILES | CURVES, true), rail, rail2]
}
Surf::Blend { .. } => vec![SelInput::new("From edges", EDGES, true), SelInput::new("To edges", EDGES, true)],
Surf::Boundary { .. } => vec![SelInput::new("Curves", CURVES | EDGES, true), SelInput::new("Cross curves", CURVES | EDGES, true)],
@@ -517,30 +517,37 @@ pub fn commands(app: &SolveApp, k: &Surf, inputs: &[SelInput], extra: &Map<Strin
("surface.sweep", p)
}
Surf::Loft { rail_fit } => {
- need(0, "the sections, one sketch each")?;
- // One section per sketch, in the order the sketches were first picked.
- let sketch_of = |x: &Sel| match x {
- Sel::Profile { sketch, .. } => Some(*sketch),
- Sel::SketchCurve { .. } => crate::dialogs::picks_sketch(app, std::slice::from_ref(x), None),
- _ => None,
- };
- let mut order: Vec<u64> = Vec::new();
- for x in items(inputs, 0) {
- let sk = sketch_of(x).ok_or("a picked curve is no longer in a sketch")?;
- if !order.contains(&sk) {
- order.push(sk);
+ need(0, "the sections")?;
+ // Each picked profile is a section, and so is each chain of picked curves (those of
+ // one sketch that meet end to end), in the order of their first pick: two sections
+ // can come from one sketch (#8221).
+ let picks = items(inputs, 0);
+ let curves: Vec<Sel> = picks.iter().filter(|x| matches!(x, Sel::SketchCurve { .. })).cloned().collect();
+ let mut chained: Vec<Option<Chain>> = chains(app, &curves)?.into_iter().map(Some).collect();
+ let mut sections: Vec<Value> = Vec::new();
+ let mut sketches: Vec<u64> = Vec::new();
+ for x in picks {
+ match x {
+ Sel::Profile { sketch, index } => {
+ sections.push(json!({"sketch": sketch, "profiles": [index]}));
+ sketches.push(*sketch);
+ }
+ Sel::SketchCurve { id, .. } => {
+ let sk = crate::dialogs::picks_sketch(app, std::slice::from_ref(x), None).ok_or("a picked curve is no longer in a sketch")?;
+ let holds = |c: &Option<Chain>| matches!(c, Some(Chain::Sketch { sketch, ids }) if *sketch == sk && ids.contains(id));
+ if let Some(Some(Chain::Sketch { sketch, ids })) = chained.iter_mut().find(|c| holds(c)).map(Option::take) {
+ sections.push(json!({"sketch": sketch, "curves": ids}));
+ sketches.push(sketch);
+ }
+ }
+ _ => {}
}
}
- if order.len() < 2 {
+ sketches.sort_unstable();
+ sketches.dedup();
+ if sketches.len() < 2 {
return Err("pick sections in two or more sketches".into());
}
- let sections: Vec<Value> = order
- .iter()
- .map(|sk| {
- let picks: Vec<Sel> = items(inputs, 0).iter().filter(|x| sketch_of(x) == Some(*sk)).cloned().collect();
- sheet_picks(app, &picks)
- })
- .collect::<Result<_, String>>()?;
let mut p = json!({ "sections": sections });
crate::dialogs_sweep::rail_params(app, *rail_fit, [items(inputs, 1), items(inputs, 2)], &mut p)?;
("surface.loft", p)
diff --git a/crates/ui-egui/src/loft_rails_dialog_tests.rs b/crates/ui-egui/src/loft_rails_dialog_tests.rs
index 6a3a74aa6d..5a5a74aa23 100644
--- a/crates/ui-egui/src/loft_rails_dialog_tests.rs
+++ b/crates/ui-egui/src/loft_rails_dialog_tests.rs
@@ -1,5 +1,6 @@
//! The Loft and Surface Loft dialogs' guide rails (#201): rails picked in the dialog reach the
//! commands with their sketches and the fit, and editing the loft brings them back and keeps them.
+//! Their sections follow the picks, two of them from one sketch included (#8221).
use crate::dialogs::Kind;
use crate::dialogs_surface::Surf;
use crate::scenario::Harness;
@@ -127,3 +128,92 @@ fn curves_that_share_an_id_are_picked_from_their_own_sketches() {
let commands = crate::dialogs::apply_commands(&h.app, h.app.dialog.as_ref().unwrap()).unwrap();
assert_eq!(commands[0].1["sections"], json!([{"sketch": low, "curves": ["l"]}, {"sketch": high, "curves": ["l"]}]));
}
+
+/// A sketch at height `z` named `name`, made by `draw`; its id.
+fn sketch_at(h: &mut Harness, name: &str, z: i32, draw: &[(&str, serde_json::Value)]) -> u64 {
+ h.app.run("sketch.create", json!({"name": name, "plane": {"origin": [0, 0, z], "x_dir": [1, 0, 0], "y_dir": [0, 1, 0]}})).unwrap();
+ for (command, params) in draw {
+ h.app.run(command, params.clone()).unwrap();
+ }
+ h.app.run("sketch.finish", json!({})).unwrap();
+ h.app.session.doc.find_feature(name).unwrap().id
+}
+
+/// Two sections can come from one sketch (#8221): Surface Loft from one line of `A`, over `B`'s,
+/// back to `A`'s other line makes three sections in pick order, and an edit keeps them.
+#[test]
+fn surface_loft_sections_from_one_sketch_stay_apart() {
+ let mut h = Harness::new();
+ let a = sketch_at(
+ &mut h,
+ "A",
+ 0,
+ &[
+ ("sketch.line", json!({"points": [[-10, 0], [10, 0]], "ids": ["front"]})),
+ ("sketch.line", json!({"points": [[-10, 40], [10, 40]], "ids": ["back"]})),
+ ],
+ );
+ let b = sketch_at(&mut h, "B", 30, &[("sketch.line", json!({"points": [[-10, 20], [10, 20]], "ids": ["top"]}))]);
+ h.step(&json!({"call":"ui.start","params":{"command":"surface.loft"}})).unwrap();
+ h.frames(3);
+ let d = h.app.dialog.as_mut().unwrap();
+ d.inputs[0].items = vec![
+ Sel::SketchCurve { id: "front".into(), sketch: Some(a) },
+ Sel::SketchCurve { id: "top".into(), sketch: Some(b) },
+ Sel::SketchCurve { id: "back".into(), sketch: Some(a) },
+ ];
+ let expected = json!([{"sketch": a, "curves": ["front"]}, {"sketch": b, "curves": ["top"]}, {"sketch": a, "curves": ["back"]}]);
+ let commands = crate::dialogs::apply_commands(&h.app, h.app.dialog.as_ref().unwrap()).unwrap();
+ assert_eq!(commands[0].1["sections"], expected);
+ ok(&mut h);
+ let name = h.app.session.doc.features.last().unwrap().name.clone();
+ h.step(&json!({"call":"ui.editFeature","params":{"feature":name}})).unwrap();
+ h.frames(8);
+ let commands = crate::dialogs::apply_commands(&h.app, h.app.dialog.as_ref().unwrap()).unwrap();
+ assert_eq!(commands[0].1["params"]["sections"], expected);
+ ok(&mut h);
+}
+
+/// Curves of one sketch that meet end to end are still one Surface Loft section, whatever order
+/// they were picked in.
+#[test]
+fn surface_loft_curves_that_meet_are_one_section() {
+ let mut h = Harness::new();
+ let a = sketch_at(&mut h, "A", 0, &[("sketch.line", json!({"points": [[-10, 0], [0, 0], [10, 5]], "ids": ["left", "right"]}))]);
+ let b = sketch_at(&mut h, "B", 30, &[("sketch.line", json!({"points": [[-10, 0], [10, 0]], "ids": ["top"]}))]);
+ h.step(&json!({"call":"ui.start","params":{"command":"surface.loft"}})).unwrap();
+ h.frames(3);
+ let d = h.app.dialog.as_mut().unwrap();
+ d.inputs[0].items = vec![
+ Sel::SketchCurve { id: "right".into(), sketch: Some(a) },
+ Sel::SketchCurve { id: "top".into(), sketch: Some(b) },
+ Sel::SketchCurve { id: "left".into(), sketch: Some(a) },
+ ];
+ let commands = crate::dialogs::apply_commands(&h.app, h.app.dialog.as_ref().unwrap()).unwrap();
+ assert_eq!(commands[0].1["sections"], json!([{"sketch": a, "curves": ["right", "left"]}, {"sketch": b, "curves": ["top"]}]));
+}
+
+/// Loft from one circle of `C`, over `D`'s, back to `C`'s other circle (#8221): each picked
+/// profile is a section, in pick order, and an edit keeps them.
+#[test]
+fn loft_profiles_from_one_sketch_stay_apart() {
+ let mut h = Harness::new();
+ let circle = |x: i32, y: i32| ("sketch.circle.center", json!({"center": [x, y], "diameter": 12}));
+ let c = sketch_at(&mut h, "C", 0, &[circle(0, 0), circle(0, 40)]);
+ let d_sketch = sketch_at(&mut h, "D", 30, &[circle(0, 20)]);
+ h.step(&json!({"call":"ui.start","params":{"command":"solid.loft"}})).unwrap();
+ h.frames(3);
+ let d = h.app.dialog.as_mut().unwrap();
+ d.inputs[0].items = vec![Sel::Profile { sketch: c, index: 0 }, Sel::Profile { sketch: d_sketch, index: 0 }, Sel::Profile { sketch: c, index: 1 }];
+ let expected = json!([{"sketch": c, "profiles": [0]}, {"sketch": d_sketch, "profiles": [0]}, {"sketch": c, "profiles": [1]}]);
+ let commands = crate::dialogs::apply_commands(&h.app, h.app.dialog.as_ref().unwrap()).unwrap();
+ assert_eq!(commands[0].1["sections"], expected);
+ ok(&mut h);
+ assert_eq!(h.app.session.world_state().bodies.len(), 1, "{:?}", h.app.session.log);
+ let name = h.app.session.doc.features.last().unwrap().name.clone();
+ h.step(&json!({"call":"ui.editFeature","params":{"feature":name}})).unwrap();
+ h.frames(8);
+ let commands = crate::dialogs::apply_commands(&h.app, h.app.dialog.as_ref().unwrap()).unwrap();
+ assert_eq!(commands[0].1["params"]["sections"], expected);
+ ok(&mut h);
+}