diff --git a/crates/engine/src/cmd/sketch_project.rs b/crates/engine/src/cmd/sketch_project.rs index a7ece1c32..6120439d6 100644 --- a/crates/engine/src/cmd/sketch_project.rs +++ b/crates/engine/src/cmd/sketch_project.rs @@ -232,6 +232,20 @@ pub(super) mod refresh_count { /// Add a link for `src` to sketch `id` in `doc` (not stored in the session). Returns the link id. pub(super) fn add_link(s: &Session, doc: &Document, id: u64, sk: &mut Sketch, kind: LinkKind, src: LinkSource) -> Result { + add_link_unless_present(s, doc, id, sk, kind, src, false).map(|l| l.unwrap_or_default()) +} + +/// [`add_link`], or `None` (nothing added) when `skip_present` and everything `src` resolves to +/// is linked into the sketch already. +fn add_link_unless_present( + s: &Session, + doc: &Document, + id: u64, + sk: &mut Sketch, + kind: LinkKind, + src: LinkSource, + skip_present: bool, +) -> Result> { let doc = placed_candidate(s, doc); let (vals, _) = doc.param_values(); let st = s.model.state_before(id); @@ -239,6 +253,9 @@ pub(super) fn add_link(s: &Session, doc: &Document, id: u64, sk: &mut Sketch, ki let st = state_for_sketch(s, &doc, id, &st)?; let plane = sketch_plane(&doc, id, &st)?; let r = resolve(&doc, &vals, &st, &plane, kind, &src)?; + if skip_present && r.wires.is_empty() && sk.link_geometry_present(&r.geom) { + return Ok(None); + } let name = if let LinkSource::Face { body, at } = &r.source { st.body(body).and_then(|body| { if let Some(index) = r.face_index { @@ -257,7 +274,7 @@ pub(super) fn add_link(s: &Session, doc: &Document, id: u64, sk: &mut Sketch, ki l.face_name = name; l.edge_name = edge_name; } - Ok(link) + Ok(Some(link)) } fn link_json(sk: &Sketch, link: &str) -> Value { @@ -281,14 +298,27 @@ fn run_links(s: &mut Session, p: &Value, cmd: &str, kind: LinkKind) -> Result out.push(link_json(&sk, &l)), + Ok(None) => present.push(what), + Err(e) => return Err(bad(cmd, format!("{what}: {e}"))), + } + } + if out.is_empty() { + return Err(bad(cmd, format!("already projected into this sketch: {}", present.join(", ")))); } *doc.sketch_mut(id)? = sk; *s.doc_mut() = doc; - Ok(json!({"links": out})) + let mut r = json!({"links": out}); + if !present.is_empty() { + r["already_projected"] = json!(present); + } + Ok(r) } fn project_cmd(s: &mut Session, p: &Value) -> Result { diff --git a/crates/engine/src/cmd/sketch_project_tests.rs b/crates/engine/src/cmd/sketch_project_tests.rs index d277073ca..d136c4c9d 100644 --- a/crates/engine/src/cmd/sketch_project_tests.rs +++ b/crates/engine/src/cmd/sketch_project_tests.rs @@ -709,3 +709,31 @@ fn a_face_seen_edge_on_projects_as_its_silhouette_and_follows_edits() { let solidcraft_sketch::CurveKind::Line { a, b } = sk.curves[ci].kind else { panic!("not a line") }; assert!((sk.point(a).unwrap().x - 50.0).abs() < 1e-6 && (sk.point(b).unwrap().x - 50.0).abs() < 1e-6); } + +/// A sketch put on a face already shows the face's border. Projecting that face, or one of the +/// border's edges, again laid a second copy over it (extra points and lines on a rounded +/// corner); those are skipped now, and a pick of nothing new is refused, so it adds no step. +#[test] +fn projecting_what_the_face_sketch_already_shows_adds_no_second_copy() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length":40,"width":30,"height":10})); + run(&mut s, "solid.fillet", json!({"edges":[[0,0,5],[40,0,5],[40,30,5],[0,30,5]],"radius":5})); + run(&mut s, "sketch.create", json!({"plane":{"face":[20,15,10]}})); + let shown = |s: &mut Session| { + let si = inspect(s); + (si["curves"].as_array().unwrap().len(), si["points"].as_array().unwrap().len()) + }; + let border = shown(&mut s); + assert_eq!(border.0, 8, "four sides and four rounded corners"); + let corner = 5.0 - 5.0 * std::f64::consts::FRAC_1_SQRT_2; + for refs in [json!([{"face":[20,15,10]}]), json!([{"edge":[20,0,10]}]), json!([{"edge":[corner,corner,10]}])] { + let e = s.execute("sketch.project", &json!({"refs": refs})).unwrap_err().to_string(); + assert!(e.contains("already projected"), "{refs}: {e}"); + assert_eq!(shown(&mut s), border, "{refs}"); + } + // Something new still projects, and alongside it a reference already shown is skipped. + let r = run(&mut s, "sketch.project", json!({"refs":[{"edge":[20,0,10]},{"axis":"X"}]})); + assert_eq!(r["links"].as_array().unwrap().len(), 1, "{r}"); + assert_eq!(r["already_projected"].as_array().unwrap().len(), 1, "{r}"); + assert_eq!(shown(&mut s).0, border.0 + 1); +} diff --git a/crates/sketch/src/link.rs b/crates/sketch/src/link.rs index d2b9dc2d1..72414f93e 100644 --- a/crates/sketch/src/link.rs +++ b/crates/sketch/src/link.rs @@ -626,6 +626,29 @@ impl Sketch { curves.first().copied().map(Some).ok_or_else(|| SketchError::Invalid(format!("sketch {name} axis"))) } + /// Is all of `geom` already linked into this sketch: the border of the face the sketch was + /// put on, or an edge projected before? Linking it again would lay a second copy over the + /// first. Lines match with either end first, circles by centre and radius, arcs by centre + /// and ends; other curves, and points on their own (the origin, a vertex), never count as + /// present. + pub fn link_geometry_present(&self, geom: &[LinkGeom]) -> bool { + let Ok(lay) = layout(geom) else { return false }; + if lay.curves.is_empty() || !lay.lone.is_empty() { + return false; + } + let at = |i: usize| self.points.get(i).map(|p| p.pos); + let near = |i: usize, q: Option| lay.pts.get(i).zip(q).is_some_and(|(p, q)| p.dist(q) <= MERGE_TOL); + let curve_present = |c: &LCurve| { + self.curves.iter().filter(|k| k.link.is_some()).any(|k| match (*c, &k.kind) { + (LCurve::Line(a, b), CurveKind::Line { a: p, b: q }) => (near(a, at(*p)) && near(b, at(*q))) || (near(a, at(*q)) && near(b, at(*p))), + (LCurve::Circle(c, r), CurveKind::Circle { c: p, r: s }) => near(c, at(*p)) && (r - s).abs() <= MERGE_TOL, + (LCurve::Arc(c, a, b), CurveKind::Arc { c: p, a: q, b: e }) => near(c, at(*p)) && near(a, at(*q)) && near(b, at(*e)), + _ => false, + }) + }; + lay.curves.iter().all(curve_present) + } + /// Add linked geometry. Returns the link id; the new curve and point ids are /// [`Sketch::link_curves`] / [`Sketch::link_points`] of it. pub fn add_link(&mut self, kind: LinkKind, source: LinkSource, geom: &[LinkGeom]) -> Result { diff --git a/crates/ui-egui/tests/scenarios/s31_sketch_on_face_projects.json b/crates/ui-egui/tests/scenarios/s31_sketch_on_face_projects.json index 4dccefb43..b772840d0 100644 --- a/crates/ui-egui/tests/scenarios/s31_sketch_on_face_projects.json +++ b/crates/ui-egui/tests/scenarios/s31_sketch_on_face_projects.json @@ -96,7 +96,7 @@ { "expect": { "sketch_curves": { - "linked": 8, + "linked": 4, "own": 0 } }