Timeline: joints in feature folders (#8306) (#8316)

* claim: #8306

* Timeline: joints in feature folders (#8306)

timeline.group and timeline.move_to_group take joints as well as features, so
hundreds of joints fold into one folder tile. A closed folder shows one tile
whatever it holds; opening it shows its features and joints in both the bottom
and left History. Suppress, delete and ungroup on a folder reach its joints too
(one undo step), deleting a joint removes it from its folder, and an unknown
joint id or name is an error. Saved with the design; designs without joint
folders read and write as before.
This commit is contained in:
raykholo
2026-10-29 15:59:18 +00:00
committed by GitHub
parent c32f6b60eb
commit 3cdfec798f
16 changed files with 588 additions and 64 deletions
+3
View File
@@ -382,6 +382,9 @@ fn unlock(s: &mut Session, p: &Value) -> Result<Value> {
}
let doc = s.doc_mut();
doc.assembly.joints.retain(|j| !gone.contains(&j.id));
for group in &mut doc.timeline_groups {
group.joints.retain(|id| !gone.contains(id));
}
doc.assembly.links.retain(|l| !gone.contains(&l.a) && !gone.contains(&l.b));
s.visibility.hidden_joints.retain(|h| !gone.contains(h));
Ok(json!({"component": id, "joints": gone}))
@@ -267,6 +267,17 @@ pub(super) fn clone_assembly(
features: &BTreeMap<u64, u64>,
bodies: &BTreeMap<String, String>,
) -> Result<Vec<String>> {
clone_assembly_with_joint_ids(doc, source, occurrences, features, bodies).map(|(messages, _)| messages)
}
/// The copied joint identity map is also needed when extracting saved timeline folders.
pub(super) fn clone_assembly_with_joint_ids(
doc: &mut Document,
source: &Document,
occurrences: &BTreeMap<u64, u64>,
features: &BTreeMap<u64, u64>,
bodies: &BTreeMap<String, String>,
) -> Result<(Vec<String>, BTreeMap<u64, u64>)> {
budget(&source.assembly)?;
budget(&doc.assembly)?;
validate_map(occurrences)?;
@@ -419,7 +430,7 @@ pub(super) fn clone_assembly(
budget(&a)?;
doc.next_id = item_id.checked_add(1).ok_or_else(|| invalid("assembly identifier space is exhausted"))?;
doc.assembly = a;
Ok(messages)
Ok((messages, joints))
}
#[cfg(test)]
+10 -3
View File
@@ -357,8 +357,6 @@ fn part_design(s: &Session, sc: &Scope, stem: &str, cmd: &str) -> Result<(Sessio
for g in &mut part.timeline_groups {
g.features.retain(|f| sc.inside.contains(f));
}
let parents: BTreeSet<u64> = part.timeline_groups.iter().filter_map(|g| g.parent).collect();
part.timeline_groups.retain(|g| !g.features.is_empty() || parents.contains(&g.id));
part.named_views.clear();
part.configs = Default::default();
part.assembly = Default::default();
@@ -371,7 +369,14 @@ fn part_design(s: &Session, sc: &Scope, stem: &str, cmd: &str) -> Result<(Sessio
scope.assembly.origins.retain(|o| mapped(&o.origin.occurrence));
scope.assembly.contact_sets.retain(|c| c.occurrences.iter().all(mapped));
scope.assembly.exploded.retain(|v| v.moves.iter().all(|(o, _)| mapped(o)));
let mut notices = super::super::component_clone_assembly::clone_assembly(&mut part, &scope, &sc.occurrences, &features, &body_map)?;
let (mut notices, joints) =
super::super::component_clone_assembly::clone_assembly_with_joint_ids(&mut part, &scope, &sc.occurrences, &features, &body_map)?;
// Extracted joints receive new ids. References to joints outside the extracted part go away.
for group in &mut part.timeline_groups {
group.joints = group.joints.iter().filter_map(|id| joints.get(id).copied()).collect();
}
let parents: BTreeSet<u64> = part.timeline_groups.iter().filter_map(|g| g.parent).collect();
part.timeline_groups.retain(|g| !g.features.is_empty() || !g.joints.is_empty() || parents.contains(&g.id));
prune_params(&mut part);
if !source.configs.is_empty() {
notices.push(format!("the configurations stay in this design; {stem} has none"));
@@ -785,6 +790,7 @@ mod tests {
fn extract_moves_the_history_to_its_own_design_and_links_it() {
let dir = folder("basic");
let (mut s, plate, other) = assembly();
run(&mut s, "timeline.group", json!({"name":"Internal joints", "joints":["PinHold"]}));
run(&mut s, "file.save", json!({"path": dir.join("asm.solidcraft")}));
let (vol, cen) = (volumes(&s), centroids(&s));
let other_features: Vec<u64> = s.doc.features.iter().filter(|f| f.component == other).map(|f| f.id).collect();
@@ -815,6 +821,7 @@ mod tests {
assert_eq!(part.doc.components.iter().map(|c| c.name.as_str()).collect::<Vec<_>>(), ["Pin"]);
assert_eq!(part.doc.occurrences.len(), 1);
assert_eq!(part.doc.assembly.joints.iter().map(|j| j.name.as_str()).collect::<Vec<_>>(), ["PinHold"]);
assert_eq!(part.doc.timeline_groups[0].joints, [part.doc.assembly.joints[0].id], "joint folders follow the extracted ids");
assert!(part.doc.param("plate_h").is_some() && part.doc.param("unused").is_none());
assert!(part.model.results.iter().all(|r| r.error.is_none()));
let part_vol = volumes(&part);
+63 -20
View File
@@ -42,19 +42,19 @@ pub static COMMANDS: &[CommandSpec] = &[
.at("SOLID", "MODIFY")
.icon("delete")
.key("Delete")
.params("feature: id|name | features: [id|name] | groups: [timeline folder ids] (a folder goes with all its features and nested folders; one undo step; a sketch takes its dependent features with it) → deleted: [ids]; groups?: [folder ids deleted]; failing?: [{feature, error}] (later features that worked and now fail)"),
.params("feature: id|name | features: [id|name] | groups: [timeline folder ids] (a folder goes with all its features, joints and nested folders; one undo step; a sketch takes its dependent features with it) → deleted: [feature ids]; joints?: [joint ids]; groups?: [folder ids deleted]; failing?: [{feature, error}] (later features that worked and now fail)"),
CommandSpec::new("timeline.rename", "Rename", rename).icon("rename").params("feature: id|name, name (references to it follow)"),
CommandSpec::new("timeline.group", "New Feature Folder", group)
.icon("folder")
.doc_only()
.params("name?; features?: [id|name]; parent?: folder id (features leave any other timeline folder)"),
.params("name?; features?: [id|name]; joints?: [id|name]; groups?: [folder ids]; parent?: folder id (features and joints leave any other timeline folder)"),
CommandSpec::new("timeline.ungroup", "Remove Feature Folders", ungroup).icon("folder")
.doc_only()
.params("group: id | groups: [ids] (features remain in the timeline)"),
CommandSpec::new("timeline.rename_group", "Rename Feature Folder", rename_group).icon("rename").doc_only().params("group: id, name"),
.params("group: id | groups: [ids] (features and joints remain in the timeline)"),
CommandSpec::new("timeline.rename_group", "Rename Feature Folder", rename_group).icon("rename").doc_only().params("group: id, name (renames the folder holding features and joints)"),
CommandSpec::new("timeline.move_to_group", "Move Features to Folder", move_to_group).icon("folder")
.doc_only()
.params("features?: [id|name], groups?: [ids], group?: id (omit or null to move to the timeline root)"),
.params("features?: [id|name], joints?: [id|name], groups?: [ids], group?: id (omit or null to move to the timeline root)"),
CommandSpec::new("timeline.move_many", "Move Timeline Items", move_many).icon("reorder").params(
"features?: [id|name], groups?: [folder ids], group?: folder id, position: insertion slot, active?: bool (true = the moved contents end up before the history marker, false = behind it; other features keep their state); atomically changes folder membership and moves all selected contents as one block",
),
@@ -72,9 +72,9 @@ pub static COMMANDS: &[CommandSpec] = &[
.noundo()
.params("feature: id|name → features deleted with it, and features that would fail without it"),
CommandSpec::new("timeline.suppress", "Suppress Feature", suppress).icon("suppress").params(
"feature: id|name | features: [id|name] | groups: [timeline folder ids] (every feature in those folders and their nested \
folders; one undo step), suppressed?: bool (default toggles: several features are all \
suppressed unless all of them already are, then all are unsuppressed) → feature: id (or features: [ids]; groups: [ids] when given), suppressed",
"feature: id|name | features: [id|name] | groups: [timeline folder ids] (every feature and joint in those folders and their nested \
folders; one undo step), suppressed?: bool (default toggles: features and joints are all \
suppressed unless all of them already are, then all are unsuppressed) → feature: id (or features: [ids]; groups: [ids] and joints: [ids] when groups are given), suppressed",
),
CommandSpec::new("timeline.edit", "Edit Feature", edit_feature).icon("edit").params(
"feature: id|name, set: {fields to change, merged into the feature's definition as timeline.inspect returns it, \
@@ -83,7 +83,7 @@ pub static COMMANDS: &[CommandSpec] = &[
expressions are text, or a number in mm or degrees} → recomputed: features rebuilt, restored: features whose earlier result for the same inputs was reused",
),
CommandSpec::new("timeline.inspect", "Inspect Feature", inspect_feature).icon("inspect").noundo().params(
"feature: id|name → type, definition (the fields timeline.edit's set changes, with their current values), \
"group: folder id → group, name, parent, features, joints; or feature: id|name → type, definition (the fields timeline.edit's set changes, with their current values), \
inputs: [{label, param, expression, unit}] (named inputs parameters.change can also set), error, warning",
),
CommandSpec::new("select.set", "Select", select_set).icon("select")
@@ -205,10 +205,12 @@ fn delete(s: &mut Session, p: &Value) -> Result<Value> {
}
// A folder goes with its features and nested folders, its features in timeline order.
let (folders, contents) = group_tree(&s.doc, &groups);
let joints = group_joint_ids(&s.doc, &groups);
keys.extend(s.doc.features.iter().filter(|f| contents.contains(&f.id)).map(|f| f.id.to_string()));
// Features that work now; any of them failing after the delete is reported (a later
// feature that names a deleted body, #267).
let worked: std::collections::BTreeSet<u64> = s.model.results.iter().filter(|r| r.error.is_none() && !r.skipped).map(|r| r.id).collect();
super::joints::delete_joints(s.doc_mut(), &joints);
let mut gone = Vec::new();
for k in keys {
let Some(id) = s.doc.find_feature(&k).map(|f| f.id) else {
@@ -239,6 +241,9 @@ fn delete(s: &mut Session, p: &Value) -> Result<Value> {
.filter_map(|r| r.error.as_ref().map(|e| json!({"feature": r.name, "error": e})))
.collect();
let mut out = json!({"deleted": gone});
if !joints.is_empty() {
out["joints"] = json!(joints);
}
if !folders.is_empty() {
out["groups"] = json!(folders);
}
@@ -315,6 +320,26 @@ fn group_features(s: &Session, p: &Value, required: bool, cmd: &str) -> Result<V
Ok(ids)
}
fn group_joints(s: &Session, p: &Value, cmd: &str) -> Result<Vec<u64>> {
let values = match p.get("joints") {
None => return Ok(Vec::new()),
Some(Value::Array(values)) if values.len() <= 10_000 => values,
_ => return Err(bad(cmd, "`joints` must list at most 10000 joint ids or names")),
};
let mut ids = std::collections::BTreeSet::new();
for value in values {
ids.insert(super::joints::joint_id(s, Some(value), cmd)?);
}
Ok(ids.into_iter().collect())
}
fn group_joint_ids(doc: &solidcraft_doc::Document, roots: &[u64]) -> Vec<u64> {
let folders = group_tree(doc, roots).0;
let ids: std::collections::BTreeSet<u64> =
doc.timeline_groups.iter().filter(|group| folders.contains(&group.id)).flat_map(|group| group.joints.iter().copied()).collect();
doc.assembly.joints.iter().filter(|joint| ids.contains(&joint.id)).map(|joint| joint.id).collect()
}
fn group_ids(s: &Session, p: &Value, required: bool, cmd: &str) -> Result<Vec<u64>> {
let values = match p.get("groups") {
Some(Value::Array(values)) => values,
@@ -403,6 +428,7 @@ fn require_contiguous_groups(doc: &solidcraft_doc::Document, cmd: &str) -> Resul
fn group(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "timeline.group";
let mut features = group_features(s, p, false, cmd)?;
let mut joints = group_joints(s, p, cmd)?;
let groups = group_ids(s, p, false, cmd)?;
let name = match str_(p, "name").map(str::trim).filter(|name| !name.is_empty()) {
Some(name) if name.len() <= 128 => name.to_string(),
@@ -421,14 +447,17 @@ fn group(s: &mut Session, p: &Value) -> Result<Value> {
}
let nested = group_feature_ids(&s.doc, &groups);
features.retain(|feature| !nested.contains(feature));
let nested_joints = group_joint_ids(&s.doc, &groups);
joints.retain(|joint| !nested_joints.contains(joint));
let mut next = (*s.doc).clone();
for group in &mut next.timeline_groups {
group.joints.retain(|joint| !joints.contains(joint));
group.features.retain(|feature| !features.contains(feature));
if groups.contains(&group.id) {
group.parent = Some(id);
}
}
next.timeline_groups.push(solidcraft_doc::TimelineGroup { id, name: name.clone(), parent, features });
next.timeline_groups.push(solidcraft_doc::TimelineGroup { id, name: name.clone(), parent, features, joints });
sort_group_features(&mut next);
require_contiguous_groups(&next, cmd)?;
*s.doc_mut() = next;
@@ -469,18 +498,19 @@ fn ungroup(s: &mut Session, p: &Value) -> Result<Value> {
parent
};
let mut next = (*s.doc).clone();
let moved_features: Vec<(Option<u64>, Vec<u64>)> = next
let moved_features: Vec<(Option<u64>, Vec<u64>, Vec<u64>)> = next
.timeline_groups
.iter()
.filter(|group| ids.contains(&group.id))
.map(|group| (surviving_parent(group.parent), group.features.clone()))
.map(|group| (surviving_parent(group.parent), group.features.clone(), group.joints.clone()))
.collect();
for group in &mut next.timeline_groups {
if ids.contains(&group.id) {
continue;
}
group.parent = surviving_parent(group.parent);
for (_, features) in moved_features.iter().filter(|(parent, _)| *parent == Some(group.id)) {
for (_, features, joints) in moved_features.iter().filter(|(parent, _, _)| *parent == Some(group.id)) {
group.joints.extend(joints.iter().copied());
group.features.extend(features.iter().copied());
}
}
@@ -507,9 +537,10 @@ fn rename_group(s: &mut Session, p: &Value) -> Result<Value> {
fn move_to_group(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "timeline.move_to_group";
let features = group_features(s, p, false, cmd)?;
let joints = group_joints(s, p, cmd)?;
let groups = group_ids(s, p, false, cmd)?;
if features.is_empty() && groups.is_empty() {
return Err(bad(cmd, "list at least one feature or folder"));
if features.is_empty() && joints.is_empty() && groups.is_empty() {
return Err(bad(cmd, "list at least one feature, joint or folder"));
}
let target = match p.get("group") {
Some(Value::Null) | None => None,
@@ -522,8 +553,10 @@ fn move_to_group(s: &mut Session, p: &Value) -> Result<Value> {
}
let mut next = (*s.doc).clone();
for group in &mut next.timeline_groups {
group.joints.retain(|joint| !joints.contains(joint));
group.features.retain(|feature| !features.contains(feature));
if Some(group.id) == target {
group.joints.extend(joints.iter().copied());
group.features.extend(features.iter().copied());
}
}
@@ -535,7 +568,7 @@ fn move_to_group(s: &mut Session, p: &Value) -> Result<Value> {
sort_group_features(&mut next);
require_contiguous_groups(&next, cmd)?;
*s.doc_mut() = next;
Ok(json!({"features": features, "groups": groups, "group": target}))
Ok(json!({"features": features, "joints": joints, "groups": groups, "group": target}))
}
fn move_many(s: &mut Session, p: &Value) -> Result<Value> {
@@ -679,14 +712,18 @@ fn suppress(s: &mut Session, p: &Value) -> Result<Value> {
let contents = group_feature_ids(&s.doc, &groups);
let more: Vec<u64> = s.doc.features.iter().map(|f| f.id).filter(|id| contents.contains(id) && !ids.contains(id)).collect();
ids.extend(more);
if ids.is_empty() {
return Err(bad(cmd, "the timeline folders hold no features"));
let joints = group_joint_ids(&s.doc, &groups);
if ids.is_empty() && joints.is_empty() {
return Err(bad(cmd, "the timeline folders hold no features or joints"));
}
let want = match bool_(p, "suppressed") {
Some(b) => b,
None if p.get("suppressed").is_some_and(|v| !v.is_null()) => return Err(bad(cmd, "`suppressed` must be true or false")),
// Toggle: one feature flips; several are all suppressed unless all already are.
None => !ids.iter().all(|id| s.doc.feature(*id).is_some_and(|f| f.suppressed)),
None => {
!(ids.iter().all(|id| s.doc.feature(*id).is_some_and(|f| f.suppressed))
&& joints.iter().all(|id| s.doc.assembly.joints.iter().any(|joint| joint.id == *id && joint.suppressed)))
}
};
let keep = if want { counted_names_from(s, &ids) } else { Vec::new() };
// One command, so one undo step however many features it changes.
@@ -700,10 +737,11 @@ fn suppress(s: &mut Session, p: &Value) -> Result<Value> {
let f = doc.feature_mut(*id).ok_or_else(|| bad(cmd, format!("no feature {id}")))?;
f.suppressed = want;
}
super::joints::suppress_joints(s.doc_mut(), &joints, want);
match ids.as_slice() {
[id] if groups.is_empty() && p.get("features").is_none_or(Value::is_null) => Ok(json!({"feature": id, "suppressed": want})),
_ if groups.is_empty() => Ok(json!({"features": ids, "suppressed": want})),
_ => Ok(json!({"features": ids, "groups": groups, "suppressed": want})),
_ => Ok(json!({"features": ids, "joints": joints, "groups": groups, "suppressed": want})),
}
}
@@ -1063,6 +1101,11 @@ fn heavy_fields(kind: &FeatureKind) -> &'static [(&'static str, &'static str)] {
fn inspect_feature(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "timeline.inspect";
if p.get("group").is_some() {
let id = timeline_group_id(s, p, cmd)?;
let group = s.doc.timeline_groups.iter().find(|group| group.id == id).ok_or_else(|| bad(cmd, "folder"))?;
return Ok(json!({"group": id, "name": group.name, "parent": group.parent, "features": group.features, "joints": group.joints}));
}
let id = feature_id(s, p.get("feature"), cmd)?;
let f = s.doc.feature(id).ok_or_else(|| bad(cmd, "feature"))?;
let mut def = serde_json::to_value(f).map_err(|e| EngineError::Other(e.to_string()))?;
+83
View File
@@ -1354,3 +1354,86 @@ fn a_move_before_a_long_history_of_one_body_fits_a_window_threads_stack() {
.join();
assert!(moved.is_ok());
}
#[test]
fn timeline_joint_folders_are_mixed_saved_and_undoable() {
let mut s = Session::default();
run(&mut s, "sketch.create", json!({"plane":"XY", "name":"Profile"}));
run(&mut s, "component.create", json!({"name":"Part"}));
let joint = run(&mut s, "joint.as_built", json!({"type":"revolute", "a":0, "b":"Part", "name":"Hinge"}))["joint"].as_u64().unwrap();
let folder = run(&mut s, "timeline.group", json!({"name":"Mixed", "features":["Profile"], "joints":["Hinge", joint]}))["group"].as_u64().unwrap();
assert_eq!(s.doc.timeline_groups[0].joints, [joint]);
assert_eq!(s.doc.timeline_groups[0].features.len(), 1);
assert_eq!(run(&mut s, "timeline.inspect", json!({"group":folder}))["joints"], json!([joint]));
let saved = s.doc.to_json();
let reopened = solidcraft_doc::Document::from_json(&saved).unwrap();
assert_eq!(reopened.to_json(), saved);
let mut damaged: Value = serde_json::from_str(&saved).unwrap();
damaged["timeline_groups"][0]["joints"] = json!([u64::MAX]);
assert!(solidcraft_doc::Document::from_json(&damaged.to_string()).is_err());
damaged["timeline_groups"][0]["joints"] = json!([joint, joint]);
assert!(solidcraft_doc::Document::from_json(&damaged.to_string()).is_err());
let destination = run(&mut s, "timeline.group", json!({"name":"Destination"}))["group"].as_u64().unwrap();
run(&mut s, "timeline.move_to_group", json!({"joints":[joint], "group":destination}));
assert!(s.doc.timeline_groups[0].joints.is_empty());
assert_eq!(s.doc.timeline_groups[1].joints, [joint]);
run(&mut s, "edit.undo", json!({}));
assert_eq!(s.doc.timeline_groups[0].joints, [joint]);
// Both contents of a nested folder participate in the same toggle and undo.
run(&mut s, "timeline.move_to_group", json!({"groups":[folder], "group":destination}));
let before = s.doc.clone();
run(&mut s, "timeline.suppress", json!({"groups":[destination]}));
assert!(s.doc.features[0].suppressed && s.doc.assembly.joints[0].suppressed);
run(&mut s, "timeline.suppress", json!({"groups":[destination]}));
assert!(!s.doc.features[0].suppressed && !s.doc.assembly.joints[0].suppressed);
run(&mut s, "edit.undo", json!({}));
assert!(s.doc.features[0].suppressed && s.doc.assembly.joints[0].suppressed);
run(&mut s, "edit.undo", json!({}));
assert_eq!(*s.doc, *before);
run(&mut s, "timeline.delete", json!({"groups":[destination]}));
assert!(s.doc.features.is_empty() && s.doc.assembly.joints.is_empty() && s.doc.timeline_groups.is_empty());
run(&mut s, "edit.undo", json!({}));
assert_eq!(*s.doc, *before);
run(&mut s, "timeline.ungroup", json!({"group":folder}));
assert_eq!(s.doc.timeline_groups[0].joints, [joint], "contents promote to the surviving parent");
run(&mut s, "timeline.ungroup", json!({"group":destination}));
assert!(s.doc.timeline_groups.is_empty());
assert_eq!(s.doc.assembly.joints[0].id, joint);
let folder = run(&mut s, "timeline.group", json!({"joints":vec![joint; 10_000]}))["group"].as_u64().unwrap();
assert_eq!(s.doc.timeline_groups[0].joints, [joint], "the 10000-entry cap is inclusive and deduplicated");
run(&mut s, "timeline.move_to_group", json!({"joints":["Hinge"], "group":null}));
assert!(s.doc.timeline_groups[0].joints.is_empty());
run(&mut s, "timeline.move_to_group", json!({"joints":["Hinge"], "group":folder}));
run(&mut s, "timeline.suppress", json!({"groups":[folder]}));
assert!(s.doc.assembly.joints[0].suppressed, "joint-only folders suppress too");
run(&mut s, "joint.delete", json!({"joint":joint}));
assert!(s.doc.timeline_groups[0].joints.is_empty());
run(&mut s, "edit.undo", json!({}));
assert_eq!(s.doc.timeline_groups[0].joints, [joint]);
run(&mut s, "component.delete", json!({"component":"Part"}));
assert!(s.doc.assembly.joints.is_empty());
assert!(s.doc.timeline_groups[0].joints.is_empty());
assert!(solidcraft_doc::Document::from_json(&s.doc.to_json()).is_ok());
}
#[test]
fn timeline_joint_folders_reject_hostile_joint_references_atomically() {
let mut s = Session::default();
run(&mut s, "component.create", json!({"name":"Part"}));
let joint = run(&mut s, "joint.as_built", json!({"type":"rigid", "a":0, "b":"Part"}))["joint"].as_u64().unwrap();
let folder = run(&mut s, "timeline.group", json!({}))["group"].as_u64().unwrap();
for command in ["timeline.group", "timeline.move_to_group"] {
for joints in
[json!([u64::MAX]), json!([-1]), json!([1e100]), json!(["missing"]), json!([{}]), json!([null]), json!("bad"), json!(vec![joint; 10_001])]
{
let before = s.doc.to_json();
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| s.execute(command, &json!({"joints":joints, "group":folder}))));
let error = result.unwrap().unwrap_err();
assert!(
matches!(&error, crate::EngineError::BadParams { .. }),
"{command} must reject invalid joint references without an internal panic: {error}"
);
assert_eq!(s.doc.to_json(), before);
}
}
}
+23 -6
View File
@@ -1393,12 +1393,12 @@ fn edit(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(f) = bool_(p, "flip") {
j.flip = f;
}
if let Some(x) = bool_(p, "suppressed") {
j.suppressed = x;
}
if let Some(n) = str_(p, "name").map(str::trim).filter(|n| !n.is_empty() && n.len() <= 128) {
j.name = n.to_string();
}
if let Some(x) = bool_(p, "suppressed") {
suppress_joints(s.doc_mut(), &[id], x);
}
// A relation whose type, offset, tilt or flip changes starts again from where its parts are.
let reseat = kind.is_some() || offset.is_some() || angle.is_some() || p.get("flip").is_some();
if reseat {
@@ -1413,6 +1413,25 @@ fn edit(s: &mut Session, p: &Value) -> Result<Value> {
Ok(sol_json(&sol, p))
}
/// Shared mutation for individual joints and timeline folders; the caller resolves once.
pub(super) fn suppress_joints(doc: &mut solidcraft_doc::Document, ids: &[u64], suppressed: bool) {
let ids: std::collections::BTreeSet<_> = ids.iter().copied().collect();
for joint in &mut doc.assembly.joints {
if ids.contains(&joint.id) {
joint.suppressed = suppressed;
}
}
}
pub(super) fn delete_joints(doc: &mut solidcraft_doc::Document, ids: &[u64]) {
let ids: std::collections::BTreeSet<_> = ids.iter().copied().collect();
doc.assembly.joints.retain(|joint| !ids.contains(&joint.id));
doc.assembly.links.retain(|link| !ids.contains(&link.a) && !ids.contains(&link.b));
for group in &mut doc.timeline_groups {
group.joints.retain(|id| !ids.contains(id));
}
}
fn delete(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "joint.delete";
if let Some(name) = str_(p, "origin") {
@@ -1435,9 +1454,7 @@ fn delete(s: &mut Session, p: &Value) -> Result<Value> {
return Ok(json!({"deleted_link": i}));
}
let id = joint_id(s, p.get("joint"), cmd)?;
let doc = s.doc_mut();
doc.assembly.joints.retain(|j| j.id != id);
doc.assembly.links.retain(|l| l.a != id && l.b != id);
delete_joints(s.doc_mut(), &[id]);
let sol = resolve(s);
let mut out = sol_json(&sol, p);
out["deleted"] = json!(id);