a770fa5a5e5f
wblock_purge.rs
//! WBLOCK writes what the block or the selection uses, not the source drawing's whole tables
//! (#2423): AutoCAD's WBLOCK of a 5-object symbol writes its layer, its style and the defaults,
//! not the other 40 layers and 20 blocks of the drawing it came from.
use cadcraft_engine::Session;
use cadcraft_engine::cmd::file::{IoHooks, set_io};
use cadcraft_engine::doc::Drawing;
use serde_json::{Value, json};
#[path = "support/temp_dir.rs"]
mod temp_dir;
use temp_dir::TempDir;
/// Writes the written drawing's table names, one table a line: layers, text styles, blocks,
/// dimension styles, linetypes. One hook for every test here: `set_io` is process-wide and the
/// tests run in parallel.
fn write(d: &Drawing, _path: &str) -> Result<Vec<u8>, String> {
let mut blocks: Vec<&str> = d.blocks.values().map(|b| b.name.as_str()).collect();
blocks.sort_unstable();
let layers: Vec<&str> = d.layers.iter().map(|l| l.name.as_str()).collect();
let styles: Vec<&str> = d.text_styles.iter().map(|t| t.name.as_str()).collect();
let dims: Vec<&str> = d.dim_styles.iter().map(|t| t.name.as_str()).collect();
let linetypes: Vec<&str> = d.linetypes.iter().map(|t| t.name.as_str()).collect();
Ok(format!("{}\n{}\n{}\n{}\n{}", layers.join(","), styles.join(","), blocks.join(","), dims.join(","), linetypes.join(",")).into_bytes())
}
/// The layers, text styles and blocks of the file WBLOCK writes with `p`.
fn written(s: &mut Session, mut p: Value) -> (String, String, String) {
let dir = TempDir::new("wblock-purge");
let path = dir.path_str("out.dxf");
p["path"] = json!(path);
s.execute("wblock", &p).unwrap();
let text = std::fs::read_to_string(&path).unwrap();
let mut lines = text.lines().map(str::to_string);
(lines.next().unwrap_or_default(), lines.next().unwrap_or_default(), lines.next().unwrap_or_default())
}
fn line_on(s: &mut Session, layer: &str, y: f64) -> String {
let h = s.execute("line", &json!({ "points": [[0, y], [5, y]] })).unwrap()["handles"][0].as_str().unwrap().to_string();
s.execute("properties.set", &json!({ "handles": [h], "layer": layer })).unwrap();
h
}
/// Block SYM: a line on SYM-LAYER and a reference to INNER, whose line is on INNER-LAYER. The
/// drawing also has a layer, a text style and a block that SYM doesn't use.
fn drawing() -> Session {
set_io(IoHooks { read: |_, _| Err("no reader in this test".into()), write, plot: None });
let mut s = Session::new();
for name in ["SYM-LAYER", "INNER-LAYER", "UNUSED-LAYER", "OTHER-LAYER"] {
s.execute("layer.new", &json!({ "name": name })).unwrap();
}
s.execute("style", &json!({ "name": "UNUSED-STYLE" })).unwrap();
s.execute("style.current", &json!({ "name": "Standard" })).unwrap();
let inner = line_on(&mut s, "INNER-LAYER", 0.0);
s.execute("block", &json!({ "name": "INNER", "base": [0, 0], "handles": [inner], "keep": "delete" })).unwrap();
let r = s.execute("insert", &json!({ "name": "INNER", "at": [0, 5] })).unwrap();
let inner_ref = r["handle"].as_str().unwrap().to_string();
let own = line_on(&mut s, "SYM-LAYER", 10.0);
s.execute("block", &json!({ "name": "SYM", "base": [0, 0], "handles": [own, inner_ref], "keep": "delete" })).unwrap();
let other = line_on(&mut s, "OTHER-LAYER", 20.0);
s.execute("block", &json!({ "name": "OTHER", "base": [0, 0], "handles": [other], "keep": "delete" })).unwrap();
s
}
#[test]
fn a_block_is_written_with_what_it_uses() {
let mut s = drawing();
let (layers, styles, blocks) = written(&mut s, json!({ "name": "SYM" }));
let layers: Vec<&str> = layers.split(',').collect();
for used in ["0", "SYM-LAYER", "INNER-LAYER"] {
assert!(layers.contains(&used), "{used} in {layers:?}");
}
for unused in ["UNUSED-LAYER", "OTHER-LAYER"] {
assert!(!layers.contains(&unused), "{unused} in {layers:?}");
}
assert!(!styles.split(',').any(|n| n == "UNUSED-STYLE"), "{styles}");
assert!(styles.split(',').any(|n| n == "Standard"), "{styles}");
// The nested block comes along; the block being written is the file itself, and OTHER stays out.
assert_eq!(blocks, "INNER");
// The source drawing is untouched.
let d = s.doc().unwrap();
assert!(d.layer("UNUSED-LAYER").is_some() && d.block("OTHER").is_some());
}
#[test]
fn selected_objects_are_written_with_what_they_use() {
let mut s = drawing();
let r = s.execute("insert", &json!({ "name": "INNER", "at": [50, 50] })).unwrap();
let h = r["handle"].as_str().unwrap().to_string();
let (layers, _, blocks) = written(&mut s, json!({ "handles": [h] }));
assert_eq!(blocks, "INNER", "only the block the selection references");
let layers: Vec<&str> = layers.split(',').collect();
assert!(layers.contains(&"INNER-LAYER") && !layers.contains(&"SYM-LAYER"), "{layers:?}");
}
/// What the block uses through its styles stays too: a leader's dimension style, the linetype a
/// dimension style draws its dimension line with, and the text style of a complex linetype's text.
#[test]
fn styles_used_through_other_styles_stay() {
set_io(IoHooks { read: |_, _| Err("no reader in this test".into()), write, plot: None });
let mut s = Session::new();
s.execute("style", &json!({ "name": "LTEXT" })).unwrap();
s.execute("style.current", &json!({ "name": "Standard" })).unwrap();
let lin = "*TXT,Text --T--\nA,.5,-.2,[\"T\",LTEXT,S=.1,X=-.1,Y=-.05],-.2\n";
s.execute("linetype", &json!({ "load": "TXT", "text": lin })).unwrap();
s.execute("linetype", &json!({ "load": "DASHED" })).unwrap();
s.execute("dimstyle", &json!({ "name": "LDR", "current": true })).unwrap();
let leader = s.execute("leader", &json!({ "points": [[0, 0], [5, 5]], "text": "NOTE" })).unwrap()["handles"].clone();
s.execute("dimstyle", &json!({ "name": "DS", "dimLinetype": "DASHED", "current": true })).unwrap();
let dim = s.execute("dimlinear", &json!({ "p1": [0, 0], "p2": [10, 0], "at": [5, -3] })).unwrap()["handle"].clone();
s.execute("dimstyle", &json!({ "name": "Standard", "current": true })).unwrap();
let line = s.execute("line", &json!({ "points": [[0, 8], [9, 8]] })).unwrap()["handles"][0].clone();
s.execute("properties.set", &json!({ "handles": [line], "linetype": "TXT" })).unwrap();
let mut handles: Vec<Value> = leader.as_array().cloned().unwrap_or_default();
handles.extend([dim, line]);
s.execute("block", &json!({ "name": "SYM", "base": [0, 0], "handles": handles, "keep": "delete" })).unwrap();
let dir = TempDir::new("wblock-purge-styles");
let path = dir.path_str("out.dxf");
s.execute("wblock", &json!({ "name": "SYM", "path": path })).unwrap();
let text = std::fs::read_to_string(&path).unwrap();
let table = |n: usize| text.lines().nth(n).unwrap_or("").split(',').map(str::to_string).collect::<Vec<_>>();
let (styles, dims, linetypes) = (table(1), table(3), table(4));
for kept in ["LDR", "DS"] {
assert!(dims.iter().any(|n| n == kept), "dimension style {kept} kept: {dims:?}");
}
for kept in ["DASHED", "TXT"] {
assert!(linetypes.iter().any(|n| n == kept), "linetype {kept} kept: {linetypes:?}");
}
assert!(styles.iter().any(|n| n == "LTEXT"), "text style LTEXT kept: {styles:?}");
}
/// PURGE keeps a dimension style a leader uses (it only counted dimensions and tolerances).
#[test]
fn purge_keeps_a_leaders_dimension_style() {
let mut s = Session::new();
s.execute("dimstyle", &json!({ "name": "LDR", "current": true })).unwrap();
s.execute("leader", &json!({ "points": [[0, 0], [5, 5]], "text": "NOTE" })).unwrap();
s.execute("dimstyle", &json!({ "name": "Standard", "current": true })).unwrap();
s.execute("purge", &json!({ "type": "all" })).unwrap();
assert!(s.doc().unwrap().dim_style("LDR").is_some(), "the leader's style stays");
}
/// PURGE keeps a linetype only a multiline style's element uses, and a text style only a table
/// style's cell style uses (from the review of #2621).
#[test]
fn purge_keeps_styles_used_by_multiline_and_table_styles() {
use cadcraft_engine::doc::{CellStyle, MLineElement, MLineStyle, TableStyle};
let mut s = Session::new();
s.execute("linetype", &json!({ "load": "DASHED" })).unwrap();
s.execute("style", &json!({ "name": "CELLTEXT" })).unwrap();
s.execute("style.current", &json!({ "name": "Standard" })).unwrap();
let d = s.doc_mut().unwrap();
let element = MLineElement { linetype: "DASHED".into(), ..MLineElement::default() };
d.mline_styles.push(MLineStyle { name: "ML".into(), elements: vec![element], ..MLineStyle::default() });
let cell = CellStyle { text_style: "CELLTEXT".into(), ..CellStyle::default() };
d.table_styles.push(TableStyle { name: "TS".into(), cell_styles: vec![cell], ..TableStyle::default() });
// Both styles are made current, so PURGE keeps them; what they use must stay with them.
d.header.set_str("CMLSTYLE", "ML");
d.header.set_str("CTABLESTYLE", "TS");
s.execute("purge", &json!({ "type": "all" })).unwrap();
let d = s.doc().unwrap();
assert!(d.linetype("DASHED").is_some(), "the multiline style's linetype stays");
assert!(d.text_style("CELLTEXT").is_some(), "the table style's text style stays");
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.