Fix copying drawn 3D sketch curves (#10134) (#10146)

* claim: #10134

* fix: copy drawn 3D sketch curves (#10134)
This commit is contained in:
Colin Faulkingham
2026-10-31 16:53:47 +00:00
committed by GitHub
parent eaf3acc297
commit aa244bcee4
3 changed files with 189 additions and 7 deletions
@@ -439,3 +439,36 @@ fn line3d_spline3d_point3d_need_a_3d_sketch() {
assert!(info.enabled, "{id} not offered inside a 3D sketch: {:?}", info.disabled_reason);
}
}
#[test]
fn wire_frame_copies_keep_component_and_legacy_coordinates() {
let (mut s, _, first, second) = second_frame();
let sketch = s.active_sketch.unwrap();
let fit = [Vec3::new(20.0, 40.0, 45.0), Vec3::new(10.0, 30.0, 45.0), Vec3::new(20.0, 20.0, 45.0)];
let local = run(&mut s, "sketch.arc3d", json!({"start":fit[0],"through":fit[1],"end":fit[2]}))["curves"][0].as_str().unwrap().to_owned();
let legacy = s.doc_mut().sketch_mut(sketch).unwrap().add_drawn_arc_in(&fit, WireSpace::World).unwrap();
s.refresh();
let originals = s.doc.sketch(sketch).unwrap().wires.clone();
let out = run(&mut s, "sketch.move", json!({"entities":[local,legacy],"copy":true,"translate":[1.6,0],"angle":90}));
let copied: Vec<_> = out["curves"].as_array().unwrap().iter().map(|id| id.as_str().unwrap().to_owned()).collect();
assert_eq!(copied.len(), 2);
let sk = s.doc.sketch(sketch).unwrap();
assert_eq!(&sk.wires[..2], &originals);
assert_eq!(sk.wires[2].space, WireSpace::Component);
assert_eq!(sk.wires[3].space, WireSpace::World);
near(sk.wires[2].arc.unwrap().center, Vec3::new(1.6, 0.0, 5.0));
near(sk.wires[3].arc.unwrap().center, Vec3::new(20.0, 31.6, 45.0));
for wire in s.world_state().sketch(sketch).unwrap().sketch.wires.iter().filter(|w| copied.contains(&w.id)) {
near(wire.arc.unwrap().center, Vec3::new(20.0, 31.6, 45.0));
near(wire.fit[0], Vec3::new(10.0, 31.6, 45.0));
}
let component = s.active_component;
run(&mut s, "component.activate", json!({"component":component,"occurrence":first,"path":[first]}));
let world = s.world_state();
let wires = &world.sketch(sketch).unwrap().sketch.wires;
near(wires.iter().find(|w| w.id == copied[0]).unwrap().arc.unwrap().center, Vec3::new(1.6, 0.0, 5.0));
near(wires.iter().find(|w| w.id == copied[1]).unwrap().arc.unwrap().center, Vec3::new(20.0, 31.6, 45.0));
run(&mut s, "component.activate", json!({"component":component,"occurrence":second,"path":[second]}));
let restored = solidcraft_doc::format::read(&s.doc.to_json()).unwrap();
assert_eq!(restored.sketch(sketch).unwrap().wires, s.doc.sketch(sketch).unwrap().wires);
}
+44 -7
View File
@@ -5,8 +5,8 @@ use std::f64::consts::{FRAC_PI_2, PI, TAU};
use serde_json::{Value, json};
use solidcraft_doc::ProfileSel;
use solidcraft_geom::Vec2;
use solidcraft_sketch::{ConstraintKind, Curve, CurveKind, Shape, Sketch, intersections, intersections_within};
use solidcraft_geom::{Plane, Vec2};
use solidcraft_sketch::{ConstraintKind, Curve, CurveKind, Shape, Sketch, Wire, WireSpace, intersections, intersections_within};
use super::sketch::{add_c, edit, ids_of, line_or_axis_ref, solve_current};
use super::sketch_store;
@@ -92,7 +92,7 @@ pub static COMMANDS: &[CommandSpec] = &[
.at("SKETCH", "MODIFY")
.icon("sketch_move")
.enabled(in_sketch)
.params(concat!("entities: [ids], translate?: [dx,dy], angle?: deg, center?: [x,y], copy?: bool, rotate_constraints?: bool (default true; false keeps constraints unchanged); a rotation turns the constraints tied to the sketch axes when all their geometry moves (horizontal and vertical relations and dimensions swap at odd quarter turns and stay at half turns, line angles turn with the line) and removes the horizontal and vertical ones that cannot hold at other angles; picking a text outline or linked point transforms its whole text source once, and copies remain editable text", created_doc!())),
.params(concat!("entities: [ids], translate?: [dx,dy], angle?: deg, center?: [x,y], copy?: bool, rotate_constraints?: bool (default true; false keeps constraints unchanged); a rotation turns the constraints tied to the sketch axes when all their geometry moves (horizontal and vertical relations and dimensions swap at odd quarter turns and stay at half turns, line angles turn with the line) and removes the horizontal and vertical ones that cannot hold at other angles; picking a text outline or linked point transforms its whole text source once, and copies remain editable text; copy also accepts drawn 3D curves, preserving exact arcs and height above the sketch plane", created_doc!())),
CommandSpec::new("sketch.mirror", "Mirror", mirror)
.at("SKETCH", "PATTERN")
.icon("sketch_mirror")
@@ -3035,20 +3035,52 @@ fn release_origin_holds(sk: &mut Sketch, pts: &[usize]) {
);
}
/// Separate drawn wires from planar entities. Their fit points use either the owner's local
/// frame or the legacy world frame, so each must turn about the sketch plane in that frame.
fn drawn_copy_selection(s: &Session, p: &Value, ids: Vec<String>) -> Result<(Vec<String>, Vec<(Wire, Plane)>)> {
let cmd = "sketch.move";
let id = sketch_store::target(s, p, cmd)?;
let sk = sketch_store::read(&s.doc, id)?;
let mut planar = Vec::new();
let mut drawn: Vec<(Wire, Plane)> = Vec::new();
for entity in ids {
let Some(wire) = sk.wires.iter().find(|w| w.id == entity) else {
planar.push(entity);
continue;
};
if wire.link.is_some() || wire.fit.is_empty() {
return Err(bad(cmd, "Copy supports drawn 3D curves; reference wires cannot be copied"));
}
if drawn.iter().any(|(w, _)| w.id == entity) {
continue;
}
let plane = match wire.space {
WireSpace::Component => s.model.state().sketch(id).map(|sk| sk.plane),
WireSpace::World => s.world_state().sketch(id).map(|sk| sk.plane),
}
.ok_or_else(|| bad(cmd, "sketch plane is unavailable"))?;
drawn.push((wire.clone(), plane));
}
Ok((planar, drawn))
}
fn move_copy(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "sketch.move";
let t = if p.get("translate").is_some() { req_vec2(cmd, p, "translate")? } else { Vec2::ZERO };
let ang = if p.get("angle").is_some() { num(p, "angle").ok_or_else(|| bad(cmd, "`angle` must be finite"))? } else { 0.0 }.to_radians();
let copy = bool_(p, "copy").unwrap_or(false);
let rotate_constraints = bool_(p, "rotate_constraints").unwrap_or(true);
let selection = string_list(cmd, p, "entities")?;
let (planar, drawn) = if copy { drawn_copy_selection(s, p, selection)? } else { (selection, Vec::new()) };
let (out, info) = edit(s, p, cmd, false, |sk, doc| {
let (mut cs, mut ps) = entities(sk, p, cmd)?;
let (mut cs, mut ps) =
if planar.is_empty() && !drawn.is_empty() { (Vec::new(), Vec::new()) } else { entities(sk, &json!({"entities": planar}), cmd)? };
let c = if p.get("center").is_some() { point_or_ref(sk, p, "center", cmd)? } else { Vec2::ZERO };
let rot = rotate(c, ang);
let f = move |q: Vec2| rot(q) + t;
let mut links = Vec::new();
for entity in string_list(cmd, p, "entities")? {
if let Some(link) = super::sketch_create::text_link(sk, &entity)
for entity in &planar {
if let Some(link) = super::sketch_create::text_link(sk, entity)
&& !links.iter().any(|old| old == link)
{
links.push(link.to_owned());
@@ -3063,7 +3095,12 @@ fn move_copy(s: &mut Session, p: &Value) -> Result<Value> {
if copy {
let (mut m, _) = copy_through(sk, &cs, &ps, &f, false, &|_| false, 1.0)?;
m.extend(text_curves);
return Ok(ids_of(sk, &m));
let mut ids = ids_of(sk, &m);
for (wire, plane) in &drawn {
let fit: Vec<_> = wire.fit.iter().map(|&q| plane.to_world(f(plane.to_local(q))) + plane.normal() * plane.height(q)).collect();
ids.push(if wire.arc.is_some() { sk.add_drawn_arc_in(&fit, wire.space)? } else { sk.add_drawn_wire_in(&fit, wire.space)? });
}
return Ok(ids);
}
transform_in_place(sk, &cs, &ps, &f, 1.0, cmd)?;
if rotate_constraints {
@@ -3984,3 +3984,115 @@ fn offset_corners_trim_the_overlap_and_extend_the_gap() {
let (_, _, _, end) = line_ends(&sk, &sk.offsets[0].outputs[0]);
assert!(end.dist(Vec2::new(23.5, -3.0)) < 1e-4, "coincident corner extended to {end:?}");
}
#[test]
fn move_copy_drawn_arc_keeps_exact_geometry_and_original() {
use solidcraft_geom::Vec3;
let mut s = new_sketch();
run(&mut s, "sketch.options", json!({"sketch_3d":true}));
let source = ids(&run(&mut s, "sketch.arc3d", json!({"start":[10,0,3],"through":[0,10,3],"end":[-10,0,3]}))["curves"])[0].clone();
let id = s.active_sketch.unwrap();
let original = s.doc.sketch(id).unwrap().wires[0].clone();
let before = s.doc.to_json();
let out = run(&mut s, "sketch.move", json!({"entities":[source],"translate":[1.6,0],"copy":true}));
let copied = ids(&out["curves"]);
assert_eq!(copied.len(), 1);
assert_ne!(copied[0], source);
let sk = s.doc.sketch(id).unwrap();
assert_eq!(sk.wires.len(), 2);
assert_eq!(sk.wires[0], original);
let wire = &sk.wires[1];
assert_eq!(wire.id, copied[0]);
assert_eq!(wire.space, original.space);
assert_eq!(wire.link, None);
let arc = wire.arc.unwrap();
assert!(arc.center.dist(Vec3::new(1.6, 0.0, 3.0)) < 1e-9);
assert!(close(arc.radius, 10.0));
assert!(close(solidcraft_sketch::arc_sweep(&arc), std::f64::consts::PI));
for (p, q) in wire.fit.iter().zip(&original.fit) {
assert!(p.dist(*q + Vec3::new(1.6, 0.0, 0.0)) < 1e-9);
}
let after = s.doc.to_json();
run(&mut s, "edit.undo", json!({}));
assert_eq!(s.doc.to_json(), before);
run(&mut s, "edit.redo", json!({}));
assert_eq!(s.doc.to_json(), after);
let reopened = solidcraft_doc::format::read(&after).unwrap();
assert_eq!(reopened.sketch(id).unwrap().wires, s.doc.sketch(id).unwrap().wires);
}
#[test]
fn move_copy_drawn_arc_turns_in_the_sketch_plane_without_flattening() {
use solidcraft_geom::Vec3;
let mut s = Session::default();
run(&mut s, "sketch.create", json!({"plane":"YZ"}));
run(&mut s, "sketch.options", json!({"sketch_3d":true}));
let source = ids(&run(&mut s, "sketch.arc3d", json!({"start":[5,12,3],"through":[5,2,13],"end":[5,-8,3]}))["curves"])[0].clone();
run(&mut s, "sketch.move", json!({"entities":[source],"translate":[4,-1],"angle":90,"center":[2,3],"copy":true}));
let sk = s.doc.sketch(s.active_sketch.unwrap()).unwrap();
let copy = &sk.wires[1];
for (p, expected) in copy.fit.iter().zip([Vec3::new(5.0, 6.0, 12.0), Vec3::new(5.0, -4.0, 2.0), Vec3::new(5.0, 6.0, -8.0)]) {
assert!(p.dist(expected) < 1e-9, "{p:?} != {expected:?}");
}
let arc = copy.arc.unwrap();
assert!(arc.center.dist(Vec3::new(5.0, 6.0, 2.0)) < 1e-9);
assert!(arc.normal.dist(Vec3::X) < 1e-9);
assert!(close(arc.radius, 10.0));
}
#[test]
fn move_copy_drawn_wires_mix_with_planar_curves_and_deduplicate() {
use solidcraft_geom::Vec3;
let mut s = new_sketch();
run(&mut s, "sketch.options", json!({"sketch_3d":true}));
let curve = ids(&run(&mut s, "sketch.line", json!({"points":[[1,2],[4,2]]}))["curves"])[0].clone();
let wire = ids(&run(&mut s, "sketch.spline3d", json!({"points":[[1,2,3],[4,6,8],[10,8,9]]}))["curves"])[0].clone();
let point = run(&mut s, "sketch.point3d", json!({"point":[2,3,4]}))["point"].as_str().unwrap().to_owned();
let before = sketch(&s);
let out = run(&mut s, "sketch.move", json!({"entities":[wire,curve,point,wire],"translate":[3,-2],"copy":true}));
assert_eq!(ids(&out["curves"]).len(), 3);
let after = sketch(&s);
assert_eq!(after.curves.len(), before.curves.len() + 1);
assert_eq!(after.wires.len(), before.wires.len() + 2);
for (original, copy) in before.wires.iter().zip(after.wires.iter().skip(2)) {
assert_eq!(after.wires.iter().find(|w| w.id == original.id).unwrap(), original);
assert_eq!(copy.space, original.space);
assert_eq!(copy.arc, None);
for (p, q) in copy.fit.iter().zip(&original.fit) {
assert!(p.dist(*q + Vec3::new(3.0, -2.0, 0.0)) < 1e-9);
}
}
let copied = &after.curves[1];
let CurveKind::Line { a, b } = copied.kind else { panic!("expected a copied line") };
assert!(after.point(a).unwrap().dist(Vec2::new(4.0, 0.0)) < 1e-9);
assert!(after.point(b).unwrap().dist(Vec2::new(7.0, 0.0)) < 1e-9);
}
#[test]
fn move_copy_drawn_wire_failures_leave_the_design_and_history_unchanged() {
let mut s = new_sketch();
run(&mut s, "sketch.options", json!({"sketch_3d":true}));
let wire = ids(&run(&mut s, "sketch.arc3d", json!({"start":[10,0,0],"through":[0,10,0],"end":[-10,0,0]}))["curves"])[0].clone();
let original = s.doc.to_json();
let history = (s.undo.len(), s.redo.len());
for params in [
json!({"entities":[wire],"copy":true,"translate":[1e9,0]}),
json!({"entities":[wire,"missing"],"copy":true,"translate":[1,0]}),
json!({"entities":[],"copy":true}),
] {
assert!(s.execute("sketch.move", &params).is_err());
assert_eq!(s.doc.to_json(), original);
assert_eq!((s.undo.len(), s.redo.len()), history);
}
// An unlinked reference wire has no editable fit points: Copy must not convert its
// samples to a fitted spline and silently change the curve's shape.
let id = s.active_sketch.unwrap();
s.doc_mut().sketch_mut(id).unwrap().wires[0].fit.clear();
s.doc_mut().sketch_mut(id).unwrap().wires[0].arc = None;
s.refresh();
let original = s.doc.to_json();
let error = s.execute("sketch.move", &json!({"entities":[wire],"copy":true})).unwrap_err().to_string();
assert!(error.contains("drawn 3D"), "{error}");
assert_eq!(s.doc.to_json(), original);
assert_eq!((s.undo.len(), s.redo.len()), history);
}