Move, Align and previews on placed components (#4455)
* Move and Align on placed components: picks and vectors where they are shown A body in a component placed away from its own frame (an imported part set upright) moved the wrong way: Move's vector, axis and pivot were taken in the root's frame, so moving the upright plate up moved it along its own frame's z. They now turn with the frame of the bodies moved. Align mapped each pick into the frame of the body under it, so aligning two separately placed parts mixed two frames (the plate landed at right angles), and its snap points were never mapped at all. Align now finds its picks, face normals and snaps where the view shows them and takes both ends into the moved bodies' frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Preview: a feature's tool is drawn where its component is shown Extrude, revolve and the primitives preview their tool on its own. That preview was built in the model's frame, so on a component placed away from it (an imported part set upright, edited with its component active) the tool showed at right angles to the picked face, though the feature itself came out right. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Follow main's lazy body frames and first-part grounding in the placed-component work 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:
@@ -106,6 +106,22 @@ fn inside_hull(sorted: &[(f64, f64)], q: (f64, f64)) -> bool {
|
||||
hull.len() >= 3 && (0..hull.len()).all(|i| cross(hull[i], hull[(i + 1) % hull.len()], q) >= -eps)
|
||||
}
|
||||
|
||||
/// The outward normal of the planar face at the world point `p`: on the model as the view
|
||||
/// places it (moved components where they are shown).
|
||||
pub(super) fn face_normal_world(s: &Session, p: Vec3) -> Option<Vec3> {
|
||||
let (m, f, d, _) = face_at_state(&s.world_state(), p, None).ok().flatten()?;
|
||||
let tol = (m.bounds().diagonal() * 1e-3).max(1e-3);
|
||||
if d > tol * 10.0 {
|
||||
return None;
|
||||
}
|
||||
m.triangles
|
||||
.iter()
|
||||
.zip(&m.tri_face)
|
||||
.filter(|(_, tf)| **tf == f)
|
||||
.filter_map(|(t, _)| m.tri(t))
|
||||
.find_map(|[a, b, c]| (b - a).cross(c - a).normalized())
|
||||
}
|
||||
|
||||
/// Is the body face at `p` planar (`None`: no face there)?
|
||||
pub(super) fn face_is_planar(s: &Session, p: Vec3) -> Option<bool> {
|
||||
let (m, f, d, _) = face_at(s, p)?;
|
||||
|
||||
@@ -410,6 +410,17 @@ fn align(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Err(bad(cmd, format!("no body `{b}`")));
|
||||
}
|
||||
}
|
||||
// Picks and snaps are where the view shows them (the world); the feature moves the bodies
|
||||
// in their component's frame, so both ends go into the first moved body's frame. A body
|
||||
// picked in another placed component (aligning two imported parts) is found where it
|
||||
// is shown, not where its own frame has it.
|
||||
let frame = s
|
||||
.placed_document()
|
||||
.body_frames_with_source(&s.model.state())
|
||||
.into_iter()
|
||||
.find(|(world, own, ..)| *world == bodies[0] || *own == bodies[0])
|
||||
.map(|(.., m)| m);
|
||||
let inverse = frame.and_then(|m| solidcraft_doc::mat_inverse(&m)).unwrap_or(solidcraft_doc::IDENTITY);
|
||||
// A side is a plane (`from_plane`/`to_plane`: XY|XZ|YZ, a construction or offset plane, or
|
||||
// {origin, normal}), kept as a plane so the bodies follow it when it moves; or a point and, for
|
||||
// a face, its normal.
|
||||
@@ -417,7 +428,7 @@ fn align(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
p.get(key).map(|_| super::features::plane_param(s, p.get(key), cmd)).transpose()
|
||||
};
|
||||
let (from_plane, to_plane) = (plane(s, "from_plane")?, plane(s, "to_plane")?);
|
||||
let pick = |s: &Session, pk: &str, fk: &str, plane: &Option<solidcraft_doc::PlaneRef>| -> Result<(Vec3, Option<Vec3>)> {
|
||||
let world_pick = |s: &Session, pk: &str, fk: &str, plane: &Option<solidcraft_doc::PlaneRef>| -> Result<(Vec3, Option<Vec3>)> {
|
||||
if plane.is_some() {
|
||||
if p.get(pk).is_some() || p.get(fk).is_some() {
|
||||
return Err(bad(cmd, format!("give `{pk}_plane` or `{pk}`/`{fk}`, not both")));
|
||||
@@ -426,7 +437,7 @@ fn align(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Ok((Vec3::ZERO, None));
|
||||
}
|
||||
if let Some(fp) = p.get(fk).and_then(vec3) {
|
||||
let n = super::face::face_normal(s, fp).ok_or_else(|| bad(cmd, format!("`{fk}` must be on a planar face")))?;
|
||||
let n = super::face::face_normal_world(s, fp).ok_or_else(|| bad(cmd, format!("`{fk}` must be on a planar face")))?;
|
||||
return Ok((fp, Some(n)));
|
||||
}
|
||||
// The origin point.
|
||||
@@ -444,6 +455,13 @@ fn align(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.map(|v| (v, None))
|
||||
.ok_or_else(|| bad(cmd, format!("give `{pk}`, `{fk}` or `{pk}_plane` ([x, y, z], \"origin\", or a plane)")))
|
||||
};
|
||||
let pick = |s: &Session, pk: &str, fk: &str, plane: &Option<solidcraft_doc::PlaneRef>| -> Result<(Vec3, Option<Vec3>)> {
|
||||
let (at, n) = world_pick(s, pk, fk, plane)?;
|
||||
if plane.is_some() {
|
||||
return Ok((at, n));
|
||||
}
|
||||
Ok((solidcraft_doc::apply_point(&inverse, at), n.and_then(|n| solidcraft_doc::apply_vector(&inverse, n).normalized())))
|
||||
};
|
||||
let (from, from_normal) = pick(s, "from", "from_face", &from_plane)?;
|
||||
let (to, to_normal) = pick(s, "to", "to_face", &to_plane)?;
|
||||
// Faces turn only to meet another face or a plane; a plane's normal is read when evaluated.
|
||||
|
||||
@@ -2046,3 +2046,64 @@ fn an_imported_assembly_with_a_repeated_part_moves_as_one() {
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A 20 × 30 × 8 plate made in its own component and stood up by its placement (turned
|
||||
/// `angle` degrees about X), the way an imported part set upright is: in the world it lies
|
||||
/// flat at `x0`..`x0 + 20`, y 0..30, z 0..8. The design is active again afterwards.
|
||||
fn placed_plate(s: &mut Session, name: &str, x0: f64, angle: f64) {
|
||||
let made = run(s, "component.create", json!({"name": name, "grounded": false}));
|
||||
// Turned +90° about X a point goes (x, y, z) → (x, −z, y); turned −90°, (x, z, −y).
|
||||
let corner = if angle > 0.0 { json!([x0, 0, -30]) } else { json!([x0, -8, 0]) };
|
||||
let (width, height) = (8, 30);
|
||||
run(s, "solid.box", json!({"length": 20, "width": width, "height": height, "corner": corner, "body_name": name}));
|
||||
run(s, "occurrence.move", json!({"occurrence": made["occurrence"], "axis": [1, 0, 0], "angle": format!("{angle} deg")}));
|
||||
run(s, "component.activate", json!({"component": 0}));
|
||||
let b = world_bounds(s, name);
|
||||
for (got, want) in [(b.min.x, x0), (b.max.x, x0 + 20.0), (b.min.y, 0.0), (b.max.y, 30.0), (b.min.z, 0.0), (b.max.z, 8.0)] {
|
||||
assert!((got - want).abs() < 1e-6, "{name} placed at {b:?}");
|
||||
}
|
||||
}
|
||||
|
||||
fn world_bounds(s: &Session, name: &str) -> solidcraft_geom::Aabb3 {
|
||||
s.world_state().body(name).unwrap_or_else(|| panic!("no {name}")).mesh().bounds()
|
||||
}
|
||||
|
||||
fn near(a: f64, b: f64) -> bool {
|
||||
(a - b).abs() < 1e-6
|
||||
}
|
||||
|
||||
/// Move's vector, axis and pivot are where the view shows them, whatever frame the moved
|
||||
/// body's component has: a plate stood up by its placement and moved up goes up.
|
||||
#[test]
|
||||
fn a_move_goes_the_way_it_is_shown_on_a_placed_component() {
|
||||
let mut s = Session::default();
|
||||
placed_plate(&mut s, "Plate", 0.0, 90.0);
|
||||
run(&mut s, "solid.move", json!({"bodies": ["Plate"], "translate": [0, 0, 10]}));
|
||||
let b = world_bounds(&s, "Plate");
|
||||
assert!(near(b.min.z, 10.0) && near(b.max.z, 18.0) && near(b.min.y, 0.0) && near(b.max.y, 30.0), "moved up: {b:?}");
|
||||
// A quarter turn about the world's Z through the origin: (x, y) → (−y, x).
|
||||
run(&mut s, "solid.move", json!({"bodies": ["Plate"], "axis": [0, 0, 1], "angle": 90}));
|
||||
let b = world_bounds(&s, "Plate");
|
||||
assert!(near(b.min.x, -30.0) && near(b.max.x, 0.0) && near(b.min.y, 0.0) && near(b.max.y, 20.0) && near(b.min.z, 10.0), "turned: {b:?}");
|
||||
}
|
||||
|
||||
/// Align between two parts placed in different frames: both ends are taken where they are
|
||||
/// shown, so the plate lands on the other one rather than at right angles to it.
|
||||
#[test]
|
||||
fn align_takes_both_ends_where_they_are_shown_across_placed_components() {
|
||||
let mut s = Session::default();
|
||||
placed_plate(&mut s, "A", 0.0, 90.0);
|
||||
placed_plate(&mut s, "B", 50.0, -90.0);
|
||||
// Corner to corner: a plain move.
|
||||
run(&mut s, "solid.align", json!({"bodies": ["A"], "from": [0, 0, 8], "to": [50, 0, 8]}));
|
||||
let a = world_bounds(&s, "A");
|
||||
assert!(near(a.min.x, 50.0) && near(a.max.x, 70.0) && near(a.min.z, 0.0) && near(a.max.z, 8.0), "{a:?}");
|
||||
// Snapped corners, then the top face onto B's top face: A turns over and sits on B.
|
||||
run(&mut s, "solid.align", json!({"bodies": ["A"], "from": {"snap": "vertex", "at": [50, 0, 8]}, "to": {"snap": "vertex", "at": [70, 30, 8]}}));
|
||||
let a = world_bounds(&s, "A");
|
||||
assert!(near(a.min.x, 70.0) && near(a.min.y, 30.0) && near(a.max.z, 8.0), "snapped: {a:?}");
|
||||
run(&mut s, "solid.align", json!({"bodies": ["A"], "from_face": [80, 45, 8], "to_face": [60, 15, 8]}));
|
||||
let a = world_bounds(&s, "A");
|
||||
assert!(near(a.min.z, 8.0) && near(a.max.z, 16.0), "on B's top, not across it: {a:?}");
|
||||
assert!(near(0.5 * (a.min.x + a.max.x), 60.0) && near(0.5 * (a.min.y + a.max.y), 15.0), "centred on the picked point: {a:?}");
|
||||
}
|
||||
|
||||
@@ -26,6 +26,9 @@ enum K {
|
||||
Make,
|
||||
/// A direction: turned with the command's frame.
|
||||
Dir,
|
||||
/// A point the command's bodies move about: into the frame of those bodies (the command's
|
||||
/// frame, as its directions are).
|
||||
Moved,
|
||||
/// An arbitrary axis object in the authoring frame: {origin, dir}.
|
||||
Axis,
|
||||
/// A plane object (`{face}`, `{origin, normal}`, `{origin, x_dir, y_dir}`) or a plane name.
|
||||
@@ -61,7 +64,10 @@ fn table(id: &str) -> &'static [(&'static str, K)] {
|
||||
"solid.draft" => &[("faces", Picks), ("neutral", Plane), ("pull", Dir), ("body", Body)],
|
||||
"solid.press_pull" => &[("face", Pick), ("edges", Picks)],
|
||||
"solid.replace_face" => &[("faces", Picks), ("target_face", Pick), ("target", Plane), ("body", Body)],
|
||||
"solid.align" => &[("from", Pick), ("from_face", Pick), ("to", Pick), ("to_face", Pick), ("bodies", Body)],
|
||||
// Align finds its picks where they are shown and takes both ends into the moved bodies'
|
||||
// frame itself: a pick mapped into the frame of the body under it would put the two ends
|
||||
// in different frames when they lie on separately placed components.
|
||||
"solid.align" => &[("bodies", Body)],
|
||||
// A mirror keeps the placement its bodies were picked at (`cmd::features::placed_bodies`);
|
||||
// its plane is in the active component, where the copies go.
|
||||
"solid.mirror" => &[("plane", HerePlane)],
|
||||
@@ -84,7 +90,7 @@ fn table(id: &str) -> &'static [(&'static str, K)] {
|
||||
"inspect.curvature_comb" | "inspect.minimum_radius" | "inspect.isocurve" => &[("edges", Picks), ("faces", Picks)],
|
||||
"parts.insert" | "parts.fastener" => &[("at", Pick), ("point", Make), ("direction", Dir)],
|
||||
"appearance.assign" => &[("faces", Picks), ("bodies", Body), ("body", Body)],
|
||||
"solid.move" => &[("bodies", Body), ("translate", Dir), ("axis", Dir), ("origin", Make)],
|
||||
"solid.move" => &[("bodies", Body), ("translate", Dir), ("axis", Dir), ("origin", Moved)],
|
||||
"solid.combine" => &[("target", Body), ("tools", Body)],
|
||||
"solid.remove" | "solid.bounding_solid" | "material.assign" => &[("bodies", Body)],
|
||||
_ => &[],
|
||||
@@ -444,7 +450,19 @@ pub fn to_local(s: &Session, id: &str, params: &Value) -> Option<Value> {
|
||||
command_frames.active = frame;
|
||||
command_frames.sketch = frame;
|
||||
}
|
||||
let mut m = Mapper { s, f: command_frames, first: None, changed: false, here: false };
|
||||
// A move has no picks: its vector, axis and pivot are in the world and turn with the frame
|
||||
// of the bodies it moves (an imported part stood upright moved up goes up, not along its
|
||||
// own frame's z). A turn with no pivot is about the world origin.
|
||||
let first = (id == "solid.move")
|
||||
.then(|| obj.get("bodies").and_then(Value::as_array)?.first()?.as_str())
|
||||
.flatten()
|
||||
.and_then(|b| command_frames.bodies(s).iter().find(|(world, own, _)| world == b || own == b).map(|(.., m)| *m));
|
||||
let mut obj = obj.clone();
|
||||
if first.is_some() && obj.contains_key("axis") && !obj.contains_key("origin") {
|
||||
obj.insert("origin".into(), serde_json::json!([0.0, 0.0, 0.0]));
|
||||
}
|
||||
let obj = &obj;
|
||||
let mut m = Mapper { s, f: command_frames, first, changed: false, here: false };
|
||||
let body = obj.get("body").or_else(|| obj.get("face_body")).and_then(Value::as_str).map(str::to_string);
|
||||
let mut out: Map<String, Value> = obj.clone();
|
||||
// Picks first: their frame turns the directions.
|
||||
@@ -460,6 +478,10 @@ pub fn to_local(s: &Session, id: &str, params: &Value) -> Option<Value> {
|
||||
Picks => m.picks(v, body.as_deref()),
|
||||
Make => m.make(v),
|
||||
Dir => m.dir(v),
|
||||
Moved => {
|
||||
let frame = m.first.unwrap_or(m.f.active);
|
||||
vec3(v).and_then(|p| m.local_point(&frame, p))
|
||||
}
|
||||
Axis => {
|
||||
if let Some(axis) = v.as_object() {
|
||||
let mut axis = axis.clone();
|
||||
|
||||
@@ -448,7 +448,9 @@ pub fn compute(s: &Session, cmds: &[(String, Value)], colors: Colors) -> Job {
|
||||
/// error. Other features: their whole preview ([`compute`]).
|
||||
fn compute_tool(s: &Session, cmds: &[(String, Value)], colors: Colors) -> Job {
|
||||
let Some((alt, op)) = as_new_body(cmds) else { return compute(s, cmds, colors) };
|
||||
let tool = match s.preview(&alt) {
|
||||
// Where the view shows it: in a component placed away from its own frame (an imported
|
||||
// part stood upright) the model's frame would draw the tool at right angles to the face.
|
||||
let tool = match s.preview_world(&alt) {
|
||||
Ok(t) => t,
|
||||
Err(e) => return Err(s.preview(cmds).err().map_or_else(|| e.to_string(), |r| r.to_string())),
|
||||
};
|
||||
@@ -1102,6 +1104,23 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// A plate in its own component, stood up by its placement (an imported part set upright)
|
||||
/// and edited with that component active: the extrusion's tool is drawn where the plate is
|
||||
/// shown, on its top face, not at right angles to it in the component's own frame.
|
||||
#[test]
|
||||
fn a_tool_on_a_turned_component_previews_where_it_is_shown() {
|
||||
let mut s = Session::default();
|
||||
let made = s.execute("component.create", &json!({"name": "Plate", "grounded": false})).unwrap();
|
||||
s.execute("solid.box", &json!({"length": 20, "width": 8, "height": 30, "corner": [0, 0, -30], "body_name": "Plate"})).unwrap();
|
||||
s.execute("occurrence.move", &json!({"occurrence": made["occurrence"], "axis": [1, 0, 0], "angle": "90 deg"})).unwrap();
|
||||
s.execute("component.activate", &json!({"component": made["component"], "occurrence": made["occurrence"], "path": [made["occurrence"]]}))
|
||||
.unwrap();
|
||||
// Shown lying flat: 20 × 30, 8 high. Its top face pulled up 10.
|
||||
let cmd = ("solid.extrude".to_string(), json!({"face": [10, 15, 8], "face_body": "Plate", "distance": "10 mm", "operation": "join"}));
|
||||
let b = compute_tool(&s, &[cmd], colors()).unwrap().bounds;
|
||||
assert!((b.max.z - 18.0).abs() < 1e-6 && (b.max.y - 30.0).abs() < 1e-6, "on top, not at right angles: {b:?}");
|
||||
}
|
||||
|
||||
fn count(bytes: &[u8], stride: usize, col: [u8; 4]) -> usize {
|
||||
bytes.chunks(stride).filter(|v| v.get(24..28) == Some(&col[..])).count()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user