diff --git a/crates/engine/src/cmd/props/set.rs b/crates/engine/src/cmd/props/set.rs index 51f6bc10e4..c4aeac503b 100644 --- a/crates/engine/src/cmd/props/set.rs +++ b/crates/engine/src/cmd/props/set.rs @@ -821,3 +821,41 @@ fn hatch(ha: &mut Hatch, c: &Call<'_>, used: &mut Used<'_>) { used.insert("angle"); } } + +#[cfg(test)] +mod tests { + use super::*; + + /// The hatch of a drawing, by handle. + fn hatch_of(s: &Session, h: Handle) -> Hatch { + match &s.doc().unwrap().entity(h).unwrap().kind { + EntityKind::Hatch(ha) => ha.clone(), + other => panic!("{h:?} is a {}", other.type_name()), + } + } + + #[test] + fn setting_the_same_pattern_keeps_the_lines_the_hatch_carries() { + // The loaded patterns are session-wide: HATCH.PATTERNS can clear them, and a drawing + // reopened without its .pat file leaves the hatch's own families as the only copy. + // Setting the name the hatch already carries must keep them (#1232). + let mut s = Session::new(); + let kind = EntityKind::Hatch( + serde_json::from_value(json!({ + "pattern": "MYGRID", + "solid": false, + "loops": [], + "patternLines": [{"angle": 0.0, "origin": [0.0, 0.0], "delta": [0.0, 0.5], "dashes": []}], + })) + .unwrap(), + ); + let h = s.add_entity(kind).unwrap(); + s.execute("properties.set", &json!({ "handles": [h.hex()], "pattern": "mygrid" })).unwrap(); + let ha = hatch_of(&s, h); + assert_eq!(ha.pattern, "MYGRID"); + assert_eq!(ha.pattern_lines.len(), 1, "the definition the hatch carries was dropped"); + // A library pattern is drawn from the library: it has no lines of its own. + s.execute("properties.set", &json!({ "handles": [h.hex()], "pattern": "ANSI31" })).unwrap(); + assert!(hatch_of(&s, h).pattern_lines.is_empty()); + } +} diff --git a/crates/engine/tests/lin_pat_files.rs b/crates/engine/tests/lin_pat_files.rs index 7a4aa68523..433a49fafb 100644 --- a/crates/engine/tests/lin_pat_files.rs +++ b/crates/engine/tests/lin_pat_files.rs @@ -210,3 +210,21 @@ fn hostile_support_files_never_panic() { } assert!(hatches(&s).is_empty()); } + +#[test] +fn properties_set_pattern_keeps_a_user_patterns_definition() { + let mut s = Session::new(); + s.execute("rectang", &json!({ "p1": [0, 0], "p2": [10, 10] })).unwrap(); + s.execute("hatch", &json!({ "points": [[5, 5]], "pattern": "MYGRID", "patternText": PAT })).unwrap(); + let hx = s.doc().unwrap().model.iter().find(|e| matches!(e.kind, EntityKind::Hatch(_))).unwrap().handle.hex(); + // Setting the same name again keeps the definition the hatch carries. + s.execute("properties.set", &json!({ "handles": [hx], "pattern": "MYGRID" })).unwrap(); + assert_eq!(hatches(&s).pop().unwrap().pattern_lines.len(), 2); + // Another loaded .pat pattern brings its own lines. + s.execute("properties.set", &json!({ "handles": [hx], "pattern": "MYSTEPS" })).unwrap(); + let h = hatches(&s).pop().unwrap(); + assert_eq!((h.pattern.as_str(), h.pattern_lines.first().map(|l| l.dashes.clone())), ("MYSTEPS", Some(vec![0.25, -0.25]))); + // A library pattern is drawn from the library, with no lines of its own. + s.execute("properties.set", &json!({ "handles": [hx], "pattern": "ANSI31" })).unwrap(); + assert!(hatches(&s).pop().unwrap().pattern_lines.is_empty()); +}