mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 02:38:30 -06:00
* Write RevcloudProps xdata on revision clouds Fixes #1570 Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * REVCLOUD: arcs use the bulge readers need to recognise a cloud (follow-up to #1605, #1570) Readers take a closed polyline for a revision cloud only with RevcloudProps xdata (#1605) and every bulge within 0.02 of 0.520567; clouds used 0.5, just outside, so ezdxf's is_revcloud still said no. Clouds now use 0.520567050552 on every creation path. Tests check the reader's condition for all six ways of drawing a cloud and after a DXF round trip. * Parity checklist: regenerated after merging main * Parity checklist: regenerated after merging main * REVCLOUD: import RawTag in the revcloud module main split out of draw2 * Parity checklist: regenerated after merging main * Revcloud: update the recognition test for main's prompts (#1692) Main now asks for the minimum and maximum arc length and, after an object pick, whether to reverse direction, so the old scripts left a prompt waiting and the test read the previous entity. A helper now checks the prompts finished and exactly one new entity was created. * Revcloud: drop the file-format-parity.md change from this PR (#1692) The LWPOLYLINE note and changelog entry are moved to the PR description, as review asked. --------- Co-authored-by: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Co-authored-by: Claude Haiku 5.5 <noreply@anthropic.com> Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> Co-authored-by: SBaksa <18648812+SBaksa@users.noreply.github.com>
86 lines
4.5 KiB
Rust
86 lines
4.5 KiB
Rust
//! Readers recognise a saved revision cloud (#1570): a closed polyline with `RevcloudProps`
|
|
//! xdata whose every arc has bulge 0.520567 (they accept 0.02 either way; 0.5 was just outside).
|
|
|
|
use cadcraft_engine::Session;
|
|
use cadcraft_engine::doc::{Drawing, EntityKind, Handle, RawTag};
|
|
use serde_json::{Value, json};
|
|
|
|
const BULGE: f64 = 0.520567050552;
|
|
|
|
fn h(v: &Value) -> Handle {
|
|
v["handle"].as_str().and_then(Handle::parse_hex).unwrap()
|
|
}
|
|
|
|
fn last(s: &Session) -> Handle {
|
|
s.doc().unwrap().model.iter().last().map(|e| e.handle).unwrap()
|
|
}
|
|
|
|
/// Types `lines` at the command line and returns the one entity that run created. The prompt
|
|
/// sequence must have finished: a stale script leaves a prompt waiting, and `last` would then be
|
|
/// the previous entity, whose XDATA this test would wrongly accept. Counts new handles, not
|
|
/// entities: Object mode erases the picked shape as it adds the cloud.
|
|
fn typed_cloud(s: &mut Session, lines: &[&str]) -> Handle {
|
|
let before: Vec<Handle> = s.doc().unwrap().model.iter().map(|e| e.handle).collect();
|
|
for l in lines {
|
|
s.cmdline(l).unwrap();
|
|
}
|
|
assert!(!s.prompting(), "{lines:?} left a prompt waiting: {}", s.prompt_text());
|
|
let new: Vec<Handle> = s.doc().unwrap().model.iter().map(|e| e.handle).filter(|h| !before.contains(h)).collect();
|
|
assert_eq!(new.len(), 1, "{lines:?} must create exactly one entity, got {new:?}");
|
|
assert_eq!(last(s), new[0], "{lines:?}: the new cloud must be the last entity");
|
|
new[0]
|
|
}
|
|
|
|
fn props(xdata: &[RawTag]) -> Vec<(i32, String)> {
|
|
let start = xdata.iter().position(|t| t.code == 1001 && t.value == "RevcloudProps").expect("RevcloudProps xdata");
|
|
xdata[start..].iter().take_while(|t| t.code != 1001 || t.value == "RevcloudProps").map(|t| (t.code, t.value.trim().to_string())).collect()
|
|
}
|
|
|
|
/// The cloud `h` is what readers take for a cloud: closed, cloud bulges, `RevcloudProps`.
|
|
fn assert_cloud(d: &Drawing, h: Handle, arc_len: f64) {
|
|
let e = d.entity(h).unwrap();
|
|
let EntityKind::LwPolyline(p) = &e.kind else { panic!("{:?}", e.kind) };
|
|
assert!(p.closed);
|
|
assert!(p.vertices.iter().all(|v| (v.bulge.abs() - BULGE).abs() < 1e-9), "{:?}", p.vertices);
|
|
let x = props(&e.common.xdata);
|
|
assert_eq!(x.len(), 3, "{x:?}");
|
|
assert_eq!(x[1], (1070, "0".to_string()));
|
|
assert_eq!(x[2].0, 1040);
|
|
assert!((x[2].1.parse::<f64>().unwrap() - arc_len).abs() < 1e-12, "{x:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn every_way_of_drawing_a_cloud_is_recognised() {
|
|
let mut s = Session::new();
|
|
let rect = h(&s.execute("revcloud", &json!({"p1": [0, 0], "p2": [100, 50], "arcLength": 15})).unwrap());
|
|
let poly = h(&s.execute("revcloud.polygonal", &json!({"points": [[0, 0], [0, 30], [30, 0]], "arcLength": 5})).unwrap());
|
|
let rev = h(&s.execute("revcloud.freehand", &json!({"freehand": [[0, 0], [50, 0], [50, 50]], "arcLength": 10, "reverse": true})).unwrap());
|
|
let circle = h(&s.execute("circle", &json!({"center": [200, 0], "radius": 20})).unwrap());
|
|
let obj = h(&s.execute("revcloud", &json!({"handle": circle, "arcLength": 4})).unwrap());
|
|
// Arc length asks the minimum and then the maximum (#1714).
|
|
let typed = typed_cloud(&mut s, &["revcloud a 2 2", "300,0 320,10"]);
|
|
s.execute("rectang", &json!({"p1": [400, 0], "p2": [420, 20]})).unwrap();
|
|
// Object then asks "Reverse direction" after the pick.
|
|
let picked = typed_cloud(&mut s, &["revcloud a 3 3 o 400,10 n"]);
|
|
let d = s.doc().unwrap();
|
|
for (h, a) in [(rect, 15.0), (poly, 5.0), (rev, 10.0), (obj, 4.0), (typed, 2.0), (picked, 3.0)] {
|
|
assert_cloud(d, h, a);
|
|
}
|
|
let EntityKind::LwPolyline(p) = &d.entity(rev).unwrap().kind else { panic!() };
|
|
assert!(p.vertices.iter().all(|v| v.bulge < 0.0), "a reversed cloud keeps its arcs turned inward");
|
|
}
|
|
|
|
#[test]
|
|
fn a_saved_cloud_reads_back_as_a_cloud() {
|
|
let mut s = Session::new();
|
|
s.execute("revcloud", &json!({"p1": [0, 0], "p2": [100, 50], "arcLength": 15})).unwrap();
|
|
let text = cadcraft_io::write_dxf(s.doc().unwrap());
|
|
let lines: Vec<&str> = text.lines().map(str::trim).collect();
|
|
let appid = lines.windows(2).any(|w| w[0] == "0" && w[1] == "APPID")
|
|
&& lines.windows(4).any(|w| w[0] == "AcDbRegAppTableRecord" && w[1] == "2" && w[2] == "RevcloudProps");
|
|
assert!(appid, "RevcloudProps is a registered application");
|
|
let read = cadcraft_io::read_dxf(text.as_bytes()).unwrap();
|
|
let cloud = read.model.iter().find(|e| matches!(e.kind, EntityKind::LwPolyline(_))).map(|e| e.handle).unwrap();
|
|
assert_cloud(&read, cloud, 15.0);
|
|
}
|