Skip to content

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

Pattern on Path: reopen saved patterns in the editor (#8784) (#8805)

Colin Faulkinghamfd8ddc23b7c7
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index bc982542d4..c7246c166d 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -1298,7 +1298,9 @@ impl Dialog {
Kind::PathPlane { t, .. } => ("Position (0 to 1)", ValueKind::Unitless, t),
Kind::Rib { thickness, .. } => ("Thickness", ValueKind::Length, thickness),
Kind::Emboss { depth, .. } => ("Depth", ValueKind::Length, depth),
- Kind::PathPattern { spacing, .. } => ("Spacing", ValueKind::Length, spacing),
+ Kind::PathPattern { spacing, .. } => {
+ (if self.extra.contains_key("distance") { "Distance" } else { "Spacing" }, ValueKind::Length, spacing)
+ }
Kind::Pipe { diameter, .. } => ("Diameter", ValueKind::Length, diameter),
Kind::Stock { margin } => ("Margin", ValueKind::Length, margin),
Kind::AnglePlane { angle } => ("Angle", ValueKind::Angle, angle),
@@ -4344,7 +4346,9 @@ fn dialog_commands(app: &SolveApp, d: &Dialog) -> Result<Vec<(String, Value)>, S
need(1, "the path")?;
let path = sels(d, 1).curve_ids();
let sketch = picks_sketch(app, sels(d, 1), None).ok_or("the path curves must be in one sketch")?;
- let mut p = json!({"path_sketch": sketch, "path": path, "count": count, "spacing": spacing});
+ let mut p = json!({"path_sketch": sketch, "path": path, "count": count});
+ // A saved extent stays first-to-last distance when the quantity changes.
+ p[if d.extra.contains_key("distance") { "distance" } else { "spacing" }] = json!(spacing);
pattern_objects(s, sels(d, 0), &mut p);
("solid.pattern.path", p)
}
@@ -5812,8 +5816,20 @@ pub fn for_feature(app: &SolveApp, id: u64, marker: Option<usize>) -> Option<Dia
}
d
}
- // No dialog for a path pattern yet: edit it with timeline.redefine.
- PatternKind::Path { .. } => return None,
+ PatternKind::Path { path_sketch, path, count, spacing, extent, flip, orient } => {
+ let mut d = start("solid.pattern.path")?;
+ d.kind = Kind::PathPattern { count: count.to_string(), spacing: spacing.to_string() };
+ if let Some(inp) = d.inputs.get_mut(1) {
+ inp.items = path.iter().map(|id| crate::selection::curve_pick(id, path_sketch.0, s.active_sketch)).collect();
+ }
+ // Preserve options the current dialog does not expose as controls.
+ d.extra.insert("flip".into(), json!(flip));
+ d.extra.insert("orientation".into(), json!(if *orient { "path" } else { "identical" }));
+ if *extent {
+ d.extra.insert("distance".into(), json!(spacing));
+ }
+ d
+ }
};
// Replay references stay features, including features that created a body.
let as_features = !features.is_empty();
diff --git a/crates/ui-egui/src/dialogs_dense.rs b/crates/ui-egui/src/dialogs_dense.rs
index 650e9bf331..744121ae15 100644
--- a/crates/ui-egui/src/dialogs_dense.rs
+++ b/crates/ui-egui/src/dialogs_dense.rs
@@ -756,7 +756,7 @@ fn additional_rows(app: &mut SolveApp, d: &mut Dialog, ui: &mut egui::Ui) -> boo
}
Kind::PathPattern { count, spacing } => {
enter |= quantity_row(ui, &app.session.doc, "Quantity", count);
- enter |= expr_row(ui, "Spacing", spacing);
+ enter |= expr_row(ui, if d.extra.contains_key("distance") { "Distance" } else { "Spacing" }, spacing);
}
Kind::Pipe { diameter, wall, operation } => {
enter |= expr_row(ui, "Diameter", diameter);
diff --git a/crates/ui-egui/src/preselect_tests.rs b/crates/ui-egui/src/preselect_tests.rs
index 9d08722f79..de6423aac4 100644
--- a/crates/ui-egui/src/preselect_tests.rs
+++ b/crates/ui-egui/src/preselect_tests.rs
@@ -1046,6 +1046,97 @@ fn a_hole_is_patterned_as_a_feature() {
assert_eq!(e.inputs[0].items, vec![Sel::Feature { id: hole }]);
}
+fn path_pattern_editor(params: serde_json::Value) -> crate::scenario::Harness {
+ let mut h = crate::scenario::Harness::new();
+ h.app.run("solid.box", json!({"length":4,"width":4,"height":4,"name":"Seed","body_name":"Pin"})).unwrap();
+ h.app.run("sketch.create", json!({"plane":"XY","name":"Path"})).unwrap();
+ h.app.run("sketch.line", json!({"points":[[0,0],[30,0],[60,0]],"ids":["first","second"]})).unwrap();
+ h.app.run("sketch.finish", json!({})).unwrap();
+ h.app.run("parameters.add", json!({"name":"copies","expression":"3","unit":""})).unwrap();
+ h.app.run("parameters.add", json!({"name":"pitch","expression":"10 mm"})).unwrap();
+ let mut p = json!({"path_sketch":"Path","path":["first","second"],"count":"copies","name":"Copies"});
+ p.as_object_mut().unwrap().extend(params.as_object().unwrap().clone());
+ h.app.run("solid.pattern.path", p).unwrap();
+ h.frames(4);
+ h
+}
+
+#[test]
+fn path_pattern_editor_restores_sources_path_expressions_and_options() {
+ for as_features in [false, true] {
+ for (distance_key, flip, orient) in [("spacing", false, false), ("spacing", true, true), ("distance", true, false), ("distance", false, true)]
+ {
+ let mut p = json!({"flip":flip,"orientation":if orient { "path" } else { "identical" }});
+ p[distance_key] = json!("pitch");
+ p[if as_features { "features" } else { "bodies" }] = if as_features { json!(["Seed"]) } else { json!(["Pin"]) };
+ let mut h = path_pattern_editor(p);
+ let original = h.app.session.doc.find_feature("Copies").unwrap().clone();
+ let seed = h.app.session.doc.find_feature("Seed").unwrap().id;
+ let path = h.app.session.doc.find_feature("Path").unwrap().id;
+ let bounds: Vec<_> = h.app.session.world_state().bodies.iter().map(|b| b.mesh().bounds()).collect();
+ h.step(&json!({"call":"ui.editFeature","params":{"feature":"Copies"}})).unwrap();
+ h.frames(8);
+ assert!(!h.app.ui.palette_open, "Edit Feature must open the existing pattern dialog");
+ let d = h.app.dialog.as_mut().expect("path pattern editor");
+ assert!(matches!(&d.kind, Kind::PathPattern { count, spacing } if count == "copies" && spacing == "pitch"));
+ assert_eq!(d.inputs[0].accept, if as_features { crate::selection::FEATURES } else { crate::selection::BODIES });
+ assert_eq!(d.inputs[0].items, if as_features { vec![Sel::Feature { id: seed }] } else { vec![Sel::Body { name: "Pin".into() }] });
+ assert_eq!(
+ d.inputs[1].items,
+ vec![Sel::SketchCurve { id: "first".into(), sketch: Some(path) }, Sel::SketchCurve { id: "second".into(), sketch: Some(path) },]
+ );
+ assert_eq!(d.primary().unwrap().0, if distance_key == "distance" { "Distance" } else { "Spacing" });
+ h.step(&json!({"widget":"OK"})).unwrap();
+ assert!(h.app.dialog.is_none(), "{:?}", h.app.session.log);
+ let edited = h.app.session.doc.find_feature("Copies").unwrap();
+ assert_eq!(edited.id, original.id);
+ assert_eq!(edited.kind, original.kind);
+ assert_eq!(edited.param_names, original.param_names);
+ assert_eq!(h.app.session.world_state().bodies.iter().map(|b| b.mesh().bounds()).collect::<Vec<_>>(), bounds);
+ }
+ }
+}
+
+#[test]
+fn path_pattern_editor_applies_in_place_cancels_and_undoes() {
+ for distance_key in ["spacing", "distance"] {
+ let mut p = json!({"bodies":["Pin"]});
+ p[distance_key] = json!("pitch");
+ let mut h = path_pattern_editor(p);
+ let original = h.app.session.doc.find_feature("Copies").unwrap().clone();
+ let feature_count = h.app.session.doc.features.len();
+ let undo = h.app.session.undo.len();
+ for cancel in [true, false] {
+ h.step(&json!({"call":"ui.editFeature","params":{"feature":"Copies"}})).unwrap();
+ h.frames(8);
+ let d = h.app.dialog.as_mut().expect("path pattern editor");
+ let Kind::PathPattern { count, spacing } = &mut d.kind else { panic!("path pattern") };
+ *count = "5".into();
+ *spacing = "12 mm".into();
+ h.frames(4);
+ h.step(&json!({"widget":if cancel { "Cancel" } else { "OK" }})).unwrap();
+ assert!(h.app.dialog.is_none(), "{:?}", h.app.session.log);
+ assert_eq!(h.app.session.doc.features.len(), feature_count);
+ let edited = h.app.session.doc.find_feature("Copies").unwrap();
+ assert_eq!(edited.id, original.id);
+ assert_eq!(edited.param_names, original.param_names);
+ if cancel {
+ assert_eq!(edited.kind, original.kind);
+ assert_eq!(h.app.session.undo.len(), undo);
+ assert_eq!(h.app.session.world_state().bodies.len(), 3);
+ } else {
+ assert_eq!(h.app.session.world_state().bodies.len(), 5);
+ let max_x = h.app.session.world_state().bodies.iter().map(|b| b.mesh().bounds().max.x).fold(f64::NEG_INFINITY, f64::max);
+ let expected = if distance_key == "distance" { 16.0 } else { 52.0 };
+ assert!((max_x - expected).abs() < 1e-7, "five 4 mm bodies, {distance_key} 12 mm: {max_x}");
+ h.app.run("edit.undo", json!({})).unwrap();
+ assert_eq!(h.app.session.doc.find_feature("Copies").unwrap().kind, original.kind);
+ assert_eq!(h.app.session.world_state().bodies.len(), 3);
+ }
+ }
+ }
+}
+
#[test]
fn a_hole_is_mirrored_as_a_feature() {
let mut app = app_with(json!([