Construction planes: refuse duplicate feature names (#10707) (#10743)

* claim: #10707

* Fix construction plane name collisions (#10707)
This commit is contained in:
Colin Faulkingham
2026-11-02 00:06:59 +00:00
committed by GitHub
parent 2b9261050f
commit eaf34d2528
4 changed files with 84 additions and 2 deletions
+8 -2
View File
@@ -3765,12 +3765,18 @@ impl Document {
} else {
0
};
let id = self.next_id.max(1);
self.next_id = id.checked_add(1).ok_or_else(|| DocError::Invalid("too many items in the design".into()))?;
let name = match name {
Some(n) if !n.trim().is_empty() => n.trim().to_string(),
_ => self.unique_name(kind.base_name()),
};
// Plane references share the feature namespace. A duplicate would silently select
// the first feature. Other shared names (a sketch and its extrude) remain supported.
let is_plane = matches!(kind, FeatureKind::ConstructionPlane { .. });
if self.features.iter().any(|f| f.name == name && (is_plane || matches!(f.kind, FeatureKind::ConstructionPlane { .. }))) {
return Err(DocError::Invalid(format!("another feature is called `{name}`")));
}
let id = self.next_id.max(1);
self.next_id = id.checked_add(1).ok_or_else(|| DocError::Invalid("too many items in the design".into()))?;
let f = Feature {
id,
name,
+22
View File
@@ -668,6 +668,28 @@ fn new_names_take_the_first_free_number() {
assert_eq!(name(&doc, next), names[0], "the first freed number is taken again");
}
#[test]
fn construction_plane_name_collisions_leave_the_document_unchanged() {
let plane = || FeatureKind::ConstructionPlane { plane: PlaneRef::Origin { name: "XY".into() } };
let sketch = || FeatureKind::Sketch { plane: PlaneRef::Origin { name: "XY".into() }, sketch: Sketch::new() };
for (first, second) in [(plane(), plane()), (plane(), sketch()), (sketch(), plane())] {
let mut doc = Document::new("names");
let id = doc.add_feature(first, Some("Mid")).unwrap();
// A suppressed feature beyond the marker still owns its name.
doc.feature_mut(id).unwrap().suppressed = true;
doc.marker = Some(0);
let before = doc.clone();
for name in ["Mid", " Mid "] {
let error = doc.add_feature(second.clone(), Some(name)).unwrap_err().to_string();
assert!(error.contains("another feature is called `Mid`"), "{error}");
assert_eq!(doc, before, "a refused name must not consume an id or change the timeline");
}
let other = doc.add_feature(second, Some("Upper")).unwrap();
assert_eq!(doc.feature(other).unwrap().name, "Upper");
assert_eq!(other, before.next_id);
}
}
#[test]
fn empty_boolean_targets_name_overlapping_component() {
for (operation, offset) in [(Operation::Cut, 0.0), (Operation::Intersect, 0.0), (Operation::Cut, 40.0)] {
+5
View File
@@ -1737,6 +1737,11 @@ fn redefine(s: &mut Session, p: &Value) -> Result<Value> {
}
trial.doc_mut().marker = Some(idx + 1);
trial.refresh();
// The edited feature keeps its name. Its temporary replacement needs a fresh one while
// the original remains in the trial as a placeholder (including construction planes).
if let Some(parameters) = params.as_object_mut() {
parameters.remove("name");
}
let r = spec.run_nested(&mut trial, &params)?;
let made = r.get("feature").and_then(Value::as_u64).ok_or_else(|| bad(cmd, format!("`{cid}` did not make a feature")))?;
let mut new = trial.doc.feature(made).cloned().ok_or_else(|| bad(cmd, format!("`{cid}` did not make a feature")))?;
+49
View File
@@ -43,6 +43,55 @@ fn plane(s: &Session, name: &str) -> Plane {
construction_planes(s).into_iter().find(|p| p.1 == name).map(|p| p.2).unwrap_or_else(|| panic!("no plane {name}"))
}
#[test]
fn construction_plane_names_cannot_be_reused_by_new_features() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 5, "body_name": "Block"}));
run(&mut s, "construct.plane.offset", json!({"base": "XY", "offset": 10, "name": "Mid"}));
let before = s.doc.clone();
for (command, params) in [
("construct.plane.offset", json!({"base": "XY", "offset": 20, "name": "Mid"})),
("construct.plane.offset", json!({"base": "XY", "offset": 20, "name": " Mid "})),
("sketch.create", json!({"plane": "XY", "name": "Mid"})),
("solid.box", json!({"length": 1, "width": 1, "height": 1, "name": "Mid"})),
("construct.plane.offset", json!({"base": "XY", "offset": 20, "name": "Box1"})),
] {
let error = s.execute(command, &params).unwrap_err().to_string();
assert!(error.contains("another feature is called"), "{command}: {error}");
assert_eq!(s.doc, before, "{command}: a refused creation changes nothing");
}
run(&mut s, "construct.plane.offset", json!({"base": "XY", "offset": 20, "name": "Upper"}));
run(&mut s, "solid.mirror", json!({"bodies": ["Block"], "plane": "Upper"}));
let measured = run(&mut s, "inspect.measure", json!({}));
let mirrored = measured["bodies"].as_array().unwrap().iter().find(|b| b["name"] == "Block (2)").unwrap();
assert_eq!(mirrored["bbox"]["min"][2], 35.0);
assert_eq!(mirrored["bbox"]["max"][2], 40.0);
}
#[test]
fn redefining_a_construction_plane_keeps_its_existing_name() {
let mut s = Session::default();
let id = run(&mut s, "construct.plane.offset", json!({"base": "XY", "offset": 10, "name": "Mid"}))["feature"].as_u64().unwrap();
run(&mut s, "sketch.create", json!({"plane": "Mid", "name": "Profile"}));
run(&mut s, "sketch.finish", json!({}));
run(
&mut s,
"timeline.redefine",
json!({"feature": id, "command": "construct.plane.offset", "params": {"base": "XY", "offset": 20, "name": "Mid"}}),
);
assert_eq!(s.doc.feature(id).unwrap().name, "Mid");
assert_eq!(s.doc.features.len(), 2);
assert!(near(plane(&s, "Mid").origin, Vec3::new(0.0, 0.0, 20.0)));
run(&mut s, "edit.undo", json!({}));
assert!(near(plane(&s, "Mid").origin, Vec3::new(0.0, 0.0, 10.0)));
run(&mut s, "edit.redo", json!({}));
let mut reopened = Session::new(solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap());
reopened.refresh();
assert_eq!(reopened.doc.feature(id).unwrap().name, "Mid");
assert!(near(plane(&reopened, "Mid").origin, Vec3::new(0.0, 0.0, 20.0)));
assert!(reopened.model.results.iter().all(|r| r.error.is_none()));
}
#[test]
fn renamed_construction_references_survive_edits_and_reopening() {
let mut s = Session::default();