Frames: renaming a cut-off piece of a frame member renames a different member after a rebuild (#10123) (#10132)
* claim: #10123 * Frames: a renamed cut-off piece keeps its own name through a rebuild (#10123) --------- Co-authored-by: Dahak Builds <dahakbuilds@users.noreply.github.com>
This commit is contained in:
+14
-4
@@ -3316,6 +3316,11 @@ fn apply_traced_with(
|
||||
// Not yet: a cut that splits one piece of such a body still splits all of them; keeping
|
||||
// the untouched ones together needs each new piece traced to the piece it came from.
|
||||
let mut taken: std::collections::BTreeSet<String> = state.bodies.iter().map(|b| b.name.clone()).collect();
|
||||
// The pieces past the first belong to this feature, like a split's pieces (#10123):
|
||||
// `body.rename` stores their names in this feature's `body_names`, in this order, so a
|
||||
// renamed piece keeps its own name through a rebuild instead of landing on a sibling
|
||||
// of the body it was cut from.
|
||||
let mut extra = 0usize;
|
||||
let mut split: Vec<ModelBody> = Vec::new();
|
||||
for mb in std::mem::take(&mut state.bodies) {
|
||||
let Some(&had) = affected.get(&mb.name).filter(|_| !state.sheets.iter().any(|s| s.body == mb.name)) else {
|
||||
@@ -3331,13 +3336,18 @@ fn apply_traced_with(
|
||||
// (so pieces of one face in different bodies are told apart).
|
||||
let lineages = crate::lineage::Lineage::lumps(&mb.body, mb.lineage().as_deref(), &pieces);
|
||||
for (k, (p, lineage)) in pieces.into_iter().zip(lineages).enumerate() {
|
||||
let name = if k == 0 {
|
||||
mb.name.clone()
|
||||
let (name, made_by) = if k == 0 {
|
||||
(mb.name.clone(), mb.feature)
|
||||
} else {
|
||||
(k..=k + taken.len()).map(|j| format!("{} ({j})", mb.name)).find(|n| !taken.contains(n)).unwrap_or_default()
|
||||
let stored = f.body_names.get(extra).filter(|n| !n.trim().is_empty() && !taken.contains(*n)).cloned();
|
||||
extra += 1;
|
||||
let name = stored.unwrap_or_else(|| {
|
||||
(k..=k + taken.len()).map(|j| format!("{} ({j})", mb.name)).find(|n| !taken.contains(n)).unwrap_or_default()
|
||||
});
|
||||
(name, f.id)
|
||||
};
|
||||
taken.insert(name.clone());
|
||||
split.push(ModelBody::new(name, p, mb.feature).with_lineage(lineage));
|
||||
split.push(ModelBody::new(name, p, made_by).with_lineage(lineage));
|
||||
}
|
||||
}
|
||||
state.bodies = split;
|
||||
|
||||
@@ -571,7 +571,12 @@ pub(crate) fn order_lumps(bodies: &mut [crate::ModelBody], origin: &Arc<crate::n
|
||||
}
|
||||
for (_, members) in families.into_iter().filter(|(_, m)| m.len() > 1) {
|
||||
let feature = members.first().and_then(|i| bodies.get(*i)).map(|b| b.feature);
|
||||
if members.iter().any(|i| bodies.get(*i).map(|b| b.feature) != feature) {
|
||||
// A cut's pieces past the first belong to the cut itself (#10123), so its family spans
|
||||
// two features: the piece that kept the body's name comes from the body's own feature
|
||||
// and the rest from this feature. Any other mix is not a split's family: leave it be.
|
||||
let same = members.iter().all(|i| bodies.get(*i).map(|b| b.feature) == feature);
|
||||
let foreign = members.iter().filter(|i| bodies.get(**i).map(|b| b.feature) != Some(origin.feature.id)).count();
|
||||
if !same && foreign > 1 {
|
||||
continue;
|
||||
}
|
||||
let least = |b: &crate::ModelBody| {
|
||||
|
||||
@@ -1474,6 +1474,82 @@ mod tests {
|
||||
assert_eq!(names(&s)[1], b);
|
||||
}
|
||||
|
||||
/// #10123: renaming a piece cut off a frame member must land on that piece after a rebuild,
|
||||
/// not on the first line's member. The repro is the issue's, verbatim.
|
||||
#[test]
|
||||
fn renaming_a_cut_off_frame_piece_lands_on_that_piece_after_rebuild() {
|
||||
let dir = std::env::temp_dir().join(format!("sc-10123-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let saved = dir.join("rename_repro.sc");
|
||||
let mut s = Session::default();
|
||||
run(&mut s, "document.units", json!({"units": "in"}));
|
||||
run(
|
||||
&mut s,
|
||||
"timeline.add",
|
||||
json!({"feature": {
|
||||
"id": 1, "name": "Lines", "type": "sketch", "suppressed": false,
|
||||
"plane": {"name": "XY", "type": "origin"},
|
||||
"sketch": {
|
||||
"constraints": [{"id": "k1", "p": 0, "q": 1, "type": "coincident"}],
|
||||
"counters": {"k": 1, "l": 2},
|
||||
"curves": [
|
||||
{"a": 1, "b": 2, "construction": false, "id": "l1", "type": "line"},
|
||||
{"a": 3, "b": 4, "construction": false, "id": "l2", "type": "line"}
|
||||
],
|
||||
"points": [
|
||||
{"fixed": true, "id": "origin", "pos": [0.0, 0.0]},
|
||||
{"fixed": false, "id": "l1.start", "pos": [0.0, 0.0]},
|
||||
{"fixed": false, "id": "l1.end", "pos": [100.0, 0.0]},
|
||||
{"fixed": false, "id": "l2.start", "pos": [0.0, 50.0]},
|
||||
{"fixed": false, "id": "l2.end", "pos": [100.0, 50.0]}
|
||||
]
|
||||
}
|
||||
}}),
|
||||
);
|
||||
run(
|
||||
&mut s,
|
||||
"timeline.add",
|
||||
json!({"feature": {
|
||||
"id": 2, "name": "Members", "type": "frame_member", "suppressed": false,
|
||||
"body_names": ["A", "B"], "names": {"l1": "A", "l2": "B"}, "param_names": ["d1"],
|
||||
"path_sketch": 1,
|
||||
"profile": {"profile": {"diameter": "10 mm", "kind": "round_bar"}, "source": "library"}
|
||||
}}),
|
||||
);
|
||||
run(
|
||||
&mut s,
|
||||
"timeline.add",
|
||||
json!({"feature": {
|
||||
"id": 3, "name": "Cutter", "type": "box", "suppressed": false,
|
||||
"body_names": ["Cutter"], "corner": [45.0, 40.0, -10.0],
|
||||
"height": "20 mm", "length": "10 mm", "width": "20 mm",
|
||||
"operation": "new_body", "param_names": ["d2", "d3", "d4"]
|
||||
}}),
|
||||
);
|
||||
run(
|
||||
&mut s,
|
||||
"timeline.add",
|
||||
json!({"feature": {
|
||||
"id": 4, "name": "Cut B", "type": "combine", "suppressed": false,
|
||||
"operation": "cut", "target": "B", "tools": ["Cutter"], "keep_tools": true
|
||||
}}),
|
||||
);
|
||||
let sorted = |v: Vec<String>| {
|
||||
let mut v = v;
|
||||
v.sort();
|
||||
v
|
||||
};
|
||||
assert_eq!(sorted(names(&s)), ["A", "B", "B (1)", "Cutter"]);
|
||||
run(&mut s, "body.rename", json!({"body": "B (1)", "name": "B2"}));
|
||||
assert_eq!(sorted(names(&s)), ["A", "B", "B2", "Cutter"]);
|
||||
run(&mut s, "file.save_as", json!({"path": saved.to_string_lossy()}));
|
||||
let mut reopened = Session::default();
|
||||
run(&mut reopened, "doc.open", json!({"path": saved.to_string_lossy()}));
|
||||
assert_eq!(sorted(names(&reopened)), ["A", "B", "B2", "Cutter"], "the rename must not move to member A on rebuild");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renaming_a_body_to_a_removed_name_preserves_the_design() {
|
||||
for removed_name in ["Base", "Body1"] {
|
||||
|
||||
Reference in New Issue
Block a user