A sketch and the feature built from it may share a name, and the sample and oracle designs do. `timeline.add` refused such a recorded name, so a design's own exported script failed to replay. `Document::insert_feature` now keeps the name, as `add_feature` does; a blank name still gets a fresh one. Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
This commit is contained in:
@@ -84,3 +84,42 @@ fn corpus_designs_replay_from_their_scripts() {
|
||||
}
|
||||
assert!(replayed >= 7, "replayed {replayed} corpus designs");
|
||||
}
|
||||
|
||||
/// #1990: a sketch and its extrude may share a name, and the design's script still replays,
|
||||
/// keeping the names: the replayed design's own script has the same steps.
|
||||
#[test]
|
||||
fn a_design_naming_a_sketch_and_its_extrude_alike_replays_from_its_script() {
|
||||
let dir = std::env::temp_dir().join(format!("solidcraft-shared-names-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let cli = |args: &[&std::ffi::OsStr]| {
|
||||
let out = std::process::Command::new(env!("CARGO_BIN_EXE_solidcraft-cli")).args(args).output().unwrap();
|
||||
assert!(
|
||||
out.status.success(),
|
||||
"{}: {}",
|
||||
args.iter().map(|a| a.to_string_lossy()).collect::<Vec<_>>().join(" "),
|
||||
String::from_utf8_lossy(&out.stderr)
|
||||
);
|
||||
};
|
||||
let script = dir.join("mini.json");
|
||||
let saved = dir.join("mini.solidcraft");
|
||||
let exported = dir.join("rt.json");
|
||||
let replayed = dir.join("rt.solidcraft");
|
||||
let again = dir.join("rt2.json");
|
||||
let commands = json!({"commands": [
|
||||
{"command": "sketch.create", "params": {"plane": "XY", "name": "Block"}},
|
||||
{"command": "sketch.rectangle.two_point", "params": {"p0": [0, 0], "p1": [20, 10]}},
|
||||
{"command": "sketch.finish", "params": {}},
|
||||
{"command": "solid.extrude", "params": {"distance": "5", "name": "Block"}},
|
||||
]});
|
||||
std::fs::write(&script, commands.to_string()).unwrap();
|
||||
cli(&["run".as_ref(), script.as_ref(), "--save".as_ref(), saved.as_ref()]);
|
||||
cli(&["script".as_ref(), saved.as_ref(), "--out".as_ref(), exported.as_ref()]);
|
||||
cli(&["run".as_ref(), exported.as_ref(), "--save".as_ref(), replayed.as_ref()]);
|
||||
cli(&["script".as_ref(), replayed.as_ref(), "--out".as_ref(), again.as_ref()]);
|
||||
let first: Value = serde_json::from_str(&std::fs::read_to_string(&exported).unwrap()).unwrap();
|
||||
let second: Value = serde_json::from_str(&std::fs::read_to_string(&again).unwrap()).unwrap();
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let names: Vec<&str> = first["commands"].as_array().unwrap().iter().filter_map(|c| c["params"]["feature"]["name"].as_str()).collect();
|
||||
assert_eq!(names, ["Block", "Block"], "{first:#}");
|
||||
assert_eq!(second["commands"], first["commands"], "the replayed design's script has the same steps");
|
||||
}
|
||||
|
||||
@@ -2916,9 +2916,10 @@ impl Document {
|
||||
/// Insert a feature as given, keeping its id, name, parameter names and stored edge and
|
||||
/// face names, at the marker (or the end), with the model parameters its sketch dimensions
|
||||
/// use: a feature replayed from a script (`timeline.add`) is the feature the design had.
|
||||
/// An id of 0 or an empty name gets a new one. Refused, changing nothing, when the id, the
|
||||
/// name or a parameter name is already used, or the feature belongs to a component this
|
||||
/// design does not have.
|
||||
/// An id of 0 or an empty name gets a new one. The name is kept even when another feature
|
||||
/// has it, as a sketch and the feature built from it often share one (#1990). Refused,
|
||||
/// changing nothing, when the id or a parameter name is already used, or the feature
|
||||
/// belongs to a component this design does not have.
|
||||
pub fn insert_feature(&mut self, mut f: Feature, params: Vec<Param>) -> Result<u64> {
|
||||
if self.features.len() >= MAX_FEATURES {
|
||||
return Err(DocError::Invalid("too many features".into()));
|
||||
@@ -2933,8 +2934,6 @@ impl Document {
|
||||
f.name = f.name.trim().to_string();
|
||||
if f.name.is_empty() {
|
||||
f.name = self.unique_name(f.kind.base_name());
|
||||
} else if self.features.iter().any(|x| x.name == f.name) {
|
||||
return Err(DocError::Invalid(format!("a feature named `{}` already exists", f.name)));
|
||||
}
|
||||
if f.component != 0 && !self.components.iter().any(|c| c.id == f.component) {
|
||||
return Err(DocError::Invalid(format!("feature `{}` belongs to an unknown component", f.name)));
|
||||
|
||||
@@ -15,8 +15,8 @@ use crate::{EngineError, Result, Session};
|
||||
|
||||
pub static COMMANDS: &[CommandSpec] = &[
|
||||
CommandSpec::new("timeline.add", "Add Recorded Feature", add).icon("new").params(
|
||||
"feature: a feature as the design file and timeline.inspect hold it ({type, name?, id?, …}; id and name are kept when free, \
|
||||
so later steps can refer to them); params?: [{name, expression, unit?, comment?}] (the model parameters its sketch \
|
||||
"feature: a feature as the design file and timeline.inspect hold it ({type, name?, id?, …}; the name is kept even when another \
|
||||
feature has it, the id is kept and refused when another feature has it, so later steps can refer to them); params?: [{name, expression, unit?, comment?}] (the model parameters its sketch \
|
||||
dimensions use); the feature goes at the marker → feature, name, error, warning",
|
||||
),
|
||||
CommandSpec::new("timeline.export_script", "Export Command Script", export_script).icon("export").noundo().doc_only().params(
|
||||
|
||||
@@ -188,7 +188,6 @@ fn a_recorded_feature_that_does_not_fit_is_refused_and_changes_nothing() {
|
||||
};
|
||||
let refused = [
|
||||
("id taken", with(&extrude, &["feature", "name"], json!("Other"))),
|
||||
("name taken", with(&extrude, &["feature", "id"], json!(0))),
|
||||
("id out of range", with(&with(&extrude, &["feature", "id"], json!(1u64 << 60)), &["feature", "name"], json!("X"))),
|
||||
("parameter taken", with(&with(&sketch, &["feature", "id"], json!(0)), &["feature", "name"], json!("S2"))),
|
||||
(
|
||||
@@ -251,3 +250,45 @@ fn replayed_features_claiming_the_same_body_name_do_not_crash() {
|
||||
let m = run(&mut s, "inspect.measure", json!({}));
|
||||
assert!(m["body_count"].as_u64().is_some_and(|n| n >= 1), "{m}");
|
||||
}
|
||||
|
||||
/// #1990: a sketch and the extrude built from it may share a name, as the sample and oracle
|
||||
/// designs do; the design's script replays into the same design, names included.
|
||||
#[test]
|
||||
fn a_design_naming_a_sketch_and_its_extrude_alike_replays() {
|
||||
let mut s = Session::default();
|
||||
run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Block"}));
|
||||
run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [20, 10]}));
|
||||
run(&mut s, "sketch.finish", json!({}));
|
||||
run(&mut s, "solid.extrude", json!({"distance": "5", "name": "Block"}));
|
||||
let names = |t: &Session| t.doc.features.iter().map(|f| f.name.clone()).collect::<Vec<_>>();
|
||||
assert_eq!(names(&s), ["Block", "Block"]);
|
||||
let written = script(&mut s);
|
||||
assert_eq!(commands(&written), ["timeline.add", "timeline.add"]);
|
||||
let mut t = replay(&written);
|
||||
same(&mut s, &mut t);
|
||||
assert_eq!(names(&t), ["Block", "Block"], "the replay keeps both names");
|
||||
assert_eq!(volume(&mut t), 1000.0);
|
||||
// The replay's own script is the same script.
|
||||
assert_eq!(script(&mut t), written);
|
||||
}
|
||||
|
||||
/// #1990: `timeline.add` keeps a recorded name another feature has, as `solid.extrude` does.
|
||||
#[test]
|
||||
fn a_replayed_feature_keeps_a_name_another_feature_has() {
|
||||
let mut s = Session::default();
|
||||
run(&mut s, "solid.box", json!({"length": "5", "width": "5", "height": "5", "name": "Twin"}));
|
||||
let r = run(
|
||||
&mut s,
|
||||
"timeline.add",
|
||||
json!({"feature": {"type": "box", "name": "Twin", "length": "5", "width": "5", "height": "5", "corner": [20, 0, 0]}}),
|
||||
);
|
||||
assert_eq!(r["name"], "Twin");
|
||||
assert_eq!(s.doc.features.iter().filter(|f| f.name == "Twin").count(), 2);
|
||||
// A blank recorded name still gets a fresh one.
|
||||
let r = run(
|
||||
&mut s,
|
||||
"timeline.add",
|
||||
json!({"feature": {"type": "box", "name": " ", "length": "5", "width": "5", "height": "5", "corner": [40, 0, 0]}}),
|
||||
);
|
||||
assert_eq!(r["name"], "Box1");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user