diff --git a/crates/engine/src/cmd/blocks/attedit.rs b/crates/engine/src/cmd/blocks/attedit.rs index 5cf06b03a..b89434251 100644 --- a/crates/engine/src/cmd/blocks/attedit.rs +++ b/crates/engine/src/cmd/blocks/attedit.rs @@ -220,7 +220,7 @@ fn modify_attrib(s: &mut Session, (h, k): (Handle, usize), f: impl FnOnce(&mut c } /// The layer and colour attribute `k` of block reference `h` is drawn with: its own, else its -/// definition's (layer 0 is the reference's layer). +/// definition's (layer 0 from the definition is the reference's layer). fn current_props(s: &Session, (h, k): (Handle, usize)) -> Option<(String, Color)> { let d = s.doc().ok()?; let e = d.entity(h)?; @@ -230,7 +230,7 @@ fn current_props(s: &Session, (h, k): (Handle, usize)) -> Option<(String, Color) .block(&ins.block) .and_then(|b| b.entities.iter().find(|be| matches!(&be.kind, EntityKind::AttDef(ad) if ad.tag.eq_ignore_ascii_case(&a.tag)))); let c = a.props.over(&def.map(|de| de.common.clone()).unwrap_or_else(Common::default)); - let layer = if c.layer == "0" { e.common.layer.clone() } else { c.layer }; + let layer = if a.props.layer.is_none() && c.layer == "0" { e.common.layer.clone() } else { c.layer }; Some((layer, c.color)) } diff --git a/crates/engine/src/cmd/blocks/burst.rs b/crates/engine/src/cmd/blocks/burst.rs index b7bcb4b37..bc7678081 100644 --- a/crates/engine/src/cmd/blocks/burst.rs +++ b/crates/engine/src/cmd/blocks/burst.rs @@ -41,13 +41,13 @@ fn burst_pieces(d: &Drawing, e: &Entity) -> Option> { } /// The properties an attribute is drawn with: its own, else those of its definition `def`; layer -/// 0 is the reference's layer and ByBlock colour the reference's colour, as EXPLODE gives the -/// block's objects. +/// 0 from the definition is the reference's layer (an attribute set on layer 0 itself stays there) +/// and ByBlock colour the reference's colour, as EXPLODE gives the block's objects. fn attrib_common(e: &Entity, def: Option<&Entity>, props: &cadcraft_doc::AttribProps) -> Common { // An attribute without a definition follows the reference. let base = def.map(|de| de.common.clone()).unwrap_or_else(|| Common { color: Color::ByBlock, ..Common::default() }); let mut c = props.over(&base); - if c.layer == "0" { + if props.layer.is_none() && c.layer == "0" { c.layer.clone_from(&e.common.layer); } if c.color == Color::ByBlock { diff --git a/crates/io/src/dxf_write/common.rs b/crates/io/src/dxf_write/common.rs index a8fda3257..a0436c2f2 100644 --- a/crates/io/src/dxf_write/common.rs +++ b/crates/io/src/dxf_write/common.rs @@ -146,13 +146,14 @@ pub(super) fn layer_and_props(w: &mut W, c: &Common, base: Option<&Common>) { } /// The properties attribute `a` of block reference `e` is drawn with: its own, else its -/// definition's, from the block's definitions by tag `defs`; an attribute defined on layer 0 is on -/// the reference's layer. Also the definition's properties. +/// definition's, from the block's definitions by tag `defs`; an attribute that takes layer 0 from +/// its definition is on the reference's layer, one set on layer 0 itself stays there. Also the +/// definition's properties. pub(super) fn attrib_common(e: &Entity, defs: &std::collections::BTreeMap, &Entity>, a: &Attrib) -> (Common, Common) { let def = defs.get(&cadcraft_doc::TagKey(&a.tag)); let base = def.map(|de| de.common.clone()).unwrap_or_default(); let mut c = a.props.over(&base); - if c.layer == "0" { + if a.props.layer.is_none() && c.layer == "0" { c.layer.clone_from(&e.common.layer); } (c, base) diff --git a/crates/io/tests/it/attrib_props.rs b/crates/io/tests/it/attrib_props.rs index 4787dd8c2..3727a8a2b 100644 --- a/crates/io/tests/it/attrib_props.rs +++ b/crates/io/tests/it/attrib_props.rs @@ -167,3 +167,40 @@ fn hostile_attrib_groups_do_not_panic() { assert_eq!((p.layer.as_deref(), p.linetype.as_deref()), (None, None), "empty names are not properties"); let _ = write_dxf(&d); } + +/// The layer and colour of each attribute of each block reference in `d`, in file order. +fn layers_and_colors(d: &Drawing) -> Vec<(Option, Option)> { + let attribs = d.model.iter().filter_map(|e| if let EntityKind::Insert(i) = &e.kind { Some(i.attribs.clone()) } else { None }); + attribs.flatten().map(|a| (a.props.layer, a.props.color)).collect() +} + +/// #3492: an ATTRIB on layer 0 of a reference on layer A stays on layer 0 when the drawing is read +/// and saved again, as DXF and as DWG; layer 0 means the reference's layer only for a definition. +#[test] +fn attribute_on_layer_zero_stays_on_layer_zero() { + cadcraft_io::dwg_worker::allow_in_process(); + let attrib = |layer: &str, color: &str| format!("0\nATTRIB\n8\n{layer}\n{color}10\n0\n20\n1\n30\n0\n40\n1\n1\nv\n2\nT1\n70\n0\n"); + let insert = |x: u32, attrib: String| format!("0\nINSERT\n8\nA\n66\n1\n2\nBLK\n10\n{x}\n20\n0\n30\n0\n{attrib}0\nSEQEND\n8\nA\n"); + let dxf = format!( + "0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLAYER\n70\n2\n0\nLAYER\n2\nA\n70\n0\n62\n3\n6\nCONTINUOUS\n\ +0\nLAYER\n2\nB\n70\n0\n62\n5\n6\nCONTINUOUS\n0\nENDTAB\n0\nENDSEC\n\ +0\nSECTION\n2\nBLOCKS\n0\nBLOCK\n8\n0\n2\nBLK\n70\n2\n10\n0\n20\n0\n30\n0\n3\nBLK\n\ +0\nLINE\n8\n0\n10\n0\n20\n0\n30\n0\n11\n5\n21\n0\n31\n0\n\ +0\nATTDEF\n8\n0\n10\n0\n20\n1\n30\n0\n40\n1\n1\nx\n3\n\n2\nT1\n70\n0\n0\nENDBLK\n8\n0\n0\nENDSEC\n\ +0\nSECTION\n2\nENTITIES\n{}{}{}0\nENDSEC\n0\nEOF\n", + insert(0, attrib("0", "")), + insert(10, attrib("B", "")), + insert(20, attrib("0", "62\n1\n")), + ); + let d = cadcraft_io::read(dxf.as_bytes(), "t.dxf").expect("read"); + let read = layers_and_colors(&d); + assert_eq!(read.len(), 3); + let layers: Vec> = read.iter().map(|(l, _)| l.as_deref()).collect(); + assert_eq!(layers, [Some("0"), Some("B"), Some("0")], "as the file says"); + assert_eq!(read[2].1, Some(Color::Index(1))); + for name in ["t.dxf", "t.dwg"] { + let saved = cadcraft_io::write(&d, name).expect("write"); + let back = cadcraft_io::read(&saved, name).expect("reopen"); + assert_eq!(layers_and_colors(&back), read, "{name}: attribute layers or colours changed"); + } +} diff --git a/crates/render/src/blocks.rs b/crates/render/src/blocks.rs index 5804bfe74..e5686c211 100644 --- a/crates/render/src/blocks.rs +++ b/crates/render/src/blocks.rs @@ -209,13 +209,19 @@ fn attributes(b: &mut Builder, ctx: &Ctx, e: &Entity, ins: &cadcraft_doc::Insert let sh = if multiline.is_none() { Some(place_text_entity(ctx.d, &a.text, &a.text.value).0) } else { None }; // Properties of the attribute's definition (as AutoCAD copies them to the attribute // on insertion), except those set on the attribute itself: ByBlock = the reference's, - // layer 0 = the reference's layer. + // layer 0 from the definition = the reference's layer. let def = defs.get(&a.tag.to_ascii_lowercase()).copied(); let (ink, lw, shown, t) = match (def, a.props.is_empty()) { (None, true) => (rgb, resolve(ctx, e, b.plotting).1, true, reference), (def, _) => { let c = a.props.over(&def.map(|d| d.common.clone()).unwrap_or_else(by_block)); - let (ink, lw, _, _, shown, layer_t, fade) = resolve_common(&sub_ctx(ctx, e, Mat3::IDENTITY), &c, b.plotting); + let mut sub = sub_ctx(ctx, e, Mat3::IDENTITY); + // An attribute set on layer 0 itself is on layer 0 where its reference is, + // not on the reference's layer. + if a.props.layer.is_some() { + sub.block_layer.clone_from(&ctx.block_layer); + } + let (ink, lw, _, _, shown, layer_t, fade) = resolve_common(&sub, &c, b.plotting); // ByBlock takes the reference's own transparency, not its locked-layer fade. let t = if b.opts.transparency { transparency(c.transparency, layer_t, own) } else { 0 }; let t = if b.opts.on_screen { t.max(fade) } else { t }; diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index b5cbbea06..a5d0cc35e 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -1020,6 +1020,32 @@ fn attributes_follow_the_reference_layer_and_have_no_definition() { } } +#[test] +fn attribute_set_on_layer_zero_does_not_take_the_reference_layer() { + // #3492: a layer-0 definition in a reference on a 40 % layer. An attribute that takes layer 0 + // from its definition is drawn on the reference's layer, like the line; one set on layer 0 + // itself is drawn on the opaque layer 0. + let drawn = |layer: Option<&str>| { + let mut d = Drawing::new_imperial(); + let mut blk = cadcraft_doc::Block::new("B"); + let (c, k) = line_common(Common::default()); + blk.entities.push(block_entity(1, c, k)); + blk.entities.push(block_entity(2, Common::default(), EntityKind::AttDef(attrib("TAG")))); + d.blocks.insert("B".into(), std::sync::Arc::new(blk)); + let EntityKind::Insert(mut ins) = insert_of("B") else { panic!("an insert") }; + let props = cadcraft_doc::AttribProps { layer: layer.map(Into::into), ..Default::default() }; + ins.attribs.push(cadcraft_doc::Attrib { props, ..attrib("TAG") }); + d.add(&Space::Model, Common { layer: "Glass".into(), ..Default::default() }, EntityKind::Insert(ins)).unwrap(); + d.layer_mut("Glass").unwrap().transparency = 40; + line_and_text(&d) + }; + let (inherited_line, inherited) = drawn(None); + let (line, own) = drawn(Some("0")); + assert_eq!(line, inherited_line, "the block's contents still take the reference's layer"); + assert!(inherited.iter().all(|t| *t == line), "{inherited:?}"); + assert!(own.iter().all(|t| *t < line), "{own:?}"); +} + #[test] fn mtext_exact_spacing_ignores_taller_lines() { // #711: a doubled middle line pushes the next baseline down with At least, not with Exactly.