//! The anonymous `*D` block written for a dimension (what other programs draw) holds its text at //! the height it is drawn at: the dimension's overrides and DIMSCALE (0 = the drawing's) count //! (issue #367). use cadcraft_doc::{Common, DimKind, Dimension, Drawing, EntityKind, Space}; use cadcraft_geom::Vec3; /// The group 40 (height) of the first MTEXT in the BLOCKS section. fn block_mtext_height(dxf: &str) -> f64 { let lines: Vec<&str> = dxf.lines().map(str::trim).collect(); let blocks = lines.windows(2).position(|w| w == ["2", "BLOCKS"]).unwrap(); let mtext = blocks + lines[blocks..].windows(2).position(|w| w == ["0", "MTEXT"]).unwrap(); let h = mtext + lines[mtext..].chunks(2).position(|w| w[0] == "40").unwrap() * 2; lines[h + 1].parse().unwrap() } fn height_written(style_scale: f64, overrides: serde_json::Value) -> f64 { let mut d = Drawing::new_imperial(); d.dim_styles.iter_mut().for_each(|s| s.scale = style_scale); let dm = Dimension { kind: DimKind::Linear { rotation: 0.0 }, defpt: Vec3::new(15.0, 5.0, 0.0), text_mid: Vec3::ZERO, p13: Vec3::ZERO, p14: Vec3::new(30.0, 0.0, 0.0), p15: Vec3::ZERO, p16: Vec3::ZERO, text: String::new(), style: "Standard".into(), measurement: 0.0, text_rotation: 0.0, user_text_pos: false, block: None, overrides: overrides.as_object().cloned().unwrap_or_default(), assoc: Vec::new(), marks: Default::default(), }; d.add(&Space::Model, Common::default(), EntityKind::Dimension(dm)).unwrap(); block_mtext_height(&cadcraft_io::write_dxf(&d)) } #[test] fn dimension_block_text_has_the_drawn_height() { let near = |a: f64, b: f64| (a - b).abs() < 1e-9; let plain = height_written(1.0, serde_json::json!({})); assert!(near(plain, 0.18), "{plain}"); let scaled = height_written(1.0, serde_json::json!({"scale": 10.0})); assert!(near(scaled, 1.8), "a DIMSCALE override scales the block text: {scaled}"); let txt = height_written(1.0, serde_json::json!({"DIMTXT": 0.5})); assert!(near(txt, 0.5), "a DIMTXT override sets the block text height: {txt}"); let zero = height_written(0.0, serde_json::json!({})); assert!(near(zero, 0.18), "DIMSCALE 0 in model space is scale 1, not ~0: {zero}"); }