Skip to content

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

Canvas and Decal: send the picked face, so a large design takes the image (#5592) (#5593)

dudedagger4259faca385096
diff --git a/crates/engine/src/cmd/sketch_canvas.rs b/crates/engine/src/cmd/sketch_canvas.rs
index f963b56d39..ed37754436 100644
--- a/crates/engine/src/cmd/sketch_canvas.rs
+++ b/crates/engine/src/cmd/sketch_canvas.rs
@@ -16,7 +16,7 @@ pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("canvas.decal", "Decal", add_decal)
.at("SKETCH", "INSERT")
.icon("decal")
- .params("path | data; face: [x,y,z] on a planar face (the image is centred there); width?: mm; angle?: deg; opacity?: (default 1)"),
+ .params("path | data; face: [x,y,z] on a planar face (the image is centred there), or the picked face as sketch.create's plane takes it ({face, body, index, path?}); width?: mm; angle?: deg; opacity?: (default 1)"),
CommandSpec::new("canvas.edit", "Edit Canvas", edit_canvas).at("SKETCH", "INSERT").icon("canvas").params("canvas: id or name; center?, width?, angle?: deg, opacity?, flip?, visible?, name?"),
CommandSpec::new("canvas.calibrate", "Calibrate Canvas", calibrate).icon("measure")
.params("canvas: id or name; a, b: two points on the canvas plane [x,y]; distance: their true distance (mm): scales the image about a"),
@@ -88,9 +88,11 @@ fn add_canvas(s: &mut Session, p: &Value) -> Result<Value> {
fn add_decal(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "canvas.decal";
let face = p.get("face").cloned().ok_or_else(|| bad(cmd, "`face` must be a point [x,y,z] on a planar face"))?;
+ // A picked face as `sketch.create` takes it ({face, body, index, path?}) is used as it is.
+ let plane = if face.get("face").is_some() { face } else { json!({"face": face}) };
let mut q = p.clone();
if let Some(o) = q.as_object_mut() {
- o.insert("plane".into(), json!({"face": face}));
+ o.insert("plane".into(), plane);
}
make(s, &q, cmd, true)
}
diff --git a/crates/ui-egui/src/file_picker.rs b/crates/ui-egui/src/file_picker.rs
index 7634ea8ce2..d64f158ecd 100644
--- a/crates/ui-egui/src/file_picker.rs
+++ b/crates/ui-egui/src/file_picker.rs
@@ -473,4 +473,35 @@ mod tests {
let n = doc.canvas_plane(&vals, canvas).unwrap().normal();
assert!(n.z.abs() > 0.99, "the canvas lies on {n:?}, not on XY");
}
+
+ /// A canvas and a decal clicked onto a face of a design with many curved faces land on it.
+ /// The click sent only the point, and the engine's search of every face of every body for it
+ /// gave up ("face capture work limit exceeded"), as on an imported trophy of nine bodies.
+ #[test]
+ fn a_canvas_and_a_decal_clicked_onto_a_face_of_a_large_design_land_on_it() {
+ let mut h = crate::scenario::Harness::new();
+ h.app.run("solid.box", json!({"length": 200, "width": 120, "height": 40, "corner": [20, 20, 0]})).unwrap();
+ for i in 0..8 {
+ let center = [60 + 45 * (i % 4), 50 + 60 * (i / 4), 60];
+ h.app.run("solid.torus", json!({"major": 30, "minor": 8, "center": center, "operation": "new_body"})).unwrap();
+ }
+ for command in ["canvas.insert", "canvas.decal"] {
+ for step in [
+ json!({"make_image": "pic.png"}),
+ json!({"choose_file": "pic.png"}),
+ json!({"call": "ui.view", "params": {"view": "iso"}}),
+ json!({"start": command}),
+ json!({"click": {"world": [30, 30, 40]}}),
+ ] {
+ h.step(&step).unwrap();
+ }
+ }
+ let doc = &h.app.session.doc;
+ assert_eq!(doc.canvases.len(), 2, "both images are placed");
+ let (vals, _) = doc.param_values();
+ for canvas in &doc.canvases {
+ let plane = doc.canvas_plane(&vals, canvas).unwrap();
+ assert!(plane.normal().z.abs() > 0.99 && (plane.origin.z - 40.0).abs() < 1e-6, "{} lies on {plane:?}", canvas.name);
+ }
+ }
}
diff --git a/crates/ui-egui/src/sketch_tools.rs b/crates/ui-egui/src/sketch_tools.rs
index ecc73632ce..e9155c5c60 100644
--- a/crates/ui-egui/src/sketch_tools.rs
+++ b/crates/ui-egui/src/sketch_tools.rs
@@ -868,7 +868,15 @@ pub fn on_click(app: &mut SolveApp, tool: &mut Tool, proj: &Proj, pos: Pos2) {
let (_, ray) = proj.ray(pos);
let seen = |name: &str| crate::dialogs::plane_by_name(&app.session, name).is_some_and(|pl| pl.normal().dot(ray).abs() > 0.2 * ray.len());
let target = hits.iter().find_map(|h| match h {
- Hit::Face { point, .. } => Some((json!({"face": [point.x, point.y, point.z]}), *point)),
+ // The picked body and face, as Create Sketch sends them: a point alone makes the
+ // engine search every face of every body, which gives up on a large design.
+ Hit::Face { body, index, point } => {
+ let face = solidcraft_engine::Sel::Face { body: body.clone(), index: *index, point: *point };
+ Some((
+ crate::sketch_attachment_ui::face_plane(app, &face).unwrap_or_else(|| json!({"face": [point.x, point.y, point.z]})),
+ *point,
+ ))
+ }
Hit::Plane { name, point } if !decal && seen(name) => Some((json!(name), *point)),
_ => None,
});
@@ -886,7 +894,7 @@ pub fn on_click(app: &mut SolveApp, tool: &mut Tool, proj: &Proj, pos: Pos2) {
});
let Some((plane, at)) = target else { return };
let p = if decal {
- json!({"path": path, "face": plane.get("face").cloned().unwrap_or(Value::Null)})
+ json!({"path": path, "face": if plane.get("face").is_some() { plane } else { Value::Null }})
} else {
json!({"path": path, "plane": plane, "at": [at.x, at.y, at.z]})
};