Files
raykholo 0c429fa389 Plane at an angle about a construction axis now follows the axis when the model changes (#16534)
* Plane at Angle: a plane turned about a construction axis by name follows the axis (#16527)

Construct Plane at Angle given a construction axis by name saved that axis's position and
direction as fixed numbers, so the plane stayed put when the axis moved with the model edge
it is defined on. It now saves a reference to the axis and finds it again on every rebuild.
A design saved with only the fixed numbers opens and behaves as before. The command's
parameter text and schema no longer say "where it is now".

Claude-Session: https://claude.ai/code/session_01W3cp86hFEo34XPitHzNx7s

* Plane at Angle: review fixes for the named-axis tests (#16527)

The tests now turn the top face, which contains the axis, about the axis, so the plane
containing the moved axis is the right expectation. The older-design test checks the
plane misses the moved rim instead of expecting it to stay through the old one, and its
edit of the saved design no longer borrows the feature twice. The axis lookup returns
the construction axis as it is, leaving normalizing to the plane's own resolve.

* Plane at Angle tests: import Plane
2026-11-14 13:52:57 +00:00

273 lines
13 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! An axis on an edge or round face of the model (`{edge|face: [x,y,z]}`): Revolve, Surface
//! Revolve, Circular Pattern and Plane at Angle turn about a circular edge's or a cylinder's
//! axis, and follow it when a parameter moves the cylinder.
use solidcraft_geom::prelude::*;
use std::f64::consts::PI;
use serde_json::{Value, json};
use solidcore_record::{Plane, Vec3};
use crate::Session;
use crate::view::construction_planes;
fn run(s: &mut Session, id: &str, p: Value) -> Value {
match s.execute(id, &p) {
Ok(v) => v,
Err(e) => panic!("{id} {p}: {e}"),
}
}
fn volume(s: &mut Session) -> f64 {
run(s, "inspect.measure", json!({}))["total"]["volume_mm3"].as_f64().unwrap_or(f64::NAN)
}
fn rel(a: f64, b: f64) -> f64 {
(a - b).abs() / b.abs().max(1e-12)
}
/// A Ø10 × 10 shaft standing on XY at x = `cx` (a parameter, 40 mm).
fn shaft() -> Session {
let mut s = Session::default();
run(&mut s, "parameters.add", json!({"name": "cx", "expression": "40 mm"}));
run(&mut s, "solid.cylinder", json!({"base": ["cx", 0, 0], "radius": 5, "height": 10, "body_name": "Shaft"}));
s
}
const SHAFT: f64 = PI * 25.0 * 10.0;
/// A 2 × 6 rectangle on XZ, x 46…48, z 2…8: 6 to 8 mm from the shaft's axis.
fn ring_profile(s: &mut Session) {
run(s, "sketch.create", json!({"plane": "XZ", "name": "Profile"}));
run(s, "sketch.rectangle.two_point", json!({"p0": [46, -2], "p1": [48, -8]}));
run(s, "sketch.finish", json!({}));
}
/// Pappus: a 2 × 6 section whose centre is `r` from the axis, turned a full turn.
fn ring(r: f64) -> f64 {
2.0 * PI * r * 12.0
}
#[test]
fn revolve_about_a_circular_edge_follows_the_cylinder() {
let mut s = shaft();
ring_profile(&mut s);
// The shaft's top rim.
run(&mut s, "solid.revolve", json!({"sketch": "Profile", "axis": {"edge": [45, 0, 10]}, "operation": "new", "name": "Turn"}));
assert!(rel(volume(&mut s), SHAFT + ring(7.0)) < 1e-4, "turned about the shaft's axis: {}", volume(&mut s));
run(&mut s, "parameters.change", json!({"name": "cx", "expression": "40.5 mm"}));
assert!(rel(volume(&mut s), SHAFT + ring(6.5)) < 1e-4, "follows the shaft: {}", volume(&mut s));
// Saved and opened again it still follows.
let saved = s.doc.to_json();
let mut reopened = Session::new(solidcraft_doc::Document::from_json(&saved).unwrap());
assert!(rel(volume(&mut reopened), volume(&mut s)) < 1e-9);
run(&mut reopened, "parameters.change", json!({"name": "cx", "expression": "40 mm"}));
assert!(rel(volume(&mut reopened), SHAFT + ring(7.0)) < 1e-4);
}
#[test]
fn revolve_about_a_cylinder_wall() {
let mut s = shaft();
ring_profile(&mut s);
run(&mut s, "solid.revolve", json!({"sketch": "Profile", "axis": {"face": [45, 0, 5]}, "operation": "new"}));
assert!(rel(volume(&mut s), SHAFT + ring(7.0)) < 1e-4, "{}", volume(&mut s));
let Some(solidcraft_doc::Feature { kind: solidcraft_doc::FeatureKind::Revolve { axis, .. }, .. }) = s.doc.features.last() else {
panic!("no revolve")
};
assert!(matches!(axis, solidcraft_doc::AxisRef::Model { .. }), "kept as a reference: {axis:?}");
}
#[test]
fn surface_revolve_about_a_circular_edge() {
let mut s = shaft();
ring_profile(&mut s);
run(&mut s, "surface.revolve", json!({"sketch": "Profile", "axis": {"edge": [45, 0, 0]}, "angle": "180 deg"}));
run(&mut s, "parameters.change", json!({"name": "cx", "expression": "41 mm"}));
let st = s.world_state().unwrap();
// A half turn about x = 41: the far side of the profile ends up at x = 41 − 7 ± 1.
let b = st.bodies.last().map(|b| b.mesh().bounds()).unwrap();
assert!((b.min.x - (41.0 - 7.0)).abs() < 1e-3, "{b:?}");
assert!((b.max.x - 48.0).abs() < 1e-3, "{b:?}");
}
#[test]
fn circular_pattern_about_a_boss_wall_follows_the_boss() {
let mut s = Session::default();
run(&mut s, "parameters.add", json!({"name": "cx", "expression": "100 mm"}));
run(&mut s, "solid.box", json!({"length": 200, "width": 200, "height": 5, "corner": [0, 0, 0], "body_name": "Plate"}));
run(&mut s, "solid.cylinder", json!({"base": ["cx", 100, 0], "radius": 10, "height": 20, "operation": "join", "targets": ["Plate"]}));
run(&mut s, "solid.hole", json!({"position": [130, 100, 5], "diameter": 2, "name": "Hole"}));
run(&mut s, "solid.pattern.circular", json!({"features": ["Hole"], "axis": {"face": [110, 100, 10]}, "count": 4, "name": "Ring"}));
let holes = |s: &Session, cx: f64, r: f64| {
let st = s.world_state().unwrap();
let body = &st.bodies[0].body;
[(r, 0.0), (0.0, r), (-r, 0.0), (0.0, -r)]
.iter()
.all(|(dx, dy)| body.classify_point(Vec3::new(cx + dx, 100.0 + dy, 2.5)).unwrap() == solidcraft_kernel::PointClass::Outside)
};
assert!(holes(&s, 100.0, 30.0), "four holes round the boss");
run(&mut s, "parameters.change", json!({"name": "cx", "expression": "90 mm"}));
assert!(holes(&s, 90.0, 40.0), "the copies turn about the boss where it is now");
let st = s.world_state().unwrap();
assert_eq!(
st.bodies[0].body.classify_point(Vec3::new(100.0, 130.0, 2.5)).unwrap(),
solidcraft_kernel::PointClass::Inside,
"not where the boss was"
);
}
#[test]
fn plane_at_angle_about_a_cylinder_axis_follows_it() {
let mut s = shaft();
run(&mut s, "construct.plane.at_angle", json!({"base": "XZ", "axis": {"face": [45, 0, 5]}, "angle": "90 deg", "name": "Square"}));
let plane = |s: &Session| construction_planes(s).into_iter().find(|p| p.1 == "Square").map(|p| p.2).unwrap();
let pl = plane(&s);
assert!(pl.normal().cross(Vec3::X).len() < 1e-9, "turned square to XZ about Z: {pl:?}");
assert!(pl.height(Vec3::new(40.0, 0.0, 0.0)).abs() < 1e-6, "through the shaft's axis: {pl:?}");
run(&mut s, "parameters.change", json!({"name": "cx", "expression": "25 mm"}));
assert!(plane(&s).height(Vec3::new(25.0, 0.0, 0.0)).abs() < 1e-6, "follows the shaft");
}
#[test]
fn plane_at_angle_on_a_face_about_an_edge_follows_both() {
let mut s = Session::default();
run(&mut s, "parameters.add", json!({"name": "h", "expression": "20 mm"}));
run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": "h", "name": "Base"}));
// The top face, turned 45° about its front edge (y = 0, along X).
run(
&mut s,
"construct.plane.at_angle",
json!({"base": {"face": [5, 5, 20]}, "axis": {"edge": [20, 0, 20]}, "angle": "45 deg", "name": "Slanted"}),
);
let feature = s.doc.find_feature("Slanted").unwrap();
let solidcraft_doc::FeatureKind::ConstructionPlane { plane: solidcraft_doc::PlaneRef::AtAngle { base, line, .. } } = &feature.kind else {
panic!("at_angle plane: {:?}", feature.kind)
};
assert!(matches!(base.as_ref(), solidcraft_doc::PlaneRef::Face { anchor: Some(_), .. }), "{base:?}");
assert!(line.is_some(), "the edge is kept as a model reference");
let plane = |s: &Session| construction_planes(s).into_iter().find(|p| p.1 == "Slanted").map(|p| p.2).unwrap();
let half = 0.5_f64.sqrt();
let pl = plane(&s);
assert!(pl.height(Vec3::new(20.0, 0.0, 20.0)).abs() < 1e-6, "through the edge: {pl:?}");
assert!((pl.normal().z.abs() - half).abs() < 1e-9 && pl.normal().x.abs() < 1e-9, "45° to the top: {pl:?}");
// A taller box: the face and the edge both move up.
run(&mut s, "parameters.change", json!({"name": "h", "expression": "30 mm"}));
let pl = plane(&s);
assert!(pl.height(Vec3::new(20.0, 0.0, 30.0)).abs() < 1e-6, "follows the edge: {pl:?}");
assert!((pl.normal().z.abs() - half).abs() < 1e-9, "{pl:?}");
}
/// A box `h` high with a construction axis "Rim" on its top front edge (y = 0, along X), and a
/// plane "Slanted" turned 45 degrees about that axis, given by the axis's name, from the top
/// face (which contains the axis, so the plane contains it too).
fn slanted_about_a_named_axis() -> Session {
let mut s = Session::default();
run(&mut s, "parameters.add", json!({"name": "h", "expression": "20 mm"}));
run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": "h", "name": "Base"}));
run(&mut s, "construct.axis.edge", json!({"edge": {"edge": [20, 0, 20]}, "name": "Rim"}));
run(&mut s, "construct.plane.at_angle", json!({"base": {"face": [5, 5, 20]}, "axis": "Rim", "angle": "45 deg", "name": "Slanted"}));
s
}
fn slanted(s: &Session) -> Plane {
construction_planes(s).into_iter().find(|p| p.1 == "Slanted").map(|p| p.2).unwrap()
}
#[test]
fn plane_at_angle_about_a_named_axis_follows_the_axis() {
let mut s = slanted_about_a_named_axis();
let feature = s.doc.find_feature("Slanted").unwrap();
let solidcraft_doc::FeatureKind::ConstructionPlane { plane: solidcraft_doc::PlaneRef::AtAngle { line, .. } } = &feature.kind else {
panic!("at_angle plane: {:?}", feature.kind)
};
assert!(matches!(line.as_deref(), Some(solidcraft_doc::construct::GeoRef::Axis { name }) if *name == "Rim"), "{line:?}");
let half = 0.5_f64.sqrt();
let pl = slanted(&s);
assert!(pl.height(Vec3::new(0.0, 0.0, 20.0)).abs() < 1e-6, "through the rim: {pl:?}");
assert!((pl.normal().z.abs() - half).abs() < 1e-9 && pl.normal().x.abs() < 1e-9, "45 degrees to the top: {pl:?}");
// A taller box moves the edge, so the axis and the plane move with it.
run(&mut s, "parameters.change", json!({"name": "h", "expression": "50 mm"}));
let pl = slanted(&s);
assert!(pl.height(Vec3::new(0.0, 0.0, 50.0)).abs() < 1e-6, "follows the axis: {pl:?}");
assert!(pl.height(Vec3::new(0.0, 0.0, 20.0)).abs() > 1.0, "not where it was: {pl:?}");
assert!((pl.normal().z.abs() - half).abs() < 1e-9 && pl.normal().x.abs() < 1e-9, "{pl:?}");
// Saved and opened again it still follows.
let mut reopened = Session::new(solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap());
run(&mut reopened, "parameters.change", json!({"name": "h", "expression": "35 mm"}));
assert!(slanted(&reopened).height(Vec3::new(0.0, 0.0, 35.0)).abs() < 1e-6);
}
#[test]
fn a_plane_at_angle_saved_with_a_fixed_axis_stays_fixed() {
let s = slanted_about_a_named_axis();
// As older designs hold it: the axis as numbers only, no reference.
let mut json: Value = serde_json::from_str(&s.doc.to_json()).unwrap();
let mut stripped = 0;
if let Some(Value::Array(features)) = json.get_mut("features") {
for f in features {
if f.get("name").and_then(Value::as_str) != Some("Slanted") {
continue;
}
if let Some(Value::Object(plane)) = f.get_mut("plane")
&& plane.remove("line").is_some()
{
stripped += 1;
}
}
}
assert_eq!(stripped, 1, "the reference is saved in the plane: {json}");
let mut old = Session::new(solidcraft_doc::Document::from_json(&json.to_string()).unwrap());
run(&mut old, "parameters.change", json!({"name": "h", "expression": "50 mm"}));
let pl = slanted(&old);
// Followed, the plane would pass through the moved rim (0, 0, 50); with the axis fixed at
// z = 20 it turns about that line instead and misses it.
assert!(pl.height(Vec3::new(0.0, 0.0, 50.0)).abs() > 1.0, "fixed where it was saved: {pl:?}");
assert!((pl.normal().z.abs() - 0.5_f64.sqrt()).abs() < 1e-9, "still turned 45 degrees: {pl:?}");
}
#[test]
fn the_axis_keeps_its_turn_whichever_way_the_edge_runs() {
// The top and bottom rims run opposite ways round the shaft; a quarter turn about either
// lands in the same place.
let quarter = |edge: [f64; 3]| {
let mut s = shaft();
ring_profile(&mut s);
run(&mut s, "solid.revolve", json!({"sketch": "Profile", "axis": {"edge": edge}, "angle": "90 deg", "operation": "new"}));
let st = s.world_state().unwrap();
st.bodies.last().map(|b| b.mesh().bounds()).unwrap()
};
let (top, bottom) = (quarter([45.0, 0.0, 10.0]), quarter([45.0, 0.0, 0.0]));
assert!(top.min.dist(bottom.min) < 1e-6 && top.max.dist(bottom.max) < 1e-6, "{top:?} vs {bottom:?}");
}
#[test]
fn hostile_model_axes_are_refused_without_panicking() {
let mut s = shaft();
ring_profile(&mut s);
let before = s.doc.to_json();
for axis in [
json!({"edge": [1e9, 0, 0]}),
json!({"face": [1e300, -1e300, 0]}),
json!({"edge": "top"}),
json!({"face": [45, 0]}),
// The flat top: no axis.
json!({"face": [40, 0, 10]}),
// The shaft's axis doesn't lie in a sketch on XY.
] {
for (cmd, p) in [
("solid.revolve", json!({"sketch": "Profile", "axis": axis})),
("surface.revolve", json!({"sketch": "Profile", "axis": axis})),
("solid.pattern.circular", json!({"bodies": ["Shaft"], "axis": axis, "count": 3})),
("construct.plane.at_angle", json!({"base": "XZ", "axis": axis, "angle": "10 deg"})),
] {
assert!(s.execute(cmd, &p).is_err(), "{cmd} {p}");
assert_eq!(s.doc.to_json(), before, "{cmd} {p} left the design as it was");
}
}
run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Flat"}));
run(&mut s, "sketch.rectangle.two_point", json!({"p0": [46, 2], "p1": [48, 8]}));
run(&mut s, "sketch.finish", json!({}));
let e = s.execute("solid.revolve", &json!({"sketch": "Flat", "axis": {"face": [45, 0, 5]}})).unwrap_err().to_string();
assert!(e.contains("does not lie in the sketch plane"), "{e}");
}