diff --git a/docs/AUTHORING-DEVELOPMENT.md b/docs/AUTHORING-DEVELOPMENT.md index d66d8a6..cc22c80 100644 --- a/docs/AUTHORING-DEVELOPMENT.md +++ b/docs/AUTHORING-DEVELOPMENT.md @@ -74,3 +74,7 @@ Tests exercise quoted library text, exact pin transforms, stable identity, retri undo, batch rollback, persistence, library-unit naming and unintended capacitor-pin contact detection. Live native load, SVG and ERC evidence belongs with the worked project and exact exported file hash. + +### Native field and label placement + +The KiCad exporter retains library Reference/Value positions and effects, transforms them with the placed component, and directs pin-attached net labels away from the symbol body. A rotation test covers all four right-angle orientations and preserves electrical anchors. Native revision-21 SVG inspection confirms the central TMF8829 artwork is no longer covered by its value. General field/label collision avoidance remains open: vertical ground labels can overlap capacitor and regulator fields. Native rendering is not electrical qualification. diff --git a/src/author.rs b/src/author.rs index 7045541..cef1f17 100644 --- a/src/author.rs +++ b/src/author.rs @@ -634,9 +634,46 @@ impl Document { for c in self.components.values() { let sid = id(&self.id, &format!("component:{}", c.reference)); out+=&format!("(symbol (lib_id {}) (at {} {} {}) (unit 1) (in_bom yes) (on_board yes) (dnp no) (uuid {sid})\n",q(&format!("Adom:{}",c.library)),c.x,c.y,c.rotation); - out+=&format!("(property \"Reference\" {} (at {} {} 0) (effects (font (size 1.27 1.27)) (justify left)))\n",q(&c.reference),c.x+2.54,c.y-2.54); - out+=&format!("(property \"Value\" {} (at {} {} 0) (effects (font (size 1.27 1.27)) (justify left)))\n",q(&c.value),c.x+2.54,c.y); let lib = library_root(&self.libraries[&c.library].symbol)?; + // Field locations are part of the component library. Putting every + // value at the instance origin covers chip artwork and pin names. + for (key, value, fallback_y) in [ + ("Reference", c.reference.as_str(), -2.54), + ("Value", c.value.as_str(), 0.0), + ] { + if let Some(source) = lib + .list() + .iter() + .find(|p| p.tag() == Some("property") && p.arg(1).ok() == Some(key)) + { + let mut field = source.clone(); + let at = source + .child("at") + .context("Library field missing position")?; + let x: f64 = at.arg(1)?.parse()?; + let y: f64 = at.arg(2)?.parse()?; + let angle: f64 = at.arg(3)?.parse()?; + if ![x, y, angle].iter().all(|v| v.is_finite()) { + bail!("Non-finite library field coordinate") + } + let a = -(c.rotation as f64).to_radians(); + let sx = c.x + x * a.cos() + y * a.sin(); + let sy = c.y + x * a.sin() - y * a.cos(); + if let S::List(items) = &mut field { + items[2] = S::Text(value.into()); + items.retain(|p| p.tag() != Some("id")); + let position = items.iter_mut().find(|p| p.tag() == Some("at")).unwrap(); + *position = parse(&format!( + "(at {sx} {sy} {})", + (angle + c.rotation as f64).rem_euclid(180.0) + ))?; + } + out += &field.emit(); + out += "\n"; + } else { + out+=&format!("(property {} {} (at {} {} 0) (effects (font (size 1.27 1.27)) (justify left)))\n",q(key),q(value),c.x+2.54,c.y+fallback_y); + } + } for k in ["Footprint", "Datasheet", "Manufacturer", "Part Number"] { if let Some(p) = lib .list() @@ -687,7 +724,15 @@ impl Document { } for l in &self.labels { let a = self.endpoint(&l.endpoint)?; - out+=&format!("(label {} (at {} {} 0) (effects (font (size 1.27 1.27)) (justify left bottom)) (uuid {}))\n",q(&l.net),a[0],a[1],id(&self.id,&format!("label:{}:{}",l.endpoint.reference,l.endpoint.pin))); + let c = &self.components[&l.endpoint.reference]; + let pin = pins(&self.libraries[&c.library].symbol)? + .into_iter() + .find(|p| p.number == l.endpoint.pin) + .context("Unknown label pin")?; + // Library pin angles point toward the body; label text extends out. + let angle = (pin.angle + c.rotation as f64 + 180.0).rem_euclid(360.0); + let justify = if angle < 180.0 { "left" } else { "right" }; + out+=&format!("(label {} (at {} {} {angle}) (effects (font (size 1.27 1.27)) (justify {justify} bottom)) (uuid {}))\n",q(&l.net),a[0],a[1],id(&self.id,&format!("label:{}:{}",l.endpoint.reference,l.endpoint.pin))); } out += ")\n"; parse(&out)?; @@ -731,6 +776,53 @@ mod tests { .unwrap() } #[test] + fn library_fields_and_outward_labels_follow_rotation() { + let mut p = placed(); + p.document.labels.push(Label { + endpoint: PinRef { + reference: "C1".into(), + pin: "1".into(), + }, + net: "SUPPLY".into(), + }); + // The manufacturer's field is (+5.08,+5.08) in Y-up library space. + // Its upper vertical pin points down toward the body (270 degrees). + for (rotation, x, y, label_angle) in [ + (0, 55.08, 44.92, 90.0), + (90, 44.92, 44.92, 180.0), + (180, 44.92, 55.08, 270.0), + (270, 55.08, 55.08, 0.0), + ] { + p.document.components.get_mut("C1").unwrap().rotation = rotation; + let exported = parse(&p.document.export_kicad().unwrap()).unwrap(); + let placed = exported + .list() + .iter() + .find(|n| n.tag() == Some("symbol")) + .unwrap(); + let reference = placed + .list() + .iter() + .find(|n| n.tag() == Some("property") && n.arg(1).ok() == Some("Reference")) + .unwrap(); + assert_eq!(reference.arg(2).unwrap(), "C1"); + let at = reference.child("at").unwrap(); + assert!((at.arg(1).unwrap().parse::().unwrap() - x).abs() < 1e-8); + assert!((at.arg(2).unwrap().parse::().unwrap() - y).abs() < 1e-8); + assert!(reference.child("id").is_none()); + let label = exported + .list() + .iter() + .find(|n| n.tag() == Some("label")) + .unwrap(); + let at = label.child("at").unwrap(); + assert_eq!(at.arg(3).unwrap().parse::().unwrap(), label_angle); + let endpoint = p.document.endpoint(&p.document.labels[0].endpoint).unwrap(); + assert!((at.arg(1).unwrap().parse::().unwrap() - endpoint[0]).abs() < 1e-8); + assert!((at.arg(2).unwrap().parse::().unwrap() - endpoint[1]).abs() < 1e-8); + } + } + #[test] fn embedded_library_units_follow_exported_identity() { let p = placed(); let out = p.document.export_kicad().unwrap();