Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions crates/engine/src/cmd/props/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
18 changes: 18 additions & 0 deletions crates/engine/tests/lin_pat_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Loading