Skip to content

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

Rectangular Pattern: a drag arrow for Spacing 2 (#2462) (#2463)

HiroKenEz7cf8c783e645
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 52c677f8bd..28b05b8568 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -141,6 +141,21 @@ pub fn anchor(app: &SolveApp, d: &Dialog) -> Option<(Vec3, Option<Vec3>)> {
})
}
+/// The direction a rectangular pattern's Spacing 2 grows along (#2462): a solid pattern's picked
+/// (or kept) second direction, none until it has one; a sketch pattern's second direction, which
+/// is square to the first unless one is picked.
+fn pattern_dir2(app: &SolveApp, d: &Dialog) -> Option<Vec3> {
+ match &d.kind {
+ Kind::PatternRect { .. } => match d.inputs.get(2).and_then(|i| i.items.first()) {
+ Some(x) => crate::dialogs::axis_of(app, x).map(|a| a.1),
+ None => d.extra.get("dir2").and_then(|v| serde_json::from_value::<[f64; 3]>(v.clone()).ok()).map(|a| Vec3::new(a[0], a[1], a[2])),
+ },
+ Kind::SketchModify(_) => crate::sketch_modify_ui::arrow2(app, d),
+ _ => None,
+ }
+ .and_then(Vec3::normalized)
+}
+
/// The point halfway along a polyline, with the unit tangent there. Indexing the middle sample
/// instead would land on a two-point line's far end, putting the value box and its arrow on a
/// corner. None for a polyline with no length.
@@ -402,6 +417,19 @@ fn draw(app: &SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj: &Proj,
{
drag_arrow(ui, painter, proj, base, bs, -n, l2, true, egui::Id::new("sc_manipulator2"), half_height, distance2);
}
+ // A rectangular pattern's second direction gets its own arrow for Spacing 2 (#2462), as the
+ // first direction's arrow sets Spacing.
+ if let Some(n2) = pattern_dir2(app, d) {
+ let spacing2 = match &mut d.kind {
+ Kind::PatternRect { spacing2, .. } | Kind::SketchModify(crate::sketch_modify_ui::Mod::PatternRect { spacing2, .. }) => Some(spacing2),
+ _ => None,
+ };
+ if let Some(spacing2) = spacing2
+ && let Some(l2) = app.session.doc.eval(spacing2, ValueKind::Length).ok().filter(|v| v.is_finite())
+ {
+ drag_arrow(ui, painter, proj, base, bs, n2, l2, false, egui::Id::new("sc_manipulator2"), half_height, spacing2);
+ }
+ }
// An offset plane shows where it will be.
if let (Kind::OffsetPlane { offset }, Some(n)) = (&d.kind, dir)
&& let Ok(off) = app.session.doc.eval(offset, ValueKind::Length)
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index afe53e8b51..510ef9688c 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -73,6 +73,8 @@ mod measurement_ui_tests;
#[cfg(test)]
mod menu_tests;
#[cfg(test)]
+mod pattern_arrow_tests;
+#[cfg(test)]
mod surface_clamp_tests;
#[cfg(test)]
mod timeline_marks_tests;
diff --git a/crates/ui-egui/src/pattern_arrow_tests.rs b/crates/ui-egui/src/pattern_arrow_tests.rs
new file mode 100644
index 0000000000..6aad4a42de
--- /dev/null
+++ b/crates/ui-egui/src/pattern_arrow_tests.rs
@@ -0,0 +1,84 @@
+//! A rectangular pattern's second direction has its own drag arrow for Spacing 2 (#2462), as
+//! the first direction's arrow sets Spacing.
+
+use crate::dialogs::Kind;
+use crate::scenario::{Harness, handle_at};
+use crate::sketch_modify_ui::Mod;
+use serde_json::json;
+
+fn spacings(h: &Harness) -> (String, String) {
+ match h.app.dialog.as_ref().map(|d| &d.kind) {
+ Some(Kind::PatternRect { spacing, spacing2, .. }) | Some(Kind::SketchModify(Mod::PatternRect { spacing, spacing2, .. })) => {
+ (spacing.clone(), spacing2.clone())
+ }
+ other => panic!("no rectangular pattern window: {other:?}"),
+ }
+}
+
+#[test]
+fn a_sketch_pattern_drags_its_second_spacing_square_to_the_first() {
+ let mut h = Harness::new();
+ h.run(&json!([
+ {"start": "sketch.create"},
+ {"click": {"plane": "XY"}},
+ {"start": "sketch.circle.center"},
+ {"click": {"sketch": [0, 0]}},
+ {"click": {"sketch": [5, 0]}},
+ {"key": "Escape"},
+ {"start": "sketch.pattern.rectangular"},
+ {"click": {"sketch": [5, 0]}}
+ ]))
+ .unwrap();
+ h.settle();
+ let (Some(a1), Some(a2)) = (handle_at("arrow"), handle_at("arrow2")) else {
+ panic!("both arrows: {:?} {:?}", handle_at("arrow"), handle_at("arrow2"));
+ };
+ // Looking down on XY, the first direction (sketch x) points right and the second (square to
+ // it) up the screen: the arrows share their base, so each lies along its own axis.
+ assert!(a1.x > a2.x + 10.0 && (a2.y - a1.y).abs() > 10.0, "arrow {a1:?}, arrow2 {a2:?}");
+ let (spacing, spacing2) = spacings(&h);
+ // Dragging the second arrow further out sets Spacing 2 only.
+ h.call("ui.drag", json!({"x0": a2.x, "y0": a2.y, "x1": a2.x, "y1": a2.y - 60.0, "steps": 6}));
+ h.settle();
+ let (after, after2) = spacings(&h);
+ assert_eq!(after, spacing);
+ assert_ne!(after2, spacing2);
+ let grown = h.app.session.doc.eval(&after2, solidcraft_engine::doc::expr::Kind::Length).unwrap();
+ let was = h.app.session.doc.eval(&spacing2, solidcraft_engine::doc::expr::Kind::Length).unwrap();
+ assert!(grown > was, "Spacing 2 {spacing2} -> {after2}");
+}
+
+#[test]
+fn a_solid_pattern_shows_the_second_arrow_once_it_has_a_second_direction() {
+ let mut h = Harness::new();
+ h.run(&json!([
+ {"call": "engine.execute", "params": {"command": "solid.box", "params": {"length": 10, "width": 10, "height": 10}}},
+ {"call": "ui.view", "params": {"view": "iso"}},
+ {"start": "solid.pattern.rectangular"},
+ {"click": {"world": [5, 5, 10]}},
+ {"call": "ui.dialogInput", "params": {"index": 1}},
+ {"click": {"axis": "X"}}
+ ]))
+ .unwrap();
+ h.settle();
+ assert!(handle_at("arrow").is_some());
+ assert!(handle_at("arrow2").is_none(), "no second direction yet, so no second arrow");
+ h.run(&json!([
+ {"call": "ui.dialogInput", "params": {"index": 2}},
+ {"click": {"axis": "Y"}}
+ ]))
+ .unwrap();
+ h.settle();
+ let Some(a2) = handle_at("arrow2") else { panic!("no second arrow with a second direction") };
+ let (spacing, spacing2) = spacings(&h);
+ let toward = h.call("ui.worldToScreen", json!({"points": [[5, 60, 5]]}))["result"][0].clone();
+ let (tx, ty) = (toward[0].as_f64().unwrap() as f32, toward[1].as_f64().unwrap() as f32);
+ // Drag it along +Y (towards a point far out on Y): Spacing 2 changes, Spacing doesn't.
+ let (dx, dy) = (tx - a2.x, ty - a2.y);
+ let len = (dx * dx + dy * dy).sqrt().max(1.0);
+ h.call("ui.drag", json!({"x0": a2.x, "y0": a2.y, "x1": a2.x + dx / len * 60.0, "y1": a2.y + dy / len * 60.0, "steps": 6}));
+ h.settle();
+ let (after, after2) = spacings(&h);
+ assert_eq!(after, spacing);
+ assert_ne!(after2, spacing2);
+}
diff --git a/crates/ui-egui/src/sketch_modify_ui.rs b/crates/ui-egui/src/sketch_modify_ui.rs
index 2f6cf12c72..9d81ae570d 100644
--- a/crates/ui-egui/src/sketch_modify_ui.rs
+++ b/crates/ui-egui/src/sketch_modify_ui.rs
@@ -408,10 +408,7 @@ pub fn arrow(app: &SolveApp, d: &Dialog) -> Option<Vec3> {
match k {
Mod::PatternRect { alignment, reversed, .. } => {
let plane = app.session.world_state().sketch(app.session.active_sketch?)?.plane;
- let dir = match d.inputs.get(1).and_then(|i| i.items.first()) {
- Some(sel) => sketch_dir(app, sel)?,
- None => Vec2::X,
- };
+ let dir = pattern_dir(app, d)?;
Some(plane.dir_to_world(if (*alignment == PatternAlignment::Left) ^ *reversed { -dir } else { dir }))
}
// A total angle is a rotation. The viewport draws a straight drag arrow and a rotation
@@ -421,6 +418,30 @@ pub fn arrow(app: &SolveApp, d: &Dialog) -> Option<Vec3> {
}
}
+/// The rectangular pattern's first direction in the sketch: the picked one, or the sketch's x axis.
+fn pattern_dir(app: &SolveApp, d: &Dialog) -> Option<Vec2> {
+ match d.inputs.get(1).and_then(|i| i.items.first()) {
+ Some(sel) => sketch_dir(app, sel),
+ None => Some(Vec2::X),
+ }
+}
+
+/// The world direction the rectangular pattern's Spacing 2 grows along, for its own drag arrow
+/// (#2462): the picked second direction, the one an edited pattern was made with, or square to
+/// the first, as the command does.
+pub fn arrow2(app: &SolveApp, d: &Dialog) -> Option<Vec3> {
+ let Kind::SketchModify(Mod::PatternRect { alignment2, reversed2, .. }) = &d.kind else { return None };
+ let plane = app.session.world_state().sketch(app.session.active_sketch?)?.plane;
+ let dir = match d.inputs.get(2).and_then(|i| i.items.first()) {
+ Some(sel) => sketch_dir(app, sel)?,
+ None => match d.extra.get("dir2").and_then(|v| serde_json::from_value::<Vec2>(v.clone()).ok()).and_then(Vec2::normalized) {
+ Some(kept) => kept,
+ None => pattern_dir(app, d)?.perp(),
+ },
+ };
+ Some(plane.dir_to_world(if (*alignment2 == PatternAlignment::Left) ^ *reversed2 { -dir } else { dir }))
+}
+
/// What the first value means for this corner type.
fn size_label(chamfer: bool, ctype: usize) -> &'static str {
if !chamfer {