This commit is contained in:
@@ -45,8 +45,23 @@ fn typed_line_angle_survives_quadrants_move_reopen_and_one_undo() {
|
||||
let mut reopened = Session { doc: std::sync::Arc::new(doc), active_sketch: s.active_sketch, ..Session::default() };
|
||||
reopened.refresh();
|
||||
close(measure(&reopened, |k| matches!(k, ConstraintKind::LineAngle { .. })), 90f64.to_radians());
|
||||
run(&mut reopened, "sketch.move", json!({"entities":out["curves"],"translate":[4,3],"angle":20}));
|
||||
close(measure(&reopened, |k| matches!(k, ConstraintKind::LineAngle { .. })), 90f64.to_radians());
|
||||
let before_move = reopened.doc.clone();
|
||||
for rotate_constraints in [false, true] {
|
||||
reopened.doc = before_move.clone();
|
||||
reopened.refresh();
|
||||
let result =
|
||||
run(&mut reopened, "sketch.move", json!({"entities":out["curves"],"translate":[4,3],"angle":20,"rotate_constraints":rotate_constraints}));
|
||||
assert_eq!(result["sketch"]["solved"], true);
|
||||
let expected = if rotate_constraints { 110.0_f64 } else { 90.0 };
|
||||
close(measure(&reopened, |k| matches!(k, ConstraintKind::LineAngle { .. })), expected.to_radians());
|
||||
close(measure(&reopened, |k| matches!(k, ConstraintKind::Length { .. })), 10.0);
|
||||
assert_eq!(reopened.doc.param("heading").unwrap().expr, "90 deg");
|
||||
let sk = sketch(&reopened);
|
||||
let c = sk.constraints.iter().find(|c| matches!(c.kind, ConstraintKind::LineAngle { .. })).unwrap();
|
||||
assert_eq!(reopened.doc.param(c.param.as_deref().unwrap()).unwrap().expr, if rotate_constraints { "(heading) + 20 deg" } else { "heading" });
|
||||
run(&mut reopened, "edit.undo", json!({}));
|
||||
close(measure(&reopened, |k| matches!(k, ConstraintKind::LineAngle { .. })), 90f64.to_radians());
|
||||
}
|
||||
let mut atomic = session();
|
||||
draw(&mut atomic, "sketch.line", json!({"points":[[0,0],[5,5]]}), json!({"length":10,"angle":45}));
|
||||
run(&mut atomic, "edit.undo", json!({}));
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Sketch modify tools: trim, extend, break, fillet, chamfer, offset, mirror, circular and
|
||||
//! rectangular patterns, move/copy and scale.
|
||||
|
||||
use std::f64::consts::TAU;
|
||||
use std::f64::consts::{FRAC_PI_2, PI, TAU};
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use solidcraft_doc::ProfileSel;
|
||||
@@ -85,7 +85,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; 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", created_doc!())),
|
||||
CommandSpec::new("sketch.mirror", "Mirror", mirror)
|
||||
.at("SKETCH", "PATTERN")
|
||||
.icon("sketch_mirror")
|
||||
@@ -2572,12 +2572,79 @@ fn transform_in_place(sk: &mut Sketch, cs: &[usize], ps: &[usize], f: &dyn Fn(Ve
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Turn the constraints tied to the sketch axes with geometry rotated by `angle` (radians), when
|
||||
/// every point they refer to is in `moved`: horizontal and vertical relations and dimensions swap
|
||||
/// at odd quarter turns and stay at half turns, and a line's angle dimension turns with the line,
|
||||
/// its parameter too. Horizontal and vertical relations and dimensions cannot hold at any other
|
||||
/// angle and are removed. Every other constraint keeps its meaning under a rigid move.
|
||||
fn rotate_axis_constraints(sk: &mut Sketch, doc: &mut solidcraft_doc::Document, moved: &[usize], angle: f64) -> Result<()> {
|
||||
let quarters = angle.rem_euclid(TAU) / FRAC_PI_2;
|
||||
let turns = quarters.round();
|
||||
if turns.rem_euclid(4.0) == 0.0 && (quarters - turns).abs() < 1e-10 {
|
||||
return Ok(());
|
||||
}
|
||||
let square = (quarters - turns).abs() < 1e-10;
|
||||
let odd = square && turns.rem_euclid(2.0) == 1.0;
|
||||
let curves = &sk.curves;
|
||||
let mut angles = Vec::new();
|
||||
sk.constraints.retain_mut(|c| {
|
||||
if c.driven {
|
||||
return true;
|
||||
}
|
||||
let (mut points, refs) = c.kind.indices();
|
||||
for l in refs {
|
||||
match curves.get(l) {
|
||||
Some(curve) => points.extend(curve.kind.point_ids()),
|
||||
None => return true,
|
||||
}
|
||||
}
|
||||
if points.is_empty() || !points.iter().all(|q| moved.contains(q)) {
|
||||
return true;
|
||||
}
|
||||
use ConstraintKind::*;
|
||||
let swapped = match c.kind {
|
||||
Horizontal { l } => Vertical { l },
|
||||
Vertical { l } => Horizontal { l },
|
||||
HorizontalPoints { p, q } => VerticalPoints { p, q },
|
||||
VerticalPoints { p, q } => HorizontalPoints { p, q },
|
||||
DistanceX { p, q, value } => DistanceY { p, q, value },
|
||||
DistanceY { p, q, value } => DistanceX { p, q, value },
|
||||
LineAngle { l, value } => {
|
||||
let turned = (value + angle + PI).rem_euclid(TAU) - PI;
|
||||
c.kind = LineAngle { l, value: turned };
|
||||
if let Some(name) = &c.param {
|
||||
angles.push((name.clone(), turned));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
_ => return true,
|
||||
};
|
||||
if odd {
|
||||
c.kind = swapped;
|
||||
}
|
||||
square
|
||||
});
|
||||
for (name, turned) in angles {
|
||||
let Some(param) = doc.param(&name) else { continue };
|
||||
let degrees = (angle.to_degrees() * 1e6).round() / 1e6;
|
||||
// A literal becomes the new angle; a formula keeps its references and gains the turn.
|
||||
let expr = if solidcraft_doc::expr::references(¶m.expr).is_empty() {
|
||||
format!("{} deg", (turned.to_degrees() * 1e6).round() / 1e6)
|
||||
} else {
|
||||
format!("({}) + {degrees} deg", param.expr)
|
||||
};
|
||||
doc.set_param(&name, &expr, None, None)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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 (out, info) = edit(s, p, cmd, false, |sk, _| {
|
||||
let rotate_constraints = bool_(p, "rotate_constraints").unwrap_or(true);
|
||||
let (out, info) = edit(s, p, cmd, false, |sk, doc| {
|
||||
let (mut cs, mut ps) = entities(sk, p, cmd)?;
|
||||
let c = if p.get("center").is_some() { point_or_ref(sk, p, "center", cmd)? } else { Vec2::ZERO };
|
||||
let rot = rotate(c, ang);
|
||||
@@ -2602,9 +2669,14 @@ fn move_copy(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Ok(ids_of(sk, &m));
|
||||
}
|
||||
transform_in_place(sk, &cs, &ps, &f, 1.0, cmd)?;
|
||||
if rotate_constraints {
|
||||
let moved = involved_points(sk, &cs, &ps);
|
||||
rotate_axis_constraints(sk, doc, &moved, ang)?;
|
||||
}
|
||||
cs.extend(text_curves);
|
||||
Ok(ids_of(sk, &cs))
|
||||
})?;
|
||||
s.doc_mut().prune_model_params();
|
||||
Ok(json!({"curves": out, "sketch": info}))
|
||||
}
|
||||
|
||||
|
||||
@@ -516,6 +516,102 @@ fn offset_chain_mirror_patterns_move_scale() {
|
||||
assert!(sk.center(sk.curve_index(&cp).unwrap()).unwrap().dist(Vec2::new(-110.0, 0.0)) < 1e-7);
|
||||
}
|
||||
|
||||
/// #4645: rotating a constrained 40 × 10 rectangle turns it at its size. Horizontal and vertical
|
||||
/// relations and dimensions swap at odd quarter turns, stay at half turns and go at other angles;
|
||||
/// lengths stay. A rotated copy is unconstrained and leaves the original as it was.
|
||||
#[test]
|
||||
fn move_rotation_turns_axis_constraints() {
|
||||
for (angle, swap) in [(90.0, Some(true)), (-90.0, Some(true)), (450.0, Some(true)), (180.0, Some(false)), (30.0, None)] {
|
||||
for dims in ["none", "length", "horizontal"] {
|
||||
for copy in [false, true] {
|
||||
let at = format!("angle={angle}, dims={dims}, copy={copy}");
|
||||
let mut s = new_sketch();
|
||||
let r = run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [40, 10]}));
|
||||
let source = ids(&r["curves"]);
|
||||
if dims != "none" {
|
||||
for (i, value, ty) in [(0, 40, dims), (1, 10, if dims == "length" { "length" } else { "vertical" })] {
|
||||
run(&mut s, "sketch.dimension", json!({"entities": [source[i]], "value": value, "type": ty}));
|
||||
}
|
||||
}
|
||||
let before = sketch(&s);
|
||||
let out = run(&mut s, "sketch.move", json!({"entities": source, "angle": angle, "center": [0, 0], "copy": copy}));
|
||||
assert_eq!(out["sketch"]["solved"], true, "{at}: {out}");
|
||||
let after = sketch(&s);
|
||||
let (sin, cos) = f64::to_radians(angle).sin_cos();
|
||||
for (i, id) in ids(&out["curves"]).iter().enumerate() {
|
||||
let new = after.curve_index(id).unwrap();
|
||||
let old = before.curves[before.curve_index(&source[i]).unwrap()].kind.point_ids();
|
||||
for (p, q) in old.iter().zip(&after.curves[new].kind.point_ids()) {
|
||||
let p = before.point(*p).unwrap();
|
||||
let expected = Vec2::new(p.x * cos - p.y * sin, p.x * sin + p.y * cos);
|
||||
assert!(after.point(*q).unwrap().dist(expected) < 1e-6, "{at}: {after:?}");
|
||||
}
|
||||
let horizontal_source = i % 2 == 0;
|
||||
let axes: Vec<bool> = after
|
||||
.constraints
|
||||
.iter()
|
||||
.filter_map(|c| match c.kind {
|
||||
ConstraintKind::Horizontal { l } if l == new => Some(true),
|
||||
ConstraintKind::Vertical { l } if l == new => Some(false),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let expected = match (copy, swap) {
|
||||
(false, Some(swap)) => vec![horizontal_source ^ swap],
|
||||
_ => vec![],
|
||||
};
|
||||
assert_eq!(axes, expected, "{at}: line {i}");
|
||||
}
|
||||
let measured: Vec<(&str, f64)> = after.constraints[if copy { before.constraints.len() } else { 0 }..]
|
||||
.iter()
|
||||
.filter_map(|c| match c.kind {
|
||||
ConstraintKind::Length { value, .. } => Some(("length", value)),
|
||||
ConstraintKind::DistanceX { value, .. } => Some(("x", value)),
|
||||
ConstraintKind::DistanceY { value, .. } => Some(("y", value)),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let expected: Vec<(&str, f64)> = match (copy, dims, swap) {
|
||||
(true, ..) | (_, "none", _) => vec![],
|
||||
(_, "length", _) => vec![("length", 40.0), ("length", 10.0)],
|
||||
(_, _, Some(true)) => vec![("y", 40.0), ("x", 10.0)],
|
||||
(_, _, Some(false)) => vec![("x", 40.0), ("y", 10.0)],
|
||||
(_, _, None) => vec![],
|
||||
};
|
||||
assert_eq!(measured, expected, "{at}");
|
||||
if copy {
|
||||
assert_eq!(&after.points[..before.points.len()], before.points.as_slice(), "{at}");
|
||||
assert_eq!(&after.constraints[..before.constraints.len()], before.constraints.as_slice(), "{at}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// #4645: a line's angle dimension turns with the line, and so does its parameter.
|
||||
#[test]
|
||||
fn move_rotation_turns_line_angle_dimension() {
|
||||
let mut s = new_sketch();
|
||||
let r = run(&mut s, "sketch.line", json!({"points": [[0, 0], [20, 0]], "connect": false}));
|
||||
let line = ids(&r["curves"]).remove(0);
|
||||
run(&mut s, "sketch.dimension", json!({"entities": [line], "type": "angle", "value": 30}));
|
||||
let length = {
|
||||
let sk = sketch(&s);
|
||||
let ends = sk.curves[sk.curve_index(&line).unwrap()].kind.point_ids();
|
||||
sk.point(ends[0]).unwrap().dist(sk.point(ends[1]).unwrap())
|
||||
};
|
||||
let out = run(&mut s, "sketch.move", json!({"entities": [line], "angle": 90, "center": [0, 0]}));
|
||||
assert_eq!(out["sketch"]["solved"], true, "{out}");
|
||||
let sk = sketch(&s);
|
||||
let c = sk.constraints.iter().find(|c| matches!(c.kind, ConstraintKind::LineAngle { .. })).unwrap();
|
||||
let value = if let ConstraintKind::LineAngle { value, .. } = c.kind { value } else { f64::NAN };
|
||||
assert!(close(value, 120f64.to_radians()), "{value}");
|
||||
assert_eq!(s.doc.param(c.param.as_deref().unwrap()).unwrap().expr, "120 deg");
|
||||
let ends = sk.curves[sk.curve_index(&line).unwrap()].kind.point_ids();
|
||||
let d = sk.point(ends[1]).unwrap() - sk.point(ends[0]).unwrap();
|
||||
assert!(close(d.len(), length) && close(d.angle(), 120f64.to_radians()), "{d:?}");
|
||||
}
|
||||
|
||||
/// #1485: after a circle is trimmed between two construction lines, clicking the trimmed end
|
||||
/// picks the construction line's end lying there, not the arc's own; the tangent arc still starts
|
||||
/// from the trimmed arc, tangent to it.
|
||||
|
||||
Reference in New Issue
Block a user