Project: don't lay a second copy over geometry the sketch already shows (#4929)
* Project: don't lay a second copy over geometry the sketch already shows A sketch on a face already holds the face's border, linked. Projecting that face again, or one of the border's edges, added a second set of curves and points over the first: on a rounded corner that shows as extra points and lines. Project now skips a reference whose curves are all linked into the sketch already (lines, circles, arcs), reports it as `already_projected`, and refuses a pick of nothing new so it adds no undo step. Points on their own (the origin, a vertex) still project. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Projecting the face a sketch already shows adds no second copy in the face scenario Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
This commit is contained in:
@@ -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<String> {
|
||||
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<Option<String>> {
|
||||
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<Va
|
||||
let mut sk = doc.sketch(id)?.clone();
|
||||
refresh(s, &doc, id, &mut sk);
|
||||
let mut out = Vec::new();
|
||||
// Projecting what the sketch already shows (the border of the face it is on, an edge
|
||||
// projected before) would lay a second copy over the first: those references are skipped.
|
||||
let mut present = Vec::new();
|
||||
for src in sources {
|
||||
let what = src.describe();
|
||||
let l = add_link(s, &doc, id, &mut sk, kind, src).map_err(|e| bad(cmd, format!("{what}: {e}")))?;
|
||||
out.push(link_json(&sk, &l));
|
||||
match add_link_unless_present(s, &doc, id, &mut sk, kind, src, kind == LinkKind::Project) {
|
||||
Ok(Some(l)) => 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<Value> {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<Vec2>| 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<String> {
|
||||
|
||||
@@ -96,7 +96,7 @@
|
||||
{
|
||||
"expect": {
|
||||
"sketch_curves": {
|
||||
"linked": 8,
|
||||
"linked": 4,
|
||||
"own": 0
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user