From 687e30afa2f619f106c2c57cf6be3fef7941e4cc Mon Sep 17 00:00:00 2001 From: midasmatterhorn <248507632+midasmatterhorn@users.noreply.github.com> Date: Sun, 1 Nov 2026 05:34:05 +0300 Subject: [PATCH 1/2] engine: keep a hatch's custom .pat definition when properties.set changes it --- crates/engine/src/cmd/props.rs | 8 +++----- crates/engine/tests/lin_pat_files.rs | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/props.rs b/crates/engine/src/cmd/props.rs index 8afaddb3d4..c44db9c57f 100644 --- a/crates/engine/src/cmd/props.rs +++ b/crates/engine/src/cmd/props.rs @@ -414,12 +414,10 @@ fn set_hatch_properties(hatch: &mut cadcraft_doc::Hatch, params: &Value) { let number = |key: &str| params.get(key).and_then(Value::as_f64).filter(|value| value.is_finite()); if let Some(v) = str_param(params, "pattern") { - if !hatch.pattern.eq_ignore_ascii_case(v) { - hatch.pattern_lines = super::hatch::lines_for(v); - } hatch.pattern = v.to_ascii_uppercase(); - // The old pattern's definition lines no longer apply. - hatch.pattern_lines.clear(); + // A library pattern is drawn from the library; a loaded .pat pattern carries its own + // line families; nothing else is known about the name. + hatch.pattern_lines = super::hatch::lines_for(&hatch.pattern); hatch.solid = hatch.pattern == "SOLID"; if !hatch.solid { // A named pattern replaces the gradient fill; the renderer draws the gradient first when it is set. diff --git a/crates/engine/tests/lin_pat_files.rs b/crates/engine/tests/lin_pat_files.rs index 91f99062ed..8522e11449 100644 --- a/crates/engine/tests/lin_pat_files.rs +++ b/crates/engine/tests/lin_pat_files.rs @@ -193,3 +193,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()); +} From 1cae514a0da2ef4ac8358284ea43491980124cfe Mon Sep 17 00:00:00 2001 From: midasmatterhorn <248507632+midasmatterhorn@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:04:17 +0300 Subject: [PATCH 2/2] engine: keep a hatch's line families when the pattern registry lost them --- crates/engine/src/cmd/props.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/props.rs b/crates/engine/src/cmd/props.rs index c44db9c57f..766e5bf36f 100644 --- a/crates/engine/src/cmd/props.rs +++ b/crates/engine/src/cmd/props.rs @@ -414,10 +414,16 @@ fn set_hatch_properties(hatch: &mut cadcraft_doc::Hatch, params: &Value) { let number = |key: &str| params.get(key).and_then(Value::as_f64).filter(|value| value.is_finite()); if let Some(v) = str_param(params, "pattern") { - hatch.pattern = v.to_ascii_uppercase(); + let name = v.to_ascii_uppercase(); // A library pattern is drawn from the library; a loaded .pat pattern carries its own - // line families; nothing else is known about the name. - hatch.pattern_lines = super::hatch::lines_for(&hatch.pattern); + // line families; nothing else is known about the name. Naming the pattern the hatch + // already carries keeps those families: the session-wide registry may no longer hold + // them (HATCH.PATTERNS cleared, or the drawing reopened without the .pat file), and + // re-setting the same name must not drop the definition (#1232). + if !hatch.pattern.eq_ignore_ascii_case(&name) || hatch.pattern_lines.is_empty() { + hatch.pattern_lines = super::hatch::lines_for(&name); + } + hatch.pattern = name; hatch.solid = hatch.pattern == "SOLID"; if !hatch.solid { // A named pattern replaces the gradient fill; the renderer draws the gradient first when it is set. @@ -1284,4 +1290,24 @@ mod tests { assert_eq!(lt(&s), ok); } } + + #[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 h: cadcraft_doc::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(); + set_hatch_properties(&mut h, &json!({ "pattern": "mygrid" })); + assert_eq!(h.pattern, "MYGRID"); + assert_eq!(h.pattern_lines.len(), 1, "the definition the hatch carries was dropped"); + // A name nothing is known about (the registry does not hold it) is still a rename. + set_hatch_properties(&mut h, &json!({ "pattern": "MYOTHER" })); + assert!(h.pattern_lines.is_empty()); + } }