Assembly: ground the first part; drag free joint chains (#2804) (#2805)

* assembly(feat): ground first parts and drag free joint chains

Ground the first top-level part when it is created and let face dragging reposition free occurrences or any member of a floating joint chain in one undo step. Preserve exact repeated-placement frames, joint values, modifier selection and Escape cancellation. Keep the accumulated fixture, viewport layout and documentation integration fixes from the existing PR.

Signed-off-by: Karl Von Richter<karl@embeint.com>

* assembly(fix): update movable fixtures for first-part grounding

Accept the documented grounded override in component creation, explicitly free movement fixtures, and keep linked clipboard pastes movable even in an empty document. Cover first and reused linked pastes, default insertion grounding, and single-step undo in the existing link tests.

Signed-off-by: Karl Von Richter<karl@embeint.com>

* assembly(test): keep the motion-cost crank free

Unground the first component in the document-level joint-face measurement fixture so its motion samples can rotate after first-part grounding. Preserve the rotation and shared-measurement assertions that caught the Linux CI failure.

Signed-off-by: Karl Von Richter<karl@embeint.com>

* assembly(test): free the frame placement fixture

Create the frame-member placement fixture ungrounded before moving it above its root sketch. Keep its world-space member assertions while respecting first-part grounding in the frame tests added on main.

Signed-off-by: Karl Von Richter<karl@embeint.com>

---------

Signed-off-by: Karl Von Richter<karl@embeint.com>
This commit is contained in:
Karl Von Richter
2026-10-23 23:15:34 +00:00
committed by GitHub
parent 30b01e8d79
commit 0dffb206b1
75 changed files with 1094 additions and 115 deletions
+27 -8
View File
@@ -11,6 +11,8 @@ use super::CommandSpec;
use crate::params::{bad, bool_, str_, string_list, vec3};
use crate::{Result, Session};
#[path = "component_drag.rs"]
mod drag;
#[path = "component_matrix.rs"]
pub(super) mod matrix;
#[path = "component_properties.rs"]
@@ -27,7 +29,7 @@ pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("component.create", "New Component", new_component)
.at("SOLID", "ASSEMBLE")
.icon("component")
.params("name?, parent?: component id or name (default: the active one); parent_path?: exact parent placement; activate?: bool (default true)"),
.params("name?, parent?: component id or name (default: the active one); parent_path?: exact parent placement; activate?: bool (default true); grounded?: bool (default: true for the design's first part, false otherwise)"),
CommandSpec::new("component.from_bodies", "Create Components from Bodies", from_bodies)
.icon("component")
.params("bodies: [names] → one new component per body, named after it"),
@@ -54,7 +56,12 @@ pub static COMMANDS: &[CommandSpec] = &[
.params("component (or occurrence): what to place again; path?: validates selected occurrence; parent?: component (default: the same parent); translate?: [x,y,z]. Copies the selected effective transform, including pending moves"),
CommandSpec::new("component.paste_new", "Paste New", paste_new).icon("paste").params("component: independently copy its nested definition graph and history, materials and styles; occurrence?, path?: selected source placement; translate?: [x,y,z]. Reassigned external bodies become base geometry; retained external links and omitted boundary joints are reported"),
CommandSpec::new("occurrence.move", "Move Occurrence", move_occurrence).icon("move")
.params("occurrence; path?: exact placement; space?: local|world (default local); translate?: [x,y,z]; axis?: [x,y,z], angle?: radians or \"30 deg\", origin?: [x,y,z]; matrix?: [12 or 16 numbers] (instead of translate/axis/angle/origin: a rigid move as a row-major 3x4 rotation-and-translation or 4x4 matrix, flat or as rows; one that is a rotation only up to rounding is orthonormalised, a scale, shear or mirror is refused); tolerance?: largest accepted |RᵀR − I| (default 1e-9, at most 1e-3); capture?: bool (default true; false keeps it pending until Capture Position). Nested occurrence definitions remain shared"),
.params("occurrence; path?: exact placement; space?: local|world (default local); translate?: [x,y,z]; axis?: [x,y,z], angle?: radians or \"30 deg\", origin?: [x,y,z]; matrix?: [12 or 16 numbers] (instead of translate/axis/angle/origin: a rigid move as a row-major 3x4 rotation-and-translation or 4x4 matrix, flat or as rows; one that is a rotation only up to rounding is orthonormalised, a scale, shear or mirror is refused); tolerance?: largest accepted |RᵀR − I| (default 1e-9, at most 1e-3); capture?: bool (default true; false keeps it pending until Capture Position); move_grounded?: bool (default false: a grounded occurrence refuses; true moves it and it stays grounded). Nested occurrence definitions remain shared"),
CommandSpec::new("occurrence.drag", "Drag Occurrence", drag::drag).icon("move").params(
"occurrence or path: exact placement; at: [x,y,z] world point on it; point: [x,y,z] where it goes. Moves the placement by \
point - at (with any chain of joints it is in that is joined to nothing grounded) and captures its position; other pending \
moves stay pending. Shared definitions move in all placements. Grounded parts and parts joints hold to something fixed are refused (joint.reach moves those)",
),
CommandSpec::new("occurrence.ground", "Ground", ground).icon("ground").params("occurrence; grounded?: bool (default toggles)"),
CommandSpec::new("component.capture_position", "Capture Position", capture).at("SOLID", "POSITION").icon("capture").params("keeps pending occurrence moves"),
CommandSpec::new("component.revert_position", "Revert Position", revert).at("SOLID", "POSITION").icon("revert").noundo().params("occurrences?: [ids] (default all); drops selected pending occurrence moves"),
@@ -110,10 +117,10 @@ pub(super) fn valid_position(m: &Mat) -> bool {
pub(super) fn new_component(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "component.create";
if let Some(params) = p.as_object()
&& let Some(key) = params.keys().find(|key| !matches!(key.as_str(), "name" | "parent" | "parent_path" | "activate"))
&& let Some(key) = params.keys().find(|key| !matches!(key.as_str(), "name" | "parent" | "parent_path" | "activate" | "grounded"))
{
let hint = if params.contains_key("bodies") { "; use component.move_bodies or component.from_bodies to move bodies" } else { "" };
return Err(bad(cmd, format!("unknown parameter `{key}`; accepted parameters: name, parent, parent_path, activate{hint}")));
return Err(bad(cmd, format!("unknown parameter `{key}`; accepted parameters: name, parent, parent_path, activate, grounded{hint}")));
}
let parent = if p.get("parent").is_some() { component(s, p, "parent", cmd)? } else { s.active_component };
let parent_path = if let Some(value) = p.get("parent_path") {
@@ -135,7 +142,16 @@ pub(super) fn new_component(s: &mut Session, p: &Value) -> Result<Value> {
if parent_path.len() >= 64 {
return Err(bad(cmd, "new component exceeds occurrence depth limit"));
}
let grounded = match p.get("grounded") {
None => None,
Some(v) => Some(v.as_bool().ok_or_else(|| bad(cmd, "grounded must be true or false"))?),
};
let id = s.doc_mut().add_component(str_(p, "name"), parent)?;
if let Some(grounded) = grounded
&& let Some(o) = s.doc_mut().occurrences.iter_mut().find(|o| o.component == id)
{
o.grounded = grounded;
}
if p.get("activate").and_then(Value::as_bool).unwrap_or(true) {
let mut path = parent_path;
if let Some(o) = s.doc.occurrence_of(id) {
@@ -145,8 +161,8 @@ pub(super) fn new_component(s: &mut Session, p: &Value) -> Result<Value> {
s.active_occurrence_path = Some(path);
}
let name = s.doc.components.iter().find(|c| c.id == id).map(|c| c.name.clone()).unwrap_or_default();
let occ = s.doc.occurrence_of(id).map(|o| o.id);
Ok(json!({"component": id, "name": name, "parent": parent, "occurrence": occ}))
let occ = s.doc.occurrence_of(id);
Ok(json!({"component": id, "name": name, "parent": parent, "occurrence": occ.map(|o| o.id), "grounded": occ.is_some_and(|o| o.grounded)}))
}
fn from_bodies(s: &mut Session, p: &Value) -> Result<Value> {
@@ -394,8 +410,11 @@ fn move_occurrence(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "occurrence.move";
let id = occurrence(s, p, cmd)?;
let o = s.doc.occurrences.iter().find(|o| o.id == id).cloned().ok_or_else(|| bad(cmd, "occurrence"))?;
if o.grounded {
return Err(bad(cmd, format!("{} is grounded", o.name)));
if o.grounded && bool_(p, "move_grounded") != Some(true) {
return Err(bad(
cmd,
format!("{} is grounded: unground it first (occurrence.ground; a design's first part is grounded when it is made)", o.name),
));
}
if let Some(mut group) = s.held_by_rigid_group(id) {
// The outermost component the group holds it in moves it.
+2 -1
View File
@@ -275,7 +275,8 @@ pub(super) fn paste(
clone_path.push(root_occurrence);
if let Some(o) = scratch.doc_mut().occurrences.iter_mut().find(|o| o.id == root_occurrence) {
o.transform = transform;
o.grounded = source_occurrence.as_ref().is_some_and(|o| o.grounded);
// A copy starts free, even of a grounded part (#2804); the parts inside it keep theirs.
o.grounded = false;
}
if let Some(o) = &source_occurrence {
occurrences.insert(o.id, root_occurrence);
@@ -5,7 +5,7 @@ fn run(s: &mut Session, cmd: &str, p: Value) -> Value {
s.execute(cmd, &p).unwrap()
}
fn component(s: &mut Session, name: &str) -> u64 {
run(s, "component.create", json!({"name":name}))["component"].as_u64().unwrap()
run(s, "component.create", json!({"name":name,"grounded":false}))["component"].as_u64().unwrap()
}
fn volume(s: &mut Session, id: u64) -> f64 {
run(s, "component.properties", json!({"component":id}))["total"]["volume_mm3"].as_f64().unwrap()
+162
View File
@@ -0,0 +1,162 @@
//! Dragging a part to put it somewhere (#2804): what the viewport's left-button drag on a free
//! part runs each frame, and what scripts and agents use to move a placement by a grabbed point.
//! A grounded part stays where it is, and so does one that joints hold to something fixed
//! (`joint.reach` moves those as far as their joints allow). A chain of joints joined to nothing
//! fixed moves as one, so its joints stay as they are.
use serde_json::{Value, json};
use solidcraft_doc::{mat_inverse, mat_mul};
use super::{move_vector, occurrence, occurrence_path, translation, valid_position};
use crate::params::bad;
use crate::{EngineError, Result, Session};
#[cfg(test)]
#[path = "component_drag_tests.rs"]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests;
/// Most placements one drag moves (the dragged one and the chain of joints it is in).
const MAX_MOVED: usize = 1000;
fn refused(message: String) -> EngineError {
EngineError::Other(message)
}
impl Session {
/// The placements a drag on the placement `path` moves, each by its exact path: that
/// placement first, then those a chain of joints joined to nothing fixed carries along with
/// it. An error says why it can't be dragged (grounded, held by joints to something fixed,
/// or held in a rigid group). Shared definitions move in all of their placements.
pub fn drag_group(&self, path: &[u64]) -> Result<Vec<Vec<u64>>> {
let placement = self.occurrence_placement(path)?;
let doc = &self.doc;
let name = |id: u64| doc.occurrences.iter().find(|o| o.id == id).map_or_else(|| id.to_string(), |o| o.name.clone());
let dragged = placement.occurrence;
if placement.grounded {
return Err(refused(format!("{} is grounded", placement.name)));
}
if let Some(group) = self.held_by_rigid_group(dragged) {
let group = doc.components.iter().find(|c| c.id == group).map(|c| c.name.clone()).unwrap_or_default();
return Err(refused(format!("{} is held in {group} by a rigid group: drag {group}, or Unlock Contents first", placement.name)));
}
// Is `o` the placement `top` or inside its component (in every placement of it)?
let within = |o: u64, top: u64| -> bool {
if o == top {
return true;
}
let (Some(o), Some(top)) = (doc.occurrences.iter().find(|x| x.id == o), doc.occurrences.iter().find(|x| x.id == top)) else {
return false;
};
o.parent != 0 && doc.component_within(o.parent, top.component)
};
let joints: Vec<_> = doc.assembly.joints.iter().filter(|j| !j.suppressed).collect();
let mut members = vec![dragged];
// Grow the chain one joint at a time; each pass adds a member or stops.
for _ in 0..MAX_MOVED {
let inside = |o: u64| members.iter().any(|m| within(o, *m));
let next = joints.iter().find_map(|j| match (inside(j.a.occurrence), inside(j.b.occurrence)) {
(true, false) => Some((j, j.b.occurrence)),
(false, true) => Some((j, j.a.occurrence)),
_ => None,
});
let Some((joint, other)) = next else {
break;
};
if other == 0 {
return Err(refused(format!("{} holds {} to the design's origin", joint.name, placement.name)));
}
// Only this member's placement is edited. A grounded ancestor fixes its own
// transform, while its free contents may still move inside that frame.
if doc.occurrences.iter().any(|o| o.id == other && o.grounded) {
return Err(refused(format!(
"{} holds {} to grounded {}: it moves only as far as its joints allow (joint.reach)",
joint.name,
placement.name,
name(other)
)));
}
if path.contains(&other) || within(dragged, other) {
return Err(refused(format!("{} holds {} to the assembly it is in: drag {}", joint.name, placement.name, name(other))));
}
members.push(other);
if members.len() > MAX_MOVED {
return Err(refused(format!("the chain of joints {} is in moves more than {MAX_MOVED} parts", placement.name)));
}
}
// A member inside another one moves with it (moving both would move it twice).
let tops: Vec<u64> = members.iter().copied().filter(|m| !members.iter().any(|o| o != m && within(*m, *o))).collect();
let mut out = vec![path.to_vec()];
for m in tops.into_iter().filter(|m| *m != dragged) {
let parent = doc.occurrences.iter().find(|o| o.id == m).map_or(0, |o| o.parent);
let mut chain = doc.first_path(parent);
chain.push(m);
out.push(chain);
}
Ok(out)
}
}
/// `occurrence.drag`: move a placement by `point - at` in world coordinates (and the free chain
/// of joints it is in), capturing its position.
pub(super) fn drag(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "occurrence.drag";
let path = if p.get("occurrence").is_some() {
let id = occurrence(s, p, cmd)?;
occurrence_path(s, p, id, cmd)?
} else {
let path = p
.get("path")
.and_then(Value::as_array)
.filter(|a| !a.is_empty() && a.len() <= 64)
.and_then(|a| a.iter().map(Value::as_u64).collect::<Option<Vec<_>>>())
.ok_or_else(|| bad(cmd, "give `occurrence` or `path` (1..64 occurrence ids)"))?;
s.occurrence_placement(&path).map_err(|e| bad(cmd, e.to_string()))?;
path
};
if p.get("at").is_none() || p.get("point").is_none() {
return Err(bad(cmd, "`at` and `point` must both be given"));
}
let at = move_vector(p, "at", Default::default(), true, cmd)?;
let point = move_vector(p, "point", Default::default(), true, cmd)?;
let t = point - at;
let world_move = translation(t);
let group = s.drag_group(&path).map_err(|e| bad(cmd, e.to_string()))?;
let dragged = s.occurrence_placement(&path).map_err(|e| bad(cmd, e.to_string()))?;
let inverse = mat_inverse(&dragged.parent_world).ok_or_else(|| bad(cmd, "parent frame is singular"))?;
let local_delta = mat_mul(&mat_mul(&inverse, &world_move), &dragged.parent_world);
// Joints refer to shared occurrence definitions, and the solver places them through their
// first path. Carry the selected placement's local change into that same frame before
// moving its peers. Using the pointer's world delta directly would break the joint when
// the selected instance has a different rotation from the first one.
let parent = s.doc.occurrences.iter().find(|o| o.id == dragged.occurrence).map_or(0, |o| o.parent);
let mut first_path = s.doc.first_path(parent);
first_path.push(dragged.occurrence);
let first = s.occurrence_placement(&first_path).map_err(|e| bad(cmd, e.to_string()))?;
let first_inverse = mat_inverse(&first.parent_world).ok_or_else(|| bad(cmd, "parent frame is singular"))?;
let world_delta = mat_mul(&mat_mul(&first.parent_world, &local_delta), &first_inverse);
// Every new position first, from the placements as they are, then all of them at once.
let mut moves = Vec::with_capacity(group.len());
for placement_path in &group {
let placement = s.occurrence_placement(placement_path).map_err(|e| bad(cmd, e.to_string()))?;
let inverse = mat_inverse(&placement.parent_world).ok_or_else(|| bad(cmd, "parent frame is singular"))?;
let delta =
if placement.occurrence == dragged.occurrence { local_delta } else { mat_mul(&mat_mul(&inverse, &world_delta), &placement.parent_world) };
let next = mat_mul(&delta, &placement.local);
if !valid_position(&next) {
return Err(bad(cmd, "drag exceeds supported numeric range"));
}
moves.push((placement.occurrence, next));
}
let moved: Vec<u64> = moves.iter().map(|(id, _)| *id).collect();
if world_move != solidcraft_doc::IDENTITY {
for (id, next) in moves {
s.pending_moves.remove(&id);
if let Some(o) = s.doc_mut().occurrences.iter_mut().find(|o| o.id == id) {
o.transform = next;
}
}
}
let continuous = p.get("continuous").and_then(Value::as_bool).unwrap_or(false);
Ok(json!({"occurrence": path.last(), "path": path, "moved": moved, "translate": [t.x, t.y, t.z], "continuous": continuous}))
}
@@ -0,0 +1,289 @@
//! The design's first part is grounded; dragging a free part moves it (#2804).
use serde_json::{Value, json};
use crate::Session;
fn run(s: &mut Session, command: &str, p: Value) -> Value {
s.execute(command, &p).unwrap_or_else(|e| panic!("{command} {p}: {e}"))
}
/// A new top-level part holding a 10 mm cube at `corner` (activates the design first, so the
/// part goes into it): (component, occurrence).
fn part(s: &mut Session, name: &str, corner: [f64; 3]) -> (u64, u64) {
run(s, "component.activate", json!({"component": "root"}));
let c = run(s, "component.create", json!({"name": name}));
run(s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": corner}));
run(s, "component.activate", json!({"component": "root"}));
(c["component"].as_u64().unwrap(), c["occurrence"].as_u64().unwrap())
}
fn grounded(s: &Session, occurrence: u64) -> bool {
s.doc.occurrences.iter().find(|o| o.id == occurrence).unwrap().grounded
}
/// Where the placement `path` puts its origin, in world coordinates (pending moves included).
fn origin(s: &Session, path: &[u64]) -> [f64; 3] {
let w = s.occurrence_placement(path).unwrap().world;
[w[3][0], w[3][1], w[3][2]]
}
fn near(a: [f64; 3], b: [f64; 3]) {
assert!(a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-9), "{a:?} != {b:?}");
}
#[test]
fn the_first_top_level_part_is_grounded_and_later_parts_and_copies_are_free() {
let mut s = Session::default();
let (a, first) = part(&mut s, "A", [0.0, 0.0, 0.0]);
let created = s.doc.occurrences.iter().find(|o| o.id == first).cloned().unwrap();
assert!(created.grounded, "the first part is grounded");
let (_, second) = part(&mut s, "B", [20.0, 0.0, 0.0]);
assert!(!grounded(&s, second), "a later part starts free");
let instance = run(&mut s, "occurrence.copy", json!({"occurrence": first, "translate": [0, 20, 0]}))["occurrence"].as_u64().unwrap();
assert!(!grounded(&s, instance), "Paste (Instance) of the grounded part is free");
let copy = run(&mut s, "component.paste_new", json!({"component": a, "translate": [0, 40, 0]}));
let copy = s.doc.occurrence_of(copy["component"].as_u64().unwrap()).unwrap().id;
assert!(!grounded(&s, copy), "Paste New of the grounded part is free");
// A part made inside another one is placed in it, not in the design: free.
run(&mut s, "component.activate", json!({"component": a}));
let nested = run(&mut s, "component.create", json!({"name": "Inner"}))["occurrence"].as_u64().unwrap();
assert!(!grounded(&s, nested));
// Several parts may be grounded, or none.
run(&mut s, "occurrence.ground", json!({"occurrence": second, "grounded": true}));
assert!(grounded(&s, first) && grounded(&s, second));
for o in [first, second] {
run(&mut s, "occurrence.ground", json!({"occurrence": o, "grounded": false}));
}
assert!(!s.doc.occurrences.iter().any(|o| o.grounded));
}
#[test]
fn components_from_bodies_ground_only_the_first_part() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "One"}));
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [20, 0, 0], "body_name": "Two", "operation": "new"}));
let made = run(&mut s, "component.from_bodies", json!({"bodies": ["One", "Two"]}));
let occurrence = |i: usize| s.doc.occurrence_of(made["components"][i]["component"].as_u64().unwrap()).unwrap().id;
let (one, two) = (occurrence(0), occurrence(1));
assert!(grounded(&s, one) && !grounded(&s, two));
}
#[test]
fn ungrounded_designs_stay_ungrounded_through_new_parts_and_reopening() {
let mut s = Session::default();
let (_, first) = part(&mut s, "A", [0.0, 0.0, 0.0]);
run(&mut s, "occurrence.ground", json!({"occurrence": first, "grounded": false}));
let (_, second) = part(&mut s, "B", [20.0, 0.0, 0.0]);
assert!(!grounded(&s, first) && !grounded(&s, second), "a part added later grounds nothing");
let back = solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap();
assert!(!back.occurrences.iter().any(|o| o.grounded), "opening a design grounds nothing");
// With every part gone, the next one is the design's first part again.
for c in ["A", "B"] {
run(&mut s, "component.delete", json!({"component": c}));
}
let (_, again) = part(&mut s, "C", [0.0, 0.0, 0.0]);
assert!(grounded(&s, again));
}
#[test]
fn dragging_moves_a_free_part_by_the_grabbed_point_in_one_undo_step() {
let mut s = Session::default();
let (_, base) = part(&mut s, "Base", [0.0, 0.0, 0.0]);
let (_, free) = part(&mut s, "Free", [20.0, 0.0, 0.0]);
let undo = s.undo.len();
let r = run(&mut s, "occurrence.drag", json!({"occurrence": free, "at": [25, 5, 10], "point": [35, 10, 10]}));
assert_eq!(r["moved"], json!([free]));
near(origin(&s, &[free]), [10.0, 5.0, 0.0]);
assert_eq!(s.undo.len(), undo + 1);
assert_eq!(s.undo.last().map(|u| u.label.as_str()), Some("Drag Occurrence"));
// Saved and opened again, it is where it was dragged.
let back = solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap();
let t = back.occurrences.iter().find(|o| o.id == free).unwrap().transform;
near([t[3][0], t[3][1], t[3][2]], [10.0, 5.0, 0.0]);
run(&mut s, "edit.undo", json!({}));
near(origin(&s, &[free]), [0.0, 0.0, 0.0]);
// A grounded part stays where it is.
let e = s.execute("occurrence.drag", &json!({"occurrence": base, "at": [0, 0, 0], "point": [5, 0, 0]})).unwrap_err().to_string();
assert!(e.contains("grounded"), "{e}");
near(origin(&s, &[base]), [0.0, 0.0, 0.0]);
}
#[test]
fn a_drag_captures_its_part_and_leaves_other_pending_moves_pending() {
let mut s = Session::default();
part(&mut s, "Base", [0.0, 0.0, 0.0]);
let (_, a) = part(&mut s, "A", [20.0, 0.0, 0.0]);
let (_, b) = part(&mut s, "B", [40.0, 0.0, 0.0]);
run(&mut s, "occurrence.move", json!({"occurrence": a, "translate": [0, 7, 0], "capture": false}));
run(&mut s, "occurrence.move", json!({"occurrence": b, "translate": [0, 9, 0], "capture": false}));
run(&mut s, "occurrence.drag", json!({"occurrence": a, "at": [0, 0, 0], "point": [3, 0, 0]}));
assert!(!s.pending_moves.contains_key(&a), "the dragged part's position is captured");
let t = s.doc.occurrences.iter().find(|o| o.id == a).unwrap().transform;
near([t[3][0], t[3][1], t[3][2]], [3.0, 7.0, 0.0]);
assert!(s.pending_moves.contains_key(&b), "another part's move stays pending");
near(origin(&s, &[b]), [0.0, 9.0, 0.0]);
run(&mut s, "edit.undo", json!({}));
near(origin(&s, &[a]), [0.0, 7.0, 0.0]);
assert!(s.pending_moves.contains_key(&a), "undo gives the pending move back");
}
#[test]
fn dragging_uses_the_exact_path_of_a_repeated_placement() {
let mut s = Session::default();
part(&mut s, "Base", [0.0, 0.0, 0.0]);
let (asm, first) = part(&mut s, "Asm", [0.0, 0.0, 0.0]);
run(&mut s, "occurrence.move", json!({"occurrence": first, "axis": [0, 0, 1], "angle": "90 deg"}));
run(&mut s, "component.activate", json!({"component": asm}));
let leaf = run(&mut s, "component.create", json!({"name": "Leaf"}))["occurrence"].as_u64().unwrap();
run(&mut s, "solid.box", json!({"length": 2, "width": 2, "height": 2}));
run(&mut s, "component.activate", json!({"component": "root"}));
let second = run(&mut s, "occurrence.copy", json!({"occurrence": first, "translate": [100, 0, 0]}))["occurrence"].as_u64().unwrap();
// The second instance of the assembly moves alone.
run(&mut s, "occurrence.drag", json!({"path": [second], "at": [100, 0, 0], "point": [100, 30, 0]}));
near(origin(&s, &[second]), [100.0, 30.0, 0.0]);
near(origin(&s, &[first]), [0.0, 0.0, 0.0]);
// Its leaf, a definition shared by both instances, moves in world X through that path's
// rotated frame, and so in both.
let before = origin(&s, &[first, leaf]);
run(&mut s, "occurrence.drag", json!({"path": [second, leaf], "at": [100, 30, 0], "point": [110, 30, 0]}));
near(origin(&s, &[second, leaf]), [110.0, 30.0, 0.0]);
near(origin(&s, &[first, leaf]), [before[0] + 10.0, before[1], before[2]]);
// The leaf alone, without the path of the instance it is in, is ambiguous.
let e = s.execute("occurrence.drag", &json!({"occurrence": leaf, "at": [0, 0, 0], "point": [1, 0, 0]})).unwrap_err().to_string();
assert!(e.contains("path"), "{e}");
}
#[test]
fn joints_hold_a_part_to_something_grounded_and_carry_a_free_chain_along() {
let mut s = Session::default();
let (_, base) = part(&mut s, "Base", [0.0, 0.0, 0.0]);
let (_, arm) = part(&mut s, "Arm", [0.0, 0.0, 10.0]);
let at = |o: u64, z: f64| json!({"occurrence": o, "point": [0, 0, z], "z": [0, 0, 1]});
run(&mut s, "joint.create", json!({"type": "revolute", "a": at(base, 10.0), "b": at(arm, 10.0), "flip": true, "name": "Hinge"}));
let e = s.execute("occurrence.drag", &json!({"occurrence": arm, "at": [0, 0, 0], "point": [5, 0, 0]})).unwrap_err().to_string();
assert!(e.contains("grounded") && e.contains("Hinge"), "{e}");
// Two parts jointed to each other and to nothing grounded move together.
let (_, x) = part(&mut s, "X", [50.0, 0.0, 0.0]);
let (_, y) = part(&mut s, "Y", [50.0, 0.0, 10.0]);
run(&mut s, "joint.create", json!({"type": "rigid", "a": at(x, 10.0), "b": at(y, 10.0), "flip": true, "name": "Weld"}));
let (x0, y0) = (origin(&s, &[x]), origin(&s, &[y]));
let r = run(&mut s, "occurrence.drag", json!({"occurrence": y, "at": [0, 0, 0], "point": [0, 25, 0]}));
assert_eq!(r["moved"].as_array().map(Vec::len), Some(2), "{r}");
near(origin(&s, &[x]), [x0[0], x0[1] + 25.0, x0[2]]);
near(origin(&s, &[y]), [y0[0], y0[1] + 25.0, y0[2]]);
// Suppressed, the joint holds nothing.
run(&mut s, "joint.edit", json!({"joint": "Hinge", "suppressed": true}));
run(&mut s, "occurrence.drag", json!({"occurrence": arm, "at": [0, 0, 0], "point": [5, 0, 0]}));
}
#[test]
fn a_drag_and_a_world_move_leave_the_same_design() {
// Whatever follows a moved part (projections, joints, Properties) follows a dragged one.
let build = || {
let mut s = Session::default();
part(&mut s, "Base", [0.0, 0.0, 0.0]);
let (_, free) = part(&mut s, "Free", [20.0, 0.0, 0.0]);
run(&mut s, "sketch.create", json!({"plane": "XY"}));
run(&mut s, "sketch.project", json!({"refs": [{"edge": [25, 0, 0]}]}));
run(&mut s, "sketch.finish", json!({}));
(s, free)
};
let (mut dragged, free) = build();
let (mut moved, _) = build();
run(&mut dragged, "occurrence.drag", json!({"occurrence": free, "at": [25, 0, 0], "point": [25, 15, 0]}));
run(&mut moved, "occurrence.move", json!({"occurrence": free, "space": "world", "translate": [0, 15, 0]}));
assert_eq!(dragged.doc.to_json(), moved.doc.to_json());
let shown = |s: &Session| format!("{:?}", s.world_state().sketches.iter().map(|k| &k.sketch.points).collect::<Vec<_>>());
assert_eq!(shown(&dragged), shown(&moved));
}
#[test]
fn bad_drag_parameters_are_refused() {
let mut s = Session::default();
part(&mut s, "Base", [0.0, 0.0, 0.0]);
let (_, free) = part(&mut s, "Free", [20.0, 0.0, 0.0]);
for p in [
json!({}),
json!({"occurrence": free}),
json!({"occurrence": free, "at": [0, 0, 0]}),
json!({"occurrence": free, "at": [0, 0, 0], "point": [1e300, 0, 0]}),
json!({"occurrence": free, "at": [0, 0, "x"], "point": [0, 0, 0]}),
json!({"path": [], "at": [0, 0, 0], "point": [1, 0, 0]}),
json!({"path": [free, free], "at": [0, 0, 0], "point": [1, 0, 0]}),
json!({"path": [999_999], "at": [0, 0, 0], "point": [1, 0, 0]}),
json!({"occurrence": "nothing", "at": [0, 0, 0], "point": [1, 0, 0]}),
] {
assert!(s.execute("occurrence.drag", &p).is_err(), "{p}");
}
near(origin(&s, &[free]), [0.0, 0.0, 0.0]);
}
#[test]
fn moving_a_grounded_part_needs_saying_so_and_it_stays_grounded() {
let mut s = Session::default();
let (_, base) = part(&mut s, "Base", [0.0, 0.0, 0.0]);
let e = s.execute("occurrence.move", &json!({"occurrence": base, "translate": [5, 0, 0]})).unwrap_err().to_string();
assert!(e.contains("is grounded") && e.contains("occurrence.ground"), "{e}");
run(&mut s, "occurrence.move", json!({"occurrence": base, "translate": [5, 0, 0], "move_grounded": true}));
near(origin(&s, &[base]), [5.0, 0.0, 0.0]);
assert!(grounded(&s, base));
}
/// The solver uses definitions' first paths; a drag through a rotated instance must carry
/// that local change to its jointed peers, including peers that are themselves shared.
#[test]
fn shared_joint_chains_follow_the_selected_frame_and_survive_solving() {
let mut s = Session::default();
part(&mut s, "Base", [0.0, 0.0, 0.0]);
let asm = run(&mut s, "component.create", json!({"name": "Assembly"}));
let first = asm["occurrence"].as_u64().unwrap();
let a = run(&mut s, "component.create", json!({"name": "A"}))["occurrence"].as_u64().unwrap();
run(&mut s, "solid.box", json!({"length": 4, "width": 4, "height": 4}));
run(&mut s, "component.activate", json!({"component": asm["component"]}));
let b = run(&mut s, "component.create", json!({"name": "B"}))["occurrence"].as_u64().unwrap();
run(&mut s, "solid.box", json!({"length": 4, "width": 4, "height": 4, "corner": [10, 0, 0]}));
run(&mut s, "component.activate", json!({"component": "root"}));
let at = |o| json!({"occurrence": o, "point": [0, 0, 0], "z": [0, 0, 1]});
run(&mut s, "joint.create", json!({"type": "revolute", "a": at(a), "b": at(b), "flip": true}));
let second = run(&mut s, "occurrence.copy", json!({"occurrence": first, "translate": [100, 0, 0]}))["occurrence"].as_u64().unwrap();
run(&mut s, "occurrence.move", json!({"occurrence": second, "angle": std::f64::consts::FRAC_PI_2, "origin": [100, 0, 0]}));
run(&mut s, "occurrence.ground", json!({"occurrence": first, "grounded": true}));
let undo = s.undo.len();
let before = s.doc.to_json();
let values = s.doc.assembly.joints[0].values.clone();
// Drag the non-root side, through the second assembly's rotated frame.
run(&mut s, "occurrence.drag", json!({"path": [second, b], "at": [100, 0, 0], "point": [110, 0, 0]}));
near(origin(&s, &[second, a]), [110.0, 0.0, 0.0]);
near(origin(&s, &[second, b]), [110.0, 0.0, 0.0]);
near(origin(&s, &[first, a]), [0.0, -10.0, 0.0]);
near(origin(&s, &[first, b]), [0.0, -10.0, 0.0]);
let dragged = s.doc.to_json();
let solution = run(&mut s, "joint.solve", json!({}));
assert_eq!(solution["conflicts"], json!([]));
assert_eq!(s.doc.to_json(), dragged, "solving preserves the dragged pose");
assert_eq!(s.doc.assembly.joints[0].values, values);
assert_eq!(s.undo.len(), undo + 1);
run(&mut s, "edit.undo", json!({}));
assert_eq!(s.doc.to_json(), before);
}
#[test]
fn a_unique_part_can_drag_a_chain_through_a_shared_peer() {
let mut s = Session::default();
part(&mut s, "Base", [0.0, 0.0, 0.0]);
let (_, free) = part(&mut s, "Free", [20.0, 0.0, 0.0]);
let asm = run(&mut s, "component.create", json!({"name": "Assembly"}));
let first = asm["occurrence"].as_u64().unwrap();
let peer = run(&mut s, "component.create", json!({"name": "Peer"}))["occurrence"].as_u64().unwrap();
run(&mut s, "component.activate", json!({"component": "root"}));
let second = run(&mut s, "occurrence.copy", json!({"occurrence": first, "translate": [100, 0, 0]}))["occurrence"].as_u64().unwrap();
let at = |o| json!({"occurrence": o, "point": [0, 0, 0], "z": [0, 0, 1]});
run(&mut s, "joint.create", json!({"type": "rigid", "a": at(free), "b": at(peer), "flip": true}));
run(&mut s, "occurrence.drag", json!({"occurrence": free, "at": [0, 0, 0], "point": [0, 15, 0]}));
near(origin(&s, &[free]), [0.0, 15.0, 0.0]);
near(origin(&s, &[first, peer]), [0.0, 15.0, 0.0]);
near(origin(&s, &[second, peer]), [100.0, 15.0, 0.0]);
let before = s.doc.to_json();
run(&mut s, "joint.solve", json!({}));
assert_eq!(s.doc.to_json(), before);
}
+1 -1
View File
@@ -758,7 +758,7 @@ mod tests {
let mut s = Session::default();
run(&mut s, "parameters.add", json!({"name": "plate_h", "expression": "12 mm"}));
run(&mut s, "parameters.add", json!({"name": "unused", "expression": "3 mm"}));
let plate = run(&mut s, "component.create", json!({"name": "Plate"}));
let plate = run(&mut s, "component.create", json!({"name": "Plate", "grounded": false}));
let (plate, plate_occ) = (plate["component"].as_u64().unwrap(), plate["occurrence"].as_u64().unwrap());
run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": "plate_h", "body_name": "PlateBody"}));
let pin = run(&mut s, "component.create", json!({"name": "Pin", "parent": plate}));
+5 -1
View File
@@ -27,7 +27,7 @@ pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("component.insert_design", "Insert Design", insert_design).at("SOLID", "ASSEMBLE").icon("component").params(
"path: a .solidcraft design → a new component linked to it, holding its solid bodies as placed in that design; \
name? (default: the design's name); parent?: component id or name (default: the active one); translate?: [x,y,z]; \
reuse?: bool (default false): insert another occurrence of an existing link to the same file",
reuse?: bool (default false): insert a free occurrence, reusing an existing link to the same file when available",
),
CommandSpec::new("component.update_links", "Update Links", update_links).icon("update").params(
"component?: id or name (default: every linked component) — reads the linked designs again and replaces their bodies where they changed",
@@ -181,6 +181,10 @@ fn insert_design(s: &mut Session, p: &Value) -> Result<Value> {
let (design_name, sf) = linked_bodies(&file, s.path.as_deref())?;
let name = str_(p, "name").filter(|n| !n.trim().is_empty()).map(str::to_string).unwrap_or(design_name);
let mut create = json!({"name": name, "activate": false});
// Clipboard insertion uses reuse even for the first paste, which must stay movable.
if p.get("reuse").and_then(Value::as_bool).unwrap_or(false) {
create["grounded"] = json!(false);
}
if let Some(parent) = p.get("parent") {
create["parent"] = parent.clone();
}
@@ -8,7 +8,7 @@ fn close(actual: &Value, expected: f64) {
assert!((actual.as_f64().unwrap() - expected).abs() < 1e-6, "{actual} != {expected}");
}
fn box_in(s: &mut Session, name: &str) -> (u64, u64, String) {
let c = run(s, "component.create", json!({"name": name}));
let c = run(s, "component.create", json!({"name": name, "grounded": false}));
run(s, "solid.box", json!({"length": 10, "width": 10, "height": 10}));
let body = s.model.state().bodies.last().unwrap().name.clone();
(c["component"].as_u64().unwrap(), c["occurrence"].as_u64().unwrap(), body)
@@ -81,7 +81,7 @@ fn body_reassignment_offsets_preserve_world_properties() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10}));
let body = s.model.state().bodies.first().unwrap().name.clone();
let c = run(&mut s, "component.create", json!({"name": "Moved"}));
let c = run(&mut s, "component.create", json!({"name": "Moved", "grounded": false}));
run(&mut s, "occurrence.move", json!({"occurrence": c["occurrence"], "translate": [100, 0, 0]}));
run(&mut s, "component.move_bodies", json!({"bodies": [body], "component": c["component"]}));
let r = run(&mut s, "component.properties", json!({"component": c["component"]}));
@@ -24,6 +24,28 @@ fn volume(s: &mut Session) -> f64 {
run(s, "inspect.measure", json!({}))["total"]["volume_mm3"].as_f64().unwrap()
}
#[test]
fn reused_design_insertion_is_free_even_before_a_link_exists() {
let dir = folder("reuse-grounding");
let file = dir.join("part.solidcraft");
part(&file, 30);
let mut s = Session::default();
for count in 1..=2 {
let inserted = run(&mut s, "component.insert_design", json!({"path": file, "reuse": true}));
let occurrence = inserted["occurrence"].as_u64().unwrap();
assert!(!s.doc.occurrences.iter().find(|o| o.id == occurrence).unwrap().grounded);
assert_eq!(s.doc.links.len(), 1);
assert_eq!(s.undo.len(), count, "each paste is one undo step");
run(&mut s, "occurrence.move", json!({"occurrence": occurrence, "translate": [10, 0, 0]}));
run(&mut s, "edit.undo", json!({}));
}
let mut ordinary = Session::default();
let inserted = run(&mut ordinary, "component.insert_design", json!({"path": file}));
let occurrence = inserted["occurrence"].as_u64().unwrap();
assert!(ordinary.doc.occurrences.iter().find(|o| o.id == occurrence).unwrap().grounded);
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn relink_repairs_a_moved_source_for_every_instance_in_one_undo_step() {
let dir = folder("moved");
@@ -117,6 +139,7 @@ fn relink_keeps_joint_definitions_and_reports_solver_diagnostics() {
let inserted = run(&mut s, "component.insert_design", json!({"path": original}));
let component = inserted["component"].as_u64().unwrap();
let occurrence = inserted["occurrence"].as_u64().unwrap();
run(&mut s, "occurrence.ground", json!({"occurrence": occurrence, "grounded": false}));
// Explicit points can keep solving after the source changes, even if the intended
// connection no longer exists. The source-change warning must still be present.
run(
@@ -34,7 +34,7 @@ fn recorded_assignment_preserves_sample_joined_material_and_timeline() {
fn transformed_pending_destination_repeated_transfer_preserves_world_pose() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10,"body_name":"Block"}));
let created = run(&mut s, "component.create", json!({"name":"Destination"}));
let created = run(&mut s, "component.create", json!({"name":"Destination","grounded":false}));
let to = created["component"].as_u64().unwrap();
let occurrence = created["occurrence"].as_u64().unwrap();
run(&mut s, "occurrence.move", json!({"occurrence":occurrence,"translate":[30,20,0],"angle":90,"axis":[0,0,1],"capture":false}));
@@ -141,7 +141,7 @@ fn transferred_thread_follows_body_frame_without_changing_world_axis() {
run(&mut s, "solid.cylinder", json!({"radius":4,"height":20,"body_name":"Shaft"}));
run(&mut s, "solid.thread", json!({"face":[4,0,10],"designation":"M8"}));
let before = s.world_state().threads[0].clone();
let c = run(&mut s, "component.create", json!({"name":"Threaded"}));
let c = run(&mut s, "component.create", json!({"name":"Threaded","grounded":false}));
let to = c["component"].as_u64().unwrap();
let occurrence = c["occurrence"].as_u64().unwrap();
run(&mut s, "occurrence.move", json!({"occurrence":occurrence,"translate":[30,20,10],"axis":[1,0,0],"angle":90,"capture":true}));
@@ -199,7 +199,7 @@ fn deleting_external_transfer_source_refuses_without_dangling_archive() {
#[test]
fn earlier_selected_cylinder_emboss_keeps_source_frame_after_transfer_and_edit() {
let mut s = Session::default();
let source = run(&mut s, "component.create", json!({"name":"Source"}));
let source = run(&mut s, "component.create", json!({"name":"Source","grounded":false}));
let occurrence = source["occurrence"].as_u64().unwrap();
run(&mut s, "solid.cylinder", json!({"radius":10,"height":20,"body_name":"Sleeve"}));
run(&mut s, "occurrence.move", json!({"occurrence":occurrence,"translate":[30,0,0],"capture":true}));
@@ -227,7 +227,7 @@ fn earlier_selected_cylinder_emboss_keeps_source_frame_after_transfer_and_edit()
#[test]
fn transfer_undo_redo_restores_original_pending_source_world_pose() {
let mut s = Session::default();
let created = run(&mut s, "component.create", json!({"name":"Source"}));
let created = run(&mut s, "component.create", json!({"name":"Source","grounded":false}));
let source = created["component"].as_u64().unwrap();
let occurrence = created["occurrence"].as_u64().unwrap();
run(&mut s, "solid.box", json!({"length":2,"width":2,"height":2,"body_name":"Block"}));
@@ -248,7 +248,7 @@ fn transfer_undo_redo_restores_original_pending_source_world_pose() {
#[test]
fn transfer_checkpoint_restores_pending_world_pose_and_history() {
let mut s = Session::default();
let created = run(&mut s, "component.create", json!({"name":"Source"}));
let created = run(&mut s, "component.create", json!({"name":"Source","grounded":false}));
let source = created["component"].as_u64().unwrap();
let occurrence = created["occurrence"].as_u64().unwrap();
run(&mut s, "solid.box", json!({"length":2,"width":2,"height":2,"body_name":"Block"}));
@@ -214,7 +214,7 @@ fn certified_side_transfer_preserves_geometry_and_unsupported_taper_refuses_atom
run(&mut s, "sketch.finish", json!({}));
let source = run(&mut s, "solid.extrude", json!({"distance":10,"body_name":"Round"}))["feature"].as_u64().unwrap();
paint(&mut s, "Round", json!([10, 0, 5]));
let created = run(&mut s, "component.create", json!({"name":"Placed"}));
let created = run(&mut s, "component.create", json!({"name":"Placed","grounded":false}));
let destination = created["component"].as_u64().unwrap();
let occurrence = created["occurrence"].as_u64().unwrap();
run(&mut s, "occurrence.move", json!({"occurrence":occurrence,"translate":[30,20,0],"angle":90,"axis":[0,0,1],"capture":false}));
+1 -1
View File
@@ -84,7 +84,7 @@ fn revolute_cannot_cross_a_blocker_between_clear_endpoints() {
let mut s = Session::default();
for (name, corner) in [("Arm", [20, -2, -2]), ("Blocker", [-2, 20, -2])] {
run(&mut s, "component.activate", json!({"component": 0}));
run(&mut s, "component.create", json!({"name": name}));
run(&mut s, "component.create", json!({"name": name, "grounded": false}));
run(&mut s, "solid.box", json!({"length": 4, "width": 4, "height": 4, "corner": corner}));
}
run(
+1 -1
View File
@@ -340,7 +340,7 @@ fn redefine_cannot_publish_a_second_transfer_that_breaks_existing_ownership() {
fn redefine_maps_placement_inputs_in_the_feature_owner_frame() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 4, "width": 4, "height": 4, "name": "RootBox", "body_name": "RootBody"}));
let owner = run(&mut s, "component.create", json!({"name": "Owner"}));
let owner = run(&mut s, "component.create", json!({"name": "Owner", "grounded": false}));
run(&mut s, "solid.box", json!({"length": 4, "width": 4, "height": 4, "corner": [10, 0, 0], "name": "OwnedBox", "body_name": "OwnedBody"}));
run(&mut s, "solid.fillet", json!({"edges": [[12, 0, 4]], "radius": 0.5, "body": "OwnedBody", "name": "OwnerRound"}));
run(&mut s, "occurrence.move", json!({"occurrence": owner["occurrence"], "translate": [100, 0, 0]}));
+2 -2
View File
@@ -84,7 +84,7 @@ fn internal_capture_does_not_bypass_missing_ambiguous_or_invalid_path_picks() {
fn generated_profiles_and_press_pull_convert_rotated_occurrence_picks_once() {
for hole in [false, true] {
let mut s = Session::default();
let component = run(&mut s, "component.create", json!({"name":"Arm"}));
let component = run(&mut s, "component.create", json!({"name":"Arm","grounded":false}));
let cid = component["component"].as_u64().unwrap();
let occ = component["occurrence"].as_u64().unwrap();
run(&mut s, "solid.box", json!({"length":40,"width":30,"height":20,"name":"Base","body_name":"Stock"}));
@@ -147,7 +147,7 @@ fn raised_half(s: &mut Session, body: &str) {
#[test]
fn legacy_internal_capture_refuses_an_uncaptured_source_in_another_component() {
let mut s = Session::default();
let component = run(&mut s, "component.create", json!({"name":"Source"}));
let component = run(&mut s, "component.create", json!({"name":"Source","grounded":false}));
let occurrence = component["occurrence"].as_u64().unwrap();
// A face Offset Face raised has no construction certificate or exact history name yet:
// the legacy path.
+5 -5
View File
@@ -5,7 +5,7 @@ use crate::Session;
#[test]
fn grouped_face_auto_operation_keeps_implicit_and_explicit_participants() {
let mut s = Session::default();
let part = run(&mut s, "component.create", json!({"name":"Part"}));
let part = run(&mut s, "component.create", json!({"name":"Part","grounded":false}));
run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10,"body_name":"Stock"}));
run(&mut s, "component.activate", json!({"component":0}));
for offset in [0., 30.] {
@@ -417,7 +417,7 @@ fn cylinder_cross_component_emboss_keeps_source_owner_and_placements() {
for (pick_offset, auto) in [(0., false), (80., false), (0., true), (80., true)] {
let run = |s: &mut Session, id: &str, p: Value| run(s, id, auto_emboss_params(p, auto && matches!(id, "solid.emboss" | "timeline.redefine")));
let mut s = Session::default();
let a = run(&mut s, "component.create", json!({"name":"Source"}))["component"].as_u64().unwrap();
let a = run(&mut s, "component.create", json!({"name":"Source","grounded":false}))["component"].as_u64().unwrap();
run(&mut s, "solid.cylinder", json!({"radius":10,"height":20,"body_name":"Sleeve"}));
let source_feature = s.model.state().bodies.first().unwrap().feature;
let source_names = solidcraft_doc::naming::face_names(s.model.state().body("Sleeve").unwrap());
@@ -532,7 +532,7 @@ fn check_active_second_source(auto: bool) {
fn cylinder_pending_leaf_or_ancestor_positions_require_capture_atomically() {
for (ancestor, auto) in [(false, false), (true, false), (false, true), (true, true)] {
let mut s = Session::default();
let parent = run(&mut s, "component.create", json!({"name":"Parent"}))["occurrence"].as_u64().unwrap();
let parent = run(&mut s, "component.create", json!({"name":"Parent","grounded":false}))["occurrence"].as_u64().unwrap();
let leaf = run(&mut s, "component.create", json!({"name":"Source"}))["occurrence"].as_u64().unwrap();
run(&mut s, "solid.cylinder", json!({"radius":10,"height":20,"body_name":"Sleeve"}));
run(&mut s, "sketch.create", json!({"plane":{"origin":[0,10,0],"x_dir":[-1,0,0],"y_dir":[0,0,1]},"name":"Badge"}));
@@ -561,7 +561,7 @@ fn cylinder_pending_third_component_profile_requires_capture_atomically() {
fn check_pending_third_profile(auto: bool) {
let mut s = Session::default();
run(&mut s, "solid.cylinder", json!({"radius":10,"height":20,"body_name":"Sleeve"}));
let profile_occurrence = run(&mut s, "component.create", json!({"name":"ProfileOwner"}))["occurrence"].as_u64().unwrap();
let profile_occurrence = run(&mut s, "component.create", json!({"name":"ProfileOwner","grounded":false}))["occurrence"].as_u64().unwrap();
run(&mut s, "sketch.create", json!({"plane":{"origin":[0,10,0],"x_dir":[-1,0,0],"y_dir":[0,0,1]},"name":"Badge"}));
run(&mut s, "sketch.rectangle.two_point", json!({"p0":[-2,5],"p1":[2,10]}));
run(&mut s, "sketch.finish", json!({}));
@@ -594,7 +594,7 @@ fn check_body_offset(auto: bool) {
let mut s = Session::default();
run(&mut s, "solid.cylinder", json!({"radius":10,"height":20,"body_name":"Sleeve"}));
let before = exact_cylinder_volume(&s);
let holder = run(&mut s, "component.create", json!({"name":"Holder"}))["component"].as_u64().unwrap();
let holder = run(&mut s, "component.create", json!({"name":"Holder","grounded":false}))["component"].as_u64().unwrap();
run(&mut s, "occurrence.move", json!({"occurrence":"Holder:1","translate":[0,50,0]}));
std::sync::Arc::make_mut(&mut s.doc).body_components.insert("Sleeve".into(), holder);
std::sync::Arc::make_mut(&mut s.doc)
+1 -1
View File
@@ -794,7 +794,7 @@ mod tests {
#[test]
fn preview_saves_do_not_build_cold_placements_or_persist_transient_edits() {
let mut s = Session::default();
let created = s.execute("component.create", &json!({"name":"PreviewPart"})).unwrap();
let created = s.execute("component.create", &json!({"name":"PreviewPart","grounded":false})).unwrap();
let occurrence = created["occurrence"].as_u64().unwrap();
s.execute("solid.box", &json!({"length":2,"width":3,"height":4,"body_name":"Block","operation":"new"})).unwrap();
for body in &s.model.state().bodies {
+1 -1
View File
@@ -595,7 +595,7 @@ fn bare_profile_numbers_are_in_the_design_units() {
fn members_follow_a_frame_sketch_in_another_component() {
let mut s = Session::default();
rectangle_frame(&mut s);
let c = run(&mut s, "component.create", json!({"name": "Frame parts"}))["component"].as_u64().unwrap();
let c = run(&mut s, "component.create", json!({"name": "Frame parts", "grounded": false}))["component"].as_u64().unwrap();
let o = s.doc.occurrence_of(c).unwrap().id;
run(&mut s, "occurrence.move", json!({"occurrence": o, "translate": [0, 0, 100], "capture": true}));
// Made in the moved component, the members still lie on the root's frame sketch.
@@ -26,7 +26,7 @@ fn disc(s: &mut Session, name: &str, x: f64, r: &str) {
/// The disc alone, on no joint.
fn loose_disc(s: &mut Session, name: &str, x: f64, r: &str) {
run(s, "component.activate", json!({"component": "root"}));
run(s, "component.create", json!({"name": name}));
run(s, "component.create", json!({"name": name, "grounded": false}));
run(s, "solid.cylinder", json!({"base": [x, 0, 0], "axis": [0, 0, 1], "radius": r, "height": 5}));
}
@@ -123,7 +123,7 @@ fn rack() -> Session {
disc(&mut s, "Pinion", 0.0, "8 mm");
for (name, corner, length) in [("Rack", [-20.0, -14.0, 0.0], 40.0), ("Stop", [30.0, -14.0, 0.0], 4.0)] {
run(&mut s, "component.activate", json!({"component": "root"}));
run(&mut s, "component.create", json!({"name": name}));
run(&mut s, "component.create", json!({"name": name, "grounded": false}));
run(&mut s, "solid.box", json!({"length": length, "width": 4, "height": 4, "corner": corner}));
}
pin(&mut s, "slider", "Rail", "Base", "Rack", Vec3::new(0.0, -10.0, 0.0), Vec3::X);
+3 -3
View File
@@ -49,7 +49,7 @@ fn deleted_joint_ids_are_not_reused_after_reopening() {
/// at y = 33, z = 4.
fn leaf(s: &mut Session, name: &str) {
run(s, "component.activate", json!({"component": "root"}));
run(s, "component.create", json!({"name": name}));
run(s, "component.create", json!({"name": name, "grounded": false}));
run(s, "solid.box", json!({"length": 40, "width": 30, "height": 3, "corner": [0, 0, 0]}));
run(s, "solid.cylinder", json!({"base": [0, 33, 4], "axis": [1, 0, 0], "radius": 4, "height": 40, "operation": "new"}));
}
@@ -128,7 +128,7 @@ fn two_cubes() -> Session {
let mut s = Session::default();
for (name, x) in [("A", 0), ("B", 20)] {
run(&mut s, "component.activate", json!({"component": "root"}));
run(&mut s, "component.create", json!({"name": name}));
run(&mut s, "component.create", json!({"name": name, "grounded": false}));
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [x, 0, 0]}));
}
let o = occ(&s, "A");
@@ -369,7 +369,7 @@ fn interference_box_sweep_matches_every_pair_check() {
/// A small block in a new component: a part of a linkage, where only its joints matter.
pub(super) fn part(s: &mut Session, name: &str, corner: [f64; 3]) {
run(s, "component.activate", json!({"component": "root"}));
run(s, "component.create", json!({"name": name}));
run(s, "component.create", json!({"name": name, "grounded": false}));
run(s, "solid.box", json!({"length": 2, "width": 2, "height": 2, "corner": corner}));
}
+1 -1
View File
@@ -11,7 +11,7 @@ use crate::Session;
/// A part made of one box (`size` from `corner`) in a new component.
fn block(s: &mut Session, name: &str, corner: [f64; 3], size: [f64; 3]) {
run(s, "component.activate", json!({"component": "root"}));
run(s, "component.create", json!({"name": name}));
run(s, "component.create", json!({"name": name, "grounded": false}));
run(s, "solid.box", json!({"length": size[0], "width": size[1], "height": size[2], "corner": corner}));
}
+10 -10
View File
@@ -62,7 +62,7 @@ enum JoinSelection {
fn joined_motion_part(authoring: PartAuthoring, selection: JoinSelection) -> Session {
let mut s = Session::default();
if matches!(authoring, PartAuthoring::InComponent) {
run(&mut s, "component.create", json!({"name":"Rotor"}));
run(&mut s, "component.create", json!({"name":"Rotor","grounded":false}));
}
run(&mut s, "solid.box", json!({"length":10,"width":8,"height":6,"body_name":"Part","name":"Base"}));
run(&mut s, "select.set", json!({"items":[]}));
@@ -75,12 +75,12 @@ fn joined_motion_part(authoring: PartAuthoring, selection: JoinSelection) -> Ses
run(&mut s, "solid.box", p);
if !matches!(authoring, PartAuthoring::InComponent) {
let parent = if matches!(authoring, PartAuthoring::RootThenNestedTransfer) {
run(&mut s, "component.create", json!({"name":"Group","activate":false}));
run(&mut s, "component.create", json!({"name":"Group","activate":false,"grounded":false}));
"Group"
} else {
"root"
};
run(&mut s, "component.create", json!({"name":"Rotor","parent":parent,"activate":false}));
run(&mut s, "component.create", json!({"name":"Rotor","parent":parent,"activate":false,"grounded":false}));
run(&mut s, "component.move_bodies", json!({"bodies":["Part"],"component":"Rotor"}));
}
let driven = if matches!(authoring, PartAuthoring::RootThenNestedTransfer) { "Group" } else { "Rotor" };
@@ -95,7 +95,7 @@ fn attach_motion_rotor(s: &mut Session, component: &str, joint: &str) {
fn unrelated_motion_rotor(s: &mut Session) {
run(s, "component.activate", json!({"component":"root"}));
run(s, "component.create", json!({"name":"Unrelated"}));
run(s, "component.create", json!({"name":"Unrelated","grounded":false}));
run(s, "solid.box", json!({"length":2,"width":2,"height":2,"corner":[100,100,100],"body_name":"Remote"}));
attach_motion_rotor(s, "Unrelated", "Other turn");
}
@@ -179,7 +179,7 @@ fn joint_pose_joined_cylinders_keep_mesh_cells_at_unseen_angles() {
"solid.cylinder",
json!({"radius":2.8,"height":30,"base":[0,36,0],"axis":[0.981627183447664,0,0.1908089953765448],"operation":"join","targets":["Crank"]}),
);
run(&mut s, "component.create", json!({"name":"Crank","activate":false}));
run(&mut s, "component.create", json!({"name":"Crank","activate":false,"grounded":false}));
run(&mut s, "component.move_bodies", json!({"bodies":["Crank"],"component":"Crank"}));
run(&mut s, "joint.as_built", json!({"type":"revolute","name":"Crank","a":0,"b":"Crank","at":{"occurrence":0,"point":[0,0,0],"z":[0,1,0]}}));
assert_joint_only_moves(&mut s, "Crank");
@@ -258,7 +258,7 @@ fn joint_pose_combine_ignores_unrelated_motion() {
/// with the target preserves that volume and must preserve the computed definition too.
fn cross_component_motion_cut(move_tool_with_target: bool, kind: &str) -> Session {
let mut s = Session::default();
run(&mut s, "component.create", json!({"name":"MovingTarget"}));
run(&mut s, "component.create", json!({"name":"MovingTarget","grounded":false}));
run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10,"body_name":"Target"}));
run(&mut s, "component.activate", json!({"component":"root"}));
if move_tool_with_target {
@@ -280,7 +280,7 @@ fn motion_target_volume(s: &Session) -> f64 {
fn motion_combine_cut(kind: &str, together: bool) -> Session {
let mut s = Session::default();
run(&mut s, "component.create", json!({"name":"MovingTarget"}));
run(&mut s, "component.create", json!({"name":"MovingTarget","grounded":false}));
run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10,"body_name":"Target"}));
run(&mut s, "component.activate", json!({"component":"root"}));
run(&mut s, "component.create", json!({"name":"Tool"}));
@@ -440,7 +440,7 @@ fn joint_pose_undo_and_redo_match_fresh_geometry() {
fn joint_pose_geometry_edit_invalidates_the_warm_definition_mesh() {
let mut s = Session::default();
run(&mut s, "parameters.add", json!({"name":"span","expression":"10 mm"}));
run(&mut s, "component.create", json!({"name":"Rotor"}));
run(&mut s, "component.create", json!({"name":"Rotor","grounded":false}));
run(&mut s, "solid.box", json!({"length":"span","width":8,"height":6,"body_name":"Part"}));
attach_motion_rotor(&mut s, "Rotor", "Turn");
run(&mut s, "joint.drive", json!({"joint":"Turn","value":5}));
@@ -563,7 +563,7 @@ fn contact_check_finds_plates_crossing_face_to_face() {
fn contact_uses_preserved_body_placements_after_component_reassignment() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Transferred"}));
let component = run(&mut s, "component.create", json!({"name": "Carrier"}));
let component = run(&mut s, "component.create", json!({"name": "Carrier", "grounded": false}));
run(&mut s, "occurrence.move", json!({"occurrence": component["occurrence"], "translate": [100, 0, 0]}));
run(&mut s, "component.move_bodies", json!({"bodies": ["Transferred"], "component": component["component"]}));
run(&mut s, "component.activate", json!({"component": "root"}));
@@ -581,7 +581,7 @@ fn contact_uses_preserved_body_placements_after_component_reassignment() {
fn automatic_explode_centres_follow_reassigned_bodies() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Transferred"}));
let component = run(&mut s, "component.create", json!({"name": "Carrier"}));
let component = run(&mut s, "component.create", json!({"name": "Carrier", "grounded": false}));
run(&mut s, "occurrence.move", json!({"occurrence": component["occurrence"], "translate": [100, 0, 0]}));
run(&mut s, "component.move_bodies", json!({"bodies": ["Transferred"], "component": component["component"]}));
run(&mut s, "component.activate", json!({"component": "root"}));
+1 -1
View File
@@ -6,7 +6,7 @@ fn run(s: &mut Session, command: &str, p: Value) -> Value {
s.execute(command, &p).unwrap()
}
fn created(s: &mut Session, name: &str) -> (u64, u64) {
let c = run(s, "component.create", json!({"name": name}));
let c = run(s, "component.create", json!({"name": name, "grounded": false}));
(c["component"].as_u64().unwrap(), c["occurrence"].as_u64().unwrap())
}
fn near(value: &Value, expected: f64) {
+2 -1
View File
@@ -396,7 +396,8 @@ fn insert(s: &mut Session, p: &Value) -> Result<Value> {
};
let name = str_(p, "name").map(str::to_string).unwrap_or_else(|| part_name(&family, &size, &sh));
let active = s.active_component;
let c = run(s, "component.create", json!({"name": name}))?;
// Hardware is fitted to other parts: it starts free even as the design's first part (#2804).
let c = run(s, "component.create", json!({"name": name, "grounded": false}))?;
let (comp, occ) = (c["component"].as_u64().unwrap_or(0), c["occurrence"].as_u64().unwrap_or(0));
build(s, &sh)?;
s.doc_mut().parts.insert(comp, solidcraft_doc::StandardPart { family: family.clone(), size: size.clone(), length: shape_len(&sh) });
@@ -7,7 +7,7 @@ fn save(s: &mut Session, patch: Value) {
#[test]
fn settings_history_preserves_geometry_cache_and_dirty_state() {
let mut s = Session::default();
let component = s.execute("component.create", &json!({"name":"Part"})).unwrap();
let component = s.execute("component.create", &json!({"name":"Part","grounded":false})).unwrap();
s.execute("solid.box", &json!({"length":10,"width":20,"height":5})).unwrap();
s.execute("component.activate", &json!({"component":0})).unwrap();
s.execute("occurrence.move", &json!({"occurrence":component["occurrence"],"translate":[30,0,0]})).unwrap();
+2 -2
View File
@@ -124,7 +124,7 @@ fn rotated_component_bend_picks_follow_connected_panels() {
let mut sizes = Vec::new();
for rotated in [false, true] {
let mut s = Session::default();
let component = run(&mut s, "component.create", json!({"name": "Bent part"}));
let component = run(&mut s, "component.create", json!({"name": "Bent part", "grounded": false}));
rule(&mut s, "Test", 2.0, 0.5, 1.0, 0.5);
let corners = bent_section(&mut s, &[(0.0, 0.0), (60.0, 0.0), (60.0, 40.0), (90.0, 40.0)], 2.0, 1.0, 100.0);
// The remote bend is picked on its tangent edge, where it meets the web.
@@ -145,7 +145,7 @@ fn rotated_component_bend_picks_follow_connected_panels() {
#[test]
fn rotated_component_disconnected_bend_reports_world_point() {
let mut s = Session::default();
let component = run(&mut s, "component.create", json!({"name": "Bent part"}));
let component = run(&mut s, "component.create", json!({"name": "Bent part", "grounded": false}));
rule(&mut s, "Test", 2.0, 0.5, 1.0, 0.5);
section(&mut s, &[(0.0, 0.0), (60.0, 0.0), (60.0, 40.0), (90.0, 40.0)], 2.0, 100.0);
run(&mut s, "occurrence.move", json!({"occurrence": component["occurrence"], "axis": [1, 0, 0], "angle": "90 deg"}));
+4 -4
View File
@@ -434,7 +434,7 @@ fn sheet_pocket_transfer_keeps_local_depth_world_pose_and_history() {
run(&mut s, "solid.extrude", json!({"sketch":"Pocket","distance":1,"operation":"cut"}));
let name = s.model.state().sheets[0].body.clone();
check(&s, &name, 0, Vec3::ZERO, Vec3::Y, false);
let destination = run(&mut s, "component.create", json!({"name":"Machined","parent":0}));
let destination = run(&mut s, "component.create", json!({"name":"Machined","parent":0,"grounded":false}));
let component = destination["component"].as_u64().unwrap();
let occurrence = destination["occurrence"].as_u64().unwrap();
run(&mut s, "occurrence.move", json!({"occurrence":occurrence,"translate":[30,20,10],"axis":[1,0,0],"angle":"90 deg","capture":true}));
@@ -2269,7 +2269,7 @@ fn partial_depth_intersect_flipped_tool_and_transferred_sheet_keep_depth_side_an
run(&mut s, "solid.extrude", json!({"sketch":"RetainDepth","distance":1,"start_offset":0.5,"operation":"intersect","name":"RetainSlab"}));
let before = s.world_state().bodies[0].mesh().bounds();
let name = s.model.state().sheets[0].body.clone();
let destination = run(&mut s, "component.create", json!({"name":"Destination","parent":0}));
let destination = run(&mut s, "component.create", json!({"name":"Destination","parent":0,"grounded":false}));
let owner = destination["component"].as_u64().unwrap();
let occurrence = destination["occurrence"].as_u64().unwrap();
run(&mut s, "occurrence.move", json!({"occurrence":occurrence,"translate":[30,20,10],"axis":[1,0,0],"angle":"90 deg","capture":true}));
@@ -2315,7 +2315,7 @@ fn partial_depth_intersect_full_gauge_in_rotated_frames_does_not_invent_pockets(
let mut s = Session::default();
base(&mut s, 100.0, 60.0);
let name = s.model.state().sheets[0].body.clone();
let destination = run(&mut s, "component.create", json!({"name":"Destination","parent":0}));
let destination = run(&mut s, "component.create", json!({"name":"Destination","parent":0,"grounded":false}));
let owner = destination["component"].as_u64().unwrap();
run(
&mut s,
@@ -2493,7 +2493,7 @@ fn partial_depth_intersect_sub_extrusion_resolution_layers_refuse_atomically() {
let mut s = Session::default();
base(&mut s, 100.0, 60.0);
let name = s.model.state().sheets[0].body.clone();
let destination = run(&mut s, "component.create", json!({"name":"Far","parent":0}));
let destination = run(&mut s, "component.create", json!({"name":"Far","parent":0,"grounded":false}));
let owner = destination["component"].as_u64().unwrap();
run(
&mut s,
@@ -123,7 +123,7 @@ fn wire_frame_hostile_inputs_rollback_full_session() {
fn wire_frame_include_other_component_refresh_break_lost_and_reopen() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10}));
let component = run(&mut s, "component.create", json!({"name":"Receiver"}));
let component = run(&mut s, "component.create", json!({"name":"Receiver","grounded":false}));
let occurrence = component["occurrence"].as_u64().unwrap();
run(&mut s, "occurrence.move", json!({"occurrence":occurrence,"translate":[20,0,0]}));
run(&mut s, "sketch.create", json!({"plane":"XY","name":"Include target"}));
@@ -235,7 +235,7 @@ fn wire_frame_cross_owner_sweep_follows_world_path_edit_undo_and_reopen() {
run(&mut s, "sketch.options", json!({"sketch_3d":true}));
let wire = run(&mut s, "sketch.line3d", json!({"points":[[17,32,45],[17,32,55]]}))["curves"][0].as_str().unwrap().to_string();
run(&mut s, "sketch.finish", json!({}));
let owner = run(&mut s, "component.create", json!({"name":"Sweep owner"}))["occurrence"].as_u64().unwrap();
let owner = run(&mut s, "component.create", json!({"name":"Sweep owner","grounded":false}))["occurrence"].as_u64().unwrap();
run(&mut s, "occurrence.move", json!({"occurrence":owner,"axis":[0,0,1],"angle":"90 deg","translate":[20,30,40]}));
let profile = run(&mut s, "sketch.create", json!({"plane":"XY","offset":5,"name":"Profile"}))["sketch"].as_u64().unwrap();
run(&mut s, "sketch.circle.center", json!({"center":[2,3],"radius":2}));
@@ -264,7 +264,7 @@ fn wire_frame_cross_owner_sweep_follows_world_path_edit_undo_and_reopen() {
#[test]
fn wire_frame_three_owner_sweep_profile_move_invalidates_warm_cache() {
let mut s = Session::default();
let profile_owner = run(&mut s, "component.create", json!({"name":"Profile owner","parent":0}));
let profile_owner = run(&mut s, "component.create", json!({"name":"Profile owner","parent":0,"grounded":false}));
let profile_occurrence = profile_owner["occurrence"].as_u64().unwrap();
run(&mut s, "occurrence.move", json!({"occurrence":profile_occurrence,"translate":[4,7,5]}));
let profile = run(&mut s, "sketch.create", json!({"plane":"XY","name":"Foreign profile"}))["sketch"].as_u64().unwrap();
@@ -116,7 +116,7 @@ fn live_text_edit_rejects_bad_values_and_leaves_the_design_as_it_was() {
#[test]
fn text_move_in_a_rotated_component_preserves_font_and_local_coordinates() {
let mut s = Session::default();
let component = run(&mut s, "component.create", json!({"name":"Label"}));
let component = run(&mut s, "component.create", json!({"name":"Label","grounded":false}));
run(&mut s, "occurrence.move", json!({"occurrence":component["occurrence"],"translate":[30,20,10],"axis":[0,0,1],"angle":90}));
let created = run(&mut s, "sketch.create", json!({"plane":"XY"}));
let sid = created["sketch"].as_u64().unwrap();
+1 -1
View File
@@ -383,7 +383,7 @@ fn urdf_names_are_unique_across_links_joints_and_files() {
let mut s = Session::default();
for name in ["Arm", "base", "Arm_link1"] {
run(&mut s, "component.activate", json!({"component": "root"}));
run(&mut s, "component.create", json!({"name": name}));
run(&mut s, "component.create", json!({"name": name, "grounded": false}));
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [0, 0, 0]}));
}
run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [50, 50, 0]}));
+1 -1
View File
@@ -308,7 +308,7 @@ mod tests {
#[test]
fn normal_to_selection_uses_the_world_normal_of_a_moved_component() {
let mut s = Session::default();
s.execute("component.create", &json!({"name":"Part"})).unwrap();
s.execute("component.create", &json!({"name":"Part","grounded":false})).unwrap();
s.execute("solid.box", &json!({"length":40,"width":30,"height":20})).unwrap();
s.execute("occurrence.move", &json!({"occurrence":"Part:1","translate":[50,20,10],"axis":[1,2,3],"angle":"37 deg"})).unwrap();
let state = s.world_state();