Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Doc: the names of split faces no longer grow with every split (#5860) (#6045)

Homero Thompson99d5b4302451
diff --git a/crates/doc/src/history_names.rs b/crates/doc/src/history_names.rs
index 0bb149c5b0..039ed4fbff 100644
--- a/crates/doc/src/history_names.rs
+++ b/crates/doc/src/history_names.rs
@@ -13,6 +13,10 @@
//! around them tells apart are numbered (`#1`, `#2`): in the order of what they came from when
//! that differs (exact), else in the kernel's order (unverified). A face made of several (a
//! coplanar join) takes one name and answers to the others (aliases).
+//!
+//! A piece that is split again keeps the face's stem and the bounds that told the piece it
+//! came from apart: its name lists the faces around it, not the qualifiers of the pieces
+//! before it, so a name does not grow with every split.
use std::collections::{BTreeMap, BTreeSet};
use std::sync::{Arc, OnceLock};
@@ -244,7 +248,7 @@ fn merged(names: Vec<Named>) -> Option<Named> {
}
common = crate::naming::parent_piece(c);
}
- let name = common.map(str::to_string).unwrap_or(first);
+ let name = common_piece(&names).or_else(|| common.map(str::to_string)).unwrap_or(first);
let mut aliases: Vec<String> = Vec::new();
for n in names {
for alias in std::iter::once(n.name).chain(n.aliases) {
@@ -256,6 +260,38 @@ fn merged(names: Vec<Named>) -> Option<Named> {
Some(Named { name, aliases, verified, origins: None })
}
+/// The name the faces have in common: their stem, the faces their qualifiers share and the
+/// number that told their pieces apart, when all of them have it. A face made of pieces that
+/// were split again keeps the name of the piece they came from (#5860).
+fn common_piece(names: &[Named]) -> Option<String> {
+ let first = names.first()?.name.as_str();
+ let (stem, around, tie) = split_piece(first);
+ let mut shared: BTreeSet<String> = around.into_iter().collect();
+ let mut same_tie = tie;
+ for n in names.iter().skip(1) {
+ let (other_stem, around, other_tie) = split_piece(&n.name);
+ if other_stem != stem {
+ return None;
+ }
+ let around: BTreeSet<String> = around.into_iter().collect();
+ shared = shared.intersection(&around).cloned().collect();
+ if same_tie != other_tie {
+ same_tie = None;
+ }
+ }
+ let mut name = stem;
+ if !shared.is_empty() {
+ name.push_str("#[");
+ name.push_str(&shared.into_iter().collect::<Vec<_>>().join(","));
+ name.push(']');
+ }
+ if let Some(tie) = same_tie {
+ name.push('#');
+ name.push_str(&tie);
+ }
+ Some(name)
+}
+
/// Whether `name` is `stem` or a piece of it (at any depth).
pub(crate) fn descends(name: &str, stem: &str) -> bool {
let mut current = Some(name);
@@ -330,11 +366,26 @@ fn qualify_at(named: Vec<Named>, adjacent: &[(usize, usize)], centres: Option<&d
})
.unwrap_or_default();
let own = |i: usize| -> Vec<&String> { bounds.get(i).unwrap_or(&empty).iter().filter(|x| !common.contains(x)).collect() };
+ // The face's stem, the faces the parent's qualifier lists (the bounds that told the
+ // parent apart from its siblings) and the number that told apart the pieces nothing
+ // else told apart.
+ let (stem, inherited, tie) = split_piece(base);
let mut by_key: BTreeMap<String, Vec<usize>> = BTreeMap::new();
for &i in members {
- let key: Vec<&str> = own(i).into_iter().map(String::as_str).collect();
- // Nothing around it of its own: the piece is only numbered (below).
- let name = if key.is_empty() { base.to_string() } else { format!("{base}#[{}]", key.join(",")) };
+ let own = own(i);
+ // Nothing around it of its own: the piece keeps the name it had.
+ let name = if own.is_empty() {
+ base.to_string()
+ } else {
+ // The bounds that told the parent apart and the bounds that tell this piece
+ // apart: the name says which piece of the face it is without repeating the
+ // qualifiers of the pieces before it (the name grew with every split
+ // otherwise, #5860).
+ let mut key: BTreeSet<&str> = inherited.iter().map(String::as_str).collect();
+ key.extend(own.iter().map(|s| s.as_str()));
+ let key: Vec<&str> = key.into_iter().collect();
+ piece_name(&stem, &key, tie.as_deref())
+ };
by_key.entry(name).or_default().push(i);
}
for (key, tied) in by_key {
@@ -394,6 +445,56 @@ fn qualify_at(named: Vec<Named>, adjacent: &[(usize, usize)], centres: Option<&d
Names { faces: Arc::new(names), info }
}
+/// The most bytes a face name may take: a command takes a face name of at most this many
+/// bytes, so a longer name could not be used to find the face again.
+const MAX_NAME_BYTES: usize = 512;
+
+/// A piece's name: the face's stem, the faces around the piece that tell it apart, as many
+/// of them as the name can hold (`MAX_NAME_BYTES`), and the number that told the pieces of
+/// the same face apart.
+fn piece_name(stem: &str, faces: &[&str], tie: Option<&str>) -> String {
+ let mut kept: Vec<&str> = Vec::with_capacity(faces.len());
+ // The stem, the `#[` and the `]` the qualifier is written in.
+ let mut len = stem.len() + 3;
+ for face in faces {
+ let more = face.len() + if kept.is_empty() { 0 } else { 1 };
+ if len + more > MAX_NAME_BYTES {
+ break;
+ }
+ len += more;
+ kept.push(face);
+ }
+ let mut name = if kept.is_empty() { stem.to_string() } else { format!("{stem}#[{}]", kept.join(",")) };
+ if let Some(tie) = tie {
+ name.push('#');
+ name.push_str(tie);
+ }
+ name
+}
+
+/// A piece name's parts: the face's stem, the faces its qualifier lists and the number that
+/// tells apart the pieces of the same face that nothing else tells apart
+/// (`F2:top#[F3:side]#2`). A name that cannot be read is taken as it is: a hostile name never
+/// panics.
+fn split_piece(name: &str) -> (String, Vec<String>, Option<String>) {
+ let (stem, around) = match crate::piece_names::piece_parts(name) {
+ Some((stem, around)) => (stem.to_string(), around.iter().map(|face| face.to_string()).collect()),
+ None => (name.to_string(), Vec::new()),
+ };
+ (stem, around, tie_number(name))
+}
+
+/// The number at the end of a piece name (`#2` of `F2:top#[F3:side]#2`), when it has one: it
+/// tells apart the pieces of a face that nothing else tells apart.
+fn tie_number(name: &str) -> Option<String> {
+ let i = name.rfind('#')?;
+ let digits = name.get(i + 1..)?;
+ if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) {
+ return None;
+ }
+ Some(digits.to_string())
+}
+
/// `origin` with the face or edge it names left out (its kind and operand kept).
fn without_index(origin: FaceOrigin) -> FaceOrigin {
match origin {
diff --git a/crates/doc/src/history_names_tests.rs b/crates/doc/src/history_names_tests.rs
index 7c67e5a718..40972a26f3 100644
--- a/crates/doc/src/history_names_tests.rs
+++ b/crates/doc/src/history_names_tests.rs
@@ -79,6 +79,38 @@ fn pieces_nothing_tells_apart_are_numbered_and_unverified() {
assert!(!names.verified(0) && !names.verified(1));
}
+/// #5860: a piece of a split face that is split again must not stack another qualifier on
+/// its name: the name grew with every split (728 characters after eleven splits).
+#[test]
+fn a_piece_split_again_does_not_grow_its_name() {
+ // A face split in two: the pieces are told apart by the walls only they touch.
+ let first = qualify(named(&["top", "top", "wall:a", "wall:b"]), &[(0, 2), (1, 3)]);
+ assert_eq!(first.faces[0], "top#[wall:a]");
+ assert_eq!(first.faces[1], "top#[wall:b]");
+ // The piece that touches wall:a is split again, and again: its name lists the walls
+ // around it, not the qualifiers of the pieces it came from.
+ let mut piece = first.faces[0].clone();
+ for _ in 0..10 {
+ let names = qualify(named(&[piece.as_str(), piece.as_str(), "wall:a", "wall:b"]), &[(0, 2), (1, 3)]);
+ piece = names.faces[0].clone();
+ }
+ assert_eq!(piece, "top#[wall:a]");
+}
+
+/// A name has to stay within the length a command accepts for a face name (512 bytes), or
+/// the face could not be named again.
+#[test]
+fn a_piece_with_many_faces_around_it_keeps_a_name_that_can_be_used() {
+ let around: Vec<String> = (0..80).map(|i| format!("F{i}:blend:face:{i}")).collect();
+ let mut faces: Vec<&str> = vec!["top", "top"];
+ faces.extend(around.iter().map(String::as_str));
+ // Each piece touches forty faces of its own: none of them tells the pieces apart.
+ let adjacent: Vec<(usize, usize)> = (2..42).map(|i| (0, i)).chain((42..82).map(|i| (1, i))).collect();
+ let names = qualify(named(&faces), &adjacent);
+ assert!(names.faces.iter().all(|n| n.len() <= 512), "{:?}", names.faces);
+ assert_ne!(names.faces[0], names.faces[1], "the pieces are still told apart");
+}
+
#[test]
fn a_merged_face_takes_the_shared_stem_or_the_first_name_and_answers_to_the_others() {
let m = merged(named(&["top#[a]", "top#[b]"])).unwrap();
diff --git a/crates/engine/src/cmd/joints_face_tests.rs b/crates/engine/src/cmd/joints_face_tests.rs
index 07027a6d21..1a74c7a42c 100644
--- a/crates/engine/src/cmd/joints_face_tests.rs
+++ b/crates/engine/src/cmd/joints_face_tests.rs
@@ -211,7 +211,9 @@ fn origins_take_a_long_piece_name() {
run(&mut s, "solid.box", json!({"length": 20, "width": 20, "height": 2, "corner": [100, 200, 0]}));
run(&mut s, "solid.hole", json!({"position": [110, 210, 2], "diameter": 6}));
let top = face_name(&mut s, "Tray", Vec3::new(20.0, 20.0, 1.0));
- assert!(top.len() > 512, "{}: {top}", top.len());
+ // Split-face names are bounded at 512 bytes (#5860), so the name is as long as a piece name gets;
+ // joints take far more than that (`MAX_FACE_NAME`), so none is refused for its length.
+ assert!(top.len() > 400 && top.len() <= 512, "{}: {top}", top.len());
let wall = face_name(&mut s, "Tray", Vec3::new(53.0, 50.0, 0.5));
let foot = face_name(&mut s, "Bracket", Vec3::new(113.0, 210.0, 1.0));
let under = face_name(&mut s, "Bracket", Vec3::new(105.0, 205.0, 0.0));