From a6ba747329634706a5c938fe73492f558f46ffdd Mon Sep 17 00:00:00 2001 From: MrBeeboh Date: Thu, 22 Oct 2026 20:20:11 -0700 Subject: [PATCH 001/100] dxf: raise the group-code limit to 150 million and allow CADCRAFT_MAX_DXF_TAGS to override it (#1948) --- crates/dxf/src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/crates/dxf/src/lib.rs b/crates/dxf/src/lib.rs index 82ecf55e8..b824e4e57 100644 --- a/crates/dxf/src/lib.rs +++ b/crates/dxf/src/lib.rs @@ -162,16 +162,31 @@ fn typed(code: i32, text: &str) -> Value { } const BINARY_SENTINEL: &[u8] = b"AutoCAD Binary DXF\r\n\x1a\0"; -/// Most group codes read from one file. A `Tag` takes 40 bytes plus its text, so this bounds the -/// tag list to a few GB; real drawings stay far below (a 10 MB DWG is typically a few million). -pub const MAX_TAGS: usize = 50_000_000; +/// Most group codes read from one file by default. A `Tag` takes 40 bytes plus its text, so this +/// bounds the tag list to roughly 6 GB plus text. It matches the 1 GB DXF rendition a DWG may +/// convert to (a 1 GB DXF holds well over 50 million group codes, so a lower cap rejected large +/// DWGs that converted fine, issue #1948). Set `CADCRAFT_MAX_DXF_TAGS` to raise or lower it. +pub const MAX_TAGS: usize = 150_000_000; + +/// Environment variable overriding [`MAX_TAGS`] (a positive integer). +pub const MAX_TAGS_ENV: &str = "CADCRAFT_MAX_DXF_TAGS"; + +/// The group-code limit in effect: [`MAX_TAGS_ENV`] when set to a positive integer, else +/// [`MAX_TAGS`]. +pub fn max_tags() -> usize { + max_tags_from(std::env::var(MAX_TAGS_ENV).ok().as_deref()) +} + +fn max_tags_from(v: Option<&str>) -> usize { + v.and_then(|s| s.trim().replace('_', "").parse::().ok()).filter(|&n| n > 0).unwrap_or(MAX_TAGS) +} mod codepage; pub use codepage::decode_code_page; /// Parse a DXF file (ASCII or binary). pub fn parse(bytes: &[u8]) -> Result> { - parse_with_limit(bytes, MAX_TAGS) + parse_with_limit(bytes, max_tags()) } /// [`parse`] with an explicit group-code limit. For ASCII DXF the limit is checked up front from @@ -258,7 +273,7 @@ fn unescape_unicode(s: &str) -> String { } pub fn parse_ascii(text: &str) -> Result> { - parse_ascii_with_limit(text, MAX_TAGS) + parse_ascii_with_limit(text, max_tags()) } #[allow(clippy::while_let_loop)] @@ -344,7 +359,7 @@ impl<'a> Cur<'a> { /// Binary DXF (R13+: 2-byte group codes). pub fn parse_binary(b: &[u8]) -> Result> { - parse_binary_with_limit(b, MAX_TAGS) + parse_binary_with_limit(b, max_tags()) } fn parse_binary_with_limit(b: &[u8], max_tags: usize) -> Result> { @@ -683,3 +698,19 @@ mod tests { assert_eq!(parse(&b).unwrap()[0].str(), "图号"); } } + +#[cfg(test)] +mod limit_tests { + use super::*; + + #[test] + fn tag_limit_defaults_and_env_override() { + assert_eq!(max_tags_from(None), MAX_TAGS); + assert_eq!(max_tags_from(Some("")), MAX_TAGS); + assert_eq!(max_tags_from(Some("0")), MAX_TAGS); + assert_eq!(max_tags_from(Some("abc")), MAX_TAGS); + assert_eq!(max_tags_from(Some(" 400_000_000 ")), 400_000_000); + // Issue #1948: the default must admit a DXF rendition well above 50 million group codes. + assert!(MAX_TAGS >= 100_000_000); + } +} From 6ed822ec30e6e8ab7b3cc137ad0cbcd2e8e324da Mon Sep 17 00:00:00 2001 From: MrBeeboh Date: Thu, 22 Oct 2026 20:22:09 -0700 Subject: [PATCH 002/100] Plot style and plotter files save atomically through write_atomic (#1928 follow-up) --- crates/engine/src/cmd/layout/plotstyle.rs | 5 ++--- crates/engine/src/cmd/plotters.rs | 4 +--- crates/engine/tests/plot_styles.rs | 27 +++++++++++++++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/layout/plotstyle.rs b/crates/engine/src/cmd/layout/plotstyle.rs index b92b15826..48e22f5ce 100644 --- a/crates/engine/src/cmd/layout/plotstyle.rs +++ b/crates/engine/src/cmd/layout/plotstyle.rs @@ -332,9 +332,8 @@ fn run_stylesmanager(s: &mut Session, p: &Value) -> Result { Some(_path) => { #[cfg(not(target_arch = "wasm32"))] { - let tmp = format!("{_path}.cadcraft-tmp"); - std::fs::write(&tmp, text.as_bytes()).map_err(|e| bad(cmd, format!("{_path}: {e}")))?; - std::fs::rename(&tmp, _path).map_err(|e| bad(cmd, format!("{_path}: {e}")))?; + crate::atomic_write::write_atomic(std::path::Path::new(_path), text.as_bytes()) + .map_err(|e| bad(cmd, format!("{_path}: {e}")))?; Ok(json!({ "name": name, "path": _path, "bytes": text.len() })) } #[cfg(target_arch = "wasm32")] diff --git a/crates/engine/src/cmd/plotters.rs b/crates/engine/src/cmd/plotters.rs index 454b977ae..c8ea9b9a8 100644 --- a/crates/engine/src/cmd/plotters.rs +++ b/crates/engine/src/cmd/plotters.rs @@ -110,9 +110,7 @@ impl PlotterStore { write: |n, t| { let p = file(n)?; std::fs::create_dir_all(dir()?).map_err(|e| e.to_string())?; - let tmp = p.with_extension("json.cadcraft-tmp"); - std::fs::write(&tmp, t).map_err(|e| format!("{n}: {e}"))?; - std::fs::rename(&tmp, &p).map_err(|e| format!("{n}: {e}")) + crate::atomic_write::write_atomic(&p, t.as_bytes()).map_err(|e| format!("{n}: {e}")) }, delete: |n| std::fs::remove_file(file(n)?).map_err(|e| format!("{n}: {e}")), } diff --git a/crates/engine/tests/plot_styles.rs b/crates/engine/tests/plot_styles.rs index 06f8cb424..4ad0922c3 100644 --- a/crates/engine/tests/plot_styles.rs +++ b/crates/engine/tests/plot_styles.rs @@ -104,3 +104,30 @@ fn stylesmanager_creates_edits_saves_and_loads_tables() { // Undo takes a table edit back. s.execute("undo", &json!({})).unwrap(); } + +/// Issue #1928: saving a plot style table to a file goes through the atomic writer: a symlink +/// planted at the old fixed temporary name is left alone, and the file keeps its permissions. +#[cfg(unix)] +#[test] +fn plot_style_save_is_atomic_and_keeps_permissions() { + use std::os::unix::fs::PermissionsExt; + let dir = std::env::temp_dir().join(format!("cadcraft-pstyle-atomic-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("office.stb.json"); + std::fs::write(&path, "old").unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap(); + let victim = dir.join("victim"); + std::fs::write(&victim, "keep").unwrap(); + std::os::unix::fs::symlink(&victim, dir.join("office.stb.json.cadcraft-tmp")).unwrap(); + + let mut s = Session::new(); + s.execute("stylesmanager", &json!({"action": "new", "name": "office.stb"})).unwrap(); + let p = path.to_string_lossy().to_string(); + s.execute("stylesmanager", &json!({"action": "save", "name": "office.stb", "path": p})).unwrap(); + + assert_ne!(std::fs::read_to_string(&path).unwrap(), "old"); + assert_eq!(std::fs::read_to_string(&victim).unwrap(), "keep"); + assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600); + let _ = std::fs::remove_dir_all(&dir); +} From a13870f376c4e6f6d7e7814ab8238d74424117d5 Mon Sep 17 00:00:00 2001 From: MrBeeboh Date: Thu, 22 Oct 2026 20:56:55 -0700 Subject: [PATCH 003/100] dxf: check the tag limit in a const block --- crates/dxf/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/dxf/src/lib.rs b/crates/dxf/src/lib.rs index b824e4e57..4eacd5cfc 100644 --- a/crates/dxf/src/lib.rs +++ b/crates/dxf/src/lib.rs @@ -711,6 +711,6 @@ mod limit_tests { assert_eq!(max_tags_from(Some("abc")), MAX_TAGS); assert_eq!(max_tags_from(Some(" 400_000_000 ")), 400_000_000); // Issue #1948: the default must admit a DXF rendition well above 50 million group codes. - assert!(MAX_TAGS >= 100_000_000); + const { assert!(MAX_TAGS >= 100_000_000) }; } } From 62b9b5acd468a5988bccb01625f7beb867e64ce7 Mon Sep 17 00:00:00 2001 From: midasmatterhorn <248507632+midasmatterhorn@users.noreply.github.com> Date: Tue, 27 Oct 2026 00:58:10 +0300 Subject: [PATCH 004/100] engine: save, render and export create a missing target folder instead of failing with os error 3 --- crates/engine/src/atomic_write.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/engine/src/atomic_write.rs b/crates/engine/src/atomic_write.rs index ff9e141d3..cd7d0fb4b 100644 --- a/crates/engine/src/atomic_write.rs +++ b/crates/engine/src/atomic_write.rs @@ -37,6 +37,9 @@ pub fn write_atomic(path: &Path, bytes: &[u8]) -> io::Result<()> { Err(e) => return Err(e), }; let dir = path.parent().filter(|p| !p.as_os_str().is_empty()).map_or_else(|| PathBuf::from("."), Path::to_path_buf); + if let Err(e) = std::fs::create_dir_all(&dir) { + return Err(e); + } let (mut file, tmp) = create_temp(&dir, permissions.as_ref())?; let write = (|| { if let Some(permissions) = permissions { @@ -101,6 +104,15 @@ fn create_exclusive(path: &Path, permissions: Option<&Permissions>) -> io::Resul mod tests { use super::*; + #[test] + fn creates_missing_parent_directories() { + let dir = std::env::temp_dir().join(format!("cadcraft-aw-mkdir-{}-{}", std::process::id(), SEQ.fetch_add(1, Ordering::Relaxed))); + let dest = dir.join("new_folder").join("out.bin"); + write_atomic(&dest, b"created").unwrap(); + assert_eq!(std::fs::read(&dest).unwrap(), b"created"); + std::fs::remove_dir_all(&dir).ok(); + } + #[test] fn replaces_contents_atomically() { let dir = std::env::temp_dir().join(format!("cadcraft-aw-{}-{}", std::process::id(), SEQ.fetch_add(1, Ordering::Relaxed))); From 9107479ade5e2a2986fe70a53ac24e65ffaba702 Mon Sep 17 00:00:00 2001 From: Francesco BV Date: Thu, 22 Oct 2026 15:17:08 +0200 Subject: [PATCH 005/100] io: open -> save -> reopen round-trip harness with loss report (FF5) cadcraft_io::roundtrip compares a drawing with itself saved and reopened (entities by handle, tables, blocks, layouts, unmodelled objects, xdata, classes, header variables; numbers within a tolerance), and roundtrip::corpus runs a folder of DXF/DWG files through DXF and DWG into report.md and report.json; exposed as `cadcraft-cli roundtrip` and `cargo xtask corpus`. The corpus itself stays outside the repo. The DWG bridge now says what its writer leaves out, per DXF type (cadcraft_dwg::dxf_to_dwg_noted, cadcraft_io::write_noted): the report shows those records as lost on save and a strict corpus run fails on them. OLE2FRAME, which the writer keeps with its data, is no longer listed as not saved in a DWG. CADCRAFT_CORPUS_STRICT=1 makes cadcraft-cli roundtrip (and so cargo xtask corpus) fail on the same strict failures as the corpus test; xtask makes a relative corpus folder and --out absolute before running the CLI. Co-Authored-By: Claude Opus 5.5 (1M context) --- apps/cadcraft-cli/src/main.rs | 126 ++ crates/dwg/src/lib.rs | 87 +- crates/engine/src/cmd/file.rs | 10 + crates/engine/tests/save_reports_not_saved.rs | 13 + crates/io/src/lib.rs | 35 +- crates/io/src/roundtrip.rs | 1148 +++++++++++++++++ crates/io/src/roundtrip/census.rs | 212 +++ crates/io/src/roundtrip/corpus.rs | 651 ++++++++++ crates/io/src/roundtrip/tests.rs | 559 ++++++++ docs/gaps.md | 7 +- xtask/src/main.rs | 50 +- 11 files changed, 2872 insertions(+), 26 deletions(-) create mode 100644 crates/io/src/roundtrip.rs create mode 100644 crates/io/src/roundtrip/census.rs create mode 100644 crates/io/src/roundtrip/corpus.rs create mode 100644 crates/io/src/roundtrip/tests.rs diff --git a/apps/cadcraft-cli/src/main.rs b/apps/cadcraft-cli/src/main.rs index 99a8b4282..7b9916a55 100644 --- a/apps/cadcraft-cli/src/main.rs +++ b/apps/cadcraft-cli/src/main.rs @@ -9,6 +9,7 @@ //! cadcraft-cli perf [N] timing table on a synthetic N-entity drawing //! cadcraft-cli perf FILE canvas display-list build of a drawing, zoomed in step by step //! cadcraft-cli sample (bracket|floorplan) OUT write a built-in sample drawing +//! cadcraft-cli roundtrip [FILE|FOLDER] [--out DIR] open → save → reopen report (Markdown + JSON) //! ``` #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] @@ -63,6 +64,11 @@ const USAGE: &str = "usage: cadcraft-cli mcp [--connect HOST:PORT [--lazy] [--launch]] cadcraft-cli perf [N | FILE] cadcraft-cli sample (bracket|floorplan) OUT.(dxf|dwg|svg|png) + cadcraft-cli roundtrip [FILE | FOLDER] [--out DIR] [--keep-saved] [--same-format] [--no-census] [--tolerance T] + open each DXF/DWG, save it as DXF and DWG, reopen and compare; writes + report.md and report.json to DIR (default: the folder's `report`). + FOLDER defaults to $CADCRAFT_CORPUS_DIR. With CADCRAFT_CORPUS_STRICT=1 it + fails when a file doesn't open, save or reopen, or loses records on save. cadcraft-cli --version image framing (png 2400x1600, svg, pdf; always model space): @@ -819,6 +825,125 @@ mod mcp_args_tests { } } +/// What `roundtrip` was asked to do. +#[derive(Debug, PartialEq)] +struct RoundTripArgs { + path: Option, + out: Option, + keep_saved: bool, + opts: cadcraft_io::roundtrip::corpus::CorpusOptions, +} + +fn parse_roundtrip_args(args: &[String]) -> Result { + let mut r = RoundTripArgs { path: None, out: None, keep_saved: false, opts: Default::default() }; + let mut it = args.iter(); + while let Some(a) = it.next() { + match a.as_str() { + "--out" => r.out = Some(it.next().filter(|v| !v.starts_with("--")).ok_or("roundtrip: --out needs a folder")?.into()), + "--keep-saved" => r.keep_saved = true, + "--same-format" => r.opts.cross_format = false, + "--no-census" => r.opts.census = false, + "--tolerance" => { + let v = it.next().ok_or("roundtrip: --tolerance needs a number")?; + match v.parse::() { + Ok(t) if t.is_finite() && t >= 0.0 => r.opts.compare.abs_tol = t, + _ => return Err(format!("roundtrip: --tolerance must be a number >= 0, got `{v}`")), + } + } + s if s.starts_with("--") => return Err(format!("roundtrip: unknown option {s}")), + _ if r.path.is_none() => r.path = Some(a.into()), + _ => return Err("roundtrip: give one FILE or FOLDER".into()), + } + } + Ok(r) +} + +/// `roundtrip`: the corpus harness of `cadcraft_io::roundtrip::corpus` over a file or folder. +fn roundtrip(args: &[String]) -> Result<(), String> { + use cadcraft_io::roundtrip::corpus; + let mut a = parse_roundtrip_args(args)?; + let path = a + .path + .take() + .or_else(|| std::env::var_os("CADCRAFT_CORPUS_DIR").map(Into::into)) + .ok_or("roundtrip: give a FILE or FOLDER, or set CADCRAFT_CORPUS_DIR")?; + let (root, files, out) = if path.is_dir() { + let out = a.out.take().unwrap_or_else(|| path.join("report")); + let files = corpus::corpus_files(&path, std::slice::from_ref(&out)); + (path, files, out) + } else if path.is_file() { + let root = path.parent().map(std::path::Path::to_path_buf).unwrap_or_default(); + let out = a.out.take().unwrap_or_else(|| root.join("report")); + (root, vec![path], out) + } else { + return Err(format!("roundtrip: no such file or folder: {}", path.display())); + }; + if files.is_empty() { + return Err(format!("roundtrip: no DXF or DWG files in {}", root.display())); + } + if a.keep_saved { + a.opts.keep_saved = Some(out.join("saved")); + } + let report = corpus::run(&root, &files, &a.opts, &mut |i, n, rel| eprintln!("[{}/{n}] {rel}", i + 1)); + for f in &report.files { + let routes: Vec = f + .routes + .iter() + .map(|r| match (&r.save_error, &r.reopen_error) { + (Some(_), _) => format!("{}: save failed", r.route), + (None, Some(_)) => format!("{}: reopen failed", r.route), + (None, None) => { + let t = r.comparison.total(); + format!("{}: {} lost, {} added, {} changed", r.route, t.lost, t.added, t.changed) + } + }) + .collect(); + match &f.open_error { + Some(e) => outln!("{}: open failed: {e}", f.path), + None => outln!("{}: opened; {}", f.path, routes.join("; ")), + } + } + let (md, json) = report.write(&out).map_err(|e| format!("roundtrip: can't write the report to {}: {e}", out.display()))?; + outln!("report: {}", md.display()); + outln!(" {}", json.display()); + // CADCRAFT_CORPUS_STRICT=1: a file that doesn't open, save or reopen, or records a writer + // leaves out, fail the run (the report is written first). + if std::env::var("CADCRAFT_CORPUS_STRICT").is_ok_and(|v| v == "1") { + let failed = report.strict_failures(); + if !failed.is_empty() { + return Err(format!("roundtrip: {} strict failures:\n{}", failed.len(), failed.join("\n"))); + } + } + Ok(()) +} + +#[cfg(test)] +mod roundtrip_args_tests { + use super::*; + + fn args(a: &[&str]) -> Vec { + a.iter().map(|s| s.to_string()).collect() + } + + #[test] + fn roundtrip_options_parse() { + let r = parse_roundtrip_args(&args(&["corpus", "--out", "rep", "--keep-saved", "--same-format", "--tolerance", "0.001"])).unwrap(); + assert_eq!(r.path, Some("corpus".into())); + assert_eq!(r.out, Some("rep".into())); + assert!(r.keep_saved && !r.opts.cross_format && r.opts.census); + assert_eq!(r.opts.compare.abs_tol, 0.001); + assert_eq!(parse_roundtrip_args(&args(&[])).unwrap().path, None); + for bad in [&["--out"][..], &["--tolerance", "nan"], &["--tolerance", "-1"], &["a", "b"], &["--typo"]] { + assert!(parse_roundtrip_args(&args(bad)).is_err(), "{bad:?}"); + } + } + + #[test] + fn roundtrip_reports_a_missing_folder() { + assert!(roundtrip(&args(&["no/such/folder/anywhere"])).is_err()); + } +} + fn main() -> ExitCode { let code = cli(); if STDOUT_ERROR.load(Ordering::Relaxed) { ExitCode::FAILURE } else { code } @@ -845,6 +970,7 @@ fn cli() -> ExitCode { outln!("{}", serde_json::to_string_pretty(&v).unwrap_or_default()); }), Some("mcp") => mcp(&rest), + Some("roundtrip") => roundtrip(&rest), Some("--version" | "-V") => { outln!("cadcraft-cli {}", env!("CARGO_PKG_VERSION")); Ok(()) diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index b81bd00d1..d6efdac70 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -360,15 +360,22 @@ mod native { /// Leave out what acadrust can't write as this DWG version: other programs' custom entities /// and objects without raw DWG data of that version. acadrust 0.6.4 refuses the whole file /// for one of them (0.6.3 left them out); CADCraft already tells the user which entity types - /// a DWG doesn't keep (`not_saved`), so the rest of the drawing is written. Returns how many. - fn drop_unwritable(doc: &mut acadrust::CadDocument) -> usize { + /// a DWG doesn't keep (`not_saved`), so the rest of the drawing is written. Returns how many + /// records of each DXF type were left out, so a save can say so. + fn drop_unwritable(doc: &mut acadrust::CadDocument) -> super::Unwritten { let version = doc.version; + let mut dropped = super::Unwritten::new(); + let mut count = |name: &str| { + let n = dropped.entry(name.trim().to_ascii_uppercase()).or_insert(0); + *n = n.saturating_add(1); + }; let unwritable: Vec = doc .entities() .filter_map(|e| match e { acadrust::entities::EntityType::Unknown(u) if u.raw_dwg_data.is_none() || u.dwg_source_version.is_some_and(|v| v != version) || version == acadrust::DxfVersion::AC1009 => { + count(&u.dxf_name); Some(u.common.handle) } _ => None, @@ -381,9 +388,10 @@ mod native { .objects .iter() .filter_map(|(h, o)| match o { - acadrust::objects::ObjectType::Unknown { raw_dwg_data, raw_dwg_version, .. } + acadrust::objects::ObjectType::Unknown { type_name, raw_dwg_data, raw_dwg_version, .. } if raw_dwg_data.is_none() || raw_dwg_version.is_some_and(|v| v != version) || version == acadrust::DxfVersion::AC1009 => { + count(type_name); Some(*h) } _ => None, @@ -392,7 +400,7 @@ mod native { for h in &objects { doc.objects.remove(h); } - unwritable.len() + objects.len() + dropped } /// Header variables the DWG reader fills in but acadrust's DXF writer (0.6.3) leaves out of @@ -460,12 +468,19 @@ mod native { /// Convert DXF bytes into a DWG file of the DXF's version, and at least AutoCAD 2004 /// (AC1018). pub fn dxf_to_dwg(dxf: &[u8]) -> Result, String> { - convert(dxf, None) + convert(dxf, None).map(|(dwg, _)| dwg) } /// Convert DXF bytes into a DWG file of `version`, a code from [`super::VERSIONS`] ("AC1032"), /// whatever version the DXF declares. pub fn dxf_to_dwg_as(dxf: &[u8], version: &str) -> Result, String> { + dxf_to_dwg_noted(dxf, version).map(|(dwg, _)| dwg) + } + + /// [`dxf_to_dwg_as`], also returning what the DWG writer left out: how many records of each + /// DXF type it can't write (other programs' custom entities and objects that the DWG reader + /// didn't read from a DWG of this version), empty when the DWG keeps everything. + pub fn dxf_to_dwg_noted(dxf: &[u8], version: &str) -> Result<(Vec, super::Unwritten), String> { let v = match version.trim().to_ascii_uppercase().as_str() { "AC1015" => acadrust::DxfVersion::AC1015, "AC1018" => acadrust::DxfVersion::AC1018, @@ -512,7 +527,7 @@ mod native { } } - fn convert(dxf: &[u8], version: Option) -> Result, String> { + fn convert(dxf: &[u8], version: Option) -> Result<(Vec, super::Unwritten), String> { // Reject a pathologically large DXF before handing it to the third-party decoder, which // allocates tables and section buffers from the input's declared sizes and cannot be // interrupted once it starts (CWE-789). This bounds the large-input variant; the decoder @@ -539,13 +554,14 @@ mod native { other_layout_owners(&mut doc); table_style_rows(&mut doc, true); dimension_flags(&mut doc); - drop_unwritable(&mut doc); + let unwritten = drop_unwritable(&mut doc); current_dimstyle(&mut doc); add_variable_dictionary(&mut doc); current_text_style(&mut doc); - std::panic::catch_unwind(AssertUnwindSafe(|| acadrust::DwgWriter::write_to_vec(&doc))) + let dwg = std::panic::catch_unwind(AssertUnwindSafe(|| acadrust::DwgWriter::write_to_vec(&doc))) .map_err(|_| "the DWG writer failed on this drawing".to_string())? - .map_err(|e| format!("DWG write: {e}")) + .map_err(|e| format!("DWG write: {e}"))?; + Ok((dwg, unwritten)) } /// AutoCAD's entry name for the drawing variables dictionary (the DXF Reference calls it the @@ -642,7 +658,11 @@ mod native { } #[cfg(not(target_arch = "wasm32"))] -pub use native::{dwg_to_dxf, dwg_to_dxf_noted, dwg_to_dxf_with, dxf_to_dwg, dxf_to_dwg_as}; +pub use native::{dwg_to_dxf, dwg_to_dxf_noted, dwg_to_dxf_with, dxf_to_dwg, dxf_to_dwg_as, dxf_to_dwg_noted}; + +/// What saving as DWG left out: how many records of each DXF type (upper case) the DWG writer +/// can't write. +pub type Unwritten = std::collections::BTreeMap; /// The error for a DWG version CADCraft can't write. #[cfg_attr(target_arch = "wasm32", allow(dead_code))] @@ -671,6 +691,10 @@ pub fn dxf_to_dwg(_dxf: &[u8]) -> Result, String> { pub fn dxf_to_dwg_as(_dxf: &[u8], _version: &str) -> Result, String> { Err("DWG files can't be written in the web build yet; save as DXF".into()) } +#[cfg(target_arch = "wasm32")] +pub fn dxf_to_dwg_noted(_dxf: &[u8], _version: &str) -> Result<(Vec, Unwritten), String> { + Err("DWG files can't be written in the web build yet; save as DXF".into()) +} #[cfg(test)] mod tests { @@ -911,4 +935,47 @@ mod tests { assert!(ls.contains(&"LSDEFINITION") && !ls.contains(&"LSINTERNALCOMPONENT"), "{ls:?}"); assert_eq!(notes, ["DWG: 3 DGN line style components that no line style definition uses were left out"]); } + + /// Saving says what the DWG writer left out, per type: entities of types acadrust's DXF + /// reader doesn't model (another program's custom entity) can't be written; a drawing the DWG + /// keeps whole reports nothing. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn saving_lists_the_types_the_dwg_writer_leaves_out() { + let line = " 0\nLINE\n 8\n0\n100\nAcDbEntity\n100\nAcDbLine\n 10\n0.0\n 20\n0.0\n 30\n0.0\n 11\n10.0\n 21\n0.0\n 31\n0.0\n"; + let widget = " 0\nACME_WIDGET\n 8\n0\n100\nAcDbEntity\n100\nAcmeWidget\n 10\n1.0\n 20\n2.0\n 30\n0.0\n"; + let dxf = |body: &str| { + format!(" 0\nSECTION\n 2\nHEADER\n 9\n$ACADVER\n 1\nAC1032\n 0\nENDSEC\n 0\nSECTION\n 2\nENTITIES\n{body} 0\nENDSEC\n 0\nEOF\n") + }; + for version in ["AC1015", "AC1018", "AC1032"] { + let (dwg, unwritten) = dxf_to_dwg_noted(dxf(&format!("{line}{widget}{widget}")).as_bytes(), version).unwrap(); + assert!(is_dwg(&dwg), "{version}"); + assert_eq!(unwritten, [("ACME_WIDGET".to_string(), 2)].into_iter().collect::(), "{version}"); + } + let (_, unwritten) = dxf_to_dwg_noted(dxf(line).as_bytes(), "AC1032").unwrap(); + assert!(unwritten.is_empty(), "{unwritten:?}"); + } + + /// An OLE2FRAME, which CADCraft keeps as its DXF groups without modelling it, is written to + /// the DWG with its data: it isn't in the list of what the writer left out. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn ole_frames_are_written_with_their_data() { + let ole = " 0\nOLE2FRAME\n 8\n0\n100\nAcDbEntity\n100\nAcDbOle2Frame\n 70\n2\n 10\n1.0\n 20\n9.0\n 30\n0.0\n 11\n8.0\n 21\n2.0\n 31\n0.0\n 71\n2\n 72\n0\n 73\n1\n 90\n4\n310\nDEADBEEF\n 1\nOLE\n"; + let dxf = + format!(" 0\nSECTION\n 2\nHEADER\n 9\n$ACADVER\n 1\nAC1032\n 0\nENDSEC\n 0\nSECTION\n 2\nENTITIES\n{ole} 0\nENDSEC\n 0\nEOF\n"); + for version in ["AC1015", "AC1018", "AC1032"] { + let (dwg, unwritten) = dxf_to_dwg_noted(dxf.as_bytes(), version).unwrap(); + assert!(unwritten.is_empty(), "{version} {unwritten:?}"); + let back = acadrust::DwgReader::from_stream(std::io::Cursor::new(dwg)).read().unwrap(); + let frames: Vec> = back + .entities() + .filter_map(|e| match e { + acadrust::entities::EntityType::Ole2Frame(o) => Some(o.encoded_payload()), + _ => None, + }) + .collect(); + assert_eq!(frames, [vec![0xDE, 0xAD, 0xBE, 0xEF]], "{version}"); + } + } } diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 95975a4e2..cfad2e61f 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -534,6 +534,13 @@ pub fn not_saved(d: &Drawing) -> std::collections::BTreeMap { /// store section, which is written back with the drawing (`Foreign::data_store`). const MODELER_TYPES: [&str; 3] = ["3DSOLID", "REGION", "BODY"]; +/// Entity types CADCraft keeps without modelling them that the DWG bridge writes all the same: +/// its DWG library models them and writes them whole (an OLE2FRAME with its data bytes, for +/// every DWG version; `cadcraft-dwg`'s `ole_frames_are_written_with_their_data`). What the bridge +/// can't write it reports per type through `cadcraft_io::write_noted`, which the round-trip +/// harness reads; a save from here warns with [`not_saved_as`] only. +const DWG_WRITTEN_TYPES: [&str; 1] = ["OLE2FRAME"]; + /// [`not_saved`] for a file saved as `version`. From 2013 on, a 3D solid, region or body the data /// store has a record for keeps its geometry and is not counted. pub fn not_saved_as(d: &Drawing, version: Version) -> std::collections::BTreeMap { @@ -545,6 +552,9 @@ pub fn not_saved_as(d: &Drawing, version: Version) -> std::collections::BTreeMap if MODELER_TYPES.iter().any(|t| u.dxf_type.eq_ignore_ascii_case(t)) && stored.contains(&e.handle) { continue; } + if DWG_WRITTEN_TYPES.iter().any(|t| u.dxf_type.eq_ignore_ascii_case(t)) { + continue; + } let n = out.entry(u.dxf_type.clone()).or_insert(0); *n = n.saturating_add(1); } diff --git a/crates/engine/tests/save_reports_not_saved.rs b/crates/engine/tests/save_reports_not_saved.rs index 615028a21..a52fa7097 100644 --- a/crates/engine/tests/save_reports_not_saved.rs +++ b/crates/engine/tests/save_reports_not_saved.rs @@ -82,3 +82,16 @@ fn solids_whose_geometry_the_data_store_keeps_are_not_reported_from_2013() { // Before 2013 a file has no data store: every solid loses its geometry. assert_eq!(not_saved_as(&d, Version::R2010), [("3DSOLID".to_string(), 2), ("REGION".to_string(), 1)].into_iter().collect()); } + +/// An OLE2FRAME (an embedded OLE object) is kept by CADCraft as its DXF groups and written to +/// DWG whole, so saving doesn't report it as not saved (it was: "… 4 OLE2FRAME"). +#[test] +fn ole_frames_are_not_reported_as_not_saved() { + use cadcraft_engine::cmd::file::{Version, not_saved_as}; + let mut d = Drawing::new_imperial(); + d.add(&Space::Model, Common::default(), unknown("OLE2FRAME")).unwrap(); + d.add(&Space::Model, Common::default(), unknown("ACME_WIDGET")).unwrap(); + for v in [Version::R2000, Version::R2018] { + assert_eq!(not_saved_as(&d, v), [("ACME_WIDGET".to_string(), 1)].into_iter().collect(), "{v:?}"); + } +} diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rs index ef56e6602..39afd2df9 100644 --- a/crates/io/src/lib.rs +++ b/crates/io/src/lib.rs @@ -14,6 +14,8 @@ mod dxf_write; mod dxf_xref; pub mod pdf; pub mod plotter; +/// Open → save → reopen comparison and the real-file corpus harness (gaps FF1, FF5). +pub mod roundtrip; pub mod svg; mod svg_read; @@ -60,18 +62,25 @@ fn ext(name: &str) -> String { /// Read a drawing; the format comes from the content (DXF ASCII/binary, DWG) or the name /// (SVG and gzip-compressed SVGZ). pub fn read(bytes: &[u8], name: &str) -> Result { + read_source(bytes, name).map(|(d, _)| d) +} + +/// [`read`], also returning the DXF rendition a DWG was read through (`None` for other files), +/// which the round-trip harness counts. +pub(crate) fn read_source(bytes: &[u8], name: &str) -> Result<(Drawing, Option>)> { if cadcraft_dwg::is_dwg(bytes) { let (dxf, notes) = cadcraft_dwg::dwg_to_dxf_noted(bytes, cadcraft_dwg::Limits::DEFAULT).map_err(IoError::Format)?; let mut d = dxf_read::read(&dxf).map_err(|e| IoError::Format(format!("{e} (in the DXF converted from this DWG)")))?; d.read_notes = notes; - return Ok(d); + return Ok((d, Some(dxf))); } - match ext(name).as_str() { + let d = match ext(name).as_str() { "svg" | "svgz" => svg_read::read(bytes), "dxf" | "" => dxf_read::read(bytes), e if bytes.len() > 4 => dxf_read::read(bytes).map_err(|_| IoError::Unsupported(e.to_string())), e => Err(IoError::Unsupported(e.to_string())), - } + }?; + Ok((d, None)) } /// Write a drawing in the format chosen by the name's extension. Images (PNG, SVG, PDF) show @@ -97,15 +106,27 @@ pub fn write_with(d: &Drawing, name: &str, opts: &SaveOptions) -> Result // The DWG bridge reads our R2000 DXF and writes the DWG at the version asked for. Without // one, at least 2004: R2000 DWG has no true colours, transparency, table styles or // gradients, so a drawing from an R2000 DXF doesn't lose them by default. - "dwg" if !opts.binary => { - let version = opts.version.unwrap_or_else(|| version.max(Version::R2004)); - cadcraft_dwg::dxf_to_dwg_as(dxf_write::dwg_bridge(d, version).as_bytes(), version.code()).map_err(IoError::Format) - } + "dwg" if !opts.binary => write_dwg(d, opts.version.unwrap_or_else(|| version.max(Version::R2004))).map(|(dwg, _)| dwg), e if opts.binary => Err(IoError::Format(format!("binary output is for DXF files, not `{e}`"))), _ => write_framed(d, name, None), } } +/// [`write_with`], also returning what the file leaves out of the drawing: for a DWG, how many +/// records of each DXF type the DWG writer can't write (other programs' custom entities and +/// objects); for other formats, nothing. +pub fn write_noted(d: &Drawing, name: &str, opts: &SaveOptions) -> Result<(Vec, cadcraft_dwg::Unwritten)> { + if ext(name) == "dwg" && !opts.binary { + return write_dwg(d, opts.version.unwrap_or_else(|| save_version(d).max(Version::R2004))); + } + write_with(d, name, opts).map(|b| (b, cadcraft_dwg::Unwritten::new())) +} + +/// The DWG bridge reads our R2000 DXF and writes the DWG at `version`. +fn write_dwg(d: &Drawing, version: Version) -> Result<(Vec, cadcraft_dwg::Unwritten)> { + cadcraft_dwg::dxf_to_dwg_noted(dxf_write::dwg_bridge(d, version).as_bytes(), version.code()).map_err(IoError::Format) +} + /// Like [`write`], but image formats (PNG, SVG, PDF) show `window` (a model-space rectangle in /// drawing units, centred and scaled to fit the image without distortion) instead of the /// extents. Formats that are not images (DXF, DWG) ignore the window. The window is checked with diff --git a/crates/io/src/roundtrip.rs b/crates/io/src/roundtrip.rs new file mode 100644 index 000000000..e801e663d --- /dev/null +++ b/crates/io/src/roundtrip.rs @@ -0,0 +1,1148 @@ +//! Round-trip checks (gaps FF1 and FF5): open a drawing, save it, open the saved file again and +//! list everything that differs between the two drawings. +//! +//! [`compare`] works on CADCraft's own model ([`Drawing`]), so it measures what saving and +//! reopening lose or change: entities per type in every space and block (matched by handle, +//! geometry and properties within a tolerance), table records, blocks, layouts, the data CADCraft +//! keeps without modelling it (other programs' objects and entities, dictionaries, CLASS records, +//! extension dictionaries, reactors, xdata) and header variables. [`census`] counts what the +//! file itself holds, so opening a file can be checked against it too. +//! +//! The corpus harness ([`corpus`], native only) runs both over a folder of real files and writes +//! a Markdown and JSON report; `cadcraft-cli roundtrip` and `cargo xtask corpus` run it. + +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; + +use cadcraft_doc::foreign::{Foreign, ForeignExtra}; +use cadcraft_doc::{Drawing, Entity, EntityKind, EntityStore, Handle, RawTag}; +use serde::Serialize; +use serde_json::Value; + +mod census; +#[cfg(not(target_arch = "wasm32"))] +pub mod corpus; + +pub use census::{Census, census, census_of, compare_census}; + +/// What a difference is about. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum Category { + /// Opening the file: what the file holds that the drawing read from it doesn't. + Read, + /// Entities of a type in a space or block. + Count, + /// Entity, object or dictionary entry handles. + Handle, + /// An entity's geometry and type-specific data. + Geometry, + /// An entity's common properties (layer, colour, linetype, lineweight, transparency…). + Properties, + /// Entities CADCraft doesn't model, kept as their DXF groups. + Opaque, + /// Other applications' extended data. + Xdata, + /// Extension dictionaries and persistent reactors. + ExtDict, + /// Table records: layers, linetypes, text, dimension, multileader, table and multiline styles, + /// views, viewport configurations, UCSs, layer states, groups, scales, plot style tables. + Table, + /// Block definitions (their own properties; their entities are under the other categories). + Block, + /// Layouts and their page setups. + Layout, + /// Non-graphical objects CADCraft doesn't model, kept as their DXF groups; constraints. + Object, + /// Entries of the named object dictionary. + Dictionary, + /// CLASS records. + Class, + /// Header variables. + Header, +} + +impl Category { + pub const ALL: [Category; 15] = [ + Category::Read, + Category::Count, + Category::Handle, + Category::Geometry, + Category::Properties, + Category::Opaque, + Category::Xdata, + Category::ExtDict, + Category::Table, + Category::Block, + Category::Layout, + Category::Object, + Category::Dictionary, + Category::Class, + Category::Header, + ]; + + /// A short name for reports. + pub fn label(self) -> &'static str { + match self { + Category::Read => "read", + Category::Count => "entity count", + Category::Handle => "handles", + Category::Geometry => "geometry", + Category::Properties => "properties", + Category::Opaque => "unknown entities", + Category::Xdata => "xdata", + Category::ExtDict => "ext. dictionaries/reactors", + Category::Table => "table records", + Category::Block => "blocks", + Category::Layout => "layouts", + Category::Object => "objects", + Category::Dictionary => "dictionary entries", + Category::Class => "classes", + Category::Header => "header", + } + } +} + +/// Whether something went missing, appeared, or is there with other values. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum Change { + Lost, + Added, + Changed, +} + +impl Change { + pub fn label(self) -> &'static str { + match self { + Change::Lost => "lost", + Change::Added => "added", + Change::Changed => "changed", + } + } +} + +/// One difference between two drawings. +#[derive(Clone, Debug, PartialEq, Serialize)] +pub struct Difference { + pub category: Category, + pub change: Change, + /// Where: `Model LINE 1A`, `Block DOOR`, `LAYER Walls`, `$LTSCALE`… + pub place: String, + /// What: the values before and after, or the items lost. + pub detail: String, + /// How many items this difference stands for (several entities lost of one type: one row). + pub count: usize, +} + +/// How many items were lost, added and changed. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)] +pub struct Tally { + pub lost: usize, + pub added: usize, + pub changed: usize, +} + +impl Tally { + pub fn total(&self) -> usize { + self.lost.saturating_add(self.added).saturating_add(self.changed) + } + fn add(&mut self, change: Change, n: usize) { + let slot = match change { + Change::Lost => &mut self.lost, + Change::Added => &mut self.added, + Change::Changed => &mut self.changed, + }; + *slot = slot.saturating_add(n); + } +} + +/// How two drawings are compared. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Options { + /// Absolute tolerance for numbers (drawing units, radians…). + pub abs_tol: f64, + /// Relative tolerance for numbers, of the larger magnitude. + pub rel_tol: f64, + /// Most differences listed; the rest are only counted ([`Comparison::omitted`]). + pub max_differences: usize, + /// Most changed values named for one object. + pub max_paths: usize, +} + +impl Default for Options { + fn default() -> Self { + Options { abs_tol: 1e-6, rel_tol: 1e-9, max_differences: 5000, max_paths: 6 } + } +} + +/// Every difference found between two drawings. +#[derive(Clone, Debug, Default, PartialEq, Serialize)] +pub struct Comparison { + pub differences: Vec, + /// Differences found beyond [`Options::max_differences`] (counted in `totals`, not listed). + pub omitted: usize, + /// Items lost, added and changed per category. + pub totals: BTreeMap, +} + +impl Comparison { + pub fn is_empty(&self) -> bool { + self.totals.values().all(|t| t.total() == 0) + } + /// Items lost, added and changed in all categories. + pub fn total(&self) -> Tally { + let mut all = Tally::default(); + for t in self.totals.values() { + all.add(Change::Lost, t.lost); + all.add(Change::Added, t.added); + all.add(Change::Changed, t.changed); + } + all + } + /// The tally of one category. + pub fn tally(&self, c: Category) -> Tally { + self.totals.get(&c).copied().unwrap_or_default() + } + /// Add another comparison's differences (within this one's limit). + pub fn merge(&mut self, other: Comparison, max: usize) { + for (c, t) in other.totals { + let mine = self.totals.entry(c).or_default(); + mine.add(Change::Lost, t.lost); + mine.add(Change::Added, t.added); + mine.add(Change::Changed, t.changed); + } + for d in other.differences { + if self.differences.len() < max { + self.differences.push(d); + } else { + self.omitted = self.omitted.saturating_add(1); + } + } + self.omitted = self.omitted.saturating_add(other.omitted); + } +} + +/// Collects differences within the limit. +struct Out<'o> { + opts: &'o Options, + cmp: Comparison, + /// Anonymous blocks saved under another name (`*D344` → `*D10`), upper case: references + /// to them compare equal. + renamed: HashMap, + /// Things that only got new handles, summed up in one row each: (what) → (how many, an + /// example). + renumbered: BTreeMap, + /// Anonymous blocks of the first drawing that nothing refers to and that the second one + /// doesn't have (saving drops them, as PURGE would), upper case: reported in one row. + orphans: BTreeSet, +} + +/// What values are compared with: the tolerances and the renamed anonymous blocks. +struct Norm<'a> { + o: &'a Options, + renamed: &'a HashMap, +} + +impl Norm<'_> { + /// Equal strings, or an anonymous block's name and the name it was saved under. + fn same_str(&self, a: &str, b: &str) -> bool { + a == b || (a.starts_with('*') && self.renamed.get(&a.to_ascii_uppercase()).is_some_and(|n| n.eq_ignore_ascii_case(b))) + } +} + +impl<'o> Out<'o> { + fn new(opts: &'o Options) -> Self { + Out { opts, cmp: Comparison::default(), renamed: HashMap::new(), renumbered: BTreeMap::new(), orphans: BTreeSet::new() } + } + fn norm(&self) -> Norm<'_> { + Norm { o: self.opts, renamed: &self.renamed } + } + /// Note that one of `what` only got a new handle. + fn renumber(&mut self, what: &str, example: impl FnOnce() -> String) { + match self.renumbered.get_mut(what) { + Some(slot) => slot.0 = slot.0.saturating_add(1), + None => { + self.renumbered.insert(what.to_string(), (1, example())); + } + } + } + /// The rows of [`Out::renumber`]. + fn flush_renumbered(&mut self) { + for (what, (n, example)) in std::mem::take(&mut self.renumbered) { + self.push_n(Category::Handle, Change::Changed, n, what.clone(), format!("{n} {what} got new handles (e.g. {example})")); + } + } + fn push(&mut self, category: Category, change: Change, place: impl Into, detail: impl Into) { + self.push_n(category, change, 1, place, detail); + } + fn push_n(&mut self, category: Category, change: Change, count: usize, place: impl Into, detail: impl Into) { + self.cmp.totals.entry(category).or_default().add(change, count); + if self.cmp.differences.len() < self.opts.max_differences { + self.cmp.differences.push(Difference { category, change, place: place.into(), detail: detail.into(), count }); + } else { + self.cmp.omitted = self.cmp.omitted.saturating_add(1); + } + } +} + +/// Header variables that change on every save (time stamps, the handle seed, GUIDs): not +/// compared. +pub const VOLATILE_HEADER: &[&str] = + &["HANDSEED", "TDCREATE", "TDUCREATE", "TDUPDATE", "TDUUPDATE", "TDINDWG", "TDUSRTIMER", "VERSIONGUID", "FINGERPRINTGUID"]; + +/// Compare `before` (the drawing as first opened) with `after` (the same drawing saved and +/// reopened). Differences are reported as what `after` lost, gained or changed. +pub fn compare(before: &Drawing, after: &Drawing, opts: &Options) -> Comparison { + let mut out = Out::new(opts); + compare_entities(before, after, &mut out); + compare_tables(before, after, &mut out); + compare_foreign(&before.foreign, &after.foreign, &mut out); + compare_header(before, after, &mut out); + out.flush_renumbered(); + out.cmp +} + +// --------------------------------------------------------------------------------------------- +// Values + +fn close(a: f64, b: f64, o: &Options) -> bool { + a == b || (a - b).abs() <= o.abs_tol + o.rel_tol * a.abs().max(b.abs()) +} + +/// The changed values of one object, as `path: before → after`. +struct Paths { + list: Vec, + cap: usize, + more: usize, +} + +impl Paths { + fn new(cap: usize) -> Self { + Paths { list: Vec::new(), cap: cap.max(1), more: 0 } + } + fn add(&mut self, s: String) { + if self.list.len() < self.cap { + self.list.push(s); + } else { + self.more = self.more.saturating_add(1); + } + } + fn is_empty(&self) -> bool { + self.list.is_empty() + } + /// Far more changes than can be listed: stop looking. + fn saturated(&self) -> bool { + self.more > 1000 + } + fn text(&self) -> String { + let mut s = self.list.join("; "); + if self.more > 0 { + s.push_str(&format!("; and {} more", if self.saturated() { "many".to_string() } else { self.more.to_string() })); + } + s + } +} + +/// A value for a report: strings quoted, at most 60 characters. +fn short(v: &Value) -> String { + let s = match v { + Value::String(s) => format!("\"{s}\""), + Value::Array(a) => format!("[{} items]", a.len()), + Value::Object(_) => "{…}".to_string(), + other => other.to_string(), + }; + clip(&s, 60) +} + +/// `s` cut to `n` characters, with an ellipsis when cut. +pub(crate) fn clip(s: &str, n: usize) -> String { + if s.chars().count() <= n { + s.to_string() + } else { + let mut t: String = s.chars().take(n.saturating_sub(1)).collect(); + t.push('…'); + t + } +} + +fn show(path: &str) -> &str { + if path.is_empty() { "value" } else { path } +} + +const MAX_DEPTH: usize = 64; + +/// Whether a value's path names an angle (in radians in the model): `rotation`, `start`, +/// `end`, `angle`, `twist`, `oblique`. +fn is_angle(path: &str) -> bool { + let last = path.rsplit('.').next().unwrap_or(path); + let last = last.split('[').next().unwrap_or(last).to_ascii_lowercase(); + ["rotation", "start", "end", "angle", "twist", "oblique"].iter().any(|k| last.ends_with(k)) +} + +/// The same angle, a whole number of turns apart (-π/4 and 7π/4). +fn same_angle(a: f64, b: f64, o: &Options) -> bool { + let d = (a - b).rem_euclid(std::f64::consts::TAU); + let tol = o.abs_tol + o.rel_tol * a.abs().max(b.abs()); + d <= tol || std::f64::consts::TAU - d <= tol +} + +fn diff_values(a: &Value, b: &Value, path: &mut String, n: &Norm, p: &mut Paths, depth: usize) { + if p.saturated() { + return; + } + if depth > MAX_DEPTH { + if a != b { + p.add(format!("{}: differs", show(path))); + } + return; + } + match (a, b) { + (Value::Number(x), Value::Number(y)) => { + let same = match (x.as_i64(), y.as_i64()) { + (Some(i), Some(j)) => i == j, + _ => match (x.as_f64(), y.as_f64()) { + (Some(f), Some(g)) => close(f, g, n.o) || (is_angle(path) && same_angle(f, g, n.o)), + _ => x == y, + }, + }; + if !same { + p.add(format!("{}: {x} → {y}", show(path))); + } + } + (Value::String(x), Value::String(y)) => { + if !n.same_str(x, y) { + p.add(format!("{}: {} → {}", show(path), short(a), short(b))); + } + } + (Value::Object(x), Value::Object(y)) => { + let keys: BTreeSet<&String> = x.keys().chain(y.keys()).collect(); + for k in keys { + let len = path.len(); + if !path.is_empty() { + path.push('.'); + } + path.push_str(k); + match (x.get(k.as_str()), y.get(k.as_str())) { + (Some(u), Some(v)) => diff_values(u, v, path, n, p, depth + 1), + (Some(Value::Null), None) | (None, Some(Value::Null)) | (None, None) => {} + (Some(u), None) => p.add(format!("{}: {} → (none)", show(path), short(u))), + (None, Some(v)) => p.add(format!("{}: (none) → {}", show(path), short(v))), + } + path.truncate(len); + } + } + (Value::Array(x), Value::Array(y)) => { + if x.len() != y.len() { + p.add(format!("{}: {} items → {}", show(path), x.len(), y.len())); + } + for (i, (u, v)) in x.iter().zip(y).enumerate() { + if p.saturated() { + return; + } + let len = path.len(); + path.push_str(&format!("[{i}]")); + diff_values(u, v, path, n, p, depth + 1); + path.truncate(len); + } + } + _ if a == b => {} + _ => p.add(format!("{}: {} → {}", show(path), short(a), short(b))), + } +} + +/// The values that differ between two serializable things. +fn diff_ser(a: &T, b: &T, n: &Norm) -> Paths { + let mut p = Paths::new(n.o.max_paths); + match (serde_json::to_value(a), serde_json::to_value(b)) { + (Ok(x), Ok(y)) => diff_values(&x, &y, &mut String::new(), n, &mut p, 0), + _ if a != b => p.add("differs (not comparable field by field)".into()), + _ => {} + } + p +} + +/// A key for matching things that may have been renumbered: the value with numbers rounded. +fn signature(v: &Value, out: &mut String, depth: usize) { + if depth > MAX_DEPTH { + return; + } + match v { + Value::Number(n) => match n.as_f64() { + Some(f) if f.is_finite() => { + let r = if f.abs() < 1e-9 { 0.0 } else { f }; + out.push_str(&format!("{r:.6e}")); + } + _ => out.push_str(&n.to_string()), + }, + Value::Array(a) => { + out.push('['); + for x in a { + signature(x, out, depth + 1); + out.push(','); + } + out.push(']'); + } + Value::Object(m) => { + out.push('{'); + for (k, x) in m { + out.push_str(k); + out.push(':'); + signature(x, out, depth + 1); + out.push(','); + } + out.push('}'); + } + other => out.push_str(&other.to_string()), + } +} + +fn sig_of(t: &T) -> String { + let mut s = String::new(); + if let Ok(v) = serde_json::to_value(t) { + signature(&v, &mut s, 0); + } + s +} + +/// Pair up the items of `a` and `b` that weren't matched by handle: equal signatures first, then +/// in order. Returns the pairs and the items left over on each side. +fn pair_up<'x, T>(a: Vec<&'x T>, b: Vec<&'x T>, sig: impl Fn(&T) -> String) -> (Vec<(&'x T, &'x T)>, Vec<&'x T>, Vec<&'x T>) { + let mut by_sig: HashMap> = HashMap::new(); + for (i, y) in b.iter().enumerate().rev() { + by_sig.entry(sig(y)).or_default().push(i); + } + let mut used = vec![false; b.len()]; + let mut pairs = Vec::new(); + let mut rest_a = Vec::new(); + for x in a { + match by_sig.get_mut(&sig(x)).and_then(Vec::pop) { + Some(i) => { + if let (Some(y), Some(u)) = (b.get(i), used.get_mut(i)) { + *u = true; + pairs.push((x, *y)); + } + } + None => rest_a.push(x), + } + } + let mut rest_b: Vec<&T> = b.iter().zip(&used).filter(|(_, u)| !**u).map(|(y, _)| *y).collect(); + let n = rest_a.len().min(rest_b.len()); + let tail_a = rest_a.split_off(n); + let tail_b = rest_b.split_off(n); + pairs.extend(rest_a.into_iter().zip(rest_b)); + (pairs, tail_a, tail_b) +} + +fn hex_list(hs: impl Iterator, n: usize) -> String { + let all: Vec = hs.map(Handle::hex).collect(); + let mut s = all.iter().take(n).cloned().collect::>().join(", "); + if all.len() > n { + s.push_str(", …"); + } + s +} + +/// Group codes that hold handles (DXF Reference: 5, 105, 320–369, 390–399, 480–481, xdata +/// 1005). A file's objects can be renumbered on save (gap FF7), so a handle compares equal to +/// any other handle; only a reference that became null (or stopped being null) counts. +fn is_handle_code(code: i32) -> bool { + matches!(code, 5 | 105 | 320..=369 | 390..=399 | 480 | 481 | 1005) +} + +fn null_handle(v: &str) -> bool { + v.trim().trim_start_matches('0').is_empty() +} + +/// Whether two groups hold the same value: the same text, numbers within the tolerance, or two +/// (non-null) handles. +fn tag_eq(a: &RawTag, b: &RawTag, o: &Options) -> bool { + if a.code != b.code { + return false; + } + if is_handle_code(a.code) { + return null_handle(&a.value) == null_handle(&b.value); + } + a.value == b.value + || a.value.trim() == b.value.trim() + || matches!((a.value.trim().parse::(), b.value.trim().parse::()), (Ok(x), Ok(y)) if close(x, y, o)) +} + +/// How two group lists differ, or `None` when they hold the same values. +fn tags_diff(a: &[RawTag], b: &[RawTag], o: &Options) -> Option { + let first = a.iter().zip(b).position(|(x, y)| !tag_eq(x, y, o)); + if first.is_none() && a.len() == b.len() { + return None; + } + let mut s = String::new(); + if a.len() != b.len() { + s.push_str(&format!("{} groups → {}; ", a.len(), b.len())); + } + let at = first.unwrap_or(a.len().min(b.len())); + let show_tag = |t: Option<&RawTag>| t.map_or("(end)".to_string(), |t| format!("{} `{}`", t.code, clip(&t.value, 40))); + s.push_str(&format!("first difference at group #{at}: {} → {}", show_tag(a.get(at)), show_tag(b.get(at)))); + Some(s) +} + +// --------------------------------------------------------------------------------------------- +// Entities + +/// An entity collection of a drawing: model space, a layout's paper space or a block. `key` is +/// upper case (`BLOCK *D12`), `name` for reports (`Block *D12`). +struct Container<'d> { + key: String, + name: String, + store: &'d EntityStore, +} + +fn containers(d: &Drawing) -> Vec> { + let mut v = vec![Container { key: "MODEL".into(), name: "Model".into(), store: &d.model }]; + for l in &d.layouts { + v.push(Container { key: format!("LAYOUT {}", l.name.to_ascii_uppercase()), name: format!("Layout {}", l.name), store: &l.entities }); + } + for (name, b) in &d.blocks { + v.push(Container { key: format!("BLOCK {}", name.to_ascii_uppercase()), name: format!("Block {name}"), store: &b.entities }); + } + v +} + +/// Which container each entity is in. +fn locate(cs: &[Container]) -> HashMap { + cs.iter().enumerate().flat_map(|(i, c)| c.store.iter().map(move |e| (e.handle, i))).collect() +} + +/// Anonymous blocks of `a` whose entities (by handle) are, at least half of them, in another +/// anonymous block of `b`: saving renamed them (`*D344` → `*D10`). Upper-case names. +fn anonymous_renames(ca: &[Container], cb: &[Container], where_b: &HashMap) -> HashMap { + let mut renamed = HashMap::new(); + let mut taken: HashSet = HashSet::new(); + for c in ca { + let Some(name) = c.key.strip_prefix("BLOCK ").filter(|n| n.starts_with('*')) else { continue }; + if c.store.is_empty() { + continue; + } + let mut votes: BTreeMap = BTreeMap::new(); + for e in c.store.iter() { + if let Some(j) = where_b.get(&e.handle) { + *votes.entry(*j).or_default() += 1; + } + } + let best = votes.into_iter().max_by(|x, y| x.1.cmp(&y.1).then(y.0.cmp(&x.0))); + let Some((j, n)) = best else { continue }; + let Some(target) = cb.get(j).and_then(|t| t.key.strip_prefix("BLOCK ")) else { continue }; + if n.saturating_mul(2) >= c.store.len() && target != name && target.starts_with('*') && taken.insert(j) { + renamed.insert(name.to_string(), target.to_string()); + } + } + renamed +} + +struct Ctx<'d> { + a: &'d Drawing, + b: &'d Drawing, + cb: &'d [Container<'d>], + where_a: &'d HashMap, + where_b: &'d HashMap, +} + +fn compare_entities(a: &Drawing, b: &Drawing, out: &mut Out) { + let ca = containers(a); + let cb = containers(b); + let where_a = locate(&ca); + let where_b = locate(&cb); + out.renamed = anonymous_renames(&ca, &cb, &where_b); + let index_b: HashMap<&str, usize> = cb.iter().enumerate().map(|(i, c)| (c.key.as_str(), i)).collect(); + let target = |c: &Container| match c.key.strip_prefix("BLOCK ").and_then(|n| out.renamed.get(n)) { + Some(n) => index_b.get(format!("BLOCK {n}").as_str()).copied(), + None => index_b.get(c.key.as_str()).copied(), + }; + let targets: Vec> = ca.iter().map(target).collect(); + // A block renamed onto the name another block had: that one has no counterpart by name. + let renamed_onto: HashSet = ca + .iter() + .zip(&targets) + .filter(|(c, _)| c.key.strip_prefix("BLOCK ").is_some_and(|n| out.renamed.contains_key(n))) + .filter_map(|(_, t)| *t) + .collect(); + let ctx = Ctx { a, b, cb: &cb, where_a: &where_a, where_b: &where_b }; + let empty = EntityStore::new(); + let mut matched: HashSet = HashSet::new(); + let mut moves: BTreeMap<(String, String), Vec> = BTreeMap::new(); + let referenced = referenced_blocks(&ca); + let mut orphan_entities = 0usize; + for (c, t) in ca.iter().zip(&targets) { + let renamed = c.key.strip_prefix("BLOCK ").is_some_and(|n| out.renamed.contains_key(n)); + let j = t.filter(|j| renamed || !renamed_onto.contains(j)).filter(|j| matched.insert(*j)); + if j.is_none() + && let Some(name) = c.key.strip_prefix("BLOCK ").filter(|n| n.starts_with('*') && !referenced.contains(*n)) + && c.store.iter().all(|e| !where_b.contains_key(&e.handle)) + { + out.orphans.insert(name.to_string()); + orphan_entities = orphan_entities.saturating_add(c.store.len()); + continue; + } + let sb = j.and_then(|j| cb.get(j)).map_or(&empty, |x| x.store); + compare_store(&c.name, c.store, sb, &ctx, &mut moves, out); + } + if let Some(first) = out.orphans.first().cloned() { + let n = out.orphans.len(); + let detail = format!("{orphan_entities} entities of {n} anonymous blocks nothing refers to (e.g. {first}) were not saved"); + out.push_n(Category::Count, Change::Lost, orphan_entities, "unreferenced anonymous blocks", detail); + } + for (j, c) in cb.iter().enumerate() { + if !matched.contains(&j) { + compare_store(&c.name, &empty, c.store, &ctx, &mut moves, out); + } + } + for ((from, to), hs) in moves { + let detail = format!("{} entities moved from {from} to {to} ({})", hs.len(), hex_list(hs.iter().copied(), 6)); + out.push_n(Category::Handle, Change::Changed, hs.len(), from, detail); + } +} + +/// The blocks a drawing's entities refer to (block references, dimensions, multileaders), upper +/// case. +fn referenced_blocks(cs: &[Container]) -> HashSet { + let mut out = HashSet::new(); + for e in cs.iter().flat_map(|c| c.store.iter()) { + match &e.kind { + EntityKind::Insert(i) => { + out.insert(i.block.to_ascii_uppercase()); + } + EntityKind::Dimension(d) => out.extend(d.block.as_ref().map(|b| b.to_ascii_uppercase())), + EntityKind::MLeader(m) => out.extend(m.block.as_ref().map(|b| b.block.to_ascii_uppercase())), + _ => {} + } + } + out +} + +fn kind_sig(e: &Entity) -> String { + sig_of(&e.kind) +} + +fn compare_store(name: &str, sa: &EntityStore, sb: &EntityStore, ctx: &Ctx, moves: &mut BTreeMap<(String, String), Vec>, out: &mut Out) { + let ib: HashMap = sb.iter().map(|e| (e.handle, e.as_ref())).collect(); + let ia: HashSet = sa.iter().map(|e| e.handle).collect(); + let mut count_a: BTreeMap = BTreeMap::new(); + let mut count_b: BTreeMap = BTreeMap::new(); + let mut un_a: BTreeMap> = BTreeMap::new(); + let mut un_b: BTreeMap> = BTreeMap::new(); + for e in sa.iter() { + let t = e.kind.dxf_name().to_string(); + *count_a.entry(t.clone()).or_default() += 1; + if let Some(f) = ib.get(&e.handle) { + compare_entity(name, e, f, ctx, out); + } else if let Some(other) = ctx.where_b.get(&e.handle).and_then(|j| ctx.cb.get(*j)) { + moves.entry((name.to_string(), other.name.clone())).or_default().push(e.handle); + if let Some(f) = other.store.get(e.handle) { + compare_entity(name, e, f, ctx, out); + } + } else { + un_a.entry(t).or_default().push(e); + } + } + for e in sb.iter() { + let t = e.kind.dxf_name().to_string(); + *count_b.entry(t.clone()).or_default() += 1; + if !ia.contains(&e.handle) && !ctx.where_a.contains_key(&e.handle) { + un_b.entry(t).or_default().push(e); + } + } + let types: BTreeSet = un_a.keys().chain(un_b.keys()).cloned().collect(); + for t in types { + let xs = un_a.remove(&t).unwrap_or_default(); + let ys = un_b.remove(&t).unwrap_or_default(); + let (pairs, lost, added) = pair_up(xs, ys, kind_sig); + for (x, y) in &pairs { + out.renumber(&format!("{t} entities"), || format!("{} → {} in {name}", x.handle.hex(), y.handle.hex())); + compare_entity(name, x, y, ctx, out); + } + let (na, nb) = (count_a.get(&t).copied().unwrap_or(0), count_b.get(&t).copied().unwrap_or(0)); + if !lost.is_empty() { + let detail = format!("{t}: {na} → {nb} ({} lost: {})", lost.len(), hex_list(lost.iter().map(|e| e.handle), 8)); + out.push_n(Category::Count, Change::Lost, lost.len(), format!("{name} {t}"), detail); + } + if !added.is_empty() { + let detail = format!("{t}: {na} → {nb} ({} added: {})", added.len(), hex_list(added.iter().map(|e| e.handle), 8)); + out.push_n(Category::Count, Change::Added, added.len(), format!("{name} {t}"), detail); + } + } +} + +fn compare_entity(container: &str, x: &Entity, y: &Entity, ctx: &Ctx, out: &mut Out) { + let handles = if x.handle == y.handle { x.handle.hex() } else { format!("{}→{}", x.handle.hex(), y.handle.hex()) }; + let place = format!("{container} {} {handles}", x.kind.dxf_name()); + if std::mem::discriminant(&x.kind) != std::mem::discriminant(&y.kind) || x.kind.dxf_name() != y.kind.dxf_name() { + out.push(Category::Geometry, Change::Changed, place, format!("type {} → {}", x.kind.dxf_name(), y.kind.dxf_name())); + return; + } + match (&x.kind, &y.kind) { + (EntityKind::Unknown(u), EntityKind::Unknown(v)) => { + if let Some(d) = tags_diff(&u.tags, &v.tags, out.opts) { + out.push(Category::Opaque, Change::Changed, place.clone(), d); + } + } + _ => { + let p = diff_ser(&x.kind, &y.kind, &out.norm()); + if !p.is_empty() { + out.push(Category::Geometry, Change::Changed, place.clone(), p.text()); + } + } + } + let common = |e: &Entity| { + let mut v = serde_json::to_value(&e.common).unwrap_or(Value::Null); + if let Value::Object(m) = &mut v { + m.remove("xdata"); + } + v + }; + let mut p = Paths::new(out.opts.max_paths); + diff_values(&common(x), &common(y), &mut String::new(), &out.norm(), &mut p, 0); + if !p.is_empty() { + out.push(Category::Properties, Change::Changed, place.clone(), p.text()); + } + compare_xdata(&place, &x.common.xdata, &y.common.xdata, out); + compare_extra(&place, ctx.a.foreign.entities.get(&x.handle), ctx.b.foreign.entities.get(&y.handle), out); +} + +/// Extended data split per application (from each 1001 group). +fn xdata_apps(tags: &[RawTag]) -> Vec<(String, &[RawTag])> { + let starts: Vec = tags.iter().enumerate().filter(|(_, t)| t.code == 1001).map(|(i, _)| i).collect(); + let mut v = Vec::new(); + for (k, &s) in starts.iter().enumerate() { + let end = starts.get(k + 1).copied().unwrap_or(tags.len()); + let name = tags.get(s).map(|t| t.value.trim().to_ascii_uppercase()).unwrap_or_default(); + v.push((name, tags.get(s..end).unwrap_or(&[]))); + } + if starts.first().is_some_and(|&s| s > 0) || (starts.is_empty() && !tags.is_empty()) { + let end = starts.first().copied().unwrap_or(tags.len()); + v.insert(0, (String::new(), tags.get(..end).unwrap_or(&[]))); + } + v +} + +fn compare_xdata(place: &str, a: &[RawTag], b: &[RawTag], out: &mut Out) { + if a.is_empty() && b.is_empty() { + return; + } + let (xa, xb) = (xdata_apps(a), xdata_apps(b)); + let mb: HashMap<&str, &[RawTag]> = xb.iter().map(|(n, t)| (n.as_str(), *t)).collect(); + let ma: HashSet<&str> = xa.iter().map(|(n, _)| n.as_str()).collect(); + for (name, ta) in &xa { + let label = if name.is_empty() { "(no application)" } else { name }; + match mb.get(name.as_str()) { + None => out.push(Category::Xdata, Change::Lost, place, format!("application {label} ({} groups)", ta.len())), + Some(tb) => { + if let Some(d) = tags_diff(ta, tb, out.opts) { + out.push(Category::Xdata, Change::Changed, place, format!("application {label}: {d}")); + } + } + } + } + for (name, tb) in &xb { + if !ma.contains(name.as_str()) { + let label = if name.is_empty() { "(no application)" } else { name }; + out.push(Category::Xdata, Change::Added, place, format!("application {label} ({} groups)", tb.len())); + } + } +} + +fn compare_extra(place: &str, a: Option<&ForeignExtra>, b: Option<&ForeignExtra>, out: &mut Out) { + let none = ForeignExtra::default(); + let (a, b) = (a.unwrap_or(&none), b.unwrap_or(&none)); + if a == b { + return; + } + match (a.xdict, b.xdict) { + (Some(h), None) => out.push(Category::ExtDict, Change::Lost, place, format!("extension dictionary {}", h.hex())), + (None, Some(h)) => out.push(Category::ExtDict, Change::Added, place, format!("extension dictionary {}", h.hex())), + (Some(h), Some(k)) if h != k => out.renumber("extension dictionaries", || format!("{} → {} of {place}", h.hex(), k.hex())), + _ => {} + } + if a.reactors.len() > b.reactors.len() { + let n = a.reactors.len() - b.reactors.len(); + out.push_n(Category::ExtDict, Change::Lost, n, place, format!("reactors: {} → {}", a.reactors.len(), b.reactors.len())); + } else if b.reactors.len() > a.reactors.len() { + let n = b.reactors.len() - a.reactors.len(); + out.push_n(Category::ExtDict, Change::Added, n, place, format!("reactors: {} → {}", a.reactors.len(), b.reactors.len())); + } + compare_xdata(place, &a.xdata, &b.xdata, out); + if a.color_book != b.color_book { + let show = |c: &Option<(String, String)>| c.as_ref().map_or("(none)".to_string(), |(book, tc)| format!("`{}` ({tc})", clip(book, 40))); + let change = match (&a.color_book, &b.color_book) { + (Some(_), None) => Change::Lost, + (None, Some(_)) => Change::Added, + _ => Change::Changed, + }; + out.push(Category::Properties, change, place, format!("colour book colour {} → {}", show(&a.color_book), show(&b.color_book))); + } + if let Some(d) = tags_diff(&a.props, &b.props, out.opts) { + out.push(Category::Properties, Change::Changed, place, format!("other property groups (material, shadows…): {d}")); + } +} + +// --------------------------------------------------------------------------------------------- +// Tables, blocks, layouts + +/// Compare two lists of named records, matched by name without case. +fn compare_named(out: &mut Out, cat: Category, kind: &str, a: &[&T], b: &[&T], name: impl Fn(&T) -> &str) { + let mut mb: HashMap = HashMap::new(); + for y in b.iter().rev() { + mb.insert(name(y).to_ascii_uppercase(), *y); + } + let mut seen: HashSet = HashSet::new(); + for x in a { + let key = name(x).to_ascii_uppercase(); + if !seen.insert(key.clone()) { + continue; + } + let place = format!("{kind} {}", name(x)); + match mb.get(&key) { + None => out.push(cat, Change::Lost, place, String::new()), + Some(y) => { + let p = diff_ser(*x, *y, &out.norm()); + if !p.is_empty() { + out.push(cat, Change::Changed, place, p.text()); + } + } + } + } + let mut seen_b: HashSet = HashSet::new(); + for y in b { + let key = name(y).to_ascii_uppercase(); + if !seen.contains(&key) && seen_b.insert(key) { + out.push(cat, Change::Added, format!("{kind} {}", name(y)), String::new()); + } + } +} + +fn refs(v: &[T]) -> Vec<&T> { + v.iter().collect() +} + +fn compare_tables(a: &Drawing, b: &Drawing, out: &mut Out) { + use Category::{Block, Layout, Table}; + compare_named(out, Table, "LAYER", &refs(&a.layers), &refs(&b.layers), |x| &x.name); + compare_named(out, Table, "LTYPE", &refs(&a.linetypes), &refs(&b.linetypes), |x| &x.name); + compare_named(out, Table, "STYLE", &refs(&a.text_styles), &refs(&b.text_styles), |x| &x.name); + compare_named(out, Table, "DIMSTYLE", &refs(&a.dim_styles), &refs(&b.dim_styles), |x| &x.name); + compare_named(out, Table, "MLEADERSTYLE", &refs(&a.mleader_styles), &refs(&b.mleader_styles), |x| &x.name); + compare_named(out, Table, "TABLESTYLE", &refs(&a.table_styles), &refs(&b.table_styles), |x| &x.name); + compare_named(out, Table, "MLINESTYLE", &refs(&a.mline_styles), &refs(&b.mline_styles), |x| &x.name); + compare_named(out, Table, "VIEW", &refs(&a.views), &refs(&b.views), |x| &x.name); + compare_named(out, Table, "VPORT", &refs(&a.vports), &refs(&b.vports), |x| &x.name); + compare_named(out, Table, "UCS", &refs(&a.ucss), &refs(&b.ucss), |x| &x.name); + compare_named(out, Table, "LAYERSTATE", &refs(&a.layer_states), &refs(&b.layer_states), |x| &x.name); + compare_named(out, Table, "GROUP", &refs(&a.groups), &refs(&b.groups), |x| &x.name); + compare_named(out, Table, "SCALE", &refs(&a.scales), &refs(&b.scales), |x| &x.name); + compare_named(out, Table, "PLOTSTYLE", &refs(&a.plot_style_tables), &refs(&b.plot_style_tables), |x| &x.name); + // Blocks: renamed anonymous blocks are compared with what they were saved as. + let renamed_to: HashSet = out.renamed.values().cloned().collect(); + let ba: Vec<&cadcraft_doc::Block> = a + .blocks + .values() + .map(|x| x.as_ref()) + .filter(|x| { + let up = x.name.to_ascii_uppercase(); + !out.renamed.contains_key(&up) && !out.orphans.contains(&up) + }) + .collect(); + if let Some(first) = out.orphans.first().cloned() { + let n = out.orphans.len(); + out.push_n( + Block, + Change::Lost, + n, + "unreferenced anonymous blocks", + format!("{n} anonymous blocks nothing refers to (e.g. {first}) were not saved"), + ); + } + let bb: Vec<&cadcraft_doc::Block> = + b.blocks.values().map(|x| x.as_ref()).filter(|x| !renamed_to.contains(&x.name.to_ascii_uppercase())).collect(); + let mut pairs: Vec<(&cadcraft_doc::Block, &cadcraft_doc::Block)> = Vec::new(); + for (from, to) in &out.renamed { + if let (Some(x), Some(y)) = (a.block(from), b.block(to)) { + pairs.push((x.as_ref(), y.as_ref())); + } + } + compare_named(out, Block, "BLOCK", &ba, &bb, |x| &x.name); + pairs.sort_by(|x, y| x.0.name.cmp(&y.0.name)); + if let Some((x, y)) = pairs.first() { + let detail = format!("{} anonymous blocks saved under new names (e.g. {} → {})", pairs.len(), x.name, y.name); + out.push_n(Block, Change::Changed, pairs.len(), "anonymous blocks", detail); + } + for (x, y) in pairs { + let p = diff_ser(x, y, &out.norm()); + if !p.is_empty() { + out.push(Block, Change::Changed, format!("BLOCK {} → {}", x.name, y.name), p.text()); + } + } + compare_named(out, Layout, "LAYOUT", &refs(&a.layouts), &refs(&b.layouts), |x| &x.name); + for (place, p) in + [("CONSTRAINTS", diff_ser(&a.constraints, &b.constraints, &out.norm())), ("PARAMETERS", diff_ser(&a.parametric, &b.parametric, &out.norm()))] + { + if !p.is_empty() { + out.push(Category::Object, Change::Changed, place, p.text()); + } + } +} + +// --------------------------------------------------------------------------------------------- +// Data kept without modelling it + +fn class_name(c: &[RawTag]) -> String { + c.iter().find(|t| t.code == 1).map(|t| t.value.trim().to_string()).unwrap_or_default() +} + +/// A CLASS record without its instance count (group 91), which follows the objects written. +fn class_groups(c: &[RawTag]) -> Vec { + c.iter().filter(|t| t.code != 91).cloned().collect() +} + +fn compare_foreign(a: &Foreign, b: &Foreign, out: &mut Out) { + let o = out.opts; + type Obj = cadcraft_doc::foreign::ForeignObject; + // Objects, by handle, then by content. + let ib: HashMap = b.objects.iter().map(|x| (x.handle, x)).collect(); + let ia: HashSet = a.objects.iter().map(|x| x.handle).collect(); + let mut un_a: BTreeMap> = BTreeMap::new(); + let mut un_b: BTreeMap> = BTreeMap::new(); + for x in &a.objects { + match ib.get(&x.handle) { + Some(y) if y.kind != x.kind => { + out.push(Category::Object, Change::Changed, format!("{} {}", x.kind, x.handle.hex()), format!("type {} → {}", x.kind, y.kind)) + } + Some(y) => { + if let Some(d) = tags_diff(&x.tags, &y.tags, o) { + out.push(Category::Object, Change::Changed, format!("{} {}", x.kind, x.handle.hex()), d); + } + } + None => un_a.entry(x.kind.clone()).or_default().push(x), + } + } + for y in &b.objects { + if !ia.contains(&y.handle) { + un_b.entry(y.kind.clone()).or_default().push(y); + } + } + // Content without handles, so renumbered objects pair up. + let content = |x: &Obj| sig_of(&x.tags.iter().filter(|t| !is_handle_code(t.code)).map(|t| (t.code, &t.value)).collect::>()); + let kinds: BTreeSet = un_a.keys().chain(un_b.keys()).cloned().collect(); + for k in kinds { + let xs = un_a.remove(&k).unwrap_or_default(); + let ys = un_b.remove(&k).unwrap_or_default(); + let (pairs, lost, added) = pair_up(xs, ys, content); + for (x, y) in &pairs { + out.renumber(&format!("{k} objects"), || format!("{} → {}", x.handle.hex(), y.handle.hex())); + if let Some(d) = tags_diff(&x.tags, &y.tags, o) { + out.push(Category::Object, Change::Changed, format!("{k} {}→{}", x.handle.hex(), y.handle.hex()), d); + } + } + if !lost.is_empty() { + out.push_n( + Category::Object, + Change::Lost, + lost.len(), + format!("objects {k}"), + format!("{} lost: {}", lost.len(), hex_list(lost.iter().map(|x| x.handle), 8)), + ); + } + if !added.is_empty() { + out.push_n( + Category::Object, + Change::Added, + added.len(), + format!("objects {k}"), + format!("{} added: {}", added.len(), hex_list(added.iter().map(|x| x.handle), 8)), + ); + } + } + // Named object dictionary entries. + let db: HashMap = b.dictionary.iter().map(|(k, h)| (k.to_ascii_uppercase(), *h)).collect(); + let da: HashSet = a.dictionary.iter().map(|(k, _)| k.to_ascii_uppercase()).collect(); + for (k, h) in &a.dictionary { + match db.get(&k.to_ascii_uppercase()) { + None => out.push(Category::Dictionary, Change::Lost, format!("NOD {k}"), format!("entry for {}", h.hex())), + Some(g) if g != h => out.renumber("dictionary entries", || format!("{k}: {} → {}", h.hex(), g.hex())), + Some(_) => {} + } + } + for (k, h) in &b.dictionary { + if !da.contains(&k.to_ascii_uppercase()) { + out.push(Category::Dictionary, Change::Added, format!("NOD {k}"), format!("entry for {}", h.hex())); + } + } + // CLASS records. + let cb: HashMap> = b.classes.iter().map(|c| (class_name(c).to_ascii_uppercase(), c)).collect(); + let ca: HashSet = a.classes.iter().map(|c| class_name(c).to_ascii_uppercase()).collect(); + for c in &a.classes { + let n = class_name(c); + match cb.get(&n.to_ascii_uppercase()) { + None => out.push(Category::Class, Change::Lost, format!("CLASS {n}"), String::new()), + Some(d) => { + if let Some(t) = tags_diff(&class_groups(c), &class_groups(d), o) { + out.push(Category::Class, Change::Changed, format!("CLASS {n}"), t); + } + } + } + } + for c in &b.classes { + let n = class_name(c); + if !ca.contains(&n.to_ascii_uppercase()) { + out.push(Category::Class, Change::Added, format!("CLASS {n}"), String::new()); + } + } + // Extras of table records, tables and blocks (renamed anonymous blocks by their new name). + let key_of = |k: &(String, String)| -> (String, String) { + match out.renamed.get(&k.1.to_ascii_uppercase()) { + Some(n) if k.0.starts_with("BLOCK") => (k.0.clone(), n.clone()), + _ => k.clone(), + } + }; + let ra: BTreeMap<(String, String), &ForeignExtra> = a.records.iter().map(|(k, v)| (key_of(k), v)).collect(); + let rb: BTreeMap<(String, String), &ForeignExtra> = b.records.iter().map(|(k, v)| ((k.0.clone(), k.1.to_ascii_uppercase()), v)).collect(); + let ra: BTreeMap<(String, String), &ForeignExtra> = ra.into_iter().map(|((k0, k1), v)| ((k0, k1.to_ascii_uppercase()), v)).collect(); + let keys: BTreeSet<&(String, String)> = ra.keys().chain(rb.keys()).collect(); + for k in keys { + compare_extra(&format!("{} {}", k.0, k.1), ra.get(k).copied(), rb.get(k).copied(), out); + } + // The data store (ACIS data of 2013+ files). + if let Some(d) = tags_diff(&a.data_store, &b.data_store, o) { + let change = match (a.data_store.is_empty(), b.data_store.is_empty()) { + (false, true) => Change::Lost, + (true, false) => Change::Added, + _ => Change::Changed, + }; + out.push(Category::Object, change, "ACDSDATA", d); + } +} + +// --------------------------------------------------------------------------------------------- +// Header + +fn compare_header(a: &Drawing, b: &Drawing, out: &mut Out) { + let skip = |k: &str| VOLATILE_HEADER.contains(&k); + for (k, va) in &a.header.vars { + if skip(k) { + continue; + } + let place = format!("${k}"); + match b.header.vars.get(k) { + None => out.push(Category::Header, Change::Lost, place, va.display()), + Some(vb) => { + let p = diff_ser(va, vb, &out.norm()); + if !p.is_empty() { + out.push(Category::Header, Change::Changed, place, format!("{} → {}", clip(&va.display(), 60), clip(&vb.display(), 60))); + } + } + } + } + for (k, vb) in &b.header.vars { + if !skip(k) && !a.header.vars.contains_key(k) { + out.push(Category::Header, Change::Added, format!("${k}"), vb.display()); + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/io/src/roundtrip/census.rs b/crates/io/src/roundtrip/census.rs new file mode 100644 index 000000000..bf0d4488e --- /dev/null +++ b/crates/io/src/roundtrip/census.rs @@ -0,0 +1,212 @@ +//! What a DXF file holds, counted from its groups, and the same counts of a drawing read from it: +//! together they show what opening a file leaves out (the DXF rendition of a DWG for DWG files). + +use std::collections::{BTreeMap, HashSet}; + +use cadcraft_doc::Drawing; +use serde::Serialize; + +use super::{Category, Change, Comparison, Options, Out}; + +/// Counts of what a file (or a drawing) holds. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Census { + /// Entities of model space and every layout, per DXF type. + pub entities: BTreeMap, + /// Entities of block definitions (other than the model and paper space blocks), per type. + pub block_entities: BTreeMap, + /// Block definitions other than the model and paper space blocks and the generated ones. + pub blocks: usize, + /// Blocks CADCraft generates and so doesn't read (tables' blocks, standard arrowheads + /// nothing inserts); their entities are not counted. + pub generated_blocks: usize, + /// Table records per table type (LAYER, LTYPE, STYLE, DIMSTYLE, VIEW, UCS…). + pub tables: BTreeMap, + /// Non-graphical objects per type. For a drawing: only those kept without being modelled. + pub objects: BTreeMap, + /// CLASS records. For a drawing: only those kept for unmodelled objects. + pub classes: usize, +} + +/// Records that belong to the record before them (or delimit blocks), not entities of their own. +const SUB_RECORDS: &[&str] = &["VERTEX", "SEQEND", "ATTRIB", "BLOCK", "ENDBLK"]; + +fn bump(m: &mut BTreeMap, k: &str) { + let n = m.entry(k.to_string()).or_insert(0); + *n = n.saturating_add(1); +} + +/// A layout's own paper-space viewport (VIEWPORT with ID 1, group 69): part of the layout, not +/// an entity of its own for CADCraft. +fn is_overall_viewport(t: &str, g: &[cadcraft_dxf::Tag]) -> bool { + t == "VIEWPORT" && g.iter().any(|x| x.code == 69 && x.i64() == 1) +} + +fn is_space_block(name: &str) -> bool { + let up = name.trim().to_ascii_uppercase(); + up.starts_with("*MODEL_SPACE") || up.starts_with("*PAPER_SPACE") +} + +/// Count what a DXF file (ASCII or binary) holds; `None` when it can't be parsed. +pub fn census(dxf: &[u8]) -> Option { + let tags = cadcraft_dxf::parse(dxf).ok()?; + let mut c = Census::default(); + // Blocks CADCraft generates (tables' blocks, standard arrowheads nothing inserts) are left out + // on read and rebuilt on save: counted apart. + let mut table_blocks: HashSet = HashSet::new(); + let mut inserted: HashSet = HashSet::new(); + let mut blocks: Vec<(String, BTreeMap)> = Vec::new(); + let mut note_refs = |t: &str, g: &[cadcraft_dxf::Tag]| { + let name = || g.iter().find(|x| x.code == 2).map(|x| x.str().trim().to_ascii_uppercase()); + match t { + "ACAD_TABLE" => table_blocks.extend(name()), + "INSERT" => inserted.extend(name()), + _ => {} + } + }; + for s in cadcraft_dxf::sections(&tags) { + let records = cadcraft_dxf::records(&s.tags); + match s.name.trim().to_ascii_uppercase().as_str() { + "ENTITIES" => { + for (t, g) in &records { + note_refs(t, g); + if !SUB_RECORDS.contains(&t.as_str()) && !is_overall_viewport(t, g) { + bump(&mut c.entities, t); + } + } + } + "BLOCKS" => { + let mut space = false; + for (t, g) in &records { + note_refs(t, g); + match t.as_str() { + "BLOCK" => { + let name = g.iter().find(|x| x.code == 2).map(cadcraft_dxf::Tag::str).unwrap_or_default(); + space = is_space_block(&name); + if !space { + blocks.push((name.trim().to_ascii_uppercase(), BTreeMap::new())); + } + } + t if SUB_RECORDS.contains(&t) || is_overall_viewport(t, g) => {} + t if space => bump(&mut c.entities, t), + t => { + if let Some((_, m)) = blocks.last_mut() { + bump(m, t); + } + } + } + } + } + "TABLES" => { + for (t, _) in &records { + if t != "TABLE" && t != "ENDTAB" { + bump(&mut c.tables, t); + } + } + } + "OBJECTS" => { + for (t, _) in &records { + bump(&mut c.objects, t); + } + } + "CLASSES" => c.classes = c.classes.saturating_add(records.iter().filter(|(t, _)| t == "CLASS").count()), + _ => {} + } + } + for (name, m) in blocks { + if table_blocks.contains(&name) || (crate::dxf_ext::is_standard_arrow(&name) && !inserted.contains(&name)) { + c.generated_blocks = c.generated_blocks.saturating_add(1); + continue; + } + c.blocks = c.blocks.saturating_add(1); + for (t, n) in m { + let slot = c.block_entities.entry(t).or_insert(0); + *slot = slot.saturating_add(n); + } + } + Some(c) +} + +/// The same counts of a drawing: its entities, blocks and table records, and the objects and +/// classes it keeps without modelling them. +pub fn census_of(d: &Drawing) -> Census { + let mut c = Census::default(); + for e in d.model.iter().chain(d.layouts.iter().flat_map(|l| l.entities.iter())) { + bump(&mut c.entities, e.kind.dxf_name()); + } + for b in d.blocks.values() { + for e in b.entities.iter() { + bump(&mut c.block_entities, e.kind.dxf_name()); + } + } + c.blocks = d.blocks.len(); + for (t, n) in [ + ("LAYER", d.layers.len()), + ("LTYPE", d.linetypes.len()), + ("STYLE", d.text_styles.len()), + ("DIMSTYLE", d.dim_styles.len()), + ("VIEW", d.views.len()), + ("UCS", d.ucss.len()), + ] { + c.tables.insert(t.to_string(), n); + } + for o in &d.foreign.objects { + bump(&mut c.objects, &o.kind); + } + c.classes = d.foreign.classes.len(); + c +} + +/// Polylines may change between the old-style POLYLINE and LWPOLYLINE when read: counted +/// together. +fn merged(m: &BTreeMap) -> BTreeMap { + let mut out = BTreeMap::new(); + for (k, n) in m { + let key = if k == "POLYLINE" || k == "LWPOLYLINE" { "POLYLINE+LWPOLYLINE" } else { k.as_str() }; + let slot = out.entry(key.to_string()).or_insert(0usize); + *slot = slot.saturating_add(*n); + } + out +} + +fn compare_counts(out: &mut Out, place: &str, file: &BTreeMap, read: &BTreeMap) { + let (file, read) = (merged(file), merged(read)); + let keys: std::collections::BTreeSet<&String> = file.keys().chain(read.keys()).collect(); + for k in keys { + let (f, r) = (file.get(k).copied().unwrap_or(0), read.get(k).copied().unwrap_or(0)); + if r < f { + out.push_n(Category::Read, Change::Lost, f - r, format!("{place} {k}"), format!("{k}: {f} in the file, {r} read")); + } else if r > f { + out.push_n(Category::Read, Change::Added, r - f, format!("{place} {k}"), format!("{k}: {f} in the file, {r} read")); + } + } +} + +/// What a drawing read from a file holds less (or more) than the file: entities per type in the +/// spaces and in blocks, blocks, and the table records CADCraft models. Blocks CADCraft generates +/// (tables' blocks, unused standard arrowheads) are left out on both sides. +pub fn compare_census(file: &Census, d: &Drawing, opts: &Options) -> Comparison { + let read = census_of(d); + let mut out = Out::new(opts); + compare_counts(&mut out, "spaces", &file.entities, &read.entities); + compare_counts(&mut out, "blocks", &file.block_entities, &read.block_entities); + if file.blocks != read.blocks { + let change = if read.blocks < file.blocks { Change::Lost } else { Change::Added }; + out.push_n( + Category::Read, + change, + file.blocks.abs_diff(read.blocks), + "BLOCK definitions", + format!("{} in the file, {} read", file.blocks, read.blocks), + ); + } + for t in ["LAYER", "LTYPE", "STYLE", "DIMSTYLE", "VIEW", "UCS"] { + let (f, r) = (file.tables.get(t).copied().unwrap_or(0), read.tables.get(t).copied().unwrap_or(0)); + if f != r { + let change = if r < f { Change::Lost } else { Change::Added }; + out.push_n(Category::Read, change, f.abs_diff(r), format!("{t} table"), format!("{f} records in the file, {r} read")); + } + } + out.cmp +} diff --git a/crates/io/src/roundtrip/corpus.rs b/crates/io/src/roundtrip/corpus.rs new file mode 100644 index 000000000..72f78ce53 --- /dev/null +++ b/crates/io/src/roundtrip/corpus.rs @@ -0,0 +1,651 @@ +//! The corpus harness: every DXF and DWG file of a folder is opened, saved as DXF and as DWG, +//! reopened and compared with the drawing first opened; the findings go to a Markdown and a JSON +//! report. A file that fails, or even makes CADCraft panic, is a reported failure: each stage runs +//! under `catch_unwind` here, as a last resort of the harness only. + +use std::collections::BTreeMap; +use std::panic::AssertUnwindSafe; +use std::path::{Path, PathBuf}; +use std::time::Instant; + +use cadcraft_doc::Drawing; +use serde::Serialize; + +use super::{Category, Census, Change, Comparison, Options, Tally, census, census_of, clip, compare, compare_census}; + +/// A drawing file format the harness checks. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)] +#[serde(rename_all = "lowercase")] +pub enum Format { + Dxf, + Dwg, +} + +impl Format { + pub fn ext(self) -> &'static str { + match self { + Format::Dxf => "dxf", + Format::Dwg => "dwg", + } + } + pub fn label(self) -> &'static str { + match self { + Format::Dxf => "DXF", + Format::Dwg => "DWG", + } + } + /// The format of a file's bytes: DWG by its magic, otherwise DXF. + pub fn of(bytes: &[u8]) -> Format { + if cadcraft_dwg::is_dwg(bytes) { Format::Dwg } else { Format::Dxf } + } + fn other(self) -> Format { + match self { + Format::Dxf => Format::Dwg, + Format::Dwg => Format::Dxf, + } + } +} + +/// How the harness runs. +#[derive(Clone, Debug, PartialEq)] +pub struct CorpusOptions { + pub compare: Options, + /// Also save in the other format (DWG → DXF, DXF → DWG). + pub cross_format: bool, + /// Count what each file holds, to report what opening it leaves out. + pub census: bool, + /// Keep the saved files in this folder (and reopen them from there). + pub keep_saved: Option, +} + +impl Default for CorpusOptions { + fn default() -> Self { + CorpusOptions { compare: Options::default(), cross_format: true, census: true, keep_saved: None } + } +} + +/// Largest file the harness opens. +pub const MAX_FILE_BYTES: u64 = 1 << 30; +/// Deepest folder nesting walked. +const MAX_DEPTH: usize = 32; +/// Most files taken from one corpus. +const MAX_FILES: usize = 100_000; + +/// One save → reopen of a drawing. +#[derive(Clone, Debug, Default, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct RouteReport { + /// `dwg → dxf`. + pub route: String, + pub saved_bytes: usize, + /// Where the saved file was kept, when it was. + #[serde(skip_serializing_if = "Option::is_none")] + pub saved_path: Option, + pub save_error: Option, + pub reopen_error: Option, + /// What reopening reported (records the reader couldn't place or decode). + pub reopen_notes: Vec, + /// What the writer itself left out of the saved file: records per DXF type (upper case) it + /// can't write in that format (for DWG: other programs' custom entities and objects). Each + /// is lost; the reopened drawing's entity counts show the same entities missing, and a strict + /// corpus run fails on any. + pub not_written: BTreeMap, + /// The reopened drawing against the drawing first opened. + pub comparison: Comparison, + pub seconds: f64, +} + +impl RouteReport { + pub fn ok(&self) -> bool { + self.save_error.is_none() && self.reopen_error.is_none() + } + /// Saved and reopened with no difference at all. + pub fn lossless(&self) -> bool { + self.ok() && self.not_written.is_empty() && self.comparison.is_empty() + } + /// How many records the writer left out ([`Self::not_written`]). + pub fn not_written_total(&self) -> usize { + self.not_written.values().fold(0usize, |a, n| a.saturating_add(*n)) + } +} + +/// `2 ACME_WIDGET, 1 OLE2FRAME`. +fn per_type(m: &BTreeMap) -> String { + m.iter().map(|(t, n)| format!("{n} {t}")).collect::>().join(", ") +} + +/// Everything found for one file. +#[derive(Clone, Debug, Default, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct FileReport { + /// The path relative to the corpus folder. + pub path: String, + pub format: Option, + /// `AC1032` for a DWG, the `$ACADVER` of a DXF. + pub version: String, + pub bytes: u64, + pub open_error: Option, + /// What opening reported (a DWG's records the reader skipped, damaged records). + pub open_notes: Vec, + pub open_seconds: f64, + /// What the file holds (for a DWG: its DXF rendition), when counted. + pub file_census: Option, + /// What the drawing read from it holds. + pub drawing_census: Option, + /// What the drawing holds less or more than the file. + pub read: Comparison, + pub routes: Vec, +} + +impl FileReport { + pub fn opened(&self) -> bool { + self.open_error.is_none() + } +} + +impl CorpusReport { + /// What makes a strict corpus run fail, one line each: a file that didn't open, a round trip + /// that didn't save or reopen, and records a writer left out ([`RouteReport::not_written`]). + /// Other differences are reported, not failed on. + pub fn strict_failures(&self) -> Vec { + let mut out = Vec::new(); + for f in &self.files { + if !f.opened() { + out.push(format!("{}: failed to open", f.path)); + } + for r in &f.routes { + if !r.ok() { + out.push(format!("{} ({}): failed to save or reopen", f.path, r.route)); + } + if !r.not_written.is_empty() { + out.push(format!("{} ({}): lost on save, not written: {}", f.path, r.route, per_type(&r.not_written))); + } + } + } + out + } +} + +/// The whole corpus run. +#[derive(Clone, Debug, Default, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct CorpusReport { + pub corpus: String, + /// UTC, `2026-10-21 09:30`. + pub generated: String, + pub cadcraft_version: String, + pub abs_tol: f64, + pub rel_tol: f64, + pub files: Vec, +} + +/// Run `f`, turning a panic into an error message. +fn guard(f: impl FnOnce() -> T) -> Result { + std::panic::catch_unwind(AssertUnwindSafe(f)).map_err(|p| { + let msg = + p.downcast_ref::<&str>().map(|s| s.to_string()).or_else(|| p.downcast_ref::().cloned()).unwrap_or_else(|| "no message".into()); + format!("CRASH (panic): {msg}") + }) +} + +/// A drawing's notes on what reading it left out. +fn notes(d: &Drawing) -> Vec { + let mut v = d.read_notes.clone(); + for (section, n) in &d.skipped_records { + v.push(format!("{n} record(s) of the {section} section couldn't be placed and were skipped")); + } + v +} + +/// Save `d` as `to`, reopen it and compare. `keep` is the file to keep the saved bytes in (and +/// reopen them from). +pub fn round_trip(d: &Drawing, from: Format, to: Format, opts: &Options, keep: Option<&Path>) -> RouteReport { + let start = Instant::now(); + let mut r = RouteReport { route: format!("{} → {}", from.ext(), to.ext()), ..RouteReport::default() }; + let name = format!("roundtrip.{}", to.ext()); + let saved = match guard(|| crate::write_noted(d, &name, &crate::SaveOptions::default())) { + Ok(Ok((b, not_written))) => { + r.not_written = not_written; + b + } + Ok(Err(e)) => { + r.save_error = Some(e.to_string()); + r.seconds = start.elapsed().as_secs_f64(); + return r; + } + Err(e) => { + r.save_error = Some(e); + r.seconds = start.elapsed().as_secs_f64(); + return r; + } + }; + r.saved_bytes = saved.len(); + let mut bytes = saved; + if let Some(path) = keep { + let kept = path.parent().map_or(Ok(()), std::fs::create_dir_all).and_then(|_| std::fs::write(path, &bytes)).and_then(|_| std::fs::read(path)); + match kept { + Ok(b) => { + bytes = b; + r.saved_path = Some(path.display().to_string()); + } + Err(e) => r.reopen_notes.push(format!("couldn't keep the saved file at {}: {e}", path.display())), + } + } + let reopened = match guard(|| crate::read(&bytes, &name)) { + Ok(Ok(d2)) => d2, + Ok(Err(e)) => { + r.reopen_error = Some(e.to_string()); + r.seconds = start.elapsed().as_secs_f64(); + return r; + } + Err(e) => { + r.reopen_error = Some(e); + r.seconds = start.elapsed().as_secs_f64(); + return r; + } + }; + r.reopen_notes.extend(notes(&reopened)); + match guard(|| compare(d, &reopened, opts)) { + Ok(c) => r.comparison = c, + Err(e) => r.reopen_error = Some(format!("comparing: {e}")), + } + attribute_unwritten(&mut r, to); + r.seconds = start.elapsed().as_secs_f64(); + r +} + +/// Say on the lost entity rows of a type the writer couldn't write that this is why they are +/// lost, so a writer's gap reads as one and isn't taken for a reader's or a comparison's. +fn attribute_unwritten(r: &mut RouteReport, to: Format) { + if r.not_written.is_empty() { + return; + } + for d in r.comparison.differences.iter_mut().filter(|d| d.category == Category::Count && d.change == Change::Lost) { + if let Some(t) = r.not_written.keys().find(|t| d.detail.starts_with(&format!("{t}: "))) { + d.detail.push_str(&format!(" (not written: the {} writer can't write {t})", to.label())); + } + } +} + +/// The kept saved file of `rel` saved as `to`, in `dir`. +fn kept_path(dir: &Path, rel: &str, to: Format) -> PathBuf { + let flat: String = rel.chars().map(|c| if matches!(c, '/' | '\\' | ':') { '_' } else { c }).collect(); + dir.join(format!("{flat}.roundtrip.{}", to.ext())) +} + +/// Check one file's bytes: open, count, save in each format, reopen, compare. `rel` names it in +/// the report. +pub fn check_bytes(bytes: &[u8], rel: &str, opts: &CorpusOptions) -> FileReport { + let format = Format::of(bytes); + let mut f = FileReport { path: rel.to_string(), format: Some(format), bytes: bytes.len() as u64, ..FileReport::default() }; + if format == Format::Dwg { + f.version = cadcraft_dwg::version(bytes).unwrap_or_default(); + } + let start = Instant::now(); + let name = format!("source.{}", format.ext()); + let opened = guard(|| crate::read_source(bytes, &name)); + f.open_seconds = start.elapsed().as_secs_f64(); + let (d, dxf) = match opened { + Ok(Ok(x)) => x, + Ok(Err(e)) => { + f.open_error = Some(e.to_string()); + return f; + } + Err(e) => { + f.open_error = Some(e); + return f; + } + }; + if format == Format::Dxf { + f.version = d.header.str("ACADVER", ""); + } + f.open_notes = notes(&d); + f.drawing_census = guard(|| census_of(&d)).ok(); + if opts.census { + let source = dxf.as_deref().unwrap_or(bytes); + match guard(|| census(source)) { + Ok(Some(c)) => { + if let Ok(read) = guard(|| compare_census(&c, &d, &opts.compare)) { + f.read = read; + } + f.file_census = Some(c); + } + Ok(None) => f.open_notes.push("the file's groups couldn't be counted".into()), + Err(e) => f.open_notes.push(format!("counting the file's groups: {e}")), + } + } + let mut targets = vec![format]; + if opts.cross_format { + targets.push(format.other()); + } + for to in targets { + let keep = opts.keep_saved.as_deref().map(|dir| kept_path(dir, rel, to)); + f.routes.push(round_trip(&d, format, to, &opts.compare, keep.as_deref())); + } + f +} + +/// Check one file on disk; `rel` names it in the report. +pub fn check_file(path: &Path, rel: &str, opts: &CorpusOptions) -> FileReport { + let fail = |e: String| FileReport { path: rel.to_string(), open_error: Some(e), ..FileReport::default() }; + match std::fs::metadata(path) { + Ok(m) if m.len() > MAX_FILE_BYTES => return fail(format!("the file is {} bytes, more than the {MAX_FILE_BYTES} the harness opens", m.len())), + Ok(_) => {} + Err(e) => return fail(format!("can't read the file: {e}")), + } + match std::fs::read(path) { + Ok(bytes) => check_bytes(&bytes, rel, opts), + Err(e) => fail(format!("can't read the file: {e}")), + } +} + +/// Whether a path names a DXF or DWG file (by extension). +pub fn is_drawing_file(path: &Path) -> bool { + path.extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).is_some_and(|e| e == "dxf" || e == "dwg") +} + +/// Every DXF and DWG file under `dir`, sorted, leaving out hidden folders and the folders in +/// `skip` (the report folder). Symbolic links are not followed. +pub fn corpus_files(dir: &Path, skip: &[PathBuf]) -> Vec { + let skip: Vec = skip.iter().map(|p| p.canonicalize().unwrap_or_else(|_| p.clone())).collect(); + let mut out = Vec::new(); + let mut stack = vec![(dir.to_path_buf(), 0usize)]; + while let Some((d, depth)) = stack.pop() { + let Ok(entries) = std::fs::read_dir(&d) else { continue }; + for entry in entries.flatten() { + let path = entry.path(); + let Ok(ft) = entry.file_type() else { continue }; + if ft.is_dir() { + let hidden = entry.file_name().to_string_lossy().starts_with('.'); + let canon = path.canonicalize().unwrap_or_else(|_| path.clone()); + if !hidden && depth < MAX_DEPTH && !skip.contains(&canon) { + stack.push((path, depth + 1)); + } + } else if ft.is_file() && is_drawing_file(&path) && out.len() < MAX_FILES { + out.push(path); + } + } + } + out.sort(); + out +} + +/// `path` relative to `root`, with forward slashes. +pub fn relative(path: &Path, root: &Path) -> String { + let rel = path.strip_prefix(root).unwrap_or(path); + rel.components().map(|c| c.as_os_str().to_string_lossy().into_owned()).collect::>().join("/") +} + +/// Check `files` (paths under `root`, which names the corpus in the report), calling `progress` +/// with (index, count, relative path) before each. +pub fn run(root: &Path, files: &[PathBuf], opts: &CorpusOptions, progress: &mut dyn FnMut(usize, usize, &str)) -> CorpusReport { + let mut report = CorpusReport { + corpus: root.display().to_string(), + generated: now_utc(), + cadcraft_version: env!("CARGO_PKG_VERSION").to_string(), + abs_tol: opts.compare.abs_tol, + rel_tol: opts.compare.rel_tol, + files: Vec::new(), + }; + for (i, path) in files.iter().enumerate() { + let rel = relative(path, root); + progress(i, files.len(), &rel); + let f = match guard(|| check_file(path, &rel, opts)) { + Ok(f) => f, + Err(e) => FileReport { path: rel, open_error: Some(e), ..FileReport::default() }, + }; + report.files.push(f); + } + report +} + +/// The current time, UTC, as `2026-10-21 09:30`. +fn now_utc() -> String { + let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0); + let days = i64::try_from(secs / 86_400).unwrap_or(0); + let (h, m) = ((secs % 86_400) / 3600, (secs % 3600) / 60); + // Days to a civil date (proleptic Gregorian), after Howard Hinnant's public-domain algorithm. + let z = days + 719_468; + let era = z.div_euclid(146_097); + let doe = z.rem_euclid(146_097); + let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; + let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); + let mp = (5 * doy + 2) / 153; + let day = doy - (153 * mp + 2) / 5 + 1; + let month = if mp < 10 { mp + 3 } else { mp - 9 }; + let year = yoe + era * 400 + i64::from(month <= 2); + format!("{year:04}-{month:02}-{day:02} {h:02}:{m:02} UTC") +} + +// --------------------------------------------------------------------------------------------- +// Rendering + +/// Most differences listed per round trip in the Markdown report (all are in the JSON). +const MD_ROWS: usize = 300; + +/// Text for a Markdown table cell. +fn cell(s: &str) -> String { + s.replace('|', "\\|").replace(['\r', '\n'], " ") +} + +fn size(n: u64) -> String { + if n >= 1 << 20 { + format!("{:.1} MB", n as f64 / f64::from(1u32 << 20)) + } else if n >= 1 << 10 { + format!("{:.0} KB", n as f64 / 1024.0) + } else { + format!("{n} B") + } +} + +fn status(err: &Option) -> String { + match err { + None => "ok".into(), + Some(e) if e.starts_with("CRASH") => "**CRASH**".into(), + Some(_) => "**failed**".into(), + } +} + +fn tally_cells(t: Tally) -> String { + format!("{} | {} | {}", t.lost, t.added, t.changed) +} + +impl CorpusReport { + pub fn to_json(&self) -> String { + serde_json::to_string_pretty(self).unwrap_or_else(|e| format!("{{\"error\": \"{e}\"}}")) + } + + /// Totals per category over every round trip. + pub fn totals(&self) -> std::collections::BTreeMap { + let mut all: std::collections::BTreeMap = std::collections::BTreeMap::new(); + for r in self.files.iter().flat_map(|f| f.routes.iter()) { + for (c, t) in &r.comparison.totals { + let slot = all.entry(*c).or_default(); + slot.add(Change::Lost, t.lost); + slot.add(Change::Added, t.added); + slot.add(Change::Changed, t.changed); + } + } + all + } + + pub fn to_markdown(&self) -> String { + let mut s = String::new(); + let opened = self.files.iter().filter(|f| f.opened()).count(); + let routes: Vec<&RouteReport> = self.files.iter().flat_map(|f| f.routes.iter()).collect(); + let save_failed = routes.iter().filter(|r| r.save_error.is_some()).count(); + let reopen_failed = routes.iter().filter(|r| r.reopen_error.is_some()).count(); + let lossless = routes.iter().filter(|r| r.lossless()).count(); + let crashes = self.files.iter().filter(|f| f.open_error.as_deref().is_some_and(|e| e.starts_with("CRASH"))).count() + + routes.iter().filter(|r| [&r.save_error, &r.reopen_error].iter().any(|e| e.as_deref().is_some_and(|e| e.contains("CRASH")))).count(); + s.push_str("# CADCraft round-trip report\n\n"); + s.push_str(&format!("- Corpus: `{}`\n", self.corpus)); + s.push_str(&format!("- Generated: {} by CADCraft {}\n", self.generated, self.cadcraft_version)); + s.push_str(&format!("- Tolerance for numbers: {:e} absolute + {:e} relative\n", self.abs_tol, self.rel_tol)); + s.push_str(&format!( + "- Files: {} ({opened} opened, {} failed to open); round trips: {} ({save_failed} failed to save, {reopen_failed} failed to reopen, {lossless} without any difference); crashes: {crashes}\n", + self.files.len(), + self.files.len() - opened, + routes.len() + )); + let mut unwritten: BTreeMap = BTreeMap::new(); + for r in &routes { + for (t, n) in &r.not_written { + let slot = unwritten.entry(format!("{t} ({})", r.route)).or_insert(0); + *slot = slot.saturating_add(*n); + } + } + if unwritten.is_empty() { + s.push_str("- Lost on save (records a writer can't write): none\n\n"); + } else { + s.push_str(&format!("- **Lost on save** (records a writer can't write): {}\n\n", per_type(&unwritten))); + } + s.push_str( + "Each file is opened, saved in its own format and in the other one, and the saved file is reopened and compared with \ + the drawing first opened. *Read* counts what the file holds that the drawing read from it doesn't (for a DWG: its \ + DXF rendition). Lost/added/changed count items (entities, records, values).\n\n", + ); + s.push_str("## Summary\n\n"); + s.push_str("| File | Format | Size | Open | Read lost | Round trip | Save | Reopen | Not written | Lost | Added | Changed |\n"); + s.push_str("|---|---|---|---|---|---|---|---|---|---|---|---|\n"); + for f in &self.files { + let fmt = f.format.map_or("?".to_string(), |x| format!("{} {}", x.label(), f.version).trim().to_string()); + let open = if f.opened() { format!("ok ({:.1} s)", f.open_seconds) } else { status(&f.open_error) }; + let lead = format!("| `{}` | {} | {} | {} | {} ", cell(&f.path), cell(&fmt), size(f.bytes), open, f.read.tally(Category::Read).lost); + if f.routes.is_empty() { + s.push_str(&format!("{lead}| — | — | — | — | — | — | — |\n")); + } + for (i, r) in f.routes.iter().enumerate() { + let head = if i == 0 { lead.clone() } else { "| | | | | ".to_string() }; + let t = r.comparison.total(); + let reopen = if r.save_error.is_some() { "—".to_string() } else { status(&r.reopen_error) }; + let unwritten = if r.not_written.is_empty() { "0".to_string() } else { format!("**{}**", r.not_written_total()) }; + s.push_str(&format!("{head}| {} | {} | {} | {unwritten} | {} |\n", r.route, status(&r.save_error), reopen, tally_cells(t))); + } + } + s.push_str("\n## By category (all round trips)\n\n| Category | Lost | Added | Changed |\n|---|---|---|---|\n"); + let totals = self.totals(); + for c in Category::ALL { + if let Some(t) = totals.get(&c).filter(|t| t.total() > 0) { + s.push_str(&format!("| {} | {} |\n", c.label(), tally_cells(*t))); + } + } + if totals.values().all(|t| t.total() == 0) { + s.push_str("| (no differences) | 0 | 0 | 0 |\n"); + } + s.push_str("\n## Files\n"); + for f in &self.files { + self.file_markdown(f, &mut s); + } + s + } + + fn file_markdown(&self, f: &FileReport, s: &mut String) { + s.push_str(&format!("\n### `{}`\n\n", f.path)); + let fmt = f.format.map_or("unknown format".to_string(), |x| format!("{} {}", x.label(), f.version).trim().to_string()); + s.push_str(&format!("- {fmt}, {}\n", size(f.bytes))); + match &f.open_error { + Some(e) => { + s.push_str(&format!("- Open: {}: {}\n", status(&f.open_error), cell(e))); + return; + } + None => { + let what = f.drawing_census.as_ref().map_or(String::new(), |c| { + let n: usize = c.entities.values().sum(); + let nb: usize = c.block_entities.values().sum(); + format!( + ": {n} entities in model and paper space, {} blocks holding {nb} entities, {} layers, {} unmodelled objects kept", + c.blocks, + c.tables.get("LAYER").copied().unwrap_or(0), + c.objects.values().sum::() + ) + }); + s.push_str(&format!("- Open: ok in {:.1} s{what}\n", f.open_seconds)); + } + } + for n in &f.open_notes { + s.push_str(&format!("- Note on opening: {}\n", cell(n))); + } + if let Some(c) = &f.file_census + && !c.objects.is_empty() + { + let kept = f.drawing_census.as_ref().map(|d| &d.objects); + let list: Vec = c + .objects + .iter() + .map(|(k, n)| match kept.and_then(|m| m.get(k)) { + Some(m) => format!("{k} {n} ({m} kept as unmodelled)"), + None => format!("{k} {n}"), + }) + .collect(); + s.push_str(&format!("- Objects in the file: {}\n", cell(&list.join(", ")))); + } + if !f.read.is_empty() { + s.push_str("\n#### Read (file → drawing)\n\n"); + diff_table(&f.read, s); + } + for r in &f.routes { + s.push_str(&format!("\n#### {}\n\n", r.route)); + match (&r.save_error, &r.reopen_error) { + (Some(e), _) => s.push_str(&format!("- Save: {}: {}\n", status(&r.save_error), cell(e))), + (None, Some(e)) => { + s.push_str(&format!("- Save: ok ({})\n", size(r.saved_bytes as u64))); + s.push_str(&format!("- Reopen: {}: {}\n", status(&r.reopen_error), cell(e))); + } + (None, None) => { + let t = r.comparison.total(); + s.push_str(&format!( + "- Saved {} and reopened in {:.1} s: {} lost, {} added, {} changed\n", + size(r.saved_bytes as u64), + r.seconds, + t.lost, + t.added, + t.changed + )); + } + } + if !r.not_written.is_empty() { + s.push_str(&format!("- **Lost on save**, not written (the writer can't write these types): {}\n", per_type(&r.not_written))); + } + if let Some(p) = &r.saved_path { + s.push_str(&format!("- Saved file: `{p}`\n")); + } + for n in &r.reopen_notes { + s.push_str(&format!("- Note on reopening: {}\n", cell(n))); + } + if !r.comparison.is_empty() { + s.push('\n'); + diff_table(&r.comparison, s); + } + } + } + + /// Write `report.md` and `report.json` into `dir` (created if needed); returns their paths. + pub fn write(&self, dir: &Path) -> std::io::Result<(PathBuf, PathBuf)> { + std::fs::create_dir_all(dir)?; + let (md, json) = (dir.join("report.md"), dir.join("report.json")); + std::fs::write(&md, self.to_markdown())?; + std::fs::write(&json, self.to_json())?; + Ok((md, json)) + } +} + +/// A comparison's differences as a Markdown table (the first [`MD_ROWS`]). +fn diff_table(c: &Comparison, s: &mut String) { + let tallies: Vec = Category::ALL + .iter() + .filter_map(|cat| { + let t = c.tally(*cat); + (t.total() > 0).then(|| format!("{} {}/{}/{}", cat.label(), t.lost, t.added, t.changed)) + }) + .collect(); + s.push_str(&format!("By category (lost/added/changed): {}\n\n", tallies.join(" · "))); + s.push_str("| Category | Change | Where | Detail |\n|---|---|---|---|\n"); + for d in c.differences.iter().take(MD_ROWS) { + s.push_str(&format!("| {} | {} | {} | {} |\n", d.category.label(), d.change.label(), cell(&clip(&d.place, 80)), cell(&clip(&d.detail, 400)))); + } + let more = c.differences.len().saturating_sub(MD_ROWS).saturating_add(c.omitted); + if more > 0 { + s.push_str(&format!("\n… and {more} more differences (see report.json; differences beyond the listing limit are only counted).\n")); + } +} diff --git a/crates/io/src/roundtrip/tests.rs b/crates/io/src/roundtrip/tests.rs new file mode 100644 index 000000000..57b77a74a --- /dev/null +++ b/crates/io/src/roundtrip/tests.rs @@ -0,0 +1,559 @@ +use std::sync::Arc; + +use cadcraft_color::Color; +use cadcraft_doc::foreign::{ForeignExtra, ForeignObject}; +use cadcraft_doc::*; +use cadcraft_geom::{PolyVertex, Vec2, Vec3}; + +use super::*; + +fn tag(code: i32, value: &str) -> RawTag { + RawTag { code, value: value.into() } +} + +fn line(a: (f64, f64), b: (f64, f64)) -> EntityKind { + EntityKind::Line(Line { a: Vec3::new(a.0, a.1, 0.0), b: Vec3::new(b.0, b.1, 0.0) }) +} + +/// A small drawing touching every part the comparator looks at. +fn drawing() -> Drawing { + let mut d = Drawing::new_metric(); + d.layers.push(Layer { name: "Walls".into(), color: Color::Index(1), ..Layer::default() }); + let walls = Common { layer: "Walls".into(), ..Common::default() }; + d.add(&Space::Model, walls.clone(), line((0.0, 0.0), (10.0, 0.0))).unwrap(); + d.add(&Space::Model, walls, line((0.0, 5.0), (10.0, 5.0))).unwrap(); + d.add(&Space::Model, Common::default(), EntityKind::Circle(Circle { center: Vec3::new(3.0, 4.0, 0.0), radius: 2.5 })).unwrap(); + let tagged = Common { xdata: vec![tag(1001, "ACME"), tag(1000, "door"), tag(1040, "2.5")], ..Common::default() }; + d.add( + &Space::Model, + tagged, + EntityKind::LwPolyline(LwPolyline { + vertices: vec![PolyVertex::new(Vec2::ZERO), PolyVertex::with_bulge(Vec2::new(4.0, 0.0), 0.5), PolyVertex::new(Vec2::new(4.0, 3.0))], + closed: true, + const_width: 0.0, + elevation: 0.0, + plinegen: false, + }), + ) + .unwrap(); + d.add( + &Space::Model, + Common::default(), + EntityKind::Unknown(Unknown { dxf_type: "ACME_WIDGET".into(), tags: vec![tag(100, "AcDbEntity"), tag(10, "1.0"), tag(20, "2.0")] }), + ) + .unwrap(); + d.add(&Space::Paper("Layout1".into()), Common::default(), line((1.0, 1.0), (2.0, 2.0))).unwrap(); + let mut b = Block::new("DOOR"); + let h = d.new_handle(); + b.entities.push(Entity::new(h, line((0.0, 0.0), (0.0, 1.0)))); + d.blocks.insert("DOOR".into(), Arc::new(b)); + let f = Arc::make_mut(&mut d.foreign); + f.objects.push(ForeignObject { handle: Handle(0x500), kind: "XRECORD".into(), tags: vec![tag(100, "AcDbXrecord"), tag(1, "kept")] }); + f.dictionary.push(("ACME_DATA".into(), Handle(0x500))); + f.classes.push(vec![tag(1, "ACME_WIDGET"), tag(2, "AcmeWidget"), tag(3, "Acme")]); + f.records.insert(("LAYER".into(), "WALLS".into()), ForeignExtra { xdata: vec![tag(1001, "ACME"), tag(1070, "3")], ..ForeignExtra::default() }); + d +} + +fn cmp(a: &Drawing, b: &Drawing) -> Comparison { + compare(a, b, &Options::default()) +} + +fn only(c: &Comparison, cat: Category, change: Change) -> Vec<&Difference> { + c.differences.iter().filter(|d| d.category == cat && d.change == change).collect() +} + +fn first_handle(d: &Drawing, kind: &str) -> Handle { + d.model.iter().find(|e| e.kind.dxf_name() == kind).map(|e| e.handle).unwrap() +} + +#[test] +fn identical_drawings_have_no_differences() { + let d = drawing(); + let c = cmp(&d, &d.clone()); + assert!(c.is_empty(), "{:#?}", c.differences); + assert_eq!(c.total(), Tally::default()); +} + +#[test] +fn geometry_is_compared_by_handle_within_the_tolerance() { + let a = drawing(); + let h = first_handle(&a, "LINE"); + let mut b = a.clone(); + b.modify_entity(h, |e| { + if let EntityKind::Line(l) = &mut e.kind { + l.b.x += 1e-9; + } + }) + .unwrap(); + assert!(cmp(&a, &b).is_empty(), "a change within the tolerance is no difference"); + b.modify_entity(h, |e| { + if let EntityKind::Line(l) = &mut e.kind { + l.b.x = 11.0; + } + }) + .unwrap(); + let c = cmp(&a, &b); + let g = only(&c, Category::Geometry, Change::Changed); + assert_eq!(g.len(), 1, "{:#?}", c.differences); + assert!(g[0].place.contains(&h.hex()) && g[0].place.starts_with("Model LINE"), "{:?}", g[0]); + assert!(g[0].detail.contains("b.x: 10") && g[0].detail.contains("11"), "{:?}", g[0]); + assert_eq!(c.tally(Category::Geometry).changed, 1); +} + +#[test] +fn angles_a_whole_turn_apart_are_the_same() { + let mut a = drawing(); + let h = a + .add(&Space::Model, Common::default(), EntityKind::Arc(cadcraft_doc::Arc { center: Vec3::ZERO, radius: 2.0, start: -0.5, end: 1.0 })) + .unwrap(); + let mut b = a.clone(); + b.modify_entity(h, |e| { + if let EntityKind::Arc(x) = &mut e.kind { + x.start += std::f64::consts::TAU; + x.end -= std::f64::consts::TAU; + } + }) + .unwrap(); + assert!(cmp(&a, &b).is_empty(), "{:#?}", cmp(&a, &b).differences); + // A radius is no angle. + b.modify_entity(h, |e| { + if let EntityKind::Arc(x) = &mut e.kind { + x.radius += std::f64::consts::TAU; + } + }) + .unwrap(); + assert_eq!(cmp(&a, &b).tally(Category::Geometry).changed, 1); +} + +#[test] +fn properties_and_type_changes_are_reported() { + let a = drawing(); + let h = first_handle(&a, "CIRCLE"); + let mut b = a.clone(); + b.modify_entity(h, |e| e.common.color = Color::Index(3)).unwrap(); + let c = cmp(&a, &b); + assert_eq!(only(&c, Category::Properties, Change::Changed).len(), 1, "{:#?}", c.differences); + b.modify_entity(h, |e| e.kind = line((0.0, 0.0), (1.0, 1.0))).unwrap(); + let c = cmp(&a, &b); + let g = only(&c, Category::Geometry, Change::Changed); + assert!(g.iter().any(|d| d.detail == "type CIRCLE → LINE"), "{:#?}", c.differences); +} + +#[test] +fn lost_and_added_entities_are_counted_per_type_and_space() { + let a = drawing(); + let h = first_handle(&a, "CIRCLE"); + let mut b = a.clone(); + b.remove_entity(h); + let mut paper = b.clone(); + paper.add(&Space::Paper("Layout1".into()), Common::default(), EntityKind::Point(Point { p: Vec3::ZERO, angle: 0.0 })).unwrap(); + let c = cmp(&a, &paper); + let lost = only(&c, Category::Count, Change::Lost); + assert_eq!(lost.len(), 1, "{:#?}", c.differences); + assert_eq!(lost[0].place, "Model CIRCLE"); + assert!(lost[0].detail.contains("1 → 0") && lost[0].detail.contains(&h.hex()), "{:?}", lost[0]); + let added = only(&c, Category::Count, Change::Added); + assert_eq!(added.len(), 1); + assert_eq!(added[0].place, "Layout Layout1 POINT"); + assert_eq!(c.tally(Category::Count), Tally { lost: 1, added: 1, changed: 0 }); +} + +#[test] +fn renumbered_entities_are_matched_by_content() { + let a = drawing(); + let mut b = a.clone(); + // Every line gets a new handle, in reverse order, and one of them moves. + let lines: Vec> = b.model.iter().filter(|e| e.kind.dxf_name() == "LINE").cloned().collect(); + for (i, e) in lines.iter().rev().enumerate() { + b.model.remove(e.handle); + let mut e = Entity::clone(e); + e.handle = Handle(0x9000 + i as u64); + b.model.push(e); + } + let c = cmp(&a, &b); + assert_eq!(c.tally(Category::Count), Tally::default(), "{:#?}", c.differences); + assert_eq!(c.tally(Category::Geometry), Tally::default(), "pairs by content, not by order: {:#?}", c.differences); + assert_eq!(c.tally(Category::Handle).changed, 2); +} + +#[test] +fn entities_moving_between_spaces_are_reported() { + let a = drawing(); + let h = first_handle(&a, "CIRCLE"); + let mut b = a.clone(); + let e = b.remove_entity(h).unwrap(); + if let Some(l) = b.layouts.first_mut() { + l.entities.push(Entity::clone(&e)); + } + let c = cmp(&a, &b); + let moved = only(&c, Category::Handle, Change::Changed); + assert_eq!(moved.len(), 1, "{:#?}", c.differences); + assert_eq!(moved[0].detail, format!("1 entities moved from Model to Layout Layout1 ({})", h.hex())); + assert_eq!(c.tally(Category::Count), Tally::default()); +} + +#[test] +fn unreferenced_anonymous_blocks_dropped_on_save_are_one_row() { + let mut a = drawing(); + for name in ["*D5", "*D6"] { + let mut blk = Block::new(name); + for _ in 0..2 { + let h = a.new_handle(); + blk.entities.push(Entity::new(h, line((0.0, 0.0), (1.0, 0.0)))); + } + a.blocks.insert(name.into(), Arc::new(blk)); + } + let mut b = a.clone(); + b.blocks.remove("*D5"); + b.blocks.remove("*D6"); + let c = cmp(&a, &b); + let lost = only(&c, Category::Count, Change::Lost); + assert_eq!(lost.len(), 1, "{:#?}", c.differences); + assert_eq!(lost[0].detail, "4 entities of 2 anonymous blocks nothing refers to (e.g. *D5) were not saved"); + assert_eq!(c.tally(Category::Count).lost, 4); + assert_eq!(c.tally(Category::Block), Tally { lost: 2, added: 0, changed: 0 }, "{:#?}", c.differences); + assert_eq!(only(&c, Category::Block, Change::Lost).len(), 1); +} + +#[test] +fn renamed_anonymous_blocks_are_matched_by_their_entities() { + let mut a = drawing(); + let mut blk = Block::new("*D1"); + let h = a.new_handle(); + blk.entities.push(Entity::new(h, line((0.0, 0.0), (5.0, 0.0)))); + a.blocks.insert("*D1".into(), Arc::new(blk.clone())); + let key = ("BLOCK_RECORD".to_string(), "*D1".to_string()); + Arc::make_mut(&mut a.foreign).records.insert(key, ForeignExtra { xdata: vec![tag(1001, "ACME"), tag(1070, "1")], ..ForeignExtra::default() }); + let mut b = a.clone(); + b.blocks.remove("*D1"); + blk.name = "*D7".into(); + b.blocks.insert("*D7".into(), Arc::new(blk)); + let f = Arc::make_mut(&mut b.foreign); + let extra = f.records.remove(&("BLOCK_RECORD".to_string(), "*D1".to_string())).unwrap(); + f.records.insert(("BLOCK_RECORD".into(), "*D7".into()), extra); + let c = cmp(&a, &b); + assert_eq!(c.tally(Category::Count), Tally::default(), "{:#?}", c.differences); + assert_eq!(c.tally(Category::Xdata), Tally::default(), "{:#?}", c.differences); + assert_eq!(c.tally(Category::Block), Tally { lost: 0, added: 0, changed: 1 }, "{:#?}", c.differences); + assert!(c.differences.iter().any(|d| d.detail == "1 anonymous blocks saved under new names (e.g. *D1 → *D7)"), "{:#?}", c.differences); +} + +#[test] +fn xdata_extension_dictionaries_and_opaque_entities_are_compared() { + let a = drawing(); + let pl = first_handle(&a, "LWPOLYLINE"); + let unknown = first_handle(&a, "ACME_WIDGET"); + let mut b = a.clone(); + b.modify_entity(pl, |e| e.common.xdata.clear()).unwrap(); + b.modify_entity(unknown, |e| { + if let EntityKind::Unknown(u) = &mut e.kind { + u.tags.pop(); + } + }) + .unwrap(); + Arc::make_mut(&mut b.foreign).records.clear(); + let mut a2 = a.clone(); + Arc::make_mut(&mut a2.foreign) + .entities + .insert(pl, ForeignExtra { xdict: Some(Handle(0x600)), reactors: vec![Handle(0x601)], ..ForeignExtra::default() }); + let c = cmp(&a2, &b); + let x = only(&c, Category::Xdata, Change::Lost); + assert_eq!(x.len(), 2, "the entity's and the layer's: {:#?}", c.differences); + assert!(x.iter().any(|d| d.place == "LAYER WALLS" && d.detail.starts_with("application ACME")), "{x:#?}"); + assert!(x.iter().any(|d| d.place.contains("LWPOLYLINE")), "{x:#?}"); + assert_eq!(c.tally(Category::ExtDict).lost, 2, "the dictionary and a reactor: {:#?}", c.differences); + let o = only(&c, Category::Opaque, Change::Changed); + assert_eq!(o.len(), 1, "{:#?}", c.differences); + assert!(o[0].detail.contains("3 groups → 2"), "{:?}", o[0]); + // Numbers in kept groups compare within the tolerance. + let mut b2 = a.clone(); + b2.modify_entity(unknown, |e| { + if let EntityKind::Unknown(u) = &mut e.kind { + u.tags[1].value = "1.0000000000001".into(); + } + }) + .unwrap(); + assert!(cmp(&a, &b2).is_empty()); +} + +#[test] +fn tables_blocks_and_layouts_are_compared_by_name() { + let a = drawing(); + let mut b = a.clone(); + b.layers.retain(|l| l.name != "Walls"); + b.linetypes.push(Linetype { name: "Dashed".into(), description: String::new(), pattern: Vec::new() }); + if let Some(s) = b.text_styles.first_mut() { + s.height = 7.0; + } + b.blocks.remove("DOOR"); + b.layouts.retain(|l| l.name != "Layout2"); + let c = cmp(&a, &b); + let t = |cat, ch, place: &str| c.differences.iter().any(|d| d.category == cat && d.change == ch && d.place == place); + assert!(t(Category::Table, Change::Lost, "LAYER Walls"), "{:#?}", c.differences); + assert!(t(Category::Table, Change::Added, "LTYPE Dashed")); + assert!(t(Category::Table, Change::Changed, "STYLE Standard")); + assert!(t(Category::Block, Change::Lost, "BLOCK DOOR")); + assert!(t(Category::Layout, Change::Lost, "LAYOUT Layout2")); + // The block's entity is lost with it, counted where it was. + assert!(t(Category::Count, Change::Lost, "Block DOOR LINE"), "{:#?}", c.differences); + // Names match without case. + let mut upper = a.clone(); + if let Some(l) = upper.layers.iter_mut().find(|l| l.name == "Walls") { + l.name = "WALLS".into(); + } + let c = cmp(&a, &upper); + assert_eq!(c.tally(Category::Table).lost, 0, "{:#?}", c.differences); +} + +#[test] +fn unmodelled_objects_dictionary_entries_and_classes_are_compared() { + let a = drawing(); + let mut b = a.clone(); + let f = Arc::make_mut(&mut b.foreign); + f.objects.clear(); + f.dictionary.clear(); + f.classes.clear(); + let c = cmp(&a, &b); + assert_eq!(c.tally(Category::Object).lost, 1, "{:#?}", c.differences); + assert_eq!(c.tally(Category::Dictionary).lost, 1); + assert_eq!(c.tally(Category::Class).lost, 1); + // A renumbered object is matched by its groups. + let mut b = a.clone(); + Arc::make_mut(&mut b.foreign).objects[0].handle = Handle(0x700); + let c = cmp(&a, &b); + assert_eq!(c.tally(Category::Object), Tally::default(), "{:#?}", c.differences); + assert_eq!(c.tally(Category::Handle).changed, 1); +} + +#[test] +fn header_variables_are_compared_without_volatile_ones() { + let a = drawing(); + let mut b = a.clone(); + b.header.set_f64("LTSCALE", 2.0); + b.header.vars.remove("TEXTSIZE"); + b.header.set_str("HANDSEED", "FFFF"); + b.header.set_str("TDUPDATE", "2461000.5"); + // 1 and 1.0 are the same value. + b.header.set_i64("DIMSCALE", 1); + let c = cmp(&a, &b); + assert!(only(&c, Category::Header, Change::Changed).iter().any(|d| d.place == "$LTSCALE"), "{:#?}", c.differences); + assert!(only(&c, Category::Header, Change::Lost).iter().any(|d| d.place == "$TEXTSIZE")); + assert_eq!(c.tally(Category::Header).total(), 2, "{:#?}", c.differences); +} + +#[test] +fn the_listing_limit_keeps_counting() { + let a = drawing(); + let mut b = a.clone(); + for i in 0..20 { + b.header.set_i64(&format!("USERI{i}"), i); + } + let c = compare(&a, &b, &Options { max_differences: 5, ..Options::default() }); + assert_eq!(c.differences.len(), 5); + assert_eq!(c.omitted, 15); + assert_eq!(c.tally(Category::Header).added, 20); +} + +#[test] +fn census_counts_what_a_dxf_holds() { + let dxf = " 0\nSECTION\n 2\nTABLES\n 0\nTABLE\n 2\nLAYER\n 0\nLAYER\n 2\n0\n 0\nLAYER\n 2\nA\n 0\nENDTAB\n 0\nENDSEC\n\ + 0\nSECTION\n 2\nBLOCKS\n 0\nBLOCK\n 2\n*Model_Space\n 0\nENDBLK\n 0\nBLOCK\n 2\n*Paper_Space\n 0\nLINE\n 0\nENDBLK\n\ + 0\nBLOCK\n 2\nB1\n 0\nCIRCLE\n 0\nPOLYLINE\n 0\nVERTEX\n 0\nSEQEND\n 0\nENDBLK\n 0\nENDSEC\n\ + 0\nSECTION\n 2\nENTITIES\n 0\nLINE\n 0\nINSERT\n 0\nATTRIB\n 0\nSEQEND\n 0\nENDSEC\n\ + 0\nSECTION\n 2\nOBJECTS\n 0\nDICTIONARY\n 0\nXRECORD\n 0\nXRECORD\n 0\nENDSEC\n 0\nEOF\n"; + let c = census(dxf.as_bytes()).unwrap(); + assert_eq!(c.entities.get("LINE"), Some(&2)); + assert_eq!(c.entities.get("INSERT"), Some(&1)); + assert!(!c.entities.contains_key("ATTRIB") && !c.entities.contains_key("SEQEND")); + assert_eq!(c.block_entities.get("CIRCLE"), Some(&1)); + assert_eq!(c.block_entities.get("POLYLINE"), Some(&1)); + assert_eq!(c.blocks, 1); + assert_eq!(c.tables.get("LAYER"), Some(&2)); + assert_eq!(c.objects.get("XRECORD"), Some(&2)); + let _ = census(b""); +} + +#[test] +fn read_census_reports_entities_the_reader_left_out() { + let d = drawing(); + let mut file = census_of(&d); + *file.entities.entry("LINE".into()).or_default() += 2; + *file.entities.entry("WEIRD".into()).or_default() += 1; + let c = compare_census(&file, &d, &Options::default()); + let lost = only(&c, Category::Read, Change::Lost); + assert_eq!(c.tally(Category::Read).lost, 3, "{:#?}", c.differences); + assert!(lost.iter().any(|x| x.place == "spaces LINE" && x.detail == "LINE: 5 in the file, 3 read"), "{lost:#?}"); + assert!(compare_census(&census_of(&d), &d, &Options::default()).is_empty()); +} + +/// The round trips of a synthetic drawing through CADCraft's own DXF and DWG writers: whatever +/// they lose shows in the comparison. Plain geometry, layers, xdata and unmodelled objects must +/// come back from DXF. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn dxf_round_trip_of_plain_geometry_is_lossless() { + use corpus::{Format, round_trip}; + let mut d = drawing(); + // What the comparator should see in a round trip: the drawing as read from its own DXF. + d = crate::read(crate::write(&d, "a.dxf").unwrap().as_slice(), "a.dxf").unwrap(); + let r = round_trip(&d, Format::Dxf, Format::Dxf, &Options::default(), None); + assert!(r.ok(), "{r:#?}"); + let core = [Category::Count, Category::Geometry, Category::Table, Category::Xdata, Category::Object, Category::Opaque]; + for cat in core { + assert_eq!(r.comparison.tally(cat), Tally::default(), "{cat:?}: {:#?}", r.comparison.differences); + } + let dwg = round_trip(&d, Format::Dxf, Format::Dwg, &Options::default(), None); + assert!(dwg.save_error.is_none() && dwg.reopen_error.is_none(), "{dwg:#?}"); + assert!(dwg.saved_bytes > 0); + // Through DWG, only the custom-class entity is lost (gap FF1: acadrust drops other programs' + // entities without DWG data of their own) — and the comparator says so. + let lost: Vec<&str> = dwg.comparison.differences.iter().filter(|d| d.category == Category::Count).map(|d| d.place.as_str()).collect(); + assert_eq!(lost, ["Model ACME_WIDGET"], "{:#?}", dwg.comparison.differences); + assert_eq!(dwg.comparison.tally(Category::Geometry), Tally::default(), "{:#?}", dwg.comparison.differences); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn hostile_files_are_reported_not_crashed() { + use corpus::{CorpusOptions, check_bytes}; + let opts = CorpusOptions::default(); + for (bytes, name) in [ + (&b""[..], "empty.dxf"), + (b"AC1032 not really a dwg file at all", "fake.dwg"), + (b" 0\nSECTION\n 2\nENTITIES\n 0\nLINE\n 10\nnan\n 20\n1e400\n 0\nENDSEC\n 0\nEOF\n", "odd.dxf"), + (&[0xFFu8; 64][..], "noise.dxf"), + ] { + let f = check_bytes(bytes, name, &opts); + assert_eq!(f.path, name); + if f.opened() { + assert!(!f.routes.is_empty()); + } else { + assert!(f.routes.is_empty() && f.open_error.as_deref().is_some_and(|e| !e.is_empty()), "{f:#?}"); + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn report_renders_as_markdown_and_json() { + use corpus::{CorpusOptions, CorpusReport, check_bytes}; + let d = drawing(); + let dxf = crate::write(&d, "a.dxf").unwrap(); + let opts = CorpusOptions::default(); + let report = CorpusReport { + corpus: "corpus".into(), + generated: "2026-10-21 09:30 UTC".into(), + cadcraft_version: "0".into(), + abs_tol: 1e-6, + rel_tol: 1e-9, + files: vec![check_bytes(&dxf, "synthetic/a.dxf", &opts), check_bytes(b"AC1032 broken", "dwg/broken.dwg", &opts)], + }; + let md = report.to_markdown(); + assert!(md.starts_with("# CADCraft round-trip report"), "{md}"); + assert!(md.contains("| `synthetic/a.dxf` | DXF AC1032 |"), "{md}"); + assert!(md.contains("dxf → dxf") && md.contains("dxf → dwg"), "{md}"); + assert!(md.contains("| `dwg/broken.dwg` | DWG AC1032 |") && md.contains("**failed**"), "{md}"); + assert!(md.contains("## By category")); + let json: serde_json::Value = serde_json::from_str(&report.to_json()).unwrap(); + assert_eq!(json["files"][0]["path"], "synthetic/a.dxf"); + assert_eq!(json["files"][0]["routes"][0]["route"], "dxf → dxf"); + assert!(json["files"][1]["openError"].as_str().is_some_and(|e| !e.is_empty()), "{json}"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn corpus_files_walks_folders_and_skips_the_report() { + use corpus::corpus_files; + let dir = std::env::temp_dir().join(format!("cadcraft-corpus-walk-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + for p in ["dxf/a.DXF", "dwg/b.dwg", "dwg/sub/c.dwg", "notes.txt", "report/a.roundtrip.dxf", ".git/x.dxf"] { + let path = dir.join(p); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(&path, b"x").unwrap(); + } + let files = corpus_files(&dir, &[dir.join("report")]); + let rel: Vec = files.iter().map(|f| corpus::relative(f, &dir)).collect(); + assert_eq!(rel, ["dwg/b.dwg", "dwg/sub/c.dwg", "dxf/a.DXF"]); + let _ = std::fs::remove_dir_all(&dir); +} + +/// The real-file corpus: set `CADCRAFT_CORPUS_DIR` to a folder of DXF/DWG files (a checkout of +/// `storytold/cadcraft-corpus`) to open, save and reopen each one and write `report.md` and +/// `report.json` to `CADCRAFT_CORPUS_REPORT` (default: the corpus folder's `report`). Skipped +/// when the variable is unset, so CI passes without the corpus. It reports, it doesn't fail on +/// differences; set `CADCRAFT_CORPUS_STRICT=1` to fail when a file doesn't open, save or reopen, +/// or when a writer leaves records out of a saved file ([`corpus::CorpusReport::strict_failures`]). +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn corpus_round_trip_report() { + let Some(dir) = std::env::var_os("CADCRAFT_CORPUS_DIR").map(std::path::PathBuf::from) else { + eprintln!("CADCRAFT_CORPUS_DIR is not set: corpus round trip skipped"); + return; + }; + let out = std::env::var_os("CADCRAFT_CORPUS_REPORT").map_or_else(|| dir.join("report"), std::path::PathBuf::from); + let files = corpus::corpus_files(&dir, std::slice::from_ref(&out)); + let report = corpus::run(&dir, &files, &corpus::CorpusOptions::default(), &mut |i, n, rel| eprintln!("[{}/{n}] {rel}", i + 1)); + let (md, json) = report.write(&out).unwrap(); + eprintln!("corpus report: {} and {}", md.display(), json.display()); + if std::env::var("CADCRAFT_CORPUS_STRICT").is_ok_and(|v| v == "1") { + let failed = report.strict_failures(); + assert!(failed.is_empty(), "corpus failures:\n{}", failed.join("\n")); + } +} + +/// A type the DWG writer can't write is reported as lost on save, per type: in the route's +/// `not_written`, on the lost entity rows, in the Markdown and as a strict failure. A type it +/// writes (OLE2FRAME, kept by CADCraft without modelling it) is none of these. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn types_the_dwg_writer_cannot_write_are_reported_lost() { + use corpus::{CorpusReport, FileReport, Format, round_trip}; + let unknown = |t: &str, tags: Vec| EntityKind::Unknown(Unknown { dxf_type: t.into(), tags }); + let mut d = Drawing::new_metric(); + let widget = vec![tag(100, "AcDbEntity"), tag(100, "AcmeWidget"), tag(10, "1.0"), tag(20, "2.0"), tag(30, "0.0")]; + d.add(&Space::Model, Common::default(), unknown("ACME_WIDGET", widget.clone())).unwrap(); + d.add(&Space::Model, Common::default(), unknown("ACME_WIDGET", widget)).unwrap(); + // Its data has no DWG wrapper, which is where a DWG keeps the corners: the reader's defaults. + let ole = ["100", "AcDbEntity", "8", "0", "100", "AcDbOle2Frame", "70", "2", "10", "1.0", "20", "1.0", "30", "0.0", "11", "0.0", "21", "0.0"] + .chunks(2) + .map(|g| tag(g[0].parse().unwrap(), g[1])) + .chain( + ["31", "0.0", "71", "2", "72", "0", "73", "0", "90", "4", "310", "C0FFEE00", "1", "OLE"] + .chunks(2) + .map(|g| tag(g[0].parse().unwrap(), g[1])), + ) + .collect(); + d.add(&Space::Model, Common::default(), unknown("OLE2FRAME", ole)).unwrap(); + let d = crate::read(&crate::write(&d, "a.dxf").unwrap(), "a.dxf").unwrap(); + + let r = round_trip(&d, Format::Dxf, Format::Dwg, &Options::default(), None); + assert!(r.ok(), "{r:#?}"); + assert_eq!(r.not_written, [("ACME_WIDGET".to_string(), 2)].into_iter().collect()); + let lost: Vec<&Difference> = r.comparison.differences.iter().filter(|x| x.category == Category::Count && x.change == Change::Lost).collect(); + assert_eq!(lost.len(), 1, "{lost:#?}"); + assert!(lost[0].place.ends_with("ACME_WIDGET") && lost[0].detail.contains("not written: the DWG writer can't write ACME_WIDGET"), "{lost:#?}"); + // The OLE2FRAME is written (only the CLASS record AutoCAD's files have is added). + assert!( + !r.comparison.differences.iter().any(|x| x.category != Category::Class && x.place.contains("OLE2FRAME")), + "{:#?}", + r.comparison.differences + ); + assert!(!r.lossless()); + + let report = + CorpusReport { files: vec![FileReport { path: "a.dxf".into(), routes: vec![r], ..FileReport::default() }], ..CorpusReport::default() }; + assert_eq!(report.strict_failures(), ["a.dxf (dxf → dwg): lost on save, not written: 2 ACME_WIDGET"]); + let md = report.to_markdown(); + assert!(md.contains("- **Lost on save** (records a writer can't write): 2 ACME_WIDGET (dxf → dwg)"), "{md}"); + assert!(md.contains("| dxf → dwg | ok | ok | **2** |"), "{md}"); + assert!(md.contains("- **Lost on save**, not written (the writer can't write these types): 2 ACME_WIDGET"), "{md}"); + + // DXF keeps everything: nothing lost on save, nothing to fail on. + let r = round_trip(&d, Format::Dxf, Format::Dxf, &Options::default(), None); + assert!(r.not_written.is_empty()); + let report = + CorpusReport { files: vec![FileReport { path: "a.dxf".into(), routes: vec![r], ..FileReport::default() }], ..CorpusReport::default() }; + assert!(report.strict_failures().is_empty()); + assert!(report.to_markdown().contains("- Lost on save (records a writer can't write): none")); +} diff --git a/docs/gaps.md b/docs/gaps.md index a4a12cff8..7ab8202fa 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where CADCraft falls short of AutoCAD -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-25 · **Change:** minor (DIMTFILL limits in the `*D` block, EXPLODE and DXF: F7 updated) · **Target:** Autodesk AutoCAD 2027 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-27 · **Change:** minor (open → save → reopen round-trip harness with loss report: FF5 narrowed) · **Target:** Autodesk AutoCAD 2027 Every known shortfall, one row each, ranked within each section by user impact. This is the work list: agents pick from here (top of a section first), and remove or update a row in the same PR @@ -14,7 +14,7 @@ do them first. | # | Gap | Why it ranks here | Est. | Doc | |---|---|---|---|---| | 1 | **[alpha blocker]** **Saving DWG drops what we don't model** (DXF now writes back unknown entities and objects, foreign xdata, extension dictionaries, reactors, layer states and colour books, #528, and native MULTILEADERs, #459 (not yet tested through DWG); through DWG, acadrust drops custom-class entities and objects, xdata on table records and blocks, entity colour books and reactors) | Opening a colleague's DWG and saving it still deletes some content: the one thing a CAD user can't forgive | 6–10 | [file formats](file-format-parity.md) | -| 2 | **[alpha blocker]** **No real-file test corpus and no check in AutoCAD** | All 36 file tests are synthetic; nobody has verified a CADCraft DWG/DXF opens cleanly in AutoCAD | 10–15 + human | [file formats](file-format-parity.md#tests) | +| 2 | **[alpha blocker]** **No real-file test corpus and no check in AutoCAD** | The open → save → reopen harness exists (FF5), but no real files run through it yet and all file tests are synthetic; nobody has verified a CADCraft DWG/DXF opens cleanly in AutoCAD | 8–12 + human | [file formats](file-format-parity.md#tests) | | 3 | **Ellipse and spline geometry is tessellated** (intersections, offsets, closest points; polyline offset ignores bulges) | Coordinates are subtly wrong; offsets of ellipses come out as polylines | 30–50 | [geometry](geometry-parity.md) | | 4 | **[alpha blocker]** **No dynamic blocks or REFEDIT; xrefs can't be edited in place or clipped** (xrefs, #525, groups, #417, and the Block Editor for static blocks landed) | Most production drawings use them | 55–90 | [features](#features) | | 5 | **No autosave in the web build** (the desktop app has SAVETIME autosave, Drawing Recovery, AUDIT and RECOVER since #406) | Browser users lose unsaved work when the tab or the browser goes down | 3–5 | [features](#features) | @@ -77,7 +77,7 @@ Detail: [file-format-parity.md](file-format-parity.md). | FF2 | Multileader leftovers (native MULTILEADER read/write landed, #459): several landings per multileader (extra leader roots are folded into the lines), per-object overrides (leader colour and linetype, arrowhead symbol, text frame), annotative scale contexts; check the text placement of our MULTILEADER output in AutoCAD | Medium | 3–5 | | FF3 | Save-version leftovers (DXF/DWG 2000–2018, binary DXF, SAVEAS `version`/`binary` and the default save version landed, #421): no R14 DWG or R12 DXF output; the Save dialog can't pick the version per file (the file picker doesn't report the chosen type; File > Default Save Format sets it); no command-line SAVEAS format prompt | Low | 2–4 | | FF4 | Code-page leftovers (pre-2007 DXF read in their `$DWGCODEPAGE` landed, #505): Johab (ANSI_1361) and DOS 855/857/860–865/869 read as ANSI_1252; saving declares `ANSI_1252` instead of the file's code page (the text is pure ASCII, so still correct); acadrust approximates DOS code pages in pre-2007 DWG (DOS437 as 866, DOS850 as 1252) | Low | 1–2 | -| FF5 | **[alpha blocker]** Real-file corpus (`storytold/cadcraft-corpus`), open → save → reopen tests, black-box check in AutoCAD | High | 10–15 + human | +| FF5 | **[alpha blocker]** Real-file corpus (`storytold/cadcraft-corpus`) and black-box check in AutoCAD. The open → save → reopen harness landed 2026-10-22 (`cadcraft_io::roundtrip`, `cadcraft-cli roundtrip`, `cargo xtask corpus`: entities by handle, tables, objects, xdata, classes and header compared, Markdown and JSON report, with the records the DWG writer leaves out). Left: the corpus itself (no real files run in CI yet); nobody has opened CADCraft's output in AutoCAD | High | 8–12 + human | | FF6 | **[alpha blocker]** DWG throughput: a 10 MB DWG took ~2 min to fail (#57) | High for real projects | 6–10 | | FF7 | Table record and object handles from other programs' files are renumbered on the first save (entity handles are kept; `*D` blocks no longer duplicate and once reopened, CADCraft's own files keep every handle across saves, #495) | Low | 4–6 | | FF8 | SHX fonts (shape and big fonts) not read | Medium-high (text metrics differ) | 8–12 | @@ -150,6 +150,7 @@ Detail: [localization-parity.md](localization-parity.md). | Date | Change | Summary | |---|---|---| +| 2026-10-27 | minor | Round-trip harness: `cadcraft_io::roundtrip` compares a drawing with itself saved and reopened (entities by handle, tables, blocks, layouts, unmodelled objects, xdata, classes, header variables; numbers within a tolerance), `roundtrip::corpus` runs a folder of DXF/DWG files through DXF and DWG into `report.md`/`report.json`, exposed as `cadcraft-cli roundtrip` and `cargo xtask corpus`; the harness reports per type the records the DWG writer leaves out (`cadcraft_dwg::dxf_to_dwg_noted`, `cadcraft_io::write_noted`), and OLE2FRAME, which it writes, is no longer listed as not saved: FF5 narrowed | | 2026-10-25 | minor | DIMTFILL limits: with DIMTFILL 1 (the drawing's background color) the written `*D` block and EXPLODE keep no background mask (a WIPEOUT over the text box would), and a true-color DIMTFILLCLR is written to DXF as the nearest ACI: F7 updated | | 2026-10-24 | minor | COMPAREEXPORT (typed, or Export on the comparison bar) opens the comparison as a new untitled drawing, `compare_ vs .dwg`: this drawing's objects in one group and the other drawing's differing objects in a second group, in the comparison colors (green, red, gray for objects in both), block contents and style colors included: F22 narrowed | | 2026-10-24 | minor | DIMTFILL and DIMTFILLCLR: a dimension's text background (none, the drawing's background color, or a color) hides what is drawn under the text; SETVAR, DIMOVERRIDE and style fields, DIMSTYLE groups 69 and 70 in DXF from R2007 (CADCraft xdata before), and a SOLID in the `*D` block and from EXPLODE for DIMTFILL 2: F7 narrowed | diff --git a/xtask/src/main.rs b/xtask/src/main.rs index d5ef7dcab..8d8d78631 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -23,8 +23,10 @@ commands: test cargo nextest run (cargo test without nextest) plus the doctests i18n print the strings each interface language shows in English (never fails) ci fmt --check, clippy -D warnings, test, i18n, assets, layers, wasm (stops at first failure) - corpus [--download] - show where test corpora live; --download fetches PngSuite into corpus/pngsuite + corpus [DIR] [--out DIR] [--keep-saved] [--same-format] + open -> save -> reopen every DXF/DWG of a real-file corpus (DIR, default + $CADCRAFT_CORPUS_DIR or ../cadcraft-corpus) and write report.md + report.json + (default DIR/report); runs `cadcraft-cli roundtrip` in release mode stats [--exact] count tests and lines per crate (--exact: ask the test harness via `-- --list`) parity recompute the AutoCAD menu-breadth checklist in docs/parity-checklist.md ico ... @@ -41,7 +43,7 @@ fn main() -> ExitCode { Some("test") => cmd_test(), Some("i18n") => cmd_i18n(), Some("ci") => cmd_ci(), - Some("corpus") => cmd_corpus(rest.contains(&"--download")), + Some("corpus") => cmd_corpus(&rest), Some("stats") => stats::run(&root(), rest.contains(&"--exact")), Some("parity") => parity::run(&root()), Some("ico") => ico::run(&rest), @@ -226,7 +228,43 @@ fn cmd_ci() -> Result<(), String> { Ok(()) } -fn cmd_corpus(_download: bool) -> Result<(), String> { - println!("Test corpora live in storytold/cadcraft-corpus (planned); never commit large binaries here."); - Ok(()) +/// `corpus [DIR] [options]`: the round-trip report over a real-file corpus (gaps FF1, FF5). The +/// corpus lives outside this repo (storytold/cadcraft-corpus); its files are never committed here. +fn cmd_corpus(args: &[&str]) -> Result<(), String> { + // `cadcraft-cli` runs from the repository root: relative paths are made absolute against the + // folder `cargo xtask` was run from first, so they mean what the user typed. + let here = std::env::current_dir().map_err(|e| format!("current folder: {e}"))?; + let mut pass: Vec = Vec::new(); + let mut dir: Option = None; + let mut it = args.iter(); + while let Some(a) = it.next() { + match *a { + "--out" => { + let v = it.next().ok_or_else(|| format!("{a} needs a value"))?; + pass.extend([a.to_string(), here.join(v).display().to_string()]); + } + "--tolerance" => { + let v = it.next().ok_or_else(|| format!("{a} needs a value"))?; + pass.extend([a.to_string(), v.to_string()]); + } + s if s.starts_with("--") => pass.push(s.to_string()), + s if dir.is_none() => dir = Some(s.to_string()), + _ => return Err("corpus takes one DIR".into()), + } + } + let dir = dir + .or_else(|| std::env::var("CADCRAFT_CORPUS_DIR").ok()) + .unwrap_or_else(|| root().parent().map(|p| p.join("cadcraft-corpus").display().to_string()).unwrap_or_else(|| "../cadcraft-corpus".into())); + let dir = here.join(&dir); + if !dir.is_dir() { + return Err(format!( + "no corpus at `{}`: clone storytold/cadcraft-corpus next to this repo, set CADCRAFT_CORPUS_DIR or pass DIR \ + (never commit corpus files to this repo)", + dir.display() + )); + } + let dir = dir.display().to_string(); + let mut c = cargo(); + c.args(["run", "--release", "-q", "-p", "cadcraft-cli", "--", "roundtrip", &dir]).args(&pass); + run(c, &format!("cadcraft-cli roundtrip {dir} {}", pass.join(" "))) } From 4398a1aab12d10c29a7088963106ab7963849c60 Mon Sep 17 00:00:00 2001 From: Andrea Brandi Date: Tue, 27 Oct 2026 18:47:15 +0100 Subject: [PATCH 006/100] fix(it): correct CADCraft chamfer terminology --- ATTRIBUTION.md | 2 +- contributors/people.toml | 2 ++ crates/ui-egui/src/i18n/it.tsv | 8 ++++---- docs/localization-parity.md | 5 +++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index e124d102a..73ea1c0e0 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -70,7 +70,7 @@ Generated-in-code assets are original and have no file to list: | `crates/ui-egui/src/i18n/fr.tsv` | @pechnikov2026-star | Original French translations of CADCraft English strings | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | No proprietary localization resources copied | | `crates/ui-egui/src/i18n/zh.tsv` | @mayloglog | Original Simplified Chinese translations of CADCraft English strings | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | No proprietary localization resources copied | | `crates/ui-egui/src/i18n/sv.tsv` | Erik Holmbom (@holmbom) with Claude Code (AI-assisted) | Original Swedish translations of CADCraft English strings | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | No proprietary localization resources copied; terminology and the running app reviewed by a native speaker | -| `crates/ui-egui/src/i18n/it.tsv` | Emanuele Bottelli (AI-assisted) | Original Italian translations of CADCraft English strings | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | No proprietary localization resources copied | +| `crates/ui-egui/src/i18n/it.tsv` | Emanuele Bottelli (AI-assisted); terminology corrections: Andrea Brandi (@starise) with OpenAI Codex (AI-assisted) | Original Italian translations of CADCraft English strings | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | No proprietary localization resources copied; targeted Chamfer terminology review by a native speaker | | `crates/ui-egui/src/i18n/ko.tsv` | @huk082 (AI-assisted) | Original Korean translations of CADCraft English strings | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | No proprietary localization resources copied; no font assets added | | `crates/ui-egui/src/i18n/ar.tsv` | @hasan-sdaif | Original Arabic translations of CADCraft English strings | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | No proprietary localization resources copied | | `crates/ui-egui/src/i18n/hu.tsv` | @Skaywalker | Original Hungarian translations of CADCraft English strings | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | No proprietary localization resources copied | diff --git a/contributors/people.toml b/contributors/people.toml index 40d6ba823..7d45ed5f2 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -91,3 +91,5 @@ sync_github_name = false real_name = "Robert Lee" sync_github_name = true +[people.starise] +real_name = "Andrea Brandi" diff --git a/crates/ui-egui/src/i18n/it.tsv b/crates/ui-egui/src/i18n/it.tsv index 27786b2e5..f012f0907 100644 --- a/crates/ui-egui/src/i18n/it.tsv +++ b/crates/ui-egui/src/i18n/it.tsv @@ -131,7 +131,7 @@ menu Modify Edita Center, Start, Angle Centro, inizio, angolo Center, Start, End Centro, inizio, fine Center, Start, Length Centro, inizio, lunghezza - Chamfer Cima + Chamfer Smusso Change Space Cambia spazio Change to Current Layer Cambia al layer corrente Changed: {n} Modificati: {n} @@ -915,8 +915,8 @@ menu Modify Edita Specify fillet radius Specificare il raggio di raccordo Specify fillet radius for rectangles Specificare il raggio di raccordo dei rettangoli Specify first angle endpoint Specificare il primo estremo dell'angolo - Specify first chamfer distance Specificare la prima distanza di cimatura - Specify first chamfer distance for rectangles Specificare la prima distanza di cimatura dei rettangoli + Specify first chamfer distance Specificare la prima distanza di smusso + Specify first chamfer distance for rectangles Specificare la prima distanza di smusso dei rettangoli Specify first constraint point Specificare il primo punto del vincolo Specify first corner Specificare il primo angolo Specify first corner point Specificare il primo angolo @@ -1001,7 +1001,7 @@ menu Modify Edita Specify scale factor Specificare il fattore di scala Specify second angle endpoint Specificare il secondo estremo dell'angolo Specify second break point Specificare il secondo punto di interruzione - Specify second chamfer distance for rectangles Specificare la seconda distanza di cimatura dei rettangoli + Specify second chamfer distance for rectangles Specificare la seconda distanza di smusso dei rettangoli Specify second constraint point Specificare il secondo punto del vincolo Specify second destination point Specificare il secondo punto di destinazione Specify second end point of circle's diameter Specificare il secondo estremo del diametro del cerchio diff --git a/docs/localization-parity.md b/docs/localization-parity.md index b4ff05f68..b19a07eb0 100644 --- a/docs/localization-parity.md +++ b/docs/localization-parity.md @@ -1,6 +1,6 @@ # Localization parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (the command line enables the OS IME and handles composition/commit, so single-line TEXT edits accept CJK input; previously: Brazilian Portuguese row updated and terminology reviewed; previously: Swedish catalog completed with the 95 strings added since; previously: Swedish catalog added: complete catalog, menu headings, native-speaker terminology review; previously: complete current Korean catalog with Hangul font coverage) · **Target:** Autodesk AutoCAD 2027 for Mac (26.0) +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-27 · **Change:** patch (Italian Chamfer terminology corrected; previously: the command line enables the OS IME and handles composition/commit, so single-line TEXT edits accept CJK input; previously: Brazilian Portuguese row updated and terminology reviewed; previously: Swedish catalog completed with the 95 strings added since; previously: Swedish catalog added: complete catalog, menu headings, native-speaker terminology review; previously: complete current Korean catalog with Hangul font coverage) · **Target:** Autodesk AutoCAD 2027 for Mac (26.0) How far CADCraft's interface and drawing text are from AutoCAD's languages. Summarised in [ROADMAP.md](../ROADMAP.md#languages); work items in [gaps.md](gaps.md#localization). @@ -71,7 +71,7 @@ plus native review by a human; extending the catalog to the remaining English-on | Ukrainian | uk | 989 catalog rows (≈ 100% of the catalog; ≈ 80% of all UI strings, estimated: engine diagnostics and uncatalogued text stay English) | dialogs and tooltips yes; command reference searchable in Ukrainian | Cyrillic: yes (bundled egui fonts) | translated by a contributor; no second review | partial | 2–4 h | | Czech | cs | 1,202 catalog rows; reference catalog and marked UI literals covered | dialogs and tooltips yes; contextual menu headings; command reference searchable in Czech | Latin: yes (bundled egui proportional and monospace fonts; Czech letters and quotes tested) | AI-assisted by @Losminek; independent review pass; not yet proofread by a native speaker | complete current catalog; English-only interface gaps remain | native review + shared English-only UI work | | Swedish | sv | 1,330 catalog rows; reference catalog, marked UI literals and menu headings covered (no gaps reported) | dialogs and tooltips yes; contextual menu headings; command reference searchable in Swedish | Latin: yes (bundled egui proportional and monospace fonts; å ä ö é and Swedish quotes tested) | AI-assisted (Claude Code) by @holmbom; terminology and the running app reviewed by a native speaker | complete current catalog; English-only interface gaps remain | line-by-line proofreading + shared English-only UI work | -| Italian | it | 1,227 catalog rows; reference catalog and marked UI literals covered | dialogs and tooltips yes; contextual menu headings (Visualizza / Vista, Edita / Modifica); command reference searchable in Italian | Latin: yes (bundled egui fonts; accented letters tested) | AI-assisted by Emanuele Bottelli (Claude), written from the English strings with Italian desktop-CAD vocabulary; no native proofreading pass yet | complete current catalog; English-only interface gaps remain | native review + shared English-only UI work | +| Italian | it | 1,227 catalog rows; reference catalog and marked UI literals covered | dialogs and tooltips yes; contextual menu headings (Visualizza / Vista, Edita / Modifica); command reference searchable in Italian | Latin: yes (bundled egui fonts; accented letters tested) | AI-assisted by Emanuele Bottelli (Claude), written from the English strings with Italian desktop-CAD vocabulary; targeted Chamfer terminology review by Andrea Brandi (@starise), a native speaker, with OpenAI Codex; full native proofreading pending | complete current catalog; English-only interface gaps remain | native review + shared English-only UI work | | Hungarian | hu | 1,326 catalog rows; reference catalog, marked UI literals and menu headings covered (no gaps reported) | dialogs and tooltips yes; contextual menu headings; command reference searchable in Hungarian | Latin: yes (bundled egui proportional and monospace fonts; ő ű Ő Ű and the other accented letters tested) | translated by @Skaywalker from the English strings; independent native proofreading pending | complete current catalog; English-only interface gaps remain | native proofreading + shared English-only UI work | | Turkish | tr | 1,330 catalog rows; reference catalog, marked UI literals and menu headings covered (no gaps reported) | dialogs and tooltips yes; contextual menu headings (Ölçülendirme / Ölçü); command reference searchable in Turkish | Latin: yes (bundled egui proportional and monospace fonts; Ç Ğ İ Ö Ş Ü ç ğ ı ö ş ü tested) | AI-assisted by @ogzhnklvz, written from the English strings with Turkish desktop-CAD vocabulary; the running app reviewed by a native speaker | complete current catalog; English-only interface gaps remain | line-by-line proofreading + shared English-only UI work | @@ -89,6 +89,7 @@ each language, which needs humans. Translations parallelise fully once the catal | Date | Change | Summary | |---|---|---| +| 2026-10-27 | patch | Italian: corrected Chamfer and its three existing distance prompts to use Smusso; targeted native-speaker terminology review with OpenAI Codex assistance | | 2026-10-22 | minor | Command line IME: enabled while a prompt collects text, composition shown but not saved, commit inserted once; single-line TEXT edits accept CJK input (#1761) | | 2026-10-19 | minor | Brazilian Portuguese: row brought up to date (it still read 0%); native-speaker terminology pass on 69 rows (Bind as Integrar, prompt keywords and SPDX kept in English, Thaw Layer, Snap type, Revolve fixed; snap, viewport, grip, offset, layer on/off and multiline wording made consistent with Brazilian usage) | | 2026-10-15 | minor | Greek (`el`) catalog: 1,330 rows, context/plural/locale/font tests; review pending | From c2cb5b86109b4df3a822a29ac0d706284e0c7b54 Mon Sep 17 00:00:00 2001 From: midasmatterhorn <236787783+midasmatterhorn@users.noreply.github.com> Date: Thu, 29 Oct 2026 21:49:48 +0300 Subject: [PATCH 007/100] engine: propagate create_dir_all errors and route render PNG writes through write_atomic --- crates/engine/src/atomic_write.rs | 4 +--- crates/mcp/src/backend.rs | 3 ++- crates/ui-egui/src/control.rs | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/atomic_write.rs b/crates/engine/src/atomic_write.rs index cd7d0fb4b..2d05e019d 100644 --- a/crates/engine/src/atomic_write.rs +++ b/crates/engine/src/atomic_write.rs @@ -37,9 +37,7 @@ pub fn write_atomic(path: &Path, bytes: &[u8]) -> io::Result<()> { Err(e) => return Err(e), }; let dir = path.parent().filter(|p| !p.as_os_str().is_empty()).map_or_else(|| PathBuf::from("."), Path::to_path_buf); - if let Err(e) = std::fs::create_dir_all(&dir) { - return Err(e); - } + std::fs::create_dir_all(&dir)?; let (mut file, tmp) = create_temp(&dir, permissions.as_ref())?; let write = (|| { if let Some(permissions) = permissions { diff --git a/crates/mcp/src/backend.rs b/crates/mcp/src/backend.rs index 9d012076a..d27012418 100644 --- a/crates/mcp/src/backend.rs +++ b/crates/mcp/src/backend.rs @@ -395,7 +395,8 @@ impl Backend for Headless { let png = cadcraft_render::raster::render_png(&list, &view, &cadcraft_render::raster::RasterOptions::default()).ok_or("render failed")?; if let Some(path) = str_p("path") { - std::fs::write(&path, &png).map_err(|e| format!("{path}: {e}"))?; + cadcraft_engine::atomic_write::write_atomic(std::path::Path::new(&path), &png) + .map_err(|e| format!("{path}: {e}"))?; } Ok(json!({"pngBase64": cadcraft_engine::cmd::file::base64_encode(&png), "width": w, "height": h})) } diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 2dc1f898a..4370b5de1 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -489,7 +489,7 @@ pub fn handle(app: &mut CadApp, ctx: &egui::Context, req: &ControlRequest) -> Ou }; #[cfg(not(target_arch = "wasm32"))] if let Some(path) = s("path") { - if let Err(e) = std::fs::write(path, &png) { + if let Err(e) = cadcraft_engine::atomic_write::write_atomic(std::path::Path::new(&path), &png) { return err(e); } return ok(render_reply(&png, w, h, Some(path))); From b6c1ab74f99dc4c66ab38ba63178fed5a1e441ba Mon Sep 17 00:00:00 2001 From: SBaksa Date: Fri, 30 Oct 2026 10:38:00 +1100 Subject: [PATCH 008/100] DXF: leave SAT-text solids out of files of 2013 and later, with a save note (#2334) REGION, 3DSOLID and BODY read from a DXF 2000 hold SAT text; DXF 2013 and later keep ACIS geometry in the data store section, so writing the text there made a file other readers reject. Those entities are now left out of 2013+ DXF output and counted in the save note; 2010 and earlier, and DWG, are unchanged. Test: sat_text_solids_are_left_out_of_dxf_2013_and_later. --- crates/doc/src/foreign.rs | 26 +++++++++++++++++++++++++ crates/engine/src/cmd/file.rs | 26 +++++++++++++++++++++++++ crates/io/src/dxf_raw.rs | 16 ++++++++++++--- crates/io/src/dxf_write/sections.rs | 2 +- crates/io/tests/it/acis_data_store.rs | 28 +++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 4 deletions(-) diff --git a/crates/doc/src/foreign.rs b/crates/doc/src/foreign.rs index e0ebb81a4..3b6aa8ef9 100644 --- a/crates/doc/src/foreign.rs +++ b/crates/doc/src/foreign.rs @@ -94,3 +94,29 @@ impl Foreign { self.data_store.iter().filter(|t| t.code == 320).filter_map(|t| Handle::parse_hex(t.value.trim())).collect() } } + +/// Whether a solid modeler entity (3DSOLID, REGION, BODY) holds its ACIS geometry as SAT text +/// (groups 1 and 3), which DXF 2013 and later no longer has: those files keep it in the data +/// store section, so an entity with SAT text and no record there can't be written to one (#2334). +pub fn sat_text_acis(u: &crate::Unknown, handle: Handle, stored: &std::collections::BTreeSet) -> bool { + ["3DSOLID", "REGION", "BODY"].iter().any(|t| u.dxf_type.eq_ignore_ascii_case(t)) + && !stored.contains(&handle) + && u.tags.iter().take_while(|r| r.code < 1000).any(|r| r.code == 1 || r.code == 3) +} + +/// The entities a DXF of 2013 or later leaves out because they hold SAT-text ACIS data +/// ([`sat_text_acis`]), counted by DXF type, across model space, layouts and block definitions. +pub fn sat_text_dropped(d: &crate::Drawing) -> BTreeMap { + let stored = d.foreign.data_store_entities(); + let mut out = BTreeMap::new(); + let stores = std::iter::once(&d.model).chain(d.layouts.iter().map(|l| &l.entities)).chain(d.blocks.values().map(|b| &b.entities)); + for e in stores.flat_map(|s| s.iter()) { + if let crate::EntityKind::Unknown(u) = &e.kind + && sat_text_acis(u, e.handle, &stored) + { + let n: &mut usize = out.entry(u.dxf_type.to_ascii_uppercase()).or_default(); + *n = n.saturating_add(1); + } + } + out +} diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 3a5296e88..5b012bb4e 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -520,6 +520,8 @@ fn run_bytes(s: &mut Session, p: &Value) -> Result { let mut r = json!({ "data": base64_encode(&bytes), "bytes": bytes.len() }); if drops_unknown(format!("drawing.{fmt}")) { report_not_saved(s, &mut r, &lost); + } else if version >= Version::R2013 && is_dxf_file(format!("drawing.{fmt}")) { + report_sat_text_dropped(s, &mut r); } Ok(r) } @@ -575,6 +577,28 @@ fn is_drawing_file(path: impl AsRef) -> bool { Format::from_path(path).is_some_and(Format::is_drawing) } +/// Whether `path` is a DXF file. +fn is_dxf_file(path: impl AsRef) -> bool { + Format::from_path(path) == Some(Format::Dxf) +} + +/// Say on the command line which solid modeler entities a DXF of 2013 or later left out because +/// they hold SAT-text ACIS data, which such files keep in a data store instead (#2334). +fn report_sat_text_dropped(s: &mut Session, r: &mut Value) { + let lost = s.doc().map(cadcraft_doc::foreign::sat_text_dropped).unwrap_or_default(); + if lost.is_empty() { + return; + } + let list: Vec = lost.iter().map(|(t, n)| format!("{n} {t}")).collect(); + s.echo(format!( + "Not saved in the DXF file (2013 and later keep ACIS geometry in a data store these entities have no record in; save as 2010 or earlier to keep them): {}", + list.join(", ") + )); + if let Some(o) = r.as_object_mut() { + o.insert("notSaved".into(), json!(lost)); + } +} + /// Say on the command line what the file couldn't keep, and list it in the result's `notSaved`. fn report_not_saved(s: &mut Session, r: &mut Value, lost: &std::collections::BTreeMap) { let Some(msg) = not_saved_message(lost) else { return }; @@ -593,6 +617,8 @@ fn save_reporting(s: &mut Session, p: &Value, run: fn(&mut Session, &Value) -> R let mut r = run(s, p)?; if r.get("path").and_then(Value::as_str).is_some_and(drops_unknown) { report_not_saved(s, &mut r, &lost); + } else if version >= Version::R2013 && r.get("path").and_then(Value::as_str).is_some_and(is_dxf_file) { + report_sat_text_dropped(s, &mut r); } Ok(r) } diff --git a/crates/io/src/dxf_raw.rs b/crates/io/src/dxf_raw.rs index 0154b01d3..5a2aa398c 100644 --- a/crates/io/src/dxf_raw.rs +++ b/crates/io/src/dxf_raw.rs @@ -848,7 +848,7 @@ fn unknown_handles(d: &Drawing) -> HashSet { } /// Put the drawing's foreign data back into a written file's tags (see the module comment). -pub(crate) fn finish(tags: &mut Vec, d: &Drawing) { +pub(crate) fn finish(tags: &mut Vec, d: &Drawing, modern: bool) { let f = d.foreign.as_ref(); let unknown = unknown_handles(d); let xd: XdataOf = all_entities(d).filter(|e| !e.common.xdata.is_empty()).map(|e| (e.handle, e.common.xdata.as_slice())).collect(); @@ -877,7 +877,7 @@ pub(crate) fn finish(tags: &mut Vec, d: &Drawing) { } let renumber = |old: Option| class_index(old).and_then(|i| f.class_order.get(i)).and_then(|n| numbers.get(n).copied()); // Entities of types CADCraft doesn't model, placed among the written ones. - let mut planned = plan_unknown(d, &recs, tags, &roles); + let mut planned = plan_unknown(d, &recs, tags, &roles, modern); for list in planned.values_mut() { list.retain_mut(|(_, t)| { if !t.first().is_some_and(|z| is_proxy(&z.str())) { @@ -1019,7 +1019,14 @@ pub(crate) fn finish(tags: &mut Vec, d: &Drawing) { /// Where the entities of types CADCraft doesn't model go: each before the next written entity of /// its space or block (else at the end of it), as `(section, record)` lists by the index of the /// record they precede. Owners come from the written block records. -fn plan_unknown(d: &Drawing, recs: &[Rec], tags: &[Tag], roles: &HashMap) -> HashMap)>> { +fn plan_unknown( + d: &Drawing, + recs: &[Rec], + tags: &[Tag], + roles: &HashMap, + modern: bool, +) -> HashMap)>> { + let stored = d.foreign.data_store_entities(); let mut out: HashMap)>> = HashMap::new(); let body = |r: &Rec| tags.get(r.start + 1..r.end).unwrap_or(&[]); let mut at: HashMap = HashMap::new(); @@ -1057,6 +1064,9 @@ fn plan_unknown(d: &Drawing, recs: &[Rec], tags: &[Tag], roles: &HashMap { + if modern && cadcraft_doc::foreign::sat_text_acis(u, e.handle, &stored) { + continue; + } let t = unknown_entity(u, e, &owner, paper); if !t.is_empty() { pending.push((sec.to_string(), t)); diff --git a/crates/io/src/dxf_write/sections.rs b/crates/io/src/dxf_write/sections.rs index fe865515b..90214b401 100644 --- a/crates/io/src/dxf_write/sections.rs +++ b/crates/io/src/dxf_write/sections.rs @@ -1115,7 +1115,7 @@ pub(super) fn tags_for(d: &Drawing, version: Version, target: Version) -> Vec= Version::R2013); crate::dxf_raw::data_store(&mut w.t, d, target >= Version::R2013); add_class_instance_counts(&mut w.t); mark_xrefs(&mut w.t, d); diff --git a/crates/io/tests/it/acis_data_store.rs b/crates/io/tests/it/acis_data_store.rs index 74b9809ce..36c947892 100644 --- a/crates/io/tests/it/acis_data_store.rs +++ b/crates/io/tests/it/acis_data_store.rs @@ -101,3 +101,31 @@ fn dwg_keeps_the_solids_geometry() { let again = cadcraft_io::read(&dwg, "x.dwg").unwrap(); assert_eq!(again.foreign.data_store_entities().len(), 2); } + +/// A solid with SAT-text ACIS data (an R2000-style file) can't be written to a DXF of 2013 or +/// later, whose files keep that data in the data store: it is left out there and counted, while +/// 2010 and earlier keep it, and solids with a data store record are kept everywhere (#2334). +#[test] +fn sat_text_solids_are_left_out_of_dxf_2013_and_later() { + let sat = "0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1015\n0\nENDSEC\n0\nSECTION\n2\nENTITIES\n\ + 0\nREGION\n5\n2B\n100\nAcDbEntity\n8\n0\n100\nAcDbModelerGeometry\n70\n1\n1\n400 1 1 0\n1\n16 ZWCAD\n3\nEnd-of-ACIS-data\n\ + 0\n3DSOLID\n5\n2C\n100\nAcDbEntity\n8\n0\n100\nAcDbModelerGeometry\n70\n1\n1\n400 1 1 0\n3\nEnd-of-ACIS-data\n\ + 0\nLINE\n5\n2D\n8\n0\n10\n0\n20\n0\n11\n1\n21\n1\n0\nENDSEC\n0\nEOF\n"; + let d = cadcraft_io::read_dxf(sat.as_bytes()).unwrap(); + let count = |v: cadcraft_io::Version, ty: &str| { + let out = cadcraft_io::write_with(&d, "x.dxf", &cadcraft_io::SaveOptions { version: Some(v), binary: false }).unwrap(); + let t = String::from_utf8(out).unwrap().replace("\r\n", "\n"); + t.matches(&format!("\n0\n{ty}\n")).count() + t.matches(&format!("\n 0\n{ty}\n")).count() + }; + assert_eq!(cadcraft_doc::foreign::sat_text_dropped(&d), [("3DSOLID".to_string(), 1), ("REGION".to_string(), 1)].into()); + for v in [cadcraft_io::Version::R2000, cadcraft_io::Version::R2010] { + assert_eq!((count(v, "REGION"), count(v, "3DSOLID")), (1, 1), "{v:?} keeps them"); + } + for v in [cadcraft_io::Version::R2013, cadcraft_io::Version::R2018] { + assert_eq!((count(v, "REGION"), count(v, "3DSOLID")), (0, 0), "{v:?} leaves them out"); + assert_eq!(count(v, "LINE"), 1, "{v:?} keeps the rest"); + } + // Solids with a data store record keep their geometry in 2013 and later. + let stored = cadcraft_io::read_dxf(dxf().as_bytes()).unwrap(); + assert!(cadcraft_doc::foreign::sat_text_dropped(&stored).is_empty()); +} From 2eecaef6c2958cc78422c9600426fdc0f5b81004 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Fri, 30 Oct 2026 10:41:07 +1100 Subject: [PATCH 009/100] Parity checklist: regenerated after merging main --- docs/parity-checklist.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index a6250d5dc..162a59e3b 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -1,10 +1,10 @@ # Parity checklist -> **Generated:** 2026-10-25 by `cargo xtask parity`. Do not edit by hand. +> **Generated:** 2026-10-29 by `cargo xtask parity`. Do not edit by hand. Menu-breadth parity: a reference menu item counts as covered when a CADCraft command is registered under the same menu label. It measures breadth, not depth; see [target-app-parity.md](target-app-parity.md) for the weighted assessment. -**Menu breadth: 297 / 491 items (60%)** · 442 registered commands. +**Menu breadth: 298 / 491 items (61%)** · 444 registered commands. | Menu | Items | Covered | % | |---|---|---|---| From 49fdbff529dea0f64e7d88a77dfedabe632a939d Mon Sep 17 00:00:00 2001 From: SBaksa Date: Fri, 30 Oct 2026 11:23:09 +1100 Subject: [PATCH 010/100] Test: saving SAT-text solids as DXF 2013 reports them, 2010 does not (#2334) --- crates/engine/tests/save_reports_not_saved.rs | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/engine/tests/save_reports_not_saved.rs b/crates/engine/tests/save_reports_not_saved.rs index 615028a21..f231f8ace 100644 --- a/crates/engine/tests/save_reports_not_saved.rs +++ b/crates/engine/tests/save_reports_not_saved.rs @@ -3,7 +3,7 @@ //! writes them back as read: nothing to report. use cadcraft_engine::Session; -use cadcraft_engine::cmd::file::{IoHooks, set_io}; +use cadcraft_engine::cmd::file::{IoHooks, SaveAs, set_io, set_write_as}; use cadcraft_engine::doc::{Block, Common, Drawing, EntityKind, RawTag, Space, Unknown}; use serde_json::json; @@ -82,3 +82,33 @@ fn solids_whose_geometry_the_data_store_keeps_are_not_reported_from_2013() { // Before 2013 a file has no data store: every solid loses its geometry. assert_eq!(not_saved_as(&d, Version::R2010), [("3DSOLID".to_string(), 2), ("REGION".to_string(), 1)].into_iter().collect()); } + +/// A DXF of 2013 or later can't hold a solid's SAT-text geometry (it lives in the data store +/// there), so the save leaves it out and says so; 2010 and earlier keep it (#2334). +#[test] +fn saving_sat_text_solids_as_dxf_2013_reports_them() { + set_io(IoHooks { read: |_, _| Err("no reader in this test".into()), write: |_, _| Ok(b"0\nEOF\n".to_vec()), plot: None }); + set_write_as(|_, _, _: &SaveAs| Ok(b"x".to_vec())); + let mut d = Drawing::new_imperial(); + let sat = EntityKind::Unknown(Unknown { + dxf_type: "REGION".into(), + tags: vec![ + RawTag { code: 8, value: "0".into() }, + RawTag { code: 1, value: "400 1 1 0".into() }, + RawTag { code: 3, value: "End-of-ACIS-data".into() }, + ], + }); + d.add(&Space::Model, Common::default(), sat).unwrap(); + let mut s = Session::empty(); + s.open_drawing(d, "region.dxf", None); + + let path = std::env::temp_dir().join(format!("cadcraft-sat-note-{}.dxf", std::process::id())); + let r = s.execute("saveas", &json!({ "path": path.to_string_lossy(), "version": 2018 })).unwrap(); + assert_eq!(r["notSaved"], json!({ "REGION": 1 })); + assert!(s.log.iter().any(|l| l.contains("Not saved in the DXF file") && l.contains("1 REGION")), "{:?}", s.log); + + let n = s.log.len(); + let r = s.execute("saveas", &json!({ "path": path.to_string_lossy(), "version": 2010 })).unwrap(); + let _ = std::fs::remove_file(&path); + assert!(r.get("notSaved").is_none() && s.log.len() == n); +} From 89771962fcbe5f238613d107c3dfe2a1a82292a8 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Fri, 30 Oct 2026 11:26:25 +1100 Subject: [PATCH 011/100] Parity checklist: regenerated after merging main --- docs/parity-checklist.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index 162a59e3b..53ffffc96 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -1,6 +1,6 @@ # Parity checklist -> **Generated:** 2026-10-29 by `cargo xtask parity`. Do not edit by hand. +> **Generated:** 2026-10-30 by `cargo xtask parity`. Do not edit by hand. Menu-breadth parity: a reference menu item counts as covered when a CADCraft command is registered under the same menu label. It measures breadth, not depth; see [target-app-parity.md](target-app-parity.md) for the weighted assessment. From 1013b482a81067f7d9f6e1e4247070ba99b8b37b Mon Sep 17 00:00:00 2001 From: SBaksa Date: Fri, 30 Oct 2026 12:26:09 +1100 Subject: [PATCH 012/100] DXF: SAT-text note only for DXF output, from the host drawing, without xref contents (#2334) The DWG bridge writes R2000 text for the DWG writer, which keeps SAT-text solids: the skip is now keyed on the DXF's declared version, not the DWG target. The save note reads the version and count from the host drawing while the Block Editor is open, and leaves out xref blocks (path or xref| name), which the DXF writer empties anyway; the xref name rule is shared with the writer. --- crates/doc/src/foreign.rs | 10 ++++++- crates/engine/src/cmd/file.rs | 10 ++++--- crates/engine/tests/save_reports_not_saved.rs | 27 +++++++++++++++++++ crates/io/src/dxf_write/sections.rs | 4 ++- crates/io/src/dxf_xref.rs | 2 +- crates/io/tests/it/acis_data_store.rs | 27 +++++++++++++++++++ 6 files changed, 74 insertions(+), 6 deletions(-) diff --git a/crates/doc/src/foreign.rs b/crates/doc/src/foreign.rs index 3b6aa8ef9..60000c6dd 100644 --- a/crates/doc/src/foreign.rs +++ b/crates/doc/src/foreign.rs @@ -109,7 +109,9 @@ pub fn sat_text_acis(u: &crate::Unknown, handle: Handle, stored: &std::collectio pub fn sat_text_dropped(d: &crate::Drawing) -> BTreeMap { let stored = d.foreign.data_store_entities(); let mut out = BTreeMap::new(); - let stores = std::iter::once(&d.model).chain(d.layouts.iter().map(|l| &l.entities)).chain(d.blocks.values().map(|b| &b.entities)); + // The writer saves xrefs as references: their blocks (and `xref|` ones) hold nothing in the file. + let own_blocks = d.blocks.values().filter(|b| b.xref_path.is_none() && !xref_dependent(&b.name)); + let stores = std::iter::once(&d.model).chain(d.layouts.iter().map(|l| &l.entities)).chain(own_blocks.map(|b| &b.entities)); for e in stores.flat_map(|s| s.iter()) { if let crate::EntityKind::Unknown(u) = &e.kind && sat_text_acis(u, e.handle, &stored) @@ -120,3 +122,9 @@ pub fn sat_text_dropped(d: &crate::Drawing) -> BTreeMap { } out } + +/// Whether a symbol name belongs to an attached xref (`xref|name`): the DXF writer leaves such +/// symbols out, and the contents of xref blocks too ([`Block::xref_path`](crate::Block::xref_path)). +pub fn xref_dependent(name: &str) -> bool { + name.contains('|') +} diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 5b012bb4e..5d89a196e 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -490,8 +490,11 @@ fn run_saveas(s: &mut Session, p: &Value) -> Result { /// The version a drawing is saved in at `path` when none is asked for: its `$ACADVER` (2018 by /// default), and for DWG at least 2004 (the io layer's rule: R2000 DWG has no true colours). fn drawing_version(s: &Session, path: &str) -> Version { - let v = - s.doc().ok().and_then(|d| d.header.get("ACADVER").and_then(cadcraft_doc::HVal::as_str).and_then(Version::for_acadver)).unwrap_or_default(); + let v = s + .state() + .ok() + .and_then(|st| st.drawing().header.get("ACADVER").and_then(cadcraft_doc::HVal::as_str).and_then(Version::for_acadver)) + .unwrap_or_default(); if is_dxf(path) { v } else { v.max(Version::R2004) } } @@ -585,7 +588,8 @@ fn is_dxf_file(path: impl AsRef) -> bool { /// Say on the command line which solid modeler entities a DXF of 2013 or later left out because /// they hold SAT-text ACIS data, which such files keep in a data store instead (#2334). fn report_sat_text_dropped(s: &mut Session, r: &mut Value) { - let lost = s.doc().map(cadcraft_doc::foreign::sat_text_dropped).unwrap_or_default(); + // The drawing a file holds: the host while the Block Editor is open. + let lost = s.state().map(|st| cadcraft_doc::foreign::sat_text_dropped(st.drawing())).unwrap_or_default(); if lost.is_empty() { return; } diff --git a/crates/engine/tests/save_reports_not_saved.rs b/crates/engine/tests/save_reports_not_saved.rs index f231f8ace..839c2489b 100644 --- a/crates/engine/tests/save_reports_not_saved.rs +++ b/crates/engine/tests/save_reports_not_saved.rs @@ -112,3 +112,30 @@ fn saving_sat_text_solids_as_dxf_2013_reports_them() { let _ = std::fs::remove_file(&path); assert!(r.get("notSaved").is_none() && s.log.len() == n); } + +/// In the Block Editor the file still holds the host drawing: its solids and version decide the +/// note, not the edited block's (#2334). +#[test] +fn the_note_counts_the_host_drawing_while_a_block_is_edited() { + set_io(IoHooks { read: |_, _| Err("no reader in this test".into()), write: |_, _| Ok(b"0\nEOF\n".to_vec()), plot: None }); + set_write_as(|_, _, _: &SaveAs| Ok(b"x".to_vec())); + let mut d = Drawing::new_imperial(); + let sat = EntityKind::Unknown(Unknown { + dxf_type: "REGION".into(), + tags: vec![ + RawTag { code: 8, value: "0".into() }, + RawTag { code: 1, value: "400 1 1 0".into() }, + RawTag { code: 3, value: "End-of-ACIS-data".into() }, + ], + }); + d.add(&Space::Model, Common::default(), sat).unwrap(); + d.blocks.insert("Door".into(), std::sync::Arc::new(Block::new("Door"))); + let mut s = Session::empty(); + s.open_drawing(d, "region.dxf", None); + s.execute("bedit", &json!({ "name": "Door" })).unwrap(); + + let path = std::env::temp_dir().join(format!("cadcraft-sat-edit-{}.dxf", std::process::id())); + let r = s.execute("saveas", &json!({ "path": path.to_string_lossy(), "version": 2018 })).unwrap(); + let _ = std::fs::remove_file(&path); + assert_eq!(r["notSaved"], json!({ "REGION": 1 })); +} diff --git a/crates/io/src/dxf_write/sections.rs b/crates/io/src/dxf_write/sections.rs index 90214b401..815f4eb32 100644 --- a/crates/io/src/dxf_write/sections.rs +++ b/crates/io/src/dxf_write/sections.rs @@ -1115,7 +1115,9 @@ pub(super) fn tags_for(d: &Drawing, version: Version, target: Version) -> Vec= Version::R2013); + // Only a DXF declares the version it is written for: the DWG bridge writes R2000 text for the + // DWG writer, which keeps SAT-text solids (#2334). + crate::dxf_raw::finish(&mut w.t, d, version >= Version::R2013); crate::dxf_raw::data_store(&mut w.t, d, target >= Version::R2013); add_class_instance_counts(&mut w.t); mark_xrefs(&mut w.t, d); diff --git a/crates/io/src/dxf_xref.rs b/crates/io/src/dxf_xref.rs index f98a044f7..9ea7904cd 100644 --- a/crates/io/src/dxf_xref.rs +++ b/crates/io/src/dxf_xref.rs @@ -13,7 +13,7 @@ use cadcraft_doc::Drawing; /// Whether a symbol name belongs to an attached xref (`xref|name`). pub(crate) fn dependent(name: &str) -> bool { - name.contains('|') + cadcraft_doc::foreign::xref_dependent(name) } /// The drawing as it is saved: xref blocks emptied, xref-dependent symbols other than layers and diff --git a/crates/io/tests/it/acis_data_store.rs b/crates/io/tests/it/acis_data_store.rs index 36c947892..308b532d9 100644 --- a/crates/io/tests/it/acis_data_store.rs +++ b/crates/io/tests/it/acis_data_store.rs @@ -129,3 +129,30 @@ fn sat_text_solids_are_left_out_of_dxf_2013_and_later() { let stored = cadcraft_io::read_dxf(dxf().as_bytes()).unwrap(); assert!(cadcraft_doc::foreign::sat_text_dropped(&stored).is_empty()); } + +/// A DWG keeps SAT-text solids at any version (its writer handles them), and a DXF's note leaves +/// out the contents of xref blocks, which the DXF doesn't hold anyway (#2334). +#[test] +fn sat_text_solids_stay_in_dwg_2018_and_xref_contents_are_not_counted() { + let sat = "0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1015\n0\nENDSEC\n0\nSECTION\n2\nENTITIES\n\ + 0\nREGION\n5\n2B\n100\nAcDbEntity\n8\n0\n100\nAcDbModelerGeometry\n70\n1\n1\n400 1 1 0\n3\nEnd-of-ACIS-data\n\ + 0\nLINE\n5\n2D\n8\n0\n10\n0\n20\n0\n11\n1\n21\n1\n0\nENDSEC\n0\nEOF\n"; + let d = cadcraft_io::read_dxf(sat.as_bytes()).unwrap(); + let opts = cadcraft_io::SaveOptions { version: Some(cadcraft_io::Version::R2018), binary: false }; + let dwg = cadcraft_io::write_with(&d, "x.dwg", &opts).unwrap(); + let back = cadcraft_io::read(&dwg, "x.dwg").unwrap(); + let unknown = |d: &cadcraft_doc::Drawing| d.model.iter().filter(|e| matches!(&e.kind, EntityKind::Unknown(u) if u.dxf_type == "REGION")).count(); + assert_eq!(unknown(&back), 1, "the DWG 2018 keeps the region"); + + // Xref blocks (and `xref|` ones) hold no entities in the file: not reported as left out. + let mut x = d.clone(); + let region = d.model.iter().find(|e| matches!(&e.kind, EntityKind::Unknown(_))).cloned().unwrap(); + let mut host = cadcraft_doc::Block::new("REF"); + host.xref_path = Some("other.dwg".into()); + host.entities.push((*region).clone()); + x.blocks.insert("REF".into(), std::sync::Arc::new(host)); + let mut dep = cadcraft_doc::Block::new("REF|PART"); + dep.entities.push((*region).clone()); + x.blocks.insert("REF|PART".into(), std::sync::Arc::new(dep)); + assert_eq!(cadcraft_doc::foreign::sat_text_dropped(&x), [("REGION".to_string(), 1)].into(), "only the model-space region"); +} From fc8270d8c52acd6dba69e221b889193b1a497739 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Fri, 30 Oct 2026 12:47:03 +1100 Subject: [PATCH 013/100] Parity checklist: regenerated after merging main --- docs/parity-checklist.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index d0bd6f967..53ffffc96 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -1,7 +1,6 @@ # Parity checklist -> **Generated:** 2026-10-29 by `cargo xtask parity`. Do not edit by hand. -> **Generated:** 2026-10-28 by `cargo xtask parity`. Do not edit by hand. +> **Generated:** 2026-10-30 by `cargo xtask parity`. Do not edit by hand. Menu-breadth parity: a reference menu item counts as covered when a CADCraft command is registered under the same menu label. It measures breadth, not depth; see [target-app-parity.md](target-app-parity.md) for the weighted assessment. From 86d010eaac0d458a528d91705c0a2118f80fa528 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Fri, 30 Oct 2026 14:59:16 +1100 Subject: [PATCH 014/100] DXF: SAT text is groups 1 and 3 of the modeler subclass; only ASM_Data records keep geometry (#2334) sat_text_acis now looks for groups 1 and 3 inside AcDbModelerGeometry only (all groups when a file has no subclass markers), and the data store counts an entity as stored only when its ACDSRECORD holds ASM_Data. The writer and the reader share one record splitter (data_store_records). --- crates/doc/src/foreign.rs | 46 ++++++++++++++++++++++++--- crates/io/src/dxf_raw.rs | 6 +--- crates/io/tests/it/acis_data_store.rs | 29 +++++++++++++++++ 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/crates/doc/src/foreign.rs b/crates/doc/src/foreign.rs index 60000c6dd..133c525e2 100644 --- a/crates/doc/src/foreign.rs +++ b/crates/doc/src/foreign.rs @@ -89,9 +89,17 @@ impl Foreign { && self.data_store.is_empty() } - /// The entities the data store has records for (their 320 handles). + /// The entities with a geometry record in the data store: an `ACDSRECORD` naming the entity + /// (320) that holds its `ASM_Data`. A 320 without one doesn't keep an entity's geometry. pub fn data_store_entities(&self) -> std::collections::BTreeSet { - self.data_store.iter().filter(|t| t.code == 320).filter_map(|t| Handle::parse_hex(t.value.trim())).collect() + let mut out = std::collections::BTreeSet::new(); + for rec in data_store_records(&self.data_store) { + let is_record = rec.first().is_some_and(|t| t.value.trim().eq_ignore_ascii_case("ACDSRECORD")); + if is_record && rec.iter().any(|t| t.code == 2 && t.value.trim().eq_ignore_ascii_case("ASM_Data")) { + out.extend(rec.iter().filter(|t| t.code == 320).filter_map(|t| Handle::parse_hex(t.value.trim()))); + } + } + out } } @@ -99,9 +107,7 @@ impl Foreign { /// (groups 1 and 3), which DXF 2013 and later no longer has: those files keep it in the data /// store section, so an entity with SAT text and no record there can't be written to one (#2334). pub fn sat_text_acis(u: &crate::Unknown, handle: Handle, stored: &std::collections::BTreeSet) -> bool { - ["3DSOLID", "REGION", "BODY"].iter().any(|t| u.dxf_type.eq_ignore_ascii_case(t)) - && !stored.contains(&handle) - && u.tags.iter().take_while(|r| r.code < 1000).any(|r| r.code == 1 || r.code == 3) + ["3DSOLID", "REGION", "BODY"].iter().any(|t| u.dxf_type.eq_ignore_ascii_case(t)) && !stored.contains(&handle) && has_sat_text(&u.tags) } /// The entities a DXF of 2013 or later leaves out because they hold SAT-text ACIS data @@ -128,3 +134,33 @@ pub fn sat_text_dropped(d: &crate::Drawing) -> BTreeMap { pub fn xref_dependent(name: &str) -> bool { name.contains('|') } + +/// The records of a data store section's groups: each starts at a `0` group and runs to the next +/// (the section's own groups before the first record are not part of one). +pub fn data_store_records(tags: &[RawTag]) -> Vec<&[RawTag]> { + let mut out = Vec::new(); + let mut start = tags.iter().position(|t| t.code == 0).unwrap_or(tags.len()); + while start < tags.len() { + let end = tags.iter().skip(start + 1).position(|t| t.code == 0).map_or(tags.len(), |k| start + 1 + k); + out.push(tags.get(start..end).unwrap_or(&[])); + start = end; + } + out +} + +/// Whether an entity's groups hold SAT text: groups 1 and 3 inside its `AcDbModelerGeometry` +/// subclass. A file of a version before the subclass markers has no `100` groups: all of its +/// groups (before the extended data) are the entity's. +fn has_sat_text(tags: &[RawTag]) -> bool { + let own = tags.iter().take_while(|r| r.code < 1000); + let marked = tags.iter().take_while(|r| r.code < 1000).any(|r| r.code == 100); + let mut in_modeler = !marked; + for r in own { + if r.code == 100 { + in_modeler = r.value.trim().eq_ignore_ascii_case("AcDbModelerGeometry"); + } else if in_modeler && (r.code == 1 || r.code == 3) { + return true; + } + } + false +} diff --git a/crates/io/src/dxf_raw.rs b/crates/io/src/dxf_raw.rs index 5a2aa398c..7856a24b8 100644 --- a/crates/io/src/dxf_raw.rs +++ b/crates/io/src/dxf_raw.rs @@ -601,17 +601,13 @@ pub(crate) fn data_store(tags: &mut Vec, d: &Drawing, modern: bool) { let first = ds.iter().position(|t| t.code == 0).unwrap_or(ds.len()); out.extend(ds.iter().take(first).map(tag)); let mut records = 0usize; - let mut i = first; - while i < ds.len() { - let end = ds.iter().skip(i + 1).position(|t| t.code == 0).map_or(ds.len(), |k| i + 1 + k); - let rec = ds.get(i..end).unwrap_or(&[]); + for rec in cadcraft_doc::foreign::data_store_records(ds) { let is_record = rec.first().is_some_and(|t| t.value.trim().eq_ignore_ascii_case("ACDSRECORD")); let owner = rec.iter().find(|t| t.code == 320).map(|t| norm(&t.value)); if !is_record || owner.as_ref().is_some_and(|h| written.contains(h)) { records += usize::from(is_record); out.extend(rec.iter().map(tag)); } - i = end; } if records == 0 { return; diff --git a/crates/io/tests/it/acis_data_store.rs b/crates/io/tests/it/acis_data_store.rs index 308b532d9..4f8f455b4 100644 --- a/crates/io/tests/it/acis_data_store.rs +++ b/crates/io/tests/it/acis_data_store.rs @@ -156,3 +156,32 @@ fn sat_text_solids_stay_in_dwg_2018_and_xref_contents_are_not_counted() { x.blocks.insert("REF|PART".into(), std::sync::Arc::new(dep)); assert_eq!(cadcraft_doc::foreign::sat_text_dropped(&x), [("REGION".to_string(), 1)].into(), "only the model-space region"); } + +/// Only groups 1 and 3 of the modeler geometry subclass are SAT text, and only a record holding +/// the entity's ASM_Data keeps its geometry in the data store (#2334). +#[test] +fn sat_text_is_told_by_its_subclass_and_a_geometry_record() { + use cadcraft_doc::foreign::{Foreign, sat_text_acis}; + use cadcraft_doc::{Handle, RawTag, Unknown}; + let tag = |code: i32, value: &str| RawTag { code, value: value.into() }; + let solid = |tags: Vec| Unknown { dxf_type: "3DSOLID".into(), tags }; + let none = std::collections::BTreeSet::new(); + let h = Handle(0x2B); + // A stray 1 or 3 outside AcDbModelerGeometry is not SAT text. + let stray = + solid(vec![tag(100, "AcDbEntity"), tag(1, "name"), tag(100, "AcDbModelerGeometry"), tag(70, "1"), tag(100, "AcDb3dSolid"), tag(3, "x")]); + assert!(!sat_text_acis(&stray, h, &none)); + let real = solid(vec![tag(100, "AcDbEntity"), tag(100, "AcDbModelerGeometry"), tag(70, "1"), tag(1, "400 1 1 0"), tag(100, "AcDb3dSolid")]); + assert!(sat_text_acis(&real, h, &none)); + // Without subclass markers (an old file) the groups are the entity's own. + assert!(sat_text_acis(&solid(vec![tag(8, "0"), tag(1, "400 1 1 0")]), h, &none)); + + // A 320 in the data store without an ASM_Data record doesn't keep the geometry. + let store = |asm: bool| { + let mut t = vec![tag(0, "ACDSRECORD"), tag(90, "1"), tag(320, "2B")]; + t.push(tag(2, if asm { "ASM_Data" } else { "Other" })); + Foreign { data_store: t, ..Foreign::default() } + }; + assert!(store(true).data_store_entities().contains(&h)); + assert!(store(false).data_store_entities().is_empty()); +} From 590b97a3211743a780de5996f7e2dadb1918879d Mon Sep 17 00:00:00 2001 From: SBaksa Date: Fri, 30 Oct 2026 18:42:24 +1100 Subject: [PATCH 015/100] Save note: QSAVE reports by the version it writes, not a `version` it ignores (#2334) QSAVE takes no version and writes the drawing's own, but the SAT-text note read a `version` passed to it, so `qsave {version: 2010}` on a 2018 drawing skipped the note while still leaving the solids out. Only SAVEAS's requested version now picks the reporting branch. Test: saving_sat_text_solids_as_dxf_2013_reports_them (qsave case fails without the fix). --- crates/engine/src/cmd/file.rs | 10 ++++++---- crates/engine/tests/save_reports_not_saved.rs | 5 +++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 28c8d66be..e91bbd9da 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -613,10 +613,11 @@ fn report_not_saved(s: &mut Session, r: &mut Value, lost: &std::collections::BTr } /// Save, then report the entities the file couldn't keep (nothing is dropped silently). -fn save_reporting(s: &mut Session, p: &Value, run: fn(&mut Session, &Value) -> Result) -> Result { +fn save_reporting(s: &mut Session, p: &Value, run: fn(&mut Session, &Value) -> Result, asked: Option) -> Result { // What the file will hold depends on the version it is saved as (the data store from 2013). + // QSAVE takes no `version` and keeps the drawing's own; SAVEAS writes the one asked for. let path = str_param(p, "path").map(str::to_string).or_else(|| s.state().ok().and_then(|st| st.path.clone())).unwrap_or_default(); - let version = save_options("save", p).ok().and_then(|o| o.version).unwrap_or_else(|| drawing_version(s, &path)); + let version = asked.unwrap_or_else(|| drawing_version(s, &path)); let lost = s.doc().map(|d| not_saved_as(d, version)).unwrap_or_default(); let mut r = run(s, p)?; if r.get("path").and_then(Value::as_str).is_some_and(drops_unknown) { @@ -628,11 +629,12 @@ fn save_reporting(s: &mut Session, p: &Value, run: fn(&mut Session, &Value) -> R } fn run_qsave_reporting(s: &mut Session, p: &Value) -> Result { - save_reporting(s, p, run_save) + save_reporting(s, p, run_save, None) } fn run_saveas_reporting(s: &mut Session, p: &Value) -> Result { - save_reporting(s, p, run_saveas) + let asked = save_options("saveas", p)?.version; + save_reporting(s, p, run_saveas, asked) } const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; diff --git a/crates/engine/tests/save_reports_not_saved.rs b/crates/engine/tests/save_reports_not_saved.rs index 839c2489b..5472254a1 100644 --- a/crates/engine/tests/save_reports_not_saved.rs +++ b/crates/engine/tests/save_reports_not_saved.rs @@ -107,6 +107,11 @@ fn saving_sat_text_solids_as_dxf_2013_reports_them() { assert_eq!(r["notSaved"], json!({ "REGION": 1 })); assert!(s.log.iter().any(|l| l.contains("Not saved in the DXF file") && l.contains("1 REGION")), "{:?}", s.log); + // QSAVE takes no version: a `version` passed to it doesn't change what it writes (the + // drawing's own 2018), so the note still comes. + let r = s.execute("qsave", &json!({ "path": path.to_string_lossy(), "version": 2010 })).unwrap(); + assert_eq!(r["notSaved"], json!({ "REGION": 1 })); + let n = s.log.len(); let r = s.execute("saveas", &json!({ "path": path.to_string_lossy(), "version": 2010 })).unwrap(); let _ = std::fs::remove_file(&path); From 7baa9703818416d132e88e0978c8de83a75ef648 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Fri, 30 Oct 2026 19:18:41 +1100 Subject: [PATCH 016/100] Save note: a data store record stores its first entity; a DWG keeps SAT-text solids (#2334) The writer keeps an ACDSRECORD by its first 320 only, so data_store_entities now credits that handle alone (one shared rule, data_store_owner). The DWG writer converts a solid's SAT text itself at any version, so not_saved_as no longer lists solids that hold SAT text as left out of a DWG. --- crates/doc/src/foreign.rs | 10 ++++++++-- crates/engine/src/cmd/file.rs | 6 +++++- crates/engine/tests/save_reports_not_saved.rs | 15 +++++++++++++++ crates/io/src/dxf_raw.rs | 2 +- crates/io/tests/it/acis_data_store.rs | 11 +++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/crates/doc/src/foreign.rs b/crates/doc/src/foreign.rs index 133c525e2..dbae3ab49 100644 --- a/crates/doc/src/foreign.rs +++ b/crates/doc/src/foreign.rs @@ -96,7 +96,7 @@ impl Foreign { for rec in data_store_records(&self.data_store) { let is_record = rec.first().is_some_and(|t| t.value.trim().eq_ignore_ascii_case("ACDSRECORD")); if is_record && rec.iter().any(|t| t.code == 2 && t.value.trim().eq_ignore_ascii_case("ASM_Data")) { - out.extend(rec.iter().filter(|t| t.code == 320).filter_map(|t| Handle::parse_hex(t.value.trim()))); + out.extend(data_store_owner(rec).and_then(|t| Handle::parse_hex(t.value.trim()))); } } out @@ -151,7 +151,7 @@ pub fn data_store_records(tags: &[RawTag]) -> Vec<&[RawTag]> { /// Whether an entity's groups hold SAT text: groups 1 and 3 inside its `AcDbModelerGeometry` /// subclass. A file of a version before the subclass markers has no `100` groups: all of its /// groups (before the extended data) are the entity's. -fn has_sat_text(tags: &[RawTag]) -> bool { +pub fn has_sat_text(tags: &[RawTag]) -> bool { let own = tags.iter().take_while(|r| r.code < 1000); let marked = tags.iter().take_while(|r| r.code < 1000).any(|r| r.code == 100); let mut in_modeler = !marked; @@ -164,3 +164,9 @@ fn has_sat_text(tags: &[RawTag]) -> bool { } false } + +/// The group naming the entity a data store record belongs to: its first `320`. A record is kept +/// or dropped by this handle alone, in the writer and wherever the store is read. +pub fn data_store_owner(record: &[RawTag]) -> Option<&RawTag> { + record.iter().find(|t| t.code == 320) +} diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index e91bbd9da..0bccbfb4f 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -550,7 +550,11 @@ pub fn not_saved_as(d: &Drawing, version: Version) -> std::collections::BTreeMap let stores = std::iter::once(&d.model).chain(d.layouts.iter().map(|l| &l.entities)).chain(d.blocks.values().map(|b| &b.entities)); for e in stores.flat_map(|s| s.iter()) { if let cadcraft_doc::EntityKind::Unknown(u) = &e.kind { - if MODELER_TYPES.iter().any(|t| u.dxf_type.eq_ignore_ascii_case(t)) && stored.contains(&e.handle) { + // A DWG writer converts SAT text to its own form at any version, and from 2013 a data + // store record keeps the geometry too: neither loses the solid. + if MODELER_TYPES.iter().any(|t| u.dxf_type.eq_ignore_ascii_case(t)) + && (stored.contains(&e.handle) || cadcraft_doc::foreign::has_sat_text(&u.tags)) + { continue; } let n = out.entry(u.dxf_type.clone()).or_insert(0); diff --git a/crates/engine/tests/save_reports_not_saved.rs b/crates/engine/tests/save_reports_not_saved.rs index 5472254a1..d50b1a503 100644 --- a/crates/engine/tests/save_reports_not_saved.rs +++ b/crates/engine/tests/save_reports_not_saved.rs @@ -144,3 +144,18 @@ fn the_note_counts_the_host_drawing_while_a_block_is_edited() { let _ = std::fs::remove_file(&path); assert_eq!(r["notSaved"], json!({ "REGION": 1 })); } + +/// A DWG writer converts a solid's SAT text itself (at any version), so a DWG save doesn't list +/// solids that hold it as left out; ones with neither SAT text nor a data store record are (#2334). +#[test] +fn a_dwg_keeps_solids_that_hold_sat_text() { + use cadcraft_engine::cmd::file::{Version, not_saved_as}; + let tag = |code: i32, value: &str| RawTag { code, value: value.into() }; + let solid = |tags: Vec| EntityKind::Unknown(Unknown { dxf_type: "3DSOLID".into(), tags }); + let mut d = Drawing::new_imperial(); + d.add(&Space::Model, Common::default(), solid(vec![tag(100, "AcDbModelerGeometry"), tag(1, "400 1 1 0")])).unwrap(); + d.add(&Space::Model, Common::default(), solid(vec![tag(100, "AcDbModelerGeometry"), tag(70, "1")])).unwrap(); + for v in [Version::R2010, Version::R2018] { + assert_eq!(not_saved_as(&d, v), [("3DSOLID".to_string(), 1)].into_iter().collect(), "{v:?}"); + } +} diff --git a/crates/io/src/dxf_raw.rs b/crates/io/src/dxf_raw.rs index 7856a24b8..9efb5b07d 100644 --- a/crates/io/src/dxf_raw.rs +++ b/crates/io/src/dxf_raw.rs @@ -603,7 +603,7 @@ pub(crate) fn data_store(tags: &mut Vec, d: &Drawing, modern: bool) { let mut records = 0usize; for rec in cadcraft_doc::foreign::data_store_records(ds) { let is_record = rec.first().is_some_and(|t| t.value.trim().eq_ignore_ascii_case("ACDSRECORD")); - let owner = rec.iter().find(|t| t.code == 320).map(|t| norm(&t.value)); + let owner = cadcraft_doc::foreign::data_store_owner(rec).map(|t| norm(&t.value)); if !is_record || owner.as_ref().is_some_and(|h| written.contains(h)) { records += usize::from(is_record); out.extend(rec.iter().map(tag)); diff --git a/crates/io/tests/it/acis_data_store.rs b/crates/io/tests/it/acis_data_store.rs index 4f8f455b4..5bb4bc8ee 100644 --- a/crates/io/tests/it/acis_data_store.rs +++ b/crates/io/tests/it/acis_data_store.rs @@ -185,3 +185,14 @@ fn sat_text_is_told_by_its_subclass_and_a_geometry_record() { assert!(store(true).data_store_entities().contains(&h)); assert!(store(false).data_store_entities().is_empty()); } + +/// The writer keeps a data store record by its first 320 only, so only that entity counts as +/// stored (#2334). +#[test] +fn a_data_store_record_stores_only_its_first_entity() { + use cadcraft_doc::foreign::Foreign; + use cadcraft_doc::{Handle, RawTag}; + let tag = |code: i32, value: &str| RawTag { code, value: value.into() }; + let store = Foreign { data_store: vec![tag(0, "ACDSRECORD"), tag(320, "2B"), tag(320, "2C"), tag(2, "ASM_Data")], ..Foreign::default() }; + assert_eq!(store.data_store_entities(), [Handle(0x2B)].into()); +} From 282ad11e60f22d4d7bc93d300eeff985a508df8a Mon Sep 17 00:00:00 2001 From: SBaksa Date: Sat, 31 Oct 2026 12:28:21 +1100 Subject: [PATCH 017/100] Save note: a DWG loses SAT-text solids from 2013 on, and the note says so (#2334) A real DWG write and read-back keeps a SAT-text REGION's groups up to 2010 and returns it without them at 2013 and 2018, so the SAT-text skip in not_saved_as applies before 2013 only. The DWG note now says solids are kept in a DXF only when saved as 2010 or earlier. --- crates/engine/src/cmd/file.rs | 11 ++++++---- crates/engine/tests/save_reports_not_saved.rs | 14 ++++++------- crates/io/tests/it/acis_data_store.rs | 21 +++++++++++++++++++ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 0bccbfb4f..683bd26f8 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -550,10 +550,10 @@ pub fn not_saved_as(d: &Drawing, version: Version) -> std::collections::BTreeMap let stores = std::iter::once(&d.model).chain(d.layouts.iter().map(|l| &l.entities)).chain(d.blocks.values().map(|b| &b.entities)); for e in stores.flat_map(|s| s.iter()) { if let cadcraft_doc::EntityKind::Unknown(u) = &e.kind { - // A DWG writer converts SAT text to its own form at any version, and from 2013 a data - // store record keeps the geometry too: neither loses the solid. + // A DWG before 2013 writes a solid's SAT text as it is; from 2013 the geometry has to be + // in the data store, and SAT text alone is lost (#2334). if MODELER_TYPES.iter().any(|t| u.dxf_type.eq_ignore_ascii_case(t)) - && (stored.contains(&e.handle) || cadcraft_doc::foreign::has_sat_text(&u.tags)) + && (stored.contains(&e.handle) || (version < Version::R2013 && cadcraft_doc::foreign::has_sat_text(&u.tags))) { continue; } @@ -570,7 +570,10 @@ pub fn not_saved_message(lost: &std::collections::BTreeMap) -> Op return None; } let list: Vec = lost.iter().map(|(t, n)| format!("{n} {t}")).collect(); - Some(format!("Not saved in the DWG file (its writer can't write these object types yet; DXF keeps them): {}", list.join(", "))) + Some(format!( + "Not saved in the DWG file (its writer can't write these object types yet; DXF keeps them, solids only when saved as 2010 or earlier): {}", + list.join(", ") + )) } /// Whether saving to `path` leaves out the entities [`not_saved`] counts: DWG does (the DWG diff --git a/crates/engine/tests/save_reports_not_saved.rs b/crates/engine/tests/save_reports_not_saved.rs index d50b1a503..295877d5c 100644 --- a/crates/engine/tests/save_reports_not_saved.rs +++ b/crates/engine/tests/save_reports_not_saved.rs @@ -29,7 +29,7 @@ fn save_lists_the_entities_the_file_does_not_keep() { let r = s.execute("saveas", &json!({ "path": path.to_string_lossy() })).unwrap(); let _ = std::fs::remove_file(&path); assert_eq!(r["notSaved"], json!({ "3DSOLID": 1, "MLINE": 2, "REGION": 1 })); - let msg = "Not saved in the DWG file (its writer can't write these object types yet; DXF keeps them): 1 3DSOLID, 2 MLINE, 1 REGION"; + let msg = "Not saved in the DWG file (its writer can't write these object types yet; DXF keeps them, solids only when saved as 2010 or earlier): 1 3DSOLID, 2 MLINE, 1 REGION"; assert!(s.log.iter().any(|l| l == msg), "{:?}", s.log); // DXF keeps them. @@ -145,17 +145,17 @@ fn the_note_counts_the_host_drawing_while_a_block_is_edited() { assert_eq!(r["notSaved"], json!({ "REGION": 1 })); } -/// A DWG writer converts a solid's SAT text itself (at any version), so a DWG save doesn't list -/// solids that hold it as left out; ones with neither SAT text nor a data store record are (#2334). +/// A DWG before 2013 writes a solid's SAT text as it is, so a save there doesn't list a solid that +/// holds it; from 2013 the geometry must be in the data store and SAT text alone is lost, so the +/// save lists it. A solid with neither is always listed (#2334). #[test] -fn a_dwg_keeps_solids_that_hold_sat_text() { +fn a_dwg_keeps_sat_text_solids_only_before_2013() { use cadcraft_engine::cmd::file::{Version, not_saved_as}; let tag = |code: i32, value: &str| RawTag { code, value: value.into() }; let solid = |tags: Vec| EntityKind::Unknown(Unknown { dxf_type: "3DSOLID".into(), tags }); let mut d = Drawing::new_imperial(); d.add(&Space::Model, Common::default(), solid(vec![tag(100, "AcDbModelerGeometry"), tag(1, "400 1 1 0")])).unwrap(); d.add(&Space::Model, Common::default(), solid(vec![tag(100, "AcDbModelerGeometry"), tag(70, "1")])).unwrap(); - for v in [Version::R2010, Version::R2018] { - assert_eq!(not_saved_as(&d, v), [("3DSOLID".to_string(), 1)].into_iter().collect(), "{v:?}"); - } + assert_eq!(not_saved_as(&d, Version::R2010), [("3DSOLID".to_string(), 1)].into_iter().collect(), "2010 keeps the SAT-text solid"); + assert_eq!(not_saved_as(&d, Version::R2018), [("3DSOLID".to_string(), 2)].into_iter().collect(), "2018 loses it"); } diff --git a/crates/io/tests/it/acis_data_store.rs b/crates/io/tests/it/acis_data_store.rs index 5bb4bc8ee..f0ad2039c 100644 --- a/crates/io/tests/it/acis_data_store.rs +++ b/crates/io/tests/it/acis_data_store.rs @@ -196,3 +196,24 @@ fn a_data_store_record_stores_only_its_first_entity() { let store = Foreign { data_store: vec![tag(0, "ACDSRECORD"), tag(320, "2B"), tag(320, "2C"), tag(2, "ASM_Data")], ..Foreign::default() }; assert_eq!(store.data_store_entities(), [Handle(0x2B)].into()); } + +/// A real DWG round trip of a SAT-text REGION: the DWG writer keeps the SAT groups up to 2010, and +/// from 2013 they come back empty (the solid's geometry is not in the file), which the DWG save +/// note has to say (#2334). +#[test] +fn a_dwg_keeps_a_sat_text_region_only_before_2013() { + let sat = "0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1015\n0\nENDSEC\n0\nSECTION\n2\nENTITIES\n\ + 0\nREGION\n5\n2B\n100\nAcDbEntity\n8\n0\n100\nAcDbModelerGeometry\n70\n1\n1\n400 1 1 0\n3\nEnd-of-ACIS-data\n0\nENDSEC\n0\nEOF\n"; + let d = cadcraft_io::read_dxf(sat.as_bytes()).unwrap(); + let holds_sat = |v: cadcraft_io::Version| { + let dwg = cadcraft_io::write_with(&d, "x.dwg", &cadcraft_io::SaveOptions { version: Some(v), binary: false }).unwrap(); + let back = cadcraft_io::read(&dwg, "x.dwg").unwrap(); + let region = back.model.iter().find_map(|e| match &e.kind { + EntityKind::Unknown(u) if u.dxf_type == "REGION" => Some(u.clone()), + _ => None, + }); + region.is_some_and(|u| cadcraft_doc::foreign::has_sat_text(&u.tags)) + }; + assert!(holds_sat(cadcraft_io::Version::R2010), "2010 keeps the SAT text"); + assert!(!holds_sat(cadcraft_io::Version::R2018), "2018 comes back without it"); +} From 57e6a89d10a8bff40391fcfa0a697d71ea8ab26d Mon Sep 17 00:00:00 2001 From: SBaksa Date: Sat, 31 Oct 2026 12:31:34 +1100 Subject: [PATCH 018/100] Parity checklist: regenerated after merging main --- docs/parity-checklist.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index 53ffffc96..f842c51a9 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -1,10 +1,10 @@ # Parity checklist -> **Generated:** 2026-10-30 by `cargo xtask parity`. Do not edit by hand. +> **Generated:** 2026-10-31 by `cargo xtask parity`. Do not edit by hand. Menu-breadth parity: a reference menu item counts as covered when a CADCraft command is registered under the same menu label. It measures breadth, not depth; see [target-app-parity.md](target-app-parity.md) for the weighted assessment. -**Menu breadth: 298 / 491 items (61%)** · 444 registered commands. +**Menu breadth: 298 / 491 items (61%)** · 446 registered commands. | Menu | Items | Covered | % | |---|---|---|---| From 5caaf73e3b065856f38e9cb58571e68edfbd2a59 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 31 Oct 2026 18:52:40 -0400 Subject: [PATCH 019/100] packaging(flatpak): add OCI image build and GHCR publishing helper (#2577) --- packaging/linux/flatpak-bundle.sh | 8 ++++ packaging/linux/flatpak-oci.sh | 64 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100755 packaging/linux/flatpak-oci.sh diff --git a/packaging/linux/flatpak-bundle.sh b/packaging/linux/flatpak-bundle.sh index 5154fe076..5c84b8470 100755 --- a/packaging/linux/flatpak-bundle.sh +++ b/packaging/linux/flatpak-bundle.sh @@ -20,10 +20,12 @@ APP_ID=ai.storyteller.cadcraft FLATHUB=https://dl.flathub.org/repo/flathub.flatpakrepo TEST=1 +OCI=0 TARBALL="" while [[ $# -gt 0 ]]; do case "$1" in --no-test) TEST=0; shift ;; + --oci) OCI=1; shift ;; -h | --help) sed -n '2,14p' "$0"; exit 0 ;; -*) echo "unknown argument: $1" >&2; exit 2 ;; *) TARBALL="$1"; shift ;; @@ -61,6 +63,12 @@ OUT="$DIST/$BASENAME.flatpak" # --runtime-repo lets `flatpak install` fetch the freedesktop runtime from Flathub if missing. flatpak build-bundle --runtime-repo="$FLATHUB" "$WORK/repo" "$OUT" "$APP_ID" stable echo "wrote $OUT" +if [[ "$OCI" = 1 ]]; then + OUT_OCI="$DIST/$BASENAME-oci" + rm -rf "$OUT_OCI" + flatpak build-bundle --oci "$WORK/repo" "$OUT_OCI" "$APP_ID" stable + echo "wrote OCI layout to $OUT_OCI" +fi if [[ "$TEST" = 1 ]]; then flatpak install --user -y --noninteractive --reinstall "$OUT" diff --git a/packaging/linux/flatpak-oci.sh b/packaging/linux/flatpak-oci.sh new file mode 100755 index 000000000..d2d2e0f25 --- /dev/null +++ b/packaging/linux/flatpak-oci.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash +# Build cadcraft as an OCI container layout and push it to GitHub Container Registry (GHCR) +# for consumption by Flatpak OCI clients (flatpak-oci-specs). +# +# Usage: +# packaging/linux/flatpak-oci.sh [--push] [TARBALL] +# +# Environment variables: +# REGISTRY Target registry image (default: ghcr.io/storytold/cadcraft) +# TAG Target image tag (default: $VERSION-$ARCH, also tags stable-$ARCH) +# GITHUB_TOKEN Token for GHCR authentication when --push is used +set -euo pipefail +# shellcheck source=../env.sh +. "$(dirname "${BASH_SOURCE[0]}")/../env.sh" +HERE="$ROOT/packaging/linux" + +PUSH=0 +TARBALL="" +while [[ $# -gt 0 ]]; do + case "$1" in + --push) PUSH=1; shift ;; + -h | --help) sed -n '2,12p' "$0"; exit 0 ;; + -*) echo "unknown argument: $1" >&2; exit 2 ;; + *) TARBALL="$1"; shift ;; + esac +done + +ARCH="$(uname -m)" +case "$ARCH" in + x86_64) ;; + aarch64 | arm64) ARCH=aarch64 ;; + *) echo "unsupported architecture $ARCH" >&2; exit 2 ;; +esac + +BASENAME="cadcraft-$VERSION-linux-$ARCH" +OUT_OCI="$DIST/$BASENAME-oci" + +echo "==> Building Flatpak OCI layout for $BASENAME" +"$HERE/flatpak-bundle.sh" --oci --no-test "${TARBALL:-}" + +[[ -d "$OUT_OCI" ]] || { echo "error: $OUT_OCI layout not generated" >&2; exit 1; } + +REGISTRY="${REGISTRY:-ghcr.io/storytold/cadcraft}" +TAG="${TAG:-$VERSION-$ARCH}" + +echo "==> OCI layout ready at $OUT_OCI (Target: $REGISTRY:$TAG)" + +if [[ "$PUSH" = 1 ]]; then + command -v skopeo >/dev/null || { echo "error: skopeo is required to push OCI images (apt install skopeo)" >&2; exit 1; } + + AUTH_ARGS=() + if [[ -n "${GITHUB_TOKEN:-}" ]]; then + AUTH_ARGS=(--dest-creds "${GITHUB_ACTOR:-storytold}:$GITHUB_TOKEN") + fi + + echo "==> Pushing to $REGISTRY:$TAG" + skopeo copy --format oci "${AUTH_ARGS[@]}" "oci:$OUT_OCI" "docker://$REGISTRY:$TAG" + + # Also update architecture-specific stable tag + echo "==> Pushing to $REGISTRY:stable-$ARCH" + skopeo copy --format oci "${AUTH_ARGS[@]}" "oci:$OUT_OCI" "docker://$REGISTRY:stable-$ARCH" + + echo "==> Push complete" +fi From 9c84141e3899bd17a5747e578b11bcbf91ad28e1 Mon Sep 17 00:00:00 2001 From: midasmatterhorn <248507632+midasmatterhorn@users.noreply.github.com> Date: Sun, 1 Nov 2026 04:56:59 +0300 Subject: [PATCH 020/100] style: rustfmt the write_atomic map_err in render --- crates/mcp/src/backend.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/mcp/src/backend.rs b/crates/mcp/src/backend.rs index d27012418..4ed805979 100644 --- a/crates/mcp/src/backend.rs +++ b/crates/mcp/src/backend.rs @@ -395,8 +395,7 @@ impl Backend for Headless { let png = cadcraft_render::raster::render_png(&list, &view, &cadcraft_render::raster::RasterOptions::default()).ok_or("render failed")?; if let Some(path) = str_p("path") { - cadcraft_engine::atomic_write::write_atomic(std::path::Path::new(&path), &png) - .map_err(|e| format!("{path}: {e}"))?; + cadcraft_engine::atomic_write::write_atomic(std::path::Path::new(&path), &png).map_err(|e| format!("{path}: {e}"))?; } Ok(json!({"pngBase64": cadcraft_engine::cmd::file::base64_encode(&png), "width": w, "height": h})) } From d7ef7da872fbd4158a5b2fc493f2da191f461d03 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 31 Oct 2026 23:43:11 -0400 Subject: [PATCH 021/100] ci: grant packages write permission to release workflow --- .github/workflows/release.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 368d4f322..dc9c462db 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -62,6 +62,7 @@ jobs: permissions: # The reusable release workflow tags and uploads the draft release. contents: write + packages: write # Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos. uses: storytold/craft-common-actions/.github/workflows/release.yml@main with: From 258c89fbb021fd22cf25ef519e5caf018b692093 Mon Sep 17 00:00:00 2001 From: Zinadin Zidan Date: Sun, 1 Nov 2026 18:11:08 +0600 Subject: [PATCH 022/100] Window opens centred; the Chinese menus have no English entries left (#1129) (#2510) * Window opens centred; the Chinese menus have no English entries left (#1129) The main window opened where the system cascades new windows, off to the top-left on wide screens; it now opens centred. 26 menu and command labels the Simplified Chinese update missed (Multileader > Collect, Block Editor, dynamic blocks, Express Tools, Customize, ...) are translated, and a test keeps every menu, command, tool and snap label translated in Chinese. The start page question in the issue is left to the owner. Co-Authored-By: Claude Opus 5.5 * File > Open Recent's Clear Recent Files is translated in Chinese (#1129) The Open Recent submenu is built at run time, so the Chinese completeness test never saw its labels, and Clear Recent Files showed in English. The label is now menus::CLEAR_RECENT, the test collects it with OPEN_RECENT, and zh.tsv translates it. The other runtime-built menu labels (context and tab menus) are all in zh.tsv already. Co-Authored-By: Claude Opus 5.5 * i18n(zh): translate Change and Units, which landed after this branch (#1129) --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> --- apps/cadcraft/src/main.rs | 52 +++++++++++++++++++++----------- crates/ui-egui/src/i18n/tests.rs | 9 ++++++ crates/ui-egui/src/i18n/zh.tsv | 31 +++++++++++++++++++ crates/ui-egui/src/menus.rs | 4 ++- 4 files changed, 78 insertions(+), 18 deletions(-) diff --git a/apps/cadcraft/src/main.rs b/apps/cadcraft/src/main.rs index 4d7bdc39b..c591c10b3 100644 --- a/apps/cadcraft/src/main.rs +++ b/apps/cadcraft/src/main.rs @@ -194,6 +194,31 @@ fn filters_for_save(name: &str) -> [(&'static str, &'static str); 6] { filters } +/// The main window: its size, title bar, placement and rendering needs. +fn window_options() -> eframe::NativeOptions { + eframe::NativeOptions { + viewport: egui::ViewportBuilder::default() + .with_title("CADCraft") + .with_inner_size([1600.0, 1000.0]) + .with_min_inner_size([900.0, 560.0]) + .with_drag_and_drop(true) + .with_fullsize_content_view(true) + .with_titlebar_shown(false) + .with_title_shown(false) + .with_app_id(APP_ID), + persist_window: false, + // The window opens centred on its screen, not cascaded from the top-left corner (#1129). + // Wayland compositors place windows themselves. + centered: true, + // CADCraft installs its own capped font chain (`theme::install_fonts`); never run + // system-font discovery as a hidden fallback. + system_font_fallback: false, + // A stencil buffer clips the contents of clipped layout viewports on the GPU. + stencil_buffer: STENCIL_BITS, + ..Default::default() + } +} + const APP_ID: &str = "ai.storyteller.cadcraft"; /// Stencil bits of the window's render pass (see `NativeOptions::stencil_buffer`). @@ -256,22 +281,7 @@ fn main() -> eframe::Result { // The crosshair is drawn by the app: present without a queue on Linux/BSD, where X11 // swapchains hold two frames back under vsync (`CADCRAFT_VSYNC=1` restores vsync). wgpu_options: eframe::egui_wgpu::WgpuConfiguration::default().with_surface_config(surface), - viewport: egui::ViewportBuilder::default() - .with_title("CADCraft") - .with_inner_size([1600.0, 1000.0]) - .with_min_inner_size([900.0, 560.0]) - .with_drag_and_drop(true) - .with_fullsize_content_view(true) - .with_titlebar_shown(false) - .with_title_shown(false) - .with_app_id(APP_ID), - persist_window: false, - // CADCraft installs its own capped font chain (`theme::install_fonts`); never run - // system-font discovery as a hidden fallback. - system_font_fallback: false, - // A stencil buffer clips the contents of clipped layout viewports on the GPU. - stencil_buffer: STENCIL_BITS, - ..Default::default() + ..window_options() }; if let Some(icon) = app_icon() { options.viewport = options.viewport.with_icon(icon); @@ -383,7 +393,7 @@ fn main() -> eframe::Result { #[cfg(test)] mod tests { - use super::{filters_for_save, follow_desktop}; + use super::{filters_for_save, follow_desktop, window_options}; #[test] fn save_dialog_offers_dwg_with_the_drawing_format_first() { @@ -401,6 +411,14 @@ mod tests { } } + /// #1129: the window opens centred, not cascaded from the top-left corner. + #[test] + fn the_window_opens_centred() { + let o = window_options(); + assert!(o.centered, "centred on launch"); + assert!(!o.persist_window, "no saved position to restore instead"); + } + /// View ▸ Theme ▸ System on Linux: winit reports no theme, so the desktop's answer stands in; /// without one (or where winit answers and the desktop doesn't) the input is left alone. #[test] diff --git a/crates/ui-egui/src/i18n/tests.rs b/crates/ui-egui/src/i18n/tests.rs index dc4e5322b..034f261e1 100644 --- a/crates/ui-egui/src/i18n/tests.rs +++ b/crates/ui-egui/src/i18n/tests.rs @@ -63,6 +63,8 @@ fn missing_commands_menus_tools_and_snap_labels(language: &str) -> Vec { let catalog = catalog(language).unwrap(); let mut sources = BTreeSet::new(); sources.extend(crate::menus::MENUS.iter().copied()); + // File ▸ Open Recent is built at run time, not from a command's menu path. + sources.extend([crate::menus::OPEN_RECENT, crate::menus::CLEAR_RECENT]); for c in cadcraft_engine::command_specs() { sources.insert(c.label); sources.extend(c.menu.iter().copied()); @@ -560,6 +562,13 @@ fn production_font_chain_covers_latin_and_shortcut_symbols() { }); } +/// #1129: in Chinese every menu, submenu, command, tool palette and object-snap label is +/// translated, so no menu shows English entries. +#[test] +fn chinese_menus_and_commands_are_fully_translated() { + assert_eq!(missing_commands_menus_tools_and_snap_labels("zh"), Vec::::new()); +} + /// Bosnian counts: 1, 21 `one`; 2–4, 22–24 (not 12–14) `few`; everything else `other`. #[test] fn bosnian_counts_take_one_few_other() { diff --git a/crates/ui-egui/src/i18n/zh.tsv b/crates/ui-egui/src/i18n/zh.tsv index 4d8da1698..2663105a2 100644 --- a/crates/ui-egui/src/i18n/zh.tsv +++ b/crates/ui-egui/src/i18n/zh.tsv @@ -1349,3 +1349,34 @@ Check for Updates… 检查更新… CADCraft is up to date. CADCraft 已是最新版本。 Could not check for updates. 无法检查更新。 +# Menu and command labels the October 2026 update missed (#1129). + Align Multileaders 对齐多重引线 + Analyze Usage 分析使用情况 + Break-line Symbol 折断线符号 + Change 更改 + Change Properties 修改特性 + Clear Recent Files 清除最近的文件 + Close Block Editor 关闭块编辑器 + Collect 合并 + Collect Multileaders 合并多重引线 + Convert Text to Mtext 将文字转换为多行文字 + Customize 自定义 + Drag Dynamic Grip 拖动动态夹点 + Dynamic Block Data 动态块数据 + Dynamic Block Grips 动态块夹点 + Dynamic Block Reference 动态块参照 + Edit Command Aliases (PGP)... 编辑命令别名 (PGP)... + Edit Multileader 编辑多重引线 + Evaluate LISP 求值 LISP + Explode Attributes to Text 将属性分解为文字 + Express Tools 扩展工具 + Groups 编组 + Hatch Pick 拾取图案填充 + Implied Loops 隐含环 + Justify 对正 + Move/Copy/Rotate 移动/复制/旋转 + Save Block 保存块 + Save Block As 块另存为 + Save Layout as Template 将布局另存为样板 + Set Dynamic Property 设置动态特性 + Units 单位 diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index bf0468d08..55d058811 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -10,6 +10,8 @@ pub const MENUS: &[&str] = &["File", "Edit", "View", "Insert", "Format", "Tools" /// File ▸ Open Recent: one `ui.recent.` item per recent file, then Clear Recent Files. pub const OPEN_RECENT: &str = "Open Recent"; +/// The last item of File ▸ Open Recent. +pub const CLEAR_RECENT: &str = "Clear Recent Files"; const RECENT_ITEM: &str = "ui.recent."; const RECENT_CLEAR: &str = "ui.recent.clear"; @@ -634,7 +636,7 @@ pub fn recent_entries(app: &CadApp) -> Vec { .map(|(i, path)| Entry::Item { label: file_name(path), id: format!("{RECENT_ITEM}{i}"), shortcut: None, enabled: true }) .collect(); let enabled = !app.ui.recent_files.is_empty(); - entries.push(Entry::Item { label: "Clear Recent Files".into(), id: RECENT_CLEAR.into(), shortcut: None, enabled }); + entries.push(Entry::Item { label: CLEAR_RECENT.into(), id: RECENT_CLEAR.into(), shortcut: None, enabled }); entries } From 7400119120a667d662a3f475372403f97d67b75d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 1 Nov 2026 12:30:06 -0500 Subject: [PATCH 023/100] docs: sync Craft App Suite list with ArtCraft Launcher --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c45b619dc..a24b33980 100644 --- a/README.md +++ b/README.md @@ -377,7 +377,15 @@ stand on its own. | | **PdfCraft** | Reading, organizing and protecting PDFs | [GitHub](https://github.com/storytold/pdfcraft) | [Website](https://getartcraft.com/apps/pdfcraft) | | | **EffectCraft** | Motion graphics and visual effects | [GitHub](https://github.com/storytold/effectcraft) | [Website](https://getartcraft.com/apps/effectcraft) | | | **DesignCraft** | Page layout and publishing | [GitHub](https://github.com/storytold/designcraft) | [Website](https://getartcraft.com/apps/designcraft) | -| | **CADCraft** | **Computer-aided design and drafting · you are here** | [GitHub](https://github.com/storytold/cadcraft) | [Website](https://getartcraft.com/apps/cadcraft) | +| | **FigCraft** | Interface design and prototyping | [GitHub](https://github.com/storytold/figcraft) | [Website](https://getartcraft.com/apps/figcraft) | +| | **WordCraft** | Word processing and documents | [GitHub](https://github.com/storytold/wordcraft) | [Website](https://getartcraft.com/apps/wordcraft) | +| | **SoundCraft** | Audio recording and editing | [GitHub](https://github.com/storytold/soundcraft) | [Website](https://getartcraft.com/apps/soundcraft) | +| | **DeckCraft** | Presentations and slide decks | [GitHub](https://github.com/storytold/deckcraft) | [Website](https://getartcraft.com/apps/deckcraft) | +| | **CADCraft** | Computer-aided design | [GitHub](https://github.com/storytold/cadcraft) | [Website](https://getartcraft.com/apps/cadcraft) | +| | **GridCraft** | Spreadsheets and data | [GitHub](https://github.com/storytold/gridcraft) | [Website](https://getartcraft.com/apps/gridcraft) | +| | **TextureCraft** | 3D texture painting | [GitHub](https://github.com/storytold/texturecraft) | [Website](https://getartcraft.com/apps/texturecraft) | +| | **Craft Anything** | See how any software works, then build your own | [GitHub](https://github.com/storytold/craft-anything) | [Website](https://getartcraft.com/apps/craft-anything) | +| | **SolidCraft** | Parametric 3D CAD by Tangency Labs (community app) | [GitHub](https://github.com/tangency-labs/solidcraft) | | And [**ArtCraft**](https://getartcraft.com/) itself, our AI image and video studio for artists who want real control. From 4b4ec8043ec6e378e1cbe238a56b7bab793ae5d0 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 1 Nov 2026 13:59:24 -0500 Subject: [PATCH 024/100] Release 0.16.0 Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 32 ++++++++++++++++---------------- Cargo.toml | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c45ba071c..aa4e1961c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -614,7 +614,7 @@ checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" [[package]] name = "cadcraft" -version = "0.15.1" +version = "0.16.0" dependencies = [ "cadcraft-engine", "cadcraft-io", @@ -643,7 +643,7 @@ dependencies = [ [[package]] name = "cadcraft-cli" -version = "0.15.1" +version = "0.16.0" dependencies = [ "cadcraft-engine", "cadcraft-io", @@ -659,14 +659,14 @@ dependencies = [ [[package]] name = "cadcraft-color" -version = "0.15.1" +version = "0.16.0" dependencies = [ "serde", ] [[package]] name = "cadcraft-constraints" -version = "0.15.1" +version = "0.16.0" dependencies = [ "cadcraft-doc", "cadcraft-geom", @@ -676,7 +676,7 @@ dependencies = [ [[package]] name = "cadcraft-doc" -version = "0.15.1" +version = "0.16.0" dependencies = [ "cadcraft-color", "cadcraft-geom", @@ -687,14 +687,14 @@ dependencies = [ [[package]] name = "cadcraft-dwg" -version = "0.15.1" +version = "0.16.0" dependencies = [ "acadrust", ] [[package]] name = "cadcraft-dxf" -version = "0.15.1" +version = "0.16.0" dependencies = [ "encoding_rs", "log", @@ -703,7 +703,7 @@ dependencies = [ [[package]] name = "cadcraft-engine" -version = "0.15.1" +version = "0.16.0" dependencies = [ "cadcraft-color", "cadcraft-constraints", @@ -724,7 +724,7 @@ dependencies = [ [[package]] name = "cadcraft-fonts" -version = "0.15.1" +version = "0.16.0" dependencies = [ "cadcraft-geom", "harfrust 0.14.0", @@ -734,14 +734,14 @@ dependencies = [ [[package]] name = "cadcraft-geom" -version = "0.15.1" +version = "0.16.0" dependencies = [ "serde", ] [[package]] name = "cadcraft-io" -version = "0.15.1" +version = "0.16.0" dependencies = [ "acadrust", "cadcraft-color", @@ -762,7 +762,7 @@ dependencies = [ [[package]] name = "cadcraft-mcp" -version = "0.15.1" +version = "0.16.0" dependencies = [ "cadcraft-engine", "cadcraft-io", @@ -775,7 +775,7 @@ dependencies = [ [[package]] name = "cadcraft-render" -version = "0.15.1" +version = "0.16.0" dependencies = [ "cadcraft-color", "cadcraft-doc", @@ -789,7 +789,7 @@ dependencies = [ [[package]] name = "cadcraft-ui-egui" -version = "0.15.1" +version = "0.16.0" dependencies = [ "cadcraft-color", "cadcraft-doc", @@ -815,7 +815,7 @@ dependencies = [ [[package]] name = "cadcraft-web" -version = "0.15.1" +version = "0.16.0" dependencies = [ "cadcraft-engine", "cadcraft-io", @@ -5894,7 +5894,7 @@ checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee" [[package]] name = "xtask" -version = "0.15.1" +version = "0.16.0" dependencies = [ "chrono", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 8e16721a3..c6b9590fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"] # default-members [workspace.package] -version = "0.15.1" +version = "0.16.0" edition = "2024" license = "MIT OR Apache-2.0" rust-version = "1.95" From 520394858c679b992038b83087e61316f4eb0a12 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Mon, 2 Nov 2026 02:37:49 +0700 Subject: [PATCH 025/100] -ATTDEF at the command line: attribute modes, AFLAGS, and the text's prompts (#2597) * -ATTDEF at the command line: attribute modes, AFLAGS, and the text's prompts -ATTDEF asked for the tag first. A script's mode answer ("_V", "_C", "_P") became the tag; the attribute kept no mode; AFLAGS never changed. The definition was made at the start point, so the height and rotation a script gives next ran as commands. It now asks as AutoCAD 2024 does: - "Current attribute modes:" from AFLAGS, then "Enter an option to change [Invisible/Constant/Verify/Preset/Lock position/Annotative/ Multiple lines] ", each answer toggling a mode, kept in AFLAGS. - The tag (spaces taken, upper-cased; an empty one gets "Tag cannot be null"), the prompt and default value, or a constant attribute's value. - "Current text style: ...", the point with Justify and Style, the height and the rotation. The definition takes Invisible, Constant, Verify, Preset and Lock position from the modes; a constant attribute stays in its block. Co-Authored-By: Claude Opus 5.5 * -ATTDEF: a typed height is a distance, as TEXT takes it The height was read with str::parse, so 1'6" (or any distance TEXT takes) was refused as "Requires a positive height." It now goes through the shared distance parser, `number`. Co-Authored-By: Claude Opus 5.5 * -ATTDEF: a wrong answer at Justify is asked again A wrong answer at "Enter an option [Left/Center/...]", or Align or Fit (not available here), went on to the point prompt with the justification unchanged, so a script's next answers landed one prompt off. Only a justification goes on to its point now; anything else is asked again. Co-Authored-By: Claude Opus 5.5 * -ATTDEF test: the modes start from AFLAGS, not from the default The other cases start from AFLAGS 16, which is also the default, so reading the modes from the drawing went untested. This starts from 0. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/blocks.rs | 49 +--- crates/engine/src/cmd/blocks/attdef.rs | 293 +++++++++++++++++++++++ crates/engine/src/cmd/draw.rs | 1 + crates/engine/tests/it/attdef_command.rs | 186 ++++++++++++++ crates/engine/tests/it/main.rs | 1 + 5 files changed, 483 insertions(+), 47 deletions(-) create mode 100644 crates/engine/src/cmd/blocks/attdef.rs create mode 100644 crates/engine/tests/it/attdef_command.rs diff --git a/crates/engine/src/cmd/blocks.rs b/crates/engine/src/cmd/blocks.rs index 19543141e..c25b7c913 100644 --- a/crates/engine/src/cmd/blocks.rs +++ b/crates/engine/src/cmd/blocks.rs @@ -17,6 +17,7 @@ use super::{CommandSpec, bad, bool_or, f64_or, find_command, ok, point_param, po use super::has_doc; use crate::{Accept, EngineError, Input, Interactive, Prompt, Result, Session, Step}; +mod attdef; mod attedit; mod burst; mod description; @@ -45,7 +46,7 @@ pub fn specs() -> Vec { .menu(&["Draw", "Block", "Define Attributes..."]) .alias(&["att", "-attdef"]) .params("{tag, prompt?, default?, at, height?, invisible?}") - .interactive(|_| Ok(Box::new(AttdefM::default()))), + .interactive(|_| Ok(Box::new(attdef::AttdefM::default()))), attedit::attedit_spec(), attedit::dash_attedit_spec(), CommandSpec::new("wblock", "Write Block", run_wblock).alias(&["w"]).params("{path, name? | handles?, base?}"), @@ -983,52 +984,6 @@ impl Interactive for InsertM { } } -#[derive(Default)] -struct AttdefM { - tag: Option, - prompt: Option, - default: Option, -} - -impl Interactive for AttdefM { - fn name(&self) -> &'static str { - "ATTDEF" - } - fn prompt(&self, _s: &Session) -> Prompt { - match (&self.tag, &self.prompt, &self.default) { - (None, ..) => Prompt::new("Enter attribute tag name", Accept::TEXT), - (Some(_), None, _) => Prompt::new("Enter attribute prompt", Accept::TEXT), - (Some(_), Some(_), None) => Prompt::new("Enter default attribute value", Accept::TEXT), - _ => Prompt::new("Specify start point of text", Accept::POINT), - } - } - fn input(&mut self, s: &mut Session, i: Input) -> Result { - let text = match &i { - Input::Text(t) => Some(t.clone()), - Input::Enter => Some(String::new()), - _ => None, - }; - match (&self.tag, &self.prompt, &self.default) { - (None, ..) => { - if let Some(t) = text.filter(|t| !t.trim().is_empty() && !t.contains(' ')) { - self.tag = Some(t.to_ascii_uppercase()); - } - } - (Some(_), None, _) => self.prompt = text, - (Some(_), Some(_), None) => self.default = text, - (Some(tag), Some(pr), Some(def)) => { - if let Input::Point(p) = i { - let h = s.doc()?.header.f64("TEXTSIZE", 0.2); - let k = attdef_kind(s, tag, pr, def, p, h, false); - s.add_entity(k)?; - return Ok(Step::Done); - } - } - } - Ok(Step::Continue) - } -} - mod attribs; mod battman; mod blockreplace; diff --git a/crates/engine/src/cmd/blocks/attdef.rs b/crates/engine/src/cmd/blocks/attdef.rs new file mode 100644 index 000000000..0803cfe95 --- /dev/null +++ b/crates/engine/src/cmd/blocks/attdef.rs @@ -0,0 +1,293 @@ +//! -ATTDEF at the command line, as AutoCAD 2024 runs it (accoreconsole): the attribute modes +//! (kept in AFLAGS), the tag, prompt and default value (a constant attribute's value instead), +//! then the text's point, justification, style, height and rotation. + +use cadcraft_doc::{AttribModes, HAlign, VAlign}; + +use super::{Accept, Attrib, EntityKind, Input, Interactive, Prompt, Result, Session, Step, Text, Vec2, number, v3}; +use crate::cmd::curves::KW; +use crate::cmd::draw::justify; + +/// AFLAGS bits, as AutoCAD numbers the modes. +const INVISIBLE: i64 = 1; +const CONSTANT: i64 = 2; +const VERIFY: i64 = 4; +const PRESET: i64 = 8; +const LOCK: i64 = 16; +const MULTILINE: i64 = 32; + +const MODES: [&str; 7] = ["Invisible", "Constant", "Verify", "Preset", "Lock position", "Annotative", "Multiple lines"]; +const JUSTIFY: [&str; 15] = ["Left", "Center", "Right", "Align", "Middle", "Fit", "TL", "TC", "TR", "ML", "MC", "MR", "BL", "BC", "BR"]; + +/// Where -ATTDEF is in its prompts. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +enum Ask { + #[default] + Modes, + Tag, + Prompt, + Value, + Point, + Justify, + Style, + Height, + Rotation, +} + +pub(super) struct AttdefM { + ask: Ask, + flags: i64, + tag: String, + prompt: String, + value: String, + justify: (HAlign, VAlign), + style: String, + at: Vec2, + height: f64, +} + +impl Default for AttdefM { + fn default() -> Self { + AttdefM { + ask: Ask::Modes, + flags: LOCK, + tag: String::new(), + prompt: String::new(), + value: String::new(), + justify: (HAlign::Left, VAlign::Baseline), + style: "Standard".into(), + at: Vec2::ZERO, + height: 0.0, + } + } +} + +impl AttdefM { + fn echo_modes(&self, s: &mut Session) { + let yn = |bit: i64| if self.flags & bit != 0 { "Y" } else { "N" }; + s.echo("Current attribute modes:"); + s.echo(format!( + "Invisible={} Constant={} Verify={} Preset={} Lock position={} Annotative=N Multiple line={}", + yn(INVISIBLE), + yn(CONSTANT), + yn(VERIFY), + yn(PRESET), + yn(LOCK), + yn(MULTILINE) + )); + } + + fn echo_style(&self, s: &mut Session) -> Result<()> { + let th = s.doc()?.header.f64("TEXTSIZE", 0.2); + s.echo(format!("Current text style: \"{}\" Text height: {th:.4} Justify: {}", self.style, justify_name(self.justify))); + Ok(()) + } + + fn point_prompt(&self) -> &'static str { + match self.justify { + (HAlign::Center, VAlign::Baseline) => "Specify center point of text", + (HAlign::Right, VAlign::Baseline) => "Specify right endpoint of text baseline", + (HAlign::Middle, _) | (HAlign::Center, VAlign::Middle) => "Specify middle point of text", + (HAlign::Left, VAlign::Top) => "Specify top-left point of text", + (HAlign::Center, VAlign::Top) => "Specify top-center point of text", + (HAlign::Right, VAlign::Top) => "Specify top-right point of text", + (HAlign::Left, VAlign::Middle) => "Specify middle-left point of text", + (HAlign::Right, VAlign::Middle) => "Specify middle-right point of text", + (HAlign::Left, VAlign::Bottom) => "Specify bottom-left point of text", + (HAlign::Center, VAlign::Bottom) => "Specify bottom-center point of text", + (HAlign::Right, VAlign::Bottom) => "Specify bottom-right point of text", + _ => "Specify start point of text", + } + } + + fn finish(&mut self, s: &mut Session, rotation: f64) -> Result { + let (h, v) = self.justify; + let left = h == HAlign::Left && v == VAlign::Baseline; + s.add_entity(EntityKind::AttDef(Attrib { + tag: self.tag.to_uppercase(), + text: Text { + insert: v3(self.at), + align_pt: if left { None } else { Some(v3(self.at)) }, + height: self.height, + value: std::mem::take(&mut self.value), + rotation, + width_factor: 1.0, + oblique: 0.0, + style: self.style.clone(), + halign: h, + valign: v, + }, + invisible: self.flags & INVISIBLE != 0, + constant: self.flags & CONSTANT != 0, + prompt: std::mem::take(&mut self.prompt), + props: Default::default(), + multiline: None, + modes: AttribModes { verify: self.flags & VERIFY != 0, preset: self.flags & PRESET != 0, lock_position: self.flags & LOCK != 0 }, + }))?; + Ok(Step::Done) + } +} + +/// The justification's name in "Current text style: … Justify: …". +fn justify_name(j: (HAlign, VAlign)) -> &'static str { + JUSTIFY.into_iter().find(|k| justify(k) == Some(j)).unwrap_or("Left") +} + +impl Interactive for AttdefM { + fn name(&self) -> &'static str { + "ATTDEF" + } + fn begin(&mut self, s: &mut Session) -> Result { + let d = s.doc()?; + self.flags = d.header.i64("AFLAGS", LOCK); + self.style = d.header.str("TEXTSTYLE", "Standard"); + self.echo_modes(s); + Ok(Step::Continue) + } + fn prompt(&self, s: &Session) -> Prompt { + match self.ask { + Ask::Modes => Prompt::new("Enter an option to change", KW).kw(&MODES).default("done"), + Ask::Tag => Prompt::new("Enter attribute tag name", Accept::TEXT), + Ask::Prompt => Prompt::new("Enter attribute prompt", Accept::TEXT), + Ask::Value if self.flags & CONSTANT != 0 => Prompt::new("Enter attribute value", Accept::TEXT), + Ask::Value => Prompt::new("Enter default attribute value", Accept::TEXT), + // Once a justification is chosen AutoCAD asks for its point alone. + Ask::Point if self.justify == (HAlign::Left, VAlign::Baseline) => { + Prompt::new(self.point_prompt(), Accept::POINT).kw(&["Justify", "Style"]) + } + Ask::Point => Prompt::new(self.point_prompt(), Accept::POINT), + Ask::Justify => Prompt::new("Enter an option", Accept::TEXT).kw(&JUSTIFY), + Ask::Style => Prompt::new("Enter style name", Accept::TEXT).kw(&["?"]).default(self.style.clone()), + Ask::Height => { + let th = s.doc().map(|d| d.header.f64("TEXTSIZE", 0.2)).unwrap_or(0.2); + Prompt::new("Specify height", Accept::POINT_OR_NUMBER).default(format!("{th:.4}")).base(self.at) + } + Ask::Rotation => Prompt::new("Specify rotation angle of text", Accept::POINT_OR_NUMBER).default("0").base(self.at), + } + } + fn input(&mut self, s: &mut Session, i: Input) -> Result { + match self.ask { + Ask::Modes => match &i { + Input::Keyword(k) => { + let bit = match k.as_str() { + "Invisible" => INVISIBLE, + "Constant" => CONSTANT, + "Verify" => VERIFY, + "Preset" => PRESET, + "Lock position" => LOCK, + _ => { + s.echo(format!("{k} attributes are not available yet.")); + return Ok(Step::Continue); + } + }; + self.flags ^= bit; + s.doc_mut()?.header.set_i64("AFLAGS", self.flags); + self.echo_modes(s); + } + Input::Enter => self.ask = Ask::Tag, + Input::Text(_) => s.echo("Invalid option keyword."), + _ => {} + }, + Ask::Tag => match &i { + // AutoCAD takes spaces in a tag here, and upper-cases it. + Input::Text(t) if !t.trim().is_empty() => { + self.tag = t.trim().to_string(); + self.ask = if self.flags & CONSTANT != 0 { Ask::Value } else { Ask::Prompt }; + } + Input::Text(_) | Input::Enter => s.echo("Tag cannot be null"), + _ => {} + }, + Ask::Prompt | Ask::Value => { + let t = match i { + Input::Text(t) => t, + Input::Enter => String::new(), + _ => return Ok(Step::Continue), + }; + if self.ask == Ask::Prompt { + self.prompt = t; + self.ask = Ask::Value; + } else { + self.value = t; + self.echo_style(s)?; + self.ask = Ask::Point; + } + } + Ask::Point => match i { + Input::Point(p) => { + self.at = p; + self.ask = Ask::Height; + } + Input::Keyword(k) if k == "Justify" => self.ask = Ask::Justify, + Input::Keyword(_) => self.ask = Ask::Style, + _ => {} + }, + Ask::Justify => { + // A justification goes on to its point; anything else is asked again. + if let Input::Keyword(k) | Input::Text(k) = &i { + match justify(k) { + // Aligned and fitted text take two points: not here yet. + Some((HAlign::Aligned | HAlign::Fit, _)) => s.echo("Align and Fit are not available here yet."), + Some(j) => { + self.justify = j; + self.ask = Ask::Point; + } + None => s.echo("Invalid option keyword."), + } + } + } + Ask::Style => { + let name = match i { + Input::Enter => self.style.clone(), + Input::Text(t) | Input::Keyword(t) => t.trim().to_string(), + _ => return Ok(Step::Continue), + }; + if name == "?" { + let names: Vec = s.doc()?.text_styles.iter().map(|st| st.name.clone()).collect(); + s.echo(format!("Text styles: {}", names.join(", "))); + return Ok(Step::Continue); + } + match s.doc()?.text_style(&name).map(|st| st.name.clone()) { + Some(n) => { + self.style = n; + self.echo_style(s)?; + self.ask = Ask::Point; + } + None => s.echo(format!("Cannot find text style \"{name}\".")), + } + } + Ask::Height => { + let th = s.doc()?.header.f64("TEXTSIZE", 0.2); + let h = match &i { + Input::Enter => th, + Input::Point(p) => self.at.dist(*p), + // A distance, as TEXT takes its height (1'6" in feet and inches, too). + Input::Text(t) => number(t).unwrap_or(0.0), + _ => return Ok(Step::Continue), + }; + if !(h.is_finite() && h > 0.0) { + s.echo("Requires a positive height."); + return Ok(Step::Continue); + } + self.height = h; + s.doc_mut()?.header.set_f64("TEXTSIZE", h); + self.ask = Ask::Rotation; + } + Ask::Rotation => { + let r = match &i { + Input::Enter => 0.0, + Input::Point(p) => self.at.angle_to(*p), + Input::Text(t) => match s.angle_settings().direction(t) { + Some(a) => a, + None => { + s.echo("Requires an angle."); + return Ok(Step::Continue); + } + }, + _ => return Ok(Step::Continue), + }; + return self.finish(s, r); + } + } + Ok(Step::Continue) + } +} diff --git a/crates/engine/src/cmd/draw.rs b/crates/engine/src/cmd/draw.rs index 1b0e0c1cf..cebc44e6f 100644 --- a/crates/engine/src/cmd/draw.rs +++ b/crates/engine/src/cmd/draw.rs @@ -33,6 +33,7 @@ use point::{DonutM, PointM, RayM, run_donut, run_point, run_ray}; use polygon::{PolygonM, run_polygon}; use solid::{SolidM, run_solid}; use spline::{SplineM, run_spline}; +pub(crate) use text::justify; use text::{MTextM, TextM, run_mtext, run_text}; /// Every draw command's spec: JSON parameters, menu path, aliases and the interactive prompt diff --git a/crates/engine/tests/it/attdef_command.rs b/crates/engine/tests/it/attdef_command.rs new file mode 100644 index 000000000..7bbec7c37 --- /dev/null +++ b/crates/engine/tests/it/attdef_command.rs @@ -0,0 +1,186 @@ +//! -ATTDEF at the command line, as AutoCAD 2024 runs it (accoreconsole: these scripts with +//! TEXTSIZE 2.5 and AFLAGS 16, the drawing and AFLAGS read back afterwards). + +use cadcraft_engine::doc::{Attrib, EntityKind, HAlign, VAlign}; +use cadcraft_engine::{Session, sysvars}; +use serde_json::json; + +fn session() -> Session { + let mut s = Session::new(); + s.execute("setvar", &json!({ "name": "TEXTSIZE", "value": 2.5 })).unwrap(); + s.execute("setvar", &json!({ "name": "AFLAGS", "value": 16 })).unwrap(); + s +} + +fn attdefs(s: &Session) -> Vec { + s.doc().unwrap().model.iter().filter_map(|e| if let EntityKind::AttDef(a) = &e.kind { Some(a.clone()) } else { None }).collect() +} + +fn aflags(s: &Session) -> i64 { + sysvars::get(s, "AFLAGS").and_then(|v| v.as_f64()).unwrap() as i64 +} + +fn run(script: &str) -> Session { + let mut s = session(); + s.cmdline(script).unwrap(); + assert!(s.running.is_none(), "{script}: {}", s.prompt_text()); + s +} + +#[test] +fn modes_are_toggled_kept_in_aflags_and_stored_on_the_definition() { + // (script, AFLAGS after, invisible, constant, verify, preset, lock position, tag, prompt, value) + type Case = (&'static str, i64, [bool; 5], &'static str, &'static str, &'static str); + let cases: [Case; 5] = [ + // ATTDEF-000: the modes as they are. + ( + "(command \"_.-ATTDEF\" \"\" \"T1\" \"Nhap ten\" \"MAC\" '(5.0 10.0) 2.5 0)", + 16, + [false, false, false, false, true], + "T1", + "Nhap ten", + "MAC", + ), + // ATTDEF-001: Invisible and Verify. + ( + "(command \"_.-ATTDEF\" \"_I\" \"_V\" \"\" \"T2\" \"Hoi hai\" \"D2\" '(5.0 10.0) 3.0 30)", + 21, + [true, false, true, false, true], + "T2", + "Hoi hai", + "D2", + ), + // ATTDEF-002: Constant asks for the value, not a prompt and a default. + ("(command \"_.-ATTDEF\" \"_C\" \"\" \"T3\" \"GIATRI\" '(5.0 10.0) 2.5 0)", 18, [false, true, false, false, true], "T3", "", "GIATRI"), + // ATTDEF-003: Preset. + ( + "(command \"_.-ATTDEF\" \"_P\" \"\" \"T4\" \"Hoi bon\" \"D4\" '(612350.678 2398775.432) 2.5 0)", + 24, + [false, false, false, true, true], + "T4", + "Hoi bon", + "D4", + ), + // ATTDEF-004: Lock position off. + ( + "(command \"_.-ATTDEF\" \"_L\" \"\" \"T5\" \"Hoi nam\" \"D5\" '(5.0 10.0) 2.5 0)", + 0, + [false, false, false, false, false], + "T5", + "Hoi nam", + "D5", + ), + ]; + for (script, flags, [inv, con, ver, pre, lock], tag, prompt, value) in cases { + let s = run(script); + assert_eq!(aflags(&s), flags, "{script}"); + let a = attdefs(&s).remove(0); + assert_eq!((a.invisible, a.constant, a.modes.verify, a.modes.preset, a.modes.lock_position), (inv, con, ver, pre, lock), "{script}"); + assert_eq!((a.tag.as_str(), a.prompt.as_str(), a.text.value.as_str()), (tag, prompt, value), "{script}"); + } + // ATTDEF-001's height 3 and 30 degrees. + let a = attdefs(&run(cases[1].0)).remove(0); + assert!((a.text.height - 3.0).abs() < 1e-12 && (a.text.rotation - 30f64.to_radians()).abs() < 1e-12, "{a:?}"); +} + +#[test] +fn tags_justification_and_style_as_autocad_takes_them() { + // ATTDEF-021, ATTDEF-022: a tag is upper-cased, spaces and all. + for (typed, tag) in [("maso", "MASO"), ("ten ban ve", "TEN BAN VE")] { + let s = run(&format!("(command \"_.-ATTDEF\" \"\" \"{typed}\" \"Hoi\" \"D\" '(5.0 10.0) 2.5 0)")); + assert_eq!(attdefs(&s)[0].tag, tag); + } + // ATTDEF-023: an empty prompt stays empty. + let s = run("(command \"_.-ATTDEF\" \"\" \"T10\" \"\" \"D10\" '(612350.678 2398775.432) 2.5 0)"); + assert_eq!(attdefs(&s)[0].prompt, ""); + // ATTDEF-025: an empty tag is refused and asked again. + let s = run("(command \"_.-ATTDEF\" \"\" \"\" \"T11\" \"Hoi muoi mot\" \"D11\" '(5.0 10.0) 2.5 0)"); + assert!(s.log.iter().any(|l| l == "Tag cannot be null"), "{:?}", s.log); + assert_eq!(attdefs(&s)[0].tag, "T11"); + // ATTDEF-005: Justify MC at the middle point. + let s = run("(command \"_.-ATTDEF\" \"\" \"T6\" \"Hoi sau\" \"D6\" \"_J\" \"_MC\" '(5.0 10.0) 2.5 0)"); + let a = attdefs(&s).remove(0); + assert_eq!((a.text.halign, a.text.valign), (HAlign::Center, VAlign::Middle)); + assert!(a.text.align_pt.is_some_and(|p| p.x == 5.0 && p.y == 10.0), "{a:?}"); + // ATTDEF-006: Style, then the point. + let s = run("(command \"_.-ATTDEF\" \"\" \"T7\" \"Hoi bay\" \"D7\" \"_S\" \"Standard\" '(0.505 0.26) 2.5 0)"); + let a = attdefs(&s).remove(0); + assert!(a.text.style == "Standard" && a.text.insert.x == 0.505 && a.text.insert.y == 0.26, "{a:?}"); +} + +#[test] +fn the_prompts_are_autocads() { + let mut s = session(); + s.cmdline("-attdef").unwrap(); + assert!(s.log.iter().any(|l| l == "Current attribute modes:"), "{:?}", s.log); + assert_eq!(s.log.last().map(String::as_str), Some("Invisible=N Constant=N Verify=N Preset=N Lock position=Y Annotative=N Multiple line=N")); + assert!(s.prompt_text().contains("[Invisible/Constant/Verify/Preset/Lock position/Annotative/Multiple lines] :"), "{}", s.prompt_text()); + s.cmdline("X").unwrap(); + assert_eq!(s.log.last().map(String::as_str), Some("Invalid option keyword.")); + s.cmdline("_C").unwrap(); + assert_eq!(s.log.last().map(String::as_str), Some("Invisible=N Constant=Y Verify=N Preset=N Lock position=Y Annotative=N Multiple line=N")); + s.cmdline("").unwrap(); + assert!(s.prompt_text().ends_with("Enter attribute tag name:"), "{}", s.prompt_text()); + s.cmdline("T3").unwrap(); + assert!(s.prompt_text().ends_with("Enter attribute value:"), "{}", s.prompt_text()); + s.cmdline("GIATRI").unwrap(); + assert_eq!(s.log.last().map(String::as_str), Some("Current text style: \"Standard\" Text height: 2.5000 Justify: Left")); + assert!(s.prompt_text().ends_with("Specify start point of text or [Justify/Style]:"), "{}", s.prompt_text()); + s.cmdline("_J").unwrap(); + // A wrong answer, or a justification not available here, is asked again. + for typed in ["X", "_A"] { + s.cmdline(typed).unwrap(); + assert!(s.prompt_text().contains("[Left/Center/Right/Align/Middle/Fit/TL/"), "{typed}: {}", s.prompt_text()); + } + s.cmdline("_MC").unwrap(); + assert!(s.prompt_text().ends_with("Specify middle point of text:"), "{}", s.prompt_text()); + s.cmdline("0,0").unwrap(); + assert!(s.prompt_text().ends_with("Specify height <2.5000>:"), "{}", s.prompt_text()); + s.cmdline("").unwrap(); + assert!(s.prompt_text().ends_with("Specify rotation angle of text <0>:"), "{}", s.prompt_text()); + s.cancel(); +} + +#[test] +fn a_constant_attribute_stays_in_the_block() { + // INSERTATT-011: the constant TC is no attribute of the reference; the other one, TN, is. + let mut s = session(); + s.execute("line", &json!({ "points": [[612345.678, 2398765.432], [612355.678, 2398765.432]] })).unwrap(); + s.cmdline("(setq hl_A (entlast))").unwrap(); + s.cmdline("(progn (command \"_.-ATTDEF\" \"_C\" \"\" \"TC\" \"HANG\" '(612350.678 2398775.432) 2.5 0) (setq hl_a1 (entlast)))").unwrap(); + s.cmdline("(setvar \"AFLAGS\" 16)").unwrap(); + s.cmdline("(progn (command \"_.-ATTDEF\" \"\" \"TN\" \"Hoi N\" \"MN\" '(612350.678 2398785.432) 2.5 0) (setq hl_a2 (entlast)))").unwrap(); + s.cmdline("(command \"_.-BLOCK\" \"B11\" '(612345.678 2398765.432) hl_a1 hl_a2 hl_A \"\")").unwrap(); + s.cmdline("(command \"_.-INSERT\" \"B11\" '(612445.678 2398815.432) 1 1 0 \"XN\")").unwrap(); + assert!(s.running.is_none(), "{}", s.prompt_text()); + let ins = s.doc().unwrap().model.iter().find_map(|e| if let EntityKind::Insert(i) = &e.kind { Some(i.clone()) } else { None }).unwrap(); + let tags: Vec<(String, String)> = ins.attribs.iter().map(|a| (a.tag.clone(), a.text.value.clone())).collect(); + assert_eq!(tags, [("TN".to_string(), "XN".to_string())]); +} + +#[test] +fn a_typed_height_is_a_distance() { + let mut s = session(); + for typed in ["-attdef", "", "T1", "P", "V", "5,10", "x"] { + s.cmdline(typed).unwrap(); + } + assert_eq!(s.log.last().map(String::as_str), Some("Requires a positive height.")); + // Feet and inches, as TEXT takes them. + for typed in ["1'6\"", "0"] { + s.cmdline(typed).unwrap(); + } + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert_eq!(attdefs(&s)[0].text.height, 18.0); +} + +#[test] +fn the_modes_start_from_aflags() { + // AFLAGS 0, not the default 16: Invisible toggled on gives 1, and nothing else. + let mut s = session(); + s.execute("setvar", &json!({ "name": "AFLAGS", "value": 0 })).unwrap(); + s.cmdline("(command \"_.-ATTDEF\" \"_I\" \"\" \"T1\" \"Hoi\" \"D\" '(5.0 10.0) 2.5 0)").unwrap(); + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert_eq!(aflags(&s), 1); + let a = attdefs(&s).remove(0); + assert_eq!((a.invisible, a.modes.lock_position), (true, false)); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 0763ee6eb..88cbefe31 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -5,6 +5,7 @@ mod annotative; mod arc_ctrl_direction; mod array_commands; mod assoc_array; +mod attdef_command; mod attdisp_attsync; mod attedit_prompts; mod attrib_modes; From 210cfc2a1d20317c6f35c587d93afd0fbe68f9aa Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Mon, 2 Nov 2026 02:37:52 +0700 Subject: [PATCH 026/100] -WBLOCK at the command line, as AutoCAD runs it (#2594) * -WBLOCK at the command line, as AutoCAD runs it WBLOCK had only its JSON form, so a script's -WBLOCK did nothing: the objects it should write and erase stayed in the drawing. This adds the command-line command, with AutoCAD 2024's prompts: - "Current settings: Object conversion=Delete", then the output file. A name without .dwg or .dxf gets .dwg. A DXF file asks for its decimal places first. A file that exists asks "... already exists, do you want to replace it? [Yes/No] "; No ends the command. - An existing block by name, "=" (the block named as the file), "*" (the whole drawing), or Enter for objects. A name that isn't a block is answered Block "NAME" not found. and asked again. - For objects: the base point (with mOde), then the objects, which are written and erased. AutoCAD erases them whatever mOde says, and keeps Delete as the setting. Nothing selected writes nothing. Its JSON form, -wblock, erases what it writes. WBLOCK's JSON form moves into the new module unchanged, sharing the writer. The web build refuses -WBLOCK rather than erase objects it can't save. Co-Authored-By: Claude Opus 5.5 * -WBLOCK's JSON form: nothing to write writes no file With no handles, an empty selection or only handles of objects that aren't there, the JSON form wrote an empty drawing. It now refuses with "no objects selected" before writing or erasing anything, as the command line writes nothing when nothing is selected. Co-Authored-By: Claude Opus 5.5 * -WBLOCK: only Yes replaces an existing file At "... already exists, do you want to replace it? [Yes/No] ", any text starting with "y" counted as Yes, so "yodel" replaced the file, and any other text ended the command. Now only the Yes keyword replaces it, No and Enter end the command, and anything else gets "Invalid option keyword." and the prompt again. Co-Authored-By: Claude Opus 5.5 * WBLOCK and -WBLOCK write their file through a temporary file Replacing an existing file truncated it first, so a write that failed part way lost it. The file now goes through atomic_write::write_atomic, as SAVE, EXPORT and autosave write theirs. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/blocks.rs | 61 +--- crates/engine/src/cmd/blocks/insert_file.rs | 2 +- crates/engine/src/cmd/blocks/wblock.rs | 323 ++++++++++++++++++++ crates/engine/tests/dash_wblock.rs | 244 +++++++++++++++ 4 files changed, 572 insertions(+), 58 deletions(-) create mode 100644 crates/engine/src/cmd/blocks/wblock.rs create mode 100644 crates/engine/tests/dash_wblock.rs diff --git a/crates/engine/src/cmd/blocks.rs b/crates/engine/src/cmd/blocks.rs index c25b7c913..81732a3f8 100644 --- a/crates/engine/src/cmd/blocks.rs +++ b/crates/engine/src/cmd/blocks.rs @@ -7,7 +7,7 @@ pub(in crate::cmd) mod purge; use std::sync::Arc; use cadcraft_doc::{Attrib, Block, Entity, EntityKind, HAlign, Handle, Insert, Text, VAlign}; -use cadcraft_geom::{Mat3, Vec2, Vec3}; +use cadcraft_geom::{Vec2, Vec3}; use serde_json::{Value, json}; use super::helpers::v3; @@ -24,6 +24,7 @@ mod description; mod dyngrip; mod extract; pub(crate) mod refedit; +mod wblock; pub fn specs() -> Vec { vec![ @@ -49,7 +50,8 @@ pub fn specs() -> Vec { .interactive(|_| Ok(Box::new(attdef::AttdefM::default()))), attedit::attedit_spec(), attedit::dash_attedit_spec(), - CommandSpec::new("wblock", "Write Block", run_wblock).alias(&["w"]).params("{path, name? | handles?, base?}"), + wblock::wblock_spec(), + wblock::dash_wblock_spec(), CommandSpec::new("base", "Base", run_base).menu(&["Draw", "Block", "Base"]).params("{at: [x,y]}"), CommandSpec::new("blocks.dynamic", "Dynamic Block Data", run_dynamic) .params("{name (a block, or \"*Model_Space\" for a drawing that is a dynamic block's own)} → {parameters, actions, other} | null (not a dynamic block)") @@ -362,61 +364,6 @@ fn run_attdef(s: &mut Session, p: &Value) -> Result { Ok(json!({ "handle": s.add_entity(k)?.hex() })) } -fn run_wblock(s: &mut Session, p: &Value) -> Result { - let path = str_param(p, "path").ok_or_else(|| bad("wblock", "`path` is required"))?.to_string(); - let src = s.doc()?; - let mut d = cadcraft_doc::Drawing::new_imperial(); - // The written drawing keeps the source's settings and styles, as AutoCAD's -WBLOCK does: its - // units ($INSUNITS, $MEASUREMENT), so a block from a metric drawing is not declared in inches - // and scaled on insert, and its defaults (text size, current styles, annotation scales). - d.header = src.header.clone(); - d.layers = src.layers.clone(); - d.linetypes = src.linetypes.clone(); - d.text_styles = src.text_styles.clone(); - d.dim_styles = src.dim_styles.clone(); - d.mleader_styles = src.mleader_styles.clone(); - d.table_styles = src.table_styles.clone(); - d.mline_styles = src.mline_styles.clone(); - d.scales = src.scales.clone(); - d.blocks = src.blocks.clone(); - // The base point becomes the written drawing's origin, its insertion base: by default 0,0 for - // objects and the block's base point for a block (`base` is then in block coordinates). - let base = point_param(p, "base"); - d.header.set("INSBASE", cadcraft_doc::HVal::Point(Vec3::ZERO)); - let m = Mat3::translate(-base.unwrap_or(Vec2::ZERO)); - let ents: Vec = if let Some(name) = str_param(p, "name") { - let b = src.block(name).ok_or_else(|| bad("wblock", format!("no block `{name}`")))?; - let m = Mat3::translate(-base.unwrap_or(b.base.xy())); - b.entities - .iter() - .map(|e| { - let mut e = (**e).clone(); - e.kind.transform(&m); - e - }) - .collect() - } else { - targets(s, p)? - .iter() - .filter_map(|h| src.entity(*h)) - .map(|e| { - let mut e = (**e).clone(); - e.kind.transform(&m); - e - }) - .collect() - }; - for mut e in ents { - e.handle = d.new_handle(); - d.model.push(e); - } - let hooks = super::file::io().ok_or_else(|| bad("wblock", "file formats are not available in this build"))?; - let bytes = (hooks.write)(&d, &path).map_err(|e| bad("wblock", e))?; - #[cfg(not(target_arch = "wasm32"))] - std::fs::write(&path, &bytes).map_err(|e| bad("wblock", format!("{path}: {e}")))?; - Ok(json!({ "path": path, "entities": d.model.len(), "bytes": bytes.len() })) -} - fn run_base(s: &mut Session, p: &Value) -> Result { let at = point_req("base", p, "at")?; s.doc_mut()?.header.set("INSBASE", cadcraft_doc::HVal::Point(v3(at))); diff --git a/crates/engine/src/cmd/blocks/insert_file.rs b/crates/engine/src/cmd/blocks/insert_file.rs index 4d0af93b3..1a90502b5 100644 --- a/crates/engine/src/cmd/blocks/insert_file.rs +++ b/crates/engine/src/cmd/blocks/insert_file.rs @@ -59,7 +59,7 @@ pub(super) fn file_ref(s: &Session, t: &str) -> Option { } /// The file name without folders and extension (either separator, so Windows paths work anywhere). -fn stem(path: &str) -> String { +pub(super) fn stem(path: &str) -> String { let file = path.rsplit(['/', '\\']).next().unwrap_or(path); match file.rsplit_once('.') { Some((stem, _)) if !stem.is_empty() => stem.to_string(), diff --git a/crates/engine/src/cmd/blocks/wblock.rs b/crates/engine/src/cmd/blocks/wblock.rs new file mode 100644 index 000000000..03356269e --- /dev/null +++ b/crates/engine/src/cmd/blocks/wblock.rs @@ -0,0 +1,323 @@ +//! WBLOCK (its JSON form) and -WBLOCK at the command line: objects, a block or the whole drawing +//! written to a drawing file. + +use cadcraft_doc::{Drawing, Entity, Handle}; +use cadcraft_geom::{Mat3, Vec2, Vec3}; +use serde_json::{Value, json}; + +use super::super::curves::KW; +use super::super::machines::{SelOutcome, SelectPhase}; +use super::super::{CommandSpec, bad, point_param, str_param, targets}; +use super::insert_file::stem; +use crate::{Accept, Input, Interactive, Prompt, Result, Session, Step}; + +pub(super) fn wblock_spec() -> CommandSpec { + CommandSpec::new("wblock", "Write Block", run_wblock).alias(&["w"]).params("{path, name? | handles?, base?}") +} + +/// -WBLOCK erases the objects it writes, whatever its mOde option says: AutoCAD 2024 does, and +/// keeps "Object conversion=Delete" as the setting. +pub(super) fn dash_wblock_spec() -> CommandSpec { + CommandSpec::new("-wblock", "Write Block", run_dash_wblock) + .params("{path, name? | handles?, base?} (the objects written are erased)") + .interactive(|_| Ok(Box::new(DashWblockM::default()))) +} + +/// The web build has no file system: -WBLOCK would erase objects it can't save. +const NO_FILES: &str = "files can't be written in the web build"; + +/// What WBLOCK writes. +enum Source<'a> { + /// These objects, `base` becoming the written drawing's origin. + Objects(&'a [Handle], Vec2), + /// A block's objects, from its base point or `base` (in block coordinates). + Block(&'a str, Option), + /// The whole drawing. + Whole, +} + +fn run_wblock(s: &mut Session, p: &Value) -> Result { + let path = str_param(p, "path").ok_or_else(|| bad("wblock", "`path` is required"))?.to_string(); + let base = point_param(p, "base"); + let (entities, bytes) = match str_param(p, "name") { + Some(name) => write(s, "wblock", &path, Source::Block(name, base))?, + None => write(s, "wblock", &path, Source::Objects(&targets(s, p)?, base.unwrap_or(Vec2::ZERO)))?, + }; + Ok(json!({ "path": path, "entities": entities, "bytes": bytes })) +} + +fn run_dash_wblock(s: &mut Session, p: &Value) -> Result { + if cfg!(target_arch = "wasm32") { + return Err(bad("-wblock", NO_FILES)); + } + let path = str_param(p, "path").ok_or_else(|| bad("-wblock", "`path` is required"))?.to_string(); + let base = point_param(p, "base"); + let (entities, bytes) = match str_param(p, "name") { + Some(name) => write(s, "-wblock", &path, Source::Block(name, base))?, + None => { + let hs = targets(s, p)?; + // As at the command line, nothing selected writes no file. + if !hs.iter().any(|h| s.doc().is_ok_and(|d| d.entity(*h).is_some())) { + return Err(bad("-wblock", "no objects selected")); + } + let written = write(s, "-wblock", &path, Source::Objects(&hs, base.unwrap_or(Vec2::ZERO)))?; + erase(s, &hs)?; + written + } + }; + Ok(json!({ "path": path, "entities": entities, "bytes": bytes })) +} + +/// Write `what` to `path` through the io hooks; the objects written and the file's size. The +/// written drawing keeps the source's settings and styles, as AutoCAD's -WBLOCK does: its units +/// ($INSUNITS, $MEASUREMENT), so a block from a metric drawing is not declared in inches and +/// scaled on insert, and its defaults (text size, current styles, annotation scales). +fn write(s: &Session, cmd: &str, path: &str, what: Source) -> Result<(usize, usize)> { + let src = s.doc()?; + // The base point becomes the written drawing's origin: by default 0,0 for objects and the + // block's base point for a block. + let d = match what { + Source::Whole => src.clone(), + Source::Block(name, base) => { + let b = src.block(name).ok_or_else(|| bad(cmd, format!("no block `{name}`")))?; + let m = Mat3::translate(-base.unwrap_or(b.base.xy())); + with_objects(src, b.entities.iter().map(|e| moved(e, &m))) + } + Source::Objects(hs, base) => { + let m = Mat3::translate(-base); + with_objects(src, hs.iter().filter_map(|h| src.entity(*h)).map(|e| moved(e, &m))) + } + }; + let hooks = super::super::file::io().ok_or_else(|| bad(cmd, "file formats are not available in this build"))?; + let bytes = (hooks.write)(&d, path).map_err(|e| bad(cmd, e))?; + // Through a temporary file, as SAVE writes, so a failed write keeps the file it replaces. + #[cfg(not(target_arch = "wasm32"))] + crate::atomic_write::write_atomic(std::path::Path::new(path), &bytes).map_err(|e| bad(cmd, format!("{path}: {e}")))?; + Ok((d.model.len(), bytes.len())) +} + +/// A new drawing of `ents`, with `src`'s settings, styles and blocks; its insertion base 0,0. +fn with_objects(src: &Drawing, ents: impl Iterator) -> Drawing { + let mut d = Drawing::new_imperial(); + d.header = src.header.clone(); + d.layers = src.layers.clone(); + d.linetypes = src.linetypes.clone(); + d.text_styles = src.text_styles.clone(); + d.dim_styles = src.dim_styles.clone(); + d.mleader_styles = src.mleader_styles.clone(); + d.table_styles = src.table_styles.clone(); + d.mline_styles = src.mline_styles.clone(); + d.scales = src.scales.clone(); + d.blocks = src.blocks.clone(); + d.header.set("INSBASE", cadcraft_doc::HVal::Point(Vec3::ZERO)); + for mut e in ents { + e.handle = d.new_handle(); + d.model.push(e); + } + d +} + +fn moved(e: &Entity, m: &Mat3) -> Entity { + let mut e = e.clone(); + e.kind.transform(m); + e +} + +fn erase(s: &mut Session, hs: &[Handle]) -> Result<()> { + let doc = s.doc_mut()?; + for h in hs { + doc.remove_entity(*h); + } + s.set_selection(Vec::new()); + Ok(()) +} + +/// The file -WBLOCK writes for a typed name: as named when it ends in .dwg or .dxf, else with +/// .dwg added, as AutoCAD names a drawing. +fn output_path(typed: &str) -> String { + let t = typed.trim().trim_matches('"'); + let lower = t.to_ascii_lowercase(); + if lower.ends_with(".dwg") || lower.ends_with(".dxf") { t.to_string() } else { format!("{t}.dwg") } +} + +/// Whether a file is there to replace; the web has no file system. +fn exists(path: &str) -> bool { + #[cfg(not(target_arch = "wasm32"))] + { + std::path::Path::new(path).exists() + } + #[cfg(target_arch = "wasm32")] + { + let _ = path; + false + } +} + +/// Where -WBLOCK is in its prompts, as AutoCAD 2024 asks them. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +enum Ask { + #[default] + File, + /// The file exists: "… already exists, do you want to replace it? [Yes/No] ". + Replace, + /// A DXF file's decimal places of accuracy. + Precision, + /// An existing block, `=` (the block named as the file), `*` (the whole drawing), or Enter + /// for objects. + Block, + Base, + /// mOde at the base point prompt: asked, but the objects are erased whatever it says. + Mode, + Objects, +} + +const SAME_NAME: &str = "= (block=output file)"; +const WHOLE: &str = "* (whole drawing)"; + +#[derive(Default)] +struct DashWblockM { + ask: Ask, + path: String, + base: Vec2, + sel: SelectPhase, +} + +impl Interactive for DashWblockM { + fn name(&self) -> &'static str { + "WBLOCK" + } + fn begin(&mut self, s: &mut Session) -> Result { + if cfg!(target_arch = "wasm32") { + s.echo(format!("-WBLOCK: {NO_FILES}.")); + return Ok(Step::Cancel); + } + s.echo("Current settings: Object conversion=Delete"); + Ok(Step::Continue) + } + fn prompt(&self, _s: &Session) -> Prompt { + match self.ask { + Ask::File => Prompt::new("Enter name of output file", Accept::TEXT), + Ask::Replace => Prompt::new(format!("{} already exists, do you want to replace it?", self.path), KW).kw(&["Yes", "No"]).default("N"), + Ask::Precision => Prompt::new("Enter decimal places of accuracy (0 to 16)", Accept::NUMBER).kw(&["Binary", "Preview"]).default("16"), + Ask::Block => Prompt::new("Enter name of existing block", Accept::TEXT).kw(&[SAME_NAME, WHOLE]).default("define new drawing"), + Ask::Base => Prompt::new("Specify insertion base point", Accept::POINT).kw(&["mOde"]), + Ask::Mode => { + Prompt::new("Choose mode for selected object conversion", KW).kw(&["Convert to block", "Retain", "Delete"]).default("Delete") + } + Ask::Objects => self.sel.prompt(), + } + } + fn input(&mut self, s: &mut Session, i: Input) -> Result { + match self.ask { + Ask::File => { + if let Input::Text(t) = &i + && !t.trim().trim_matches('"').is_empty() + { + self.path = output_path(t); + self.ask = if exists(&self.path) { Ask::Replace } else { self.after_file(s) }; + } + Ok(Step::Continue) + } + Ask::Replace => match &i { + // Yes goes on; No, the default, ends the command; anything else is asked again. + Input::Keyword(k) if k == "Yes" => { + self.ask = self.after_file(s); + Ok(Step::Continue) + } + Input::Keyword(_) | Input::Enter => Ok(Step::Done), + _ => { + s.echo("Invalid option keyword."); + Ok(Step::Continue) + } + }, + Ask::Precision => { + match &i { + Input::Enter => self.ask = Ask::Block, + // The file is written at full precision whatever is asked. + Input::Text(t) if t.trim().parse::().is_ok_and(|n| n <= 16) => self.ask = Ask::Block, + Input::Keyword(k) if k == "Binary" => s.echo("Binary DXF output is not available."), + Input::Keyword(_) => {} + _ => s.echo("Requires an integer between 0 and 16."), + } + Ok(Step::Continue) + } + Ask::Block => { + let name = match &i { + Input::Enter => { + self.ask = Ask::Base; + return Ok(Step::Continue); + } + Input::Keyword(k) if k == WHOLE => return self.write(s, Source::Whole), + Input::Keyword(k) if k == SAME_NAME => stem(&self.path), + Input::Text(t) => t.trim().to_string(), + // A point (from a LISP list) is a name too: AutoCAD answers Block "0,0" not found. + Input::Point(p) => format!("{},{}", p.x, p.y), + _ => return Ok(Step::Continue), + }; + if s.doc()?.block(&name).is_none() { + s.echo(format!("Block \"{name}\" not found.")); + return Ok(Step::Continue); + } + self.write(s, Source::Block(&name, None)) + } + Ask::Base => { + match i { + Input::Keyword(_) => self.ask = Ask::Mode, + Input::Enter | Input::Text(_) => s.echo("Point or option keyword required."), + Input::Point(p) => { + self.base = p; + self.sel = SelectPhase::begin(s); + if self.sel.done { + let picked = self.sel.picked.clone(); + return self.write_objects(s, &picked); + } + self.ask = Ask::Objects; + } + _ => {} + } + Ok(Step::Continue) + } + Ask::Mode => { + // Any answer, Enter included, goes back to the base point. + self.ask = Ask::Base; + Ok(Step::Continue) + } + Ask::Objects => match self.sel.feed(s, &i)? { + SelOutcome::More => Ok(Step::Continue), + // Nothing selected: nothing is written. + SelOutcome::Empty => Ok(Step::Done), + SelOutcome::Done(hs) => self.write_objects(s, &hs), + }, + } + } +} + +impl DashWblockM { + /// The prompt after the file name: a DXF file's precision, else the block. + fn after_file(&self, s: &mut Session) -> Ask { + if self.path.to_ascii_lowercase().ends_with(".dxf") { + s.echo("Current DXF settings: Precision= 16 Format= ASCII Preview= No"); + Ask::Precision + } else { + Ask::Block + } + } + + fn write(&self, s: &mut Session, what: Source) -> Result { + if let Err(e) = write(s, "-wblock", &self.path, what) { + s.echo(e.to_string()); + } + Ok(Step::Done) + } + + fn write_objects(&self, s: &mut Session, hs: &[Handle]) -> Result { + if hs.is_empty() { + return Ok(Step::Done); + } + match write(s, "-wblock", &self.path, Source::Objects(hs, self.base)) { + Ok(_) => erase(s, hs)?, + Err(e) => s.echo(e.to_string()), + } + Ok(Step::Done) + } +} diff --git a/crates/engine/tests/dash_wblock.rs b/crates/engine/tests/dash_wblock.rs new file mode 100644 index 000000000..348cde45a --- /dev/null +++ b/crates/engine/tests/dash_wblock.rs @@ -0,0 +1,244 @@ +//! -WBLOCK at the command line, as AutoCAD 2024 runs it (accoreconsole: these scripts, the +//! drawing read back afterwards). It writes objects from a base point and erases them, whatever +//! its mOde option says; a block by name, `=` (the block named as the file) or `*` (the whole +//! drawing); asks before replacing a file; and writes nothing when nothing is selected. + +use cadcraft_engine::Session; +use cadcraft_engine::cmd::file::{IoHooks, set_io}; +use cadcraft_engine::doc::{Drawing, EntityKind, Handle}; +use serde_json::json; + +#[path = "support/temp_dir.rs"] +mod temp_dir; + +use temp_dir::TempDir; + +/// One line per object written: a line's start (where the base point puts it), else its type. +fn write(d: &Drawing, _path: &str) -> Result, String> { + let mut out = String::new(); + for e in d.model.iter() { + match &e.kind { + EntityKind::Line(l) => out.push_str(&format!("LINE {},{}\n", l.a.x, l.a.y)), + k => out.push_str(&format!("{}\n", k.dxf_name())), + } + } + Ok(out.into_bytes()) +} + +/// A session writing through [`write`], with the drawing the measured scripts start from: line A +/// (0,0)-(10,0), circle B at (5,5) radius 3, line C (0,20)-(10,20). +fn drawing() -> (Session, Handle, Handle, Handle) { + set_io(IoHooks { read: |_, _| Err("no reader in this test".into()), write, plot: None }); + let mut s = Session::new(); + let handle = |v: serde_json::Value| Handle::parse_hex(v["handles"][0].as_str().or(v["handle"].as_str()).unwrap()).unwrap(); + let a = handle(s.execute("line", &json!({ "points": [[0, 0], [10, 0]] })).unwrap()); + let b = handle(s.execute("circle", &json!({ "center": [5, 5], "radius": 3 })).unwrap()); + let c = handle(s.execute("line", &json!({ "points": [[0, 20], [10, 20]] })).unwrap()); + (s, a, b, c) +} + +/// A file in a test's own folder, with forward slashes for a LISP string. +fn file(dir: &TempDir, name: &str) -> String { + dir.path_str(name).replace('\\', "/") +} + +fn lisp(s: &mut Session, expr: &str) { + s.cmdline(expr).unwrap(); +} + +fn ent(h: Handle) -> String { + format!("(handent \"{}\")", h.hex()) +} + +fn alive(s: &Session, h: Handle) -> bool { + s.doc().unwrap().entity(h).is_some() +} + +fn read(path: &str) -> Vec { + std::fs::read_to_string(path).unwrap().lines().map(str::to_string).collect() +} + +#[test] +fn objects_are_written_from_the_base_point_and_erased() { + // WBLOCK-000: A and B from (5,5); AutoCAD erases both and keeps C. + let (mut s, a, b, c) = drawing(); + let dir = TempDir::new("dash-wblock"); + let path = file(&dir, "WB00.dwg"); + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{path}\" \"\" '(5.0 5.0) {} {} \"\")", ent(a), ent(b))); + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert_eq!(read(&path), ["LINE -5,-5", "CIRCLE"]); + assert!(!alive(&s, a) && !alive(&s, b) && alive(&s, c)); + assert!(s.log.iter().any(|l| l == "Current settings: Object conversion=Delete"), "{:?}", s.log); +} + +#[test] +fn the_prompts_are_autocads() { + let (mut s, a, _, _) = drawing(); + let dir = TempDir::new("dash-wblock"); + s.cmdline("-wblock").unwrap(); + assert!(s.prompt_text().ends_with("Enter name of output file:"), "{}", s.prompt_text()); + s.cmdline(&file(&dir, "P.dwg")).unwrap(); + assert!( + s.prompt_text().ends_with("Enter name of existing block or [= (block=output file)/* (whole drawing)] :"), + "{}", + s.prompt_text() + ); + s.cmdline("").unwrap(); + assert!(s.prompt_text().ends_with("Specify insertion base point or [mOde]:"), "{}", s.prompt_text()); + // WBLOCK-008: anything but a point or mOde there. + s.cmdline("").unwrap(); + assert_eq!(s.log.last().map(String::as_str), Some("Point or option keyword required.")); + s.cmdline("_O").unwrap(); + assert!(s.prompt_text().ends_with("[Convert to block/Retain/Delete] :"), "{}", s.prompt_text()); + s.cmdline("_R").unwrap(); + assert!(s.prompt_text().ends_with("Specify insertion base point or [mOde]:"), "{}", s.prompt_text()); + s.cmdline("0,0").unwrap(); + assert!(s.prompt_text().ends_with("Select objects:"), "{}", s.prompt_text()); + lisp(&mut s, &ent(a)); + s.cmdline("").unwrap(); + assert!(s.running.is_none()); +} + +#[test] +fn mode_is_asked_but_the_objects_are_erased_whatever_it_says() { + // WBM-01, WBM-02: Retain and Convert to block are taken, yet A is erased (no block reference + // takes its place), and the next -WBLOCK still says "Object conversion=Delete". + for mode in ["_R", "_C"] { + let (mut s, a, b, c) = drawing(); + let dir = TempDir::new("dash-wblock"); + let first = file(&dir, "M01a.dwg"); + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{first}\" \"\" \"_O\" \"{mode}\" '(0.0 0.0) {} \"\")", ent(a))); + assert!(s.running.is_none(), "{mode}: {}", s.prompt_text()); + assert_eq!(read(&first), ["LINE 0,0"]); + assert!(!alive(&s, a) && alive(&s, b) && alive(&s, c), "{mode}"); + assert_eq!(s.doc().unwrap().model.len(), 2, "{mode}: nothing takes A's place"); + let before = s.log.len(); + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{}\" \"\" '(0.0 0.0) {} \"\")", file(&dir, "M01b.dwg"), ent(b))); + assert!(s.log[before..].iter().any(|l| l == "Current settings: Object conversion=Delete"), "{mode}: {:?}", s.log); + assert!(!alive(&s, b) && alive(&s, c), "{mode}"); + } +} + +#[test] +fn nothing_selected_writes_nothing() { + // WBM-05: the command ends; no file, nothing erased. + let (mut s, a, b, c) = drawing(); + let dir = TempDir::new("dash-wblock"); + let path = file(&dir, "M05.dwg"); + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{path}\" \"\" '(0.0 0.0) \"\")")); + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert!(!std::path::Path::new(&path).exists()); + assert!(alive(&s, a) && alive(&s, b) && alive(&s, c)); +} + +#[test] +fn an_existing_file_is_replaced_only_on_yes() { + // WBM-06: No (the default) ends the command; WBM-07: Yes goes on to the block prompt. + let (mut s, a, b, _) = drawing(); + let dir = TempDir::new("dash-wblock"); + let path = file(&dir, "M06.dwg"); + std::fs::write(&path, "old").unwrap(); + s.cmdline("-wblock").unwrap(); + s.cmdline(&path).unwrap(); + assert!(s.prompt_text().ends_with(&format!("{path} already exists, do you want to replace it? [Yes/No] :")), "{}", s.prompt_text()); + // Only Yes replaces it: other text is asked again. + s.cmdline("yodel").unwrap(); + assert_eq!(s.log.last().map(String::as_str), Some("Invalid option keyword.")); + assert!(s.prompt_text().ends_with("[Yes/No] :"), "{}", s.prompt_text()); + s.cmdline("_N").unwrap(); + assert!(s.running.is_none()); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "old"); + // Enter is No too. + s.cmdline("-wblock").unwrap(); + s.cmdline(&path).unwrap(); + s.cmdline("").unwrap(); + assert!(s.running.is_none()); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "old"); + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{path}\" \"_Y\" \"\" '(0.0 0.0) {} \"\")", ent(b))); + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert_eq!(read(&path), ["CIRCLE"]); + assert!(alive(&s, a) && !alive(&s, b)); +} + +#[test] +fn a_block_is_written_by_name_by_equals_or_the_whole_drawing_by_star() { + let (mut s, a, b, c) = drawing(); + let dir = TempDir::new("dash-wblock"); + s.execute("block", &json!({ "name": "WB01K", "base": [5, 5], "handles": [a.hex(), b.hex()], "keep": "delete" })).unwrap(); + // WBLOCK-001: a block by name, from its base point; nothing is erased. + let path = file(&dir, "WB01.dwg"); + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{path}\" \"WB01K\")")); + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert_eq!(read(&path), ["LINE -5,-5", "CIRCLE"]); + assert!(alive(&s, c)); + // WBLOCK-002: "=" is the block named as the file. + let path = file(&dir, "WB01K.dwg"); + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{path}\" \"=\")")); + assert_eq!(read(&path), ["LINE -5,-5", "CIRCLE"]); + // WBLOCK-003: "*" is the whole drawing. + let path = file(&dir, "WB03.dwg"); + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{path}\" \"*\")")); + assert_eq!(read(&path), ["LINE 0,20"]); + assert!(alive(&s, c)); + // WBLOCK-005: a point given there is a name too. + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{}\" '(0.0 0.0))", file(&dir, "WB05.dwg"))); + assert!(s.log.iter().any(|l| l == "Block \"0,0\" not found."), "{:?}", s.log); + s.cancel(); + // WBLOCK-008, WBLOCK-015: a name that isn't a block, typed or by "=", is refused and asked again. + for (answer, name) in [("WB08ZZ", "WB08ZZ"), ("=", "WB15")] { + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{}\" \"{answer}\")", file(&dir, "WB15.dwg"))); + assert!(s.log.iter().any(|l| *l == format!("Block \"{name}\" not found.")), "{answer}: {:?}", s.log); + assert!(s.prompt_text().contains("Enter name of existing block"), "{}", s.prompt_text()); + s.cancel(); + } + assert!(!std::path::Path::new(&file(&dir, "WB15.dwg")).exists()); +} + +#[test] +fn a_name_without_extension_is_a_dwg_and_a_dxf_asks_for_its_precision() { + // WBLOCK-004: no extension. + let (mut s, a, b, _) = drawing(); + let dir = TempDir::new("dash-wblock"); + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{}\" \"\" '(0.0 0.0) {} \"\")", file(&dir, "WB04"), ent(a))); + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert_eq!(read(&file(&dir, "WB04.dwg")), ["LINE 0,0"]); + // WBLOCK-010: a DXF file asks for its decimal places first; Enter takes 16. + let path = file(&dir, "WB10.dxf"); + s.cmdline("-wblock").unwrap(); + s.cmdline(&path).unwrap(); + assert!(s.log.iter().any(|l| l.starts_with("Current DXF settings: Precision= 16 Format= ASCII")), "{:?}", s.log); + assert!(s.prompt_text().ends_with("Enter decimal places of accuracy (0 to 16) or [Binary/Preview] <16>:"), "{}", s.prompt_text()); + s.cancel(); + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{path}\" \"\" \"\" '(0.0 0.0) {} \"\")", ent(b))); + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert_eq!(read(&path), ["CIRCLE"]); + assert!(!alive(&s, b)); +} + +#[test] +fn undo_brings_the_written_objects_back() { + // WBM-09: U after -WBLOCK. + let (mut s, a, b, c) = drawing(); + let dir = TempDir::new("dash-wblock"); + lisp(&mut s, &format!("(command \"_.-WBLOCK\" \"{}\" \"\" '(5.0 5.0) {} {} \"\")", file(&dir, "M09.dwg"), ent(a), ent(b))); + assert!(!alive(&s, a) && !alive(&s, b)); + s.cmdline("u").unwrap(); + assert!(alive(&s, a) && alive(&s, b) && alive(&s, c)); +} + +#[test] +fn the_json_form_of_dash_wblock_erases_what_it_writes() { + let (mut s, a, b, _) = drawing(); + let dir = TempDir::new("dash-wblock"); + s.execute("wblock", &json!({ "path": file(&dir, "keep.dwg"), "handles": [a.hex()] })).unwrap(); + assert!(alive(&s, a), "WBLOCK's JSON form keeps them"); + s.execute("-wblock", &json!({ "path": file(&dir, "erase.dwg"), "handles": [b.hex()], "base": [5, 5] })).unwrap(); + assert_eq!(read(&file(&dir, "erase.dwg")), ["CIRCLE"]); + assert!(!alive(&s, b)); + // Nothing selected writes no file, as at the command line. + let none = file(&dir, "none.dwg"); + assert!(s.execute("-wblock", &json!({ "path": none, "handles": [] })).is_err()); + assert!(s.execute("-wblock", &json!({ "path": none, "handles": [b.hex()] })).is_err(), "erased already"); + assert!(s.execute("-wblock", &json!({ "path": none })).is_err()); + assert!(!std::path::Path::new(&none).exists()); +} From 0e2bff3b2b22c922b03ffa811cd3fc18f1d92d17 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Mon, 2 Nov 2026 02:37:56 +0700 Subject: [PATCH 027/100] LEADER at the command line, as AutoCAD runs it (#2596) LEADER had only its JSON form, so a script's LEADER drew nothing. This adds the command-line command with AutoCAD 2024's prompts: the points, Format (Spline/STraight/Arrow/None) and Undo, then annotation lines, or the options None, Block, Copy, and Mtext and Tolerance (which come back to the first line at the command line, as in accoreconsole). Measured, and followed: - A hook line, a horizontal last segment DIMASZ long, is added when the last segment is straight and more than 15 degrees off horizontal and text follows: 14.9 and 15 degrees keep the end, 15.1 hooks. A spline, no annotation and a block take none. - The text sits DIMGAP past the end on the side the leader runs to: above it, bottom-attached, with DIMTAD on; centred on it with DIMTAD 0. Sizes are DIMSCALE times the style's, with SETVAR overrides. - A copied text keeps its contents, height and width, is centred on its line, and with DIMTAD on the leader ends half the text height lower. The JSON form now draws through the same code. The drawing model has no link from a leader to its annotation, so the two are separate objects. Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/annotate.rs | 6 +- crates/engine/src/cmd/annotate/leader.rs | 330 +++++++++++++++++++++ crates/engine/src/cmd/annotate/mleader.rs | 30 +- crates/engine/tests/it/leader_command.rs | 340 ++++++++++++++++++++++ crates/engine/tests/it/main.rs | 1 + 5 files changed, 678 insertions(+), 29 deletions(-) create mode 100644 crates/engine/src/cmd/annotate/leader.rs create mode 100644 crates/engine/tests/it/leader_command.rs diff --git a/crates/engine/src/cmd/annotate.rs b/crates/engine/src/cmd/annotate.rs index 08f87fc4b..d983ce25d 100644 --- a/crates/engine/src/cmd/annotate.rs +++ b/crates/engine/src/cmd/annotate.rs @@ -16,6 +16,7 @@ use crate::{Accept, EngineError, Input, Interactive, LeaderOrder, MLeaderOptions mod angular; mod common; mod edit; +mod leader; mod linear; mod mleader; mod radial; @@ -88,7 +89,10 @@ pub fn specs() -> Vec { .alias(&["mld"]) .params("{points: [[arrow], ..., [landing]] ([location] with leaderType none), text?, leaderType?: straight|spline|none, block?: name, blockAttach?: center|insertion}") .interactive(|s| Ok(Box::new(MLeaderM::new(s)))), - CommandSpec::new("leader", "Leader", run_leader).alias(&["lead"]).params("{points: [[x,y]...], text?}"), + CommandSpec::new("leader", "Leader", run_leader) + .alias(&["lead"]) + .params("{points: [[x,y]...], text?}") + .interactive(|_| Ok(Box::new(leader::LeaderM::default()))), CommandSpec::new("dimstyle.update", "Update", run_update) .menu(&["Dimension", "Update"]) .params("{handles?}") diff --git a/crates/engine/src/cmd/annotate/leader.rs b/crates/engine/src/cmd/annotate/leader.rs new file mode 100644 index 000000000..1c5ce7276 --- /dev/null +++ b/crates/engine/src/cmd/annotate/leader.rs @@ -0,0 +1,330 @@ +//! LEADER at the command line, as AutoCAD 2024 runs it (accoreconsole, synthetic drawings): the +//! points, Format and Undo, then annotation text, no annotation, or a block. +//! +//! The leader takes a hook line, a horizontal last segment DIMASZ long, when its last segment is +//! straight and more than 15° off horizontal and text follows it; the text sits DIMGAP past the +//! leader's end, above it (DIMTAD on) or centred on it (DIMTAD 0), on the side the leader runs to. +//! All sizes are scaled by DIMSCALE. The text and the leader are separate objects here: the +//! drawing model has no link from a leader to its annotation. + +use super::common::style; +use super::{Accept, EntityKind, Handle, Input, Interactive, MText, Prompt, Result, Session, Step, Vec2, v3}; +use crate::cmd::curves::KW; + +/// Where LEADER is in its prompts. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +enum Ask { + #[default] + Points, + Format, + FirstLine, + NextLine, + Option, + /// Copy: the text or multiline text to copy. + Copy, + BlockName, + BlockPoint, + BlockX, + BlockY, + BlockRotation, +} + +#[derive(Default)] +pub(super) struct LeaderM { + ask: Ask, + pts: Vec, + spline: bool, + no_arrow: bool, + lines: Vec, + block: String, + block_at: Vec2, + block_scale: Vec2, +} + +/// What follows the leader. +enum Annotation { + Text(Vec), + /// A copy of a text or multiline text: its contents, height, width and style. + Copy(MText), + None, + Block { + name: String, + at: Vec2, + scale: Vec2, + rotation: f64, + }, +} + +impl Interactive for LeaderM { + fn name(&self) -> &'static str { + "LEADER" + } + fn prompt(&self, _s: &Session) -> Prompt { + let last = self.pts.last().copied(); + match self.ask { + Ask::Points => match self.pts.len() { + 0 => Prompt::new("Specify leader start point", Accept::POINT), + 1 => Prompt::new("Specify next point", Accept::POINT).base_opt(last), + _ => Prompt::new("Specify next point", Accept::POINT).kw(&["Annotation", "Format", "Undo"]).default("Annotation").base_opt(last), + }, + Ask::Format => Prompt::new("Enter leader format option", KW).kw(&["Spline", "STraight", "Arrow", "None"]).default("Exit"), + // "or ": the default is the options, not a keyword list. + Ask::FirstLine => Prompt::new("Enter first line of annotation text or", Accept::TEXT).default("options"), + Ask::NextLine => Prompt::new("Enter next line of annotation text", Accept::TEXT), + Ask::Copy => Prompt::new("Select an object to copy", Accept::SELECT), + Ask::Option => Prompt::new("Enter an annotation option", KW).kw(&["Tolerance", "Copy", "Block", "None", "Mtext"]).default("Mtext"), + Ask::BlockName => Prompt::new("Enter block name", Accept::TEXT).kw(&["?"]), + Ask::BlockPoint => Prompt::new("Specify insertion point", Accept::POINT).kw(&["Basepoint", "Scale", "X", "Y", "Z", "Rotate"]), + Ask::BlockX => Prompt::new("Enter X scale factor, specify opposite corner", Accept::NUMBER).kw(&["Corner", "XYZ"]).default("1"), + Ask::BlockY => Prompt::new("Enter Y scale factor", Accept::NUMBER).default("use X scale factor"), + Ask::BlockRotation => Prompt::new("Specify rotation angle", Accept::NUMBER).default("0"), + } + } + fn input(&mut self, s: &mut Session, i: Input) -> Result { + match self.ask { + Ask::Points => match i { + Input::Point(p) => self.pts.push(p), + // Enter after one point: no leader. + Input::Enter if self.pts.len() < 2 => return Ok(Step::Done), + Input::Enter => self.ask = Ask::FirstLine, + Input::Keyword(k) => match k.as_str() { + "Annotation" => self.ask = Ask::FirstLine, + "Format" => self.ask = Ask::Format, + _ => { + self.pts.pop(); + } + }, + _ => {} + }, + Ask::Format => { + // Enter, or Exit typed, changes nothing; either way back to the points. + if let Input::Keyword(k) = &i { + match k.as_str() { + "Spline" => self.spline = true, + "STraight" => self.spline = false, + "Arrow" => self.no_arrow = false, + _ => self.no_arrow = true, + } + } + self.ask = Ask::Points; + } + Ask::FirstLine | Ask::NextLine => match i { + Input::Text(t) if !t.is_empty() => { + self.lines.push(t); + self.ask = Ask::NextLine; + } + _ if self.ask == Ask::NextLine => { + let lines = std::mem::take(&mut self.lines); + return self.finish(s, Annotation::Text(lines)); + } + _ => self.ask = Ask::Option, + }, + Ask::Option => match &i { + Input::Keyword(k) if k == "None" => return self.finish(s, Annotation::None), + Input::Keyword(k) if k == "Block" => self.ask = Ask::BlockName, + Input::Keyword(k) if k == "Copy" => self.ask = Ask::Copy, + Input::Text(_) => s.echo("Invalid option keyword."), + // Mtext (the default) and Tolerance open an editor or a dialog in AutoCAD; at the + // command line, as in accoreconsole, they come back to the first line. + _ => self.ask = Ask::FirstLine, + }, + Ask::Copy => { + let picked = match &i { + Input::Pick(hs) => hs.first().copied(), + Input::Point(p) => crate::select::pick(s.doc()?, &s.state()?.edit_space(), *p, s.settings.pickbox.max(1.0) * s.pixel_size()), + _ => None, + }; + let text = picked.and_then(|h| s.doc().ok()?.entity(h).cloned()).and_then(|e| match &e.kind { + EntityKind::Text(t) => Some(MText { + insert: t.insert, + height: t.height, + width: 0.0, + attach: 4, + rotation: 0.0, + style: t.style.clone(), + contents: t.value.clone(), + line_spacing: 1.0, + line_spacing_exact: false, + columns: None, + }), + EntityKind::MText(m) => Some(m.clone()), + _ => None, + }); + match text { + Some(m) => return self.finish(s, Annotation::Copy(m)), + None if picked.is_some() => s.echo("The object is not text."), + None => {} + } + } + Ask::BlockName => { + let name = match i { + Input::Text(t) => t.trim().to_string(), + Input::Keyword(k) => k, + _ => return Ok(Step::Continue), + }; + if name == "?" { + let names: Vec = s.doc()?.blocks.values().filter(|b| !b.anonymous).map(|b| b.name.clone()).collect(); + s.echo(format!("Defined blocks: {}", if names.is_empty() { "(none)".into() } else { names.join(", ") })); + } else if s.doc()?.block(&name).is_none() { + s.echo(format!("Block \"{name}\" not found.")); + } else { + self.block = name; + self.ask = Ask::BlockPoint; + } + } + Ask::BlockPoint => match i { + Input::Point(p) => { + self.block_at = p; + self.ask = Ask::BlockX; + } + Input::Keyword(_) => s.echo("This option is not available here yet."), + _ => {} + }, + Ask::BlockX => match scale(&i) { + Some(x) => { + self.block_scale = Vec2::new(x, x); + self.ask = Ask::BlockY; + } + None => s.echo("Requires a nonzero number."), + }, + Ask::BlockY => { + match scale(&i) { + Some(y) if !matches!(i, Input::Enter) => self.block_scale.y = y, + Some(_) => {} + None => { + s.echo("Requires a nonzero number."); + return Ok(Step::Continue); + } + } + self.ask = Ask::BlockRotation; + } + Ask::BlockRotation => { + let rotation = match &i { + Input::Enter => 0.0, + Input::Text(t) => match s.angle_settings().direction(t) { + Some(a) => a, + None => { + s.echo("Requires a valid angle."); + return Ok(Step::Continue); + } + }, + _ => return Ok(Step::Continue), + }; + let a = Annotation::Block { name: self.block.clone(), at: self.block_at, scale: self.block_scale, rotation }; + return self.finish(s, a); + } + } + Ok(Step::Continue) + } +} + +/// A scale factor typed, or 1 for Enter; `None` for zero or anything that isn't a number. +fn scale(i: &Input) -> Option { + match i { + Input::Enter => Some(1.0), + Input::Text(t) => t.trim().parse::().ok().filter(|v| v.is_finite() && *v != 0.0), + _ => None, + } +} + +impl LeaderM { + fn finish(&mut self, s: &mut Session, ann: Annotation) -> Result { + let pts = std::mem::take(&mut self.pts); + add_leader(s, &pts, self.spline, !self.no_arrow, ann)?; + Ok(Step::Done) + } +} + +/// The dimension settings LEADER draws with, from the current dimension style and DIMSCALE. +struct Sizes { + arrow: f64, + gap: f64, + text: f64, + above: bool, + text_style: String, +} + +fn sizes(s: &Session) -> Sizes { + // The current style with the drawing's DIM* overrides (what SETVAR DIMTXT… sets). + let st = s.doc().ok().and_then(|d| d.dim_style(&style(s)).map(|st| st.with_overrides(&d.dim_overrides()))).unwrap_or_default(); + let k = if st.scale > 0.0 { st.scale } else { 1.0 }; + let text_style = if st.text_style.is_empty() { "Standard".to_string() } else { st.text_style.clone() }; + Sizes { arrow: st.arrow_size * k, gap: st.text_gap.abs() * k, text: st.text_height * k, above: st.text_above != 0, text_style } +} + +/// LEADER's JSON form: a straight leader with an arrow through `pts`, and `text` (lines split at +/// `\n`) placed as the command line places it. The handles of the leader and its text. +pub(super) fn json_leader(s: &mut Session, pts: &[Vec2], text: Option<&str>) -> Result> { + let ann = match text { + Some(t) if !t.is_empty() => Annotation::Text(t.split('\n').map(str::to_string).collect()), + _ => Annotation::None, + }; + add_leader(s, pts, false, true, ann) +} + +/// Draw the leader through `pts` and its annotation, as AutoCAD 2024 places them; their handles. +fn add_leader(s: &mut Session, pts: &[Vec2], spline: bool, arrow: bool, ann: Annotation) -> Result> { + let (Some(&end), Some(&before)) = (pts.last(), pts.len().checked_sub(2).and_then(|i| pts.get(i))) else { return Ok(Vec::new()) }; + let z = sizes(s); + // The side the leader runs to: right unless its last segment runs left. + let dir = if end.x < before.x { -1.0 } else { 1.0 }; + let mut vertices = pts.to_vec(); + if let Annotation::Text(_) | Annotation::Copy(_) = ann { + let d = end - before; + // A copied text is centred on its line, and with DIMTAD on that line is above the leader: + // the leader ends half the text height lower. + if let (Annotation::Copy(_), true, Some(last)) = (&ann, z.above, vertices.last_mut()) { + last.y -= z.text / 2.0; + } + let end = vertices.last().copied().unwrap_or(end); + // More than 15° off horizontal: a hook line DIMASZ long into the end. + if !spline && d.y.abs() > d.x.abs() * 15f64.to_radians().tan() { + let at = vertices.len() - 1; + vertices.insert(at, Vec2::new(end.x - dir * z.arrow, end.y)); + } + } + let leader = s.add_entity(EntityKind::Leader(cadcraft_doc::Leader { + vertices: vertices.iter().map(|p| v3(*p)).collect(), + arrow, + spline, + style: style(s), + }))?; + let annotation = match ann { + Annotation::Text(lines) => { + let (attach, lift) = match (z.above, dir > 0.0) { + (true, true) => (7, z.gap), + (true, false) => (9, z.gap), + (false, true) => (4, 0.0), + (false, false) => (6, 0.0), + }; + Some(s.add_entity(EntityKind::MText(MText { + insert: v3(end + Vec2::new(dir * z.gap, lift)), + height: z.text, + width: 0.0, + attach, + rotation: 0.0, + style: z.text_style, + contents: lines.join("\\P"), + line_spacing: 1.0, + line_spacing_exact: false, + columns: None, + }))?) + } + Annotation::Copy(m) => { + let lift = if z.above { z.gap } else { 0.0 }; + let attach = if dir > 0.0 { 4 } else { 6 }; + Some(s.add_entity(EntityKind::MText(MText { insert: v3(end + Vec2::new(dir * z.gap, lift)), attach, rotation: 0.0, ..m }))?) + } + Annotation::Block { name, at, scale, rotation } => { + match crate::cmd::blocks::insert_with(s, &name, at, scale, rotation, None, &serde_json::Map::new()) { + Ok(h) => Some(h), + Err(e) => { + s.doc_mut()?.remove_entity(leader); + return Err(e); + } + } + } + Annotation::None => None, + }; + Ok(std::iter::once(leader).chain(annotation).collect()) +} diff --git a/crates/engine/src/cmd/annotate/mleader.rs b/crates/engine/src/cmd/annotate/mleader.rs index 536e83b8c..9b4e74128 100644 --- a/crates/engine/src/cmd/annotate/mleader.rs +++ b/crates/engine/src/cmd/annotate/mleader.rs @@ -1,4 +1,3 @@ -use super::common::style; use super::{ Accept, EngineError, EntityKind, Input, Interactive, LeaderOrder, MLeader, MLeaderOptions, MText, Prompt, Result, Session, Step, Value, Vec2, bad, json, points_param, str_param, v3, @@ -57,33 +56,8 @@ pub(super) fn run_leader(s: &mut Session, p: &Value) -> Result { if pts.len() < 2 { return Err(bad("leader", "need 2+ points")); } - let st = style(s); - let h = s.add_entity(EntityKind::Leader(cadcraft_doc::Leader { - vertices: pts.iter().map(|q| v3(*q)).collect(), - arrow: true, - spline: false, - style: st, - }))?; - let mut out = vec![h.hex()]; - if let (Some(t), Some(last)) = (str_param(p, "text"), pts.last()) { - let th = s.doc()?.dim_style(&style(s)).map(|d| d.text_height).unwrap_or(0.18); - out.push( - s.add_entity(EntityKind::MText(MText { - insert: v3(*last + Vec2::new(th * 0.5, th * 0.5)), - height: th, - width: 0.0, - attach: 4, - rotation: 0.0, - style: "Standard".into(), - contents: t.into(), - line_spacing: 1.0, - line_spacing_exact: false, - columns: None, - }))? - .hex(), - ); - } - Ok(json!({ "handles": out })) + let hs = super::leader::json_leader(s, &pts, str_param(p, "text"))?; + Ok(json!({ "handles": hs.iter().map(|h| h.hex()).collect::>() })) } /// The MLEADER Options prompt being answered. diff --git a/crates/engine/tests/it/leader_command.rs b/crates/engine/tests/it/leader_command.rs new file mode 100644 index 000000000..f71c99462 --- /dev/null +++ b/crates/engine/tests/it/leader_command.rs @@ -0,0 +1,340 @@ +//! LEADER at the command line, as AutoCAD 2024 runs it (accoreconsole: these scripts, with +//! DIMTAD 1, DIMGAP 0.625, DIMASZ 2.5, DIMSCALE 1 and DIMTXT 2.5 unless a case sets another, and +//! the drawing read back afterwards). + +use cadcraft_engine::Session; +use cadcraft_engine::doc::{EntityKind, Insert, Leader, MText}; +use serde_json::json; + +fn near(a: f64, b: f64) -> bool { + (a - b).abs() <= 1e-9 * a.abs().max(b.abs()).max(1.0) +} + +/// A drawing as the measured scripts start from: the dimension settings, and a text "XYZ" 2.5 +/// high at (100,40) that the Copy cases copy (`(entlast)` after it). +fn session(setup: &str) -> Session { + let mut s = Session::new(); + for (name, v) in [("DIMTAD", 1.0), ("DIMGAP", 0.625), ("DIMASZ", 2.5), ("DIMSCALE", 1.0), ("DIMTXT", 2.5)] { + s.execute("setvar", &json!({ "name": name, "value": v })).unwrap(); + } + s.execute("text", &json!({ "at": [100, 40], "text": "XYZ", "height": 2.5 })).unwrap(); + s.cmdline("(setq hl_T (entlast))").unwrap(); + if !setup.is_empty() { + s.cmdline(setup).unwrap(); + } + s +} + +fn run(setup: &str, script: &str) -> Session { + let mut s = session(setup); + s.cmdline(script).unwrap(); + assert!(s.running.is_none(), "{script}: {}", s.prompt_text()); + s +} + +fn leaders(s: &Session) -> Vec { + s.doc().unwrap().model.iter().filter_map(|e| if let EntityKind::Leader(l) = &e.kind { Some(l.clone()) } else { None }).collect() +} + +fn mtexts(s: &Session) -> Vec { + s.doc().unwrap().model.iter().filter_map(|e| if let EntityKind::MText(m) = &e.kind { Some(m.clone()) } else { None }).collect() +} + +fn inserts(s: &Session) -> Vec { + s.doc().unwrap().model.iter().filter_map(|e| if let EntityKind::Insert(i) = &e.kind { Some(i.clone()) } else { None }).collect() +} + +fn vertices(l: &Leader) -> Vec<[f64; 2]> { + l.vertices.iter().map(|v| [v.x, v.y]).collect() +} + +fn same_points(got: &[[f64; 2]], want: &[[f64; 2]]) -> bool { + got.len() == want.len() && got.iter().zip(want).all(|(g, w)| near(g[0], w[0]) && near(g[1], w[1])) +} + +/// (case, setup, script, leader vertices, text insertion point, attachment, height, contents) +type Case = (&'static str, &'static str, &'static str, &'static [[f64; 2]], [f64; 2], u8, f64, &'static str); + +#[test] +fn leaders_and_their_text_are_where_autocad_puts_them() { + let cases: [Case; 17] = [ + // A horizontal last segment: no hook line; the text DIMGAP right of and above the end. + ( + "000", + "", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) '(40.0 15.0) \"\" \"ABC\" \"\")", + &[[0.0, 0.0], [20.0, 15.0], [40.0, 15.0]], + [40.625, 15.625], + 7, + 2.5, + "ABC", + ), + // More than 15° off horizontal: a hook line DIMASZ long into the end. + ( + "001", + "", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) '(35.0 30.0) \"\" \"ABC\" \"\")", + &[[0.0, 0.0], [20.0, 15.0], [32.5, 30.0], [35.0, 30.0]], + [35.625, 30.625], + 7, + 2.5, + "ABC", + ), + ( + "002", + "", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) \"\" \"AB\" \"CD\" \"\")", + &[[0.0, 0.0], [17.5, 15.0], [20.0, 15.0]], + [20.625, 15.625], + 7, + 2.5, + "AB\\PCD", + ), + // To the left: the text right-attached, DIMGAP left of the end. + ( + "004", + "", + "(command \"_.LEADER\" '(0.0 0.0) '(-20.0 15.0) \"\" \"ABC\" \"\")", + &[[0.0, 0.0], [-17.5, 15.0], [-20.0, 15.0]], + [-20.625, 15.625], + 9, + 2.5, + "ABC", + ), + // A vertical last segment hooks to the right. + ( + "007-VN", + "", + "(command \"_.LEADER\" '(612345.678 2398765.432) '(612345.678 2398795.432) \"\" \"ABC\" \"\")", + &[[612345.678, 2398765.432], [612343.178, 2398795.432], [612345.678, 2398795.432]], + [612346.303, 2398796.057], + 7, + 2.5, + "ABC", + ), + // 14.9° and 15° keep the end; 15.1° hooks. + ( + "100", + "", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) '(40.0 20.32158853384029) \"\" \"ABC\" \"\")", + &[[0.0, 0.0], [20.0, 15.0], [40.0, 20.32158853384029]], + [40.625, 20.94658853384029], + 7, + 2.5, + "ABC", + ), + ( + "101", + "", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) '(40.0 20.358983848622454) \"\" \"ABC\" \"\")", + &[[0.0, 0.0], [20.0, 15.0], [40.0, 20.358983848622454]], + [40.625, 20.983983848622454], + 7, + 2.5, + "ABC", + ), + ( + "102", + "", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) '(40.0 20.39641415635812) \"\" \"ABC\" \"\")", + &[[0.0, 0.0], [20.0, 15.0], [37.5, 20.39641415635812], [40.0, 20.39641415635812]], + [40.625, 21.02141415635812], + 7, + 2.5, + "ABC", + ), + // DIMTAD 0: centred on the end, left- or right-attached. + ( + "020", + "(setvar \"DIMTAD\" 0)", + "(command \"_.LEADER\" '(0.5 0.25) '(0.52 0.265) '(0.54 0.265) \"\" \"ABC\" \"\")", + &[[0.5, 0.25], [0.52, 0.265], [0.54, 0.265]], + [1.165, 0.265], + 4, + 2.5, + "ABC", + ), + ( + "103-VN", + "(setvar \"DIMTAD\" 0)", + "(command \"_.LEADER\" '(612345.678 2398765.432) '(612325.678 2398780.432) '(612305.678 2398780.432) \"\" \"ABC\" \"\")", + &[[612345.678, 2398765.432], [612325.678, 2398780.432], [612305.678, 2398780.432]], + [612305.053, 2398780.432], + 6, + 2.5, + "ABC", + ), + // DIMGAP, DIMSCALE (hook, gap and height) and DIMTXT. + ( + "021", + "(setvar \"DIMGAP\" 2.0)", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) '(40.0 15.0) \"\" \"ABC\" \"\")", + &[[0.0, 0.0], [20.0, 15.0], [40.0, 15.0]], + [42.0, 17.0], + 7, + 2.5, + "ABC", + ), + ( + "022", + "(setvar \"DIMSCALE\" 2.0)", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) '(35.0 30.0) \"\" \"ABC\" \"\")", + &[[0.0, 0.0], [20.0, 15.0], [30.0, 30.0], [35.0, 30.0]], + [36.25, 31.25], + 7, + 5.0, + "ABC", + ), + ( + "024", + "(setvar \"DIMTXT\" 5.0)", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) '(40.0 15.0) \"\" \"AB\" \"CD\" \"\")", + &[[0.0, 0.0], [20.0, 15.0], [40.0, 15.0]], + [40.625, 15.625], + 7, + 5.0, + "AB\\PCD", + ), + // A spline takes no hook line. + ( + "026", + "", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) \"_F\" \"_S\" '(35.0 30.0) \"\" \"ABC\" \"\")", + &[[0.0, 0.0], [20.0, 15.0], [35.0, 30.0]], + [35.625, 30.625], + 7, + 2.5, + "ABC", + ), + // Undo takes the last point back. + ( + "014", + "", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) '(35.0 30.0) \"_U\" '(40.0 15.0) \"\" \"ABC\" \"\")", + &[[0.0, 0.0], [20.0, 15.0], [40.0, 15.0]], + [40.625, 15.625], + 7, + 2.5, + "ABC", + ), + // Annotation options: Mtext, at the command line, comes back to the first line. + ( + "106", + "", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) \"\" \"\" \"_M\" \"ABC\" \"\")", + &[[0.0, 0.0], [17.5, 15.0], [20.0, 15.0]], + [20.625, 15.625], + 7, + 2.5, + "ABC", + ), + // Copy: centred on its line; with DIMTAD on the leader ends half the text height lower. + ( + "016", + "", + "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) \"\" \"\" \"_C\" hl_T)", + &[[0.0, 0.0], [17.5, 13.75], [20.0, 13.75]], + [20.625, 15.625], + 4, + 2.5, + "XYZ", + ), + ]; + for (case, setup, script, want_vertices, at, attach, height, contents) in cases { + let s = run(setup, script); + let ls = leaders(&s); + assert_eq!(ls.len(), 1, "{case}"); + assert!(same_points(&vertices(&ls[0]), want_vertices), "{case}: {:?}", vertices(&ls[0])); + let ms = mtexts(&s); + assert_eq!(ms.len(), 1, "{case}"); + let m = &ms[0]; + assert!(near(m.insert.x, at[0]) && near(m.insert.y, at[1]), "{case}: {:?}", m.insert); + assert_eq!((m.attach, m.contents.as_str()), (attach, contents), "{case}"); + assert!(near(m.height, height) && m.width == 0.0, "{case}: {} {}", m.height, m.width); + } +} + +#[test] +fn format_sets_the_path_and_the_arrow() { + // LEADER-011 Format None; LEADER-012 None then Arrow; LEADER-013 Spline then STraight. + for (script, spline, arrow) in [ + ("(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) \"_F\" \"_N\" '(40.0 15.0) \"\" \"ABC\" \"\")", false, false), + ("(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) \"_F\" \"_N\" \"_F\" \"_A\" '(40.0 15.0) \"\" \"ABC\" \"\")", false, true), + ("(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) \"_F\" \"_S\" \"_F\" \"_ST\" '(40.0 15.0) \"\" \"ABC\" \"\")", false, true), + ("(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) \"_F\" \"\" '(40.0 15.0) \"\" \"ABC\" \"\")", false, true), + ] { + let l = leaders(&run("", script)).remove(0); + assert_eq!((l.spline, l.arrow), (spline, arrow), "{script}"); + } +} + +#[test] +fn no_annotation_a_block_or_a_copied_multiline_text() { + // LEADER-015: None leaves the leader alone, without a hook line. + let s = run("", "(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) \"\" \"\" \"_N\")"); + assert!(same_points(&vertices(&leaders(&s)[0]), &[[0.0, 0.0], [20.0, 15.0]])); + assert!(mtexts(&s).is_empty()); + // LEADER-107: a block, scale 2 (Y as X), 30°, where it is placed; no hook line. + let mut s = session(""); + s.execute("line", &json!({ "points": [[100, 0], [110, 0]] })).unwrap(); + s.cmdline("(command \"_.-BLOCK\" \"LB107\" '(100.0 0.0) (entlast) \"\")").unwrap(); + s.cmdline("(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) \"\" \"\" \"_B\" \"LB107\" '(50.0 20.0) 2 \"\" 30)").unwrap(); + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert!(same_points(&vertices(&leaders(&s)[0]), &[[0.0, 0.0], [20.0, 15.0]])); + let i = inserts(&s).remove(0); + assert!(i.block == "LB107" && near(i.insert.x, 50.0) && near(i.insert.y, 20.0), "{i:?}"); + assert!(near(i.scale.x, 2.0) && near(i.scale.y, 2.0) && near(i.rotation, 30f64.to_radians()), "{i:?}"); + // LEADER-105: a copied multiline text keeps its width; LEADER-137 to the left. + let mut s = session(""); + s.cmdline("(command \"_.MTEXT\" '(60.0 40.0) '(90.0 30.0) \"MT\" \"\")").unwrap(); + s.cmdline("(command \"_.LEADER\" '(0.0 0.0) '(20.0 15.0) \"\" \"\" \"_C\" (entlast))").unwrap(); + let copy = mtexts(&s).into_iter().find(|m| near(m.insert.x, 20.625)).unwrap(); + assert!(near(copy.width, 30.0) && copy.attach == 4 && copy.contents == "MT", "{copy:?}"); + let s = run("", "(command \"_.LEADER\" '(0.0 0.0) '(-20.0 15.0) \"\" \"\" \"_C\" hl_T)"); + assert!(same_points(&vertices(&leaders(&s)[0]), &[[0.0, 0.0], [-17.5, 13.75], [-20.0, 13.75]])); + let m = mtexts(&s).into_iter().next().unwrap(); + assert!(near(m.insert.x, -20.625) && near(m.insert.y, 15.625) && m.attach == 6, "{m:?}"); +} + +#[test] +fn the_json_form_places_its_text_as_the_command_line_does() { + let mut s = session(""); + let v = s.execute("leader", &json!({ "points": [[0, 0], [20, 15], [35, 30]], "text": "ABC" })).unwrap(); + assert_eq!(v["handles"].as_array().map(Vec::len), Some(2)); + assert!(same_points(&vertices(&leaders(&s)[0]), &[[0.0, 0.0], [20.0, 15.0], [32.5, 30.0], [35.0, 30.0]])); + let m = mtexts(&s).remove(0); + assert!(near(m.insert.x, 35.625) && near(m.insert.y, 30.625) && m.attach == 7 && near(m.height, 2.5), "{m:?}"); +} + +#[test] +fn the_prompts_are_autocads_and_one_point_draws_nothing() { + let mut s = session(""); + s.cmdline("leader").unwrap(); + assert!(s.prompt_text().ends_with("Specify leader start point:"), "{}", s.prompt_text()); + s.cmdline("0,0").unwrap(); + assert!(s.prompt_text().ends_with("Specify next point:"), "{}", s.prompt_text()); + s.cmdline("20,15").unwrap(); + assert!(s.prompt_text().ends_with("Specify next point or [Annotation/Format/Undo] :"), "{}", s.prompt_text()); + s.cmdline("_F").unwrap(); + assert!(s.prompt_text().ends_with("[Spline/STraight/Arrow/None] :"), "{}", s.prompt_text()); + s.cmdline("").unwrap(); + s.cmdline("").unwrap(); + assert!(s.prompt_text().ends_with("Enter first line of annotation text or :"), "{}", s.prompt_text()); + s.cmdline("").unwrap(); + assert!(s.prompt_text().ends_with("[Tolerance/Copy/Block/None/Mtext] :"), "{}", s.prompt_text()); + // LEADER-019: Tolerance, at the command line, comes back to the first line. + s.cmdline("_T").unwrap(); + assert!(s.prompt_text().ends_with("Enter first line of annotation text or :"), "{}", s.prompt_text()); + // An answer that isn't an option is refused; `?` at the block name lists the blocks. + s.cmdline("").unwrap(); + s.cmdline("X").unwrap(); + assert_eq!(s.log.last().map(String::as_str), Some("Invalid option keyword.")); + s.cmdline("_B").unwrap(); + assert!(s.prompt_text().ends_with("Enter block name or [?]:"), "{}", s.prompt_text()); + s.cmdline("?").unwrap(); + assert_eq!(s.log.last().map(String::as_str), Some("Defined blocks: (none)")); + s.cancel(); + // LEADER-139: one point and Enter. + let s = run("", "(command \"_.LEADER\" '(0.0 0.0) \"\")"); + assert!(leaders(&s).is_empty() && mtexts(&s).is_empty()); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 88cbefe31..2aae7c117 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -70,6 +70,7 @@ mod layer_cmdline; mod layer_merge_delete; mod layoff_repeat; mod layout_prompts; +mod leader_command; #[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] mod lisp_corpus; mod malformed_handles; From 4a9371ad2cacd067837d30ff03ec592820fcad91 Mon Sep 17 00:00:00 2001 From: NateMarida Date: Mon, 2 Nov 2026 02:38:00 +0700 Subject: [PATCH 028/100] Thai text: stacked marks, Thai fonts first, MTEXT word wrap (#2591) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Thai text: stacked marks, Thai fonts first, MTEXT word wrap, Windows fonts under WSL Thai text in DWG/DXF files decoded correctly but laid out badly. Vowels and tone marks stacked on one consonant collided (ดิ์), because only right-to-left text went through the shaper. - Thai lines go through the harfrust shaper (complex::needed). Shaped left-to-right glyphs keep their characters (TextGlyph) for PDF text. - Thai fallback fonts: Cordia, Angsana and Browallia first (Thai drawings are laid out with their widths), then IBM Plex Sans Thai, Noto Sans/Serif Thai and the fonts each OS ships. - The web build embeds the craft-fonts Thai regular faces. - MTEXT wraps a run of Thai between words when the run is too wide for the column by itself. Text that fits at its spaces keeps those line breaks, so existing drawings lay out as before. Word breaks come from icu_segmenter's LSTM model (about 410 KB). ICU's dictionary segmenter was not used: after loanwords it breaks inside words, even between a consonant and its tone mark. Breaks inside a character cluster are dropped, and results are cached. - Under WSL the font scan also reads /mnt/c/Windows/Fonts, at the lowest priority, so drawings find the Windows fonts they name (Cordia New). The scan reads entry types from the directory listing instead of a stat per file. * Thai fallback: match older Angsana and Browallia file names; document touched functions Today's Windows ships Angsana New and Browallia New as angsana.ttc and browalia.ttc. Older Windows used single faces (angsa.ttf, angsab.ttf, browa.ttf, ...), which still turn up when fonts are copied to Linux machines. The Thai fallback list now matches the shorter stems `angsa` and `browa`, which cover both. Review feedback on #2591. Doc comments for scan, plain_lines and the paragraph layout's word_width and place. * MTEXT: wrap a Thai run after a first-line indent or a tab A Thai run that fit a line at the left indent, but not the room left after a first-line indent or a tab stop, wasn't split between its words: the first line of a paragraph can't wrap, so it ran out of the column. An empty line now measures the run from where it starts. Review feedback on #2591. Also restores the space in `image = {` in Cargo.toml. * Font scan: take the WSL font folder out of this change Thai text doesn't depend on it: with no Windows or system fonts, the craft-fonts Thai faces (Noto Sans Thai, IBM Plex Sans Thai) stack marks and wrap MTEXT the same way. Reading the Windows host's /mnt/c/Windows/Fonts under WSL is a separate decision, so the scan is back to main's: Linux system and user font folders only. --- Cargo.lock | 33 +++++ Cargo.toml | 3 + crates/fonts/Cargo.toml | 1 + crates/fonts/build.rs | 11 +- crates/fonts/src/complex.rs | 173 +++++++++++++++++++++++++- crates/fonts/src/craft_fonts.rs | 5 +- crates/fonts/src/lib.rs | 8 +- crates/fonts/src/linebreak.rs | 205 +++++++++++++++++++++++++++++++ crates/fonts/src/mtext.rs | 209 ++++++++++++++++++++++++++++---- crates/fonts/src/mtext/para.rs | 30 ++++- crates/fonts/src/ttf.rs | 102 +++++++++++++--- 11 files changed, 726 insertions(+), 54 deletions(-) create mode 100644 crates/fonts/src/linebreak.rs diff --git a/Cargo.lock b/Cargo.lock index aa4e1961c..81a772f6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -728,6 +728,7 @@ version = "0.16.0" dependencies = [ "cadcraft-geom", "harfrust 0.14.0", + "icu_segmenter", "skrifa 0.48.0", "unicode-bidi", ] @@ -1115,6 +1116,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" +[[package]] +name = "core_maths" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77745e017f5edba1a9c1d854f6f3a52dac8a12dd5af5d2f54aecf61e43d80d30" +dependencies = [ + "libm", +] + [[package]] name = "cpufeatures" version = "0.2.17" @@ -2435,6 +2445,29 @@ dependencies = [ "zerovec", ] +[[package]] +name = "icu_segmenter" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82d07aafccd67af15d02512a6adf5896fbc5ed00f2e99b471d2efa14016db3db" +dependencies = [ + "core_maths", + "icu_collections", + "icu_locale_fallback", + "icu_provider", + "icu_segmenter_data", + "potential_utf", + "smallvec", + "utf8_iter", + "zerovec", +] + +[[package]] +name = "icu_segmenter_data" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae293c039020f9ec10710af98d29ce6aa2051486638b49c9a6409f3b4a9e98ad" + [[package]] name = "idna" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index c6b9590fa..b84651eee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,6 +51,9 @@ skrifa = "0.48" # so the graph has both) and the Unicode Bidirectional Algorithm for right-to-left drawing text. harfrust = "0.14" unicode-bidi = "0.3.18" +# Line-break opportunities between Thai words for MTEXT word wrap (Thai writes no spaces between +# words): UAX #14 with ICU4X's compiled-in LSTM word model (not its dictionaries). +icu_segmenter = { version = "~2.3", default-features = false, features = ["compiled_data", "lstm"] } image = { version = "0.25", default-features = false, features = ["png", "jpeg"] } encoding_rs = "0.8.35" # XML for the SVG reader (streaming parser, no entity expansion beyond the five predefined ones) diff --git a/crates/fonts/Cargo.toml b/crates/fonts/Cargo.toml index d2bdddf88..0fc676c7b 100644 --- a/crates/fonts/Cargo.toml +++ b/crates/fonts/Cargo.toml @@ -13,3 +13,4 @@ cadcraft-geom = { workspace = true } skrifa = { workspace = true } harfrust = { workspace = true } unicode-bidi = { workspace = true } +icu_segmenter = { workspace = true } diff --git a/crates/fonts/build.rs b/crates/fonts/build.rs index 6df86f7eb..8f46dcc44 100644 --- a/crates/fonts/build.rs +++ b/crates/fonts/build.rs @@ -3,9 +3,10 @@ //! empty, `CRAFT_FONTS` is empty and the build is unchanged. It only reads the local checkout //! (no network). A bad checkout is a warning, or an error with `CRAFT_FONTS_REQUIRED=1`. //! -//! On wasm32 it embeds only BIZ UDPGothic Regular and the `Arab` faces (about 5.5 MB), to keep the -//! web build within hosting limits (Cloudflare Pages: 25 MiB per file). The web build has no -//! system fonts, so these are its only fallback for Japanese and Arabic-script text. +//! On wasm32 it embeds only BIZ UDPGothic Regular, the `Arab` faces and the `Thai` Regular faces +//! (about 5.9 MB), to keep the web build within hosting limits (Cloudflare Pages: 25 MiB per +//! file). The web build has no system fonts, so these are its only fallback for Japanese, +//! Arabic-script and Thai text. use std::fmt::Write as _; use std::path::PathBuf; @@ -54,8 +55,8 @@ fn craft_fonts(dir: &std::path::Path) -> Result { println!("cargo::warning=craft-fonts: not embedding {family} {style} (AGENTS.md: no Adobe fonts)"); continue; } - let arabic = scripts.split(',').any(|s| s.trim() == "Arab"); - let web_face = (*family == "BIZ UDPGothic" && *style == "Regular") || arabic; + let has = |script: &str| scripts.split(',').any(|s| s.trim() == script); + let web_face = (*family == "BIZ UDPGothic" && *style == "Regular") || has("Arab") || (has("Thai") && *style == "Regular"); if wasm && !web_face { continue; } diff --git a/crates/fonts/src/complex.rs b/crates/fonts/src/complex.rs index a8dd367c3..4a4ac461f 100644 --- a/crates/fonts/src/complex.rs +++ b/crates/fonts/src/complex.rs @@ -1,10 +1,15 @@ -//! Lines with right-to-left text: the line is split into bidi runs in display order +//! Lines with right-to-left text or Thai: the line is split into bidi runs in display order //! ([`crate::bidi::visual_runs`]), each run into pieces by the font that draws them (the text's //! font, a fallback font for characters it lacks, or the stroke font), and each font piece is //! shaped with `harfrust` (OpenType GSUB/GPOS: Arabic contextual forms, lam-alef and other //! ligatures, mark positioning, mirroring) before its glyphs are placed. Shaping happens per //! piece in the font that draws it, so Arabic in a stroke-font style is joined by the fallback //! font. Fonts without OpenType Arabic features get Unicode presentation forms instead. +//! +//! Thai is left to right but needs the shaper too: a consonant carries up to two stacked marks +//! above (a vowel, then a tone mark) and one below, placed by the font's `mark`/`mkmk` +//! positioning; letters with a descender (ฐ, ญ) lose it under a below vowel (`ccmp`); and sara +//! am (ำ) splits into nikhahit and sara aa with the nikhahit moved before any tone mark. use std::ops::Range; use std::panic::{AssertUnwindSafe, catch_unwind}; @@ -17,6 +22,17 @@ use crate::{Shaped, bidi, stroke}; /// Longest line (characters) laid out this way; longer lines keep one glyph per character. const MAX_CHARS: usize = 10_000; +/// Whether a line is laid out here rather than one glyph per character: it has right-to-left +/// text, or Thai (see the module notes). +pub(crate) fn needed(s: &str) -> bool { + s.chars().any(|c| bidi::is_rtl(c) || is_thai(c)) +} + +/// Whether `c` is in the Thai block. +pub(crate) fn is_thai(c: char) -> bool { + matches!(c, '\u{0E00}'..='\u{0E7F}') +} + /// What draws a piece of a run. #[derive(Clone)] enum Src { @@ -100,7 +116,11 @@ fn shape_chars(primary: Option<&Face>, chars: &[(char, bool, bool)], height: f64 } } Src::Face(face) => { - for g in shape_run(face, &cs, r, rtl) { + let glyphs = shape_run(face, &cs, r.clone(), rtl); + // Right-to-left runs stay outlines only: their glyphs come in display order + // and include ligatures (lam-alef) that stand for several characters. + let shows = if rtl { Vec::new() } else { glyph_chars(face, &cs, &glyphs, r.end) }; + for (n, g) in glyphs.iter().enumerate() { let outline = ttf::glyph_by_id(face, g.gid); let k = h / outline.cap * wf; let dy = g.dy * h / outline.cap; @@ -108,7 +128,14 @@ fn shape_chars(primary: Option<&Face>, chars: &[(char, bool, bool)], height: f64 for p in contours.iter_mut().flatten() { *p += Vec2::new(dy * shear, dy); } - out.push_glyph(contours, None); + let origin = Vec2::new(x + g.dx * k + dy * shear, dy); + let text = shows + .get(n) + .copied() + .flatten() + .filter(|c| !c.is_whitespace()) + .and_then(|c| ttf::text_glyph_at(&outline, c, origin, h / outline.cap, wf, shear)); + out.push_glyph(contours, text); } let adv = g.advance * k; let (u, o) = flags(g.cluster); @@ -216,6 +243,44 @@ fn shape_run(face: &Face, cs: &[char], r: Range, rtl: bool) -> Vec .collect() } +/// Longest cluster whose glyphs are matched to its characters by glyph id; the glyphs of a +/// longer one (a hostile run of thousands of stacked marks) take its characters in order. +const MAX_MATCHED_CLUSTER: usize = 32; + +/// The character each glyph of a left-to-right run shows, for output that keeps text as text +/// ([`crate::TextGlyph`]). The shaper reorders, splits and substitutes glyphs inside a cluster +/// (a letter with its marks), so within each cluster a glyph that is a character's own glyph +/// takes that character first, then the other glyphs take the cluster's remaining characters in +/// order. Each character is shown once: a glyph left over (the second half of a split Thai sara +/// am) shows none and is drawn as an outline only. +fn glyph_chars(face: &Face, cs: &[char], glyphs: &[Glyph], end: usize) -> Vec> { + let mut out: Vec> = vec![None; glyphs.len()]; + let mut i = 0; + while let Some(cluster) = glyphs.get(i).map(|g| g.cluster) { + let j = glyphs.iter().skip(i).position(|g| g.cluster != cluster).map_or(glyphs.len(), |n| i + n); + // Clusters ascend in a left-to-right run: this one's characters end where the next starts. + let to = glyphs.get(j).map_or(end, |g| g.cluster).clamp(cluster, end.max(cluster)); + let chars = cs.get(cluster..to).unwrap_or_default(); + let mut free: Vec> = chars.iter().copied().map(Some).collect(); + if chars.len() <= MAX_MATCHED_CLUSTER { + let ids = face.glyph_ids(chars); + for n in i..j { + let gid = glyphs.get(n).map(|g| g.gid); + let own = ids.iter().zip(&free).position(|(id, f)| f.is_some() && *id == gid); + if let (Some(p), Some(o)) = (own, out.get_mut(n)) { + *o = free.get_mut(p).and_then(Option::take); + } + } + } + let mut rest = free.into_iter().flatten(); + for o in out.iter_mut().take(j).skip(i).filter(|o| o.is_none()) { + *o = rest.next(); + } + i = j; + } + out +} + #[cfg(test)] mod tests { use super::*; @@ -372,6 +437,108 @@ mod tests { assert!(matches!((adv, hmtx), (Some(a), Some(h)) if (a - f64::from(h)).abs() < 1e-3), "{adv:?} vs {hmtx:?}"); } + #[test] + fn thai_lines_are_shaped_and_latin_lines_are_not() { + assert!(needed("ระดับดินเดิม") && needed("ขนาด 50x50 mm.") && needed("\u{0E4C}")); + assert!(needed("abc سلام")); + assert!(!needed("50x50 mm.") && !needed("Ünïcödé %%c25") && !needed("")); + } + + fn glyph(cluster: usize) -> Glyph { + Glyph { gid: 0, cluster, advance: 500.0, dx: 0.0, dy: 0.0 } + } + + /// Without installed fonts: a face that maps no character, so glyphs take their cluster's + /// characters in order. + #[test] + fn glyphs_show_their_cluster_characters_once() { + let face = empty_glyph_face(); + let cs: Vec = "xabcd".chars().collect(); + // A run from index 1: cluster 1 has "ab" (two glyphs), cluster 3 has "cd" and a split + // character's extra glyph, which shows nothing. + let gs = Vec::from([1, 1, 3, 3, 3].map(glyph)); + assert_eq!(glyph_chars(&face, &cs, &gs, 5), vec![Some('a'), Some('b'), Some('c'), Some('d'), None]); + // Clusters out of order, past the run or past the line show nothing and don't panic. + let gs = Vec::from([3, 1, usize::MAX, 9].map(glyph)); + assert_eq!(glyph_chars(&face, &cs, &gs, 5), vec![None, Some('a'), None, None]); + assert!(glyph_chars(&face, &cs, &[], 5).is_empty()); + // A cluster longer than the matching limit still shows each character once, in order. + let long: Vec = std::iter::once('ก').chain(std::iter::repeat_n('\u{0E48}', MAX_CHARS)).collect(); + let gs: Vec = (0..long.len()).map(|_| glyph(0)).collect(); + let shows = glyph_chars(&face, &long, &gs, long.len()); + assert_eq!(shows.first(), Some(&Some('ก'))); + assert!(shows.iter().all(Option::is_some)); + } + + /// An installed font with Thai, or `None` (the tests then skip). + fn thai_face() -> Option { + ttf::fallback_for('\u{0E01}').filter(|f| ['\u{0E14}', '\u{0E34}', '\u{0E4C}', '\u{0E33}', '\u{0E10}'].iter().all(|c| f.has_glyph(*c))) + } + + /// Lowest point of the glyph that shows `c`. + fn bottom_of(sh: &Shaped, c: char) -> Option { + let t = sh.text.iter().find(|t| t.ch == c)?; + sh.glyphs.get(t.outline)?.iter().flatten().map(|p| p.y).reduce(f64::min) + } + + #[test] + fn thai_marks_stack_in_an_installed_font() { + let Some(face) = thai_face() else { return }; + let shape = |s: &str| ttf::shape(&face, s, 1.0, 1.0, 0.0).expect("the face parses"); + // Thanthakhat over sara i (ดิ์) sits above the vowel, higher than over the bare consonant + // (ด์): the font's mark-to-mark positioning. One glyph per character drew both marks at + // the same height, on top of each other. + let stacked = shape("ดิ์"); + let alone = shape("ด์"); + let (Some(over_vowel), Some(over_letter)) = (bottom_of(&stacked, '\u{0E4C}'), bottom_of(&alone, '\u{0E4C}')) else { + panic!("thanthakhat drawn as text: {:?}", stacked.text.iter().map(|t| t.ch).collect::()) + }; + assert!(over_vowel > over_letter + 0.05, "stacked {over_vowel} vs alone {over_letter}"); + // Every character is written as text exactly once, however the shaper reorders, splits + // (sara am) or substitutes (ฐ without its tail under sara u) the glyphs. + for s in ["ดิ์", "น้ำ", "ฐุ", "ปั้น", "จีรศักดิ์"] { + let mut shown: Vec = shape(s).text.iter().map(|t| t.ch).collect(); + let mut typed: Vec = s.chars().collect(); + shown.sort_unstable(); + typed.sort_unstable(); + assert_eq!(shown, typed, "{s}"); + } + // Measured and laid-out widths agree, in the face and in a stroke-font style. + let s = "ทาสีขาว 50x50 mm."; + assert!((ttf::width(&face, s, 1.0, 1.0).unwrap_or_default() - shape(s).width).abs() < 1e-9); + assert!((crate::line_width(s, 1.0, 1.0) - crate::shape_stroke(s, 1.0, 1.0, 0.0).width).abs() < 1e-9); + // In a stroke-font style Thai comes from the fallback font as text, without + // missing-glyph boxes, and the Latin stays in the stroke font. + let sh = crate::shape_stroke("ระดับดินเดิม +0.00", 1.0, 1.0, 0.0); + assert!(!sh.strokes.iter().any(|s| s.len() == 5 && s.first() == s.last()), "no missing-glyph boxes"); + assert_eq!(sh.text.len(), "ระดับดินเดิม".chars().count()); + assert!(!sh.strokes.is_empty(), "\"+0.00\" in the stroke font"); + } + + #[test] + fn hostile_thai_lines_never_panic() { + for s in [ + "\u{0E33}\u{0E33}\u{0E33}", + "\u{0E4C}\u{0E49}\u{0E34}", + "ก\u{0E49}\u{0E49}\u{0E49}\u{0E33}\u{0E33}", + "%%u\u{0E34}%%o", + "\u{0E01}\u{200D}\u{0E33}\u{10FFFF}", + ] { + let _ = crate::shape_stroke(s, f64::NAN, -1.0, f64::INFINITY); + let _ = crate::line_width(s, 0.0, 0.0); + if let Some(face) = thai_face() { + let _ = ttf::shape(&face, s, f64::NAN, 0.0, f64::NAN); + let _ = ttf::width(&face, s, -1.0, f64::INFINITY); + } + } + // One consonant under thousands of tone marks: a single huge cluster. + let marks = format!("ก{}", "\u{0E48}".repeat(MAX_CHARS - 1)); + assert!(crate::shape_stroke(&marks, 1.0, 1.0, 0.0).width > 0.0); + let long = "ก".repeat(MAX_CHARS + 1); + assert!(shape(None, &long, 1.0, 1.0, 0.0).is_none()); + assert!(crate::shape_stroke(&long, 1.0, 1.0, 0.0).width > 0.0); + } + #[test] fn hostile_rtl_lines_never_panic() { for s in ["\u{202E}%%u\u{05D0}%%", "\u{2067}\u{0644}\u{0627}\u{2069}%%o", "\u{064E}\u{064E}\u{05D0}", "\u{05D0}\u{E000}\u{10FFFF}"] { diff --git a/crates/fonts/src/craft_fonts.rs b/crates/fonts/src/craft_fonts.rs index fb290775f..801b84027 100644 --- a/crates/fonts/src/craft_fonts.rs +++ b/crates/fonts/src/craft_fonts.rs @@ -1,8 +1,8 @@ //! Fonts from the optional craft-fonts build input (). //! //! Built with `CRAFT_FONTS_DIR=`, `build.rs` embeds the fonts listed in its -//! `fonts/manifest.txt` (web builds: only BIZ UDPGothic Regular and the Arabic faces). -//! Without it [`CRAFT_FONTS`] is empty and nothing changes. The font table ([`crate::ttf`]) +//! `fonts/manifest.txt` (web builds: only BIZ UDPGothic Regular, the Arabic faces and the Thai +//! Regular faces). Without it [`CRAFT_FONTS`] is empty and nothing changes. The font table ([`crate::ttf`]) //! lists the embedded fonts with the installed ones, under [`CraftFont::stem`]. A text style //! can name one (`BIZ UDPGothic`), and they are fallbacks for characters a text's font lacks. //! They are copied out of the binary only when first used. @@ -83,6 +83,7 @@ mod tests { let sample = match f.scripts.first().copied() { Some("Jpan") => '\u{3042}', Some("Arab") => '\u{0628}', + Some("Thai") => '\u{0E01}', Some("Hang" | "Kore") => '가', _ => 'A', }; diff --git a/crates/fonts/src/lib.rs b/crates/fonts/src/lib.rs index 5bbae6883..dd5c37597 100644 --- a/crates/fonts/src/lib.rs +++ b/crates/fonts/src/lib.rs @@ -9,12 +9,14 @@ //! Right-to-left lines (Persian, Arabic, Urdu, Hebrew) go through [`bidi`] (the Unicode //! Bidirectional Algorithm, Arabic joining) and `complex` (OpenType shaping with `harfrust`): //! each run is shaped in the font that draws it before glyphs are placed, so the fallback font -//! drawing Arabic in a stroke-font style joins its letters too. +//! drawing Arabic in a stroke-font style joins its letters too. Thai lines are shaped the same +//! way, which stacks their vowels and tone marks above and below each consonant. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] #![forbid(unsafe_code)] pub mod bidi; mod complex; +mod linebreak; mod mtext; mod stroke; pub mod subset; @@ -153,7 +155,7 @@ pub fn shape_stroke(s: &str, height: f64, width_factor: f64, oblique: f64) -> Sh let mut under_start: Option = None; let mut over_start: Option = None; let mut gap = stroke::GAP * sc * wf; - if bidi::has_rtl(s) + if complex::needed(s) && let Some(sh) = complex::shape(None, s, h, wf, oblique) { return sh; @@ -244,7 +246,7 @@ pub fn layout_line(s: &str, height: f64, width_factor: f64, oblique: f64) -> Run /// Width of a line without building strokes. pub fn line_width(s: &str, height: f64, width_factor: f64) -> f64 { - if bidi::has_rtl(s) { + if complex::needed(s) { let h = if height.is_finite() && height > 0.0 { height } else { 1.0 }; let wf = if width_factor.is_finite() && width_factor.abs() > 1e-6 { width_factor } else { 1.0 }; if let Some(sh) = complex::shape(None, s, h, wf, 0.0) { diff --git a/crates/fonts/src/linebreak.rs b/crates/fonts/src/linebreak.rs new file mode 100644 index 000000000..5669db121 --- /dev/null +++ b/crates/fonts/src/linebreak.rs @@ -0,0 +1,205 @@ +//! Line-break opportunities inside Thai text, for MTEXT word wrap. +//! +//! Thai is written without spaces between words (a space ends a phrase or sentence), so a run of +//! Thai can only wrap where one word ends and the next begins. Those places come from the Unicode +//! line breaking algorithm (UAX #14), which leaves Thai to a word segmenter: `icu_segmenter`'s +//! compiled-in LSTM model for Southeast Asian scripts. Only breaks between two Thai characters +//! are used, so text in other scripts wraps at spaces exactly as before. +//! +//! The model rather than ICU's Thai dictionary: after a word the dictionary lacks (names, +//! loanwords such as เมทัลชีท) the dictionary segmenter loses its place and breaks inside the +//! words that follow, even between a consonant and its tone mark (ทแผ|่|นเร|ียบ), where the model +//! breaks at word boundaries and, when unsure, leaves words together (a line then wraps a little +//! early, never mid-word). The model is also a sixth of the dictionary's size. Any break inside +//! a character cluster is still dropped ([`cluster_boundary`]), so a line never starts with a +//! mark or a following vowel and never ends with a leading vowel. +//! +//! The model takes a few microseconds a character and text is laid out again on every display +//! rebuild, so results are kept in a small cache. + +use std::collections::HashMap; +use std::panic::{AssertUnwindSafe, catch_unwind}; +use std::sync::{Arc, OnceLock, PoisonError, RwLock}; + +use icu_segmenter::LineSegmenter; +use icu_segmenter::options::LineBreakOptions; + +use crate::complex::is_thai; + +/// Longest text (characters) looked up; longer text (hostile contents) gets no Thai breaks and +/// wraps at spaces only. +const MAX_CHARS: usize = 4_000; + +/// Most texts and bytes of text kept in the cache; it is emptied when either would be passed. +const MAX_CACHED: usize = 8_192; +const MAX_CACHED_BYTES: usize = 4 << 20; + +/// Whether a line may break between Thai characters `prev` and `next` without splitting a +/// character cluster: `next` is no combining mark (vowels above and below, tone marks) and no +/// following vowel or sign (ะ า ำ ๅ, ๆ, ฯ), and `prev` is no leading vowel (เ แ โ ใ ไ), which is +/// written before the consonant it belongs to. +fn cluster_boundary(prev: char, next: char) -> bool { + !crate::bidi::is_mark(next) && !matches!(next, 'ะ' | 'า' | 'ำ' | 'ๅ' | 'ๆ' | 'ฯ') && !matches!(prev, 'เ' | 'แ' | 'โ' | 'ใ' | 'ไ') +} + +/// The line segmenter with the Southeast Asian LSTM models, built once. +fn segmenter() -> &'static icu_segmenter::LineSegmenterBorrowed<'static> { + static S: OnceLock> = OnceLock::new(); + S.get_or_init(|| LineSegmenter::new_lstm(LineBreakOptions::default())) +} + +#[derive(Default)] +struct Cache { + entries: HashMap>, + bytes: usize, +} + +/// A read-write lock: text is laid out on all cores and nearly every lookup is a hit. +fn cache() -> &'static RwLock { + static CACHE: OnceLock> = OnceLock::new(); + CACHE.get_or_init(|| RwLock::new(Cache::default())) +} + +/// Character offsets in `text`, ascending, where a line may break between two Thai characters +/// (between two Thai words, at a cluster boundary). Empty when `text` has no Thai, is longer than +/// [`MAX_CHARS`], or the segmenter fails. +pub(crate) fn thai_breaks(text: &str) -> Arc<[usize]> { + if !text.chars().any(is_thai) || text.chars().nth(MAX_CHARS).is_some() { + return Arc::default(); + } + if let Some(found) = cache().read().unwrap_or_else(PoisonError::into_inner).entries.get(text) { + return found.clone(); + } + let breaks: Arc<[usize]> = segment(text).into(); + let mut c = cache().write().unwrap_or_else(PoisonError::into_inner); + let bytes = text.len().saturating_add(breaks.len().saturating_mul(std::mem::size_of::())); + if c.entries.len() >= MAX_CACHED || c.bytes.saturating_add(bytes) > MAX_CACHED_BYTES { + c.entries.clear(); + c.bytes = 0; + } + if c.entries.insert(text.to_string(), breaks.clone()).is_none() { + c.bytes = c.bytes.saturating_add(bytes); + } + breaks +} + +/// [`thai_breaks`] without the cache. +fn segment(text: &str) -> Vec { + let chars: Vec<(usize, char)> = text.char_indices().collect(); + // A last-resort guard around the segmenter: a panic in it must not take the drawing down + // (the text then wraps at spaces only). + let bytes: Vec = catch_unwind(AssertUnwindSafe(|| segmenter().segment_str(text).collect())).unwrap_or_default(); + bytes + .into_iter() + .filter_map(|b| chars.binary_search_by_key(&b, |(at, _)| *at).ok()) + .filter(|&i| { + let prev = i.checked_sub(1).and_then(|p| chars.get(p)).map(|(_, c)| *c); + let next = chars.get(i).map(|(_, c)| *c); + matches!((prev, next), (Some(p), Some(n)) if is_thai(p) && is_thai(n) && cluster_boundary(p, n)) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The pieces of `text` between its Thai breaks. + fn words(text: &str) -> Vec { + let chars: Vec = text.chars().collect(); + let mut out = Vec::new(); + let mut from = 0; + for at in thai_breaks(text).iter().copied().chain(std::iter::once(chars.len())) { + out.push(chars.get(from..at).unwrap_or_default().iter().collect()); + from = at; + } + out + } + + #[test] + fn thai_breaks_between_words() { + assert_eq!(words("ภาษาไทย"), ["ภาษา", "ไทย"]); + assert_eq!(words("เหล็กกล่อง"), ["เหล็ก", "กล่อง"]); + assert_eq!(words("น้ำทำให้ไม่ใช่"), ["น้ำ", "ทำ", "ให้", "ไม่", "ใช่"]); + // The sample drawing's specification text, with a loanword (metal sheet) inside: the + // words after it are still whole. + let spec = words("เหล็กกล่องขนาดมาตรฐานสำหรับโครงหลังคาเมทัลชีทแผ่นเรียบสีขาว"); + for w in ["เหล็ก", "กล่อง", "ขนาด", "มาตรฐาน", "สำหรับ", "เมทัล", "ชีท", "สี", "ขาว"] + { + assert!(spec.iter().any(|p| p == w), "{w}: {spec:?}"); + } + let wrong = ["ทแผ", "นเร", "ยบ"]; + assert!(spec.iter().all(|p| !wrong.iter().any(|w| p.starts_with(w))), "{spec:?}"); + } + + /// Words no dictionary has (names, loanwords from the sample drawing): no break splits a + /// character cluster there. + #[test] + fn breaks_never_split_a_character_cluster() { + for text in ["จีรศักดิ์สุพรมวัน", "เมทัลชีทแผ่นเรียบสีขาว", "ศุภประดิษฐ์มาสงค์", "น้ำทำให้ไม่ใช่ๆฯ"] + { + let pieces = words(text); + assert_eq!(pieces.concat(), text); + for w in &pieces { + let first = w.chars().next().unwrap_or(' '); + assert!(!crate::bidi::is_mark(first) && !matches!(first, 'ะ' | 'า' | 'ำ' | 'ๅ' | 'ๆ' | 'ฯ'), "{text}: {pieces:?}"); + assert!(!w.ends_with(['เ', 'แ', 'โ', 'ใ', 'ไ']), "{text}: {pieces:?}"); + } + } + } + + #[test] + fn breaks_inside_a_cluster_are_dropped() { + assert!(cluster_boundary('ก', 'ข') && cluster_boundary('า', 'เ') && cluster_boundary('\u{0E48}', 'น')); + for (prev, next) in [ + ('ก', '\u{0E48}'), + ('ด', '\u{0E34}'), + ('\u{0E34}', '\u{0E4C}'), + ('ช', '\u{0E35}'), + ('ก', 'า'), + ('น', 'ำ'), + ('ก', 'ๆ'), + ('เ', 'ม'), + ('ไ', 'ท'), + ] { + assert!(!cluster_boundary(prev, next), "{prev}|{next}"); + } + } + + #[test] + fn other_scripts_and_mixed_text_keep_their_breaks() { + assert!(thai_breaks("hello world").is_empty()); + assert!(thai_breaks("").is_empty()); + // Between Thai and Latin or digits there is no break added (spaces still break). + let text = "ขนาด50x50mm"; + assert!(thai_breaks(text).is_empty(), "{:?}", thai_breaks(text)); + } + + #[test] + fn hostile_text_never_panics_and_stays_bounded() { + for s in ["\u{0E33}\u{0E33}\u{0E33}", "\u{0E4C}\u{0E49}\u{0E34}", "ก\u{0E49}\u{0E49}\u{0E49}", "ก\u{200B}ข\u{FFFF}\u{10FFFF}ค"] { + let n = s.chars().count(); + assert!(thai_breaks(s).iter().all(|&i| i > 0 && i < n)); + } + assert!(thai_breaks(&"ภาษาไทย".repeat(MAX_CHARS)).is_empty(), "too long: no lookup"); + // The limit is on characters, not bytes (a Thai character is three bytes). + assert!(!thai_breaks(&"ภาษาไทย".repeat(MAX_CHARS / 7)).is_empty()); + let long = "ภาษาไทย".repeat(MAX_CHARS / 8); + let n = long.chars().count(); + let b = thai_breaks(&long); + assert!(!b.is_empty() && b.windows(2).all(|w| w[0] < w[1]) && b.iter().all(|&i| i < n)); + } + + #[test] + fn cached_breaks_match_and_the_cache_stays_bounded() { + let text = "เหล็กกล่องขนาดมาตรฐาน"; + assert_eq!(thai_breaks(text), segment(text).into()); + assert_eq!(thai_breaks(text), thai_breaks(text)); + // Many different texts: the cache empties itself rather than growing. + for i in 0..MAX_CACHED + 10 { + let _ = thai_breaks(&format!("ภาษา{i}ไทย")); + } + let c = cache().read().unwrap_or_else(PoisonError::into_inner); + assert!(c.entries.len() <= MAX_CACHED && c.bytes <= MAX_CACHED_BYTES); + } +} diff --git a/crates/fonts/src/mtext.rs b/crates/fonts/src/mtext.rs index 23736d017..a8d49fcbd 100644 --- a/crates/fonts/src/mtext.rs +++ b/crates/fonts/src/mtext.rs @@ -407,6 +407,67 @@ fn parse(contents: &str, base: Fmt) -> (Vec, Vec) { (atoms, fmts) } +/// `word` split where a line may wrap inside it: between two Thai words, as Thai writes no +/// spaces between words ([`crate::linebreak::thai_breaks`]). The pieces keep the word's segments +/// and formats in order and are laid out side by side, so the text looks the same and only gains +/// places to wrap. A word without Thai comes back whole. +/// +/// Lines still wrap at spaces first, as before: only a word too wide for a line of its own (which +/// would run out of the column) is split, so text that fits keeps its line breaks, as its author +/// saw them. +fn split_thai(word: Vec<(Seg, usize)>) -> Vec> { + if !word.iter().any(|(seg, _)| matches!(seg, Seg::Text(t) if t.chars().any(crate::complex::is_thai))) { + return vec![word]; + } + // The word's characters, a stacked fraction standing as one object replacement character + // (never next to a Thai break). + let text: String = word + .iter() + .map(|(seg, _)| match seg { + Seg::Text(t) => t.as_str(), + Seg::Stack(..) => "\u{FFFC}", + }) + .collect(); + let breaks = crate::linebreak::thai_breaks(&text); + if breaks.is_empty() { + return vec![word]; + } + let mut pieces = Vec::with_capacity(breaks.len().saturating_add(1)); + let mut breaks = breaks.iter().copied().peekable(); + let mut piece: Vec<(Seg, usize)> = Vec::new(); + // Character offset of the segment being split. + let mut at = 0usize; + for (seg, format) in word { + let Seg::Text(t) = seg else { + piece.push((seg, format)); + at += 1; + continue; + }; + let n = t.chars().count(); + let (mut rest, mut rest_at) = (t.as_str(), at); + while let Some(b) = breaks.next_if(|&b| b < at + n) { + let k = b.saturating_sub(rest_at); + let byte = rest.char_indices().nth(k).map_or(rest.len(), |(i, _)| i); + let (head, tail) = rest.split_at_checked(byte).unwrap_or((rest, "")); + if !head.is_empty() { + piece.push((Seg::Text(head.to_string()), format)); + } + if !piece.is_empty() { + pieces.push(std::mem::take(&mut piece)); + } + (rest, rest_at) = (tail, b.max(rest_at)); + } + if !rest.is_empty() { + piece.push((Seg::Text(rest.to_string()), format)); + } + at += n; + } + if !piece.is_empty() { + pieces.push(piece); + } + pieces +} + fn seg_width(seg: &Seg, f: &Fmt) -> f64 { match seg { Seg::Text(t) => text_width(&f.font, t, f.height, f.wf), @@ -537,6 +598,8 @@ pub fn layout_mtext(contents: &str, insert: Vec2, height: f64, width: f64, attac layout_mtext_with(contents, &MTextParams { insert, height, width, attach, rotation, line_spacing, ..MTextParams::new(insert, height) }) } +/// The lines of MTEXT without paragraph codes: words wrap at spaces to `width` (0: no wrapping), +/// and a word too wide for a line of its own wraps between its Thai words. fn plain_lines(atoms: &[Atom], fmts: &[Fmt], base: &Fmt, width: f64) -> Vec { let fmt = |index: usize| fmts.get(index).unwrap_or(base); let mut lines = Vec::new(); @@ -566,32 +629,44 @@ fn plain_lines(atoms: &[Atom], fmts: &[Fmt], base: &Fmt, width: f64) -> Vec { - let word_width: f64 = segments.iter().map(|(segment, format)| seg_width(segment, fmt(*format))).sum(); - let space_width = - pending_space.map(|format| crate::space_width(&fmt(format).font, fmt(format).height, fmt(format).wf)).unwrap_or(0.0); - if width > 0.0 && !line.items.is_empty() && line.width + space_width + word_width > width + 1e-9 { - finish(&mut lines, &mut line, fmt(last_fmt), paragraph); - } else if !line.items.is_empty() || pending_space.is_some() { - if let Some(format) = pending_space { + Atom::Word(word) => { + let word_width = + |segments: &[(Seg, usize)]| -> f64 { segments.iter().map(|(segment, format)| seg_width(segment, fmt(*format))).sum() }; + // Wider than the column: Thai breaks between its words (see `split_thai`). + let split; + let pieces = if width > 0.0 && word_width(word) > width + 1e-9 { + split = split_thai(word.clone()); + split.as_slice() + } else { + std::slice::from_ref(word) + }; + for segments in pieces { + let word_width = word_width(segments); + let space_width = + pending_space.map(|format| crate::space_width(&fmt(format).font, fmt(format).height, fmt(format).wf)).unwrap_or(0.0); + if width > 0.0 && !line.items.is_empty() && line.width + space_width + word_width > width + 1e-9 { + finish(&mut lines, &mut line, fmt(last_fmt), paragraph); + } else if !line.items.is_empty() || pending_space.is_some() { + if let Some(format) = pending_space { + let at = line.chars; + line.items.push(Item { seg: None, fmt: format, x: line.width, w: space_width, at }); + line.text.push(' '); + line.chars += 1; + } + line.width += space_width; + } + pending_space = None; + for (segment, format) in segments { + let segment_width = seg_width(segment, fmt(*format)); + line.max_h = line.max_h.max(fmt(*format).height); let at = line.chars; - line.items.push(Item { seg: None, fmt: format, x: line.width, w: space_width, at }); - line.text.push(' '); - line.chars += 1; + let plain = seg_plain(segment); + line.chars += plain.chars().count(); + line.text.push_str(&plain); + line.items.push(Item { seg: Some(segment.clone()), fmt: *format, x: line.width, w: segment_width, at }); + line.width += segment_width; + last_fmt = *format; } - line.width += space_width; - } - pending_space = None; - for (segment, format) in segments { - let segment_width = seg_width(segment, fmt(*format)); - line.max_h = line.max_h.max(fmt(*format).height); - let at = line.chars; - let plain = seg_plain(segment); - line.chars += plain.chars().count(); - line.text.push_str(&plain); - line.items.push(Item { seg: Some(segment.clone()), fmt: *format, x: line.width, w: segment_width, at }); - line.width += segment_width; - last_fmt = *format; } } } @@ -608,10 +683,10 @@ pub fn layout_mtext_with(contents: &str, p: &MTextParams) -> MTextLayout { let width = if p.width.is_finite() && p.width > 0.0 { p.width } else { 0.0 }; let rotation = if p.rotation.is_finite() { p.rotation } else { 0.0 }; let base = Fmt { font: p.font.clone(), height: h, color: None, under: false, over: false, wf, oblique: p.oblique, valign: 1 }; - let (atoms, fmts) = parse(contents, base.clone()); if para::needed(contents, p) { return para::layout(contents, p, &base); } + let (atoms, fmts) = parse(contents, base.clone()); let fmt = |i: usize| fmts.get(i).unwrap_or(&base); let mut lines = plain_lines(&atoms, &fmts, &base, width); // Right-to-left text: each line in display order, its paragraph's direction from the @@ -875,5 +950,89 @@ mod tests { } let long = "word ".repeat(5000); assert!(lay(&long, 10.0).lines.len() > 100); + // Thai with stacks, format runs, stray marks and unpaired braces in one word. + for s in ["ก\\S1/2;ข{\\C1;ภาษา}ไทย\u{0E4C}\u{0E4C}", "{\\H2x;ภาษา}{\\H3x;ไทย}", "\u{0E33}\u{0E49}\\S/;ไทย}}", "ภาษาไทย\tภาษาไทย"] + { + let _ = lay(s, 0.5); + let _ = lay(&format!("\\pi1,l2,t3;{s}"), 0.5); + } + // Too long to look up: one unbreakable word, like a long Latin word. + assert_eq!(lay(&"ภาษาไทย".repeat(20_000), 3.0).lines.len(), 1); + } + + /// Thai writes no spaces between words: a long line wraps between words, in the plain and the + /// paragraph layout, without fonts installed (the line breaks don't depend on glyph widths). + #[test] + fn thai_wraps_between_words() { + let text = "เหล็กกล่องขนาดมาตรฐานสำหรับโครงหลังคาเมทัลชีทแผ่นเรียบสีขาว"; + let one = lay(text, 0.0); + assert_eq!(one.lines, vec![text]); + let wide = one.bounds.width(); + for contents in [text.to_string(), format!("\\pi0,l0;{text}"), format!("{{\\C1;เหล็กกล่อง}}{}", text.trim_start_matches("เหล็กกล่อง"))] + { + let l = lay(&contents, wide / 3.0); + assert!(l.lines.len() >= 3, "{contents}: {:?}", l.lines); + assert_eq!(l.lines.concat(), text, "nothing lost or added: {:?}", l.lines); + for line in &l.lines { + let first = line.chars().next().unwrap_or(' '); + assert!(!crate::bidi::is_mark(first) && !matches!(first, 'ะ' | 'า' | 'ำ'), "{line}"); + assert!(!line.ends_with(['เ', 'แ', 'โ', 'ใ', 'ไ']), "{line}"); + } + } + // The pieces sit side by side with nothing between them: drawn with a Thai font they are + // as wide as the word laid out in one piece (up to kerning). + let pieces = split_thai(vec![(Seg::Text(text.to_string()), 0)]); + if one.pieces.iter().any(|p| !p.shaped.glyphs.is_empty()) { + let base = Fmt { font: TextFont::Stroke, height: 1.0, color: None, under: false, over: false, wf: 1.0, oblique: 0.0, valign: 1 }; + let sum: f64 = pieces.iter().flatten().map(|(seg, _)| seg_width(seg, &base)).sum(); + assert!((sum - wide).abs() < 0.01 * wide, "{sum} {wide}"); + } + assert!(pieces.len() > 3); + // Latin and other words stay whole; stacks keep their place. + assert_eq!(split_thai(vec![(Seg::Text("hello".into()), 0)]).len(), 1); + let mixed = split_thai(vec![(Seg::Text("ภาษา".into()), 0), (Seg::Stack("1".into(), "2".into(), '/'), 0), (Seg::Text("ภาษาไทย".into()), 1)]); + let flat: Vec = mixed.iter().map(|p| p.iter().map(|(s, _)| seg_plain(s)).collect()).collect(); + assert_eq!(flat, ["ภาษา1/2ภาษา", "ไทย"]); + } + + /// A Thai run that would fit a line at the left indent but not after a first-line indent or a + /// leading tab still breaks between its words instead of running out of the column. The indent + /// is well over the slack: with no Thai font the stroke font's pieces are narrower than the run + /// (each loses its trailing letter gap). + #[test] + fn thai_wraps_after_first_line_indent_and_tab() { + let text = "เหล็กกล่องขนาดมาตรฐานสำหรับโครงหลังคา"; + let wide = lay(text, 0.0).bounds.width(); + for contents in [format!("\\pi4,l0;{text}"), format!("\\pxt4;\t{text}")] { + let l = lay(&contents, wide + 1.0); + assert!(l.lines.len() >= 2, "{contents}: {:?}", l.lines); + assert_eq!(l.lines.concat().trim_start(), text, "{contents}: {:?}", l.lines); + } + } + + /// Text that fits at its spaces keeps those line breaks: a note from the sample drawing + /// (frame, box section 50x50 mm, painted white) at every column width from one word up never + /// breaks inside ทาสีขาว or any other spaced word, in the plain and the paragraph layout. + #[test] + fn spaced_thai_keeps_its_space_breaks() { + let text = "โครงแนวนอน เหล็กกล่อง ขนาด 50x50 mm. ทาสีขาว"; + let words: Vec<&str> = text.split(' ').collect(); + let widest = words.iter().map(|w| lay(w, 0.0).bounds.width()).fold(0.0, f64::max); + let full = lay(text, 0.0).bounds.width(); + let mut width = widest * 1.001; + while width < full * 1.05 { + for contents in [text.to_string(), format!("\\pxql;\\A1;{text}")] { + let l = lay(&contents, width); + assert_eq!(l.lines.join(" "), text, "{width}: {:?}", l.lines); + assert!(l.lines.iter().flat_map(|line| line.split(' ')).all(|w| words.contains(&w)), "{width}: {:?}", l.lines); + } + width *= 1.05; + } + // After a Latin word, a Thai run too long for any line breaks between its words, filling + // the line it starts on. + let long = "เหล็กกล่องขนาดมาตรฐานสำหรับโครงหลังคา"; + let l = lay(&format!("abc {long}"), lay(long, 0.0).bounds.width() / 2.0); + assert!(l.lines.len() >= 3 && l.lines.first().is_some_and(|f| f.starts_with("abc ") && f.len() > 4), "{:?}", l.lines); + assert_eq!(l.lines.concat(), format!("abc {long}")); } } diff --git a/crates/fonts/src/mtext/para.rs b/crates/fonts/src/mtext/para.rs index 064e62d02..c3b6da3e6 100644 --- a/crates/fonts/src/mtext/para.rs +++ b/crates/fonts/src/mtext/para.rs @@ -7,7 +7,7 @@ use cadcraft_geom::{Bounds2, Vec2}; -use super::{Atom, Fmt, MTextLayout, MTextParams, MTextPiece, Seg, arg, num, parse, seg_plain, seg_shape, seg_width}; +use super::{Atom, Fmt, MTextLayout, MTextParams, MTextPiece, Seg, arg, num, parse, seg_plain, seg_shape, seg_width, split_thai}; /// Columns an MTEXT's text flows through, each as wide as the MTEXT's width. #[derive(Clone, Debug, Default, PartialEq)] @@ -512,8 +512,34 @@ impl<'a> LineBuilder<'a> { } } + /// Place a word (`index` > 0: the part of a word after a tab, with no space before it). A + /// word too wide for a line of its own breaks between its Thai words (see `split_thai`). fn push_text(&mut self, index: usize, segments: &[(Seg, usize)]) { - let word_width: f64 = segments.iter().map(|(segment, format)| seg_width(segment, self.format(*format))).sum(); + // An empty line can't wrap, so the word gets the room from where the line starts (a + // first-line indent or a tab stop); otherwise it can wrap to a line at the left indent. + let room = { + let props = self.paragraphs.props(self.paragraph); + let start = if self.line.items.is_empty() { self.line.width } else { props.left.max(0.0) }; + self.available_width(props) - start + }; + if self.width > 0.0 && self.word_width(segments) > room + 1e-9 { + for (k, piece) in split_thai(segments.to_vec()).iter().enumerate() { + self.place(if k == 0 { index } else { index.max(1) }, piece); + } + } else { + self.place(index, segments); + } + } + + /// Width of a word's segments, each in its format. + fn word_width(&self, segments: &[(Seg, usize)]) -> f64 { + segments.iter().map(|(segment, format)| seg_width(segment, self.format(*format))).sum() + } + + /// Put a word, or a piece of one, on the current line after any pending space, or start a + /// new line when it doesn't fit (`index` as in [`Self::push_text`]). + fn place(&mut self, index: usize, segments: &[(Seg, usize)]) { + let word_width = self.word_width(segments); let space_width = if index == 0 { self.pending_space .map(|format| crate::space_width(&self.format(format).font, self.format(format).height, self.format(format).wf)) diff --git a/crates/fonts/src/ttf.rs b/crates/fonts/src/ttf.rs index 9c1888aa8..c426db243 100644 --- a/crates/fonts/src/ttf.rs +++ b/crates/fonts/src/ttf.rs @@ -74,6 +74,12 @@ impl Face { pub fn has_glyph(&self, c: char) -> bool { self.font().is_some_and(|f| f.charmap().map(c).is_some()) } + /// The glyph ids the face's character map gives `chars` (`None` for an unmapped one). + pub(crate) fn glyph_ids(&self, chars: &[char]) -> Vec> { + let Some(f) = self.font() else { return vec![None; chars.len()] }; + let map = f.charmap(); + chars.iter().map(|c| map.map(*c).map(|g| g.to_u32())).collect() + } fn names(&self) -> Vec { let Some(f) = self.font() else { return Vec::new() }; [skrifa::string::StringId::FAMILY_NAME, skrifa::string::StringId::TYPOGRAPHIC_FAMILY_NAME, skrifa::string::StringId::FULL_NAME] @@ -155,14 +161,58 @@ const ARABIC_FALLBACK: &[&str] = &[ "dejavusans", ]; const HEBREW_FALLBACK: &[&str] = &["notosanshebrew", "notoserifhebrew", "arial", "tahoma", "segoeui", "dejavusans"]; +/// Fonts known to cover Thai, with the OpenType mark positioning its stacked vowels and tone +/// marks need. First the Windows faces Thai drawings are laid out with (Cordia New, Angsana New, +/// Browallia New: Thai SHX fonts such as `cordia2.shx` copy their design and widths, so text +/// keeps its fit in title blocks and tables), then IBM Plex Sans Thai (installed, or embedded +/// from craft-fonts, which makes it the web build's Thai font), then Noto and the fonts each OS +/// ships (macOS Thonburi and Sukhumvit, Windows Leelawadee and Tahoma, the Linux TLWG fonts). +/// Matched as parts of file names: `angsa` and `browa` find both today's Windows collections +/// (`angsana.ttc`, `browalia.ttc`) and the older single faces (`angsa.ttf`, `browab.ttf`). +const THAI_FALLBACK: &[&str] = &[ + "cordia", + "angsa", + "browa", + "ibmplexsansthai", + "notosansthai", + "notoserifthai", + "thonburi", + "sukhumvit", + "leelawui", + "leelawadeeui", + "leelawad", + "tahoma", + "kinnari", + "norasi", + "garuda", + "loma", + "waree", + "sarabun", +]; /// Script-specific fallback files loaded per character, at most. const MAX_SCRIPT_FILES: usize = 4; +/// The font file stems matching a script's fallback `names`, best first: regular weights before +/// bold, light and the like, then in the order of `names`, then by name. +fn script_candidates(stems: &[String], names: &[&str]) -> Vec { + let mut v: Vec<(usize, usize, String)> = stems + .iter() + .filter_map(|s| { + let n = norm(s); + names.iter().position(|p| n.contains(p)).map(|pref| (weight_penalty(s, ""), pref, s.clone())) + }) + .collect(); + v.sort(); + v.dedup_by(|a, b| a.2 == b.2); + v.into_iter().map(|(_, _, s)| s).collect() +} + /// Script-specific fallback font names for `c`, if any. fn script_fallback(c: char) -> &'static [&'static str] { match c { '\u{0600}'..='\u{08FF}' | '\u{FB50}'..='\u{FDFF}' | '\u{FE70}'..='\u{FEFF}' => ARABIC_FALLBACK, '\u{0590}'..='\u{05FF}' | '\u{FB1D}'..='\u{FB4F}' => HEBREW_FALLBACK, + '\u{0E00}'..='\u{0E7F}' => THAI_FALLBACK, _ => &[], } } @@ -362,18 +412,8 @@ impl Db { if names.is_empty() { return None; } - let mut stems: Vec<(usize, usize, String)> = self - .paths - .keys() - .chain(self.files.keys()) - .filter_map(|s| { - let n = norm(s); - names.iter().position(|p| n.contains(p)).map(|pref| (weight_penalty(s, ""), pref, s.clone())) - }) - .collect(); - stems.sort(); - stems.dedup_by(|a, b| a.2 == b.2); - for (_, _, stem) in stems.into_iter().take(MAX_SCRIPT_FILES) { + let stems: Vec = self.paths.keys().chain(self.files.keys()).cloned().collect(); + for stem in script_candidates(&stems, names).into_iter().take(MAX_SCRIPT_FILES) { let Some(bytes) = self.file(&stem) else { continue }; let Some(face) = faces(&bytes).into_iter().find(|f| f.has_glyph(c)) else { continue }; if let Some(i) = self.fallback.faces.iter().position(|f| f.same(&face)) { @@ -855,7 +895,7 @@ pub(crate) fn place_glyph_at(g: &GlyphOutline, origin: Vec2, scale: f64, wf: f64 /// Characters the face lacks come from a fallback font, scaled to its own cap height. pub fn shape(face: &Face, s: &str, height: f64, width_factor: f64, oblique: f64) -> Option { face.font()?; - if crate::bidi::has_rtl(s) + if crate::complex::needed(s) && let Some(sh) = crate::complex::shape(Some(face), s, height, width_factor, oblique) { return Some(sh); @@ -893,7 +933,7 @@ pub fn shape(face: &Face, s: &str, height: f64, width_factor: f64, oblique: f64) /// Width of one line set in a TrueType face. pub fn width(face: &Face, s: &str, height: f64, width_factor: f64) -> Option { face.font()?; - if crate::bidi::has_rtl(s) + if crate::complex::needed(s) && let Some(sh) = crate::complex::shape(Some(face), s, height, width_factor, 0.0) { return Some(sh.width); @@ -923,6 +963,40 @@ pub fn layout_line(face: &Face, s: &str, height: f64, width_factor: f64, oblique mod family_tests { use super::*; + #[test] + fn thai_looks_in_thai_fonts_before_the_general_search() { + // Letters, vowels, tone marks and digits: the general search loads a bounded number of + // files with the CJK fonts first, none of which has Thai. + for c in ['\u{0E01}', '\u{0E33}', '\u{0E34}', '\u{0E4C}', '\u{0E50}'] { + assert!(script_fallback(c).contains(&"notosansthai"), "{c}"); + } + assert!(script_fallback('a').is_empty() && script_fallback('\u{0E80}').is_empty()); + // Windows, macOS and Linux each ship one of them under these file names. + let stems: Vec = ["LeelawUI", "Thonburi", "Loma", "msyh"].map(norm).to_vec(); + assert!(stems.iter().filter(|s| THAI_FALLBACK.iter().any(|p| s.contains(p))).count() == 3); + // The faces Thai drawings are laid out with come first, then IBM Plex Sans Thai (an + // installed file or the craft-fonts face, both under this stem), then the others; regular + // weights before bold. + let stems: Vec = + ["notosansthai-regular", "ibmplexsansthai-bold", "ibmplexsansthai-regular", "msyh", "cordiab", "cordia", "ibmplexsansthailooped-regular"] + .map(String::from) + .to_vec(); + assert_eq!( + script_candidates(&stems, THAI_FALLBACK), + ["cordia", "cordiab", "ibmplexsansthai-regular", "ibmplexsansthailooped-regular", "notosansthai-regular", "ibmplexsansthai-bold"] + ); + // A text style naming the family looks in its regular file first, not the Latin Plex + // Sans or the looped or bold variants. + let stems: Vec = ["ibmplexsans-regular", "ibmplexsansthai-bold", "ibmplexsansthailooped-regular", "ibmplexsansthai-regular", "arial"] + .map(String::from) + .to_vec(); + let found = family_candidates(&stems, &norm("IBM Plex Sans Thai"), "ibm"); + assert_eq!(found.first().map(String::as_str), Some("ibmplexsansthai-regular"), "{found:?}"); + // Angsana New and Browallia New under today's Windows file names and the older ones. + let stems: Vec = ["browa", "notosansthai-regular", "angsana", "browalia", "angsa", "arial"].map(String::from).to_vec(); + assert_eq!(script_candidates(&stems, THAI_FALLBACK), ["angsa", "angsana", "browa", "browalia", "notosansthai-regular"]); + } + #[test] fn a_family_is_looked_for_in_files_starting_with_its_first_word() { let stems: Vec = ["gost", "gost2304a", "gost common", "arial", "isocpeur"].map(String::from).to_vec(); From c72c0eebfb445abddc35e159efbbd153d851c608 Mon Sep 17 00:00:00 2001 From: Andrea Ficarra Date: Sun, 1 Nov 2026 20:38:04 +0100 Subject: [PATCH 029/100] SAVEAS portableFonts: Windows fonts for Linux text style fonts (#2576) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * SAVEAS portableFonts: Windows fonts for Linux text style fonts A text style using a Linux TrueType font (Liberation, DejaVu, Open Sans) shows in AutoCAD on Windows with a substitute it picks. A save now names those styles on the command line (and as `nonPortableFonts` in the result), and SAVEAS {"portableFonts": true} writes the Windows equivalent in the file (Liberation -> Arial, Times New Roman, Courier New; DejaVu Sans -> Verdana; Open Sans -> Arial) while the drawing keeps its own fonts. Narrow and condensed families have no Windows stand-in (Arial Narrow comes with Office) and are kept and named. QSAVE takes no SAVEAS options. Tests: stand_in_tests in cmd/file.rs, tests/save_portable_fonts.rs. Co-Authored-By: Claude Opus 5.5 * Portable fonts kept in the file, applied by the writer Reworked after review. SAVEAS `portableFonts` writes a copy of the drawing with `Drawing::portable_fonts` set, and the DXF/DWG writer turns each style's Linux font into its Windows font, keeping the style's own font in the CADCraft FONT xdata already used for the built-in font. Reading the file gives the Linux fonts back (unless the font was changed since, in AutoCAD); the drawing itself is never changed. It applies to that save: a later QSAVE writes the drawing's fonts and says so, naming the styles and SAVEAS portableFonts. The family table moves to `cadcraft_fonts::windows`, used by the writer and the note. Styles from an xref aren't saved and aren't named. The parity docs are left to the maintainers (CLAUDE.md). Tests: crates/io/tests/it/portable_fonts.rs (DXF, DWG, a font changed in AutoCAD), crates/engine/tests/save_portable_fonts.rs, windows.rs tests. Co-Authored-By: Claude Opus 5.5 * Portable fonts: match the Windows font by file name A STYLE whose Windows font carries a path (C:\Windows\Fonts\…) still gets its own font back. Co-Authored-By: Claude Opus 5.5 * Portable fonts: say the font list again only when it changes A save still returns nonPortableFonts, but the command line note isn't repeated on every QSAVE of the same drawing. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/doc/src/lib.rs | 5 ++ crates/engine/src/cmd/file.rs | 59 ++++++++++++++-- crates/engine/src/doc_state.rs | 4 ++ crates/engine/tests/save_portable_fonts.rs | 54 ++++++++++++++ crates/fonts/src/lib.rs | 1 + crates/fonts/src/windows.rs | 82 ++++++++++++++++++++++ crates/io/src/dxf_read/tables.rs | 10 +-- crates/io/src/dxf_write/sections.rs | 7 +- crates/io/tests/it/main.rs | 1 + crates/io/tests/it/portable_fonts.rs | 59 ++++++++++++++++ 10 files changed, 268 insertions(+), 14 deletions(-) create mode 100644 crates/engine/tests/save_portable_fonts.rs create mode 100644 crates/fonts/src/windows.rs create mode 100644 crates/io/tests/it/portable_fonts.rs diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index e07a08609..8f2a1e2e4 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -132,6 +132,10 @@ pub struct Drawing { /// Records of the file this drawing was read from that the reader could not place (a damaged /// file), counted by the section they came from. Never written to a file. pub skipped_records: BTreeMap, + /// A save option, set on the copy SAVEAS `portableFonts` writes: the file gets Windows fonts + /// for the Linux fonts of text styles, and each style keeps its own in CADCraft xdata, so the + /// drawing reopens with it. Never set on an open drawing. + pub portable_fonts: bool, } impl Default for Drawing { @@ -191,6 +195,7 @@ impl Drawing { read_notes: Vec::new(), refedit: None, skipped_records: BTreeMap::new(), + portable_fonts: false, } } diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 1c3fbd5c8..234769ad4 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -85,12 +85,13 @@ pub fn deliver() -> Option { DELIVER.get().copied() } -/// How a drawing file is written: its version (`None`: the drawing's own, `$ACADVER`) and -/// binary instead of ASCII DXF. +/// How a drawing file is written: its version (`None`: the drawing's own, `$ACADVER`), binary +/// instead of ASCII DXF, and Windows fonts for Linux ones ([`cadcraft_doc::Drawing::portable_fonts`]). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct SaveAs { pub version: Option, pub binary: bool, + pub portable_fonts: bool, } /// Write a drawing with [`SaveAs`] options (installed by hosts whose writer supports them). @@ -131,7 +132,12 @@ fn save_options(cmd: &str, p: &Value) -> Result { Some(Value::Bool(b)) => *b, Some(_) => return Err(bad(cmd, "`binary` must be true or false")), }; - Ok(SaveAs { version, binary }) + let portable_fonts = match p.get("portableFonts") { + None | Some(Value::Null) => false, + Some(Value::Bool(b)) => *b, + Some(_) => return Err(bad(cmd, "`portableFonts` must be true or false")), + }; + Ok(SaveAs { version, binary, portable_fonts }) } /// Whether `path` names a DXF file (or has no extension, which saves as DXF). @@ -148,7 +154,9 @@ fn write_bytes(s: &Session, cmd: &str, path: &str, opts: &SaveAs) -> Result Vec { CommandSpec::new("saveas", "Save As...", run_saveas_reporting) .menu(&["File", "Save As..."]) .key("Cmd+Shift+S") - .params("{path, version?: 2000|2004|2007|2010|2013|2018, binary?: bool}") + .params( + "{path, version?: 2000|2004|2007|2010|2013|2018, binary?: bool, portableFonts?: bool (Windows fonts for Linux ones, in this file)}", + ) .noundo(), CommandSpec::new("document.switch", "Switch Drawing", run_switch).params("{index}").enabled(always).noundo(), CommandSpec::new("document.bytes", "Drawing as bytes", run_bytes) - .params("{format?: dxf, version?: 2000…2018, binary?: bool} → {data: base64}") + .params("{format?: dxf, version?: 2000…2018, binary?: bool, portableFonts?: bool} → {data: base64}") .noundo(), CommandSpec::new("script", "Run Script...", run_script) .menu(&["Tools", "Run Script..."]) @@ -395,7 +405,7 @@ fn run_closeall(s: &mut Session, _p: &Value) -> Result { } pub(crate) fn save_to(s: &mut Session, path: &str) -> Result { - let opts = SaveAs { version: None, binary: s.state()?.binary_dxf && is_dxf(path) }; + let opts = SaveAs { binary: s.state()?.binary_dxf && is_dxf(path), ..SaveAs::default() }; save_as(s, "save", path, &opts) } @@ -677,6 +687,7 @@ fn run_saveas(s: &mut Session, p: &Value) -> Result { if is_drawing_file(&path) { r["version"] = json!(opts.version.unwrap_or_else(|| drawing_version(s, &path)).year()); r["binary"] = json!(opts.binary); + r["portableFonts"] = json!(opts.portable_fonts); } Ok(r) } @@ -799,9 +810,43 @@ fn save_reporting(s: &mut Session, p: &Value, run: fn(&mut Session, &Value) -> R if r.get("path").and_then(Value::as_str).is_some_and(drops_unknown) { report_not_saved(s, &mut r, &lost); } + if r.get("path").and_then(Value::as_str).is_some_and(is_drawing_file) { + report_fonts(s, &mut r); + } Ok(r) } +/// Name the text styles whose font in the saved file AutoCAD on Windows doesn't have: in the +/// result, and at the command line when the list changed since the last save. Styles from an xref +/// (`XREF|name`) aren't saved: AutoCAD takes them from the xref. +fn report_fonts(s: &mut Session, r: &mut Value) { + use cadcraft_fonts::windows::{missing_on_windows, windows_font, written_font}; + let portable = r.get("portableFonts").and_then(Value::as_bool).unwrap_or(false); + let Ok(d) = s.state().map(|st| st.drawing()) else { return }; + let fonts: Vec<(String, String)> = d + .text_styles + .iter() + .filter(|t| !t.name.contains('|') && missing_on_windows(written_font(&t.font, portable))) + .map(|t| (t.name.clone(), t.font.clone())) + .collect(); + let list = fonts.iter().map(|(style, font)| format!("{style} ({font})")).collect::>().join(", "); + let changed = s.state_mut().map(|st| std::mem::replace(&mut st.fonts_noted, list.clone()) != list).unwrap_or(true); + if fonts.is_empty() { + return; + } + if changed { + let hint = if fonts.iter().any(|(_, font)| windows_font(font).is_some()) { + " SAVEAS with portableFonts writes Windows fonts for those that have one." + } else { + "" + }; + s.echo(format!("Fonts AutoCAD on Windows doesn't have: {list}.{hint}")); + } + if let Some(o) = r.as_object_mut() { + o.insert("nonPortableFonts".into(), json!(fonts.iter().map(|(style, font)| json!({ "style": style, "font": font })).collect::>())); + } +} + fn run_qsave_reporting(s: &mut Session, p: &Value) -> Result { save_reporting(s, p, run_save) } diff --git a/crates/engine/src/doc_state.rs b/crates/engine/src/doc_state.rs index c70989a58..d11c6f0eb 100644 --- a/crates/engine/src/doc_state.rs +++ b/crates/engine/src/doc_state.rs @@ -35,6 +35,9 @@ pub struct DocState { /// The Block Editor (BEDIT), when it is open: `doc` is then the editor's drawing, and the /// drawing itself waits in it ([`DocState::drawing`]). pub block_edit: Option, + /// The fonts missing on Windows that a save last named: the next save names them again only + /// when they change. + pub(crate) fonts_noted: String, } static NEXT_UID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); @@ -61,6 +64,7 @@ impl DocState { uid: NEXT_UID.fetch_add(1, std::sync::atomic::Ordering::Relaxed), binary_dxf: false, block_edit: None, + fonts_noted: String::new(), } } /// The drawing has changes not saved to its file, or the Block Editor has changes not yet diff --git a/crates/engine/tests/save_portable_fonts.rs b/crates/engine/tests/save_portable_fonts.rs new file mode 100644 index 000000000..a63016d60 --- /dev/null +++ b/crates/engine/tests/save_portable_fonts.rs @@ -0,0 +1,54 @@ +//! Text styles with Linux fonts AutoCAD on Windows doesn't have: a save names them, and SAVEAS +//! with portableFonts writes Windows fonts in that file while the drawing keeps its own. + +use cadcraft_doc::{Drawing, TextStyle}; +use cadcraft_engine::Session; +use cadcraft_engine::cmd::file::{IoHooks, set_io}; +use serde_json::json; + +fn dxf(d: &Drawing, _: &str) -> Result, String> { + Ok(cadcraft_io::write_dxf(d).into_bytes()) +} + +fn session() -> Session { + set_io(IoHooks { read: |_, _| Ok(Drawing::new_metric()), write: dxf, plot: None }); + let mut d = Drawing::new_metric(); + for (name, font) in [("Notes", "DejaVuSans.ttf"), ("Plain", "arial.ttf"), ("Tight", "Liberation Sans Narrow")] { + d.text_styles.push(TextStyle { name: name.into(), font: font.into(), ..TextStyle::default() }); + } + let mut s = Session::empty(); + s.open_drawing(d, "fonts.dxf", None); + s +} + +#[test] +fn portable_fonts_writes_windows_fonts_in_that_file() { + let mut s = session(); + let dir = std::env::temp_dir().join(format!("cadcraft-portable-fonts-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = |name: &str| dir.join(name).to_string_lossy().to_string(); + let file = |name: &str| std::fs::read_to_string(dir.join(name)).unwrap(); + let both = json!([{ "style": "Notes", "font": "DejaVuSans.ttf" }, { "style": "Tight", "font": "Liberation Sans Narrow" }]); + + // portableFonts writes Verdana for DejaVu Sans; the narrow style has no Windows font. + let r = s.execute("saveas", &json!({ "path": path("portable.dxf"), "portableFonts": true })).unwrap(); + assert_eq!(r["nonPortableFonts"], json!([{ "style": "Tight", "font": "Liberation Sans Narrow" }])); + assert_eq!(s.log.last().map(String::as_str), Some("Fonts AutoCAD on Windows doesn't have: Tight (Liberation Sans Narrow).")); + assert!(file("portable.dxf").contains("verdana.ttf")); + assert!(s.state().unwrap().drawing().text_styles.iter().any(|t| t.font == "DejaVuSans.ttf"), "the drawing keeps its fonts"); + + // A later save without it writes the drawing's fonts again, and says so. + let r = s.execute("qsave", &json!({})).unwrap(); + assert_eq!(r["nonPortableFonts"], both); + assert!(s.log.last().is_some_and(|l| l.ends_with("SAVEAS with portableFonts writes Windows fonts for those that have one.")), "{:?}", s.log); + assert!(!file("portable.dxf").contains("verdana.ttf")); + + // The same list isn't said again on every save. + let lines = s.log.len(); + let r = s.execute("qsave", &json!({})).unwrap(); + assert_eq!(r["nonPortableFonts"], both); + assert!(s.log.iter().skip(lines).all(|l| !l.starts_with("Fonts AutoCAD")), "{:?}", s.log); + + assert!(s.execute("saveas", &json!({ "path": path("bad.dxf"), "portableFonts": "yes" })).is_err()); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/crates/fonts/src/lib.rs b/crates/fonts/src/lib.rs index dd5c37597..beb8996ff 100644 --- a/crates/fonts/src/lib.rs +++ b/crates/fonts/src/lib.rs @@ -21,6 +21,7 @@ mod mtext; mod stroke; pub mod subset; pub mod ttf; +pub mod windows; use cadcraft_geom::{Bounds2, Vec2}; diff --git a/crates/fonts/src/windows.rs b/crates/fonts/src/windows.rs new file mode 100644 index 000000000..729a2eea0 --- /dev/null +++ b/crates/fonts/src/windows.rs @@ -0,0 +1,82 @@ +//! Linux font families that AutoCAD on Windows doesn't have, and the Windows fonts a drawing +//! saved with portable fonts gets instead. + +/// Family key (lower case, without spaces, hyphens or underscores; a longer key before a shorter +/// one it starts with) and the Windows fonts for its regular, bold, italic and bold italic styles. +const FAMILIES: [(&str, [&str; 4]); 7] = [ + ("liberationsans", ["arial.ttf", "arialbd.ttf", "ariali.ttf", "arialbi.ttf"]), + ("liberationserif", ["times.ttf", "timesbd.ttf", "timesi.ttf", "timesbi.ttf"]), + ("liberationmono", ["cour.ttf", "courbd.ttf", "couri.ttf", "courbi.ttf"]), + ("opensans", ["arial.ttf", "arialbd.ttf", "ariali.ttf", "arialbi.ttf"]), + ("dejavusansmono", ["cour.ttf", "courbd.ttf", "couri.ttf", "courbi.ttf"]), + ("dejavusans", ["verdana.ttf", "verdanab.ttf", "verdanai.ttf", "verdanaz.ttf"]), + ("dejavuserif", ["times.ttf", "timesbd.ttf", "timesi.ttf", "timesbi.ttf"]), +]; + +/// The family of a TrueType `font` (file or family name) from [`FAMILIES`], with the rest of its +/// key (the style: `bold`, `italic`, …). +fn family(font: &str) -> Option<(&'static [&'static str; 4], String)> { + let name = font.trim().rsplit(['/', '\\']).next().unwrap_or_default().to_lowercase(); + let stem = name.strip_suffix(".ttf").or_else(|| name.strip_suffix(".otf")).unwrap_or(&name); + if stem.contains('.') { + return None; // another kind of file, such as an SHX font + } + let key: String = stem.chars().filter(|c| !matches!(c, ' ' | '-' | '_')).collect(); + let (family, fonts) = FAMILIES.iter().find(|(f, _)| key.starts_with(f))?; + Some((fonts, key.get(family.len()..).unwrap_or_default().to_string())) +} + +/// Whether `font` is a Linux font AutoCAD on Windows doesn't have. +pub fn missing_on_windows(font: &str) -> bool { + family(font).is_some() +} + +/// The Windows font for a Linux `font`. Narrow and condensed styles have none (Arial Narrow comes +/// with Office, not with Windows). +pub fn windows_font(font: &str) -> Option<&'static str> { + let (fonts, style) = family(font)?; + if style.contains("narrow") || style.contains("condensed") { + return None; + } + let bold = style.contains("bold"); + let italic = style.contains("italic") || style.contains("oblique"); + fonts.get(usize::from(bold) + 2 * usize::from(italic)).copied() +} + +/// The font a drawing file gets for a text style's `font`: with `portable`, the Windows font for +/// a Linux one. +pub fn written_font(font: &str, portable: bool) -> &str { + if portable { windows_font(font).unwrap_or(font) } else { font } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn windows_fonts_by_file_or_family_name() { + assert_eq!(windows_font("DejaVuSans.ttf"), Some("verdana.ttf")); + assert_eq!(windows_font("/usr/share/fonts/truetype/LiberationSerif-Bold.ttf"), Some("timesbd.ttf")); + assert_eq!(windows_font("Liberation Mono"), Some("cour.ttf")); + assert_eq!(windows_font("LiberationMono-BoldItalic.ttf"), Some("courbi.ttf")); + assert_eq!(windows_font("OpenSans-SemiBold.ttf"), Some("arialbd.ttf")); + assert_eq!(windows_font("Open Sans"), Some("arial.ttf")); + assert_eq!(windows_font("DejaVuSansMono-BoldOblique.ttf"), Some("courbi.ttf")); + assert_eq!(windows_font("DejaVuSerif-Italic.ttf"), Some("timesi.ttf")); + // Narrow and condensed styles are Linux fonts with no Windows one. + for font in ["Liberation Sans Narrow", "LiberationSansNarrow-Bold.ttf", "OpenSans-CondensedBold.ttf", "DejaVuSansCondensed.ttf"] { + assert!(missing_on_windows(font) && windows_font(font).is_none(), "{font}"); + } + // Fonts AutoCAD has, other kinds of file, or names that aren't fonts. + for font in ["arial.ttf", "txt", "dejavusans.shx", "", "/", "Liberation"] { + assert!(!missing_on_windows(font) && windows_font(font).is_none(), "{font}"); + } + } + + #[test] + fn written_font_only_changes_with_portable_fonts() { + assert_eq!(written_font("DejaVuSans.ttf", true), "verdana.ttf"); + assert_eq!(written_font("DejaVuSans.ttf", false), "DejaVuSans.ttf"); + assert_eq!(written_font("Liberation Sans Narrow", true), "Liberation Sans Narrow"); + } +} diff --git a/crates/io/src/dxf_read/tables.rs b/crates/io/src/dxf_read/tables.rs index 8a57f7f9e..ee42b8a58 100644 --- a/crates/io/src/dxf_read/tables.rs +++ b/crates/io/src/dxf_read/tables.rs @@ -4,6 +4,7 @@ use super::{ Color, DashElement, DimStyle, Drawing, HVal, HashMap, Layer, Linetype, Lineweight, NamedView, Rgb, Rx, Tag, TextStyle, Ucs, Vec2, Vec3, VportConfig, VportTile, records, }; +use cadcraft_fonts::windows::windows_font; pub(super) fn header(tags: &[Tag], d: &mut Drawing) { let mut i = 0; @@ -66,9 +67,9 @@ fn annotative(tags: &[Tag]) -> bool { } /// A text style's font: CADCraft's own font name from `CADCRAFT` xdata (`1000 FONT`, name) -/// while the font file is still the `txt` written for other readers; the TrueType family of the -/// style's `ACAD` xdata (`1000` typeface) when it has no font file (how AutoCAD stores TrueType -/// styles); otherwise the file. +/// while the font file is still the `txt` or Windows font written for other readers; the TrueType +/// family of the style's `ACAD` xdata (`1000` typeface) when it has no font file (how AutoCAD +/// stores TrueType styles); otherwise the file. fn style_font(tags: &[Tag], file: String) -> String { if file.trim().is_empty() && let Some(face) = @@ -82,8 +83,9 @@ fn style_font(tags: &[Tag], file: String) -> String { _ => None, }); let stem = file.trim().to_ascii_lowercase(); + let base = stem.rsplit(['/', '\\']).next().unwrap_or_default(); match own { - Some(name) if !name.trim().is_empty() && (stem == "txt" || stem == "txt.shx") => name, + Some(name) if !name.trim().is_empty() && (stem == "txt" || stem == "txt.shx" || windows_font(&name) == Some(base)) => name, _ => file, } } diff --git a/crates/io/src/dxf_write/sections.rs b/crates/io/src/dxf_write/sections.rs index 9a21ef7a9..2d45a532f 100644 --- a/crates/io/src/dxf_write/sections.rs +++ b/crates/io/src/dxf_write/sections.rs @@ -90,9 +90,10 @@ fn write_tables(w: &mut W, plan: TableWritePlan<'_>) -> HashMap w.f(50, s.oblique.to_degrees()); w.i(71, if s.backwards { 2 } else { 0 } | if s.upside_down { 4 } else { 0 }); w.f(42, 0.2); - // The built-in font goes out as `txt`, which every reader has; CADCraft's own name - // travels in `CADCRAFT` xdata (`1000 FONT`, name) so the style reopens with it. - let file = if s.font == cadcraft_fonts_name() { "txt" } else { s.font.as_str() }; + // The built-in font goes out as `txt`, which every reader has, and a Linux font as its + // Windows one with portable fonts; CADCraft's own name travels in `CADCRAFT` xdata + // (`1000 FONT`, name) so the style reopens with it. + let file = if s.font == cadcraft_fonts_name() { "txt" } else { cadcraft_fonts::windows::written_font(&s.font, d.portable_fonts) }; w.s(3, file); w.s(4, &s.big_font); if s.annotative { diff --git a/crates/io/tests/it/main.rs b/crates/io/tests/it/main.rs index 52f37068a..e0ff04f97 100644 --- a/crates/io/tests/it/main.rs +++ b/crates/io/tests/it/main.rs @@ -54,6 +54,7 @@ mod pdf_read; mod plot_layout_scale; mod plot_model_png_scale; mod plot_styles; +mod portable_fonts; mod region_roundtrip; mod roundtrip; mod roundtrip_audit; diff --git a/crates/io/tests/it/portable_fonts.rs b/crates/io/tests/it/portable_fonts.rs new file mode 100644 index 000000000..070c376e8 --- /dev/null +++ b/crates/io/tests/it/portable_fonts.rs @@ -0,0 +1,59 @@ +//! Portable fonts: a drawing saved with them gets the Windows font for each Linux font in its +//! STYLE table, keeps its own in CADCraft xdata, and reopens with it, from DXF and DWG. + +use cadcraft_doc::{Drawing, TextStyle}; +use cadcraft_io::{read, read_dxf, write, write_dxf}; + +fn drawing(portable_fonts: bool) -> Drawing { + let mut d = Drawing { portable_fonts, ..Drawing::new_metric() }; + // A Linux font, and one with no Windows font. + for (name, font) in [("Notes", "DejaVuSans-Bold.ttf"), ("Tight", "Liberation Sans Narrow")] { + d.text_styles.push(TextStyle { name: name.into(), font: font.into(), ..TextStyle::default() }); + } + d +} + +/// (style, font) of the drawing's text styles. +fn fonts(d: &Drawing) -> Vec<(String, String)> { + d.text_styles.iter().map(|t| (t.name.clone(), t.font.clone())).collect() +} + +/// (style, font file) of the STYLE records of DXF text. +fn style_files(dxf: &str) -> Vec<(String, String)> { + let mut out: Vec<(String, String)> = Vec::new(); + let mut style = false; + for t in cadcraft_dxf::parse(dxf.as_bytes()).unwrap() { + match t.code { + 0 => style = t.str() == "STYLE", + 2 if style => out.push((t.str(), String::new())), + 3 if style => out.last_mut().unwrap().1 = t.str(), + _ => {} + } + } + out +} + +#[test] +fn styles_get_windows_fonts_and_reopen_with_their_own() { + let d = drawing(true); + let dxf = write_dxf(&d); + let files = style_files(&dxf); + for (style, file) in [("Notes", "verdanab.ttf"), ("Tight", "Liberation Sans Narrow")] { + assert!(files.contains(&(style.into(), file.into())), "{style}: {files:?}"); + } + for back in [read_dxf(dxf.as_bytes()).unwrap(), read(&write(&d, "t.dwg").unwrap(), "t.dwg").unwrap()] { + assert_eq!(fonts(&back), fonts(&drawing(false))); + } + // The Windows font with a path is still the one written for the style's own. + let path = read_dxf(dxf.replace("verdanab.ttf", "C:\\Windows\\Fonts\\verdanab.ttf").as_bytes()).unwrap(); + assert_eq!(fonts(&path), fonts(&drawing(false))); + // A font changed in AutoCAD since (the xdata left as it was) is the font read. + let edited = read_dxf(dxf.replace("verdanab.ttf", "times.ttf").as_bytes()).unwrap(); + assert!(fonts(&edited).contains(&("Notes".into(), "times.ttf".into()))); +} + +#[test] +fn without_portable_fonts_the_file_has_the_drawings_fonts() { + let files = style_files(&write_dxf(&drawing(false))); + assert!(files.contains(&("Notes".into(), "DejaVuSans-Bold.ttf".into())), "{files:?}"); +} From e187c5fecb26937bd2781c7f11f4b8cdd7491058 Mon Sep 17 00:00:00 2001 From: SoldierPePe Date: Sun, 1 Nov 2026 22:38:07 +0300 Subject: [PATCH 030/100] i18n(ar): translate 129 new strings, use the catalog's terms in 3 (#2564) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Arabic catalog showed 129 interface strings in English: the Block Editor and REFEDIT, dynamic blocks, multileader editing, hatch islands, origin and boundaries, selection and grip settings, visual effects, command aliases, backup and units options, and the MTEXT ruler. `cargo xtask i18n` now reports `ar: nothing missing`. Hatch, Leader and Multileader now use the terms the rest of the catalog uses (تهشير, خط الإشارة) instead of تظليل and قائد. --- crates/ui-egui/src/i18n/ar.tsv | 135 ++++++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/i18n/ar.tsv b/crates/ui-egui/src/i18n/ar.tsv index 40e14cdb5..026190729 100644 --- a/crates/ui-egui/src/i18n/ar.tsv +++ b/crates/ui-egui/src/i18n/ar.tsv @@ -412,7 +412,7 @@ Group Edit تحرير المجموعة Group Options خيارات المجموعة Group Selection تحديد المجموعة - Hatch تظليل + Hatch تهشير Hatch Edit تحرير التهشير Hatch Pattern نمط التهشير Hatch Patterns أنماط التهشير @@ -504,7 +504,7 @@ Layout list قائمة التخطيطات Layout paper ورقة التخطيط Layout {name}: {table} التخطيط {name}: {table} - Leader قائد + Leader خط الإشارة Leaders Only خطوط الإشارة فقط Left يسار Left device button: zoom extents. Right device button: calibrate. زر الجهاز الأيسر: تكبير الحدود. زر الجهاز الأيمن: معايرة. @@ -573,7 +573,7 @@ More toolbar commands المزيد من أوامر شريط الأدوات Move نقل Move sideways to pan; push/pull or twist to zoom. حرّك جانبياً للإزاحة؛ ادفع أو اسحب أو لُف للتكبير. - Multileader قائد متعدد + Multileader خط الإشارة المتعدد Multileader Style... نمط خط الإشارة المتعدد... Multileader style نمط خط الإشارة المتعدد Multiline متعدد الأسطر @@ -1242,3 +1242,132 @@ menu View عرض Check for Updates… التحقق من وجود تحديثات… CADCraft is up to date. CADCraft محدّث. Could not check for updates. تعذّر التحقق من وجود تحديثات. + Add to Working Set إضافة إلى مجموعة العمل + Add: Pick points إضافة: انتقاء نقاط + Add: Select objects إضافة: تحديد كائنات + Add… إضافة… + Alias اختصار + Alias file: {file} ملف الاختصارات: {file} + Align Multileaders محاذاة خطوط الإشارة المتعددة + Analyze Shapes تحليل الأشكال + Analyze Usage تحليل الاستخدام + Animation Effects تأثيرات الحركة + Animation effects تأثيرات الحركة + Associative ترابطي + Backup copy نسخة احتياطية + Block to create or edit الكتلة المراد إنشاؤها أو تحريرها + Bottom left أسفل اليسار + Bottom right أسفل اليمين + Boundaries الحدود + Boundaries picked: {n} الحدود المنتقاة: {n} + Break-line Symbol رمز خط الكسر + Centered متمركز + Change Properties تغيير الخصائص + Choose the command the alias starts. اختر الأمر الذي يشغّله الاختصار. + Click the ruler to add a tab stop, right-click to remove one انقر على المسطرة لإضافة علامة جدولة، وانقر بالزر الأيمن لإزالتها + Click to set new origin انقر لتعيين نقطة أصل جديدة + Close Block Editor إغلاق محرر الكتل + Collect جمع + Collect Multileaders جمع خطوط الإشارة المتعددة + Color 1 اللون 1 + Color 2 اللون 2 + Command الأمر + Convert Text to Mtext تحويل النص إلى نص متعدد الأسطر + Create backup copy with each save إنشاء نسخة احتياطية عند كل حفظ + Create separate hatches إنشاء تهشيرات منفصلة + Crossing selection color: لون تحديد التقاطع: + Curved منحنٍ + Customize تخصيص + Default to boundary extents الافتراضي: امتدادات الحدود + Define each corner of the selection window with a separate click. حدد كل زاوية من نافذة التحديد بنقرة منفصلة. + Discard تجاهل + Drag Dynamic Grip سحب المقبض الديناميكي + Dynamic Block Data بيانات الكتلة الديناميكية + Dynamic Block Grips مقابض الكتلة الديناميكية + Dynamic Block Reference مرجع كتلة ديناميكية + Edit Block Definition تحرير تعريف الكتلة + Edit Command Aliases (PGP)... تحرير اختصارات الأوامر (PGP)... + Edit Multileader تحرير خط الإشارة المتعدد + Edit Reference In-Place تحرير المرجع في مكانه + Edit… تحرير… + Evaluate LISP تقييم LISP + Exclude: استثناء: + Explode Attributes to Text تفجير السمات إلى نص + Express Tools أدوات Express + Font الخط + Grips المقابض + Groups المجموعات + Hatch Pick انتقاء التهشير + Hatch and Gradient التهشير والتدرج اللوني + Hatch origin نقطة أصل التهشير + Hatches تهشيرات + Hemispherical نصف كروي + Ignore تجاهل + Imperial (inches) إنجليزي (بوصة) + Implied Loops حلقات ضمنية + Indicate selection area إظهار منطقة التحديد + Interface scale مقياس الواجهة + Inverted معكوس + Island جزيرة + Island detection اكتشاف الجزر + Italic مائل + Justify ضبط المحاذاة + Load Application... تحميل تطبيق... + Metric (millimetres) متري (مليمتر) + More than {n} aliases. أكثر من {n} اختصار. + Move/Copy/Rotate نقل/نسخ/تدوير + Multiline text نص متعدد الأسطر + New and the first drawing at startup use these units unless a default template is set (QNEWTEMPLATE). تستخدم الرسومات الجديدة وأول رسم عند بدء التشغيل هذه الوحدات ما لم يُعيَّن قالب افتراضي (QNEWTEMPLATE). + New block كتلة جديدة + No alias file loaded. REINIT replaces these aliases with a file's. لم يُحمَّل ملف اختصارات. يستبدل REINIT هذه الاختصارات باختصارات ملف. + Not a number or distance: {text} ليس رقماً أو مسافة: {text} + Object grouping تجميع الكائنات + Object selection limit for display of grips حد تحديد الكائنات لعرض المقابض + Objects on locked layers الكائنات على الطبقات المقفلة + Open in Block Editor فتح في محرر الكتل + Origin: {x}, {y} نقطة الأصل: {x}, {y} + Outer خارجي + Overline خط علوي + Preview معاينة + Query Texts الاستعلام عن النصوص + Save Block حفظ الكتلة + Save Block As حفظ الكتلة باسم + Save Layout as Template حفظ التخطيط كقالب + Save Reference Edits حفظ تعديلات المرجع + Save alias حفظ الاختصار + Save changes to block "{name}"? حفظ التغييرات على الكتلة "{name}"؟ + Select boundaries to remove حدد الحدود المراد إزالتها + Select using two clicks only التحديد بنقرتين فقط + Selection Area Effect تأثير منطقة التحديد + Selection Preview Filter مرشح معاينة التحديد + Selection area opacity: عتامة منطقة التحديد: + Selection effect color لون تأثير التحديد + Selection modes أوضاع التحديد + Selection preview معاينة التحديد + Set Dynamic Property تعيين خاصية ديناميكية + Shape الشكل + Sheet Books to PDF... دفاتر اللوحات إلى PDF... + Show grips إظهار المقابض + Specified origin نقطة أصل محددة + Spherical كروي + Tables الجداول + Text color لون النص + Top left أعلى اليسار + Top right أعلى اليمين + Type an alias name. اكتب اسم الاختصار. + Underline تسطير + Units of new drawings وحدات الرسومات الجديدة + Unsaved changes تغييرات غير محفوظة + Use current origin استخدام نقطة الأصل الحالية + Visual Effect Settings إعدادات التأثيرات المرئية + Visual Effect Settings... إعدادات التأثيرات المرئية... + When a command is active عندما يكون أمر ما نشطاً + When no command is active عندما لا يكون أي أمر نشطاً + Window selection color: لون تحديد النافذة: + Working Set مجموعة العمل + Xref and Block In-place Editing تحرير المراجع الخارجية والكتل في مكانها + Xrefs المراجع الخارجية + built-in مدمج + overrides built-in يتجاوز المدمج + {alias} is already defined; select it and use Edit. {alias} معرَّف مسبقاً؛ حدده واستخدم «تحرير». + {command} is an unknown command. {command} أمر غير معروف. From 5e9c478bfbd2d9520bd3535f0d9a41674f948b4c Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Mon, 2 Nov 2026 02:38:10 +0700 Subject: [PATCH 031/100] U inside a LISP routine takes back the routine's last command (#2610) Inside a routine, commands recorded no undo step (the routine becomes one when it ends), so U inside a routine took back the step from before the routine: after drawing a line D, ERASE B then U in one routine brought B back and removed D. As AutoCAD 2024 does (accoreconsole, the drawing read back after one routine), U inside a routine now takes back the routine's last command, with what the routine did after it (entdel, entmake); U again, UNDO n and REDO go on through its commands. With none left, U takes back the drawing's step before the routine, as before, and the routine's step then starts from there. Once the routine ends it is still one undo step. The routine's steps live in the session's LISP state (RoutineSteps), not in the drawing's undo stack, so a long routine doesn't push the drawing's earlier steps out. A command inside the routine ends what REDO could bring back; UNDO Control One and None apply. DocState::swap_in is the one restore step undo, redo and the routine share. Co-authored-by: Claude Opus 5.5 --- crates/engine/src/doc_state.rs | 8 + crates/engine/src/lisp.rs | 85 +++++++++- crates/engine/src/session/execute.rs | 43 +++-- crates/engine/src/session/interactive.rs | 6 +- .../engine/tests/it/lisp_undo_in_routine.rs | 154 ++++++++++++++++++ crates/engine/tests/it/main.rs | 1 + 6 files changed, 278 insertions(+), 19 deletions(-) create mode 100644 crates/engine/tests/it/lisp_undo_in_routine.rs diff --git a/crates/engine/src/doc_state.rs b/crates/engine/src/doc_state.rs index d11c6f0eb..97a1ede49 100644 --- a/crates/engine/src/doc_state.rs +++ b/crates/engine/src/doc_state.rs @@ -72,6 +72,14 @@ impl DocState { pub fn is_dirty(&self) -> bool { !Arc::ptr_eq(self.drawing(), &self.saved) || self.block_unsaved() } + /// Show the drawing an undo or redo step holds, with nothing selected; returns the drawing as + /// it was, as the step back. + pub(crate) fn swap_in(&mut self, snap: Snapshot) -> Snapshot { + let cur = Snapshot { label: snap.label, doc: std::mem::replace(&mut self.doc, snap.doc), selection: std::mem::take(&mut self.selection) }; + self.follow_layout(&cur.doc); + self.revision += 1; + cur + } /// After undo/redo replaced the drawing (`before` is the one shown until then): if the current /// layout no longer exists, follow it to its other name when it was renamed (same tab order, /// a name `before` did not have), else go to the Model tab. diff --git a/crates/engine/src/lisp.rs b/crates/engine/src/lisp.rs index b3175c512..977d4f551 100644 --- a/crates/engine/src/lisp.rs +++ b/crates/engine/src/lisp.rs @@ -18,7 +18,7 @@ use std::sync::Arc; use cadcraft_doc::{Drawing, Handle}; -use crate::{Input, Prompt, Session}; +use crate::{Input, Prompt, Session, undoctl}; use eval::{Ctx, Machine, Need, Outcome}; use value::{Sym, Value}; @@ -111,6 +111,8 @@ pub struct LispState { run: Option>, /// A routine runs, from its start to its end (also while its machine is out of `run`). active: bool, + /// The running routine's own undo steps (also while its machine is out of `run`). + steps: RoutineSteps, /// Files loaded by APPLOAD, by name, for `(load name)` where there is no file system. files: HashMap, /// Where startup files are read from (see `startup.rs`). @@ -123,6 +125,19 @@ pub struct LispState { pub(crate) limits: Limits, } +/// What U and REDO inside the running routine take back: the commands it ran, each as the drawing +/// was before it, last last. The routine is still one undo step once it ends. +#[derive(Default)] +struct RoutineSteps { + /// The drawing the routine runs in. + uid: u64, + undo: std::collections::VecDeque, + redo: std::collections::VecDeque, + /// Where the routine's undo step starts, when U or REDO inside it went on to the drawing's + /// own steps; else where the routine started. + base: Option<(Arc, Vec)>, +} + /// What a run may use (see [`value`]'s memory notes). #[derive(Clone, Copy, Debug)] pub(crate) struct Limits { @@ -247,6 +262,7 @@ impl Session { let st = self.state()?; let before = Some((st.doc.clone(), st.selection.clone())); let uid = st.uid; + self.lisp.steps = RoutineSteps { uid, ..RoutineSteps::default() }; self.lisp.run = Some(Box::new(Run { machine, uid, @@ -324,7 +340,7 @@ impl Session { Ok(o) => o, Err(p) => { // A panic inside a built-in: put the drawing back as the routine found it. - if let Some((doc, sel)) = &run.before + if let Some((doc, sel)) = self.lisp.steps.base.as_ref().or(run.before.as_ref()) && let Some(st) = self.docs.iter_mut().find(|d| d.uid == run.uid) { st.doc = doc.clone(); @@ -422,6 +438,7 @@ impl Session { fn lisp_end(&mut self, result: std::result::Result) { let Some(run) = self.lisp.run.take() else { return }; self.lisp.active = false; + let steps = std::mem::take(&mut self.lisp.steps); let mut env = self.lisp.envs.remove(&run.uid).unwrap_or_default(); self.lisp_flush(&mut env, true); self.lisp.envs.insert(run.uid, env); @@ -431,7 +448,7 @@ impl Session { self.echo(text); } } - if let Some((doc, sel)) = run.before + if let Some((doc, sel)) = steps.base.or(run.before) && let Some(st) = self.docs.iter_mut().find(|d| d.uid == run.uid) && !Arc::ptr_eq(&doc, &st.doc) { @@ -489,6 +506,7 @@ impl Session { self.lisp_error(LispError::quit(msg)); } else if self.lisp.active { self.lisp.active = false; + self.lisp.steps = RoutineSteps::default(); self.echo(format!("; error: {msg}")); self.lisp_last = Some(Err(msg)); } else { @@ -577,6 +595,67 @@ impl Session { self.lisp.active } + /// A command the running routine ran changed the drawing (`snap` holds it as it was): U inside + /// the routine takes it back, as AutoCAD's does, while the routine stays one undo step. UNDO + /// Control One keeps only the last, None none. + pub(crate) fn lisp_record_step(&mut self, snap: crate::Snapshot) { + let Some(uid) = self.routine_steps() else { return }; + let mut control = undoctl::Control::All; + // A command ends what REDO could bring back, the drawing's own steps too. + if let Some(st) = self.docs.iter_mut().find(|d| d.uid == uid) { + st.redo.clear(); + control = st.undo_ctl.control; + } + let steps = &mut self.lisp.steps; + steps.redo.clear(); + match control { + undoctl::Control::None => steps.undo.clear(), + undoctl::Control::One => { + steps.undo.clear(); + steps.undo.push_back(snap); + } + // As many as the drawing keeps. + undoctl::Control::All => { + steps.undo.push_back(snap); + if steps.undo.len() > undoctl::MAX_UNDO { + steps.undo.pop_front(); + } + } + } + } + + /// U (or with `redo`, REDO) inside the running routine: take its last command back (or bring + /// back what U took). None when it has none left, and the drawing's own steps go on from there. + pub(crate) fn lisp_take_back(&mut self, redo: bool) -> Option { + let uid = self.routine_steps()?; + let st = self.docs.iter_mut().find(|d| d.uid == uid)?; + let steps = &mut self.lisp.steps; + let (from, to) = if redo { (&mut steps.redo, &mut steps.undo) } else { (&mut steps.undo, &mut steps.redo) }; + let cur = st.swap_in(from.pop_back()?); + let label = cur.label.clone(); + to.push_back(cur); + Some(label) + } + + /// U or REDO inside the running routine went on to the drawing's steps before it: the + /// routine's own undo step starts from the drawing as that left it, and REDO brings back that + /// step before what U took back of the routine's. + pub(crate) fn lisp_rebase(&mut self) { + if self.routine_steps().is_none() { + return; + } + if let Ok(st) = self.state() { + self.lisp.steps.base = Some((st.doc.clone(), st.selection.clone())); + self.lisp.steps.redo.clear(); + } + } + + /// The active drawing's uid when a routine runs in it. + fn routine_steps(&self) -> Option { + let uid = self.state().ok()?.uid; + (self.lisp.active && self.lisp.steps.uid == uid).then_some(uid) + } + /// The commands the active drawing's LISP defines (`c:` functions), uppercase and sorted. pub fn lisp_commands(&self) -> Vec { let Ok(uid) = self.state().map(|s| s.uid) else { return Vec::new() }; diff --git a/crates/engine/src/session/execute.rs b/crates/engine/src/session/execute.rs index a4c57eec5..230422e09 100644 --- a/crates/engine/src/session/execute.rs +++ b/crates/engine/src/session/execute.rs @@ -66,17 +66,25 @@ impl Session { (r, _) => r, }; let grouped = self.lisp_groups_undo(); + let mut in_routine = None; if spec.undoable && let Some((doc, sel, uid)) = before && let Some(st) = self.docs.iter_mut().find(|d| d.uid == uid) && !Arc::ptr_eq(&doc, &st.doc) { - // Inside a LISP routine the routine's single undo step records it. - if !grouped { - st.push_undo(Snapshot { label: spec.label.to_string(), doc, selection: sel }); + let snap = Snapshot { label: spec.label.to_string(), doc, selection: sel }; + // Inside a LISP routine the routine's single undo step records it, and the routine + // keeps it for U inside it. + if grouped { + in_routine = Some(snap); + } else { + st.push_undo(snap); } st.revision += 1; } + if let Some(snap) = in_routine { + self.lisp_record_step(snap); + } result } @@ -89,27 +97,32 @@ impl Session { // ---------------- undo ---------------- + /// Take the last step back. Inside a LISP routine, the routine's last command first; past + /// those, the drawing's step before the routine, which then starts from there. pub fn undo(&mut self) -> Result> { + if let Some(label) = self.lisp_take_back(false) { + return Ok(Some(label)); + } let st = self.state_mut()?; let Some(snap) = st.undo.pop() else { return Ok(None) }; - let cur = Snapshot { label: snap.label.clone(), doc: st.doc.clone(), selection: st.selection.clone() }; - st.doc = snap.doc; - st.follow_layout(&cur.doc); - st.selection = Vec::new(); + let cur = st.swap_in(snap); + let label = cur.label.clone(); st.redo.push(cur); - st.revision += 1; - Ok(Some(snap.label)) + self.lisp_rebase(); + Ok(Some(label)) } + /// Bring back what the last undo took back; inside a LISP routine, what U inside it took first. pub fn redo(&mut self) -> Result> { + if let Some(label) = self.lisp_take_back(true) { + return Ok(Some(label)); + } let st = self.state_mut()?; let Some(snap) = st.redo.pop() else { return Ok(None) }; - let cur = Snapshot { label: snap.label.clone(), doc: st.doc.clone(), selection: st.selection.clone() }; - st.doc = snap.doc; - st.follow_layout(&cur.doc); - st.selection = Vec::new(); + let cur = st.swap_in(snap); + let label = cur.label.clone(); st.undo.push(cur); - st.revision += 1; - Ok(Some(snap.label)) + self.lisp_rebase(); + Ok(Some(label)) } } diff --git a/crates/engine/src/session/interactive.rs b/crates/engine/src/session/interactive.rs index d90c91683..aa66bd4c5 100644 --- a/crates/engine/src/session/interactive.rs +++ b/crates/engine/src/session/interactive.rs @@ -379,12 +379,16 @@ impl Session { } self.after_command(Some(&run.before), None, true); // A transparent command's changes belong to the command it ran inside, and a command a - // LISP routine ran belongs to the routine's single undo step. + // LISP routine ran belongs to the routine's single undo step (the routine keeps it for U + // inside it). if self.suspended.is_some() || self.lisp_groups_undo() { if let Ok(st) = self.state_mut() && !Arc::ptr_eq(&run.before, &st.doc) { st.revision += 1; + if self.suspended.is_none() { + self.lisp_record_step(Snapshot { label: label.to_string(), doc: run.before, selection: run.selection_before }); + } } return; } diff --git a/crates/engine/tests/it/lisp_undo_in_routine.rs b/crates/engine/tests/it/lisp_undo_in_routine.rs new file mode 100644 index 000000000..9afa3c9f5 --- /dev/null +++ b/crates/engine/tests/it/lisp_undo_in_routine.rs @@ -0,0 +1,154 @@ +//! U, UNDO and REDO inside a LISP routine, as AutoCAD 2024 runs them (accoreconsole: a line A, a +//! line B, a circle C and a line D, then one routine, then the drawing's `entnext` order read back). + +use cadcraft_engine::Session; +use cadcraft_engine::cmd::file::{EntityCodec, set_entity_codec}; +use cadcraft_engine::doc::Handle; +use serde_json::json; + +/// A session with A, B, C and D in draw order, each its own undo step, and their handles. +fn drawing() -> (Session, Vec) { + set_entity_codec(EntityCodec { groups: cadcraft_io::entity_groups, entity: cadcraft_io::entity_from_groups }); + let mut s = Session::new(); + s.execute("line", &json!({ "points": [[0, 0], [10, 0]] })).unwrap(); + s.execute("line", &json!({ "points": [[0, 10], [10, 10]] })).unwrap(); + s.execute("circle", &json!({ "center": [5, 25], "radius": 3 })).unwrap(); + s.execute("line", &json!({ "points": [[0, 40], [10, 40]] })).unwrap(); + let hs = handles(&s); + (s, hs) +} + +fn handles(s: &Session) -> Vec { + s.doc().unwrap().model.iter().map(|e| e.handle).collect() +} + +/// The drawing's objects in draw order: A to D by their handles, E for any other. +fn order(s: &Session, hs: &[Handle]) -> String { + let name = |h: &Handle| hs.iter().position(|x| x == h).map_or('E', |i| (b'A' + i as u8) as char); + handles(s).iter().map(|h| name(h).to_string()).collect::>().join(" ") +} + +/// Run `parts` as ONE routine, `@A`…`@D` standing for the objects. +fn routine(s: &mut Session, hs: &[Handle], parts: &[&str]) { + let mut code = format!("(progn {})", parts.join(" ")); + for (i, h) in hs.iter().enumerate() { + code = code.replace(&format!("@{}", (b'A' + i as u8) as char), &format!("(handent \"{}\")", h.hex())); + } + s.cmdline(&code).unwrap(); + assert!(s.running.is_none(), "{code}: {}", s.prompt_text()); +} + +const EB: &str = "(command \"_.ERASE\" @B \"\")"; +const EC: &str = "(command \"_.ERASE\" @C \"\")"; +const U: &str = "(command \"_.U\")"; +const LINE: &str = "(command \"_.LINE\" '(0.0 50.0) '(10.0 50.0) \"\")"; +const MAKE: &str = "(entmake '((0 . \"LINE\") (8 . \"0\") (10 0.0 60.0 0.0) (11 10.0 60.0 0.0)))"; + +#[test] +fn u_in_a_routine_takes_back_the_routines_last_command() { + // (case, the routine, the drawing's order afterwards) + let cases: [(&str, &[&str], &str); 9] = [ + ("U-001: ERASE, U", &[EB, U], "A B C D"), + ("U-002: two erases, U", &[EB, EC, U], "A C D"), + ("U-003: two erases, U, U", &[EB, EC, U, U], "A B C D"), + ("U-005: entdel, ERASE, U", &["(entdel @B)", EC, U], "A C D"), + ("U-006: ERASE, entdel, U", &[EB, "(entdel @C)", U], "A B C D"), + ("U-007: LINE, U, REDO", &[LINE, U, "(command \"_.REDO\")"], "A B C D E"), + ("U-008: two erases, UNDO 2", &[EB, EC, "(command \"_.UNDO\" \"2\")"], "A B C D"), + ("U-012: ERASE, entmake, U", &[EB, MAKE, U], "A B C D"), + // OOPS-011 had an OOPS after the U, which brought B back: B was still erased. + ("OOPS-011: ERASE, LINE, U", &[EB, LINE, U], "A C D"), + ]; + for (case, parts, want) in cases { + let (mut s, hs) = drawing(); + routine(&mut s, &hs, parts); + assert_eq!(order(&s, &hs), want, "{case}"); + } +} + +#[test] +fn the_routine_is_still_one_undo_step_after_it() { + let (mut s, hs) = drawing(); + let depth = s.state().unwrap().undo.len(); + routine(&mut s, &hs, &[EB, EC, LINE, U, "(entdel @A)"]); + assert_eq!(order(&s, &hs), "D"); + assert_eq!(s.state().unwrap().undo.len(), depth + 1); + s.cmdline("U").unwrap(); + assert_eq!(order(&s, &hs), "A B C D"); + s.cmdline("REDO").unwrap(); + assert_eq!(order(&s, &hs), "D"); +} + +#[test] +fn u_with_nothing_of_the_routine_left_takes_back_the_step_before_it() { + let (mut s, hs) = drawing(); + let depth = s.state().unwrap().undo.len(); + routine(&mut s, &hs, &[EB, U, U]); + // The second U takes back D's line, drawn before the routine. + assert_eq!(order(&s, &hs), "A B C"); + assert_eq!(s.state().unwrap().undo.len(), depth - 1, "the routine changed nothing more of its own"); + // A command after that makes the routine's step, from where the U took the drawing. + routine(&mut s, &hs, &[EB, U, U, "(command \"_.ERASE\" @A \"\")"]); + assert_eq!(order(&s, &hs), "B"); + s.cmdline("U").unwrap(); + assert_eq!(order(&s, &hs), "A B"); +} + +#[test] +fn undo_off_still_refuses_inside_a_routine() { + let (mut s, hs) = drawing(); + routine(&mut s, &hs, &["(command \"_.UNDO\" \"_C\" \"_N\")", EB, U]); + assert_eq!(order(&s, &hs), "A C D"); +} + +#[test] +fn redo_inside_a_routine_brings_back_the_last_u_first() { + // U past the routine's own command, then REDO: the drawing's step comes back, not the erase. + let (mut s, hs) = drawing(); + let depth = s.state().unwrap().undo.len(); + routine(&mut s, &hs, &[EB, U, U, "(command \"_.REDO\")"]); + assert_eq!(order(&s, &hs), "A B C D"); + assert_eq!(s.state().unwrap().undo.len(), depth, "the routine left nothing of its own to undo"); + // A command inside the routine ends what REDO could bring back from before it. + let (mut s, hs) = drawing(); + s.cmdline("U").unwrap(); + assert_eq!(order(&s, &hs), "A B C"); + routine(&mut s, &hs, &[EB, "(command \"_.REDO\")"]); + assert_eq!(order(&s, &hs), "A C"); +} + +#[test] +fn a_command_without_prompts_is_a_step_of_the_routine_too() { + // HATCHTOBACK runs as its JSON form from a routine; U takes it back, not the hatch. + let mut s = Session::new(); + s.execute("rectang", &json!({ "p1": [0, 0], "p2": [10, 10] })).unwrap(); + let rect = handles(&s)[0]; + s.execute("hatch", &json!({ "handles": [rect.hex()], "pattern": "SOLID" })).unwrap(); + let both = handles(&s); + assert_eq!(both.len(), 2); + s.cmdline("(progn (command \"_.HATCHTOBACK\") (command \"_.U\"))").unwrap(); + assert_eq!(handles(&s), both); +} + +#[test] +fn undo_control_one_keeps_only_the_routines_last_command() { + // As UNDO Control One keeps only the drawing's last step: the second U goes on to the step + // before the routine (D's line), the drawing as it was then. + let (mut s, hs) = drawing(); + routine(&mut s, &hs, &["(command \"_.UNDO\" \"_C\" \"_O\")", EB, EC, U, U]); + assert_eq!(order(&s, &hs), "A B C"); +} + +#[test] +fn a_routine_keeps_as_many_steps_as_the_drawing() { + use cadcraft_engine::undoctl::MAX_UNDO; + let (mut s, hs) = drawing(); + // Two lines more than the routine keeps steps for, then a U for each kept step and one more, + // which goes on to the drawing's step before the routine (D's line): the drawing as it was + // then, without the two lines whose steps went. + let code = + format!("(progn (repeat {} (command \"_.LINE\" '(0.0 50.0) '(10.0 50.0) \"\")) (repeat {} (command \"_.U\")))", MAX_UNDO + 2, MAX_UNDO + 1); + s.cmdline(&code).unwrap(); + assert!(s.running.is_none()); + assert_eq!(order(&s, &hs), "A B C"); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 2aae7c117..65b8e244f 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -73,6 +73,7 @@ mod layout_prompts; mod leader_command; #[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] mod lisp_corpus; +mod lisp_undo_in_routine; mod malformed_handles; mod matchprop; mod measure_distance_snapping; From fb05c7859030ca7063228bd4e184044ccc278814 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Mon, 2 Nov 2026 02:38:14 +0700 Subject: [PATCH 032/100] Read bearings and compass points as AutoCAD does (#2590) Measured in AutoCAD 2024 (accoreconsole): - N, E, S and W are taken on their own in any angle format (LINE to @10 --- crates/engine/src/cmd/props/dash_units.rs | 13 +--- crates/engine/src/units.rs | 48 +++++++++------ crates/engine/tests/it/angle_settings.rs | 74 ++++++++++++++++++++++- crates/engine/tests/it/dash_units.rs | 65 +++----------------- 4 files changed, 112 insertions(+), 88 deletions(-) diff --git a/crates/engine/src/cmd/props/dash_units.rs b/crates/engine/src/cmd/props/dash_units.rs index a10153f56..95b6bc021 100644 --- a/crates/engine/src/cmd/props/dash_units.rs +++ b/crates/engine/src/cmd/props/dash_units.rs @@ -66,17 +66,10 @@ impl Units { } /// A typed direction of angle 0, in radians counterclockwise from +X: an amount in the angle - /// format, or with its own unit (`45d`, `0.5r`, `100g`, a bearing `N45dE`, and in surveyor's - /// units N, E, S or W), measured from +X as the drawing measures angles. + /// format, or with its own unit (`45d`, `0.5r`, `100g`, a bearing `N45dE`, `N`), measured + /// from +X as the drawing measures angles. fn direction(&self, t: &str) -> Option { - let cardinal = match t.trim().to_ascii_lowercase().as_str() { - "e" if self.aunits == 4 => Some(0.0), - "n" if self.aunits == 4 => Some(90f64.to_radians()), - "w" if self.aunits == 4 => Some(180f64.to_radians()), - "s" if self.aunits == 4 => Some(270f64.to_radians()), - _ => None, - }; - let a = cardinal.or_else(|| AngleSettings { aunits: self.aunits, ..AngleSettings::default() }.amount(t))?; + let a = AngleSettings { aunits: self.aunits, ..AngleSettings::default() }.amount(t)?; let a = if self.clockwise { -a } else { a }; a.is_finite().then(|| a.rem_euclid(TAU)) } diff --git a/crates/engine/src/units.rs b/crates/engine/src/units.rs index 560551f1c..edfa24377 100644 --- a/crates/engine/src/units.rs +++ b/crates/engine/src/units.rs @@ -277,7 +277,8 @@ impl AngleSettings { } /// A typed angle size in radians (included angles, sweeps, angles from an axis): a bare - /// number in AUNITS, or with an explicit unit: `30d`, `45d30'15"`, `0.5r`, `100g`. + /// number in AUNITS, or with an explicit unit: `30d`, `45d30'15"`, `0.5r`, `100g`; a + /// surveyor's bearing (`N45dE`, or `N`, `E`, `S`, `W`) is its angle counterclockwise from east. pub fn amount(&self, s: &str) -> Option { parse_units(s, self.aunits).map(|(a, _)| a) } @@ -288,20 +289,19 @@ impl AngleSettings { } /// A typed direction in radians counterclockwise from +X: the amount measured from ANGBASE in - /// the ANGDIR sense. Surveyor's bearings (`N45dE`) are absolute. The overrides measure from - /// +X counterclockwise whatever ANGBASE and ANGDIR say: `< Option { let t = s.trim(); if let Some(r) = t.strip_prefix("<<<") { return parse_units(r, self.aunits).map(|(a, _)| a); } if let Some(r) = t.strip_prefix("<<") { - return parse_units(r, 0).map(|(a, _)| a); - } - let (a, absolute) = parse_units(t, self.aunits)?; - if absolute { - return Some(a); + return parse_units(r, 0).filter(|(_, bearing)| !bearing).map(|(a, _)| a); } + let (a, _) = parse_units(t, self.aunits)?; let v = self.angbase + if self.clockwise { -a } else { a }; v.is_finite().then_some(v) } @@ -328,7 +328,8 @@ pub fn parse_angle(s: &str) -> Option { AngleSettings::default().direction(s) } -/// A typed angle in radians and whether it is absolute (a surveyor's bearing). +/// A typed angle in radians and whether it is a surveyor's bearing (`N45dE`, or `N`, `E`, `S`, +/// `W`), whose angle is its direction counterclockwise from east. fn parse_units(s: &str, aunits: i64) -> Option<(f64, bool)> { let t = s.trim().to_ascii_lowercase(); if t.is_empty() { @@ -339,10 +340,7 @@ fn parse_units(s: &str, aunits: i64) -> Option<(f64, bool)> { (num(r)?, false) } else if let Some(g) = t.strip_suffix('g') { ((num(g)? * 0.9).to_radians(), false) - } else if let Some(b) = bearing(&t) { - (b, true) - } else if let Some(b) = (aunits == 4).then(|| cardinal(&t)).flatten() { - // A due bearing as surveyor's units show it: one letter. + } else if let Some(b) = cardinal(&t).or_else(|| bearing(&t)) { (b, true) } else if let Some(v) = num(&t) { let a = match aunits { @@ -357,7 +355,8 @@ fn parse_units(s: &str, aunits: i64) -> Option<(f64, bool)> { v.0.is_finite().then_some(v) } -/// A due bearing (`n`, `e`, `s`, `w`, lower case) as radians counterclockwise from +X. +/// A compass point (`n`, `e`, `s`, `w`, lower case), a due bearing as surveyor's units show it, +/// as radians counterclockwise from +X. AutoCAD 2024 takes one in any angle format. fn cardinal(t: &str) -> Option { match t { "e" => Some(0.0), @@ -567,14 +566,19 @@ mod tests { #[test] fn surveyors_units_read_a_due_bearing_typed_as_one_letter() { - // What surveyor's units show for a due bearing can be typed back, e.g. `@10 assert_near(l.b.xy(), 0.0, -10.0, "ROTATE by N with ANGDIR 1"), + k => panic!("expected a line, got {k:?}"), + } + // ANGTOF reads them the same way: AutoCAD 2024 returns 0 for "N" with ANGBASE 90, ANGDIR 1 + // and AUNITS 3, and 4.18879020478639 for "S 30d W". + let mut s = Session::new(); + setvar(&mut s, "AUNITS", json!(3)); + setvar(&mut s, "ANGBASE", json!(90)); + setvar(&mut s, "ANGDIR", json!(1)); + let n = s.lisp_eval("(angtof \"N\")", false, true, Vec::new()).unwrap(); + assert!(matches!(n, Some(Ok(Value::Real(a))) if a.abs() < 1e-12), "{n:?}"); + let mut s = Session::new(); + setvar(&mut s, "AUNITS", json!(4)); + let sw = s.lisp_eval("(angtof \"S 30d W\")", false, true, Vec::new()).unwrap(); + assert!(matches!(sw, Some(Ok(Value::Real(a))) if (a - 4.18879020478639).abs() < 1e-12), "{sw:?}"); } #[test] diff --git a/crates/engine/tests/it/dash_units.rs b/crates/engine/tests/it/dash_units.rs index 5c8c3e761..f554378d8 100644 --- a/crates/engine/tests/it/dash_units.rs +++ b/crates/engine/tests/it/dash_units.rs @@ -39,7 +39,7 @@ fn run(s: &mut Session, answers: &str) { #[test] fn the_measured_scripts_set_the_units() { - let cases: [(Units, &str, Units); 21] = [ + let cases: [(Units, &str, Units); 22] = [ (DEFAULT, "4 16 1 0 0 \"_N\"", (4, 4, 0, 0, 0.0, 0)), (DEFAULT, "3 2 2 4 90 \"_Y\"", (3, 2, 1, 4, FRAC_PI_2, 1)), (DEFAULT, "2 3 3 2 100 \"_N\"", (2, 3, 2, 2, FRAC_PI_2, 0)), @@ -61,6 +61,7 @@ fn the_measured_scripts_set_the_units() { (DEFAULT, "\"\" \"\" \"\" \"\" \"\" \"Yes\"", (2, 4, 0, 0, 0.0, 1)), ((2, 4, 2, 6, 0.0, 0), "\"\" \"\" \"\" \"\" \"\" \"\"", (2, 4, 2, 6, 0.0, 0)), ((2, 4, 4, 2, 0.0, 1), "\"\" \"\" \"\" \"\" \"S45dE\" \"\"", (2, 4, 4, 2, 0.7853981633974482, 1)), + (DEFAULT, "\"\" \"\" \"\" \"\" \"N\" \"\"", (2, 4, 0, 0, FRAC_PI_2, 0)), ]; for (from, answers, to) in cases { let mut s = session(from); @@ -112,15 +113,7 @@ fn prompts_and_listings_show_the_drawings_units() { // Surveyor's units, clockwise: north is listed as S, as AutoCAD lists it. let mut s = session((2, 4, 0, 0, 0.0, 1)); - s.script( - "-units - - -5 - -", - ) - .unwrap(); + s.script("-units\n\n\n5\n\n").unwrap(); let directions = [ "Direction for angle E:", " East 3 o'clock = E", @@ -133,15 +126,7 @@ fn prompts_and_listings_show_the_drawings_units() { // The listing shows directions to the precision just given. let mut s = session(DEFAULT); - s.script( - "-units - - -2 -2 -", - ) - .unwrap(); + s.script("-units\n\n\n2\n2\n").unwrap(); let directions = [ "Direction for angle 0d0':", " East 3 o'clock = 0d0'", @@ -166,27 +151,11 @@ fn a_rejected_answer_is_asked_again() { s.cmdline("3").unwrap(); assert!(ends_with(&s, &["Invalid choice.", "Enter denominator of smallest fraction to display"]), "{:?}", s.log); assert_eq!(asks(&s), "(1, 2, 4, 8, 16, 32, 64, 128, or 256) <16>:"); - s.script( - " - -9 -", - ) - .unwrap(); + s.script("\n\n9\n").unwrap(); assert!(ends_with(&s, &["Requires an integer between 0 and 8."]), "{:?}", s.log); - s.script( - " -abc -", - ) - .unwrap(); + s.script("\nabc\n").unwrap(); assert!(ends_with(&s, &["Requires valid numeric angle or two points."]), "{:?}", s.log); - s.script( - " - -", - ) - .unwrap(); + s.script("\n\n").unwrap(); assert!(s.running.is_none()); assert!(same(units(&s), (4, 4, 0, 0, 0.0, 0)), "{:?}", units(&s)); } @@ -196,28 +165,12 @@ fn each_answer_is_kept_as_it_is_given() { // Esc after the angle format: the answers before it are kept, and the new angle format's // precision is reset to 4. let mut s = session(DEFAULT); - s.script( - "-units -4 -16 -2 -", - ) - .unwrap(); + s.script("-units\n4\n16\n2\n").unwrap(); s.cancel(); assert!(s.running.is_none()); assert!(same(units(&s), (4, 4, 1, 4, 0.0, 0)), "{:?}", units(&s)); let mut s = session(DEFAULT); - s.script( - "-units -3 -2 -3 -5 -90 -", - ) - .unwrap(); + s.script("-units\n3\n2\n3\n5\n90\n").unwrap(); s.cancel(); assert!(same(units(&s), (3, 2, 2, 5, 1.413716694115407, 0)), "{:?}", units(&s)); } From 5cc342b033728fe8261273abd0a578c421af61d5 Mon Sep 17 00:00:00 2001 From: Andrea Ficarra Date: Sun, 1 Nov 2026 20:38:17 +0100 Subject: [PATCH 033/100] DXF/DWG: viewport frozen layers as group 331 (341 in an R2000 DXF) (#2575) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * DXF/DWG: viewport frozen layers as group 331 (341 in an R2000 DXF) AutoCAD 2004 and later, and the DWG converter, read a VIEWPORT's frozen layers from group 331; CADCraft wrote only group 341 (the DXF 2000 form), so a DWG or 2004+ DXF lost the freeze outside CADCraft, and a file frozen only in group 331 (as AutoCAD 2026 and ezdxf write it) lost it on the first save. Now: group 341 in an R2000 DXF, 331 otherwise and for the DWG bridge; both are read, each layer once. CADCraft's own xdata still wins. Tests in crates/io/src/tests.rs, including a_331_only_viewport_freeze_survives_open_and_save. Co-Authored-By: Claude Opus 5.5 * Leave the parity docs to the maintainers CLAUDE.md: PRs don't edit the parity docs; the affected rows are named in the PR description instead. Co-Authored-By: Claude Opus 5.5 * Viewport frozen layers: group 331 wins over stale xdata AutoCAD now sees the freeze in group 331, edits it, and keeps CADCraft's VPFROZEN xdata as it was. Reading preferred the xdata, so a layer thawed in AutoCAD came back frozen. The handles now win; the xdata still gives names that have no LAYER record, and the whole list for records without handles (entget/entmod, older files). The handles are written before group 90, as in the DXF Reference. Test: viewport_frozen_331_wins_over_the_xdata. Co-Authored-By: Claude Opus 5.5 * Viewport frozen layers: handles for real layers, xdata only for the rest Simpler and safe against edits in AutoCAD. A layer with a LAYER record is written only as a handle (331, or 341 in an R2000 DXF); CADCraft's VPFROZEN xdata now holds only the names with no record (and the whole list for entget, which writes no handles). Reading takes the handles, then the xdata names, each layer once: no precedence, and a viewport thawed in AutoCAD (even its last frozen layer) stays thawed, since no stale list is left behind. The reader no longer builds a layer-name set per viewport. Tests: viewport_frozen_layers_read_from_331_or_341_once_each, viewport_freeze_edited_in_autocad_is_read_as_edited. Co-Authored-By: Claude Opus 5.5 * Viewport frozen layers: skip 102 application groups A group 331 inside another application's `102 {…}` group isn't a frozen layer. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/io/src/dxf_read.rs | 2 +- crates/io/src/dxf_read/entities.rs | 22 ++++--- crates/io/src/dxf_write.rs | 6 +- crates/io/src/dxf_write/ctx.rs | 3 + crates/io/src/dxf_write/entity.rs | 22 +++---- crates/io/src/dxf_write/sections.rs | 5 +- crates/io/src/tests.rs | 89 ++++++++++++++++++----------- 7 files changed, 93 insertions(+), 56 deletions(-) diff --git a/crates/io/src/dxf_read.rs b/crates/io/src/dxf_read.rs index 1489f4def..5b8865953 100644 --- a/crates/io/src/dxf_read.rs +++ b/crates/io/src/dxf_read.rs @@ -59,7 +59,7 @@ struct Rx { pstyles: HashMap, /// Upper-case block name → associative array parameters from its BLOCK_RECORD. block_arrays: HashMap, - /// LAYER record handle (upper case) → layer name (VIEWPORT group 341 frozen layers). + /// LAYER record handle (upper case) → layer name (VIEWPORT group 331/341 frozen layers). layers: HashMap, } diff --git a/crates/io/src/dxf_read/entities.rs b/crates/io/src/dxf_read/entities.rs index 12abae7da..a074a6517 100644 --- a/crates/io/src/dxf_read/entities.rs +++ b/crates/io/src/dxf_read/entities.rs @@ -466,14 +466,20 @@ pub(super) fn entity_extras(kind: &str, tags: &[Tag], h: Handle, k: &mut EntityK } rx.mleader_fix.push((h, pending)); } - // CADCraft's VPFROZEN xdata wins; otherwise AutoCAD's group 341 layer handles. - ("VIEWPORT", EntityKind::Viewport(v)) if v.frozen_layers.is_empty() => { - v.frozen_layers = tags - .iter() - .take_while(|t| t.code < 1000) - .filter(|t| t.code == 341) - .filter_map(|t| rx.layers.get(&t.str().trim().to_ascii_uppercase()).cloned()) - .collect(); + // The frozen layers: the LAYER handles of group 331 (341 in DXF 2000), outside `102 {…}` + // application groups, then the names in CADCraft's VPFROZEN xdata, which holds those + // without one; each layer once. + ("VIEWPORT", EntityKind::Viewport(v)) => { + let mut app_group = false; + let handles = tags.iter().take_while(|t| t.code < 1000).filter(|t| { + if t.code == 102 { + app_group = t.str().starts_with('{'); + } + !app_group && (t.code == 331 || t.code == 341) + }); + let named = handles.filter_map(|t| rx.layers.get(&t.str().trim().to_ascii_uppercase())); + let mut seen = std::collections::HashSet::new(); + v.frozen_layers = named.chain(&v.frozen_layers).filter(|n| seen.insert(n.to_ascii_uppercase())).cloned().collect(); } _ => {} } diff --git a/crates/io/src/dxf_write.rs b/crates/io/src/dxf_write.rs index 8eb3ab91f..341f70e62 100644 --- a/crates/io/src/dxf_write.rs +++ b/crates/io/src/dxf_write.rs @@ -41,7 +41,7 @@ struct W { next: u64, /// Upper-case plot style name → its `ACDBPLACEHOLDER` handle (group 390 of layers and objects). pstyles: HashMap, - /// Upper-case layer name → its LAYER table record handle (group 341 of viewports). + /// Upper-case layer name → its LAYER table record handle (group 331/341 of viewports). layers: HashMap, /// Persistent reactors of boundary objects: each source object of an associative hatch → /// the hatches (groups `{ACAD_REACTORS 330 …}`), from [`hatch_reactors`]. @@ -112,13 +112,13 @@ pub fn write(d: &Drawing) -> String { /// The drawing's DXF tags, declaring `version` in `$ACADVER`. pub fn tags(d: &Drawing, version: Version) -> Vec { - tags_for(d, version, version) + tags_for(d, version, version, version < Version::R2004) } /// The R2000 DXF the DWG bridge converts to a `dwg` version DWG: its entity types are the DWG /// version's (MULTILEADER from 2007). pub(crate) fn dwg_bridge(d: &Drawing, dwg: Version) -> String { - let mut tags = tags_for(d, Version::R2000, dwg); + let mut tags = tags_for(d, Version::R2000, dwg, false); bridge_markers(&mut tags); cadcraft_dxf::write_ascii(&tags) } diff --git a/crates/io/src/dxf_write/ctx.rs b/crates/io/src/dxf_write/ctx.rs index 157dcd441..25194bb46 100644 --- a/crates/io/src/dxf_write/ctx.rs +++ b/crates/io/src/dxf_write/ctx.rs @@ -53,6 +53,9 @@ pub(super) struct Ctx { /// The version the file ends up in (the DWG version for a DWG save): it decides which /// entity types exist (MULTILEADER from 2007). pub(super) target: Version, + /// A VIEWPORT's frozen layers in group 341, as an R2000 DXF has them; otherwise 331 (DXF 2004 + /// and later, and the DWG bridge, which reads only 331). + pub(super) frozen_341: bool, /// MTEXT columns as the legacy groups 75..79 even from R2018 (the groups AutoLISP lists). pub(super) legacy_columns: bool, } diff --git a/crates/io/src/dxf_write/entity.rs b/crates/io/src/dxf_write/entity.rs index f5771f106..a33437017 100644 --- a/crates/io/src/dxf_write/entity.rs +++ b/crates/io/src/dxf_write/entity.rs @@ -620,22 +620,24 @@ fn write_viewport_entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: // 340 entity, 131072 = off. let clip = v.clip.filter(|h| *h != e.handle && d.entity(*h).is_some()); let flags = 32768 | if v.locked { 16384 } else { 0 } | if clip.is_some() { 65536 } else { 0 } | if v.on { 0 } else { 131072 }; + // AutoCAD's frozen-layer list, before group 90 as in the DXF Reference: one pointer per + // layer, to its LAYER record. A name with no record goes in CADCraft's xdata below. + let mut unrecorded: Vec = Vec::new(); + for name in &v.frozen_layers { + match w.layers.get(&name.to_ascii_uppercase()).cloned() { + Some(h) => w.s(if cx.frozen_341 { 341 } else { 331 }, h), + None => unrecorded.push(name.clone()), + } + } w.i(90, flags); if let Some(h) = clip { w.s(340, h.hex()); } - // AutoCAD's frozen-layer list: one soft pointer per layer, to its LAYER record. Other - // programs read only this; CADCraft prefers its own xdata below when present. - for name in &v.frozen_layers { - if let Some(h) = w.layers.get(&name.to_ascii_uppercase()).cloned() { - w.s(341, h); - } - } // One CADCraft xdata group holds both lists: readers stop at the first group of an app. - if !v.frozen_layers.is_empty() || !v.layer_colors.is_empty() { + if !unrecorded.is_empty() || !v.layer_colors.is_empty() { w.s(1001, dxf_ext::APP); - if !v.frozen_layers.is_empty() { - w.xdata(dxf_ext::frozen_xdata(&v.frozen_layers)); + if !unrecorded.is_empty() { + w.xdata(dxf_ext::frozen_xdata(&unrecorded)); } if !v.layer_colors.is_empty() { w.xdata(dxf_ext::layer_colors_xdata(&v.layer_colors)); diff --git a/crates/io/src/dxf_write/sections.rs b/crates/io/src/dxf_write/sections.rs index 2d45a532f..e9f8a9e35 100644 --- a/crates/io/src/dxf_write/sections.rs +++ b/crates/io/src/dxf_write/sections.rs @@ -883,7 +883,8 @@ fn plan_dimension_associations(d: &Drawing, all_spaces: &[&EntityStore], w: &mut /// The drawing's DXF tags, declaring `version` in `$ACADVER`, with what `target` (the version the /// file ends up in: `version`, or the DWG version the bridge writes) has. -pub(super) fn tags_for(d: &Drawing, version: Version, target: Version) -> Vec { +/// `frozen_341`: see [`Ctx::frozen_341`]. +pub(super) fn tags_for(d: &Drawing, version: Version, target: Version, frozen_341: bool) -> Vec { // An in-place block edit in progress is not written: the drawing as before it. let committed = d.committed(); let d: &Drawing = &committed; @@ -916,7 +917,7 @@ pub(super) fn tags_for(d: &Drawing, version: Version, target: Version) -> Vec = user_blocks.into_iter().map(|(n, _, b)| (n, w.h(), b)).collect(); - let mut cx = Ctx { paper_views: paper_views(d), target, ..Ctx::default() }; + let mut cx = Ctx { paper_views: paper_views(d), target, frozen_341, ..Ctx::default() }; cx.block_records = user_blocks.iter().map(|(n, h, _)| (n.to_ascii_uppercase(), h.clone())).collect(); let all_spaces: Vec<&EntityStore> = std::iter::once(&d.model).chain(d.layouts.iter().map(|l| &l.entities)).collect(); let mut dim_defs = dimension_block_definitions(d, &all_spaces, &dim_plan, &mut w, &mut cx); diff --git a/crates/io/src/tests.rs b/crates/io/src/tests.rs index 977a7c184..12d9f0a12 100644 --- a/crates/io/src/tests.rs +++ b/crates/io/src/tests.rs @@ -604,6 +604,12 @@ fn viewport_frozen_layers_roundtrip() { assert!(frozen.contains(&(3, Vec::new())), "{frozen:?}"); } +/// `d` as an R2018 DXF (frozen viewport layers in group 331). +fn dxf_2018(d: &Drawing) -> String { + let opts = SaveOptions { version: Some(Version::R2018), binary: false }; + String::from_utf8(write_with(d, "t.dxf", &opts).unwrap()).unwrap() +} + /// A layout with viewport 2 frozen on `Walls` and `A B` (both real layers), and its written DXF as /// (code, value) pairs. fn frozen_vp_dxf() -> (Drawing, Vec<(String, String)>) { @@ -624,10 +630,14 @@ fn frozen_vp_dxf() -> (Drawing, Vec<(String, String)>) { on: true, }); d.add(&paper, Common::default(), vp).unwrap(); - let text = write_dxf(&d); + let text = dxf_2018(&d); + (d, pairs(&text)) +} + +/// The (code, value) pairs of DXF text. +fn pairs(text: &str) -> Vec<(String, String)> { let lines: Vec<&str> = text.lines().collect(); - let pairs = lines.chunks(2).map(|c| (c[0].trim().to_string(), c.get(1).copied().unwrap_or("").to_string())).collect(); - (d, pairs) + lines.chunks(2).map(|c| (c[0].trim().to_string(), c.get(1).copied().unwrap_or("").to_string())).collect() } /// The pair range of the VIEWPORT record whose group 69 is `id`. @@ -676,47 +686,60 @@ fn join(p: &[(String, String)]) -> String { .collect() } -#[test] -fn viewport_frozen_layers_written_as_341_handles() { - let (_, p) = frozen_vp_dxf(); - let r = vp_record(&p, "2"); - let rec = &p[r]; - let h341: Vec<&str> = rec.iter().filter(|(c, _)| c == "341").map(|(_, v)| v.as_str()).collect(); - assert_eq!(h341, vec![layer_handle(&p, "Walls").as_str(), layer_handle(&p, "A B").as_str()]); - let last341 = rec.iter().rposition(|(c, _)| c == "341").unwrap(); - let x = rec.iter().position(|(c, _)| c == "1001").expect("xdata"); - assert!(last341 < x, "341 must precede the xdata"); +/// The group 331 values of viewport 2's record. +fn frozen_331(p: &[(String, String)]) -> Vec { + p[vp_record(p, "2")].iter().filter(|(c, _)| c == "331").map(|(_, v)| v.trim().to_string()).collect() } #[test] -fn viewport_frozen_layers_read_from_341_without_xdata() { +fn viewport_frozen_layers_written_as_331_handles() { + // Real layers go out as handles only, before group 90. let (_, p) = frozen_vp_dxf(); - let r = vp_record(&p, "2"); - // Strip the CADCraft xdata, and add a 341 to a handle that names no layer. - let x = p[r.clone()].iter().position(|(c, _)| c == "1001").unwrap() + r.start; - let mut q: Vec<(String, String)> = p[..r.start].to_vec(); - let mut head = p[r.start..x].to_vec(); - head.push(("341".into(), "FFFFF".into())); - q.extend(head); - q.extend(p[r.end..].iter().cloned()); - let back = read_dxf(join(&q).as_bytes()).unwrap(); - assert_eq!(viewport_frozen(&back, 2), vec!["Walls".to_string(), "A B".to_string()]); + assert_eq!(frozen_331(&p), [layer_handle(&p, "Walls"), layer_handle(&p, "A B")]); + let rec = &p[vp_record(&p, "2")]; + assert!(!rec.iter().any(|(c, v)| c == "1000" && v == "VPFROZEN"), "no xdata for layers with a handle"); + assert!(rec.iter().position(|(c, _)| c == "331") < rec.iter().position(|(c, _)| c == "90")); + // An R2000 DXF holds them in group 341, as the DXF 2000 Reference has them. + let (d, _) = frozen_vp_dxf(); + let p = pairs(&write_dxf(&d)); + let h341: Vec = p[vp_record(&p, "2")].iter().filter(|(c, _)| c == "341").map(|(_, v)| v.clone()).collect(); + assert_eq!(h341, [layer_handle(&p, "Walls"), layer_handle(&p, "A B")]); + assert!(frozen_331(&p).is_empty()); } #[test] -fn viewport_frozen_xdata_wins_over_341() { +fn viewport_frozen_layers_read_from_331_or_341_once_each() { let (_, p) = frozen_vp_dxf(); let r = vp_record(&p, "2"); - // Point every 341 at layer 0: the xdata list must still be what is read. - let zero = layer_handle(&p, "0"); + // 341 instead of 331, then Walls again in 331 and a handle that names no layer. let mut q = p.clone(); - for t in &mut q[r] { - if t.0 == "341" { - t.1 = zero.clone(); + for t in &mut q[r.clone()] { + if t.0 == "331" { + t.0 = "341".into(); } } + q.splice(r.end..r.end, [("331".into(), "FFFFF".into()), ("331".into(), layer_handle(&p, "Walls"))]); + // A 331 inside another application's `102 {…}` group isn't a frozen layer. + let app = [("102".into(), "{ACAD_OTHER".into()), ("331".into(), layer_handle(&p, "0")), ("102".into(), "}".into())]; + q.splice(r.start + 2..r.start + 2, app); let back = read_dxf(join(&q).as_bytes()).unwrap(); - assert_eq!(viewport_frozen(&back, 2), vec!["Walls".to_string(), "A B".to_string()]); + assert_eq!(viewport_frozen(&back, 2), ["Walls", "A B"]); +} + +#[test] +fn viewport_freeze_edited_in_autocad_is_read_as_edited() { + // Every layer thawed in AutoCAD (no 331 left) stays thawed; a layer it froze instead is read. + let (_, p) = frozen_vp_dxf(); + let r = vp_record(&p, "2"); + let thawed: Vec<(String, String)> = p.iter().enumerate().filter(|(i, (c, _))| !(r.contains(i) && c == "331")).map(|(_, t)| t.clone()).collect(); + assert!(viewport_frozen(&read_dxf(join(&thawed).as_bytes()).unwrap(), 2).is_empty()); + let mut zero = p.clone(); + for t in &mut zero[r] { + if t.0 == "331" { + t.1 = layer_handle(&p, "0"); + } + } + assert_eq!(viewport_frozen(&read_dxf(join(&zero).as_bytes()).unwrap(), 2), ["0"]); } #[test] @@ -725,7 +748,9 @@ fn viewport_frozen_layers_survive_dwg() { let dwg = write(&d, "t.dwg").unwrap(); let back = read(&dwg, "t.dwg").unwrap(); assert_eq!(viewport_frozen(&back, 2), vec!["Walls".to_string(), "A B".to_string()]); - // Survives via the CADCraft xdata: the DWG converter does not yet carry group 341 itself. + // The DWG holds them in group 331, what AutoCAD reads. + let p = pairs(&String::from_utf8(cadcraft_dwg::dwg_to_dxf(&dwg).unwrap()).unwrap()); + assert_eq!(frozen_331(&p), [layer_handle(&p, "Walls"), layer_handle(&p, "A B")]); } #[test] From 2d02a5faa599eab78497aad52af056765f29633a Mon Sep 17 00:00:00 2001 From: Andrea Ficarra Date: Sun, 1 Nov 2026 20:38:21 +0100 Subject: [PATCH 034/100] DXF: escape carets in TEXT, ATTDEF, ATTRIB and DIMENSION strings (#2574) * DXF: escape carets in TEXT, ATTDEF, ATTRIB and DIMENSION strings AutoCAD reads `^x` in a DXF string as a control character, so a TEXT of `10^2` opened in AutoCAD showed a control character instead of the caret. The DXF Reference writes a caret as `^` and a space: the writer now does for the text (group 1) of TEXT, ATTRIB, ATTDEF and DIMENSION and an ATTDEF's prompt (group 3), and the reader turns it back. A bare `^x` from older files stays as it is; single-line strings keep `^J` literal (they hold no line feeds). AutoLISP's entget shows the plain caret, and a DIMENSION's style name (also group 3) is left as written. Tests: crates/io/tests/it/text_carets_roundtrip.rs. Co-Authored-By: Claude Opus 5.5 * entget gives TEXT strings as the file has them, like MTEXT Decoding the carets in entity_groups broke entmod (it reads the groups as a file, so `A^ B` came back as `A^B`) and reached into an ATTDEF's embedded MTEXT. entity_groups is left as on main: it gives the file's groups, as it already does for MTEXT, and entmod reads them back unchanged (test entmod_of_entget_keeps_the_strings). Co-Authored-By: Claude Opus 5.5 * entget unescapes carets, entmake and entmod escape them e61b4dd left entget with the file's escapes, but entmake still read a plain `p^ q` as `p^q`. Now one rule, map_carets, picks the groups that hold an escaped string (the text, 1, and an ATTDEF's prompt, 3, up to an embedded MTEXT at group 101): entity_groups unescapes them and entity_from_groups escapes them before reading, so entget is plain, entmake takes plain strings and entmod of entget changes nothing. Tests: entget_shows_plain_carets_and_entmake_takes_them, entmod_leaves_the_embedded_text_alone. Co-Authored-By: Claude Opus 5.5 * Leave the parity docs to the maintainers CLAUDE.md: PRs don't edit the parity docs; the affected rows are named in the PR description instead. Co-Authored-By: Claude Opus 5.5 * Single-line strings read like MTEXT: a tab from AutoCAD survives AutoCAD writes a tab in a TEXT as `^I`. The reader left it as two characters and the writer then saved `^ I`, so the tab was lost. Single-line strings are now read with decode_contents, as MTEXT is (`^ `, `^I`, `^J`; other bare carets stay), and encode_line writes a caret as `^ ` and a tab as `^I`. unescape_carets is gone. Co-Authored-By: Claude Opus 5.5 * Rename map_carets to map_text_groups (it encodes tabs too) Co-Authored-By: Claude Opus 5.5 * Single-line strings keep a bare ^J; entity_from_groups doc A TEXT holding `A^JB` (from a file saved before this change, which wrote carets bare) was read as a line feed and saved as `A B`. Single-line strings now decode only `^ ` and `^I` (decode_line, decode_contents without line feeds), so `^J` stays two characters. entity_from_groups documents that it takes plain strings, as entity_groups gives them. The caret test also goes through a DWG. Co-Authored-By: Claude Opus 5.5 * Test a caret before a space and at the end of a TEXT Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/io/src/dxf_read.rs | 8 +- crates/io/src/dxf_read/entities.rs | 3 +- crates/io/src/dxf_read/mtext_dxf.rs | 37 +++++- crates/io/src/dxf_read/text.rs | 4 +- crates/io/src/dxf_write.rs | 5 +- crates/io/src/dxf_write/common.rs | 4 +- crates/io/src/dxf_write/entity.rs | 2 +- crates/io/tests/it/main.rs | 1 + crates/io/tests/it/multiline_attributes.rs | 15 +++ crates/io/tests/it/text_carets_roundtrip.rs | 125 ++++++++++++++++++++ 10 files changed, 193 insertions(+), 11 deletions(-) create mode 100644 crates/io/tests/it/text_carets_roundtrip.rs diff --git a/crates/io/src/dxf_read.rs b/crates/io/src/dxf_read.rs index 5b8865953..324caa361 100644 --- a/crates/io/src/dxf_read.rs +++ b/crates/io/src/dxf_read.rs @@ -143,9 +143,13 @@ fn read_budgeted(bytes: &[u8], mut table_cells_left: usize) -> Result { } /// An entity from the groups of a DXF record of type `kind` (without its `0` group), as the -/// reader makes it. AutoLISP's `entmake` and `entmod` are built on it. +/// reader makes it, with plain strings as [`crate::entity_groups`] gives them (not the file's +/// `^ ` escapes). AutoLISP's `entmake` and `entmod` are built on it. pub fn entity_from_groups(kind: &str, tags: &[Tag]) -> Option<(Common, EntityKind)> { - entity(&kind.to_ascii_uppercase(), tags) + // AutoLISP's strings are plain: escape them as the file has them (see `entity_groups`). + let mut tags = tags.to_vec(); + mtext_dxf::map_text_groups(kind, &mut tags, mtext_dxf::encode_line); + entity(&kind.to_ascii_uppercase(), &tags) } #[cfg(test)] diff --git a/crates/io/src/dxf_read/entities.rs b/crates/io/src/dxf_read/entities.rs index a074a6517..394feb785 100644 --- a/crates/io/src/dxf_read/entities.rs +++ b/crates/io/src/dxf_read/entities.rs @@ -1,5 +1,6 @@ use super::common::{T, common, ocs}; use super::hatch::hatch; +use super::mtext_dxf; use super::text::{attdef, attrib_modes, attrib_props, mtext_from, multiline_attrib, split_embedded, text_from}; use super::{ Arc, Attrib, Circle, Common, DimKind, Dimension, Ellipse, EntityKind, Face3d, Handle, HashMap, Helix, HelixConstrain, Insert, Leader, Line, @@ -189,7 +190,7 @@ pub(super) fn entity_with_table_budget(kind: &str, tags: &[Tag], table_cells_lef p14: t.p(14), p15: t.p(15), p16: t.p(16), - text: t.s(1).unwrap_or_default(), + text: mtext_dxf::decode_line(&t.s(1).unwrap_or_default()), style: t.s(3).unwrap_or_else(|| "Standard".into()), measurement: t.fd(42, 0.0), text_rotation: t.fd(53, 0.0).to_radians(), diff --git a/crates/io/src/dxf_read/mtext_dxf.rs b/crates/io/src/dxf_read/mtext_dxf.rs index 9d9213947..822384f73 100644 --- a/crates/io/src/dxf_read/mtext_dxf.rs +++ b/crates/io/src/dxf_read/mtext_dxf.rs @@ -1,7 +1,9 @@ //! MTEXT specifics of DXF (DXF Reference, MTEXT): control characters in the contents written //! as a caret and a letter (`^I` a tab, `^J` a line feed, `^ ` a caret), and the column groups //! 75 (type), 76 (count), 78 (flow reversed), 79 (auto height), 48 (width), 49 (gutter) and -//! 50 (column heights, after the column type; before it, 50 is the rotation). +//! 50 (column heights, after the column type; before it, 50 is the rotation). TEXT, ATTDEF, +//! ATTRIB and DIMENSION strings use the same encoding ([`encode_line`]): AutoCAD reads a bare +//! `^2` in `10^2` as a control character. use cadcraft_doc::{MText, MTextColumnType, MTextColumns}; use cadcraft_dxf::Tag; @@ -12,6 +14,15 @@ const MAX_COLUMNS: usize = 1_000; /// Decode the contents' `^I` (tab), `^J` (line feed) and `^ ` (caret). Other carets stay as /// they are (older files write stacked tolerances `\S+0.1^-0.1;` with a bare caret). pub(crate) fn decode_contents(s: &str) -> String { + decode(s, true) +} + +/// A single-line string as read: [`decode_contents`] without line feeds (`^J` stays as it is). +pub(crate) fn decode_line(s: &str) -> String { + decode(s, false) +} + +fn decode(s: &str, line_feeds: bool) -> String { if !s.contains('^') { return s.to_string(); } @@ -25,7 +36,7 @@ pub(crate) fn decode_contents(s: &str) -> String { match it.peek().copied() { Some(' ') => out.push('^'), Some('I') => out.push('\t'), - Some('J') => out.push('\n'), + Some('J') if line_feeds => out.push('\n'), _ => { out.push('^'); continue; @@ -36,6 +47,28 @@ pub(crate) fn decode_contents(s: &str) -> String { out } +/// A single-line string (TEXT, ATTDEF/ATTRIB, DIMENSION) for a DXF file: a caret as `^ ` and a +/// tab as `^I`, read back by [`decode_line`]. It holds no line break (the writer makes it a +/// space). +pub(crate) fn encode_line(s: &str) -> String { + s.replace('^', "^ ").replace('\t', "^I") +} + +/// Apply `f` ([`encode_line`] or [`decode_line`]) to the groups of a `kind` record that hold +/// an encoded string: the text (1), and an ATTDEF's prompt (3). An embedded MTEXT (from group 101) +/// has its own and is left alone. AutoLISP's entget and entmake/entmod use it. +pub(crate) fn map_text_groups(kind: &str, tags: &mut [Tag], f: fn(&str) -> String) { + let kind = kind.to_ascii_uppercase(); + if !matches!(kind.as_str(), "TEXT" | "ATTRIB" | "ATTDEF" | "DIMENSION") { + return; + } + for t in tags.iter_mut().take_while(|t| t.code != 101) { + if t.code == 1 || (t.code == 3 && kind == "ATTDEF") { + *t = Tag::s(t.code, f(&t.str())); + } + } +} + /// Encode the contents for a DXF string: a tab as `^I`, a line feed as `^J`, a caret as `^ `; /// other control characters (which draw nothing) are dropped. pub(crate) fn encode_contents(s: &str) -> String { diff --git a/crates/io/src/dxf_read/text.rs b/crates/io/src/dxf_read/text.rs index 492825df7..df73b878c 100644 --- a/crates/io/src/dxf_read/text.rs +++ b/crates/io/src/dxf_read/text.rs @@ -112,7 +112,7 @@ pub(super) fn text_from(t: &T) -> Text { insert: t.p(10), align_pt: if h == HAlign::Left && v == VAlign::Baseline { None } else { align }, height: t.fd(40, 0.2).max(1e-9), - value: t.s(1).unwrap_or_default(), + value: mtext_dxf::decode_line(&t.s(1).unwrap_or_default()), rotation: t.fd(50, 0.0).to_radians(), width_factor: t.fd(41, 1.0), oblique: t.fd(51, 0.0).to_radians(), @@ -172,7 +172,7 @@ pub(super) fn attdef(tags: &[Tag], c: &Common) -> Attrib { text, invisible: own.i(70).unwrap_or(0) & 1 != 0, constant: own.i(70).unwrap_or(0) & 2 != 0, - prompt: own.s(3).unwrap_or_default(), + prompt: mtext_dxf::decode_line(&own.s(3).unwrap_or_default()), props: Default::default(), multiline, modes: attrib_modes(own.0), diff --git a/crates/io/src/dxf_write.rs b/crates/io/src/dxf_write.rs index 341f70e62..d6aa337aa 100644 --- a/crates/io/src/dxf_write.rs +++ b/crates/io/src/dxf_write.rs @@ -152,5 +152,8 @@ pub fn entity_groups(d: &Drawing, e: &Entity) -> (String, Vec) { entity(&mut w, d, e, "0", paper, &cx); let mut tags = w.t.into_iter(); let kind = tags.next().filter(|t| t.code == 0).map(|t| t.value.as_str()).unwrap_or_default(); - (kind, tags.take_while(|t| t.code != 0).filter(|t| t.code != 330).collect()) + let mut tags: Vec = tags.take_while(|t| t.code != 0).filter(|t| t.code != 330).collect(); + // Strings as AutoLISP sees them: `10^2`, not the file's `10^ 2`. + crate::dxf_read::mtext_dxf::map_text_groups(&kind, &mut tags, crate::dxf_read::mtext_dxf::decode_line); + (kind, tags) } diff --git a/crates/io/src/dxf_write/common.rs b/crates/io/src/dxf_write/common.rs index b09d90410..25d2bb074 100644 --- a/crates/io/src/dxf_write/common.rs +++ b/crates/io/src/dxf_write/common.rs @@ -255,7 +255,7 @@ pub(super) fn text_tags(w: &mut W, d: &Drawing, t: &Text, attrib: Option<(&Attri }; w.p(10, insert); w.f(40, t.height); - w.s(1, &t.value); + w.s(1, crate::dxf_read::mtext_dxf::encode_line(&t.value)); if t.rotation != 0.0 { w.f(50, t.rotation.to_degrees()); } @@ -293,7 +293,7 @@ pub(super) fn text_tags(w: &mut W, d: &Drawing, t: &Text, attrib: Option<(&Attri w.i(280, 0); } if let Some(prompt) = attdef_prompt { - w.s(3, prompt); + w.s(3, crate::dxf_read::mtext_dxf::encode_line(prompt)); } w.s(2, &a.tag); w.i(70, attrib_flags(a)); diff --git a/crates/io/src/dxf_write/entity.rs b/crates/io/src/dxf_write/entity.rs index a33437017..f1e0a1d73 100644 --- a/crates/io/src/dxf_write/entity.rs +++ b/crates/io/src/dxf_write/entity.rs @@ -355,7 +355,7 @@ fn write_dimension_entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper // The actual measurement (read-only in DXF): AutoLISP's entget shows it. w.f(42, g.measurement); if !dm.text.is_empty() { - w.s(1, &dm.text); + w.s(1, crate::dxf_read::mtext_dxf::encode_line(&dm.text)); } w.s(3, &dm.style); if dm.text_rotation != 0.0 { diff --git a/crates/io/tests/it/main.rs b/crates/io/tests/it/main.rs index e0ff04f97..8442a7629 100644 --- a/crates/io/tests/it/main.rs +++ b/crates/io/tests/it/main.rs @@ -67,6 +67,7 @@ mod svg_arc_budget; mod svg_tiny_extents; mod table_cellstyle_roundtrip; mod tables_objects_roundtrip; +mod text_carets_roundtrip; mod text_left_justify; mod tolerance_roundtrip; mod user_lin_pat_roundtrip; diff --git a/crates/io/tests/it/multiline_attributes.rs b/crates/io/tests/it/multiline_attributes.rs index 625c38a30..428dc7db5 100644 --- a/crates/io/tests/it/multiline_attributes.rs +++ b/crates/io/tests/it/multiline_attributes.rs @@ -335,3 +335,18 @@ fn damaged_embedded_text_does_not_panic() { let _ = read_dxf(save(&d).as_bytes()).unwrap(); } } + +#[test] +fn entmod_leaves_the_embedded_text_alone() { + // entget and entmod convert the carets of the attribute's own groups, not the embedded MTEXT's, + // which has its own encoding: `^ J` there is a caret and a J, not a line feed. + let d = read_dxf(dxf(FIXTURE).replace("SITE\\PNAME", "SITE^ JNAME").as_bytes()).unwrap(); + let Some(e) = d.block("TAG").and_then(|b| b.entities.iter().find(|e| matches!(e.kind, EntityKind::AttDef(_)))) else { + panic!("no definition"); + }; + let (kind, groups) = cadcraft_io::entity_groups(&d, e); + let embedded: Vec = groups.iter().skip_while(|t| t.code != 101).filter(|t| t.code == 1).map(|t| t.str()).collect(); + assert_eq!(embedded, ["SITE^ JNAME"]); + let Some((_, EntityKind::AttDef(a))) = cadcraft_io::entity_from_groups(&kind, &groups) else { panic!("no ATTDEF") }; + assert_eq!(a.multiline.map(|m| m.mtext.contents), Some("SITE^JNAME".to_string())); +} diff --git a/crates/io/tests/it/text_carets_roundtrip.rs b/crates/io/tests/it/text_carets_roundtrip.rs new file mode 100644 index 000000000..1c30dd1a3 --- /dev/null +++ b/crates/io/tests/it/text_carets_roundtrip.rs @@ -0,0 +1,125 @@ +//! Carets in TEXT, ATTDEF, ATTRIB and DIMENSION strings are written as `^ ` and read back: AutoCAD +//! reads a bare `^2` in `10^2` as a control character. A tab is `^I`, as AutoCAD writes it. + +use cadcraft_doc::{Attrib, Common, DimKind, Dimension, Drawing, EntityKind, HAlign, Space, Text, VAlign}; +use cadcraft_geom::Vec3; +use cadcraft_io::{read_dxf, write_dxf}; + +fn text(value: &str) -> Text { + Text { + insert: Vec3::new(1.0, 1.0, 0.0), + align_pt: None, + height: 2.5, + value: value.into(), + rotation: 0.0, + width_factor: 1.0, + oblique: 0.0, + style: "Standard".into(), + halign: HAlign::Left, + valign: VAlign::Baseline, + } +} + +fn dimension(label: &str) -> Dimension { + Dimension { + kind: DimKind::Linear { rotation: 0.0 }, + defpt: Vec3::new(0.0, 5.0, 0.0), + text_mid: Vec3::ZERO, + p13: Vec3::ZERO, + p14: Vec3::new(10.0, 0.0, 0.0), + p15: Vec3::ZERO, + p16: Vec3::ZERO, + text: label.into(), + style: "Standard".into(), + measurement: 0.0, + text_rotation: 0.0, + user_text_pos: false, + block: None, + overrides: Default::default(), + assoc: Vec::new(), + marks: Default::default(), + } +} + +fn drawing() -> Drawing { + let mut d = Drawing::new_metric(); + d.add(&Space::Model, Common::default(), EntityKind::Text(text("10^2 m, A^JB"))).unwrap(); + let attdef = Attrib { + tag: "AREA".into(), + text: text("1^3"), + invisible: false, + constant: false, + prompt: "Area in m^2".into(), + props: Default::default(), + multiline: None, + modes: Default::default(), + }; + d.add(&Space::Model, Common::default(), EntityKind::AttDef(attdef)).unwrap(); + d.add(&Space::Model, Common::default(), EntityKind::Dimension(dimension("<> x10^3"))).unwrap(); + d +} + +fn strings(k: &EntityKind) -> Vec { + match k { + EntityKind::Text(t) => vec![t.value.clone()], + EntityKind::AttDef(a) => vec![a.text.value.clone(), a.prompt.clone()], + EntityKind::Dimension(dm) => vec![dm.text.clone()], + _ => Vec::new(), + } +} + +fn all_strings(d: &Drawing) -> Vec { + d.model.iter().flat_map(|e| strings(&e.kind)).collect() +} + +#[test] +fn carets_are_written_escaped_and_read_back() { + let dxf = write_dxf(&drawing()); + // `^J` stays two characters: a single-line string has no line feeds. + for escaped in ["10^ 2 m, A^ JB", "1^ 3", "Area in m^ 2", "<> x10^ 3"] { + assert!(dxf.contains(escaped), "{escaped}"); + } + let back = read_dxf(dxf.as_bytes()).unwrap(); + assert_eq!(all_strings(&back), ["10^2 m, A^JB", "1^3", "Area in m^2", "<> x10^3"]); + // Older files have bare carets, `^J` too: kept as they are (a single-line string has no line feed). + let old = read_dxf(dxf.replace("10^ 2 m, A^ JB", "10^2 m, A^JB").as_bytes()).unwrap(); + assert_eq!(all_strings(&old)[0], "10^2 m, A^JB"); + assert!(write_dxf(&old).contains("10^ 2 m, A^ JB")); + // AutoCAD writes a tab as `^I`: it is read as a tab and written back as `^I`. + let tab = read_dxf(dxf.replace("10^ 2 m, A^ JB", "A^IB").as_bytes()).unwrap(); + assert_eq!(all_strings(&tab)[0], "A\tB"); + assert!(write_dxf(&tab).contains("A^IB")); + // Through a DWG. + let dwg = cadcraft_io::write(&drawing(), "t.dwg").unwrap(); + assert_eq!(all_strings(&cadcraft_io::read(&dwg, "t.dwg").unwrap()), all_strings(&drawing())); +} + +#[test] +fn a_caret_before_a_space_or_at_the_end_round_trips() { + let mut d = Drawing::new_metric(); + d.add(&Space::Model, Common::default(), EntityKind::Text(text("x^ y^"))).unwrap(); + let dxf = write_dxf(&d); + assert!(dxf.contains("x^ y^ "), "{dxf}"); + assert_eq!(all_strings(&read_dxf(dxf.as_bytes()).unwrap()), ["x^ y^"]); +} + +#[test] +fn entget_shows_plain_carets_and_entmake_takes_them() { + let mut d = Drawing::new_metric(); + d.add(&Space::Model, Common::default(), EntityKind::Text(text("x^ y 10^2"))).unwrap(); + let e = d.model.iter().next().unwrap(); + let (kind, groups) = cadcraft_io::entity_groups(&d, e); + assert!(groups.iter().any(|t| t.code == 1 && t.str() == "x^ y 10^2"), "{groups:?}"); + // entmod of entget changes nothing; entmake takes a plain string. + let (_, back) = cadcraft_io::entity_from_groups(&kind, &groups).unwrap(); + assert_eq!(strings(&back), ["x^ y 10^2"]); + let made = [cadcraft_dxf::Tag::s(1, "p^ q")]; + assert_eq!(strings(&cadcraft_io::entity_from_groups("TEXT", &made).unwrap().1), ["p^ q"]); + // Every kind, and a DIMENSION's style name (also group 3) as it is. + let d = drawing(); + for e in d.model.iter() { + let (kind, groups) = cadcraft_io::entity_groups(&d, e); + let (_, back) = cadcraft_io::entity_from_groups(&kind, &groups).unwrap(); + assert_eq!(strings(&back), strings(&e.kind), "{kind}"); + } +} From 06c5760ea6e477fa72857753b737d8a23123270b Mon Sep 17 00:00:00 2001 From: campbellcody <142101597+campbellcody@users.noreply.github.com> Date: Sun, 1 Nov 2026 14:38:24 -0500 Subject: [PATCH 035/100] Persist QuickCalc history, variables and last answer (#2572) * Persist QuickCalc history, variables and last answer Store bounded calculator data in app preferences for desktop and web. Restore finite numeric and point variables, normalize names and ignore malformed entries without resuming transient drawing input. Covers gaps F22. Validation: 3079 workspace tests passed; wasm, fmt, assets, layers and i18n passed; UI screenshot inspected. Strict CI remains blocked by pre-existing nonminimal_bool lints in DXF and UI code; scoped UI Clippy passes with only that lint disabled. * quickcalc: apply the variable limit after validation Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Brandon Thomas Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/lib.rs | 10 +- crates/ui-egui/src/palettes/quickcalc.rs | 166 +++++++++++++++++++++-- crates/ui-egui/tests/quickcalc_toggle.rs | 47 ++++++- 3 files changed, 212 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 97ab2faf4..142a589a3 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -239,6 +239,8 @@ pub struct CadApp { pub canvas: canvas::CanvasState, /// Desktop HID status and navigation preferences; the web host supplies no HID backend. pub spacemouse: spacemouse::State, + /// Calculator data shared by its floating and docked views; durable fields live in prefs. + pub(crate) quickcalc: palettes::quickcalc::State, pub cmd: cmdline::CmdLine, pub status: Option<(String, f64)>, /// Drawings being read on worker threads ([`opening`]). @@ -327,6 +329,7 @@ impl CadApp { services, canvas: canvas::CanvasState::default(), spacemouse: spacemouse::State::default(), + quickcalc: palettes::quickcalc::State::default(), cmd: cmdline::CmdLine::default(), status: None, opening: opening::Opening::default(), @@ -465,6 +468,7 @@ impl CadApp { "interfaceLanguage": self.ui.interface_language.code(), "saveFormat": self.ui.save_format.as_str(), "saveVersion": self.ui.save_version, + "quickcalc": self.quickcalc.prefs(), }); if self.spacemouse.settings != spacemouse::Settings::default() { prefs["spacemouse"] = json!(self.spacemouse.settings); @@ -481,6 +485,9 @@ impl CadApp { /// Restore preferences saved by [`Self::prefs_json`]; unknown or malformed values are ignored. pub fn load_prefs(&mut self, json: &str) { let Ok(v) = serde_json::from_str::(json) else { return }; + if let Some(calculator) = v.get("quickcalc") { + self.quickcalc.load_prefs(calculator); + } if let Some(prefs) = v.get("spacemouse") { let _ = self.spacemouse.load_settings(prefs); } @@ -1129,7 +1136,8 @@ mod tests { frame(&mut a, &ctx, None); assert_eq!(Tokens::get(), Tokens::of(theme::SYSTEM_FALLBACK), "no OS appearance: the documented fallback"); assert_eq!(a.ui.theme, ThemePref::System); - assert_eq!(a.prefs_json(), r#"{"animations":"system","interfaceLanguage":"auto","saveFormat":"dxf","saveVersion":"2018","theme":"system"}"#); + let prefs: Value = serde_json::from_str(&a.prefs_json()).unwrap(); + assert_eq!(prefs["theme"], "system"); } #[test] diff --git a/crates/ui-egui/src/palettes/quickcalc.rs b/crates/ui-egui/src/palettes/quickcalc.rs index 5f601b2fa..d6948e363 100644 --- a/crates/ui-egui/src/palettes/quickcalc.rs +++ b/crates/ui-egui/src/palettes/quickcalc.rs @@ -14,10 +14,14 @@ const PICKED: &str = "QuickCalc: "; const MAX_HISTORY: usize = 100; /// Variables kept. const MAX_VARS: usize = 50; +/// Bound each saved expression/result and variable name, including hand-edited preferences. +// CAL accepts expressions up to 10,000 bytes, including a variable's name on its own. +const MAX_TEXT_BYTES: usize = 10_000; +const MAX_NAME_BYTES: usize = MAX_TEXT_BYTES; const KINDS: [(&str, &str); 4] = [("length", "Length"), ("area", "Area"), ("volume", "Volume"), ("angle", "Angle")]; #[derive(Clone, Default)] -struct State { +pub(crate) struct State { input: String, /// (expression, result) newest last. history: Vec<(String, String)>, @@ -39,8 +43,17 @@ struct State { pending: Option, } -fn id() -> egui::Id { - egui::Id::unique("cadcraft-quickcalc") +fn valid_name(name: &str) -> bool { + name.len() <= MAX_NAME_BYTES + && name.chars().next().is_some_and(|c| c.is_ascii_alphabetic()) + && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') + && !["pi", "e", "ans"].contains(&name) +} + +/// CAL variables are finite numbers or 2D points; no nested arrays or objects. +fn valid_value(value: &Value) -> bool { + let number = |v: &Value| v.as_f64().is_some_and(f64::is_finite); + number(value) || value.as_array().is_some_and(|a| a.len() == 2 && a.iter().all(number)) } /// A variable's value as typed into an expression: a number, or a point as `[x,y]`. @@ -57,6 +70,61 @@ fn result_value(r: &Value) -> Option { } impl State { + /// Only durable calculator data: never resume an in-flight drawing pick after a restart. + pub(crate) fn prefs(&self) -> Value { + let history: Vec<_> = self + .history + .iter() + .rev() + .take(MAX_HISTORY) + .filter(|(expr, result)| expr.len() <= MAX_TEXT_BYTES && result.len() <= MAX_TEXT_BYTES) + .collect(); + let vars: Vec<_> = self.vars.iter().filter(|(name, value)| valid_name(name) && valid_value(value)).take(MAX_VARS).collect(); + json!({"history": history.into_iter().rev().collect::>(), "vars": vars, + "ans": self.ans.as_ref().filter(|v| valid_value(v))}) + } + + /// Restore bounded, independently validated entries; older preferences have no calculator. + pub(crate) fn load_prefs(&mut self, value: &Value) { + if !value.is_object() { + return; + } + if let Some(history) = value.get("history").and_then(Value::as_array) { + self.history = history + .iter() + .rev() + .take(MAX_HISTORY) + .filter_map(|entry| { + let pair = entry.as_array().filter(|a| a.len() == 2)?; + let expr = pair.first()?.as_str().filter(|s| s.len() <= MAX_TEXT_BYTES)?; + let result = pair.get(1)?.as_str().filter(|s| s.len() <= MAX_TEXT_BYTES)?; + Some((expr.to_string(), result.to_string())) + }) + .collect(); + self.history.reverse(); + } + if let Some(vars) = value.get("vars").and_then(Value::as_array) { + self.vars.clear(); + for entry in vars { + // Only valid entries count toward the limit. + if self.vars.len() >= MAX_VARS { + break; + } + let Some(pair) = entry.as_array().filter(|a| a.len() == 2) else { continue }; + let Some(name) = pair.first().and_then(Value::as_str).filter(|s| s.len() <= MAX_NAME_BYTES) else { continue }; + let name = name.to_ascii_lowercase(); + let Some(value) = pair.get(1).filter(|v| valid_value(v)) else { continue }; + if valid_name(&name) { + self.vars.retain(|(key, _)| key != &name); + self.vars.push((name, value.clone())); + } + } + } + if let Some(ans) = value.get("ans") { + self.ans = valid_value(ans).then(|| ans.clone()); + } + } + fn vars_json(&self) -> Value { let mut m = serde_json::Map::new(); for (k, v) in &self.vars { @@ -88,9 +156,7 @@ impl State { } fn add_var(&mut self, app: &mut CadApp) { let name = self.var_name.trim().to_ascii_lowercase(); - let valid = name.chars().next().is_some_and(|c| c.is_ascii_alphabetic()) - && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') - && !["pi", "e", "ans"].contains(&name.as_str()); + let valid = valid_name(&name); if !valid { self.error = Some(crate::tl!("A variable name starts with a letter and has only letters, digits and _").into()); return; @@ -219,7 +285,7 @@ fn value_row(ui: &mut egui::Ui, value: &mut String) { /// Quick Calculator dock body: expression entry with evaluation, variables and unit conversion. pub fn show(app: &mut CadApp, ui: &mut egui::Ui) { let t = Tokens::get(); - let mut st: State = ui.ctx().data_mut(|d| d.get_temp(id())).unwrap_or_default(); + let mut st = std::mem::take(&mut app.quickcalc); st.collect(app); egui::ScrollArea::vertical().id_salt("dock-quickcalc").auto_shrink([false, false]).show(ui, |ui| { ui.add_space(6.0); @@ -371,16 +437,98 @@ pub fn show(app: &mut CadApp, ui: &mut egui::Ui) { }); }); }); - ui.ctx().data_mut(|d| d.insert_temp(id(), st)); + app.quickcalc = st; } #[cfg(test)] mod tests { + use cadcraft_engine::Session; use egui::accesskit::Role; use egui_kittest::Harness; use egui_kittest::kittest::{NodeT, Queryable}; - use super::value_row; + use super::{MAX_HISTORY, MAX_TEXT_BYTES, MAX_VARS, State, value_row}; + use crate::{CadApp, Services}; + use serde_json::{Value, json}; + + #[test] + fn preferences_restore_numbers_points_history_and_ans_without_pending_input() { + let mut app = CadApp::new(Session::default(), Services::default()); + let mut state = State { var_name: "Width".into(), var_expr: "2+3".into(), ..State::default() }; + state.add_var(&mut app); + state.var_name = "origin".into(); + state.var_expr = "[3,4]".into(); + state.add_var(&mut app); + state.input = "origin*width".into(); + state.evaluate(&mut app); + state.pending = Some(123); + state.error = Some("transient error".into()); + app.quickcalc = state; + + let mut restarted = CadApp::new(Session::default(), Services::default()); + let drawing = restarted.session.doc().unwrap().clone(); + restarted.load_prefs(&app.prefs_json()); + let mut state = std::mem::take(&mut restarted.quickcalc); + assert_eq!(state.history, [("origin*width".into(), "15,20".into())]); + assert_eq!(state.vars, [("width".into(), json!(5.0)), ("origin".into(), json!([3.0, 4.0]))]); + assert_eq!(state.ans, Some(json!([15.0, 20.0]))); + assert!(state.pending.is_none()); + assert!(state.error.is_none()); + assert!(state.input.is_empty()); + state.input = "ans+origin".into(); + state.evaluate(&mut restarted); + assert_eq!(state.ans, Some(json!([18.0, 24.0]))); + assert_eq!(restarted.session.doc().unwrap(), &drawing); + assert!(restarted.session.running.is_none()); + } + + #[test] + fn malformed_entries_do_not_hide_valid_calculator_data_or_other_preferences() { + let mut app = CadApp::new(Session::default(), Services::default()); + app.load_prefs( + &json!({"theme": "light", "quickcalc": { + "history": [null, ["bad"], [false, 3], ["2+3", "5"]], + "vars": [["PI", 3], ["1bad", 5], ["nested", [[1,2],3]], ["bad", [1,2,3]], + ["object", {"x":1}], ["WIDTH", 4], ["width", 5], ["origin", [3,4]]], + "ans": "NaN" + }}) + .to_string(), + ); + assert_eq!(app.ui.theme.as_str(), "light"); + assert_eq!(app.quickcalc.history, [("2+3".into(), "5".into())]); + assert_eq!(app.quickcalc.vars, [("width".into(), json!(5)), ("origin".into(), json!([3, 4]))]); + assert!(app.quickcalc.ans.is_none()); + let saved = app.prefs_json(); + for malformed in ["not JSON", "{}", r#"{"quickcalc":null}"#, r#"{"quickcalc":{"vars":false,"history":0}}"#] { + app.load_prefs(malformed); + assert_eq!(app.prefs_json(), saved); + } + } + + #[test] + fn stored_calculator_data_is_bounded_and_cleared_data_stays_cleared() { + let mut state = State::default(); + let history: Vec<_> = (0..MAX_HISTORY + 10).map(|i| json!([i.to_string(), i.to_string()])).collect(); + let vars: Vec<_> = (0..MAX_VARS + 10).map(|i| json!([format!("v{i}"), i])).collect(); + state.load_prefs(&json!({"history": history, "vars": vars, "ans": 5})); + assert_eq!(state.history.len(), MAX_HISTORY); + assert_eq!(state.history.first().unwrap().0, "10"); + assert_eq!(state.history.last().unwrap().0, (MAX_HISTORY + 9).to_string()); + assert_eq!(state.vars.len(), MAX_VARS); + let oversized = "x".repeat(MAX_TEXT_BYTES + 1); + state.load_prefs(&json!({"history": [[oversized, "1"]], "vars": [[oversized, 1]]})); + assert!(state.history.is_empty()); + assert!(state.vars.is_empty()); + let mut restored = State { history: vec![("old".into(), "1".into())], vars: vec![("old".into(), json!(1))], ..State::default() }; + restored.load_prefs(&state.prefs()); + assert!(restored.history.is_empty()); + assert!(restored.vars.is_empty()); + assert_eq!(restored.ans, Some(json!(5))); + // An invalid answer is discarded rather than reused as a calculator value. + restored.load_prefs(&json!({"ans": [1, null]})); + assert_eq!(restored.ans, None); + assert_eq!(restored.prefs()["ans"], Value::Null); + } /// The conversion value field is named by its visible label under bundled fonts only, so a /// system face cannot hide a missing glyph and a bare "1" placeholder cannot pass as a name. diff --git a/crates/ui-egui/tests/quickcalc_toggle.rs b/crates/ui-egui/tests/quickcalc_toggle.rs index 9b6bec6d1..caf52f0df 100644 --- a/crates/ui-egui/tests/quickcalc_toggle.rs +++ b/crates/ui-egui/tests/quickcalc_toggle.rs @@ -9,10 +9,17 @@ use egui_kittest::kittest::{NodeT, Queryable, by}; use serde_json::{Value, json}; fn harness() -> Harness<'static, CadApp> { + harness_from_prefs("{}", false) +} + +fn harness_from_prefs(prefs: &str, render: bool) -> Harness<'static, CadApp> { let mut app = CadApp::new(Session::default(), Services::default()); + app.load_prefs(prefs); app.ui.start_tab = false; app.ui.in_window_menu = false; - let mut h = Harness::builder().with_size(vec2(1280.0, 800.0)).build_ui_state( + let builder = Harness::builder().with_size(vec2(1280.0, 800.0)); + let builder = if render { builder.wgpu() } else { builder }; + let mut h = builder.build_ui_state( |ui, app: &mut CadApp| { app.logic(&ui.ctx().clone()); app.ui(ui); @@ -23,6 +30,44 @@ fn harness() -> Harness<'static, CadApp> { h } +#[test] +fn restarting_restores_calculator_data_and_ui_edits_are_saved_when_hidden() { + let prefs = json!({"quickcalc": { + "history": [["2+3", "5"]], "vars": [["width", 5]], "ans": 5 + }}) + .to_string(); + let screenshot = std::env::var_os("CADCRAFT_QUICKCALC_SHOT"); + let mut h = harness_from_prefs(&prefs, screenshot.is_some()); + h.state_mut().run("ui.panel.quickcalc", Value::Null).unwrap(); + h.run_steps(5); + assert!(h.query_all(by().value("2+3")).next().is_some()); + expression(&h).click(); + h.run_steps(3); + expression(&h).type_text("ans+width"); + h.run_steps(3); + h.get_by_role_and_label(Role::Button, "=").click(); + h.run_steps(3); + let prefs: Value = serde_json::from_str(&h.state().prefs_json()).unwrap(); + assert_eq!(prefs["quickcalc"]["ans"], 10.0); + assert_eq!(prefs["quickcalc"]["history"][1], json!(["ans+width", "10"])); + if let Some(path) = screenshot { + h.render().unwrap().save(std::path::PathBuf::from(path)).unwrap(); + } + + h.get_by_role_and_label(Role::Button, "Clear History").click(); + h.run_steps(3); + h.get_by_role_and_label(Role::Button, "×").click(); + h.run_steps(3); + h.state_mut().run("ui.toggle.quickcalc", Value::Null).unwrap(); + h.run_steps(5); + let mut restarted = CadApp::new(Session::default(), Services::default()); + restarted.load_prefs(&h.state().prefs_json()); + let prefs: Value = serde_json::from_str(&restarted.prefs_json()).unwrap(); + assert_eq!(prefs["quickcalc"]["history"], json!([])); + assert_eq!(prefs["quickcalc"]["vars"], json!([])); + assert_eq!(prefs["quickcalc"]["ans"], 10.0); +} + /// In these tests QuickCalc floats alone; scope to its body to avoid the Properties text fields. /// Its first text input is the expression, before conversion values and variable definitions. fn expression<'a>(h: &'a Harness<'_, CadApp>) -> egui_kittest::Node<'a> { From f0b46e34dc5a32153a2f062eee761f6660d69ab7 Mon Sep 17 00:00:00 2001 From: Steffen <576706+camperking@users.noreply.github.com> Date: Sun, 1 Nov 2026 20:38:27 +0100 Subject: [PATCH 036/100] Hit tests don't lay out dashes: moving the mouse over a block stays smooth (#2603) * Hit tests don't lay out dashes: moving the mouse over a block stays smooth (#1285) Every mouse move hit-tests the block references under the cursor, and hit_polylines built them with a full-detail display list, one primitive per dash. A fine pattern on long lines in a block inserted at 0.001 made millions of dashes per move. Options::linetypes off draws every line whole and continuous, and hit_polylines uses it for blocks, dimensions and multileaders. A dashed line in a block is now hit across its gaps, as one outside a block already was. Co-Authored-By: Claude Opus 5.5 * render: with linetypes off, a lightweight polyline is one continuous stroke Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Steffen Co-authored-by: Claude Opus 5.5 Co-authored-by: Brandon Thomas --- crates/engine/src/select.rs | 52 ++++++++++++++++++++++++++- crates/io/src/pdf.rs | 1 + crates/render/src/draw.rs | 8 ++--- crates/render/src/mline.rs | 2 +- crates/render/src/pipeline.rs | 5 +++ crates/render/src/tests.rs | 25 +++++++++++++ crates/ui-egui/src/canvas/list.rs | 1 + crates/ui-egui/src/canvas/viewport.rs | 1 + 8 files changed, 89 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/select.rs b/crates/engine/src/select.rs index 6c70bee37..8047412fd 100644 --- a/crates/engine/src/select.rs +++ b/crates/engine/src/select.rs @@ -29,7 +29,7 @@ pub fn hit_polylines(d: &Drawing, e: &Entity, tol: f64) -> Vec> { let list = cadcraft_render::build_entities( d, std::iter::once(e), - &cadcraft_render::Options { tolerance: tol, text: false, ..Default::default() }, + &cadcraft_render::Options { tolerance: tol, text: false, linetypes: false, ..Default::default() }, ); v = list.prims.iter().map(|p| list.points(p).to_vec()).collect(); v.push(vec![c[0], c[1], c[2], c[3], c[0]]); @@ -502,6 +502,56 @@ mod tests { // Only one within reach: that one, whatever the order. assert_eq!(pick(&d, &Space::Model, Vec2::new(41.3, 50.0), 1.0), Some(line)); } + + /// A block holding a diagonal dashed line, inserted at `scale`. + fn dashed_block(length: f64, scale: f64) -> (Drawing, Handle) { + let mut d = Drawing::new_imperial(); + d.linetypes.extend(cadcraft_doc::library::standard_linetypes().into_iter().filter(|l| l.name == "DASHED")); + let mut line = + Entity::new(Handle(1), EntityKind::Line(cadcraft_doc::Line { a: Vec3::new(0.0, 0.0, 0.0), b: Vec3::new(length, length, 0.0) })); + line.common.linetype = "DASHED".into(); + let mut blk = cadcraft_doc::Block::new("B"); + blk.entities.push(line); + d.blocks.insert("B".into(), std::sync::Arc::new(blk)); + let insert = cadcraft_doc::Insert { + block: "B".into(), + insert: Vec3::new(0.0, 0.0, 0.0), + scale: Vec3::new(scale, scale, 1.0), + rotation: 0.0, + attribs: Vec::new(), + cols: 1, + rows: 1, + col_spacing: 0.0, + row_spacing: 0.0, + }; + let h = d.add(&Space::Model, Common::default(), EntityKind::Insert(insert)).unwrap(); + (d, h) + } + + /// Every mouse move over a block hit-tests what it draws. Two blocks with a fine pattern on + /// long lines (inserted at 0.001) made 5.8 million dashes, and half a second, per move (#1285). + #[test] + fn hit_testing_a_block_does_not_lay_out_its_dashes() { + let (d, h) = dashed_block(20_000.0, 0.001); + let e = d.entity(h).unwrap(); + let drawn = cadcraft_render::build_entities(&d, std::iter::once(e.as_ref()), &cadcraft_render::Options { text: false, ..Default::default() }); + assert!(drawn.prims.len() > 10_000, "the pattern draws {} dashes", drawn.prims.len()); + // The line, and the block's bounding rectangle. + assert_eq!(hit_polylines(&d, e, 0.01).len(), 2); + } + + /// A dashed line in a block is hit across its gaps, as one outside a block is. + #[test] + fn a_dashed_line_in_a_block_is_picked_in_a_gap() { + let (d, h) = dashed_block(100.0, 1.0); + let e = d.entity(h).unwrap(); + let drawn = cadcraft_render::build_entities(&d, std::iter::once(e.as_ref()), &cadcraft_render::Options { text: false, ..Default::default() }); + let (a, b) = (drawn.prims.first().unwrap(), drawn.prims.get(1).unwrap()); + let (end, start) = (*drawn.points(a).last().unwrap(), *drawn.points(b).first().unwrap()); + assert!(end.dist(start) > 0.01, "a gap between the first two dashes"); + let gap = end + (start - end) * 0.5; + assert_eq!(pick(&d, &Space::Model, gap, end.dist(start) / 8.0), Some(h)); + } } #[cfg(test)] diff --git a/crates/io/src/pdf.rs b/crates/io/src/pdf.rs index 3f35233a8..b06580d53 100644 --- a/crates/io/src/pdf.rs +++ b/crates/io/src/pdf.rs @@ -520,6 +520,7 @@ fn sheet(d: &Drawing, space: &Space, o: &PdfOptions, links_to: &[(Handle, String tolerance: 0.001, min_dash: 0.0, text: true, + linetypes: true, fill: true, lineweights, view_height: 0.0, diff --git a/crates/render/src/draw.rs b/crates/render/src/draw.rs index 7e00b4c8c..39b0ddfcc 100644 --- a/crates/render/src/draw.rs +++ b/crates/render/src/draw.rs @@ -77,7 +77,7 @@ fn entity_parts(b: &mut Builder, ctx: &Ctx, e: &Entity) { let tol = b.opts.tolerance / ctx.xf.scale_factor().max(1e-12); let lw = if b.opts.lineweights { lw } else { 0.0 }; // Stroke a polyline with the entity's linetype. - let stroke = |b: &mut Builder, pts: &[Vec2]| match < { + let stroke = |b: &mut Builder, pts: &[Vec2]| match lt.as_ref().filter(|_| b.opts.linetypes) { Some(lt) => { let min = b.opts.min_dash / ctx.xf.scale_factor().max(1e-12); for dash in linetype::apply(pts, lt, ltscale, min) { @@ -140,7 +140,7 @@ fn entity_parts(b: &mut Builder, ctx: &Ctx, e: &Entity) { // the list serves a whole band of zooms. Plots and exports draw the band alone, and // a transparent band gets none (its alpha would add up along the centre). let hairline = !b.plotting && b.opts.min_dash > 0.0 && b.transparency == 0; - match < { + match lt.as_ref().filter(|_| b.opts.linetypes) { // Dashed: each dash a filled piece of the band; dots a line across it. Some(lt) => { let min = b.opts.min_dash / ctx.xf.scale_factor().max(1e-12); @@ -173,7 +173,7 @@ fn entity_parts(b: &mut Builder, ctx: &Ctx, e: &Entity) { } EntityKind::LwPolyline(p) => { let pl = Polyline { vertices: p.vertices.clone(), closed: p.closed }; - if p.plinegen || lt.is_none() { + if !b.opts.linetypes || p.plinegen || lt.is_none() { stroke(b, &pl.tessellate(tol)); } else { for s in pl.segments() { @@ -355,7 +355,7 @@ fn draw_dimension(b: &mut Builder, ctx: &Ctx, e: &Entity, dm: &cadcraft_doc::Dim /// Draw a polyline with a linetype (None: continuous). pub(crate) fn stroke_with(b: &mut Builder, ctx: &Ctx, rgb: Ink, lw: f32, lt: Option<&cadcraft_doc::Linetype>, ltscale: f64, pts: &[Vec2]) { - match lt { + match lt.filter(|_| b.opts.linetypes) { Some(lt) => { let min = b.opts.min_dash / ctx.xf.scale_factor().max(1e-12); for dash in linetype::apply(pts, lt, ltscale, min) { diff --git a/crates/render/src/mline.rs b/crates/render/src/mline.rs index bbaacb5cc..882f3a626 100644 --- a/crates/render/src/mline.rs +++ b/crates/render/src/mline.rs @@ -59,7 +59,7 @@ pub(super) fn draw(b: &mut Builder, ctx: &Ctx, e: &Entity, m: &MLine, rgb: Ink, #[allow(clippy::too_many_arguments)] fn stroke(b: &mut Builder, ctx: &Ctx, rgb: Ink, lw: f32, lt: Option<&cadcraft_doc::Linetype>, scale: f64, min: f64, pts: &[Vec2]) { - match lt { + match lt.filter(|_| b.opts.linetypes) { Some(lt) => { for dash in linetype::apply(pts, lt, scale, min) { match dash.as_slice() { diff --git a/crates/render/src/pipeline.rs b/crates/render/src/pipeline.rs index 8cbcb95b9..3fc72f518 100644 --- a/crates/render/src/pipeline.rs +++ b/crates/render/src/pipeline.rs @@ -15,6 +15,10 @@ pub struct Options { pub min_dash: f64, /// Draw text (off for very coarse previews). pub text: bool, + /// Draw lines with their linetypes. Off, every line is drawn whole and continuous, as picking + /// sees it: a dashed line is hit across its gaps, and a small pattern on a long line makes + /// millions of dashes. + pub linetypes: bool, /// Highlighted (selected) handles are not special here; the canvas overlays them. pub fill: bool, pub lineweights: bool, @@ -52,6 +56,7 @@ impl Default for Options { tolerance: 0.001, min_dash: 0.0, text: true, + linetypes: true, fill: true, lineweights: false, view_height: 0.0, diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index ae35f27f9..fe2dc6470 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -1122,3 +1122,28 @@ fn raster_hatch_pattern_matches_one_by_one() { assert!(render(&l, &view, &o).unwrap().data() == render_one_by_one(&l, &view, &o).data()); } } + +/// `Options::linetypes` off draws a dashed line, a dashed wide polyline and a linetype on a layer +/// as the same whole line a Continuous one draws: what hit tests look at. +#[test] +fn without_linetypes_every_line_is_drawn_whole() { + let drawing = |lt: &str| { + let mut d = Drawing::new_imperial(); + d.linetypes.extend(cadcraft_doc::library::standard_linetypes().into_iter().filter(|l| l.name == "DASHED")); + d.layer_mut("0").unwrap().linetype = lt.into(); + let line = EntityKind::Line(cadcraft_doc::Line { a: Vec3::new(0.0, 0.0, 0.0), b: Vec3::new(30.0, 0.0, 0.0) }); + d.add(&Space::Model, Common::default(), line).unwrap(); + let vertices = [(0.0, 5.0), (30.0, 5.0)].map(|(x, y)| PolyVertex::new(Vec2::new(x, y))).to_vec(); + let wide = cadcraft_doc::LwPolyline { vertices, closed: false, const_width: 0.2, elevation: 0.0, plinegen: false }; + d.add(&Space::Model, Common::default(), EntityKind::LwPolyline(wide)).unwrap(); + d + }; + let whole = Options { linetypes: false, ..Options::default() }; + let continuous = build(&drawing("Continuous"), &Space::Model, &Options::default()); + let dashed = build(&drawing("DASHED"), &Space::Model, &whole); + assert_eq!(dashed.prims.len(), continuous.prims.len()); + assert_eq!(dashed.verts, continuous.verts); + assert_eq!(dashed.tris, continuous.tris); + let drawn = build(&drawing("DASHED"), &Space::Model, &Options::default()); + assert!(drawn.prims.len() > 2 * continuous.prims.len(), "drawn with its linetype: {} primitives", drawn.prims.len()); +} diff --git a/crates/ui-egui/src/canvas/list.rs b/crates/ui-egui/src/canvas/list.rs index 2d548264e..f965b0ae1 100644 --- a/crates/ui-egui/src/canvas/list.rs +++ b/crates/ui-egui/src/canvas/list.rs @@ -128,6 +128,7 @@ pub(super) fn ensure_list(app: &mut CadApp, xf: &Xf, views: &[(Bounds2, f64)], c // level of detail must hold at the finest one. min_dash: 2f64.powi(lod_band) * 2.0, text: true, + linetypes: true, fill: true, lineweights: lwdisplay, view_height: st.paper_view().height, diff --git a/crates/ui-egui/src/canvas/viewport.rs b/crates/ui-egui/src/canvas/viewport.rs index c79354b6c..0857e23ee 100644 --- a/crates/ui-egui/src/canvas/viewport.rs +++ b/crates/ui-egui/src/canvas/viewport.rs @@ -128,6 +128,7 @@ pub(super) fn ensure_vp_content(app: &mut CadApp, xf_paper: &Xf, ctx: &egui::Con tolerance: 2f64.powi(band) * 0.5, min_dash: 2f64.powi(band) * 2.0, text: true, + linetypes: true, fill: true, lineweights: lwdisplay, view_height: st.paper_view().height, From ccbc4fa8541ec71655448eddb2f2e283006836fc Mon Sep 17 00:00:00 2001 From: Books Date: Mon, 2 Nov 2026 06:38:31 +1100 Subject: [PATCH 037/100] DXF: link dimension sources to their associations (fixes #2221) (#2589) * DXF: link dimension sources to their associations (#2221) Source objects lacked the persistent reactor back to their DIMASSOC. Register validated source and intersection references once through the existing reactor list, keeping a self-link from duplicating the dimension's own reactor. Synthetic regressions cover missing links, repeated references and self-links. * Parity checklist: regenerated for dimension reactor gate * Parity checklist: regenerated after merging main * DXF: a dimension source keeps its other reactors next to the DIMASSOC (review on #2589) The DIMASSOC reactor made the writer emit an ACAD_REACTORS group, so the raw passthrough skipped the source's retained foreign reactors. They are now merged, deduplicated, into that one group. Test: a line reactor-linked to a kept object keeps both links after an associative dimension is added. * DXF: an unmodelled DIMASSOC source gets the DIMASSOC reactor (review on #2589) Unknown entities are written by the raw passthrough, which never saw the planned reactors. The map is now passed to dxf_raw::finish and merged into the entity's reactor group. Test: an unknown entity with a dimension snapped to it names the DIMASSOC. * Docs: leave the file-format parity document to maintainers (review on #2589) * DXF: pass the unknown-entity reactors as one argument (review on #2589) * DXF: collapse the extension dictionary condition (review on #2589) * Test: pin the reactor merge for dimension sources (review on #2589) * DXF: a kept extension dictionary follows the reactors group (review on #2589) When a written record already had its reactors group, the kept extension dictionary was inserted right after the handle, ahead of it; the DXF reference puts the reactors first. The reactor tests now find each group by name, so either order can no longer pass. * docs: leave the generated parity checklist to maintainers Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Brandon Thomas Co-authored-by: Claude Opus 5.5 --- crates/io/src/dxf_raw.rs | 60 +++++-- crates/io/src/dxf_write.rs | 4 +- crates/io/src/dxf_write/sections.rs | 17 +- crates/io/tests/dimassoc_angular_radial.rs | 47 ++++++ crates/io/tests/it/roundtrip_audit.rs | 179 +++++++++++++++++++++ 5 files changed, 288 insertions(+), 19 deletions(-) diff --git a/crates/io/src/dxf_raw.rs b/crates/io/src/dxf_raw.rs index 9b755f80f..16ea48b6c 100644 --- a/crates/io/src/dxf_raw.rs +++ b/crates/io/src/dxf_raw.rs @@ -846,7 +846,7 @@ fn unknown_handles(d: &Drawing) -> HashSet { } /// Put the drawing's foreign data back into a written file's tags (see the module comment). -pub(crate) fn finish(tags: &mut Vec, d: &Drawing) { +pub(crate) fn finish(tags: &mut Vec, d: &Drawing, reactors: &HashMap>) { let f = d.foreign.as_ref(); let unknown = unknown_handles(d); let xd: XdataOf = all_entities(d).filter(|e| !e.common.xdata.is_empty()).map(|e| (e.handle, e.common.xdata.as_slice())).collect(); @@ -993,8 +993,10 @@ pub(crate) fn finish(tags: &mut Vec, d: &Drawing) { let (Some(zero), Some(ub)) = (t.first(), t.get(1..)) else { continue }; let ur = Rec { sec: sec.clone(), kind: zero.str(), start: 0, end: 0 }; let x = head(ub).handle.and_then(|h| handle(&h)).and_then(|h| entity_extra(f, &xd, h)); + // Reactors the writer planned for this source (a DIMASSOC, an associative hatch). + let extra = head(ub).handle.and_then(|h| handle(&h)).and_then(|h| reactors.get(&h)).map_or(&[][..], Vec::as_slice); out.push(zero.clone()); - record(&map, &ur, ub, x.as_deref(), true, false, &mut out); + record(&map, &ur, ub, x.as_deref(), Some(extra), false, &mut out); } out.extend(tags.get(r.start).cloned()); let x = extra_for(f, &xd, r, b, &block); @@ -1002,7 +1004,7 @@ pub(crate) fn finish(tags: &mut Vec, d: &Drawing) { out.extend(b.iter().cloned()); continue; } - record(&map, r, b, x.as_deref(), false, Some(r.start) == root, &mut out); + record(&map, r, b, x.as_deref(), None, Some(r.start) == root, &mut out); } add_appids(&mut out); let top = max_handle(&out).saturating_add(1); @@ -1076,10 +1078,23 @@ fn plan_unknown(d: &Drawing, recs: &[Rec], tags: &[Tag], roles: &HashMap, unknown: Option<&[String]>) -> Vec { + let extra = unknown.unwrap_or_default(); + let mut have: HashSet = h.reactors.iter().cloned().collect(); + let kept = x.filter(|_| !(unknown.is_some() && h.has_reactors)).into_iter().flat_map(|x| x.reactors.iter()).filter_map(|r| map.get(&r.hex())); + kept.chain(extra.iter().cloned()).filter(|r| r != "0" && have.insert(norm(r))).collect() +} + /// A written record with its kept extras, remapped references (unknown entities) and kept /// dictionary entries (the named object dictionary). -fn record(map: &Map, r: &Rec, b: &[Tag], x: Option<&ForeignExtra>, unknown: bool, root: bool, out: &mut Vec) { +fn record(map: &Map, r: &Rec, b: &[Tag], x: Option<&ForeignExtra>, unknown: Option<&[String]>, root: bool, out: &mut Vec) { let h = head(b); + let adds = reactors_to_add(map, &h, x, unknown); + let mut in_reactors = false; let entity = matches!(r.sec.as_str(), "ENTITIES" | "BLOCKS") && r.kind != "BLOCK"; let xdata_at = b.iter().position(|t| t.code == 1001).unwrap_or(b.len()); let own_apps: HashSet = b.iter().filter(|t| t.code == 1001).map(|t| up(&t.str())).collect(); @@ -1096,6 +1111,12 @@ fn record(map: &Map, r: &Rec, b: &[Tag], x: Option<&ForeignExtra>, unknown: bool let rgb = b.iter().take(xdata_at).find(|t| t.code == 420).map(Tag::str); let has_book = b.iter().take(xdata_at).any(|t| t.code == 430); let handle_at = b.iter().position(|t| matches!(t.code, 5 | 105)); + // A kept extension dictionary follows the reactors group, as the DXF reference orders them. + let reactors_end = b.iter().position(|t| t.code == 102 && t.str().trim() == "{ACAD_REACTORS").and_then(|s| { + let rest = b.get(s + 1..)?; + rest.iter().position(|t| t.code == 102 && t.str().trim() == "}").map(|e| s + 1 + e) + }); + let xdict_at = if h.has_reactors { reactors_end.or(handle_at) } else { handle_at }; let mut seen_owner = false; for (i, t) in b.iter().enumerate() { if i == props_at @@ -1114,8 +1135,18 @@ fn record(map: &Map, r: &Rec, b: &[Tag], x: Option<&ForeignExtra>, unknown: bool { xdata(map, x, &own_apps, out); } + if t.code == 102 { + match t.str().trim() { + "{ACAD_REACTORS" => in_reactors = true, + "}" if in_reactors => { + in_reactors = false; + out.extend(adds.iter().map(|a| Tag::s(330, a.clone()))); + } + _ => {} + } + } let mut t = t.clone(); - if unknown && i < xdata_at && is_pointer(t.code) && t.code != 102 { + if unknown.is_some() && i < xdata_at && is_pointer(t.code) && t.code != 102 { if t.code == 330 && !seen_owner { seen_owner = true; } else { @@ -1123,18 +1154,17 @@ fn record(map: &Map, r: &Rec, b: &[Tag], x: Option<&ForeignExtra>, unknown: bool } } out.push(t); - if Some(i) == handle_at + if Some(i) == handle_at && !h.has_reactors && !adds.is_empty() { + out.push(Tag::s(102, "{ACAD_REACTORS")); + out.extend(adds.iter().map(|a| Tag::s(330, a.clone()))); + out.push(Tag::s(102, "}")); + } + if Some(i) == xdict_at && let Some(x) = x + && h.xdict.is_none() + && let Some(xd) = x.xdict { - if !h.has_reactors { - let rs: Vec = x.reactors.iter().map(|r| r.hex()).collect(); - map.handle_group(out, "ACAD_REACTORS", 330, &rs); - } - if h.xdict.is_none() - && let Some(xd) = x.xdict - { - map.handle_group(out, "ACAD_XDICTIONARY", 360, &[xd.hex()]); - } + map.handle_group(out, "ACAD_XDICTIONARY", 360, &[xd.hex()]); } } if let Some(x) = x { diff --git a/crates/io/src/dxf_write.rs b/crates/io/src/dxf_write.rs index d6aa337aa..b93471b01 100644 --- a/crates/io/src/dxf_write.rs +++ b/crates/io/src/dxf_write.rs @@ -43,8 +43,8 @@ struct W { pstyles: HashMap, /// Upper-case layer name → its LAYER table record handle (group 331/341 of viewports). layers: HashMap, - /// Persistent reactors of boundary objects: each source object of an associative hatch → - /// the hatches (groups `{ACAD_REACTORS 330 …}`), from [`hatch_reactors`]. + /// Persistent reactors of source geometry: the associative hatches and DIMASSOC objects + /// that follow it (groups `{ACAD_REACTORS 330 …}`). reactors: HashMap>, } diff --git a/crates/io/src/dxf_write/sections.rs b/crates/io/src/dxf_write/sections.rs index e9f8a9e35..b2eff022d 100644 --- a/crates/io/src/dxf_write/sections.rs +++ b/crates/io/src/dxf_write/sections.rs @@ -874,7 +874,20 @@ fn plan_dimension_associations(d: &Drawing, all_spaces: &[&EntityStore], w: &mut if let EntityKind::Dimension(dm) = &e.kind { let (flags, refs) = std_assoc_refs(d, dm); if !refs.is_empty() { - cx.assoc.insert(e.handle, AssocObj { xdict: w.h(), handle: w.h(), flags, refs }); + let assoc = AssocObj { xdict: w.h(), handle: w.h(), flags, refs }; + // Source geometry must name the DIMASSOC as well, or other readers do not + // follow later edits. Both objects of an intersection need the reactor. + for source in assoc.refs.iter().flat_map(|r| std::iter::once(r.main).chain(r.other)) { + // common_x already gives the dimension its own DIMASSOC reactor. + if source == e.handle { + continue; + } + let reactors = w.reactors.entry(source).or_default(); + if !reactors.contains(&assoc.handle) { + reactors.push(assoc.handle.clone()); + } + } + cx.assoc.insert(e.handle, assoc); } } } @@ -1117,7 +1130,7 @@ pub(super) fn tags_for(d: &Drawing, version: Version, target: Version, frozen_34 if let Some(t) = w.t.get_mut(seed_pos + 1) { *t = Tag::s(5, seed); } - crate::dxf_raw::finish(&mut w.t, d); + crate::dxf_raw::finish(&mut w.t, d, &w.reactors); crate::dxf_raw::data_store(&mut w.t, d, target >= Version::R2013); add_class_instance_counts(&mut w.t, target); mark_xrefs(&mut w.t, d); diff --git a/crates/io/tests/dimassoc_angular_radial.rs b/crates/io/tests/dimassoc_angular_radial.rs index ec1a11f69..f955ad7b5 100644 --- a/crates/io/tests/dimassoc_angular_radial.rs +++ b/crates/io/tests/dimassoc_angular_radial.rs @@ -288,3 +288,50 @@ fn standard_dimassoc_associates_in_dxf_and_dwg() { let back = read(&dwg, "case.dwg").unwrap(); assert_standard_assoc(&c, &back, "DWG"); } + +/// Every source object names each DIMASSOC that follows it once, before its owner group. +#[test] +fn dimension_sources_carry_their_persistent_reactors() { + let c = case(); + let text = write_dxf(&c.d); + let all = records(&text); + for dimension in [c.hd, c.hr, c.ha, c.hl] { + let assoc = dimassoc(&text, dimension).expect("DIMASSOC"); + let reactor = string(&assoc, 5).expect("DIMASSOC handle"); + for reference in point_refs(&assoc) { + // Both objects of an intersection need the link back. Repeated snaps to one + // object must not write the same DIMASSOC reactor twice. + for source in [string(&reference, 331), string(&reference, 332)].into_iter().flatten() { + let geometry = all.iter().find(|r| string(r, 5) == Some(source)).expect("referenced geometry"); + let start = geometry.iter().position(|(code, value)| *code == 102 && value == "{ACAD_REACTORS").expect("source reactor group"); + let end = + start + 1 + geometry.iter().skip(start + 1).position(|(code, value)| *code == 102 && value == "}").expect("reactor group end"); + assert_eq!(geometry[start + 1..end].iter().filter(|(code, value)| *code == 330 && value == reactor).count(), 1); + assert_eq!(start, 2, "reactors immediately follow the entity handle"); + assert_eq!(geometry.get(end + 1).map(|(code, _)| *code), Some(330), "entity owner follows reactors"); + assert_eq!(geometry.iter().filter(|(code, value)| *code == 102 && value == "{ACAD_REACTORS").count(), 1); + } + } + } +} + +/// Malformed self-links must not duplicate the reactor the dimension already carries. +#[test] +fn a_self_referential_dimension_writes_its_reactor_once() { + let mut c = case(); + assert!(c.d.model.modify(c.hl, |e| { + let EntityKind::Dimension(dm) = &mut e.kind else { panic!("dimension") }; + for reference in &mut dm.assoc { + reference.handle = c.hl; + } + })); + let text = write_dxf(&c.d); + let assoc = dimassoc(&text, c.hl).expect("DIMASSOC"); + let reactor = string(&assoc, 5).expect("DIMASSOC handle"); + let all = records(&text); + let handle = c.hl.hex(); + let dimension = all.iter().find(|r| string(r, 5) == Some(&handle)).expect("dimension record"); + let start = dimension.iter().position(|(code, value)| *code == 102 && value == "{ACAD_REACTORS").expect("reactor group"); + let end = start + 1 + dimension.iter().skip(start + 1).position(|(code, value)| *code == 102 && value == "}").expect("reactor group end"); + assert_eq!(dimension[start + 1..end].iter().filter(|(code, value)| *code == 330 && value == reactor).count(), 1); +} diff --git a/crates/io/tests/it/roundtrip_audit.rs b/crates/io/tests/it/roundtrip_audit.rs index 0b15f3181..c2a326935 100644 --- a/crates/io/tests/it/roundtrip_audit.rs +++ b/crates/io/tests/it/roundtrip_audit.rs @@ -1010,3 +1010,182 @@ fn dwg_keeps_extension_dictionaries_xrecords_and_entity_xdata() { assert_eq!(d.block("DOOR").map(|b| b.description.as_str()), Some("A door leaf")); assert!(d.ucss.iter().any(|u| u.name == "TILTED")); } + +/// A linear dimension whose two extension origins are snapped to `a` and `b`, so the saved file +/// carries a DIMASSOC naming both objects. +fn add_associated_dimension(d: &mut Drawing, a: cadcraft_doc::Handle, b: cadcraft_doc::Handle) { + use cadcraft_doc::{AssocSnap, Common, DimAssoc, DimKind, Dimension, Space}; + use cadcraft_geom::Vec3; + let at = |point: &str, handle| DimAssoc { point: point.into(), handle, snap: AssocSnap::Mid }; + let dm = Dimension { + kind: DimKind::Linear { rotation: 0.0 }, + defpt: Vec3::new(5.0, -3.0, 0.0), + text_mid: Vec3::ZERO, + p13: Vec3::ZERO, + p14: Vec3::new(10.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: Default::default(), + assoc: vec![at("p13", a), at("p14", b)], + marks: Default::default(), + }; + d.add(&Space::Model, Common::default(), EntityKind::Dimension(dm)).expect("add dimension"); +} + +/// The handle of the one DIMASSOC object of a written file. +fn dimassoc_handle(o: &Out) -> String { + let assocs = o.of("DIMASSOC"); + assert_eq!(assocs.len(), 1, "one DIMASSOC"); + h(assocs.first().copied().unwrap_or_default()) +} + +/// A source object that already has a reactor to a kept foreign object (the fixture's line 2A +/// names ACME_OBJECT 42) keeps it when a DIMASSOC reactor is added: one group with both (review on #2589). +#[test] +fn a_dimension_source_keeps_its_foreign_reactors_next_to_the_dimassoc() { + let mut d = read_dxf(fixture().as_bytes()).expect("read fixture"); + add_associated_dimension(&mut d, cadcraft_doc::Handle(0x2A), cadcraft_doc::Handle(0x2A)); + let o = Out::new(&write_dxf(&d)); + let line = o.of("LINE").into_iter().find(|t| h(t).eq_ignore_ascii_case("2A")).expect("line 2A"); + assert_eq!(line.iter().filter(|t| t.code == 102 && t.str() == "{ACAD_REACTORS").count(), 1, "one reactor group"); + let reactors = group(line, "ACAD_REACTORS"); + assert!(reactors.contains(&dimassoc_handle(&o)), "DIMASSOC reactor: {reactors:?}"); + assert!(reactors.iter().any(|r| o.handle(r).is_some_and(|(k, _)| k == "ACME_OBJECT")), "foreign reactor kept: {reactors:?}"); + assert!(o.dangling().is_empty(), "{:?}", o.dangling()); +} +/// An unmodelled entity a DIMASSOC refers to gets the reactor too, next to what it had (review on #2589). +#[test] +fn an_unknown_dimension_source_carries_the_dimassoc_reactor() { + let mut d = read_dxf(fixture().as_bytes()).expect("read fixture"); + add_associated_dimension(&mut d, cadcraft_doc::Handle(0x30), cadcraft_doc::Handle(0x2A)); + let o = Out::new(&write_dxf(&d)); + let widget = entity_by_text(&o, "ACME_WIDGET", "widget-in-model"); + let reactors = group(widget, "ACAD_REACTORS"); + assert!(reactors.contains(&dimassoc_handle(&o)), "DIMASSOC reactor: {reactors:?}"); + assert_eq!(widget.iter().filter(|t| t.code == 102 && t.str() == "{ACAD_REACTORS").count(), 1); + assert_eq!(owner(widget), h(o.named("BLOCK_RECORD", "*Model_Space")), "owner unchanged"); + assert!(o.dangling().is_empty(), "{:?}", o.dangling()); +} + +/// The handles of the `{ACAD_REACTORS` group of the record of handle `at`, after checking the +/// written text holds exactly one such group, placed as the DXF reference orders a record: +/// handle (5), then the reactors, then the owner (330), then the first subclass marker (100) and +/// any xdata (1001). Each handle appears once and names a DIMASSOC when `assoc_only` is set. +fn one_reactor_group(o: &Out, at: &str, assoc_only: bool) -> Vec { + let (_, rec) = o.handle(at).expect("source record"); + let codes: Vec<(i32, String)> = rec.iter().map(|t| (t.code, t.str())).collect(); + let starts: Vec = (0..codes.len()).filter(|&i| codes[i].0 == 102 && codes[i].1 == "{ACAD_REACTORS").collect(); + assert_eq!(starts.len(), 1, "exactly one reactor group in {codes:?}"); + let start = starts[0]; + let end = start + codes[start..].iter().position(|c| c.0 == 102 && c.1 == "}").expect("group end"); + let handles: Vec = codes[start + 1..end].iter().map(|c| c.1.clone()).collect(); + assert!(codes[start + 1..end].iter().all(|c| c.0 == 330), "only 330 groups inside: {codes:?}"); + let unique: std::collections::HashSet = handles.iter().map(|r| r.to_ascii_uppercase()).collect(); + assert_eq!(unique.len(), handles.len(), "each reactor once: {handles:?}"); + let pos = |code: i32| codes.iter().position(|c| c.0 == code); + assert_eq!(pos(5), Some(0), "handle first"); + let owner_at = end + 1 + codes[end + 1..].iter().position(|c| c.0 == 330).expect("owner"); + assert!(owner_at > end, "owner after the reactors: {codes:?}"); + assert!(end < pos(100).expect("subclass marker"), "reactors before the first subclass marker"); + if let Some(x) = pos(1001) { + assert!(end < x, "reactors before the xdata"); + } + if assoc_only { + for r in &handles { + assert_eq!(o.handle(r).map(|(k, _)| k), Some("DIMASSOC"), "{r} resolves to a DIMASSOC"); + } + } + handles +} + +/// A record that already has a reactors group (line 2A names ACME_OBJECT 42) comes out with one +/// group holding the foreign handle and the DIMASSOC, each once, in the same order after a +/// second save and with the extension dictionary group still between it and the owner. +/// Pinning test for the merge (review on #2589). +#[test] +fn a_record_with_a_reactor_group_gets_one_group_with_every_handle_once() { + let mut d = read_dxf(fixture().as_bytes()).expect("read fixture"); + add_associated_dimension(&mut d, cadcraft_doc::Handle(0x2A), cadcraft_doc::Handle(0x2A)); + let text = write_dxf(&d); + let o = Out::new(&text); + let reactors = one_reactor_group(&o, "2A", false); + assert_eq!(reactors.len(), 2, "foreign + DIMASSOC: {reactors:?}"); + let assoc = dimassoc_handle(&o); + assert_eq!(reactors.iter().filter(|r| r.eq_ignore_ascii_case(&assoc)).count(), 1); + assert!(reactors.iter().any(|r| o.handle(r).is_some_and(|(k, _)| k == "ACME_OBJECT"))); + let (_, line) = o.handle("2A").expect("line"); + let order: Vec = line.iter().map(|t| t.code).collect(); + let start = |name: &str| line.iter().position(|t| t.code == 102 && t.str() == format!("{{{name}")); + let (rx, xd) = (start("ACAD_REACTORS"), start("ACAD_XDICTIONARY")); + assert!(rx.is_some() && xd.is_some() && rx < xd, "reactors group before the extension dictionary group: {order:?}"); + assert_eq!(group(line, "ACAD_XDICTIONARY").len(), 1); + // Our reader accepts it, the association survives, and a second save keeps the same group. + let back = read_dxf(text.as_bytes()).expect("reopen"); + let again = Out::new(&write_dxf(&back)); + assert_eq!(again.of("DIMASSOC").len(), 1); + let second = one_reactor_group(&again, "2A", false); + assert_eq!(second.len(), 2, "{second:?}"); + assert!(second.iter().any(|r| again.handle(r).is_some_and(|(k, _)| k == "DIMASSOC"))); + assert!(again.dangling().is_empty(), "{:?}", again.dangling()); +} + +/// A record with no reactors group (line 2A with its group removed, keeping the extension +/// dictionary and the xdata) gets a new one in the preamble, between the handle and the owner, +/// so before the first subclass marker and the xdata. Pinning test (review on #2589). +#[test] +fn a_record_without_a_reactor_group_gets_a_new_one_in_the_preamble() { + let bare = fixture().replacen("5\n2A\n102\n{ACAD_REACTORS\n330\n42\n102\n}\n", "5\n2A\n", 1); + assert_ne!(bare, fixture(), "the fixture line lost its reactors"); + let mut d = read_dxf(bare.as_bytes()).expect("read fixture"); + add_associated_dimension(&mut d, cadcraft_doc::Handle(0x2A), cadcraft_doc::Handle(0x2A)); + let text = write_dxf(&d); + let o = Out::new(&text); + let (_, line) = o.handle("2A").expect("line"); + assert!(line.iter().any(|t| t.code == 1001), "the record keeps its xdata"); + let xdict_at = line.iter().position(|t| t.code == 102 && t.str() == "{ACAD_XDICTIONARY").expect("xdict group"); + let reactors = one_reactor_group(&o, "2A", true); + assert_eq!(reactors, vec![dimassoc_handle(&o)]); + // The preamble is the handle, then the reactors group and the extension dictionary group in + // the DXF reference's order, then the owner: nothing else in between. + let reactors_at = line.iter().position(|t| t.code == 102 && t.str() == "{ACAD_REACTORS").expect("reactor group"); + let owner_at = line.iter().position(|t| t.code == 330 && t.str() != reactors[0]).expect("owner"); + assert!(reactors_at < xdict_at && xdict_at < owner_at, "reactors {reactors_at}, xdict {xdict_at}, owner {owner_at}"); + assert!(line.iter().take(owner_at).all(|t| matches!(t.code, 5 | 102 | 330 | 360)), "only handle and groups before the owner"); + // Reading it back keeps the association and the DIMASSOC still resolves. + let back = read_dxf(text.as_bytes()).expect("reopen"); + let again = Out::new(&write_dxf(&back)); + one_reactor_group(&again, "2A", true); + assert!(again.dangling().is_empty(), "{:?}", again.dangling()); +} + +/// A new line used by two dimensions (so by two DIMASSOC objects) lists both, once each, in one +/// group that sits between its handle and its owner. Pinning test (review on #2589). +#[test] +fn a_source_used_by_two_associations_lists_both_dimassocs_once() { + use cadcraft_doc::{Common, Line, Space}; + use cadcraft_geom::Vec3; + let mut d = Drawing::new_metric(); + let line = d.add(&Space::Model, Common::default(), EntityKind::Line(Line { a: Vec3::ZERO, b: Vec3::new(10.0, 0.0, 0.0) })).expect("add line"); + add_associated_dimension(&mut d, line, line); + add_associated_dimension(&mut d, line, line); + let text = write_dxf(&d); + let o = Out::new(&text); + let assocs: Vec = o.of("DIMASSOC").into_iter().map(h).collect(); + assert_eq!(assocs.len(), 2, "one DIMASSOC per dimension"); + let reactors = one_reactor_group(&o, &line.hex(), true); + let mut got: Vec = reactors.iter().map(|r| r.to_ascii_uppercase()).collect(); + let mut want: Vec = assocs.iter().map(|r| r.to_ascii_uppercase()).collect(); + got.sort(); + want.sort(); + assert_eq!(got, want, "both associations, once each"); + let back = read_dxf(text.as_bytes()).expect("reopen"); + let again = Out::new(&write_dxf(&back)); + assert_eq!(one_reactor_group(&again, &line.hex(), true).len(), 2); + assert!(again.dangling().is_empty(), "{:?}", again.dangling()); +} From 152617f4df0b81d733cb0ca8a0596079c97b63d1 Mon Sep 17 00:00:00 2001 From: Books Date: Mon, 2 Nov 2026 06:38:34 +1100 Subject: [PATCH 038/100] Plot: a layout's saved page setup is used by default (part of #2493) (#2586) * Plot: a layout's saved page setup is used by default (#2493) A layout whose page setup says plot window, scale to fit or a plot scale was plotted 1:1 from the sheet origin unless the caller passed fit. plot, exportpdf, the PNG device and preview now read the layout's saved window, scale to fit, scale and centring when the options don't say (render::paper::saved_plot); explicit options win, and fit: false without a scale is still the plain 1:1 sheet. Tests: saved_setup_tests in pdf.rs (four), and preview_uses_the_layouts_saved_page_setup_by_default. * Parity checklist: regenerated after merging main * Parity checklist: regenerated after merging main * Plot: saved page setup in preview and PNG matches the PDF (review on #2586) Preview honours the saved centre when fitting a window and an explicit scale beats a saved scale to fit (PDF too); PNG places a saved window without fit and clips to the printable area. * Preview: a saved window clips to the printable area (review on #2586) The preview drew the placed plot over the margins while the PDF and PNG blank them. The blanking helper now lives in the render crate and both the PNG plot and the preview use it. Test: preview_clips_a_saved_window_to_the_printable_area. * Preview: pin hostile saved page setups and the default setup (review on #2586) Degenerate or far-away saved windows and saved scales of 0 or not finite already fall back to the plain plot in the PDF, PNG and preview; tests now pin that, and that a layout with no saved setup plots as before. * Preview: the printable clip follows the view's fit (review on #2586) The clip assumed the sheet filled the image; when the height cap applies View::fit scales and centres it. The printable corners now go through View::to_screen. Test: preview_clip_follows_the_view_when_the_height_cap_applies. * docs: leave the generated parity checklist to maintainers Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Brandon Thomas Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/exchange.rs | 169 ++++++++++++++++++++++++++++-- crates/io/src/pdf.rs | 129 +++++++++++++++++++++-- crates/io/src/plotter.rs | 158 +++++++++++++++++++++++++--- crates/render/src/paper.rs | 27 +++++ crates/render/src/raster.rs | 31 ++++++ 5 files changed, 487 insertions(+), 27 deletions(-) diff --git a/crates/engine/src/cmd/exchange.rs b/crates/engine/src/cmd/exchange.rs index e28de89fc..f6ce1e9b6 100644 --- a/crates/engine/src/cmd/exchange.rs +++ b/crates/engine/src/cmd/exchange.rs @@ -330,17 +330,23 @@ fn run_preview(s: &mut Session, p: &Value) -> Result { if let Some(l) = p.get("landscape").and_then(Value::as_bool) { page.landscape = l; } + // A layout's saved page setup (plot window, scale to fit, scale, centring) applies unless the call says otherwise (#2493). + // An explicit `fit: false` without a `scale` asks for the plain 1:1 plot. + let plain = p.get("fit").and_then(Value::as_bool) == Some(false) && p.get("scale").is_none(); + let saved = if space.is_paper() && !plain { cadcraft_render::paper::saved_plot(&page) } else { Default::default() }; let sheet = Sheet::from_page(&page, paper::page_unit_mm(d, &page)); let lineweights = p.get("lineweights").and_then(Value::as_bool).unwrap_or(if space.is_paper() { page.lineweights } else { true }); let transparency = super::plotters::plot_transparency(s, p).unwrap_or(page.plot_transparency); - let fit = p.get("fit").and_then(Value::as_bool).unwrap_or(matches!(space, Space::Model)); - let scale = p.get("scale").and_then(Value::as_f64).filter(|v| v.is_finite() && *v > 0.0); + let given_scale = p.get("scale").and_then(Value::as_f64).filter(|v| v.is_finite() && *v > 0.0); + // A saved scale to fit applies only when the call gives neither `fit` nor `scale`, as in the PDF. + let fit = p.get("fit").and_then(Value::as_bool).unwrap_or(matches!(space, Space::Model) || (saved.fit && given_scale.is_none())); + let scale = given_scale.or(saved.scale); let width = p.get("width").and_then(Value::as_u64).unwrap_or(PREVIEW_PX).clamp(16, MAX_PREVIEW_PX); let height = ((width as f64) * sheet.size.y / sheet.size.x).round(); let height = if height.is_finite() { (height as u64).clamp(16, MAX_PREVIEW_PX) } else { width }; let px_per_paper_unit = width as f64 / sheet.size.x; // Curves are flattened to about half a pixel. - let est = plot_scale(&d.extents(&space), &sheet, fit, scale); + let est = plot_scale(&saved.window.unwrap_or_else(|| d.extents(&space)), &sheet, fit, scale); let tol = 0.5 / (px_per_paper_unit * est); let ropts = cadcraft_render::Options { tolerance: if tol.is_finite() && tol > 0.0 { tol } else { 0.01 }, @@ -352,9 +358,13 @@ fn run_preview(s: &mut Session, p: &Value) -> Result { ..Default::default() }; let list = cadcraft_render::build_plot(d, &space, &ropts); - let b = list.bounds; + let b = saved.window.unwrap_or(list.bounds); // Paper point q shows drawing point (q - to) / k + from. - let (from, k, to) = if fit || space == Space::Model { + let (from, k, to) = if saved.window.is_some() { + // A saved window, fitted or at a saved scale: its centre (or corner) on the printable area's. + let c = saved.center; + (if c { b.center() } else { b.min }, plot_scale(&b, &sheet, fit, scale), if c { sheet.printable.center() } else { sheet.printable.min }) + } else if fit || space == Space::Model { (if b.is_empty() { Vec2::ZERO } else { b.center() }, plot_scale(&b, &sheet, fit, scale), sheet.printable.center()) } else { (Vec2::ZERO, plot_scale(&b, &sheet, fit, scale), Vec2::ZERO) @@ -372,7 +382,20 @@ fn run_preview(s: &mut Session, p: &Value) -> Result { px_per_mm: if lineweights && px_per_mm.is_finite() { px_per_mm } else { 0.0 }, antialias: true, }; - let png = cadcraft_render::raster::render_png(&list, &view, &ro).ok_or_else(|| bad("preview", "rendering the preview failed"))?; + let mut pm = cadcraft_render::raster::render(&list, &view, &ro).ok_or_else(|| bad("preview", "rendering the preview failed"))?; + // A placed plot (fitted, or a saved window) shows only inside the printable area, as the PDF and PNG plots clip it. + if fit || saved.window.is_some() || space == Space::Model { + // The view may scale and centre the sheet (the height cap), so map the printable corners + // (paper point q shows drawing point (q - to) / k + from) through it. + let screen = |q: Vec2| { + let (x, y) = view.to_screen((q - to) / k + from); + (f64::from(x), f64::from(y)) + }; + let (min, max) = (sheet.printable.min, sheet.printable.max); + let (a, b) = (screen(Vec2::new(min.x, max.y)), screen(Vec2::new(max.x, min.y))); + cadcraft_render::raster::blank_outside_pixels(&mut pm, a, b); + } + let png = pm.encode_png().map_err(|_| bad("preview", "rendering the preview failed"))?; let layout = space.tab_name(); Ok(json!({ "data": base64_encode(&png), "width": w, "height": h, "layout": layout, "paper": page.paper, "landscape": page.landscape })) } @@ -738,6 +761,140 @@ mod tests { assert_ne!(png(&mut s, json!({ "layout": "Layout1", "width": 200, "fit": false, "scale": 0.5 })), default, "scale 0.5 is not 1:1"); } + /// A layout's saved page setup (plot window, scale to fit) shapes its preview by default, as + /// it shapes the PDF; `fit: false` still gives the plain 1:1 sheet (#2493). + #[test] + fn preview_uses_the_layouts_saved_page_setup_by_default() { + let mut s = Session::new(); + s.execute("layout.set", &json!({ "name": "Layout1" })).unwrap(); + s.execute("line", &json!({ "points": [[20.0, 5.0], [836.0, 589.0]] })).unwrap(); + s.execute("pagesetup", &json!({ "layout": "Layout1", "paper": "A4", "landscape": true, "margins": [0, 0, 0, 0] })).unwrap(); + let png = |s: &mut Session, p: Value| s.execute("preview", &p).unwrap()["data"].as_str().unwrap_or_default().to_string(); + let unsaved = png(&mut s, json!({ "layout": "Layout1", "width": 200 })); + let page = &mut s.doc_mut().unwrap().layouts.iter_mut().find(|l| l.name == "Layout1").unwrap().page; + page.plot_area = "window".into(); + page.plot_window = [20.0, 5.0, 836.0, 589.0]; + page.scale_to_fit = true; + let saved = png(&mut s, json!({ "layout": "Layout1", "width": 200 })); + assert_ne!(saved, unsaved, "the saved window and scale to fit change the preview"); + assert_eq!(png(&mut s, json!({ "layout": "Layout1", "width": 200, "fit": false })), unsaved, "fit: false is the plain 1:1 sheet"); + } + + /// With a saved plot window the preview follows the page setup's centring (fitted or not) and an + /// explicit `scale` beats a saved scale to fit, as in the PDF. + /// A placed saved window shows only inside the printable area, as the PDF and PNG plots clip it: + /// geometry in the margin leaves the preview unchanged. + #[test] + fn preview_clips_a_saved_window_to_the_printable_area() { + let build = |margin_line: bool| { + let mut s = Session::new(); + s.execute("layout.set", &json!({ "name": "Layout1" })).unwrap(); + s.execute("line", &json!({ "points": [[0.0, 30.0], [100.0, 30.0]] })).unwrap(); + if margin_line { + s.execute("line", &json!({ "points": [[-3.0, 0.0], [-3.0, 60.0]] })).unwrap(); + } + s.execute("pagesetup", &json!({ "layout": "Layout1", "paper": "A4", "landscape": true, "margins": [10, 10, 10, 10] })).unwrap(); + let page = &mut s.doc_mut().unwrap().layouts.iter_mut().find(|l| l.name == "Layout1").unwrap().page; + page.plot_area = "window".into(); + page.plot_window = [0.0, 0.0, 100.0, 60.0]; + page.scale_to_fit = true; + page.center = true; + s.execute("preview", &json!({ "layout": "Layout1", "width": 600 })).unwrap()["data"].as_str().unwrap_or_default().to_string() + }; + assert_eq!(build(true), build(false), "a line in the margin is clipped away"); + } + + /// On a tall sheet the height cap makes the view scale and centre the sheet in the image; the + /// printable clip must follow it, so ink in the margin is still blanked. + #[test] + fn preview_clip_follows_the_view_when_the_height_cap_applies() { + let build = |margin_line: bool| { + let mut s = Session::new(); + s.execute("layout.set", &json!({ "name": "Layout1" })).unwrap(); + s.execute("line", &json!({ "points": [[0.0, 50.0], [100.0, 50.0]] })).unwrap(); + if margin_line { + s.execute("line", &json!({ "points": [[-2.6, 0.0], [-2.6, 100.0]] })).unwrap(); + } + s.execute("pagesetup", &json!({ "layout": "Layout1", "margins": [10, 10, 10, 10] })).unwrap(); + let page = &mut s.doc_mut().unwrap().layouts.iter_mut().find(|l| l.name == "Layout1").unwrap().page; + page.width_mm = 210.0; + page.height_mm = 1200.0; + page.landscape = false; + page.plot_area = "window".into(); + page.plot_window = [0.0, 0.0, 100.0, 100.0]; + page.scale_to_fit = true; + page.center = true; + s.execute("preview", &json!({ "layout": "Layout1", "width": 1000 })).unwrap()["data"].as_str().unwrap_or_default().to_string() + }; + assert_eq!(build(true), build(false), "a line in the margin is clipped away on a tall sheet"); + } + + /// Hostile saved setups (a degenerate or far-away window, a scale of 0 or not finite) fall back + /// to the plain preview and never panic, and a layout with no saved setup previews exactly as + /// the plain 1:1 sheet does. + #[test] + fn preview_survives_hostile_saved_setups_and_default_is_unchanged() { + let mut s = Session::new(); + s.execute("layout.set", &json!({ "name": "Layout1" })).unwrap(); + s.execute("line", &json!({ "points": [[20.0, 5.0], [200.0, 100.0]] })).unwrap(); + s.execute("pagesetup", &json!({ "layout": "Layout1", "paper": "A4", "landscape": true })).unwrap(); + let png = |s: &mut Session, p: Value| s.execute("preview", &p).unwrap()["data"].as_str().unwrap_or_default().to_string(); + let call = json!({ "layout": "Layout1", "width": 200 }); + let plain = png(&mut s, json!({ "layout": "Layout1", "width": 200, "fit": false })); + assert_eq!(png(&mut s, call.clone()), plain, "no saved setup: the default preview is the plain sheet"); + let set = |s: &mut Session, window: [f64; 4], scale: f64, fit: bool| { + let page = &mut s.doc_mut().unwrap().layouts.iter_mut().find(|l| l.name == "Layout1").unwrap().page; + page.plot_area = "window".into(); + page.plot_window = window; + page.scale = scale; + page.scale_to_fit = fit; + }; + let good = [0.0, 0.0, 100.0, 60.0]; + let (nan, inf) = (f64::NAN, f64::INFINITY); + for window in [[5.0, 5.0, 5.0, 40.0], [5.0, 5.0, 40.0, 5.0], [nan, 0.0, 10.0, 10.0], [0.0, 0.0, inf, 10.0]] { + set(&mut s, window, 1.0, true); + assert_eq!(png(&mut s, call.clone()), plain, "{window:?}: a degenerate window changes nothing"); + } + for scale in [0.0, -2.0, nan, inf] { + set(&mut s, good, scale, false); + let _ = png(&mut s, call.clone()); + set(&mut s, [5.0, 5.0, 5.0, 40.0], scale, false); + assert_eq!(png(&mut s, call.clone()), plain, "{scale}: an unusable saved scale is ignored"); + } + set(&mut s, [1e9, 1e9, 1e9 + 10.0, 1e9 + 10.0], 1.0, true); + let _ = png(&mut s, call.clone()); + set(&mut s, [-1e300, -1e300, 1e300, 1e300], 1.0, true); + let _ = png(&mut s, call.clone()); + } + + #[test] + fn preview_honours_saved_centering_and_an_explicit_scale() { + let mut s = Session::new(); + s.execute("layout.set", &json!({ "name": "Layout1" })).unwrap(); + s.execute("line", &json!({ "points": [[20.0, 5.0], [836.0, 589.0]] })).unwrap(); + s.execute("pagesetup", &json!({ "layout": "Layout1", "paper": "A4", "landscape": true, "margins": [0, 0, 0, 0] })).unwrap(); + let page = &mut s.doc_mut().unwrap().layouts.iter_mut().find(|l| l.name == "Layout1").unwrap().page; + page.plot_area = "window".into(); + page.plot_window = [20.0, 5.0, 400.0, 200.0]; + page.scale_to_fit = true; + let png = |s: &mut Session, p: Value| s.execute("preview", &p).unwrap()["data"].as_str().unwrap_or_default().to_string(); + let set_center = |s: &mut Session, c: bool| s.doc_mut().unwrap().layouts.iter_mut().find(|l| l.name == "Layout1").unwrap().page.center = c; + set_center(&mut s, true); + let centred = png(&mut s, json!({ "layout": "Layout1", "width": 200 })); + set_center(&mut s, false); + // The window is fitted by width, so only the other axis shows where it sits. + let corner = png(&mut s, json!({ "layout": "Layout1", "width": 200 })); + assert_ne!(corner, centred, "a saved centre: false puts the fitted window at the printable corner"); + let page = &mut s.doc_mut().unwrap().layouts.iter_mut().find(|l| l.name == "Layout1").unwrap().page; + page.center = true; + let by_scale = png(&mut s, json!({ "layout": "Layout1", "width": 200, "scale": 0.5 })); + assert_ne!(by_scale, centred, "an explicit scale is not the fitted plot"); + assert_eq!( + by_scale, + png(&mut s, json!({ "layout": "Layout1", "width": 200, "fit": false, "scale": 0.5 })), + "scale decides, not the saved fit" + ); + } #[test] fn pdfbook_collects_frames_into_one_pdf_per_row() { our_hooks(); diff --git a/crates/io/src/pdf.rs b/crates/io/src/pdf.rs index b06580d53..ba9d65867 100644 --- a/crates/io/src/pdf.rs +++ b/crates/io/src/pdf.rs @@ -508,8 +508,37 @@ struct SheetPage { label: String, } +/// `o` with a layout's saved page setup filled in where the options don't say: its plot window, +/// scale to fit or saved scale, and centring (#2493). Explicit options win; model space has no +/// saved setup. +pub(crate) fn with_saved_setup(d: &Drawing, space: &Space, o: &PdfOptions) -> PdfOptions { + let mut o = o.clone(); + let Some(page) = space.layout().and_then(|n| d.layout(n)).map(|l| &l.page) else { return o }; + // An explicit `fit: false` without a `scale` asks for the plain 1:1 plot of the sheet. + if o.fit == Some(false) && o.scale.is_none() { + o.window = None; + return o; + } + let saved = paper::saved_plot(page); + // A layout is plotted over its own sheet: only its saved window applies, not one in the options. + o.window = saved.window; + // An explicit scale beats a saved scale to fit (the scale is ignored while fitting). + if o.fit.is_none() && o.scale.is_none() && saved.fit { + o.fit = Some(true); + } + if o.scale.is_none() { + o.scale = saved.scale; + } + if o.center.is_none() && saved.window.is_some() { + o.center = Some(saved.center); + } + o +} + /// Plot one space. fn sheet(d: &Drawing, space: &Space, o: &PdfOptions, links_to: &[(Handle, String)]) -> Result { + let saved = with_saved_setup(d, space, o); + let o = &saved; let page = page_for(d, space, o)?; let unit_mm = paper::page_unit_mm(d, &page); let sheet = Sheet::from_page(&page, unit_mm); @@ -530,8 +559,8 @@ fn sheet(d: &Drawing, space: &Space, o: &PdfOptions, links_to: &[(Handle, String cancel: None, }; let k = unit_mm * PT_PER_MM; - let window = o.window.filter(|w| matches!(space, Space::Model) && !w.is_empty()); - let fit = o.fit.unwrap_or(window.is_some() || matches!(space, Space::Model)); + let window = o.window.filter(|w| !w.is_empty()); + let fit = o.fit.unwrap_or(matches!(space, Space::Model)); // Chord tolerance: about 0.05 mm on paper. let est = plot_scale(&window.unwrap_or_else(|| d.extents(space)), &sheet, fit, o.scale); let tol = 0.05 / unit_mm / est.max(1e-300); @@ -541,8 +570,11 @@ fn sheet(d: &Drawing, space: &Space, o: &PdfOptions, links_to: &[(Handle, String let list = cadcraft_render::build_plot(d, space, &cadcraft_render::Options { tolerance, view_height, ..ropts }); let b = window.unwrap_or(list.bounds); let s = plot_scale(&b, &sheet, fit, o.scale); - let map = if fit || matches!(space, Space::Model) { - let (from, to) = placement(space, &b, fit, &sheet, o); + // A layout plotted over a window is placed like model space: the window on the printable area. + let placed = fit || window.is_some() || matches!(space, Space::Model); + let map = if placed { + let by = if window.is_some() { &Space::Model } else { space }; + let (from, to) = placement(by, &b, fit, &sheet, o); Map { from, s, to, k } } else { Map { from: Vec2::ZERO, s, to: Vec2::ZERO, k } @@ -551,11 +583,7 @@ fn sheet(d: &Drawing, space: &Space, o: &PdfOptions, links_to: &[(Handle, String let mut list = list; cadcraft_render::pen::apply(&mut list, map.s * unit_mm); let media = sheet.size * k; - let clip_pt = if fit || matches!(space, Space::Model) { - Bounds2::new(sheet.printable.min * k, sheet.printable.max * k) - } else { - Bounds2::new(Vec2::ZERO, media) - }; + let clip_pt = if placed { Bounds2::new(sheet.printable.min * k, sheet.printable.max * k) } else { Bounds2::new(Vec2::ZERO, media) }; // The layers drawn on, in order of first use. let mut layers: Vec = Vec::new(); if o.layers { @@ -989,3 +1017,86 @@ fn uri(url: &str) -> Option { } Some(out) } + +#[cfg(test)] +mod saved_setup_tests { + use super::*; + use cadcraft_doc::{Common, EntityKind, Layout, Line}; + use cadcraft_geom::Vec3; + use serde_json::json; + + /// A layout drawn in mm at A1 size (frame 20,5 - 836,589) whose page setup is A4 landscape + /// plotting that window, as in #2493. + fn drawing(scale_to_fit: bool, scale: f64) -> Drawing { + let mut d = Drawing::new_metric(); + let mut l = Layout::new("L1", 3); + l.page = PageSetup { + width_mm: 297.0, + height_mm: 210.0, + landscape: true, + margins_mm: [0.0; 4], + plot_area: "window".into(), + plot_window: [20.0, 5.0, 836.0, 589.0], + scale_to_fit, + scale, + center: true, + ..PageSetup::default() + }; + d.layouts.push(l); + let line = EntityKind::Line(Line { a: Vec3::new(20.0, 5.0, 0.0), b: Vec3::new(836.0, 589.0, 0.0) }); + d.add(&Space::Paper("L1".into()), Common::default(), line).unwrap(); + d + } + + fn map(d: &Drawing, opts: serde_json::Value) -> Map { + sheet(d, &Space::Paper("L1".into()), &PdfOptions::from_json(&opts), &[]).unwrap().map + } + + /// A layout plotted with no options uses its saved scale to fit and plot window, not 1:1. + #[test] + fn a_layout_plots_its_saved_window_scaled_to_fit_by_default() { + let d = drawing(true, 1.0); + let m = map(&d, json!({})); + let want = (297.0_f64 / 816.0).min(210.0 / 584.0); + assert!((m.s - want).abs() < 1e-9, "scale {} not {want}", m.s); + assert!(m.from.dist(Vec2::new(428.0, 297.0)) < 1e-9, "window centre, got {:?}", m.from); + assert!(m.to.dist(Vec2::new(148.5, 105.0)) < 1e-9, "page centre, got {:?}", m.to); + } + + /// The options still win: `fit: false` is the plain 1:1 plot, and a `scale` is used as given. + #[test] + fn explicit_options_override_the_saved_setup() { + let d = drawing(true, 1.0); + let m = map(&d, json!({"fit": false})); + assert_eq!((m.s, m.from, m.to), (1.0, Vec2::ZERO, Vec2::ZERO)); + let m = map(&d, json!({"fit": false, "scale": 0.25})); + assert!((m.s - 0.25).abs() < 1e-12, "{}", m.s); + } + + /// A saved scale (not scale to fit) is used by default, with the window centred on the page. + #[test] + fn a_layout_plots_at_its_saved_scale_by_default() { + let d = drawing(false, 0.3394); + let m = map(&d, json!({})); + assert!((m.s - 0.3394).abs() < 1e-12, "{}", m.s); + assert!(m.from.dist(Vec2::new(428.0, 297.0)) < 1e-9 && m.to.dist(Vec2::new(148.5, 105.0)) < 1e-9, "{:?} {:?}", m.from, m.to); + } + + /// A page setup with nothing saved to fit plots as it always did. + #[test] + fn a_layout_without_a_saved_window_or_scale_is_unchanged() { + let mut d = drawing(false, 1.0); + d.layouts.iter_mut().find(|l| l.name == "L1").unwrap().page.plot_area = "layout".into(); + let m = map(&d, json!({})); + assert_eq!((m.s, m.from, m.to), (1.0, Vec2::ZERO, Vec2::ZERO)); + } + + /// An explicit `scale` beats a saved scale to fit, in the PDF as in the preview. + #[test] + fn an_explicit_scale_overrides_the_saved_scale_to_fit() { + let d = drawing(true, 1.0); + let m = map(&d, json!({"scale": 0.25})); + assert!((m.s - 0.25).abs() < 1e-12, "{}", m.s); + assert!(m.from.dist(Vec2::new(428.0, 297.0)) < 1e-9 && m.to.dist(Vec2::new(148.5, 105.0)) < 1e-9, "still placed: {:?} {:?}", m.from, m.to); + } +} diff --git a/crates/io/src/plotter.rs b/crates/io/src/plotter.rs index 12da96c3b..f0a174ea1 100644 --- a/crates/io/src/plotter.rs +++ b/crates/io/src/plotter.rs @@ -63,7 +63,7 @@ fn fit_scale(b: &Bounds2, sheet: &Sheet, o: &PdfOptions) -> f64 { } fn png(d: &Drawing, space: &Space, opts: &Value) -> Result> { - let o = PdfOptions::from_json(opts); + let o = pdf::with_saved_setup(d, space, &PdfOptions::from_json(opts)); let (sheet, page) = sheet(d, space, &o)?; let dpi = opts.get("resolution").and_then(Value::as_f64).filter(|r| r.is_finite()).unwrap_or(300.0).clamp(50.0, 2400.0); // Pixels per paper unit, lowered so the image stays within the caps. @@ -78,10 +78,17 @@ fn png(d: &Drawing, space: &Space, opts: &Value) -> Result> { let (width, height) = (px(sheet.size.x), px(sheet.size.y)); let lineweights = o.lineweights.unwrap_or(if space.is_paper() { page.lineweights } else { true }); let table = cadcraft_doc::plot_style_table(d, &page.plot_style_table).map(Arc::new); - // Paper units per drawing unit: 1 for a layout at 1:1, the fitted scale for model space. + // Paper units per drawing unit: the fitted scale for model space or a fitted layout, else the + // `scale` option or 1:1, as PDF plots it. let fit = matches!(space, Space::Model) || o.fit == Some(true); - let window = o.window.filter(|w| matches!(space, Space::Model) && !w.is_empty()); - let est = if fit { fit_scale(&window.unwrap_or_else(|| d.extents(space)), &sheet, &o) } else { 1.0 }; + let window = o.window.filter(|w| !w.is_empty()); + // A layout over a saved window is placed like model space, the window on the printable area. + let placed = fit || window.is_some(); + let est = if fit { + fit_scale(&window.unwrap_or_else(|| d.extents(space)), &sheet, &o) + } else { + pdf::plot_scale(&Bounds2::EMPTY, &sheet, false, o.scale) + }; // Chord tolerance: a fifth of a pixel. let tolerance = (0.2 / (k * est)).max(1e-9); let transparency = o.plot_transparency.unwrap_or(page.plot_transparency); @@ -89,21 +96,27 @@ fn png(d: &Drawing, space: &Space, opts: &Value) -> Result> { let mut list = cadcraft_render::build_plot(d, space, &ropts); let half = Vec2::new(f64::from(width), f64::from(height)) / (2.0 * k); let b = window.unwrap_or(list.bounds); - let s = if fit { fit_scale(&b, &sheet, &o) } else { 1.0 }; + let s = if fit { fit_scale(&b, &sheet, &o) } else { est }; // Plot style ends, joins and fill patterns at the plot scale, as PDF draws them (a layout's // are already drawn). cadcraft_render::pen::apply(&mut list, s * sheet.unit_mm); - let view = if fit { - let (from, to) = pdf::placement(space, &b, true, &sheet, &o); - // The world point at the image centre, mapped through the same paper origin as PDF. - let center = from + (half - to) / s; - raster::View { center, scale: k * s, width, height } + let (from, to) = if placed { + // A window is placed like model space, whatever the space (as in PDF). + pdf::placement(if window.is_some() { &Space::Model } else { space }, &b, fit, &sheet, &o) } else { - raster::View { center: half, scale: k, width, height } + (Vec2::ZERO, Vec2::ZERO) }; + // The world point at the image centre, mapped through the same paper origin as PDF. + let center = from + (half - to) / s; + let view = raster::View { center, scale: k * s, width, height }; let px_per_mm = if lineweights { (k / sheet.unit_mm) as f32 } else { 0.0 }; let ro = raster::RasterOptions { background: Rgb(255, 255, 255), hairline: 1.0, px_per_mm, antialias: true }; - raster::render_png(&list, &view, &ro).ok_or_else(|| IoError::Format("render failed".into())) + let mut pm = raster::render(&list, &view, &ro).ok_or_else(|| IoError::Format("render failed".into()))?; + if placed { + // PDF clips a placed plot to the printable area: blank the rest of the sheet. + raster::blank_outside_printable(&mut pm, &sheet.printable, k); + } + pm.encode_png().map_err(|_| IoError::Format("render failed".into())) } fn svg(d: &Drawing, space: &Space, opts: &Value) -> Result> { @@ -237,4 +250,125 @@ mod tests { assert!(err.to_string().contains("not supported for SVG")); assert!(plot(&d, &model, &json!({"output": "svg", "center": true})).unwrap().starts_with(b" Drawing { + let mut d = Drawing::new_metric(); + let mut l = cadcraft_doc::Layout::new("L1", 3); + l.page = cadcraft_doc::PageSetup { + width_mm: 297.0, + height_mm: 210.0, + landscape: true, + margins_mm: [10.0; 4], + plot_area: "window".into(), + plot_window: [0.0, 0.0, 100.0, 60.0], + scale_to_fit, + scale: 1.0, + center: true, + ..cadcraft_doc::PageSetup::default() + }; + d.layouts.push(l); + let sp = Space::Paper("L1".into()); + let mut line = |a: (f64, f64), b: (f64, f64)| { + d.add(&sp, Common::default(), EntityKind::Line(Line { a: Vec3::new(a.0, a.1, 0.0), b: Vec3::new(b.0, b.1, 0.0) })).unwrap(); + }; + line((0.0, 30.0), (100.0, 30.0)); + line((-3.0, 0.0), (-3.0, 60.0)); + d + } + + /// Whether any pixel of the PNG in the column range `x0..=x1` (mm from the sheet's left edge) + /// at the row of `y_mm` (from the sheet's bottom edge, give or take 2 px) is not white. + fn inked(png: &[u8], x0_mm: f64, x1_mm: f64, y_mm: f64) -> bool { + let pm = tiny_skia::Pixmap::decode_png(png).unwrap(); + let px_per_mm = f64::from(pm.width()) / 297.0; + let row = (f64::from(pm.height()) - y_mm * px_per_mm).round() as i64; + let (c0, c1) = ((x0_mm * px_per_mm).round() as i64, (x1_mm * px_per_mm).round() as i64); + (row - 2..=row + 2).any(|r| (c0..=c1).any(|c| pm.pixel(c as u32, r as u32).is_some_and(|p| p.red() < 200))) + } + + const LAYOUT_PNG: &str = r#"{"output": "png", "resolution": 100}"#; + + /// Without scale to fit, a saved window is still placed (centred on the printable area, 1:1) as PDF + /// places it, not left at the sheet origin. + #[test] + fn png_places_the_saved_window_when_fit_is_off() { + let d = layout_drawing(false); + let png = plot(&d, &Space::Paper("L1".into()), &serde_json::from_str(LAYOUT_PNG).unwrap()).unwrap(); + // The 100 mm line is centred on the sheet: 98.5 to 198.5 mm. At the origin it would end at 100 mm. + assert!(inked(&png, 140.0, 160.0, 105.0), "the window's line crosses the sheet centre"); + assert!(!inked(&png, 10.0, 90.0, 105.0), "nothing at the sheet origin"); + } + + /// An explicit scale overrides a saved scale to fit, and the PNG uses it. + #[test] + fn png_explicit_scale_beats_saved_fit() { + let d = layout_drawing(true); + let sp = Space::Paper("L1".into()); + let fitted = plot(&d, &sp, &serde_json::from_str(LAYOUT_PNG).unwrap()).unwrap(); + assert!(inked(&fitted, 200.0, 280.0, 105.0), "fitted, the 100 unit line is 277 mm wide"); + let half = plot(&d, &sp, &json!({"output": "png", "resolution": 100, "scale": 0.5})).unwrap(); + assert!(inked(&half, 130.0, 170.0, 105.0)); + assert!(!inked(&half, 200.0, 280.0, 105.0), "at 0.5 the line is 50 mm wide, centred"); + } + + /// A placed window shows only inside the printable area, as PDF clips it. + #[test] + fn png_clips_a_placed_window_to_the_printable_area() { + let d = layout_drawing(true); + let png = plot(&d, &Space::Paper("L1".into()), &serde_json::from_str(LAYOUT_PNG).unwrap()).unwrap(); + // The line 3 units left of the window lands about 1.7 mm from the sheet edge, in the margin. + assert!(!inked(&png, 0.0, 9.0, 105.0), "the margin stays blank"); + } + + /// A layout with `window`/`scale` saved (any values, hostile ones included). + fn saved_drawing(window: [f64; 4], scale: f64, fit: bool) -> Drawing { + let mut d = layout_drawing(fit); + let page = &mut d.layouts.iter_mut().find(|l| l.name == "L1").unwrap().page; + page.plot_window = window; + page.scale = scale; + d + } + + /// Degenerate windows, a window off the drawing, and a saved scale of 0 or not finite fall back + /// to the plain plot: the setup resolves to no window or scale, and PDF and PNG still plot. + #[test] + fn hostile_saved_setups_fall_back_and_never_panic() { + let sp = Space::Paper("L1".into()); + let nan = f64::NAN; + let inf = f64::INFINITY; + for window in [[5.0, 5.0, 5.0, 40.0], [5.0, 5.0, 40.0, 5.0], [nan, 0.0, 10.0, 10.0], [0.0, 0.0, inf, 10.0]] { + let d = saved_drawing(window, 1.0, true); + let o = pdf::with_saved_setup(&d, &sp, &PdfOptions::from_json(&json!({}))); + assert!(o.window.is_none(), "{window:?}: a degenerate window is dropped, got {:?}", o.window); + assert!(plot(&d, &sp, &json!({})).unwrap().starts_with(b"%PDF"), "{window:?}"); + assert!(plot(&d, &sp, &serde_json::from_str(LAYOUT_PNG).unwrap()).is_ok(), "{window:?}"); + } + for scale in [0.0, -2.0, nan, inf] { + let d = saved_drawing([0.0, 0.0, 100.0, 60.0], scale, false); + let o = pdf::with_saved_setup(&d, &sp, &PdfOptions::from_json(&json!({}))); + assert!(o.scale.is_none(), "{scale}: an unusable saved scale is ignored"); + assert!(plot(&d, &sp, &json!({})).unwrap().starts_with(b"%PDF"), "{scale}"); + assert!(plot(&d, &sp, &serde_json::from_str(LAYOUT_PNG).unwrap()).is_ok(), "{scale}"); + } + // A window far from everything drawn is plotted (an empty sheet), not an error. + let d = saved_drawing([1e9, 1e9, 1e9 + 10.0, 1e9 + 10.0], 1.0, true); + assert!(plot(&d, &sp, &json!({})).unwrap().starts_with(b"%PDF")); + assert!(plot(&d, &sp, &serde_json::from_str(LAYOUT_PNG).unwrap()).is_ok()); + } + + /// A layout with no saved setup (`PageSetup::default()`) plots as the options say: nothing is + /// filled in from it. + #[test] + fn a_layout_without_saved_setup_changes_nothing() { + let mut d = Drawing::new_metric(); + d.layouts.push(cadcraft_doc::Layout::new("L1", 3)); + let sp = Space::Paper("L1".into()); + let given = PdfOptions::from_json(&json!({"scale": 0.5})); + let o = pdf::with_saved_setup(&d, &sp, &given); + assert_eq!((o.window, o.fit, o.scale, o.center), (None, None, Some(0.5), None)); + let o = pdf::with_saved_setup(&d, &sp, &PdfOptions::from_json(&json!({}))); + assert_eq!((o.window, o.fit, o.scale, o.center), (None, None, None, None)); + } } diff --git a/crates/render/src/paper.rs b/crates/render/src/paper.rs index 457a2a48d..ff4a027ba 100644 --- a/crates/render/src/paper.rs +++ b/crates/render/src/paper.rs @@ -155,3 +155,30 @@ mod tests { assert!(s.size.x > 0.0 && s.printable == s.bounds()); } } + +/// What a layout's saved page setup says about how to plot it, for a plot that was not given the +/// matching options (#2493): the plot window, whether to scale to fit, the saved scale, and +/// centring. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct SavedPlot { + /// The saved plot window, when the plot area is "window" and its corners are a real box. + pub window: Option, + /// Scale to fit the window (or the extents) on the printable area. + pub fit: bool, + /// The saved plot scale, paper units per drawing unit, when it isn't scale to fit or 1:1. + pub scale: Option, + /// Centre the plot on the printable area. + pub center: bool, +} + +/// The saved plot settings of `page`. A layout plotted over its whole sheet ("layout" plot area) +/// has nothing to fit, so scale to fit only counts for a window or the extents. +pub fn saved_plot(page: &PageSetup) -> SavedPlot { + let [x1, y1, x2, y2] = page.plot_window; + let window = (page.plot_area.eq_ignore_ascii_case("window") && [x1, y1, x2, y2].iter().all(|v| v.is_finite())) + .then(|| Bounds2::new(Vec2::new(x1, y1), Vec2::new(x2, y2))) + .filter(|w| w.width() > 0.0 && w.height() > 0.0); + let fit = page.scale_to_fit && (window.is_some() || page.plot_area.eq_ignore_ascii_case("extents")); + let scale = Some(page.scale).filter(|s| !page.scale_to_fit && s.is_finite() && *s > 0.0 && (*s - 1.0).abs() > 1e-9); + SavedPlot { window, fit, scale, center: page.center } +} diff --git a/crates/render/src/raster.rs b/crates/render/src/raster.rs index 72ac821d0..a32818a38 100644 --- a/crates/render/src/raster.rs +++ b/crates/render/src/raster.rs @@ -222,6 +222,37 @@ impl Hairlines { } } +/// Blank (white) everything outside `printable`, a rectangle in paper units with the origin at the +/// sheet's bottom-left, on a pixmap showing the whole sheet at `px_per_unit` pixels per paper unit. +/// A placed plot is clipped to the printable area this way, in the PNG plot and in the preview. +pub fn blank_outside_printable(pm: &mut Pixmap, printable: &Bounds2, px_per_unit: f64) { + let ph = f64::from(pm.height()); + blank_outside_pixels( + pm, + (printable.min.x * px_per_unit, ph - printable.max.y * px_per_unit), + (printable.max.x * px_per_unit, ph - printable.min.y * px_per_unit), + ); +} + +/// Blank (white) everything outside the pixel rectangle from `top_left` to `bottom_right` (image +/// coordinates, y down; rounded and clamped to the image). For a pixmap whose view does not map +/// the sheet onto the whole image, map the printable corners through `View::to_screen`. +pub fn blank_outside_pixels(pm: &mut Pixmap, top_left: (f64, f64), bottom_right: (f64, f64)) { + let (pw, ph) = (pm.width(), pm.height()); + let col = |x: f64| x.round().clamp(0.0, f64::from(pw)) as usize; + let row = |y: f64| y.round().clamp(0.0, f64::from(ph)) as usize; + let (x0, x1) = (col(top_left.0), col(bottom_right.0)); + let (y0, y1) = (row(top_left.1), row(bottom_right.1)); + let stride = pw as usize * 4; + for (r, line) in pm.data_mut().chunks_exact_mut(stride.max(4)).enumerate() { + for (c, px) in line.as_chunks_mut::<4>().0.iter_mut().enumerate() { + if r < y0 || r >= y1 || c < x0 || c >= x1 { + px.copy_from_slice(&[255, 255, 255, 255]); + } + } + } +} + /// Render and encode as PNG. pub fn render_png(list: &DisplayList, view: &View, o: &RasterOptions) -> Option> { render(list, view, o)?.encode_png().ok() From e8bcc38e34285c40c24ffc0513f81f7424a2c1f9 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Mon, 2 Nov 2026 02:40:15 +0700 Subject: [PATCH 039/100] OOPS: bring back the last erase, each object where it was (#2605) There was no OOPS. It now brings back what the last ERASE removed (the Delete key too), or BLOCK deleting the objects it makes a block of, as AutoCAD 2024 does (accoreconsole, the drawing's entnext order read back): - Each object keeps its handle and goes back directly above the object that was under it, or under the one that was above it when that is gone. An erase records both neighbours (EntityStore::neighbours). - Only the last erase counts; entdel doesn't. OOPS says nothing and is one undo step. - An object stays out when it is already in the drawing, when an undo took the drawing back to before it was made, when its layer, block or layout is gone, and, during REFEDIT, when it isn't in the working set. REFCLOSE forgets the working copies; the Block Editor has an OOPS of its own. - The command's label is in the 17 interface catalogs. Co-authored-by: Claude Opus 5.5 Co-authored-by: Brandon Thomas --- crates/doc/src/store.rs | 35 ++++ crates/engine/src/blockedit.rs | 4 +- crates/engine/src/cmd.rs | 1 + crates/engine/src/cmd/blocks.rs | 13 +- crates/engine/src/cmd/blocks/refedit.rs | 2 + crates/engine/src/cmd/edit.rs | 8 +- crates/engine/src/cmd/modify.rs | 4 + crates/engine/src/cmd/modify/oops.rs | 171 ++++++++++++++++ crates/engine/src/cmd/modify/transform.rs | 13 +- crates/engine/src/doc_state.rs | 3 + crates/engine/tests/it/main.rs | 1 + crates/engine/tests/it/oops_command.rs | 232 ++++++++++++++++++++++ crates/ui-egui/src/i18n/ar.tsv | 1 + crates/ui-egui/src/i18n/bs.tsv | 1 + crates/ui-egui/src/i18n/cs.tsv | 1 + crates/ui-egui/src/i18n/de.tsv | 1 + crates/ui-egui/src/i18n/el.tsv | 1 + crates/ui-egui/src/i18n/es.tsv | 1 + crates/ui-egui/src/i18n/fr.tsv | 1 + crates/ui-egui/src/i18n/hu.tsv | 1 + crates/ui-egui/src/i18n/it.tsv | 1 + crates/ui-egui/src/i18n/ko.tsv | 1 + crates/ui-egui/src/i18n/pt_br.tsv | 1 + crates/ui-egui/src/i18n/ru.tsv | 1 + crates/ui-egui/src/i18n/sv.tsv | 1 + crates/ui-egui/src/i18n/tr.tsv | 1 + crates/ui-egui/src/i18n/uk.tsv | 1 + crates/ui-egui/src/i18n/vi.tsv | 1 + crates/ui-egui/src/i18n/zh.tsv | 1 + 29 files changed, 484 insertions(+), 20 deletions(-) create mode 100644 crates/engine/src/cmd/modify/oops.rs create mode 100644 crates/engine/tests/it/oops_command.rs diff --git a/crates/doc/src/store.rs b/crates/doc/src/store.rs index 3e9b440e1..00b04970e 100644 --- a/crates/doc/src/store.rs +++ b/crates/doc/src/store.rs @@ -189,6 +189,19 @@ impl EntityStore { None => false, } } + /// The objects directly under and above `h` in the draw order (None at the bottom or the + /// top), when `h` is here. + pub fn neighbours(&self, h: Handle) -> Option<(Option, Option)> { + let pos = self.chunk_pos(self.chunk_id(h)?)?; + let items = &self.chunks.get(pos)?.items; + let i = items.iter().position(|e| e.handle == h)?; + let below = match i.checked_sub(1) { + Some(j) => items.get(j), + None => pos.checked_sub(1).and_then(|p| self.chunks.get(p)).and_then(|c| c.items.last()), + }; + let above = items.get(i + 1).or_else(|| self.chunks.get(pos + 1).and_then(|c| c.items.first())); + Some((below.map(|e| e.handle), above.map(|e| e.handle))) + } /// Move `h` directly above (`above`) or under `anchor` in the draw order. False (and nothing /// moves) when either is absent or they are the same object. pub fn move_next_to(&mut self, h: Handle, anchor: Handle, above: bool) -> bool { @@ -364,4 +377,26 @@ mod tests { assert_eq!(s.get(Handle(h)).map(|e| e.handle.0), Some(h)); } } + + #[test] + fn neighbours_across_chunks() { + let mut s = EntityStore::new(); + for h in 1..=3000 { + s.push(ent(h)); + } + assert_eq!(s.neighbours(Handle(1)), Some((None, Some(Handle(2))))); + assert_eq!(s.neighbours(Handle(1500)), Some((Some(Handle(1499)), Some(Handle(1501))))); + // The first and last objects of a chunk look into the chunks next to it. + let k = CHUNK_MAX as u64; + assert_eq!(s.neighbours(Handle(k)), Some((Some(Handle(k - 1)), Some(Handle(k + 1))))); + assert_eq!(s.neighbours(Handle(k + 1)), Some((Some(Handle(k)), Some(Handle(k + 2))))); + assert_eq!(s.neighbours(Handle(3000)), Some((Some(Handle(2999)), None))); + assert_eq!(s.neighbours(Handle(9999)), None); + assert!(s.move_next_to(Handle(3000), Handle(1), false)); + assert_eq!(s.neighbours(Handle(1)), Some((Some(Handle(3000)), Some(Handle(2))))); + assert_eq!(s.neighbours(Handle(3000)), Some((None, Some(Handle(1))))); + let mut one = EntityStore::new(); + one.push(ent(7)); + assert_eq!(one.neighbours(Handle(7)), Some((None, None))); + } } diff --git a/crates/engine/src/blockedit.rs b/crates/engine/src/blockedit.rs index fae52757e..89da207f0 100644 --- a/crates/engine/src/blockedit.rs +++ b/crates/engine/src/blockedit.rs @@ -268,11 +268,12 @@ impl DocState { } /// Start editing in the Block Editor `be` (the drawing is already the editor's): a fresh - /// undo history, nothing selected, model space. + /// undo history, nothing selected, nothing for OOPS, model space. fn enter_block_editor(&mut self, be: BlockEdit) { self.undo.clear(); self.redo.clear(); self.undo_ctl = Default::default(); + self.erased = Default::default(); self.selection.clear(); self.previous_selection.clear(); self.space = cadcraft_doc::Space::Model; @@ -338,6 +339,7 @@ impl DocState { self.undo = host.undo; self.redo = host.redo; self.undo_ctl = host.undo_ctl; + self.erased = host.erased; // The drawing comes back with nothing selected; Previous is what was selected before. self.selection.clear(); self.previous_selection = if host.selection.is_empty() { host.previous_selection } else { host.selection }; diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 172e416d1..bc93b9f55 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -25,6 +25,7 @@ mod lisp; mod mleader_content; mod mleader_edit; mod modify; +pub(crate) use modify::Erased; mod modify2; mod mtext_opts; mod overkill; diff --git a/crates/engine/src/cmd/blocks.rs b/crates/engine/src/cmd/blocks.rs index 81732a3f8..258594ff0 100644 --- a/crates/engine/src/cmd/blocks.rs +++ b/crates/engine/src/cmd/blocks.rs @@ -187,9 +187,16 @@ pub(crate) fn make_block(s: &mut Session, name: &str, base: Vec2, hs: &[Handle], b.entities.push(e); } doc.blocks.insert(name.to_string(), Arc::new(b)); - if keep != Keep::Retain { - for h in hs { - doc.remove_entity(*h); + match keep { + Keep::Retain => {} + Keep::Convert => { + for h in hs { + doc.remove_entity(*h); + } + } + // As after ERASE, OOPS brings them back. + Keep::Delete => { + super::modify::erase_kept(s, hs)?; } } s.set_selection(Vec::new()); diff --git a/crates/engine/src/cmd/blocks/refedit.rs b/crates/engine/src/cmd/blocks/refedit.rs index 0a61122a5..18d0d436b 100644 --- a/crates/engine/src/cmd/blocks/refedit.rs +++ b/crates/engine/src/cmd/blocks/refedit.rs @@ -281,6 +281,8 @@ fn close(s: &mut Session, save: bool) -> Result { } nd.end_refedit_discard(&r); *s.doc_mut()? = nd; + // The objects the edit managed were its own: OOPS doesn't bring them back after it. + s.state_mut()?.erased.forget(|h| r.owns(h)); s.set_selection(Vec::new()); Ok(json!({ "block": r.block, "saved": save, "objects": objects })) } diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 3a4c43e73..754248cd3 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -549,13 +549,7 @@ fn run_pasteorig(s: &mut Session, _p: &Value) -> Result { fn run_clear(s: &mut Session, _p: &Value) -> Result { let mut hs = s.selection(); hs.retain(|h| !super::curves::is_locked(s, *h)); - let d = s.doc_mut()?; - let mut n = 0; - for h in hs { - if d.remove_entity(h).is_some() { - n += 1; - } - } + let n = super::modify::erase_kept(s, &hs)?; s.set_selection(Vec::new()); Ok(json!({ "erased": n })) } diff --git a/crates/engine/src/cmd/modify.rs b/crates/engine/src/cmd/modify.rs index 22126f900..828ce1c01 100644 --- a/crates/engine/src/cmd/modify.rs +++ b/crates/engine/src/cmd/modify.rs @@ -20,6 +20,7 @@ mod break_join; mod explode; mod fillet; mod offset; +mod oops; mod select_then; mod stretch; mod transform; @@ -30,6 +31,8 @@ use explode::run_explode; pub(crate) use explode::{explode_kind, explode_with}; use fillet::{FilletM, run_chamfer, run_fillet}; use offset::{OffsetM, run_offset}; +use oops::run_oops; +pub(crate) use oops::{Erased, erase_kept}; use select_then::{DisplaceM, Op, SelectThen}; use stretch::{StretchM, run_stretch}; pub(crate) use transform::transform_entities; @@ -43,6 +46,7 @@ pub fn specs() -> Vec { .alias(&["e", "delete"]) .params("{handles?: [hex]} (default: selection)") .interactive(|_| Ok(Box::new(SelectThen::new(Op::Erase)))), + CommandSpec::new("oops", "Oops", run_oops).params("{} (brings back what the last ERASE or BLOCK removed)"), CommandSpec::new("move", "Move", run_move) .menu(&["Modify", "Move"]) .alias(&["m"]) diff --git a/crates/engine/src/cmd/modify/oops.rs b/crates/engine/src/cmd/modify/oops.rs new file mode 100644 index 000000000..217f57ae6 --- /dev/null +++ b/crates/engine/src/cmd/modify/oops.rs @@ -0,0 +1,171 @@ +//! OOPS: bring back the objects the last ERASE (or the Delete key) removed, or BLOCK deleting the +//! objects it makes a block of, with their handles and each where it was in the draw order, as +//! AutoCAD 2024 does (accoreconsole, the drawing's `entnext` order read back afterwards). + +use std::collections::{HashMap, HashSet}; +use std::sync::Arc; + +use cadcraft_doc::{Drawing, Entity, EntityKind, EntityStore, Handle, Space}; +use serde_json::{Value, json}; + +use crate::{Result, Session}; + +/// One erased object, with the objects that were directly under and above it. +#[derive(Clone, Debug)] +struct Taken { + space: Space, + entity: Arc, + below: Option, + above: Option, +} + +/// What the drawing's last erase removed, for OOPS. +#[derive(Clone, Debug, Default)] +pub(crate) struct Erased(Vec); + +impl Erased { + /// Never bring back the objects `drop` picks. + pub(crate) fn forget(&mut self, drop: impl Fn(Handle) -> bool) { + self.0.retain(|t| !drop(t.entity.handle)); + } +} + +/// Remove the objects `hs` and keep them for OOPS in place of what an earlier erase kept. +/// Returns how many went; when none did, OOPS keeps what it had. +pub(crate) fn erase_kept(s: &mut Session, hs: &[Handle]) -> Result { + let d = s.doc_mut()?; + // Every neighbour is read before anything goes: objects erased together may be neighbours. + let at: Vec<(Handle, Space, Option, Option)> = hs + .iter() + .filter_map(|&h| { + let space = d.space_of(h)?; + let (below, above) = d.space(&space)?.neighbours(h)?; + Some((h, space, below, above)) + }) + .collect(); + let mut taken = Vec::with_capacity(at.len()); + for (h, space, below, above) in at { + if let Some(entity) = d.space_mut(&space).and_then(|st| st.remove(h)) { + taken.push(Taken { space, entity, below, above }); + } + } + let n = taken.len(); + if n > 0 { + s.state_mut()?.erased = Erased(taken); + } + Ok(n) +} + +pub(super) fn run_oops(s: &mut Session, _p: &Value) -> Result { + let taken = s.state()?.erased.0.clone(); + let back: Vec = { + let d = s.doc()?; + let in_blocks = in_blocks(d, &taken); + taken.iter().map(|t| !in_blocks.contains(&t.entity.handle) && can_return(d, t)).collect() + }; + // Nothing to bring back leaves the drawing as it is: no undo step. + if !back.contains(&true) { + return Ok(json!({ "restored": 0 })); + } + let n = restore(s.doc_mut()?, &taken, &back); + Ok(json!({ "restored": n })) +} + +/// The handles of `taken` that a block's object has now (an undo can take the drawing back to +/// before an erased object was made, and a block's new object take its handle). +fn in_blocks(d: &Drawing, taken: &[Taken]) -> HashSet { + let hs: HashSet = taken.iter().map(|t| t.entity.handle).collect(); + let mut out = HashSet::new(); + for b in d.blocks.values() { + // Through whichever is smaller. + if b.entities.len() < hs.len() { + out.extend(b.entities.iter().map(|e| e.handle).filter(|h| hs.contains(h))); + } else { + out.extend(hs.iter().copied().filter(|h| b.entities.contains(*h))); + } + } + out +} + +/// Whether `t` can come back: it is not in the drawing (OOPS again, or an undo brought it back), +/// its handle is one the drawing has given out, a reference being edited in place (REFEDIT) +/// doesn't keep it out, and its layout, layer and block are still there. +fn can_return(d: &Drawing, t: &Taken) -> bool { + let h = t.entity.handle; + h.0 < d.handseed + && d.entity(h).is_none() + && !crate::cmd::blocks::refedit::locked(d, h) + && d.space(&t.space).is_some() + && d.layer(&t.entity.common.layer).is_some() + && match &t.entity.kind { + EntityKind::Insert(i) => d.block(&i.block).is_some(), + _ => true, + } +} + +/// Put back the objects `back` marks. Each run of objects that were neighbours comes back from +/// its bottom one up, so each goes directly above the one that was under it. +fn restore(d: &mut Drawing, taken: &[Taken], back: &[bool]) -> usize { + let index: HashMap = taken.iter().enumerate().map(|(i, t)| (t.entity.handle, i)).collect(); + let next: HashMap = taken.iter().enumerate().filter_map(|(i, t)| Some((t.below.filter(|b| index.contains_key(b))?, i))).collect(); + let mut done = vec![false; taken.len()]; + let mut n = 0; + for first in 0..taken.len() { + if taken[first].below.is_some_and(|b| index.contains_key(&b)) { + continue; + } + let mut at = Some(first); + while let Some(i) = at { + if std::mem::replace(&mut done[i], true) { + break; + } + if back[i] && put_back(d, taken, &index, i) { + n += 1; + } + at = next.get(&taken[i].entity.handle).copied(); + } + } + n +} + +/// Put `taken[i]` back directly above the nearest object that was under it and is here; when +/// those are gone, directly under the nearest one that was above it; else on top. +fn put_back(d: &mut Drawing, taken: &[Taken], index: &HashMap, i: usize) -> bool { + let t = &taken[i]; + let Some(store) = d.space_mut(&t.space) else { return false }; + let h = t.entity.handle; + store.push(Entity::clone(&t.entity)); + match nearest(store, taken, index, t.below, |t| t.below) { + Some(Some(b)) => { + store.move_next_to(h, b, true); + } + Some(None) => { + store.send_to_back(h); + } + None => { + if let Some(Some(a)) = nearest(store, taken, index, t.above, |t| t.above) { + store.move_next_to(h, a, false); + } + } + } + true +} + +/// From `h`, past objects erased with it that are not here, to the first object that is +/// (`Some(Some)`), the end of the draw order (`Some(None)`), or an object that is gone (`None`). +fn nearest( + store: &EntityStore, + taken: &[Taken], + index: &HashMap, + mut h: Option, + step: fn(&Taken) -> Option, +) -> Option> { + for _ in 0..=taken.len() { + let Some(x) = h else { return Some(None) }; + if store.contains(x) { + return Some(Some(x)); + } + h = step(taken.get(*index.get(&x)?)?); + } + None +} diff --git a/crates/engine/src/cmd/modify/transform.rs b/crates/engine/src/cmd/modify/transform.rs index 9adc54ec1..11e52ea23 100644 --- a/crates/engine/src/cmd/modify/transform.rs +++ b/crates/engine/src/cmd/modify/transform.rs @@ -1,6 +1,6 @@ use cadcraft_geom::Vec3; -use super::{Entity, Handle, Mat3, Result, Session, Value, Vec2, bad, bool_or, f64_req, json, point_param, point_req, targets}; +use super::{Entity, Handle, Mat3, Result, Session, Value, Vec2, bad, bool_or, erase_kept, f64_req, json, point_param, point_req, targets}; // ---------------- core operations ---------------- @@ -54,14 +54,9 @@ fn edit_entities(s: &mut Session, hs: &[Handle], copy: bool, edit: impl Fn(&mut } pub(super) fn erase(s: &mut Session, hs: &[Handle]) -> Result { - let d = s.doc_mut()?; - let mut n = 0; - for h in hs { - let locked = d.entity(*h).and_then(|e| d.layer(&e.common.layer)).is_some_and(|l| l.locked); - if !locked && d.remove_entity(*h).is_some() { - n += 1; - } - } + let d = s.doc()?; + let free: Vec = hs.iter().copied().filter(|h| !d.entity(*h).and_then(|e| d.layer(&e.common.layer)).is_some_and(|l| l.locked)).collect(); + let n = erase_kept(s, &free)?; s.set_selection(Vec::new()); Ok(n) } diff --git a/crates/engine/src/doc_state.rs b/crates/engine/src/doc_state.rs index 97a1ede49..31d23a94e 100644 --- a/crates/engine/src/doc_state.rs +++ b/crates/engine/src/doc_state.rs @@ -35,6 +35,8 @@ pub struct DocState { /// The Block Editor (BEDIT), when it is open: `doc` is then the editor's drawing, and the /// drawing itself waits in it ([`DocState::drawing`]). pub block_edit: Option, + /// What the last erase removed, for OOPS. + pub(crate) erased: crate::cmd::Erased, /// The fonts missing on Windows that a save last named: the next save names them again only /// when they change. pub(crate) fonts_noted: String, @@ -64,6 +66,7 @@ impl DocState { uid: NEXT_UID.fetch_add(1, std::sync::atomic::Ordering::Relaxed), binary_dxf: false, block_edit: None, + erased: Default::default(), fonts_noted: String::new(), } } diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 65b8e244f..f49d7bb7e 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -93,6 +93,7 @@ mod non_finite_edits; mod offset_closed_spline; mod offset_ellipse_side; mod offset_move; +mod oops_command; mod options_profile; mod overkill; mod parallel_display_list; diff --git a/crates/engine/tests/it/oops_command.rs b/crates/engine/tests/it/oops_command.rs new file mode 100644 index 000000000..465484e0f --- /dev/null +++ b/crates/engine/tests/it/oops_command.rs @@ -0,0 +1,232 @@ +//! OOPS, as AutoCAD 2024 runs it (accoreconsole: a line A, a line B, a circle C and a line D, these +//! scripts, then the drawing's `entnext` order read back). + +use cadcraft_engine::Session; +use cadcraft_engine::cmd::file::{EntityCodec, set_entity_codec}; +use cadcraft_engine::doc::{Common, EntityKind, Handle, Line, Space}; +use cadcraft_engine::geom::Vec3; +use serde_json::{Value, json}; + +/// A session that `entmake` works in. +fn session() -> Session { + set_entity_codec(EntityCodec { groups: cadcraft_io::entity_groups, entity: cadcraft_io::entity_from_groups }); + Session::new() +} + +/// A session with A, B, C and D in draw order, and their handles. +fn drawing() -> (Session, Vec) { + let mut s = session(); + s.execute("line", &json!({ "points": [[0, 0], [10, 0]] })).unwrap(); + s.execute("line", &json!({ "points": [[0, 10], [10, 10]] })).unwrap(); + s.execute("circle", &json!({ "center": [5, 25], "radius": 3 })).unwrap(); + s.execute("line", &json!({ "points": [[0, 40], [10, 40]] })).unwrap(); + let hs = handles(&s); + (s, hs) +} + +fn handles(s: &Session) -> Vec { + s.doc().unwrap().model.iter().map(|e| e.handle).collect() +} + +/// The drawing's objects in draw order: A to D by their handles, E for any other. +fn order(s: &Session, hs: &[Handle]) -> String { + let name = |h: &Handle| hs.iter().position(|x| x == h).map_or('E', |i| (b'A' + i as u8) as char); + handles(s).iter().map(|h| name(h).to_string()).collect::>().join(" ") +} + +/// Run `script` with `@A`…`@D` standing for the objects. +fn run(s: &mut Session, hs: &[Handle], script: &[&str]) { + for line in script { + let mut line = line.to_string(); + for (i, h) in hs.iter().enumerate() { + line = line.replace(&format!("@{}", (b'A' + i as u8) as char), &format!("(handent \"{}\")", h.hex())); + } + s.cmdline(&line).unwrap(); + assert!(s.running.is_none(), "{line}: {}", s.prompt_text()); + } +} + +fn oops(s: &mut Session) -> Value { + s.execute("oops", &json!({})).unwrap() +} + +#[test] +fn the_last_erase_comes_back_where_it_was() { + const LINE: &str = "(command \"_.LINE\" '(0.0 50.0) '(10.0 50.0) \"\")"; + const OOPS: &str = "(command \"_.OOPS\")"; + // (case, script, the drawing's order afterwards: E is the line drawn after the erase) + let cases: [(&str, &[&str], &str); 14] = [ + ("OOPS-000: nothing erased yet", &[OOPS], "A B C D"), + ("OOPS-001: two in the middle", &["(command \"_.ERASE\" @B @C \"\")", OOPS], "A B C D"), + ("OOPS-002: picked in reverse", &["(command \"_.ERASE\" @C @B \"\")", OOPS], "A B C D"), + ("OOPS-003: a new object after the erase", &["(command \"_.ERASE\" @B \"\")", LINE, OOPS], "A B C D E"), + ("OOPS-004: the bottom object", &["(command \"_.ERASE\" @A \"\")", LINE, OOPS], "A B C D E"), + ("OOPS-005: twice", &["(command \"_.ERASE\" @B \"\")", OOPS, OOPS], "A B C D"), + ("OOPS-006: after U", &["(command \"_.ERASE\" @B \"\")", "U", OOPS], "A B C D"), + ("OOPS-007: the last erase only", &["(command \"_.ERASE\" @B \"\")", "(command \"_.ERASE\" @C \"\")", OOPS], "A C D"), + ("OOPS-008: U after OOPS", &["(command \"_.ERASE\" @B \"\")", OOPS, "U"], "A C D"), + ("OOPS-009: entdel", &["(entdel @B)", OOPS], "A C D"), + ("OOPS-010: -BLOCK", &["(command \"_.-BLOCK\" \"BKO\" '(0.0 0.0) @A @C \"\")", OOPS], "A B C D"), + ("OOPS-011: U of another command", &["(command \"_.ERASE\" @B \"\")", LINE, "U", OOPS], "A B C D"), + ("OOPS-012: the object under it erased since", &["(command \"_.ERASE\" @B \"\")", "(entdel @A)", OOPS], "B C D"), + ("OOPS-013: everything", &["(command \"_.ERASE\" \"_ALL\" \"\")", OOPS], "A B C D"), + ]; + for (case, script, want) in cases { + let (mut s, hs) = drawing(); + run(&mut s, &hs, script); + assert_eq!(order(&s, &hs), want, "{case}"); + } +} + +#[test] +fn oops_says_nothing_and_counts_what_came_back() { + let (mut s, hs) = drawing(); + s.cmdline("oops").unwrap(); + assert_eq!(s.log.last().map(String::as_str), Some("Command: OOPS")); + s.execute("erase", &json!({ "handles": [hs[1].hex(), hs[3].hex()] })).unwrap(); + s.cmdline("oops").unwrap(); + assert_eq!(s.log.last().map(String::as_str), Some("Command: OOPS")); + assert_eq!(order(&s, &hs), "A B C D"); + assert_eq!(oops(&mut s), json!({ "restored": 0 })); + s.execute("u", &json!({})).unwrap(); + assert_eq!(order(&s, &hs), "A C"); + assert_eq!(oops(&mut s), json!({ "restored": 2 })); + // They come back as they were. + let d = s.doc().unwrap(); + assert!(matches!(d.entity(hs[3]).map(|e| &e.kind), Some(EntityKind::Line(l)) if l.a.y == 40.0 && l.b.x == 10.0)); +} + +#[test] +fn the_delete_key_and_block_feed_oops_too() { + // Edit > Clear, the Delete key. + let (mut s, hs) = drawing(); + s.set_selection(vec![hs[2]]); + s.execute("erase.selection", &json!({})).unwrap(); + assert_eq!(order(&s, &hs), "A B D"); + assert_eq!(oops(&mut s), json!({ "restored": 1 })); + assert_eq!(order(&s, &hs), "A B C D"); + // BLOCK deleting its objects. + let (mut s, hs) = drawing(); + s.execute("block", &json!({ "name": "BK", "base": [0, 0], "handles": [hs[0].hex(), hs[3].hex()], "keep": "delete" })).unwrap(); + assert_eq!(order(&s, &hs), "B C"); + assert_eq!(oops(&mut s), json!({ "restored": 2 })); + assert_eq!(order(&s, &hs), "A B C D"); +} + +#[test] +fn an_object_an_undo_took_away_stays_away() { + // U past the line's creation: its handle is free again. + let mut s = session(); + s.execute("line", &json!({ "points": [[0, 0], [10, 0]] })).unwrap(); + let x = handles(&s)[0]; + s.execute("erase", &json!({ "handles": [x.hex()] })).unwrap(); + s.execute("u", &json!({})).unwrap(); + s.execute("u", &json!({})).unwrap(); + assert!(handles(&s).is_empty()); + assert_eq!(oops(&mut s), json!({ "restored": 0 })); + assert!(handles(&s).is_empty()); + // A new object takes the handle. + s.execute("circle", &json!({ "center": [0, 0], "radius": 1 })).unwrap(); + assert_eq!(handles(&s), [x]); + assert_eq!(oops(&mut s), json!({ "restored": 0 })); + assert!(matches!(s.doc().unwrap().entity(x).map(|e| &e.kind), Some(EntityKind::Circle(_)))); + // A block's object takes the handle: one erased object of two, one of a block's two. + for (erased, in_block) in [(2, 1), (1, 2)] { + let mut s = session(); + for y in 0..erased { + s.execute("line", &json!({ "points": [[0, y], [10, y]] })).unwrap(); + } + let hs = handles(&s); + s.execute("erase", &json!({ "handles": hs.iter().map(|h| h.hex()).collect::>() })).unwrap(); + for _ in 0..=erased { + s.execute("u", &json!({})).unwrap(); + } + assert!(handles(&s).is_empty()); + s.cmdline("(entmake '((0 . \"BLOCK\") (2 . \"BX\") (10 0.0 0.0 0.0) (70 . 0)))").unwrap(); + for _ in 0..in_block { + s.cmdline("(entmake '((0 . \"LINE\") (8 . \"0\") (10 0.0 0.0 0.0) (11 1.0 0.0 0.0)))").unwrap(); + } + s.cmdline("(entmake '((0 . \"ENDBLK\")))").unwrap(); + assert!(s.doc().unwrap().block("BX").is_some_and(|b| b.entities.contains(hs[0])), "{erased} {in_block}"); + assert_eq!(oops(&mut s), json!({ "restored": 0 }), "{erased} {in_block}"); + assert!(handles(&s).is_empty()); + } +} + +#[test] +fn an_object_whose_layer_or_block_is_gone_stays_away() { + // Its layer purged. + let (mut s, hs) = drawing(); + run(&mut s, &hs, &["(command \"_.-LAYER\" \"_M\" \"L1\" \"\")"]); + s.execute("line", &json!({ "points": [[0, 60], [10, 60]] })).unwrap(); + let on_l1 = *handles(&s).last().unwrap(); + run(&mut s, &hs, &["(setvar \"CLAYER\" \"0\")"]); + s.execute("erase", &json!({ "handles": [on_l1.hex()] })).unwrap(); + s.execute("purge", &json!({ "type": "layers", "names": "L1" })).unwrap(); + assert!(s.doc().unwrap().layer("L1").is_none()); + assert_eq!(oops(&mut s), json!({ "restored": 0 })); + // Its block purged. + let (mut s, hs) = drawing(); + s.execute("block", &json!({ "name": "BK", "base": [0, 0], "handles": [hs[0].hex()], "keep": "retain" })).unwrap(); + s.execute("insert", &json!({ "name": "BK", "at": [50, 0] })).unwrap(); + let ins = *handles(&s).last().unwrap(); + s.execute("erase", &json!({ "handles": [ins.hex()] })).unwrap(); + s.execute("purge", &json!({ "type": "blocks", "names": "BK" })).unwrap(); + assert!(s.doc().unwrap().block("BK").is_none()); + assert_eq!(oops(&mut s), json!({ "restored": 0 })); + assert_eq!(order(&s, &hs), "A B C D"); +} + +#[test] +fn the_block_editor_has_an_oops_of_its_own() { + let (mut s, hs) = drawing(); + s.execute("block", &json!({ "name": "BK", "base": [0, 0], "handles": [hs[0].hex()], "keep": "retain" })).unwrap(); + s.execute("erase", &json!({ "handles": [hs[1].hex()] })).unwrap(); + s.execute("bedit", &json!({ "name": "BK" })).unwrap(); + // The drawing's erased line doesn't come back into the block. + assert_eq!(oops(&mut s), json!({ "restored": 0 })); + s.execute("bclose", &json!({ "save": false })).unwrap(); + // Back in the drawing, it does. + assert_eq!(oops(&mut s), json!({ "restored": 1 })); + assert_eq!(order(&s, &hs), "A B C D"); +} + +#[test] +fn a_layouts_objects_come_back_to_it() { + let mut s = session(); + let paper = Space::Paper("Layout1".into()); + let line = |y: f64| EntityKind::Line(Line { a: Vec3::new(0.0, y, 0.0), b: Vec3::new(10.0, y, 0.0) }); + let hs: Vec = (0..3).map(|y| s.doc_mut().unwrap().add(&paper, Common::default(), line(f64::from(y))).unwrap()).collect(); + s.execute("erase", &json!({ "handles": [hs[1].hex()] })).unwrap(); + assert_eq!(oops(&mut s), json!({ "restored": 1 })); + let d = s.doc().unwrap(); + assert!(d.model.is_empty()); + assert_eq!(d.space(&paper).unwrap().handles(), hs); +} + +#[test] +fn a_reference_edited_in_place_keeps_its_objects_and_the_drawings_apart() { + let (mut s, hs) = drawing(); + s.execute("block", &json!({ "name": "BK", "base": [0, 0], "handles": [hs[0].hex(), hs[1].hex()], "keep": "convert" })).unwrap(); + let ins = *handles(&s).last().unwrap(); + s.execute("erase", &json!({ "handles": [hs[3].hex()] })).unwrap(); + let r = s.execute("refedit", &json!({ "handle": ins.hex() })).unwrap(); + // The drawing's line stays out while the reference is edited... + assert_eq!(oops(&mut s), json!({ "restored": 0 })); + // ...and a working copy comes back. + let copy = r["working"][0].as_str().unwrap().to_string(); + s.execute("erase", &json!({ "handles": [copy] })).unwrap(); + assert_eq!(oops(&mut s), json!({ "restored": 1 })); + // Once the edit is over, its copies don't come back into the drawing. + s.execute("erase", &json!({ "handles": [copy] })).unwrap(); + s.execute("refclose", &json!({ "save": true })).unwrap(); + assert_eq!(s.doc().unwrap().block("BK").unwrap().entities.len(), 1); + assert_eq!(oops(&mut s), json!({ "restored": 0 })); + assert_eq!(order(&s, &hs), "C E"); + // The drawing's own erased line does, after an edit that ends. + s.execute("erase", &json!({ "handles": [hs[2].hex()] })).unwrap(); + s.execute("refedit", &json!({ "handle": ins.hex() })).unwrap(); + s.execute("refclose", &json!({ "save": false })).unwrap(); + assert_eq!(oops(&mut s), json!({ "restored": 1 })); + assert_eq!(order(&s, &hs), "C E"); +} diff --git a/crates/ui-egui/src/i18n/ar.tsv b/crates/ui-egui/src/i18n/ar.tsv index 026190729..ccc9b72d5 100644 --- a/crates/ui-egui/src/i18n/ar.tsv +++ b/crates/ui-egui/src/i18n/ar.tsv @@ -643,6 +643,7 @@ One of the Crafting Apps by the ArtCraft team and community. أحد تطبيقات Crafting من فريق ArtCraft والمجتمع. Only in this drawing: {n} في هذا الرسم فقط: {n} Only in {name}: {n} في {name} فقط: {n} + Oops استعادة الكائنات الممحوة Open فتح Open (⌘O) فتح (⌘O) Open Sample Drawing فتح رسم نموذجي diff --git a/crates/ui-egui/src/i18n/bs.tsv b/crates/ui-egui/src/i18n/bs.tsv index 14565ce2f..460959a33 100644 --- a/crates/ui-egui/src/i18n/bs.tsv +++ b/crates/ui-egui/src/i18n/bs.tsv @@ -585,6 +585,7 @@ One of the Crafting Apps by the ArtCraft team and community. Jedna od Crafting aplikacija ArtCraft tima i zajednice. Only in this drawing: {n} Samo u ovom crtežu: {n} Only in {name}: {n} Samo u {name}: {n} + Oops Vrati obrisane objekte Open (⌘O) Otvori (⌘O) Open Sample Drawing Otvori primjer crteža Open sample drawing Otvori primjer crteža diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsv index c979cd786..2419bf41f 100644 --- a/crates/ui-egui/src/i18n/cs.tsv +++ b/crates/ui-egui/src/i18n/cs.tsv @@ -583,6 +583,7 @@ One of the Crafting Apps by the ArtCraft team and community. Jedna z aplikací Crafting Apps od týmu ArtCraft a komunity. Only in this drawing: {n} Pouze v tomto výkresu: {n} Only in {name}: {n} Pouze v {name}: {n} + Oops Obnovit vymazané objekty Open (⌘O) Otevřít (⌘O) Open Sample Drawing Otevřít ukázkový výkres Open sample drawing Otevřít ukázku výkresu diff --git a/crates/ui-egui/src/i18n/de.tsv b/crates/ui-egui/src/i18n/de.tsv index 84b944529..2186fc743 100644 --- a/crates/ui-egui/src/i18n/de.tsv +++ b/crates/ui-egui/src/i18n/de.tsv @@ -495,6 +495,7 @@ On Ein On/Off Ein/Aus One of the Crafting Apps by the ArtCraft team and community. Eine der Crafting Apps vom ArtCraft-Team und seiner Community. + Oops Gelöschte Objekte wiederherstellen Open (⌘O) Öffnen (⌘O) Open Sample Drawing Beispielzeichnung öffnen Open sample drawing Beispielzeichnung öffnen diff --git a/crates/ui-egui/src/i18n/el.tsv b/crates/ui-egui/src/i18n/el.tsv index 2825e8fd2..16fcecfeb 100644 --- a/crates/ui-egui/src/i18n/el.tsv +++ b/crates/ui-egui/src/i18n/el.tsv @@ -702,6 +702,7 @@ One of the Crafting Apps by the ArtCraft team and community. Μία από τις εφαρμογές Crafting Apps της ομάδας και της κοινότητας ArtCraft. Only in this drawing: {n} Μόνο σε αυτό το σχέδιο: {n} Only in {name}: {n} Μόνο στο {name}: {n} + Oops Επαναφορά διαγραμμένων αντικειμένων Open Άνοιγμα Open (⌘O) Άνοιγμα (⌘O) Open a drawing first. Ανοίξτε πρώτα ένα σχέδιο. diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsv index b24970979..8c45b7f97 100644 --- a/crates/ui-egui/src/i18n/es.tsv +++ b/crates/ui-egui/src/i18n/es.tsv @@ -620,6 +620,7 @@ One of the Crafting Apps by the ArtCraft team and community. Una de las Crafting Apps del equipo y la comunidad de ArtCraft. Only in this drawing: {n} Solo en este dibujo: {n} Only in {name}: {n} Solo en {name}: {n} + Oops Restaurar objetos borrados Open Abrir Open (⌘O) Abrir (⌘O) Open Sample Drawing Abrir dibujo de ejemplo diff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsv index 42ad7ee48..548ed6a66 100644 --- a/crates/ui-egui/src/i18n/fr.tsv +++ b/crates/ui-egui/src/i18n/fr.tsv @@ -490,6 +490,7 @@ On Activé On/Off Activer/Désactiver One of the Crafting Apps by the ArtCraft team and community. L'une des applications Crafting Apps de l'équipe et de la communauté ArtCraft. + Oops Restaurer les objets effacés Open (⌘O) Ouvrir (⌘O) Open Sample Drawing Ouvrir un dessin d'exemple Open sample drawing Ouvrir un dessin d'exemple diff --git a/crates/ui-egui/src/i18n/hu.tsv b/crates/ui-egui/src/i18n/hu.tsv index fc27bd7ce..926ad0e6c 100644 --- a/crates/ui-egui/src/i18n/hu.tsv +++ b/crates/ui-egui/src/i18n/hu.tsv @@ -711,6 +711,7 @@ One of the Crafting Apps by the ArtCraft team and community. Az ArtCraft csapata és közössége által készített Craft-alkalmazások egyike. Only in this drawing: {n} Csak ebben a rajzban: {n} Only in {name}: {n} Csak ebben: {name} – {n} + Oops Törölt objektumok visszaállítása Open Megnyitás Open (⌘O) Megnyitás (⌘O) Open Recent Legutóbbi megnyitása diff --git a/crates/ui-egui/src/i18n/it.tsv b/crates/ui-egui/src/i18n/it.tsv index 634249093..3287ec077 100644 --- a/crates/ui-egui/src/i18n/it.tsv +++ b/crates/ui-egui/src/i18n/it.tsv @@ -594,6 +594,7 @@ menu Modify Edita One of the Crafting Apps by the ArtCraft team and community. Una delle Crafting App del team e della comunità ArtCraft. Only in this drawing: {n} Solo in questo disegno: {n} Only in {name}: {n} Solo in {name}: {n} + Oops Ripristina oggetti cancellati Open (⌘O) Apri (⌘O) Open Sample Drawing Apri disegno di esempio Open sample drawing Apri disegno di esempio diff --git a/crates/ui-egui/src/i18n/ko.tsv b/crates/ui-egui/src/i18n/ko.tsv index 1bd7e0fd5..f41128ba1 100644 --- a/crates/ui-egui/src/i18n/ko.tsv +++ b/crates/ui-egui/src/i18n/ko.tsv @@ -585,6 +585,7 @@ One of the Crafting Apps by the ArtCraft team and community. ArtCraft 팀과 커뮤니티가 만드는 Crafting Apps 제품군의 하나입니다. Only in this drawing: {n} 이 도면에만 있음: {n}개 Only in {name}: {n} {name}에만 있음: {n}개 + Oops 지운 객체 복원 Open (⌘O) 열기(⌘O) Open Sample Drawing 예제 도면 열기 Open sample drawing 예제 도면 열기 diff --git a/crates/ui-egui/src/i18n/pt_br.tsv b/crates/ui-egui/src/i18n/pt_br.tsv index 8f6533d51..3e16b65cf 100644 --- a/crates/ui-egui/src/i18n/pt_br.tsv +++ b/crates/ui-egui/src/i18n/pt_br.tsv @@ -616,6 +616,7 @@ One of the Crafting Apps by the ArtCraft team and community. Um dos Crafting Apps da equipe e da comunidade ArtCraft. Only in this drawing: {n} Somente neste desenho: {n} Only in {name}: {n} Somente em {name}: {n} + Oops Restaurar objetos apagados Open Abrir Open (⌘O) Abrir (⌘O) Open Sample Drawing Abrir desenho de exemplo diff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsv index b8f97bf73..7e4ba21d5 100644 --- a/crates/ui-egui/src/i18n/ru.tsv +++ b/crates/ui-egui/src/i18n/ru.tsv @@ -490,6 +490,7 @@ On Включено On/Off Включить/выключить One of the Crafting Apps by the ArtCraft team and community. Одна из программ Crafting Apps от команды и сообщества ArtCraft. + Oops Восстановить стертые объекты Open (⌘O) Открыть (⌘O) Open Sample Drawing Открыть образец чертежа Open sample drawing Открыть образец чертежа diff --git a/crates/ui-egui/src/i18n/sv.tsv b/crates/ui-egui/src/i18n/sv.tsv index 6bea4c3da..96114f857 100644 --- a/crates/ui-egui/src/i18n/sv.tsv +++ b/crates/ui-egui/src/i18n/sv.tsv @@ -619,6 +619,7 @@ One of the Crafting Apps by the ArtCraft team and community. En av Crafting Apps, gjord av ArtCraft-teamet och dess community. Only in this drawing: {n} Bara i den här ritningen: {n} Only in {name}: {n} Bara i {name}: {n} + Oops Återställ raderade objekt Open Öppna Open (⌘O) Öppna (⌘O) Open Sample Drawing Öppna exempelritning diff --git a/crates/ui-egui/src/i18n/tr.tsv b/crates/ui-egui/src/i18n/tr.tsv index 2bf979f85..3fed6bc3b 100644 --- a/crates/ui-egui/src/i18n/tr.tsv +++ b/crates/ui-egui/src/i18n/tr.tsv @@ -712,6 +712,7 @@ One of the Crafting Apps by the ArtCraft team and community. ArtCraft ekibi ve topluluğunun Crafting Apps uygulamalarından biri. Only in this drawing: {n} Yalnızca bu çizimde: {n} Only in {name}: {n} Yalnızca {name} içinde: {n} + Oops Silinen Nesneleri Geri Yükle Open Aç Open (⌘O) Aç (⌘O) Open Recent Son Kullanılanları Aç diff --git a/crates/ui-egui/src/i18n/uk.tsv b/crates/ui-egui/src/i18n/uk.tsv index 714ce6c4c..bbb70e489 100644 --- a/crates/ui-egui/src/i18n/uk.tsv +++ b/crates/ui-egui/src/i18n/uk.tsv @@ -598,6 +598,7 @@ One of the Crafting Apps by the ArtCraft team and community. Одна з програм Crafting Apps від команди та спільноти ArtCraft. Only in this drawing: {n} Лише в цьому кресленні: {n} Only in {name}: {n} Лише в {name}: {n} + Oops Відновити стерті об'єкти Open (⌘O) Відкрити (⌘O) Open Sample Drawing Відкрити зразок креслення Open sample drawing Відкрити зразок креслення diff --git a/crates/ui-egui/src/i18n/vi.tsv b/crates/ui-egui/src/i18n/vi.tsv index 9b1493031..8887bb45b 100644 --- a/crates/ui-egui/src/i18n/vi.tsv +++ b/crates/ui-egui/src/i18n/vi.tsv @@ -584,6 +584,7 @@ One of the Crafting Apps by the ArtCraft team and community. Một trong các ứng dụng Crafting của nhóm và cộng đồng ArtCraft. Only in this drawing: {n} Chỉ có trong bản vẽ này: {n} Only in {name}: {n} Chỉ có trong {name}: {n} + Oops Khôi phục đối tượng đã xóa Open (⌘O) Mở (⌘O) Open Sample Drawing Mở bản vẽ mẫu Open sample drawing Mở bản vẽ mẫu diff --git a/crates/ui-egui/src/i18n/zh.tsv b/crates/ui-egui/src/i18n/zh.tsv index 2663105a2..21c86d1fe 100644 --- a/crates/ui-egui/src/i18n/zh.tsv +++ b/crates/ui-egui/src/i18n/zh.tsv @@ -489,6 +489,7 @@ On 开启 On/Off 开/关 One of the Crafting Apps by the ArtCraft team and community. 由 ArtCraft 团队和社区打造的 Crafting 系列应用之一。 + Oops 恢复已擦除对象 Open (⌘O) 打开 (⌘O) Open Sample Drawing 打开示例图形 Open sample drawing 打开示例图形 From 2034d6899eba72363028723ec75d4c36eacfceac Mon Sep 17 00:00:00 2001 From: Andrea Ficarra Date: Sun, 1 Nov 2026 20:40:30 +0100 Subject: [PATCH 040/100] Polyface meshes stay one object through DXF and DWG (#2600) * Polyface meshes stay one object through DXF and DWG A polyface mesh (POLYLINE flag 64) was read as loose 3D faces. It is now read, drawn, exploded into its 3D faces and written back as a POLYLINE. Hidden edges aren't drawn. A mesh too large for the 16-bit groups 71-74 is saved as 3D faces. Split from #1005. Co-Authored-By: Claude Opus 5.5 * A polyface face ends at its first zero index A face record with a gap ([1, 2, 0, 3]) has two corners and is dropped, not read as a triangle. Co-Authored-By: Claude Opus 5.5 * Polyface meshes: write only faces that name their vertices; ID gives their Z The writer drops the faces the reader would drop, so no record names a missing vertex, and the 16-bit check only needs the counts. ID and DIST give a mesh the Z of its nearest visible edge, as they do for 3D faces. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Brandon Thomas --- crates/doc/src/entity.rs | 16 ++- crates/doc/src/entity/curves.rs | 46 +++++++ crates/doc/tests/it/face3d_hidden_edges.rs | 19 +++ crates/doc/tests/it/main.rs | 1 + crates/engine/src/cmd/inquiry.rs | 8 ++ crates/engine/src/cmd/modify/explode.rs | 6 + crates/engine/src/cmd/modify2/selop.rs | 1 + crates/engine/src/grips.rs | 1 + crates/engine/tests/it/inquiry_z.rs | 12 +- crates/engine/tests/it/main.rs | 1 + crates/engine/tests/it/polyface_explode.rs | 32 +++++ crates/engine/tests/it/scale_offset_z.rs | 13 +- crates/io/src/dxf_read.rs | 2 +- crates/io/src/dxf_read/document.rs | 2 +- crates/io/src/dxf_read/entities.rs | 48 +++----- crates/io/src/dxf_write.rs | 6 +- crates/io/src/dxf_write/entity.rs | 61 +++++++++- crates/io/tests/it/entity_props_roundtrip.rs | 16 ++- crates/io/tests/it/low_handseed.rs | 24 ++-- crates/io/tests/it/main.rs | 1 + crates/io/tests/it/polyface_roundtrip.rs | 121 +++++++++++++++++++ 21 files changed, 376 insertions(+), 61 deletions(-) create mode 100644 crates/doc/tests/it/face3d_hidden_edges.rs create mode 100644 crates/engine/tests/it/polyface_explode.rs create mode 100644 crates/io/tests/it/polyface_roundtrip.rs diff --git a/crates/doc/src/entity.rs b/crates/doc/src/entity.rs index b19d10961..a516137fa 100644 --- a/crates/doc/src/entity.rs +++ b/crates/doc/src/entity.rs @@ -48,7 +48,7 @@ mod wipeout; pub use block::{Attrib, AttribModes, AttribProps, Insert, MultilineAttrib, mtext_literal}; pub use common::{Common, Handle, Lineweight, RawTag, Transparency}; -pub use curves::{Arc, Circle, Ellipse, Face3d, Line, LwPolyline, Point, Polyline3d, RayLine, Solid}; +pub use curves::{Arc, Circle, Ellipse, Face3d, Line, LwPolyline, Point, PolyfaceMesh, Polyline3d, RayLine, Solid}; pub use dim::{AssocSnap, DimAssoc, DimBreak, DimKind, DimMarks, Dimension, Inspection, JoggedRadius, Leader, MLeader}; pub use fill::{Gradient, Hatch, HatchLoop}; pub use layout::{Image, Table, TableCell, Viewport}; @@ -86,6 +86,7 @@ pub enum EntityKind { Solid(Solid), Trace(Solid), Face3d(Face3d), + PolyfaceMesh(PolyfaceMesh), Region(Region), Viewport(Viewport), Image(Image), @@ -140,6 +141,7 @@ impl EntityKind { EntityKind::Solid(_) => "2D Solid", EntityKind::Trace(_) => "Trace", EntityKind::Face3d(_) => "3D Face", + EntityKind::PolyfaceMesh(_) => "Polyface Mesh", EntityKind::Region(_) => "Region", EntityKind::Viewport(_) => "Viewport", EntityKind::Image(_) => "Raster Image", @@ -175,6 +177,7 @@ impl EntityKind { EntityKind::Solid(_) => "SOLID", EntityKind::Trace(_) => "TRACE", EntityKind::Face3d(_) => "3DFACE", + EntityKind::PolyfaceMesh(_) => "POLYLINE", EntityKind::Region(_) => "REGION", EntityKind::Viewport(_) => "VIEWPORT", EntityKind::Image(_) => "IMAGE", @@ -218,10 +221,10 @@ impl EntityKind { let c = &s.corners; vec![Prim::Fill(vec![c[0].xy(), c[1].xy(), c[3].xy(), c[2].xy()])] } - EntityKind::Face3d(f) => { - let c = &f.corners; - let pts = [c[0].xy(), c[1].xy(), c[2].xy(), c[3].xy(), c[0].xy()]; - pts.windows(2).filter_map(|w| Some(Prim::Seg(Segment::Line(GLine::new(*w.first()?, *w.get(1)?))))).collect() + EntityKind::Face3d(f) => f.visible_edges().into_iter().map(|(a, b)| Prim::Seg(Segment::Line(GLine::new(a.xy(), b.xy())))).collect(), + // Each face's visible edges (an edge flagged hidden, by a negative index, isn't drawn). + EntityKind::PolyfaceMesh(m) => { + m.faces3d().iter().flat_map(Face3d::visible_edges).map(|(a, b)| Prim::Seg(Segment::Line(GLine::new(a.xy(), b.xy())))).collect() } EntityKind::Region(r) => r.segments().into_iter().map(Prim::Seg).collect(), EntityKind::Hatch(h) => { @@ -424,6 +427,7 @@ impl EntityKind { } EntityKind::Solid(so) | EntityKind::Trace(so) => so.corners.iter_mut().for_each(t3), EntityKind::Face3d(f) => f.corners.iter_mut().for_each(t3), + EntityKind::PolyfaceMesh(pm) => pm.vertices.iter_mut().for_each(t3), EntityKind::Region(r) => { for v in r.loops.iter_mut().flat_map(|l| l.vertices.iter_mut()) { v.p = m.apply(v.p); @@ -542,6 +546,7 @@ impl EntityKind { EntityKind::Hatch(h) => scale(&mut h.elevation), EntityKind::Solid(so) | EntityKind::Trace(so) => so.corners.iter_mut().for_each(z), EntityKind::Face3d(fc) => fc.corners.iter_mut().for_each(z), + EntityKind::PolyfaceMesh(m) => m.vertices.iter_mut().for_each(z), EntityKind::Viewport(v) => z(&mut v.center), EntityKind::Image(i) => z(&mut i.insert), EntityKind::Table(t) => z(&mut t.insert), @@ -611,6 +616,7 @@ impl EntityKind { } EntityKind::Solid(s) | EntityKind::Trace(s) => s.corners.iter().map(|c| c.xy()).collect(), EntityKind::Face3d(f) => f.corners.iter().map(|c| c.xy()).collect(), + EntityKind::PolyfaceMesh(m) => m.vertices.iter().map(|v| v.xy()).collect(), EntityKind::Region(r) => vec![r.bounds().center()], EntityKind::Viewport(v) => { let c = v.center.xy(); diff --git a/crates/doc/src/entity/curves.rs b/crates/doc/src/entity/curves.rs index 8eeb556c4..f32319b02 100644 --- a/crates/doc/src/entity/curves.rs +++ b/crates/doc/src/entity/curves.rs @@ -69,6 +69,52 @@ pub struct Face3d { pub hidden_edges: u8, } +impl Face3d { + /// The edges drawn: side i (corner i to the next) unless bit i of `hidden_edges` hides it, + /// without the zero-length side of a triangle. + pub fn visible_edges(&self) -> Vec<(Vec3, Vec3)> { + let c = self.corners; + (0..4).filter(|i| self.hidden_edges & (1 << i) == 0).map(|i| (c[i], c[(i + 1) % 4])).filter(|(a, b)| a != b).collect() + } +} + +/// POLYLINE polyface mesh (DXF flag 64): its vertices, and faces naming three or four of them +/// by 1-based index as DXF stores them (0: no fourth vertex; a negative index hides the edge +/// that starts at that vertex). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct PolyfaceMesh { + pub vertices: Vec, + pub faces: Vec<[i32; 4]>, +} + +impl PolyfaceMesh { + /// The faces as 3D faces (what EXPLODE makes). Faces with fewer than three vertices or an + /// index outside the vertex list are skipped. + pub fn faces3d(&self) -> Vec { + self.faces.iter().filter_map(|f| self.face3d(f)).collect() + } + /// One face record as a 3D face: `None` when it names fewer than three vertices before a 0, + /// or one outside the vertex list. + pub fn face3d(&self, f: &[i32; 4]) -> Option { + let mut corners: Vec = Vec::with_capacity(4); + let mut hidden = 0u8; + for &ix in f.iter().take_while(|&&ix| ix != 0) { + let at = usize::try_from(ix.unsigned_abs()).ok().and_then(|n| n.checked_sub(1)); + let p = *at.and_then(|n| self.vertices.get(n))?; + if ix < 0 { + hidden |= 1 << corners.len(); + } + corners.push(p); + } + match corners.as_slice() { + // A triangle's closing edge is the 3DFACE's fourth (the third is degenerate). + [a, b, c] => Some(Face3d { corners: [*a, *b, *c, *c], hidden_edges: hidden | if hidden & 4 != 0 { 8 } else { 0 } }), + [a, b, c, d] => Some(Face3d { corners: [*a, *b, *c, *d], hidden_edges: hidden }), + _ => None, + } + } +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct RayLine { pub base: Vec3, diff --git a/crates/doc/tests/it/face3d_hidden_edges.rs b/crates/doc/tests/it/face3d_hidden_edges.rs new file mode 100644 index 000000000..ee49f462f --- /dev/null +++ b/crates/doc/tests/it/face3d_hidden_edges.rs @@ -0,0 +1,19 @@ +//! A 3D face draws only its visible edges: `hidden_edges` bit i hides the side from corner i to +//! the next, as DXF group 70 defines it, and a triangle's zero-length side isn't drawn. + +use cadcraft_doc::{EntityKind, Face3d, Prim}; +use cadcraft_geom::Vec3; + +fn sides(f: Face3d) -> usize { + EntityKind::Face3d(f).prims().iter().filter(|p| matches!(p, Prim::Seg(_))).count() +} + +#[test] +fn hidden_edges_are_not_drawn() { + let quad = [Vec3::new(0.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0), Vec3::new(1.0, 1.0, 0.0), Vec3::new(0.0, 1.0, 0.0)]; + assert_eq!(sides(Face3d { corners: quad, hidden_edges: 0 }), 4); + assert_eq!(sides(Face3d { corners: quad, hidden_edges: 1 | 4 }), 2); + assert_eq!(sides(Face3d { corners: quad, hidden_edges: 15 }), 0); + let triangle = [quad[0], quad[1], quad[2], quad[2]]; + assert_eq!(sides(Face3d { corners: triangle, hidden_edges: 0 }), 3); +} diff --git a/crates/doc/tests/it/main.rs b/crates/doc/tests/it/main.rs index dcad121df..18d01e293 100644 --- a/crates/doc/tests/it/main.rs +++ b/crates/doc/tests/it/main.rs @@ -1,2 +1,3 @@ +mod face3d_hidden_edges; mod lin_pat_parse; mod sub_segment_alias; diff --git a/crates/engine/src/cmd/inquiry.rs b/crates/engine/src/cmd/inquiry.rs index 0fa342e23..119d727e1 100644 --- a/crates/engine/src/cmd/inquiry.rs +++ b/crates/engine/src/cmd/inquiry.rs @@ -285,6 +285,14 @@ fn entity_z(e: &Entity, p: Vec2) -> Option { EntityKind::MText(t) => Some(t.insert.z), EntityKind::Insert(i) => Some(i.insert.z), EntityKind::Face3d(f) => face_z(&f.corners, p).or_else(|| along(&f.corners, true)), + // Drawn as its faces' visible edges: the Z on the nearest one. + EntityKind::PolyfaceMesh(m) => m + .faces3d() + .iter() + .flat_map(cadcraft_doc::Face3d::visible_edges) + .map(|(a, b)| on_segment(a, b)) + .min_by(|a, b| a.1.total_cmp(&b.1)) + .map(|(z, _)| z), _ => None, } } diff --git a/crates/engine/src/cmd/modify/explode.rs b/crates/engine/src/cmd/modify/explode.rs index 46ada655d..d7db29d35 100644 --- a/crates/engine/src/cmd/modify/explode.rs +++ b/crates/engine/src/cmd/modify/explode.rs @@ -151,6 +151,12 @@ pub(crate) fn explode_kind(d: &cadcraft_doc::Drawing, e: &Entity) -> Option { + let faces = m.faces3d(); + (!faces.is_empty()) + .then(|| faces.into_iter().map(|f| Entity { handle: Handle(0), common: e.common.clone(), kind: EntityKind::Face3d(f) }).collect()) + } EntityKind::MLine(m) => Some(crate::cmd::draw2::mline::explode(e, m)), EntityKind::MText(m) => Some(explode_mtext(e, m)), // A region explodes into its boundary's lines and arcs. diff --git a/crates/engine/src/cmd/modify2/selop.rs b/crates/engine/src/cmd/modify2/selop.rs index f1b63e3ce..53939d4b6 100644 --- a/crates/engine/src/cmd/modify2/selop.rs +++ b/crates/engine/src/cmd/modify2/selop.rs @@ -103,6 +103,7 @@ pub(crate) fn flatten_kind(k: &mut EntityKind) { EntityKind::Hatch(h) => h.elevation = 0.0, EntityKind::Solid(so) | EntityKind::Trace(so) => so.corners.iter_mut().for_each(z), EntityKind::Face3d(f) => f.corners.iter_mut().for_each(z), + EntityKind::PolyfaceMesh(m) => m.vertices.iter_mut().for_each(z), EntityKind::Region(_) => {} EntityKind::Image(i) => { z(&mut i.insert); diff --git a/crates/engine/src/grips.rs b/crates/engine/src/grips.rs index 8c58e30b5..b0c1a1bca 100644 --- a/crates/engine/src/grips.rs +++ b/crates/engine/src/grips.rs @@ -226,6 +226,7 @@ pub fn stretch_grip(kind: &EntityKind, index: usize, to: Vec2) -> Option set_xy(s.corners.get_mut(index)?, to), EntityKind::Face3d(f) => set_xy(f.corners.get_mut(index)?, to), + EntityKind::PolyfaceMesh(m) => set_xy(m.vertices.get_mut(index)?, to), EntityKind::Region(_) => return Some(translated(kind, delta)), EntityKind::Viewport(v) => { let opposite = *grips.get((index + 2) % 4)?; diff --git a/crates/engine/tests/it/inquiry_z.rs b/crates/engine/tests/it/inquiry_z.rs index 17f44ac03..274eeacd0 100644 --- a/crates/engine/tests/it/inquiry_z.rs +++ b/crates/engine/tests/it/inquiry_z.rs @@ -3,7 +3,7 @@ //! all said Z = 0, so a topographic drawing's levels couldn't be read in CADCraft. use cadcraft_engine::Session; -use cadcraft_engine::doc::{EntityKind, Face3d, Handle, Line, LwPolyline, Point}; +use cadcraft_engine::doc::{EntityKind, Face3d, Handle, Line, LwPolyline, Point, PolyfaceMesh}; use cadcraft_engine::geom::{PolyVertex, Vec2, Vec3}; use serde_json::json; @@ -56,6 +56,16 @@ fn id_reports_the_z_of_what_is_at_the_point() { assert!(s.log.iter().any(|l| l.contains("Z = 1883.5460")), "{:?}", s.log.iter().rev().take(3).collect::>()); } +#[test] +fn id_reports_the_z_on_a_polyface_mesh() { + let mut s = Session::new(); + // A sloped TIN as two triangles: z = 1000 + x / 10 over (0..100, 0..100). + let vertices = vec![Vec3::new(0.0, 0.0, 1000.0), Vec3::new(100.0, 0.0, 1010.0), Vec3::new(100.0, 100.0, 1010.0), Vec3::new(0.0, 100.0, 1000.0)]; + add(&mut s, EntityKind::PolyfaceMesh(PolyfaceMesh { vertices, faces: vec![[1, 2, -3, 0], [3, 4, -1, 0]] })); + s.cmdline("zoom e").ok(); + assert!((z(&mut s, [50.0, 0.0]) - 1005.0).abs() < 1e-9, "on the mesh's edge"); +} + fn z_given(s: &mut Session) -> f64 { s.execute("id", &json!({ "at": [10, 10, 3] })).unwrap()["z"].as_f64().unwrap() } diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index f49d7bb7e..54ea40675 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -104,6 +104,7 @@ mod pickdrag; mod pline_arc_xline_options; mod plot_styles; mod point_modifiers; +mod polyface_explode; mod post_command_passes; mod prompt_keywords; mod props_bad_lineweight; diff --git a/crates/engine/tests/it/polyface_explode.rs b/crates/engine/tests/it/polyface_explode.rs new file mode 100644 index 000000000..d6d850e88 --- /dev/null +++ b/crates/engine/tests/it/polyface_explode.rs @@ -0,0 +1,32 @@ +//! EXPLODE turns a polyface mesh into its 3D faces, as AutoCAD does; hidden edges stay hidden. + +use cadcraft_engine::Session; +use cadcraft_engine::doc::{Common, EntityKind, PolyfaceMesh, Space}; +use cadcraft_geom::Vec3; +use serde_json::json; + +#[test] +fn exploding_a_polyface_mesh_gives_its_3d_faces() { + let mut s = Session::new(); + let mesh = PolyfaceMesh { + vertices: vec![Vec3::new(0.0, 0.0, 0.0), Vec3::new(10.0, 0.0, 0.0), Vec3::new(10.0, 10.0, 2.0), Vec3::new(0.0, 10.0, 2.0)], + faces: vec![[1, 2, -3, 0], [3, 4, -1, 0]], + }; + let h = s.doc_mut().unwrap().add(&Space::Model, Common::default(), EntityKind::PolyfaceMesh(mesh)).unwrap(); + s.execute("explode", &json!({"handles": [h.hex()]})).unwrap(); + let faces: Vec<_> = s + .doc() + .unwrap() + .model + .iter() + .filter_map(|e| match &e.kind { + EntityKind::Face3d(f) => Some(f.clone()), + _ => None, + }) + .collect(); + assert_eq!(faces.len(), 2); + assert!(s.doc().unwrap().model.iter().all(|e| !matches!(e.kind, EntityKind::PolyfaceMesh(_)))); + // The diagonal starts at the third corner of the first triangle: edge 3 (and the closing 4th) hidden. + assert_eq!(faces[0].corners[2], Vec3::new(10.0, 10.0, 2.0)); + assert_eq!(faces[0].hidden_edges, 4 | 8); +} diff --git a/crates/engine/tests/it/scale_offset_z.rs b/crates/engine/tests/it/scale_offset_z.rs index a32cea081..d82d53ce0 100644 --- a/crates/engine/tests/it/scale_offset_z.rs +++ b/crates/engine/tests/it/scale_offset_z.rs @@ -5,7 +5,7 @@ //! has no thickness while a polyline's keeps it. use cadcraft_engine::Session; -use cadcraft_engine::doc::{Arc, Circle, Common, EntityKind, Handle, Line, LwPolyline, Point}; +use cadcraft_engine::doc::{Arc, Circle, Common, EntityKind, Handle, Line, LwPolyline, Point, PolyfaceMesh}; use cadcraft_engine::geom::{PolyVertex, Vec2, Vec3}; use serde_json::json; @@ -182,3 +182,14 @@ fn offset_keeps_a_polylines_elevation_and_thickness() { let first = k.vertices.first().unwrap().p; assert!(close(first.x, 0.0) && close(first.y, 3.0), "{first:?}"); } + +#[test] +fn scale_scales_a_polyface_meshs_vertex_heights() { + let mut s = Session::new(); + let vertices = vec![Vec3::new(0.0, 0.0, 2.0), Vec3::new(10.0, 0.0, 2.0), Vec3::new(0.0, 10.0, 6.0)]; + let m = add(&mut s, EntityKind::PolyfaceMesh(PolyfaceMesh { vertices, faces: vec![[1, 2, 3, 0]] }), 0.0); + scale(&mut s, m, "0.0 0.0"); + let EntityKind::PolyfaceMesh(k) = get(&s, m).0 else { panic!() }; + let want = [Vec3::new(0.0, 0.0, 3.4), Vec3::new(17.0, 0.0, 3.4), Vec3::new(0.0, 17.0, 10.2)]; + assert!(k.vertices.iter().zip(want).all(|(a, b)| near3(*a, b)), "{:?}", k.vertices); +} diff --git a/crates/io/src/dxf_read.rs b/crates/io/src/dxf_read.rs index 324caa361..341e72a77 100644 --- a/crates/io/src/dxf_read.rs +++ b/crates/io/src/dxf_read.rs @@ -93,7 +93,7 @@ fn read_budgeted(bytes: &[u8], mut table_cells_left: usize) -> Result { let mut objs = Objects::default(); // Every handle another program's file already uses is taken before anything gets a new one: // its $HANDSEED can be lower than its handles, and entities made while reading (the faces of - // a polyface mesh) would otherwise get handles that records further on still have. A file + // a polygon mesh) would otherwise get handles that records further on still have. A file // CADCraft wrote names its own next handle, above which its rebuilt records are numbered // (#495), so it is left to that. let ours = secs.iter().any(|s| s.name == "OBJECTS" && s.tags.iter().any(|t| t.code == 3 && t.str() == crate::dxf_ext::HANDSEED_KEY)); diff --git a/crates/io/src/dxf_read/document.rs b/crates/io/src/dxf_read/document.rs index d732f4450..db935974a 100644 --- a/crates/io/src/dxf_read/document.rs +++ b/crates/io/src/dxf_read/document.rs @@ -23,7 +23,7 @@ pub(super) fn parse_entities( { c.plot_style = n; } - // More entities from this record (the faces of a polyface or polygon mesh). + // More entities from this record (the faces of a polygon mesh). let mut extra: Vec = Vec::new(); i += 1; match kind.as_str() { diff --git a/crates/io/src/dxf_read/entities.rs b/crates/io/src/dxf_read/entities.rs index 394feb785..119d00bbd 100644 --- a/crates/io/src/dxf_read/entities.rs +++ b/crates/io/src/dxf_read/entities.rs @@ -7,6 +7,7 @@ use super::{ LwPolyline, MAX_FRAME_CHARS, Point, PolyVertex, Polyline3d, RawTag, RayLine, Rx, Solid, Spline, Table, TableCell, Tag, Tolerance, Unknown, Vec2, Vec3, Viewport, Wipeout, records, }; +use cadcraft_doc::PolyfaceMesh; /// Parse one entity record. POLYLINE/INSERT followers are handled by the caller. pub(super) fn entity(kind: &str, tags: &[Tag]) -> Option<(Common, EntityKind)> { @@ -514,11 +515,20 @@ pub(super) fn polyline_entity(recs: &[(String, Vec)], i: &mut usize, t: &T) } let c = common(t); let mut extra: Vec = Vec::new(); - let k = if flags & (16 | 64) != 0 { - // Polyface (64) and polygon (16) meshes become 3D faces; the first takes the - // polyline's place. + let k = if flags & 64 != 0 { + // Face records that name a missing vertex (or fewer than three) are dropped: they draw + // nothing and would be written back broken. A mesh left with no face is skipped. + let mut mesh = PolyfaceMesh { vertices: pts.iter().map(|p| p.0).collect(), faces: Vec::new() }; + let kept = faces.iter().map(|f| f.map(|ix| i32::try_from(ix).unwrap_or(i32::MAX))).filter(|f| mesh.face3d(f).is_some()).collect(); + mesh.faces = kept; + if mesh.faces.is_empty() { + return None; + } + EntityKind::PolyfaceMesh(mesh) + } else if flags & 16 != 0 { + // Polygon meshes become 3D faces; the first takes the polyline's place. let verts: Vec = pts.iter().map(|p| p.0).collect(); - let mut fs = if flags & 64 != 0 { polyface_faces(&verts, &faces) } else { mesh_faces(&verts, t, flags) }.into_iter().map(EntityKind::Face3d); + let mut fs = mesh_faces(&verts, t, flags).into_iter().map(EntityKind::Face3d); let first = fs.next()?; extra.extend(fs); first @@ -573,36 +583,6 @@ pub(super) fn insert_attribs(recs: &[(String, Vec)], i: &mut usize, ins_lay attribs } -/// Faces of a polyface mesh: each face record names three or four vertices by 1-based index; -/// a negative index hides the edge that starts at that vertex. Faces with fewer vertices or -/// an index outside the vertex list are skipped. -fn polyface_faces(verts: &[Vec3], faces: &[[i64; 4]]) -> Vec { - let mut out = Vec::new(); - for f in faces { - let mut corners: Vec = Vec::with_capacity(4); - let mut hidden = 0u8; - let mut ok = true; - for &ix in f.iter().filter(|&&ix| ix != 0) { - let at = usize::try_from(ix.unsigned_abs()).ok().and_then(|n| n.checked_sub(1)); - let Some(p) = at.and_then(|n| verts.get(n)) else { - ok = false; - break; - }; - if ix < 0 { - hidden |= 1 << corners.len(); - } - corners.push(*p); - } - match (ok, corners.as_slice()) { - // A triangle's closing edge is the 3DFACE's fourth (the third is degenerate). - (true, [a, b, c]) => out.push(Face3d { corners: [*a, *b, *c, *c], hidden_edges: hidden | if hidden & 4 != 0 { 8 } else { 0 } }), - (true, [a, b, c, d]) => out.push(Face3d { corners: [*a, *b, *c, *d], hidden_edges: hidden }), - _ => {} - } - } - out -} - /// Quads of an M×N polygon mesh (71/72, or the smoothed 73/74 when the vertex count matches /// those), closed in M by bit 1 and in N by bit 32. Vertices run row by row. fn mesh_faces(verts: &[Vec3], t: &T, flags: i64) -> Vec { diff --git a/crates/io/src/dxf_write.rs b/crates/io/src/dxf_write.rs index b93471b01..411f1c4b2 100644 --- a/crates/io/src/dxf_write.rs +++ b/crates/io/src/dxf_write.rs @@ -68,7 +68,11 @@ impl W { self.f(c + 10, p.y); } fn h(&mut self) -> String { - let h = format!("{:X}", self.next); + format!("{:X}", self.next_handle()) + } + /// A new handle for a record the drawing doesn't hold (as [`W::h`]). + fn next_handle(&mut self) -> u64 { + let h = self.next; self.next = self.next.saturating_add(1); h } diff --git a/crates/io/src/dxf_write/entity.rs b/crates/io/src/dxf_write/entity.rs index f1e0a1d73..51b6b4388 100644 --- a/crates/io/src/dxf_write/entity.rs +++ b/crates/io/src/dxf_write/entity.rs @@ -32,6 +32,64 @@ pub(super) fn hatch_reactors(d: &Drawing) -> HashMap> { out } +/// A polyface mesh as one POLYLINE record: the vertices (flags 64 | 128), then one face record +/// (128) per face that names them (the others draw nothing and are dropped, as on read). Counts +/// and indices are 16-bit groups (71–74), and no index is above the vertex count: a mesh too large +/// for them is written as its 3D faces rather than with numbers cut short. +fn polyface(w: &mut W, d: &Drawing, e: &Entity, m: &cadcraft_doc::PolyfaceMesh, owner: &str, paper: bool, cx: &Ctx) { + let faces: Vec<&[i32; 4]> = m.faces.iter().filter(|f| m.face3d(f).is_some()).collect(); + let max = usize::try_from(i16::MAX).unwrap_or(usize::MAX); + if m.vertices.len() > max || faces.len() > max { + for (i, f) in m.faces3d().into_iter().enumerate() { + let handle = if i == 0 { e.handle } else { Handle(w.next_handle()) }; + let face = Entity { handle, common: e.common.clone(), kind: EntityKind::Face3d(f) }; + entity(w, d, &face, owner, paper, cx); + } + return; + } + w.s(0, "POLYLINE"); + common(w, e, owner, paper, "AcDbPolyFaceMesh"); + w.i(66, 1); + w.p(10, Vec3::ZERO); + w.i(70, 64); + w.i(71, m.vertices.len() as i64); + w.i(72, faces.len() as i64); + for q in &m.vertices { + let vh = w.h(); + w.s(0, "VERTEX"); + w.s(5, vh); + w.s(330, e.handle.hex()); + w.s(100, "AcDbEntity"); + w.s(8, &e.common.layer); + w.s(100, "AcDbVertex"); + w.s(100, "AcDbPolyFaceMeshVertex"); + w.p(10, *q); + w.i(70, 192); + } + for f in faces { + let vh = w.h(); + w.s(0, "VERTEX"); + w.s(5, vh); + w.s(330, e.handle.hex()); + w.s(100, "AcDbEntity"); + w.s(8, &e.common.layer); + w.s(100, "AcDbFaceRecord"); + w.p(10, Vec3::ZERO); + w.i(70, 128); + for (code, ix) in [71, 72, 73, 74].into_iter().zip(f) { + if *ix != 0 { + w.i(code, i64::from(*ix)); + } + } + } + let sh = w.h(); + w.s(0, "SEQEND"); + w.s(5, sh); + w.s(330, e.handle.hex()); + w.s(100, "AcDbEntity"); + w.s(8, &e.common.layer); +} + pub(super) fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: bool, cx: &Ctx) { write_basic_entity(w, d, e, owner, paper, cx); write_text_entity(w, d, e, owner, paper, cx); @@ -45,7 +103,7 @@ pub(super) fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: boo write_other_entity(w, d, e, owner, paper, cx); } -fn write_basic_entity(w: &mut W, _d: &Drawing, e: &Entity, owner: &str, paper: bool, cx: &Ctx) { +fn write_basic_entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: bool, cx: &Ctx) { match &e.kind { EntityKind::Line(l) => { w.s(0, "LINE"); @@ -144,6 +202,7 @@ fn write_basic_entity(w: &mut W, _d: &Drawing, e: &Entity, owner: &str, paper: b w.s(100, "AcDbEntity"); w.s(8, &e.common.layer); } + EntityKind::PolyfaceMesh(m) => polyface(w, d, e, m, owner, paper, cx), EntityKind::Spline(s) => { w.s(0, "SPLINE"); common(w, e, owner, paper, "AcDbSpline"); diff --git a/crates/io/tests/it/entity_props_roundtrip.rs b/crates/io/tests/it/entity_props_roundtrip.rs index 38d325604..a5467d7cc 100644 --- a/crates/io/tests/it/entity_props_roundtrip.rs +++ b/crates/io/tests/it/entity_props_roundtrip.rs @@ -257,8 +257,16 @@ fn faces(d: &Drawing) -> Vec { .collect() } +/// The faces of the one polyface mesh in `d`, as 3D faces. +fn polyface_faces(d: &Drawing) -> Vec { + match d.model.iter().map(|e| &e.kind).collect::>().as_slice() { + [EntityKind::PolyfaceMesh(m)] => m.faces3d(), + ks => panic!("expected one polyface mesh, got {ks:?}"), + } +} + #[test] -fn polyface_and_polygon_mesh_polylines_become_faces_not_bogus_vertices() { +fn polyface_and_polygon_mesh_polylines_read_as_meshes_and_faces_not_bogus_vertices() { let p = |x: f64, y: f64, z: f64| Vec3::new(x, y, z); // Polyface: four vertices, a quad, a triangle whose third edge is hidden, and faces with an // out-of-range index or only two vertices (skipped). @@ -274,14 +282,14 @@ fn polyface_and_polygon_mesh_polylines_become_faces_not_bogus_vertices() { recs.push(vec![(0, "SEQEND"), (8, "0")]); let refs: Vec<&[(i32, &str)]> = recs.iter().map(Vec::as_slice).collect(); let d = read_dxf(dxf(&refs).as_bytes()).expect("read polyface"); - let fs = faces(&d); + let fs = polyface_faces(&d); assert_eq!(fs.len(), 2, "{fs:?}"); assert_eq!(fs[0].corners, [p(0.0, 0.0, 2.0), p(1.0, 0.0, 2.0), p(1.0, 1.0, 2.0), p(0.0, 1.0, 2.0)]); assert_eq!(fs[0].hidden_edges, 0); assert_eq!(fs[1].corners, [p(0.0, 0.0, 2.0), p(1.0, 1.0, 2.0), p(0.0, 1.0, 2.0), p(0.0, 1.0, 2.0)]); assert_eq!(fs[1].hidden_edges, 4 | 8, "the triangle's closing edge is hidden"); - // The faces survive a save and reopen as 3DFACEs. - assert_eq!(faces(&roundtrip(&d)), fs); + // The mesh survives a save and reopen. + assert_eq!(polyface_faces(&roundtrip(&d)), fs); // 3×2 polygon mesh: two quads. let mut recs: Vec> = diff --git a/crates/io/tests/it/low_handseed.rs b/crates/io/tests/it/low_handseed.rs index fbc1a77b6..26273f778 100644 --- a/crates/io/tests/it/low_handseed.rs +++ b/crates/io/tests/it/low_handseed.rs @@ -1,32 +1,32 @@ //! Another program's file can name a $HANDSEED below handles it uses. Entities CADCraft makes -//! while reading (the faces a polyface mesh becomes) must not take handles that records further +//! while reading (the faces a polygon mesh becomes) must not take handles that records further //! on still have: a DWG keeps one object per handle, so the duplicates were lost on save. use std::collections::HashSet; use cadcraft_doc::{Drawing, EntityKind, Handle}; -/// A block holding a polyface mesh of three triangles (handle 2000) and, in model space after it, +/// A block holding a 2×4 polygon mesh, three quads (handle 2000) and, in model space after it, /// lines with handles 2001–2003 (the next ones after the mesh's), under a $HANDSEED of 10. The /// collision is across separately read lists (a block and model space), as in the files it came /// from: within one list duplicates were already renumbered. fn dxf() -> String { - let vertex = |h: &str, x: f64, y: f64| format!("0\nVERTEX\n5\n{h}\n8\n0\n10\n{x}\n20\n{y}\n30\n0.0\n70\n192\n"); - let face = |h: &str, a: i32, b: i32, c: i32| format!("0\nVERTEX\n5\n{h}\n8\n0\n10\n0.0\n20\n0.0\n30\n0.0\n70\n128\n71\n{a}\n72\n{b}\n73\n{c}\n"); + let vertex = |h: &str, x: f64, y: f64| format!("0\nVERTEX\n5\n{h}\n8\n0\n10\n{x}\n20\n{y}\n30\n0.0\n70\n64\n"); let line = |h: &str| format!("0\nLINE\n5\n{h}\n8\n0\n10\n0.0\n20\n0.0\n30\n0.0\n11\n1.0\n21\n1.0\n31\n0.0\n"); format!( "0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1032\n9\n$HANDSEED\n5\n10\n0\nENDSEC\n\ 0\nSECTION\n2\nBLOCKS\n0\nBLOCK\n5\n30\n8\n0\n2\nMESH\n70\n0\n10\n0.0\n20\n0.0\n30\n0.0\n\ - 0\nPOLYLINE\n5\n2000\n8\n0\n66\n1\n10\n0.0\n20\n0.0\n30\n0.0\n70\n64\n71\n4\n72\n3\n\ - {}{}{}{}{}{}{}0\nSEQEND\n5\n2010\n8\n0\n0\nENDBLK\n5\n31\n8\n0\n0\nENDSEC\n\ + 0\nPOLYLINE\n5\n2000\n8\n0\n66\n1\n10\n0.0\n20\n0.0\n30\n0.0\n70\n16\n71\n2\n72\n4\n\ + {}{}{}{}{}{}{}{}0\nSEQEND\n5\n2010\n8\n0\n0\nENDBLK\n5\n31\n8\n0\n0\nENDSEC\n\ 0\nSECTION\n2\nENTITIES\n{}{}{}0\nINSERT\n5\n60\n8\n0\n2\nMESH\n10\n0.0\n20\n0.0\n30\n0.0\n0\nENDSEC\n0\nEOF\n", vertex("2004", 0.0, 0.0), - vertex("2005", 1.0, 0.0), - vertex("2006", 1.0, 1.0), - vertex("2007", 0.0, 1.0), - face("2008", 1, 2, 3), - face("2009", 1, 3, 4), - face("200A", 2, 3, 4), + vertex("2005", 0.0, 1.0), + vertex("2006", 0.0, 2.0), + vertex("2007", 0.0, 3.0), + vertex("2008", 1.0, 0.0), + vertex("2009", 1.0, 1.0), + vertex("200A", 1.0, 2.0), + vertex("200B", 1.0, 3.0), line("2001"), line("2002"), line("2003"), diff --git a/crates/io/tests/it/main.rs b/crates/io/tests/it/main.rs index 8442a7629..79c4e4b3e 100644 --- a/crates/io/tests/it/main.rs +++ b/crates/io/tests/it/main.rs @@ -54,6 +54,7 @@ mod pdf_read; mod plot_layout_scale; mod plot_model_png_scale; mod plot_styles; +mod polyface_roundtrip; mod portable_fonts; mod region_roundtrip; mod roundtrip; diff --git a/crates/io/tests/it/polyface_roundtrip.rs b/crates/io/tests/it/polyface_roundtrip.rs new file mode 100644 index 000000000..0ad05518d --- /dev/null +++ b/crates/io/tests/it/polyface_roundtrip.rs @@ -0,0 +1,121 @@ +//! A polyface mesh stays one object through DXF and DWG, instead of becoming loose 3D faces; +//! its hidden edges stay hidden. + +use cadcraft_doc::{Drawing, EntityKind, PolyfaceMesh, Prim}; +use cadcraft_geom::Vec3; +use cadcraft_io::{read_dxf, write_dxf}; + +/// A 10 × 10 square as two triangles; the diagonal (from vertex 3) is hidden in both. +const POLYFACE_DXF: &str = "0\nSECTION\n2\nENTITIES\n\ +0\nPOLYLINE\n5\n30\n100\nAcDbEntity\n8\n0\n100\nAcDbPolyFaceMesh\n66\n1\n10\n0\n20\n0\n30\n0\n70\n64\n71\n4\n72\n2\n\ +0\nVERTEX\n5\n31\n8\n0\n10\n0\n20\n0\n30\n0\n70\n192\n\ +0\nVERTEX\n5\n32\n8\n0\n10\n10\n20\n0\n30\n0\n70\n192\n\ +0\nVERTEX\n5\n33\n8\n0\n10\n10\n20\n10\n30\n2\n70\n192\n\ +0\nVERTEX\n5\n34\n8\n0\n10\n0\n20\n10\n30\n2\n70\n192\n\ +0\nVERTEX\n5\n35\n8\n0\n10\n0\n20\n0\n30\n0\n70\n128\n71\n1\n72\n2\n73\n-3\n\ +0\nVERTEX\n5\n36\n8\n0\n10\n0\n20\n0\n30\n0\n70\n128\n71\n3\n72\n4\n73\n-1\n\ +0\nSEQEND\n5\n37\n8\n0\n0\nENDSEC\n0\nEOF\n"; + +fn mesh(d: &Drawing) -> PolyfaceMesh { + let meshes: Vec<&PolyfaceMesh> = d + .model + .iter() + .filter_map(|e| match &e.kind { + EntityKind::PolyfaceMesh(m) => Some(m), + _ => None, + }) + .collect(); + assert_eq!(meshes.len(), 1, "one polyface mesh"); + assert!(d.model.iter().all(|e| !matches!(e.kind, EntityKind::Face3d(_))), "no loose 3D faces"); + meshes[0].clone() +} + +fn expected() -> PolyfaceMesh { + PolyfaceMesh { + vertices: vec![Vec3::new(0.0, 0.0, 0.0), Vec3::new(10.0, 0.0, 0.0), Vec3::new(10.0, 10.0, 2.0), Vec3::new(0.0, 10.0, 2.0)], + faces: vec![[1, 2, -3, 0], [3, 4, -1, 0]], + } +} + +#[test] +fn polyface_mesh_is_read_as_one_object_with_its_hidden_edges() { + let m = mesh(&read_dxf(POLYFACE_DXF.as_bytes()).unwrap()); + assert_eq!(m, expected()); + assert_eq!(m.faces3d().len(), 2); + // The square's four sides; the hidden diagonal isn't drawn. + let edges = EntityKind::PolyfaceMesh(m).prims().into_iter().filter(|p| matches!(p, Prim::Seg(_))).count(); + assert_eq!(edges, 4); +} + +#[test] +fn polyface_mesh_round_trips_through_dxf_and_dwg() { + let d = read_dxf(POLYFACE_DXF.as_bytes()).unwrap(); + let text = write_dxf(&d); + assert!(text.contains("AcDbPolyFaceMesh") && text.contains("AcDbFaceRecord")); + assert_eq!(mesh(&read_dxf(text.as_bytes()).unwrap()), expected()); + #[cfg(not(target_arch = "wasm32"))] + { + let dwg = cadcraft_io::write(&d, "x.dwg").unwrap(); + assert_eq!(mesh(&cadcraft_io::read(&dwg, "x.dwg").unwrap()), expected()); + } +} + +#[test] +fn a_polyface_mesh_too_large_for_its_record_is_saved_as_3d_faces() { + use cadcraft_doc::{Common, Space}; + use cadcraft_io::{SaveOptions, read, write_with}; + // 40,000 vertices: counts and indices past 32,767 don't fit the 16-bit groups 71–74. + let vertices: Vec = (0..40_000).map(|i| Vec3::new(f64::from(i), 0.0, f64::from(i % 7))).collect(); + let mesh = PolyfaceMesh { vertices, faces: vec![[1, 2, 39_999, 0], [39_998, -39_999, 40_000, 3]] }; + let mut d = Drawing::new_metric(); + let common = Common { layer: "0".into(), ..Common::default() }; + d.add(&Space::Model, common, EntityKind::PolyfaceMesh(mesh.clone())).unwrap(); + let want = mesh.faces3d(); + for binary in [false, true] { + let bytes = write_with(&d, "big.dxf", &SaveOptions { version: None, binary }).unwrap(); + let back = read(&bytes, "big.dxf").unwrap(); + let faces: Vec<_> = back + .model + .iter() + .filter_map(|e| match &e.kind { + EntityKind::Face3d(f) => Some(f.clone()), + _ => None, + }) + .collect(); + // Every face keeps its corners and hidden edges: no index was cut to 32,767. + assert_eq!(faces, want, "binary: {binary}"); + assert!(back.model.iter().all(|e| !matches!(e.kind, EntityKind::PolyfaceMesh(_))), "binary: {binary}"); + let mut handles: Vec<_> = back.model.iter().map(|e| e.handle).collect(); + handles.sort(); + handles.dedup(); + assert_eq!(handles.len(), back.model.iter().count(), "each face has its own handle"); + } +} + +#[test] +fn faces_naming_missing_vertices_are_dropped_on_read() { + // Added faces: one names vertex 9 of a 4-vertex mesh, one has only two corners, one has two + // before its first 0 (which ends the face). + let text = POLYFACE_DXF.replace( + "0\nSEQEND\n", + "0\nVERTEX\n5\n38\n8\n0\n10\n0\n20\n0\n30\n0\n70\n128\n71\n1\n72\n2\n73\n9\n\ +0\nVERTEX\n5\n39\n8\n0\n10\n0\n20\n0\n30\n0\n70\n128\n71\n1\n72\n2\n\ +0\nVERTEX\n5\n3A\n8\n0\n10\n0\n20\n0\n30\n0\n70\n128\n71\n1\n72\n2\n74\n3\n0\nSEQEND\n", + ); + let d = read_dxf(text.as_bytes()).unwrap(); + assert_eq!(mesh(&d), expected(), "only the faces that name its vertices"); + let back = read_dxf(write_dxf(&d).as_bytes()).unwrap(); + assert_eq!(mesh(&back), expected()); +} + +#[test] +fn faces_naming_missing_vertices_are_not_written() { + use cadcraft_doc::{Common, Space}; + let mut m = expected(); + m.faces.push([1, 2, 9, 0]); + let mut d = Drawing::new_metric(); + d.add(&Space::Model, Common { layer: "0".into(), ..Common::default() }, EntityKind::PolyfaceMesh(m)).unwrap(); + let text = write_dxf(&d); + assert_eq!(text.matches("AcDbFaceRecord").count(), 2, "only the faces that name its vertices"); + assert_eq!(mesh(&read_dxf(text.as_bytes()).unwrap()), expected()); +} From b006110dc591c268ac558dc25c32705262d01503 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Mon, 2 Nov 2026 17:31:14 +0700 Subject: [PATCH 041/100] OOPS: bring back what -WBLOCK erased; keep the last erase through REFEDIT; the Block Editor (#2625) * OOPS brings back what -WBLOCK erased -WBLOCK erases the objects it writes, and OOPS brings back what the last ERASE or BLOCK removed; the two landed separately, so OOPS never saw what -WBLOCK erased. -WBLOCK now erases through erase_kept, as ERASE and BLOCK do. As AutoCAD 2024 does (accoreconsole, the drawing's entnext order read back): OOPS after -WBLOCK brings its objects back, each where it was and with its handle; only the last erase counts, -WBLOCK's or ERASE's; and -WBLOCK * erases nothing, so OOPS still brings back the erase before it. Co-Authored-By: Claude Opus 5.5 * OOPS: keep the last erase through a refused REFEDIT command; Block Editor During REFEDIT, a command that removes an object outside the working set is undone (refedit::after_command), but erase_kept had already replaced what OOPS kept: the objects came back, and the erase before them was lost. erase_kept now leaves OOPS as it was when an object it removes is outside the working set. That covers BLOCK, -WBLOCK, ERASE and the Delete key. In the Block Editor, its objects keep the handles they have in the block definition, so in_blocks found them there and OOPS brought back nothing erased in the editor. in_blocks now skips the block open there. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/blocks/wblock.rs | 8 ++-- crates/engine/src/cmd/modify.rs | 2 +- crates/engine/src/cmd/modify/oops.rs | 22 +++++++--- crates/engine/tests/dash_wblock.rs | 59 ++++++++++++++++++++++++++ crates/engine/tests/it/oops_command.rs | 28 ++++++++++++ 5 files changed, 107 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/cmd/blocks/wblock.rs b/crates/engine/src/cmd/blocks/wblock.rs index 03356269e..98ab0c344 100644 --- a/crates/engine/src/cmd/blocks/wblock.rs +++ b/crates/engine/src/cmd/blocks/wblock.rs @@ -7,6 +7,7 @@ use serde_json::{Value, json}; use super::super::curves::KW; use super::super::machines::{SelOutcome, SelectPhase}; +use super::super::modify::erase_kept; use super::super::{CommandSpec, bad, point_param, str_param, targets}; use super::insert_file::stem; use crate::{Accept, Input, Interactive, Prompt, Result, Session, Step}; @@ -16,7 +17,7 @@ pub(super) fn wblock_spec() -> CommandSpec { } /// -WBLOCK erases the objects it writes, whatever its mOde option says: AutoCAD 2024 does, and -/// keeps "Object conversion=Delete" as the setting. +/// keeps "Object conversion=Delete" as the setting. OOPS brings them back, as after ERASE. pub(super) fn dash_wblock_spec() -> CommandSpec { CommandSpec::new("-wblock", "Write Block", run_dash_wblock) .params("{path, name? | handles?, base?} (the objects written are erased)") @@ -124,10 +125,7 @@ fn moved(e: &Entity, m: &Mat3) -> Entity { } fn erase(s: &mut Session, hs: &[Handle]) -> Result<()> { - let doc = s.doc_mut()?; - for h in hs { - doc.remove_entity(*h); - } + erase_kept(s, hs)?; s.set_selection(Vec::new()); Ok(()) } diff --git a/crates/engine/src/cmd/modify.rs b/crates/engine/src/cmd/modify.rs index 828ce1c01..f68d08ebb 100644 --- a/crates/engine/src/cmd/modify.rs +++ b/crates/engine/src/cmd/modify.rs @@ -46,7 +46,7 @@ pub fn specs() -> Vec { .alias(&["e", "delete"]) .params("{handles?: [hex]} (default: selection)") .interactive(|_| Ok(Box::new(SelectThen::new(Op::Erase)))), - CommandSpec::new("oops", "Oops", run_oops).params("{} (brings back what the last ERASE or BLOCK removed)"), + CommandSpec::new("oops", "Oops", run_oops).params("{} (brings back what the last ERASE, BLOCK or -WBLOCK removed)"), CommandSpec::new("move", "Move", run_move) .menu(&["Modify", "Move"]) .alias(&["m"]) diff --git a/crates/engine/src/cmd/modify/oops.rs b/crates/engine/src/cmd/modify/oops.rs index 217f57ae6..6308cc615 100644 --- a/crates/engine/src/cmd/modify/oops.rs +++ b/crates/engine/src/cmd/modify/oops.rs @@ -1,6 +1,7 @@ //! OOPS: bring back the objects the last ERASE (or the Delete key) removed, or BLOCK deleting the -//! objects it makes a block of, with their handles and each where it was in the draw order, as -//! AutoCAD 2024 does (accoreconsole, the drawing's `entnext` order read back afterwards). +//! objects it makes a block of, or -WBLOCK the objects it writes, with their handles and each where +//! it was in the draw order, as AutoCAD 2024 does (accoreconsole, the drawing's `entnext` order read +//! back afterwards). use std::collections::{HashMap, HashSet}; use std::sync::Arc; @@ -50,7 +51,10 @@ pub(crate) fn erase_kept(s: &mut Session, hs: &[Handle]) -> Result { } } let n = taken.len(); - if n > 0 { + // During REFEDIT, removing an object outside the working set gets the command undone + // (`refedit::after_command`), so what OOPS had stays. + let refused = taken.iter().any(|t| crate::cmd::blocks::refedit::locked(d, t.entity.handle)); + if n > 0 && !refused { s.state_mut()?.erased = Erased(taken); } Ok(n) @@ -58,9 +62,10 @@ pub(crate) fn erase_kept(s: &mut Session, hs: &[Handle]) -> Result { pub(super) fn run_oops(s: &mut Session, _p: &Value) -> Result { let taken = s.state()?.erased.0.clone(); + let editing = s.state()?.block_edit.as_ref().map(|b| b.name.clone()); let back: Vec = { let d = s.doc()?; - let in_blocks = in_blocks(d, &taken); + let in_blocks = in_blocks(d, &taken, editing.as_deref()); taken.iter().map(|t| !in_blocks.contains(&t.entity.handle) && can_return(d, t)).collect() }; // Nothing to bring back leaves the drawing as it is: no undo step. @@ -72,11 +77,16 @@ pub(super) fn run_oops(s: &mut Session, _p: &Value) -> Result { } /// The handles of `taken` that a block's object has now (an undo can take the drawing back to -/// before an erased object was made, and a block's new object take its handle). -fn in_blocks(d: &Drawing, taken: &[Taken]) -> HashSet { +/// before an erased object was made, and a block's new object take its handle). The block open +/// in the Block Editor (`editing`) doesn't count: its objects there keep the handles they have in +/// it. +fn in_blocks(d: &Drawing, taken: &[Taken], editing: Option<&str>) -> HashSet { let hs: HashSet = taken.iter().map(|t| t.entity.handle).collect(); let mut out = HashSet::new(); for b in d.blocks.values() { + if editing.is_some_and(|n| b.name.eq_ignore_ascii_case(n)) { + continue; + } // Through whichever is smaller. if b.entities.len() < hs.len() { out.extend(b.entities.iter().map(|e| e.handle).filter(|h| hs.contains(h))); diff --git a/crates/engine/tests/dash_wblock.rs b/crates/engine/tests/dash_wblock.rs index 348cde45a..52d456f42 100644 --- a/crates/engine/tests/dash_wblock.rs +++ b/crates/engine/tests/dash_wblock.rs @@ -242,3 +242,62 @@ fn the_json_form_of_dash_wblock_erases_what_it_writes() { assert!(s.execute("-wblock", &json!({ "path": none })).is_err()); assert!(!std::path::Path::new(&none).exists()); } + +#[test] +fn oops_brings_back_what_it_erased_where_it_was() { + // OOPSW-001 to OOPSW-005 (the drawing's `entnext` order read back): OOPS brings back what + // -WBLOCK erased, each where it was, as after ERASE. Only the last erase counts, and "*" (the + // whole drawing) erases nothing. + const OOPS: &str = "(command \"_.OOPS\")"; + // (script with @A to @D for the objects and @F for a new file, the order afterwards) + let cases: [(&[&str], &str); 5] = [ + (&["(command \"_.-WBLOCK\" @F \"\" '(0.0 0.0) @B @C \"\")", OOPS], "A B C D"), + (&["(command \"_.-WBLOCK\" @F \"\" '(0.0 0.0) @B \"\")", "(command \"_.ERASE\" @D \"\")", OOPS], "A C D"), + (&["(command \"_.ERASE\" @D \"\")", "(command \"_.-WBLOCK\" @F \"\" '(0.0 0.0) @B \"\")", OOPS], "A B C"), + (&["(command \"_.ERASE\" @D \"\")", "(command \"_.-WBLOCK\" @F \"*\")", OOPS], "A B C D"), + (&["(command \"_.-WBLOCK\" @F \"\" \"_O\" \"_R\" '(0.0 0.0) @A @C \"\")", OOPS], "A B C D"), + ]; + let dir = TempDir::new("dash-wblock"); + for (i, (script, want)) in cases.into_iter().enumerate() { + let (mut s, a, b, c) = drawing(); + let v = s.execute("line", &json!({ "points": [[0, 40], [10, 40]] })).unwrap(); + let d = Handle::parse_hex(v["handles"][0].as_str().or(v["handle"].as_str()).unwrap()).unwrap(); + let hs = [a, b, c, d]; + for line in script { + let mut line = line.to_string(); + for (n, h) in ["@A", "@B", "@C", "@D"].into_iter().zip(hs) { + line = line.replace(n, &ent(h)); + } + let line = line.replace("@F", &format!("\"{}\"", file(&dir, &format!("OOPSW{i}.dwg")))); + lisp(&mut s, &line); + assert!(s.running.is_none(), "{line}: {}", s.prompt_text()); + } + let order: Vec<&str> = + s.doc().unwrap().model.iter().map(|e| hs.iter().position(|h| *h == e.handle).map_or("E", |k| ["A", "B", "C", "D"][k])).collect(); + assert_eq!(order.join(" "), want, "{script:?}"); + } + // The JSON form too. + let (mut s, a, b, c) = drawing(); + s.execute("-wblock", &json!({ "path": file(&dir, "json.dwg"), "handles": [b.hex()] })).unwrap(); + assert!(!alive(&s, b)); + s.execute("oops", &json!({})).unwrap(); + let order: Vec = s.doc().unwrap().model.iter().map(|e| e.handle).collect(); + assert_eq!(order, [a, b, c]); +} + +#[test] +fn a_dash_wblock_refused_during_refedit_leaves_oops_as_it_was() { + let (mut s, a, b, c) = drawing(); + let dir = TempDir::new("dash-wblock"); + s.execute("block", &json!({ "name": "BK", "base": [0, 0], "handles": [a.hex()], "keep": "convert" })).unwrap(); + let ins = Handle::parse_hex(s.execute("entities", &json!({ "type": "INSERT" })).unwrap()["entities"][0]["handle"].as_str().unwrap()).unwrap(); + s.execute("erase", &json!({ "handles": [c.hex()] })).unwrap(); + s.execute("refedit", &json!({ "handle": ins.hex() })).unwrap(); + // B isn't in the working set: -WBLOCK is refused and the drawing put back. + assert!(s.execute("-wblock", &json!({ "path": file(&dir, "out.dwg"), "handles": [b.hex()] })).is_err()); + assert!(alive(&s, b)); + s.execute("refclose", &json!({ "save": false })).unwrap(); + // OOPS still has C, the last erase that stood. + s.execute("oops", &json!({})).unwrap(); + assert!(alive(&s, c)); +} diff --git a/crates/engine/tests/it/oops_command.rs b/crates/engine/tests/it/oops_command.rs index 465484e0f..f42564c6f 100644 --- a/crates/engine/tests/it/oops_command.rs +++ b/crates/engine/tests/it/oops_command.rs @@ -230,3 +230,31 @@ fn a_reference_edited_in_place_keeps_its_objects_and_the_drawings_apart() { assert_eq!(oops(&mut s), json!({ "restored": 1 })); assert_eq!(order(&s, &hs), "C E"); } + +#[test] +fn a_block_refused_during_refedit_leaves_oops_as_it_was() { + let (mut s, hs) = drawing(); + s.execute("block", &json!({ "name": "BK", "base": [0, 0], "handles": [hs[0].hex()], "keep": "convert" })).unwrap(); + let ins = *handles(&s).last().unwrap(); + s.execute("erase", &json!({ "handles": [hs[3].hex()] })).unwrap(); + s.execute("refedit", &json!({ "handle": ins.hex() })).unwrap(); + // B isn't in the working set: the block is refused and the drawing put back. + let r = s.execute("block", &json!({ "name": "BK2", "base": [0, 0], "handles": [hs[1].hex()], "keep": "delete" })); + assert!(r.is_err(), "{r:?}"); + s.execute("refclose", &json!({ "save": false })).unwrap(); + // OOPS still has D, the last erase that stood. + assert_eq!(oops(&mut s), json!({ "restored": 1 })); + assert!(s.doc().unwrap().entity(hs[3]).is_some()); +} + +#[test] +fn an_erase_in_the_block_editor_comes_back_there() { + let (mut s, hs) = drawing(); + s.execute("block", &json!({ "name": "BK", "base": [0, 0], "handles": [hs[0].hex(), hs[1].hex()], "keep": "retain" })).unwrap(); + s.execute("bedit", &json!({ "name": "BK" })).unwrap(); + let inside = handles(&s); + assert_eq!(inside.len(), 2); + s.execute("erase", &json!({ "handles": [inside[0].hex()] })).unwrap(); + assert_eq!(oops(&mut s), json!({ "restored": 1 })); + assert_eq!(handles(&s), inside); +} From fa03bf2a29b714e0832a120caa25b871e5f147d2 Mon Sep 17 00:00:00 2001 From: andrewbrake Date: Mon, 2 Nov 2026 23:31:18 +1300 Subject: [PATCH 042/100] feat(engine): INSERT of a drawing file keeps an attribute definition's field (#2619) * feat(engine): INSERT of a drawing file keeps an attribute definition's field The attribute definitions of a drawing file read by INSERT name=file lost their extension dictionary, so the ACAD_FIELD dictionary and the FIELD objects of an attribute whose value is a field (the file name in a title block) were gone from the block. adopt() now copies that object tree to the drawing under new handles, with the FIELD class and a field list if the drawing has none. Co-Authored-By: Claude Sonnet 5.5 * foreign: index the kept objects once when walking and copying a dictionary tree A file can keep many objects; a linear lookup per pointer grew with their product. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: andrewbrake <6925387+andrewbrake@users.noreply.github.com> Co-authored-by: Claude Sonnet 5.5 Co-authored-by: Brandon Thomas --- crates/doc/src/foreign.rs | 88 +++++++++++++++- crates/engine/src/cmd/blocks/insert_file.rs | 17 +++- crates/engine/tests/insert_file_field.rs | 107 ++++++++++++++++++++ 3 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 crates/engine/tests/insert_file_field.rs diff --git a/crates/doc/src/foreign.rs b/crates/doc/src/foreign.rs index e0ebb81a4..d732df261 100644 --- a/crates/doc/src/foreign.rs +++ b/crates/doc/src/foreign.rs @@ -6,7 +6,7 @@ //! named object dictionary, a table record, a block, an entity); references to objects that are //! gone are dropped. Commands don't edit it. -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use crate::{Handle, RawTag}; @@ -94,3 +94,89 @@ impl Foreign { self.data_store.iter().filter(|t| t.code == 320).filter_map(|t| Handle::parse_hex(t.value.trim())).collect() } } + +impl Foreign { + /// The kept objects by handle (the first of any that share one). + fn object_index(&self) -> BTreeMap { + let mut m = BTreeMap::new(); + for o in &self.objects { + m.entry(o.handle).or_insert(o); + } + m + } + + /// The kept object `root` and the kept objects it owns, directly or not (dictionary entries + /// and child pointers, groups 350 and 360), `root` first. + pub fn tree(&self, root: Handle) -> Vec { + // Indexed once: a file can keep many objects, and a lookup per pointer would grow with + // their product. + let by_handle = self.object_index(); + let mut out = vec![root]; + let mut seen = BTreeSet::from([root]); + let mut i = 0; + while let Some(&h) = out.get(i) { + i += 1; + let Some(o) = by_handle.get(&h) else { continue }; + for c in o.tags.iter().filter(|t| matches!(t.code, 350 | 360)).filter_map(|t| Handle::parse_hex(t.value.trim())) { + if out.len() < 4096 && by_handle.contains_key(&c) && seen.insert(c) { + out.push(c); + } + } + } + out + } + + /// Copy the objects of `tree` (see [`Foreign::tree`]) of `src` into this one under the handles + /// `fresh` (the same order; two more follow for a field list), `owner` taking the place of the + /// root's owner. References to objects outside the tree become null. Fields keep a field + /// list: this one gets a copy of the source's if it has none. + pub fn adopt_tree(&mut self, src: &Foreign, tree: &[Handle], fresh: &[Handle], owner: Handle) { + if fresh.len() < tree.len() + 2 { + return; + } + let map: BTreeMap = tree.iter().copied().zip(fresh.iter().copied()).collect(); + let by_handle = src.object_index(); + let mut kinds = Vec::new(); + for (n, h) in tree.iter().enumerate() { + let Some(o) = by_handle.get(h) else { continue }; + let mut c = (*o).clone(); + let Some(&new) = fresh.get(n) else { continue }; + c.handle = new; + for t in &mut c.tags { + if t.code == 5 { + t.value = new.hex(); + } else if matches!(t.code, 320..=369 | 390..=399) { + let to = Handle::parse_hex(t.value.trim()).and_then(|h| map.get(&h).copied()).or((n == 0 && t.code == 330).then_some(owner)); + t.value = to.map_or_else(|| "0".to_string(), Handle::hex); + } + } + kinds.push(c.kind.clone()); + self.objects.push(c); + } + if let Some(list) = src.objects.iter().find(|o| o.kind == "FIELDLIST").filter(|_| !self.objects.iter().any(|o| o.kind == "FIELDLIST")) { + let (Some(&h), Some(&nod)) = (fresh.get(tree.len()), fresh.get(tree.len() + 1)) else { return }; + let mut c = list.clone(); + c.handle = h; + for t in &mut c.tags { + match t.code { + 5 => t.value = h.hex(), + 330 => t.value = "0".to_string(), + _ => {} + } + } + if let Some(t) = c.tags.iter_mut().find(|t| t.code == 330) { + t.value = nod.hex(); + } + self.targets.insert(nod, ("NOD".to_string(), String::new())); + self.dictionary.push(("ACAD_FIELDLIST".to_string(), h)); + kinds.push(c.kind.clone()); + self.objects.push(c); + } + for k in kinds { + let named = |c: &Vec| c.iter().any(|t| t.code == 1 && t.value.eq_ignore_ascii_case(&k)); + if !self.classes.iter().any(named) { + self.classes.extend(src.classes.iter().filter(|c| named(c)).cloned()); + } + } + } +} diff --git a/crates/engine/src/cmd/blocks/insert_file.rs b/crates/engine/src/cmd/blocks/insert_file.rs index 1a90502b5..a00dbc64c 100644 --- a/crates/engine/src/cmd/blocks/insert_file.rs +++ b/crates/engine/src/cmd/blocks/insert_file.rs @@ -109,7 +109,7 @@ fn define_from(s: &mut Session, src: &Drawing, name: &str) -> Result { nb.name = to.clone(); nb.entities = Default::default(); for e in b.entities.iter() { - let e = adopt(&mut d, e, &renames); + let e = adopt(&mut d, src, e, &renames); nb.entities.push(e); } d.blocks.insert(to.clone(), Arc::new(nb)); @@ -118,7 +118,7 @@ fn define_from(s: &mut Session, src: &Drawing, name: &str) -> Result { b.base = src.header.point("INSBASE").filter(|p| p.x.is_finite() && p.y.is_finite() && p.z.is_finite()).unwrap_or_default(); let mut ents = Vec::new(); for e in src.model.iter() { - let e = adopt(&mut d, e, &renames); + let e = adopt(&mut d, src, e, &renames); d.ensure_layer(&e.common.layer); ents.push(e); } @@ -183,9 +183,20 @@ fn block_names(d: &Drawing, src: &Drawing, defining: &str) -> BTreeMap) -> Entity { +fn adopt(d: &mut Drawing, src: &Drawing, e: &Entity, renames: &BTreeMap) -> Entity { let mut e = e.clone(); + let old = e.handle; e.handle = d.new_handle(); + // An attribute definition keeps its field: its extension dictionary and the objects in it. + if matches!(e.kind, EntityKind::AttDef(_)) + && let Some(root) = src.foreign.entities.get(&old).and_then(|x| x.xdict) + { + let tree = src.foreign.tree(root); + let fresh: Vec = (0..tree.len() + 2).map(|_| d.new_handle()).collect(); + let foreign = Arc::make_mut(&mut d.foreign); + foreign.adopt_tree(&src.foreign, &tree, &fresh, e.handle); + foreign.entities.entry(e.handle).or_default().xdict = fresh.first().copied(); + } match &mut e.kind { EntityKind::Insert(i) => { if let Some(n) = renames.get(&i.block) { diff --git a/crates/engine/tests/insert_file_field.rs b/crates/engine/tests/insert_file_field.rs new file mode 100644 index 000000000..1365f88b5 --- /dev/null +++ b/crates/engine/tests/insert_file_field.rs @@ -0,0 +1,107 @@ +//! `INSERT name=file` keeps the field of an attribute definition: the definition's extension +//! dictionary and the objects in it come along under new handles, so the attributes the block's +//! references get can carry the field when the drawing is saved. + +use std::sync::Arc; + +use cadcraft_engine::Session; +use cadcraft_engine::cmd::file::{IoHooks, set_io}; +use cadcraft_engine::doc::foreign::{ForeignExtra, ForeignObject}; +use cadcraft_engine::doc::{Attrib, Drawing, Entity, EntityKind, Handle, RawTag, Text}; +use cadcraft_engine::geom::Vec3; +use serde_json::json; + +#[path = "support/temp_dir.rs"] +mod temp_dir; + +use temp_dir::TempDir; + +fn tag(code: i32, value: &str) -> RawTag { + RawTag { code, value: value.into() } +} + +fn object(handle: u64, kind: &str, tags: Vec) -> ForeignObject { + ForeignObject { handle: Handle(handle), kind: kind.into(), tags } +} + +/// A drawing file in a test format: model space holds a definition NUMBER (handle 0x50) whose +/// extension dictionary 0x60 owns the ACAD_FIELD dictionary 0x61, which owns the field 0x62 with +/// a child field 0x63. Handles 0x61..0x63 stand for the file's own; a second, unrelated object +/// 0x70 is not part of that tree. The bytes `plain` give the same definition without any of it. +fn read(bytes: &[u8], _name: &str) -> Result { + let mut d = Drawing::new_imperial(); + let text = Text { + insert: Vec3::ZERO, + align_pt: None, + height: 2.0, + value: "TEMPLATE".into(), + rotation: 0.0, + width_factor: 1.0, + oblique: 0.0, + style: "Standard".into(), + halign: Default::default(), + valign: Default::default(), + }; + let def = Attrib { + tag: "NUMBER".into(), + text, + invisible: false, + constant: false, + prompt: String::new(), + props: Default::default(), + multiline: None, + modes: Default::default(), + }; + d.model.push(Entity::new(Handle(0x50), EntityKind::AttDef(def))); + if bytes == b"plain" { + return Ok(d); + } + let f = Arc::make_mut(&mut d.foreign); + f.entities.insert(Handle(0x50), ForeignExtra { xdict: Some(Handle(0x60)), ..Default::default() }); + f.objects.push(object(0x60, "DICTIONARY", vec![tag(5, "60"), tag(330, "50"), tag(3, "ACAD_FIELD"), tag(360, "61")])); + f.objects.push(object(0x61, "DICTIONARY", vec![tag(5, "61"), tag(330, "60"), tag(3, "TEXT"), tag(360, "62")])); + f.objects.push(object(0x62, "FIELD", vec![tag(5, "62"), tag(330, "61"), tag(1, "_text"), tag(360, "63")])); + f.objects.push(object(0x63, "FIELD", vec![tag(5, "63"), tag(330, "62"), tag(1, "AcVar")])); + f.objects.push(object(0x70, "XRECORD", vec![tag(5, "70"), tag(330, "0")])); + f.classes.push(vec![tag(1, "FIELD"), tag(2, "AcDbField")]); + Ok(d) +} + +fn hooks() { + set_io(IoHooks { read, write: |_, _| Err("no writer in this test".into()), plot: None }); +} + +#[test] +fn the_definition_of_an_inserted_file_keeps_its_field() { + hooks(); + let dir = TempDir::new("insert-file-field"); + let path = dir.file("border.dxf", ""); + let mut s = Session::new(); + s.execute("insert", &json!({ "name": "BORDER", "file": path, "at": [0, 0], "attribs": { "NUMBER": "A-1" } })).unwrap(); + let d = s.doc().unwrap(); + let def = d.block("BORDER").and_then(|b| b.entities.iter().find(|e| matches!(e.kind, EntityKind::AttDef(_)))).expect("the definition"); + assert_ne!(def.handle, Handle(0x50), "the definition has a handle of this drawing"); + let root = d.foreign.entities.get(&def.handle).and_then(|x| x.xdict).expect("the definition has an extension dictionary"); + let tree = d.foreign.tree(root); + assert_eq!(tree.len(), 4, "the dictionaries and both fields, not the unrelated object"); + assert!(tree.iter().all(|h| ![0x60, 0x61, 0x62, 0x63, 0x70].contains(&h.0)), "the copies have new handles"); + // The root's owner is the definition, the others point at each other. + let owner = + |h: Handle| d.foreign.objects.iter().find(|o| o.handle == h).and_then(|o| o.tags.iter().find(|t| t.code == 330)).map(|t| t.value.clone()); + assert_eq!(owner(tree[0]), Some(def.handle.hex())); + assert_eq!(owner(tree[1]), Some(tree[0].hex())); + assert_eq!(owner(tree[3]), Some(tree[2].hex())); + assert!(d.foreign.classes.iter().any(|c| c.iter().any(|t| t.code == 1 && t.value == "FIELD")), "the field class comes along"); +} + +#[test] +fn a_definition_without_a_field_gets_none() { + hooks(); + let dir = TempDir::new("insert-file-nofield"); + let path = dir.file("plain.dxf", "plain"); + let mut s = Session::new(); + s.execute("insert", &json!({ "name": "PLAIN", "file": path, "at": [0, 0] })).unwrap(); + let d = s.doc().unwrap(); + assert!(d.block("PLAIN").is_some()); + assert!(d.foreign.is_empty(), "nothing is kept for a drawing without fields"); +} From ebc67fa81dccb0d89c157babb27e7d5c3a5c1177 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Mon, 2 Nov 2026 17:31:22 +0700 Subject: [PATCH 043/100] Dynamic blocks: stretch the points inside the frame, as AutoCAD does (#2627) * Dynamic blocks: stretch the points inside the frame, as AutoCAD does A dynamic block made by a third-party tool came out distorted when a distance changed: its stretch action has distance multiplier 0 with angle offset 1, and stored vertex lists that don't match its objects. apply turned the 0 into 1 but kept the angle (a 57 degree slant), and moved exactly the listed points. AutoCAD 2024, on a copy of that drawing, moved the block straight along the parameter, and every point of its lines and polylines exactly when it was inside the frame, whatever the lists said. So: - a zero multiplier moves as 1, without its angle offset; - a line's or lightweight polyline's moving points are those inside the frame where the dynamic block has them; a selected one wholly outside the frame moves whole, as in AutoCAD's own stretched copies; - a frame of fewer than 2 or more than 1,000 points keeps the stored lists, so a huge frame can't make every point test every edge. Against AutoCAD's own anonymous copies in two library drawings (556 references set back to their block's distances), 249 come back exactly on main and 258 with this change; none stops matching. Co-Authored-By: Claude Opus 5.5 * Dynamic blocks: a point on a polygon frame's edge is inside it in_frame counted a point on a two-corner frame's edge as inside, but the even-odd test of a polygon frame put one on its right edge outside. A line whose end lay there, with its other end outside, then had no point inside the frame and moved whole instead of its end alone. A polygon frame now counts a point on an edge (within 1e-9, relative to the edge's length) as inside too, as the two-corner frame does. Co-Authored-By: Claude Opus 5.5 * Dynamic blocks: frame edge tolerance by the edge's length; tilted polylines keep their lists A point counted as on a polygon frame's edge within 1e-9 * (1 + the edge's length), so never less than 1e-9 drawing units: in a tiny frame, a point visibly outside was taken as inside. The tolerance is now a billionth of the edge's length. A lightweight polyline's vertices are in its own plane. The reader flips a mirrored one (extrusion along -z) into the block's plane, but leaves a tilted one's in its OCS, which the frame (block coordinates) can't be tested against. A tilted polyline keeps the stored vertex list, as before the frame test. Co-Authored-By: Claude Opus 5.5 * dynblock: only an exactly untilted polyline is tested against the frame; a tilted one keeps its stored list Co-Authored-By: Claude Opus 5.5 * fmt Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Brandon Thomas --- crates/doc/src/dynblock.rs | 4 +- crates/doc/src/dynblock/apply.rs | 38 ++++++- crates/io/tests/it/dynblock_read.rs | 154 ++++++++++++++++++++++++++++ 3 files changed, 192 insertions(+), 4 deletions(-) diff --git a/crates/doc/src/dynblock.rs b/crates/doc/src/dynblock.rs index ef0a88be2..392861388 100644 --- a/crates/doc/src/dynblock.rs +++ b/crates/doc/src/dynblock.rs @@ -194,7 +194,9 @@ pub struct Action { #[derive(Clone, Debug, PartialEq, Serialize)] #[serde(tag = "type", rename_all = "camelCase", rename_all_fields = "camelCase")] pub enum ActionKind { - /// Moves the vertices inside `frame` of the objects listed (all of an object not listed). + /// Moves the points of its objects inside `frame`: a line's or polyline's points inside it + /// (the whole of one outside it), any other object whole. `vertices` are the point indices the + /// file lists for each object, used only when the frame can't be tested (see `apply`). Stretch { frame: Vec, #[serde(serialize_with = "hex_vertices")] diff --git a/crates/doc/src/dynblock/apply.rs b/crates/doc/src/dynblock/apply.rs index ed5281bf6..d5651faac 100644 --- a/crates/doc/src/dynblock/apply.rs +++ b/crates/doc/src/dynblock/apply.rs @@ -403,7 +403,20 @@ fn move_entity(e: &mut Entity, vertices: Option<&[i32]>, v: Vec2) { } } -/// Whether point `p` lies inside a stretch frame (two corners, or a polygon). +/// Most points of a stretch frame whose objects' points are tested against it one by one. A frame +/// is two corners or a short polygon; testing every point of every object against every edge of a +/// longer one would grow with their product, so the action's own vertex lists are used instead. +const MAX_FRAME_POINTS: usize = 1000; + +/// The indices of `points` inside stretch frame `frame`, or `None` when none is: an object the +/// action selects that lies outside its frame moves whole. +fn inside(frame: &[Vec2], points: impl Iterator) -> Option> { + let idx: Vec = points.enumerate().filter(|(_, p)| in_frame(frame, *p)).filter_map(|(i, _)| i32::try_from(i).ok()).collect(); + (!idx.is_empty()).then_some(idx) +} + +/// Whether point `p` lies inside a stretch frame (two corners, or a polygon). A point on its edge is +/// inside, for a polygon as for two corners: within a billionth of the edge's length of it. fn in_frame(frame: &[Vec2], p: Vec2) -> bool { match frame { [a, b] => p.x >= a.x.min(b.x) && p.x <= a.x.max(b.x) && p.y >= a.y.min(b.y) && p.y <= a.y.max(b.y), @@ -412,6 +425,10 @@ fn in_frame(frame: &[Vec2], p: Vec2) -> bool { let mut j = f.len() - 1; for i in 0..f.len() { let (Some(pi), Some(pj)) = (f.get(i), f.get(j)) else { break }; + let edge = cadcraft_geom::Line { a: *pj, b: *pi }; + if edge.dist(p) <= 1e-9 * pi.dist(*pj) { + return true; + } if (pi.y > p.y) != (pj.y > p.y) && p.x < (pj.x - pi.x) * (p.y - pi.y) / (pj.y - pi.y) + pi.x { inside = !inside; } @@ -464,7 +481,10 @@ fn apply( _ => 0.0, }) }; - let mult = if mult == 0.0 { 1.0 } else { mult }; + // A zero multiplier would move nothing. AutoCAD 2024 moves such an action by the + // parameter's change and leaves its angle offset out (measured on a stretch action written + // with multiplier 0, angle offset 1 and group 280 = 1). + let (mult, angle) = if mult == 0.0 { (1.0, 0.0) } else { (mult, angle) }; let v = Vec2::new(comp(0), comp(1)).rotate(angle) * mult; if !(v.x.is_finite() && v.y.is_finite()) || (v.x == 0.0 && v.y == 0.0) { continue; @@ -473,6 +493,8 @@ fn apply( // this action's selection, so a selection that repeats a handle does not re-scan the // vertex list and re-clone its index vector per occurrence (CWE-400). let vtx_by_handle: HashMap> = vertices.iter().rev().map(|(vh, i)| (*vh, i)).collect(); + // Whether the frame (two corners or a polygon) is tested point by point. + let framed = (2..=MAX_FRAME_POINTS).contains(&frame.len()); let mut handled: HashSet = HashSet::new(); for h in &a.selection { if !handled.insert(*h) { @@ -502,7 +524,17 @@ fn apply( continue; } let Some(c) = copy_of(h) else { continue }; - let idx = vtx_by_handle.get(h).map(|i| (*i).clone()); + // The points of a line or polyline that move are those inside the frame where the + // dynamic block has them, all of them when none is, as AutoCAD 2024 moves them. It + // doesn't go by the vertex lists the action stores, which a block from another program + // can get wrong. A polyline's vertices are in its own plane: in the block's when its + // extrusion is along z (a mirrored one is read flipped into it), so a tilted one keeps + // the stored list. + let idx = match def.entities.get(*h).map(|e| (&e.kind, e.common.extrusion)) { + Some((EntityKind::Line(l), _)) if framed => inside(frame, [l.a.xy(), l.b.xy()].into_iter()), + Some((EntityKind::LwPolyline(pl), n)) if framed && n.x == 0.0 && n.y == 0.0 => inside(frame, pl.vertices.iter().map(|v| v.p)), + _ => vtx_by_handle.get(h).map(|i| (*i).clone()), + }; moves.entry(c).or_default().push((idx, v)); } } diff --git a/crates/io/tests/it/dynblock_read.rs b/crates/io/tests/it/dynblock_read.rs index d265f9e70..bd9f2d1e1 100644 --- a/crates/io/tests/it/dynblock_read.rs +++ b/crates/io/tests/it/dynblock_read.rs @@ -406,6 +406,160 @@ fn parameters_in_a_stretch_selection_are_carried_along() { assert_eq!(tags[grip + 1..grip + 3], [(300, "GRIPLOC".to_string()), (11, "-15".to_string())]); } +/// A reference to Door at the origin with Door's own values, in a drawing read from `src`. +fn door_reference(src: &str) -> (cadcraft_doc::Drawing, Handle) { + let mut d = read_dxf(dxf(src).as_bytes()).unwrap(); + let ins = cadcraft_doc::Insert { + block: "Door".into(), + insert: cadcraft_doc::geom::Vec3::new(0.0, 0.0, 0.0), + scale: cadcraft_doc::geom::Vec3::new(1.0, 1.0, 1.0), + rotation: 0.0, + attribs: Vec::new(), + cols: 1, + rows: 1, + col_spacing: 0.0, + row_spacing: 0.0, + }; + let r = d.add(&cadcraft_doc::Space::Model, cadcraft_doc::Common::default(), cadcraft_doc::EntityKind::Insert(ins)).unwrap(); + (d, r) +} + +/// The points of the lines and polylines of reference `r`'s block, in its order. +fn shapes(d: &cadcraft_doc::Drawing, r: Handle) -> Vec> { + let b = d.block(&block_of(d, &r.hex())).unwrap(); + b.entities + .iter() + .filter_map(|e| match &e.kind { + cadcraft_doc::EntityKind::Line(l) => Some(vec![(l.a.x, l.a.y), (l.b.x, l.b.y)]), + cadcraft_doc::EntityKind::LwPolyline(p) => Some(p.vertices.iter().map(|v| (v.p.x, v.p.y)).collect()), + _ => None, + }) + .collect() +} + +#[test] +fn a_stretch_moves_the_points_inside_its_frame_not_the_listed_ones() { + // The stretch (frame (34,-2)-(38,2)) also acts on line 32, which is all outside the frame, and + // on a polyline whose last two vertices are inside it; and the vertex lists it stores are + // wrong for all three. AutoCAD 2024 moves the points inside the frame, measured on a dynamic + // block from another program whose lists didn't match its objects: every point of its lines and + // polylines moved exactly when it was inside the frame, whatever the lists said. A selected + // line or polyline all outside the frame moves whole, as in AutoCAD's own stretched copies. + let polyline = "0 LWPOLYLINE\n5 33\n330 1D\n100 AcDbEntity\n8 0\n100 AcDbPolyline\n90 3\n70 0\n10 20\n20 1\n10 36\n20 1\n10 36\n20 -1\n"; + let src = fixture() + .replace("71 1\n330 30\n1010 40", "71 3\n330 30\n330 32\n330 33\n1010 40") + .replace("73 1\n331 30\n74 1\n94 1\n75 0", "73 3\n331 30\n74 1\n94 0\n331 32\n74 1\n94 1\n331 33\n74 1\n94 0\n75 0") + .replace("21 25\n31 0\n0 ENDBLK\n", &format!("21 25\n31 0\n{polyline}0 ENDBLK\n")); + let (mut d, r) = door_reference(&src); + let ActionKind::Stretch { vertices, .. } = &dynamic_block(&d, "Door").unwrap().actions[0].kind else { panic!("a stretch action") }; + assert_eq!(vertices, &[(h("30"), vec![0]), (h("32"), vec![1]), (h("33"), vec![0])]); + cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap(); + let want: Vec> = vec![ + vec![(0.0, 0.0), (30.0, 0.0)], + vec![(0.0, 0.0), (0.0, 36.0)], + vec![(-6.0, 0.0), (19.0, 25.0)], + vec![(20.0, 1.0), (30.0, 1.0), (30.0, -1.0)], + ]; + assert_eq!(shapes(&d, r), want); +} + +#[test] +fn a_zero_distance_multiplier_moves_as_one_without_its_angle_offset() { + // A stretch written with multiplier 0 and angle offset 1 (radian), and group 280 = 1. AutoCAD + // 2024 moved such an action by the parameter's change, straight along it. + let src = fixture().replace("140 1\n141 0\n280 0", "140 0\n141 1\n280 1"); + assert_ne!(src, fixture()); + let (mut d, r) = door_reference(&src); + let ActionKind::Stretch { multiplier, angle_offset, .. } = &dynamic_block(&d, "Door").unwrap().actions[0].kind else { + panic!("a stretch action") + }; + assert_eq!((*multiplier, *angle_offset), (0.0, 1.0)); + cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap(); + assert_eq!(shapes(&d, r)[0], [(0.0, 0.0), (30.0, 0.0)]); +} + +#[test] +fn a_point_on_the_edge_of_a_polygon_frame_is_inside_it() { + // Line 30 ends at (36, 0), and its start is outside every frame here. On the edge of the frame + // (34,-2)-(36,2) given as two corners, the end is inside and moves alone. The same frame as a + // polygon, and triangles whose slanted edge passes through (36, 0), exactly or up to rounding, + // take it the same way. + let frames = [ + "72 2\n1011 34\n1021 -2\n1011 36\n1021 2\n", + "72 4\n1011 34\n1021 -2\n1011 36\n1021 -2\n1011 36\n1021 2\n1011 34\n1021 2\n", + "72 3\n1011 34\n1021 -2\n1011 38\n1021 2\n1011 34\n1021 2\n", + "72 3\n1011 35.7\n1021 -0.3\n1011 36.7\n1021 0.7\n1011 30\n1021 10\n", + ]; + for frame in frames { + let src = fixture().replace("72 2\n1011 34\n1021 -2\n1011 38\n1021 2\n", frame); + assert_ne!(src, fixture()); + let (mut d, r) = door_reference(&src); + cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap(); + assert_eq!(shapes(&d, r)[0], [(0.0, 0.0), (30.0, 0.0)], "{frame}"); + } +} + +#[test] +fn a_point_just_outside_a_tiny_frame_is_outside_it() { + // A frame 1e-8 across whose right edge is 5e-10 short of line 30's end (36, 0): the end is + // outside, so the line, with no point inside, moves whole. An edge's tolerance is a billionth of + // its length, not of a drawing unit. + let frame = "72 4\n1011 35.99999999\n1021 -1e-8\n1011 35.9999999995\n1021 -1e-8\n1011 35.9999999995\n1021 1e-8\n1011 35.99999999\n1021 1e-8\n"; + let src = fixture().replace("72 2\n1011 34\n1021 -2\n1011 38\n1021 2\n", frame); + assert_ne!(src, fixture()); + let (mut d, r) = door_reference(&src); + cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap(); + assert_eq!(shapes(&d, r)[0], [(-6.0, 0.0), (30.0, 0.0)]); +} + +#[test] +fn a_tilted_polyline_keeps_the_stored_vertex_list() { + // Polyline 34 lies in a plane tilted about the y axis (extrusion (1,0,0)): its vertices are in + // that plane's coordinates, not the block's, so the frame isn't tested against them and the + // stored list (vertex 0) is used, as before. Polyline 35 is mirrored (extrusion (0,0,-1)): it + // is read flipped into the block's plane, (-20, 1) as (20, 1), so the frame is tested. + let polyline = |h: &str, [x0, x1]: [i32; 2], [ex, ey, ez]: [i32; 3]| { + format!( + "0 LWPOLYLINE\n5 {h}\n330 1D\n100 AcDbEntity\n8 0\n100 AcDbPolyline\n90 2\n70 0\n10 {x0}\n20 1\n10 {x1}\n20 1\n210 {ex}\n220 {ey}\n230 {ez}\n" + ) + }; + let src = fixture() + .replace("71 1\n330 30\n1010 40", "71 3\n330 30\n330 34\n330 35\n1010 40") + .replace("73 1\n331 30\n74 1\n94 1\n75 0", "73 3\n331 30\n74 1\n94 1\n331 34\n74 1\n94 0\n331 35\n74 1\n94 0\n75 0") + .replace( + "21 25\n31 0\n0 ENDBLK\n", + &format!("21 25\n31 0\n{}{}0 ENDBLK\n", polyline("34", [20, 36], [1, 0, 0]), polyline("35", [-20, -36], [0, 0, -1])), + ); + let (mut d, r) = door_reference(&src); + cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap(); + let s = shapes(&d, r); + assert_eq!(s[3], [(14.0, 1.0), (36.0, 1.0)]); + assert_eq!(s[4], [(20.0, 1.0), (30.0, 1.0)]); +} + +#[test] +fn a_frame_with_too_many_points_to_test_one_by_one_keeps_the_stored_lists() { + // The same frame as a polygon of `n` points (its four corners, the last repeated). Up to 1000 + // points each point of the objects is tested against it; past that the stored list (here the + // line's start, outside the frame) is used, so a huge frame doesn't make every point test every + // edge. + let frame = |n: usize| { + let mut f = format!("72 {n}\n1011 34\n1021 -2\n1011 38\n1021 -2\n1011 38\n1021 2\n"); + f.push_str(&"1011 34\n1021 2\n".repeat(n - 3)); + f + }; + for (n, want) in [(1000, [(0.0, 0.0), (30.0, 0.0)]), (1001, [(-6.0, 0.0), (36.0, 0.0)])] { + let src = fixture() + .replace("72 2\n1011 34\n1021 -2\n1011 38\n1021 2\n", &frame(n)) + .replace("73 1\n331 30\n74 1\n94 1\n75 0", "73 1\n331 30\n74 1\n94 0\n75 0"); + let (mut d, r) = door_reference(&src); + let ActionKind::Stretch { frame, .. } = &dynamic_block(&d, "Door").unwrap().actions[0].kind else { panic!("a stretch action") }; + assert_eq!(frame.len(), n); + cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap(); + assert_eq!(shapes(&d, r)[0], want, "{n} frame points"); + } +} + /// The object entry `key` of dictionary `dict` refers to. fn dict_entry(d: &cadcraft_doc::Drawing, dict: Handle, key: &str) -> Option { let o = d.foreign.objects.iter().find(|o| o.handle == dict)?; From 3146b4765665eafe0e4c7d1c8d47a367140468c7 Mon Sep 17 00:00:00 2001 From: austin62jake Date: Mon, 2 Nov 2026 15:25:41 -0600 Subject: [PATCH 044/100] fix(engine): WBLOCK writes only what the block or selection uses (#2423) (#2621) * fix(engine): WBLOCK writes only what the block or selection uses (#2423) WBLOCK of a block, or of selected objects, copied the source drawing's whole layer, linetype, style and block tables into the new file, so a 5-object symbol carried 40 unused layers and 20 blocks. The written drawing is now purged of what nothing in it uses, as AutoCAD's WBLOCK writes it: purge::purge_unused runs PURGE All's scan on a plain Drawing until a pass removes nothing (a block can free its layer), keeping the defaults and the current ones. Writing the entire drawing is unchanged. Co-Authored-By: Claude Opus 5.5 * fix(engine): PURGE's scan counts leaders' dimension styles and styles used by other styles From the review of #2621: PURGE (and now WBLOCK, which runs its scan) took as unused a dimension style only a LEADER used, a linetype only a dimension style or a dimension's override named (DIMLTYPE, DIMLTEX1, DIMLTEX2), and a text style only a complex linetype's text used, and removed them while the objects still named them. The scan counts all three now; text_styles_in_use, which STYLE's delete also asks, includes complex linetypes' text styles. Co-Authored-By: Claude Opus 5.5 * fix(engine): PURGE's scan counts multiline styles' linetypes and table styles' text styles From the review of #2621: a linetype only a multiline style's element used, and a text style only a table style's cell style used, were taken as unused and purged while the styles still named them. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: austin62jake <340374338+austin62jake@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/blocks/purge.rs | 52 +++++++- crates/engine/src/cmd/blocks/wblock.rs | 5 +- crates/engine/src/cmd/props.rs | 5 +- crates/engine/tests/wblock_purge.rs | 165 +++++++++++++++++++++++++ 4 files changed, 224 insertions(+), 3 deletions(-) create mode 100644 crates/engine/tests/wblock_purge.rs diff --git a/crates/engine/src/cmd/blocks/purge.rs b/crates/engine/src/cmd/blocks/purge.rs index 68aea3c36..b91507b15 100644 --- a/crates/engine/src/cmd/blocks/purge.rs +++ b/crates/engine/src/cmd/blocks/purge.rs @@ -197,7 +197,21 @@ pub(in crate::cmd) fn candidates(d: &Drawing, kind: Kind) -> Vec { .collect() } Kind::Linetypes => { - let used = lower(&mut entities(d).map(|e| e.common.linetype.clone()).chain(d.layers.iter().map(|l| l.linetype.clone()))); + // Objects and layers use linetypes, and so do dimension styles and a dimension's own + // overrides (DIMLTYPE, DIMLTEX1, DIMLTEX2), and multiline styles' elements. + let styles = d.dim_styles.iter().flat_map(|s| [s.dim_linetype.clone(), s.ext_linetype1.clone(), s.ext_linetype2.clone()]); + let overrides = entities(d).filter_map(|e| if let EntityKind::Dimension(dm) = &e.kind { Some(dm) } else { None }).flat_map(|dm| { + ["dimLinetype", "extLinetype1", "extLinetype2"].into_iter().filter_map(|k| dm.overrides.get(k)?.as_str().map(str::to_string)) + }); + let mlines = d.mline_styles.iter().flat_map(|s| s.elements.iter().map(|el| el.linetype.clone())); + let used = lower( + &mut entities(d) + .map(|e| e.common.linetype.clone()) + .chain(d.layers.iter().map(|l| l.linetype.clone())) + .chain(styles) + .chain(overrides) + .chain(mlines), + ); let cur = d.header.str("CELTYPE", "ByLayer"); d.linetypes .iter() @@ -218,6 +232,7 @@ pub(in crate::cmd) fn candidates(d: &Drawing, kind: Kind) -> Vec { let used = lower(&mut entities(d).filter_map(|e| match &e.kind { EntityKind::Dimension(dm) => Some(dm.style.clone()), EntityKind::Tolerance(t) => Some(t.style.clone()), + EntityKind::Leader(l) => Some(l.style.clone()), _ => None, })); let cur = d.header.str("DIMSTYLE", "Standard"); @@ -299,6 +314,41 @@ fn retain(v: &mut Vec, name: impl Fn(&T) -> &String, n: &str) -> bool { v.len() != before } +/// Most passes [`purge_unused`] makes: each can free what the last one's objects used (a nested +/// block, its layer, that layer's linetype). +const MAX_PASSES: usize = 64; + +/// Remove every named object drawing `d` doesn't use, as PURGE All would, nested ones included; +/// the defaults and the current ones stay. For a drawing being written (WBLOCK's), not the one +/// being edited: nothing goes through the session or its undo. +pub(in crate::cmd) fn purge_unused(d: &mut Drawing) { + for _ in 0..MAX_PASSES { + let mut removed = false; + for kind in ALL { + for name in candidates(d, *kind) { + removed |= match kind { + Kind::Blocks => { + let key = d.blocks.keys().find(|k| k.eq_ignore_ascii_case(&name)).cloned(); + key.is_some_and(|k| d.blocks.remove(&k).is_some()) + } + Kind::Layers => retain(&mut d.layers, |l| &l.name, &name), + Kind::TextStyles => retain(&mut d.text_styles, |t| &t.name, &name), + Kind::Linetypes => retain(&mut d.linetypes, |t| &t.name, &name), + Kind::Dimstyles => retain(&mut d.dim_styles, |t| &t.name, &name), + Kind::MleaderStyles => retain(&mut d.mleader_styles, |t| &t.name, &name), + Kind::MlineStyles => retain(&mut d.mline_styles, |t| &t.name, &name), + Kind::TableStyles => retain(&mut d.table_styles, |t| &t.name, &name), + Kind::Groups => retain(&mut d.groups, |g| &g.name, &name), + Kind::ZeroLength | Kind::EmptyText | Kind::Absent | Kind::All => false, + }; + } + } + if !removed { + break; + } + } +} + /// Model-space and layout objects with no length, or text with no characters. fn degenerate(d: &Drawing, kind: Kind) -> Vec { let zero = |e: &EntityKind| match e { diff --git a/crates/engine/src/cmd/blocks/wblock.rs b/crates/engine/src/cmd/blocks/wblock.rs index 98ab0c344..bc2ff711a 100644 --- a/crates/engine/src/cmd/blocks/wblock.rs +++ b/crates/engine/src/cmd/blocks/wblock.rs @@ -97,7 +97,9 @@ fn write(s: &Session, cmd: &str, path: &str, what: Source) -> Result<(usize, usi Ok((d.model.len(), bytes.len())) } -/// A new drawing of `ents`, with `src`'s settings, styles and blocks; its insertion base 0,0. +/// A new drawing of `ents`, with `src`'s settings and the layers, linetypes, styles and blocks +/// they use, as AutoCAD's WBLOCK writes them (#2423): what nothing in it uses is purged, and +/// the defaults and the current ones stay. Its insertion base is 0,0. fn with_objects(src: &Drawing, ents: impl Iterator) -> Drawing { let mut d = Drawing::new_imperial(); d.header = src.header.clone(); @@ -115,6 +117,7 @@ fn with_objects(src: &Drawing, ents: impl Iterator) -> Drawing { e.handle = d.new_handle(); d.model.push(e); } + super::purge::purge_unused(&mut d); d } diff --git a/crates/engine/src/cmd/props.rs b/crates/engine/src/cmd/props.rs index 8afaddb3d..018e2dbf6 100644 --- a/crates/engine/src/cmd/props.rs +++ b/crates/engine/src/cmd/props.rs @@ -765,10 +765,13 @@ fn run_style_current(s: &mut Session, p: &Value) -> Result { ok() } -/// Text style names used by entities (in model, paper spaces and blocks) and dimension styles. +/// Text style names used by entities (in model, paper spaces and blocks), dimension, +/// multileader and table styles (their cell styles), and the text of complex linetypes. pub(super) fn text_styles_in_use(d: &cadcraft_doc::Drawing) -> Vec { let mut out: Vec = d.dim_styles.iter().map(|s| s.text_style.clone()).collect(); out.extend(d.mleader_styles.iter().map(|s| s.text_style.clone())); + out.extend(d.table_styles.iter().flat_map(|s| s.cell_styles.iter().map(|c| c.text_style.clone()))); + out.extend(d.linetypes.iter().flat_map(|lt| lt.pattern.iter().filter_map(|el| el.style.clone()))); let stores = std::iter::once(&d.model).chain(d.layouts.iter().map(|l| &l.entities)).chain(d.blocks.values().map(|b| &b.entities)); for st in stores { for e in st.iter() { diff --git a/crates/engine/tests/wblock_purge.rs b/crates/engine/tests/wblock_purge.rs new file mode 100644 index 000000000..3fc3bad5e --- /dev/null +++ b/crates/engine/tests/wblock_purge.rs @@ -0,0 +1,165 @@ +//! 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, 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 = 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::>(); + 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"); +} From a277346c2f31385e7a76ace2952641df33d03df0 Mon Sep 17 00:00:00 2001 From: austin62jake Date: Mon, 2 Nov 2026 15:25:46 -0600 Subject: [PATCH 045/100] fix(io): a viewport looks at its view target plus its centre (#2492) (#2508) * fix(io): a viewport looks at its view target plus its centre (#2492) A DXF stores a view's centre in display coordinates, whose origin is the view target (DXF Reference: VIEWPORT 12/22 and 17/27/37, VPORT 12/22 and 17/27/37, VIEW 10/20 and 12/22/32). The readers took the centre alone, so a file that keeps the location in the target with a centre of 0,0 (as ArchiCAD's AutoCAD 2018 export does) showed model point 0,0 in every layout viewport, on screen and when plotted. The VIEWPORT entity, VPORT tiles and named views now read the target plus the centre. A sum that overflows keeps the stored centre. The writers already store the whole location as the centre with a target of 0, so a save and reopen keeps the view, through DXF and DWG. Co-Authored-By: Claude Opus 5.5 * test(io): a viewport looking along another direction centres on its target Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: austin62jake <340374338+austin62jake@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- crates/io/src/dxf_read/common.rs | 9 ++ crates/io/src/dxf_read/entities.rs | 3 +- crates/io/src/dxf_read/tables.rs | 6 +- crates/io/tests/it/main.rs | 1 + crates/io/tests/it/view_target.rs | 181 +++++++++++++++++++++++++++++ 5 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 crates/io/tests/it/view_target.rs diff --git a/crates/io/src/dxf_read/common.rs b/crates/io/src/dxf_read/common.rs index e46f3c09d..1dbabf6e9 100644 --- a/crates/io/src/dxf_read/common.rs +++ b/crates/io/src/dxf_read/common.rs @@ -19,6 +19,15 @@ impl T<'_> { pub(super) fn p(&self, code: i32) -> Vec3 { Vec3::new(self.fd(code, 0.0), self.fd(code + 10, 0.0), self.fd(code + 20, 0.0)) } + /// A view's centre in the drawing. The DXF stores it in display coordinates (`center`), whose + /// origin is the view target (`target`), so the point in the middle of a plan view is their + /// sum: a file whose view centre is 0,0 keeps the location in the target (#2492). A sum that + /// overflows leaves the stored centre. + pub(super) fn view_center(&self, center: i32, target: i32) -> Vec2 { + let c = self.p(center).xy(); + let sum = c + self.p(target).xy(); + if sum.is_finite() { sum } else { c } + } pub(super) fn all_f(&self, code: i32) -> Vec { self.0.iter().filter(|t| t.code == code).map(Tag::f64).collect() } diff --git a/crates/io/src/dxf_read/entities.rs b/crates/io/src/dxf_read/entities.rs index 119d00bbd..980b1718c 100644 --- a/crates/io/src/dxf_read/entities.rs +++ b/crates/io/src/dxf_read/entities.rs @@ -246,7 +246,8 @@ pub(super) fn entity_with_table_budget(kind: &str, tags: &[Tag], table_cells_lef center: t.p(10), width: t.fd(40, 1.0), height: t.fd(41, 1.0), - view_center: Vec2::new(t.fd(12, 0.0), t.fd(22, 0.0)), + // 12/22 is in display coordinates, whose origin is the view target 17/27/37. + view_center: t.view_center(12, 17), view_height: t.fd(45, 1.0), id: t.i(69).unwrap_or(0) as u32, locked: t.i(90).unwrap_or(0) & 16384 != 0, diff --git a/crates/io/src/dxf_read/tables.rs b/crates/io/src/dxf_read/tables.rs index ee42b8a58..c914ea9f5 100644 --- a/crates/io/src/dxf_read/tables.rs +++ b/crates/io/src/dxf_read/tables.rs @@ -217,7 +217,8 @@ pub(super) fn tables(tags: &[Tag], d: &mut Drawing, rx: &mut Rx) { if name.is_empty() || !(h.is_finite() && w.is_finite() && h > 0.0 && w > 0.0) || d.views.len() >= MAX_OBJECTS { continue; } - let center = t.p(10).xy(); + // The view centre 10/20 is in display coordinates, whose origin is the target 12/22/32. + let center = t.view_center(10, 12); if !(center.x.is_finite() && center.y.is_finite()) { continue; } @@ -316,7 +317,8 @@ const MAX_VPORT_TILES: usize = 64; /// are fractions of the drawing area; unusable tiles are skipped. fn vport_record(d: &mut Drawing, t: &T) { let name = t.s(2).map(|n| n.trim().to_string()).unwrap_or_default(); - let (min, max, center) = (t.p(10).xy(), t.p(11).xy(), t.p(12).xy()); + // The view centre 12/22 is in display coordinates, whose origin is the view target 17/27/37. + let (min, max, center) = (t.p(10).xy(), t.p(11).xy(), t.view_center(12, 17)); let height = t.fd(40, 0.0); let unit = |v: f64| v.is_finite() && (-1e-9..=1.0 + 1e-9).contains(&v); let ok = [min.x, min.y, max.x, max.y].into_iter().all(unit) diff --git a/crates/io/tests/it/main.rs b/crates/io/tests/it/main.rs index 79c4e4b3e..aa77795fa 100644 --- a/crates/io/tests/it/main.rs +++ b/crates/io/tests/it/main.rs @@ -73,5 +73,6 @@ mod text_left_justify; mod tolerance_roundtrip; mod user_lin_pat_roundtrip; mod view_tables_roundtrip; +mod view_target; mod viewport_clip_roundtrip; mod xref_roundtrip; diff --git a/crates/io/tests/it/view_target.rs b/crates/io/tests/it/view_target.rs new file mode 100644 index 000000000..c6a2eea27 --- /dev/null +++ b/crates/io/tests/it/view_target.rs @@ -0,0 +1,181 @@ +//! A view's centre is stored in display coordinates, whose origin is the view target (DXF +//! Reference: VIEWPORT 12/22 and 17/27/37, VPORT 12/22 and 17/27/37, VIEW 10/20 and 12/22/32). +//! ArchiCAD's AutoCAD 2018 export puts each layout viewport's location in the target with a +//! centre of 0,0, so reading the centre alone showed model point 0,0 (#2492). + +use cadcraft_doc::geom::Vec2; +use cadcraft_doc::{Drawing, EntityKind, Viewport}; +use cadcraft_io::{read_dxf, write_dxf}; + +/// An ASCII DXF of `tags`. +fn dxf(tags: &[(i32, &str)]) -> String { + tags.iter().map(|(c, v)| format!("{c}\n{v}\n")).collect() +} + +/// A layout viewport with view centre `center` (12/22) and view target `target` (17/27/37). +fn with_viewport(center: [&str; 2], target: [&str; 3]) -> Drawing { + looking_along(center, target, ["0", "0", "1"]) +} + +/// The same, looking along view direction `dir` (16/26/36). +fn looking_along(center: [&str; 2], target: [&str; 3], dir: [&str; 3]) -> Drawing { + let tags: Vec<(i32, &str)> = vec![ + (0, "SECTION"), + (2, "ENTITIES"), + (0, "VIEWPORT"), + (5, "40"), + (67, "1"), + (8, "0"), + (10, "148.5"), + (20, "105"), + (30, "0"), + (40, "250"), + (41, "180"), + (68, "2"), + (69, "2"), + (12, center[0]), + (22, center[1]), + (16, dir[0]), + (26, dir[1]), + (36, dir[2]), + (17, target[0]), + (27, target[1]), + (37, target[2]), + (45, "48680.2"), + (0, "ENDSEC"), + (0, "EOF"), + ]; + read_dxf(dxf(&tags).as_bytes()).expect("read") +} + +fn viewports(d: &Drawing) -> Vec { + d.layouts + .iter() + .flat_map(|l| l.entities.iter()) + .filter_map(|e| match &e.kind { + EntityKind::Viewport(v) if v.id > 1 => Some(v.clone()), + _ => None, + }) + .collect() +} + +fn view_center(d: &Drawing) -> Vec2 { + let vs = viewports(d); + assert_eq!(vs.len(), 1, "{vs:?}"); + vs[0].view_center +} + +/// `a` and `b` agree to within the rounding of adding two decimals. +fn close(a: Vec2, b: Vec2) -> bool { + (a - b).len() < 1e-6 +} + +#[test] +fn a_viewport_looks_at_its_target_plus_its_centre() { + // The issue's file: centre 0,0, the facade in the target. + let c = view_center(&with_viewport(["0", "0"], ["230748.7", "-867.4", "0"])); + assert_eq!(c, Vec2::new(230748.7, -867.4)); + // Both set: their sum. A target alone of 0 leaves the centre as it is. + assert_eq!(view_center(&with_viewport(["10", "5"], ["100", "50", "7"])), Vec2::new(110.0, 55.0)); + assert_eq!(view_center(&with_viewport(["10", "5"], ["0", "0", "0"])), Vec2::new(10.0, 5.0)); +} + +/// CADCraft draws every viewport as a plan view (the view direction, 16/26/36, isn't applied). A +/// viewport looking along another direction is centred on what it looks at, its target, as +/// AutoCAD's view of it is; its centre is in that view's own axes, so only the target is exact. +#[test] +fn a_viewport_looking_along_another_direction_centres_on_its_target() { + let c = view_center(&looking_along(["0", "0"], ["500", "-200", "30"], ["1", "1", "1"])); + assert_eq!(c, Vec2::new(500.0, -200.0)); +} + +/// Saved and reopened, the viewport still looks at the same place: CADCraft writes the whole +/// location as the centre with a target of 0. +#[test] +fn the_view_survives_a_save_and_reopen() { + let d = with_viewport(["3", "4"], ["84672.2", "25851.5", "0"]); + let back = read_dxf(write_dxf(&d).as_bytes()).expect("reopen"); + let c = view_center(&back); + assert!(close(c, Vec2::new(84675.2, 25855.5)), "{c:?}"); +} + +/// A target coordinate that is not a finite number reads as 0, as any DXF real does, and a sum +/// that overflows leaves the centre. +#[test] +fn a_hostile_target_leaves_the_centre() { + for target in [["1e999", "0", "0"], ["nan", "0", "0"], ["1e308", "0", "0"]] { + assert_eq!(view_center(&with_viewport(["1e308", "2"], target)), Vec2::new(1e308, 2.0), "{target:?}"); + } + assert_eq!(view_center(&with_viewport(["1", "2"], ["inf", "5", "0"])), Vec2::new(1.0, 7.0)); +} + +/// The model viewport tiles of a saved configuration (VPORT 12/22 and 17/27/37) and named views +/// (VIEW 10/20 and 12/22/32) are read the same way. +#[test] +fn vport_tiles_and_named_views_add_their_targets() { + let tags: &[(i32, &str)] = &[ + (0, "SECTION"), + (2, "TABLES"), + (0, "TABLE"), + (2, "VPORT"), + (0, "VPORT"), + (2, "TWO"), + (70, "0"), + (10, "0"), + (20, "0"), + (11, "0.5"), + (21, "1"), + (12, "1"), + (22, "2"), + (17, "100"), + (27, "200"), + (37, "0"), + (40, "50"), + (41, "1"), + (0, "VPORT"), + (2, "TWO"), + (70, "0"), + (10, "0.5"), + (20, "0"), + (11, "1"), + (21, "1"), + (12, "0"), + (22, "0"), + (17, "-40"), + (27, "30"), + (37, "0"), + (40, "50"), + (41, "1"), + (0, "ENDTAB"), + (0, "TABLE"), + (2, "VIEW"), + (0, "VIEW"), + (2, "Facade"), + (70, "0"), + (40, "20"), + (41, "30"), + (10, "1"), + (20, "2"), + (11, "0"), + (21, "0"), + (31, "1"), + (12, "367097.3"), + (22, "-441.8"), + (32, "0"), + (0, "ENDTAB"), + (0, "ENDSEC"), + (0, "EOF"), + ]; + let d = read_dxf(dxf(tags).as_bytes()).expect("read"); + let two = d.vports.iter().find(|c| c.name.eq_ignore_ascii_case("TWO")).expect("configuration TWO"); + let centers: Vec = two.tiles.iter().map(|t| t.center).collect(); + assert_eq!(centers, [Vec2::new(101.0, 202.0), Vec2::new(-40.0, 30.0)]); + let facade = d.views.iter().find(|v| v.name == "Facade").expect("view Facade"); + assert!(close(facade.center, Vec2::new(367098.3, -439.8)), "{:?}", facade.center); + // And again after a save and reopen. + let back = read_dxf(write_dxf(&d).as_bytes()).expect("reopen"); + let two = back.vports.iter().find(|c| c.name.eq_ignore_ascii_case("TWO")).expect("configuration TWO"); + assert_eq!(two.tiles.iter().map(|t| t.center).collect::>(), centers); + let again = back.views.iter().find(|v| v.name == "Facade").map(|v| v.center).expect("view Facade"); + assert!(close(again, Vec2::new(367098.3, -439.8)), "{again:?}"); +} From c4ac8761e80df60d4bf9e9ab4ab91cc9c02c70f1 Mon Sep 17 00:00:00 2001 From: assamaral Date: Mon, 2 Nov 2026 18:25:51 -0300 Subject: [PATCH 046/100] fix(io): a ByBlock plot style (group 380 = 1) is written and read (#2242) (#2566) * test(io): a ByBlock plot style (group 380 = 1) survives a DXF round trip (#2242) Red: the writer leaves group 380 out and the reader maps it to ByLayer. Co-Authored-By: Claude Fable 5.1 * fix(io): a ByBlock plot style is group 380 = 1, written and read (#2242) The writer only knew named styles (group 390) and left ByBlock out like ByLayer; the reader mapped a record without 390 to ByLayer. Both now use the plot style name type. Co-Authored-By: Claude Fable 5.1 --------- Co-authored-by: Claude Fable 5.1 --- crates/io/src/dxf_read/entities.rs | 16 +++++++++++----- crates/io/src/dxf_write/common.rs | 4 ++++ crates/io/tests/it/plot_styles.rs | 13 +++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/io/src/dxf_read/entities.rs b/crates/io/src/dxf_read/entities.rs index 980b1718c..2011c3d63 100644 --- a/crates/io/src/dxf_read/entities.rs +++ b/crates/io/src/dxf_read/entities.rs @@ -411,20 +411,26 @@ pub(super) fn plot_style_names(secs: &[cadcraft_dxf::Section]) -> HashMap Option { let mut in_entity = false; + let mut by_block = false; for t in tags { if t.code == 100 { if in_entity { - return None; + break; } in_entity = t.str() == "AcDbEntity"; - } else if t.code == 390 && in_entity { - return rx.pstyles.get(&t.str().trim().to_ascii_uppercase()).cloned(); + } else if in_entity { + match t.code { + 390 => return rx.pstyles.get(&t.str().trim().to_ascii_uppercase()).cloned(), + 380 => by_block = t.i64() == 1, + _ => {} + } } } - None + by_block.then(|| "ByBlock".to_string()) } /// Dimension overrides and associativity, table references: data of an entity record that diff --git a/crates/io/src/dxf_write/common.rs b/crates/io/src/dxf_write/common.rs index 25d2bb074..ef2632665 100644 --- a/crates/io/src/dxf_write/common.rs +++ b/crates/io/src/dxf_write/common.rs @@ -101,6 +101,10 @@ pub(super) fn common_x(w: &mut W, e: &Entity, owner: &str, paper: bool, subclass if let Some(v) = transparency_440(e.common.transparency) { w.i(440, v); } + // ByBlock is the plot style name type 1 (ByLayer, 0, is the default and left out). + if e.common.plot_style.trim().eq_ignore_ascii_case("byblock") { + w.i(380, 1); + } // A named plot style (not ByLayer/ByBlock): its placeholder object. if let Some(h) = w.pstyles.get(&e.common.plot_style.trim().to_ascii_uppercase()).cloned() { w.s(390, h); diff --git a/crates/io/tests/it/plot_styles.rs b/crates/io/tests/it/plot_styles.rs index 679c167b9..73a5c8e5b 100644 --- a/crates/io/tests/it/plot_styles.rs +++ b/crates/io/tests/it/plot_styles.rs @@ -86,6 +86,19 @@ fn named_plot_styles_round_trip_through_the_plot_style_dictionary() { assert!(plain.contains("ACAD_PLOTSTYLENAME") && !plain.contains("CADCRAFT_PLOTSTYLES")); } +/// #2242: a ByBlock plot style is group 380 = 1 (AcDb::PlotStyleNameType); it was written like +/// ByLayer (no group at all) and read back as ByLayer. +#[test] +fn by_block_plot_style_round_trips() { + let mut d = Drawing::new_metric(); + let line = EntityKind::Line(Line { a: Vec3::new(0.0, 0.0, 0.0), b: Vec3::new(1.0, 0.0, 0.0) }); + let h = d.add(&Space::Model, Common { plot_style: "ByBlock".into(), ..Default::default() }, line).unwrap(); + let text = write_dxf(&d); + let lines: Vec<&str> = text.lines().collect(); + assert!(lines.windows(2).any(|w| w[0].trim() == "380" && w[1].trim() == "1"), "group 380 = 1 written"); + assert_eq!(read_dxf(text.as_bytes()).unwrap().entity(h).unwrap().common.plot_style, "ByBlock"); +} + #[test] fn pdf_applies_a_named_table_from_the_drawing() { use cadcraft_doc::{PlotStyle, PlotStyleTable}; From ab15e42c6c12049f2c68e1c2715ca7183075703b Mon Sep 17 00:00:00 2001 From: austin62jake Date: Mon, 2 Nov 2026 15:25:55 -0600 Subject: [PATCH 047/100] fix(io): a layout keeps its limits and paper image origin through a save (#2109) (#2623) Every LAYOUT was written with limits 0,0 to 12,9 (AcDbLayout groups 10/11) and paper image origin 0,0 (groups 148/149), whatever the file had, so another program drew the sheet elsewhere and a Layout plot came out shifted. Layout::limits and PageSetup::image_origin keep what the file had (finite, within 1e12) and a save writes it back; a layout without them writes the old defaults, which read back as none. Co-authored-by: austin62jake <340374338+austin62jake@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- crates/doc/src/tables.rs | 11 +++- crates/engine/src/cmd/layout.rs | 2 +- crates/io/src/dxf_read.rs | 3 +- crates/io/src/dxf_read/document.rs | 16 +++-- crates/io/src/dxf_read/objects.rs | 4 ++ crates/io/src/dxf_write/sections.rs | 17 +++-- crates/io/tests/it/layout_limits_roundtrip.rs | 64 +++++++++++++++++++ crates/io/tests/it/main.rs | 1 + 8 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 crates/io/tests/it/layout_limits_roundtrip.rs diff --git a/crates/doc/src/tables.rs b/crates/doc/src/tables.rs index a2c63ca6a..a5f02454f 100644 --- a/crates/doc/src/tables.rs +++ b/crates/doc/src/tables.rs @@ -799,6 +799,10 @@ pub struct PageSetup { /// The layout's paper units are millimetres (plot paper units 1 in DXF) whatever the /// drawing's MEASUREMENT says; otherwise paper units follow MEASUREMENT (#1058). pub paper_mm: bool, + /// The paper image origin (DXF groups 148/149, in plot paper units) as the file had it, so a + /// save keeps where the sheet sits; `None` for a page CADCraft made, written as 0,0 (#2109). + #[serde(skip_serializing_if = "Option::is_none")] + pub image_origin: Option, } impl Default for PageSetup { @@ -820,6 +824,7 @@ impl Default for PageSetup { show_plot_styles: false, plot_transparency: false, paper_mm: false, + image_origin: None, } } } @@ -835,11 +840,15 @@ pub struct Layout { /// Saved paper-space view (centre, height). #[serde(default)] pub view: Option<(Vec2, f64)>, + /// The layout's limits (LAYOUT groups 10/11, lower left and upper right) as the file had + /// them; `None` for a layout CADCraft made, written as 0,0 to 12,9 (#2109). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub limits: Option<(Vec2, Vec2)>, } impl Layout { pub fn new(name: &str, tab_order: u32) -> Self { - Layout { name: name.into(), tab_order, page: PageSetup::default(), entities: EntityStore::new(), view: None } + Layout { name: name.into(), tab_order, page: PageSetup::default(), entities: EntityStore::new(), view: None, limits: None } } } diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index a538d73b5..efe9bf13f 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -313,7 +313,7 @@ fn run_copy(s: &mut Session, p: &Value) -> Result { let h = d.new_handle(); store.push(Entity { handle: h, ..Entity::clone(e) }); } - d.layouts.push(Layout { name: to.clone(), tab_order: order, page: src.page, entities: store, view: src.view }); + d.layouts.push(Layout { name: to.clone(), tab_order: order, page: src.page, entities: store, view: src.view, limits: src.limits }); Ok(json!({ "name": to })) } diff --git a/crates/io/src/dxf_read.rs b/crates/io/src/dxf_read.rs index 341e72a77..2dcb14fc0 100644 --- a/crates/io/src/dxf_read.rs +++ b/crates/io/src/dxf_read.rs @@ -73,7 +73,8 @@ pub fn read(bytes: &[u8]) -> Result { type BlockDef = (String, Vec3, String, Vec<(Option, bool, Entity)>, i64); /// A layout read from OBJECTS: name, tab order, paper block handle and page setup. -type LayoutDef = (String, u32, String, PageSetup); +/// A LAYOUT object: name, tab order, block record, page setup and its limits (groups 10/11). +type LayoutDef = (String, u32, String, PageSetup, Option<(Vec2, Vec2)>); /// Import DXF sections while sharing one materialized-table-cell budget across blocks and entity sections. fn read_budgeted(bytes: &[u8], mut table_cells_left: usize) -> Result { diff --git a/crates/io/src/dxf_read/document.rs b/crates/io/src/dxf_read/document.rs index db935974a..6f61e3415 100644 --- a/crates/io/src/dxf_read/document.rs +++ b/crates/io/src/dxf_read/document.rs @@ -1,7 +1,7 @@ use super::common::T; use super::entities::{entity_extras, entity_plot_style, entity_with_table_budget, insert_attribs, polyline_entity}; use super::objects::{MAX_OBJECTS, Objects, page_setup}; -use super::{Block, BlockDef, Drawing, Entity, EntityKind, Handle, HashMap, Layout, LayoutDef, Rx, Tag, records}; +use super::{Block, BlockDef, Drawing, Entity, EntityKind, Handle, HashMap, Layout, LayoutDef, Rx, Tag, Vec2, records}; pub(super) fn parse_entities( recs: &[(String, Vec)], @@ -138,21 +138,29 @@ pub(super) fn read_objects(tags: &[Tag], objs: &mut Objects, layout_objs: &mut V // LAYOUT has two 330s: owner dict and the block record; take the last. let br = tg.iter().rfind(|x| x.code == 330).map(Tag::str).unwrap_or_default(); let plot = T(tg.get(..start).unwrap_or(&[])); - layout_objs.push((t.s(1).unwrap_or_default(), t.i(71).unwrap_or(0) as u32, br.to_ascii_uppercase(), page_setup(&plot))); + layout_objs.push((t.s(1).unwrap_or_default(), t.i(71).unwrap_or(0) as u32, br.to_ascii_uppercase(), page_setup(&plot), limits(&t))); } } } +/// A LAYOUT's limits (groups 10/20 and 11/21 after the AcDbLayout marker), when both corners +/// are there and usable, so a save writes back the file's own (#2109). The 0,0 to 12,9 a +/// layout without its own limits writes reads back as none. +fn limits(t: &T) -> Option<(Vec2, Vec2)> { + let corner = |x: i32, y: i32| Some(Vec2::new(t.f(x)?, t.f(y)?)).filter(|p| p.is_finite() && p.x.abs() < 1e12 && p.y.abs() < 1e12); + corner(10, 20).zip(corner(11, 21)).filter(|l| *l != (Vec2::ZERO, Vec2::new(12.0, 9.0))) +} + /// Add the drawing's layouts, and return the layout each paper-space block belongs to, and the /// layout that takes paper-space entities without an owner. pub(super) fn setup_layouts(d: &mut Drawing, layout_objs: &mut [LayoutDef], rx: &Rx) -> (HashMap, String) { let mut paper_block_for: HashMap = HashMap::new(); layout_objs.sort_by_key(|l| l.1); - for (name, order, br, page) in layout_objs.iter() { + for (name, order, br, page, limits) in layout_objs.iter() { if name.eq_ignore_ascii_case("Model") { continue; } - d.layouts.push(Layout { page: page.clone(), ..Layout::new(name, *order) }); + d.layouts.push(Layout { page: page.clone(), limits: *limits, ..Layout::new(name, *order) }); if let Some(bname) = rx.brs.get(br) { paper_block_for.insert(bname.to_ascii_uppercase(), name.clone()); } diff --git a/crates/io/src/dxf_read/objects.rs b/crates/io/src/dxf_read/objects.rs index 4d692ccee..359dc887a 100644 --- a/crates/io/src/dxf_read/objects.rs +++ b/crates/io/src/dxf_read/objects.rs @@ -274,6 +274,10 @@ pub(super) fn page_setup(t: &T) -> PageSetup { .into(); p.plot_window = [t.fd(48, 0.0), t.fd(49, 0.0), t.fd(140, 0.0), t.fd(141, 0.0)].map(|v| if v.is_finite() { v } else { 0.0 }); p.plot_style_table = t.s(7).unwrap_or_default(); + // Paper image origin: kept as the file has it, so a save puts the sheet back where it was (#2109). + // 0,0 is what a page without one writes, so it reads back as none. + p.image_origin = + t.f(148).zip(t.f(149)).map(|(x, y)| Vec2::new(x, y)).filter(|o| o.is_finite() && o.x.abs() < 1e12 && o.y.abs() < 1e12 && *o != Vec2::ZERO); p } diff --git a/crates/io/src/dxf_write/sections.rs b/crates/io/src/dxf_write/sections.rs index b2eff022d..0f93013a5 100644 --- a/crates/io/src/dxf_write/sections.rs +++ b/crates/io/src/dxf_write/sections.rs @@ -384,6 +384,8 @@ struct LayoutWriteData<'a> { flags: i64, page: &'a PageSetup, view: Option<(Vec2, f64)>, + /// The layout's limits as read (`None`: the default 0,0 to 12,9). + limits: Option<(Vec2, Vec2)>, } fn layout_obj(w: &mut W, context: LayoutWriteContext<'_>, data: LayoutWriteData<'_>) { @@ -448,14 +450,18 @@ fn layout_obj(w: &mut W, context: LayoutWriteContext<'_>, data: LayoutWriteData< w.s(7, &page.plot_style_table); w.i(75, if page.scale_to_fit { 0 } else { 16 }); w.f(147, scale); - w.f(148, 0.0); - w.f(149, 0.0); + // The paper image origin the file had (#2109), else 0,0. + let origin = page.image_origin.filter(|o| o.is_finite()).unwrap_or(Vec2::ZERO); + w.f(148, origin.x); + w.f(149, origin.y); w.s(100, "AcDbLayout"); w.s(1, name); w.i(70, flags); w.i(71, tab); - w.p2(10, Vec2::ZERO); - w.p2(11, Vec2::new(12.0, 9.0)); + // The layout's limits as the file had them (#2109), else 0,0 to 12,9. + let (lo, hi) = data.limits.filter(|(a, b)| a.is_finite() && b.is_finite()).unwrap_or((Vec2::ZERO, Vec2::new(12.0, 9.0))); + w.p2(10, lo); + w.p2(11, hi); w.p(12, Vec3::ZERO); w.p(14, Vec3::ZERO); w.p(15, Vec3::ZERO); @@ -524,7 +530,7 @@ fn write_layout_objects(w: &mut W, plan: &ObjectWritePlan<'_>) { layout_obj( w, LayoutWriteContext { drawing: d, dictionary: layout_dict }, - LayoutWriteData { handle: model_layout, name: "Model", tab: 0, block_record: ms_br, flags: 1, page: &model_page, view: None }, + LayoutWriteData { handle: model_layout, name: "Model", tab: 0, block_record: ms_br, flags: 1, page: &model_page, view: None, limits: None }, ); for (i, (_, brh, l)) in ps_brs.iter().enumerate() { let h = layout_handles.get(i).cloned().unwrap_or_default(); @@ -539,6 +545,7 @@ fn write_layout_objects(w: &mut W, plan: &ObjectWritePlan<'_>) { flags: 1, page: &l.page, view: l.view, + limits: l.limits, }, ); } diff --git a/crates/io/tests/it/layout_limits_roundtrip.rs b/crates/io/tests/it/layout_limits_roundtrip.rs new file mode 100644 index 000000000..31baea67d --- /dev/null +++ b/crates/io/tests/it/layout_limits_roundtrip.rs @@ -0,0 +1,64 @@ +//! A layout keeps its limits (LAYOUT groups 10/11) and its page's paper image origin +//! (groups 148/149) through a DXF and a DWG save, as the file had them, so another program puts +//! the sheet back where it was (#2109). A layout CADCraft made writes the defaults as before. + +use cadcraft_doc::geom::Vec2; +use cadcraft_doc::{Drawing, Layout}; +use cadcraft_io::{SaveOptions, Version, read, read_dxf, write_dxf, write_with}; + +/// A drawing whose layout has the limits and image origin of the ZWCAD file in #2109. +fn drawing() -> Drawing { + let mut d = Drawing::new_imperial(); + let mut layout = Layout::new("Sheet", 1); + layout.limits = Some((Vec2::new(1.038, 0.694), Vec2::new(9.506, 6.681))); + layout.page.image_origin = Some(Vec2::new(-1.054, -0.71)); + d.layouts = vec![layout]; + d +} + +fn sheet(d: &Drawing) -> &Layout { + d.layouts.iter().find(|l| l.name == "Sheet").expect("the layout") +} + +fn near(a: Vec2, b: Vec2) -> bool { + (a - b).len() < 1e-9 +} + +fn assert_kept(d: &Drawing, how: &str) { + let l = sheet(d); + let (lo, hi) = l.limits.unwrap_or_else(|| panic!("{how}: the limits are kept")); + assert!(near(lo, Vec2::new(1.038, 0.694)) && near(hi, Vec2::new(9.506, 6.681)), "{how}: {lo:?} {hi:?}"); + let origin = l.page.image_origin.unwrap_or_else(|| panic!("{how}: the image origin is kept")); + assert!(near(origin, Vec2::new(-1.054, -0.71)), "{how}: {origin:?}"); +} + +#[test] +fn a_layout_keeps_its_limits_and_image_origin_through_dxf() { + let back = read_dxf(write_dxf(&drawing()).as_bytes()).expect("reopen"); + assert_kept(&back, "DXF"); + // And again: a second save writes what the first read. + let again = read_dxf(write_dxf(&back).as_bytes()).expect("reopen"); + assert_kept(&again, "DXF twice"); +} + +#[test] +fn a_layout_keeps_its_limits_and_image_origin_through_dwg() { + let dwg = write_with(&drawing(), "t.dwg", &SaveOptions { version: Some(Version::R2018), binary: false }).expect("DWG"); + let back = read(&dwg, "t.dwg").expect("reopen"); + assert_kept(&back, "DWG"); +} + +#[test] +fn a_new_layout_writes_the_defaults() { + let text = write_dxf(&Drawing::new_imperial()); + let lines: Vec<&str> = text.lines().map(str::trim).collect(); + // The AcDbLayout limits after the layout's name, and the image origin before it. + let after = |code: &str| lines.windows(2).filter(|w| w[0] == code).map(|w| w[1]).collect::>(); + assert!(after("148").iter().all(|v| v.parse::() == Ok(0.0)), "{:?}", after("148")); + let back = read_dxf(text.as_bytes()).expect("reopen"); + let l = back.layouts.first().expect("a layout"); + // The defaults read back as none, so a drawing CADCraft made round-trips unchanged. + assert_eq!((l.limits, l.page.image_origin), (None, None)); + let limits = (after("10"), after("11")); + assert!(limits.0.iter().any(|v| v.parse::() == Ok(0.0)) && limits.1.iter().any(|v| v.parse::() == Ok(12.0)), "{limits:?}"); +} diff --git a/crates/io/tests/it/main.rs b/crates/io/tests/it/main.rs index aa77795fa..1601fc0ea 100644 --- a/crates/io/tests/it/main.rs +++ b/crates/io/tests/it/main.rs @@ -31,6 +31,7 @@ mod groups_roundtrip; mod hatch_assoc_roundtrip; mod hatch_pattern_def; mod helix_roundtrip; +mod layout_limits_roundtrip; mod layout_paper_units; mod layout_plot_window; mod leader_vertex_elevation; From ed08716f306f815a92d9c4a8ec6469403a249e2e Mon Sep 17 00:00:00 2001 From: austin62jake Date: Mon, 2 Nov 2026 15:26:00 -0600 Subject: [PATCH 048/100] fix(io): the paper viewport a save adds is the sheet at the layout's plot scale (#2108) (#2624) A layout without a paper-space viewport (id 1) gets one on save, sized by the page alone; a custom plot scale (142/143) wasn't applied, so an A3 layout at 49.6 mm per unit got a 420 x 297 viewport around its 8.4 x 6 unit sheet, and its extents went wrong. paper_view divides the sheet by the layout's plot scale; a layout scaled to fit, at 1:1 or with an unusable scale writes what it did. Co-authored-by: austin62jake <340374338+austin62jake@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- crates/io/src/dxf_write/layout.rs | 11 ++++++++++- crates/io/tests/it/dwg_roundtrip_losses.rs | 23 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/io/src/dxf_write/layout.rs b/crates/io/src/dxf_write/layout.rs index 4ad19ffbb..364648daf 100644 --- a/crates/io/src/dxf_write/layout.rs +++ b/crates/io/src/dxf_write/layout.rs @@ -33,12 +33,21 @@ pub(super) fn sheet_first(l: &Layout) -> Vec<&Entity> { all().filter(sheet).chain(all().filter(|e| !sheet(e))).collect() } +/// Paper units per paper-space unit of layout `l`: its custom plot scale (groups 142/143), so +/// the sheet is as large in paper space as it plots (a 420 mm sheet at 49.6 mm per unit is 8.47 +/// units wide, #2108). 1 when it is scaled to fit or the scale is unusable. +fn sheet_scale(l: &Layout) -> f64 { + let s = l.page.scale; + if l.page.scale_to_fit || !(s.is_finite() && (1e-6..=1e6).contains(&s)) { 1.0 } else { s } +} + /// The paper-space viewport (id 1) of the layout whose first viewport is `e`, if it needs one /// ([`paper_views`]): the whole sheet, viewed 1:1. Marked with CADCraft xdata so that our reader /// leaves it implicit again. pub(super) fn paper_view(w: &mut W, d: &Drawing, e: &Entity, owner: &str, cx: &Ctx) { let Some(l) = cx.paper_views.get(&e.handle).and_then(|i| d.layouts.get(*i)) else { return }; - let size = cadcraft_render::paper::sheet(d, &l.name).map(|s| s.size).unwrap_or(Vec2::new(12.0, 9.0)); + let paper = cadcraft_render::paper::sheet(d, &l.name).map(|s| s.size).unwrap_or(Vec2::new(12.0, 9.0)); + let size = paper / sheet_scale(l); let center = size * 0.5; let h = w.h(); w.s(0, "VIEWPORT"); diff --git a/crates/io/tests/it/dwg_roundtrip_losses.rs b/crates/io/tests/it/dwg_roundtrip_losses.rs index ec9c2b93d..aa00458a6 100644 --- a/crates/io/tests/it/dwg_roundtrip_losses.rs +++ b/crates/io/tests/it/dwg_roundtrip_losses.rs @@ -159,3 +159,26 @@ fn a_paper_viewport_behind_a_model_viewport_stays_id_1_through_dwg() { assert_eq!((id(model), id(sheet)), (Some(2), Some(1)), "layout {name}"); } } + +/// The paper-space viewport a save adds is the sheet as large as it plots: a layout with a custom +/// plot scale (here 49.6 mm of paper per unit, as in #2108's file) has an A3 sheet of 8.47 × 5.99 +/// units in paper space, not 420 × 297. A layout at 1:1 or scaled to fit keeps the page size. +#[test] +fn the_added_paper_viewport_follows_the_plot_scale() { + let sheet_width = |scale: f64, fit: bool| { + let mut d = Drawing::new_metric(); + let page = &mut d.layouts[0].page; + (page.width_mm, page.height_mm, page.landscape, page.paper_mm) = (297.0, 420.0, true, true); + (page.scale, page.scale_to_fit) = (scale, fit); + let first = Space::Paper(d.layouts[0].name.clone()); + d.add(&first, Common::default(), viewport(2)).unwrap(); + let text = cadcraft_io::write_dxf(&d); + let vps = records(&text, "VIEWPORT"); + assert_eq!(group(&vps[0], "1000"), Some("PAPERVIEW"), "{text}"); + group(&vps[0], "40").unwrap().parse::().unwrap() + }; + assert!((sheet_width(49.6, false) - 420.0 / 49.6).abs() < 1e-9, "{}", sheet_width(49.6, false)); + assert!((sheet_width(1.0, false) - 420.0).abs() < 1e-9); + assert!((sheet_width(49.6, true) - 420.0).abs() < 1e-9, "scaled to fit: the scale means nothing"); + assert!((sheet_width(f64::NAN, false) - 420.0).abs() < 1e-9, "an unusable scale is 1"); +} From f4c68e18fcd3e38d4122486c374e9d805cec8139 Mon Sep 17 00:00:00 2001 From: Elarionus <29511515+Elarionus@users.noreply.github.com> Date: Mon, 2 Nov 2026 13:26:04 -0800 Subject: [PATCH 049/100] macOS: Quit (Cmd+Q), the Dock's Quit and logout ask to save unsaved drawings (#2157) (#2620) Quit CADCraft in the app menu, the Dock's Quit and logout end the app through AppKit's terminate, which skipped the "Save changes to ...?" prompt, so unsaved work was lost. The event loop now turns termination into a close request for the window (the storytold winit fork's opt-in, as DesignCraft does), so it asks first, as the close button does. Cancel keeps the app open; Force Quit can't be intercepted. Co-authored-by: Ethan Dale Co-authored-by: Claude Opus 5.5 --- apps/cadcraft/Cargo.toml | 3 +++ apps/cadcraft/src/main.rs | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/apps/cadcraft/Cargo.toml b/apps/cadcraft/Cargo.toml index d8b4ef4c5..993668694 100644 --- a/apps/cadcraft/Cargo.toml +++ b/apps/cadcraft/Cargo.toml @@ -50,6 +50,9 @@ muda = { version = "0.21", default-features = false } # View ▸ Accessibility ▸ Animation Effects ▸ Use System Setting: the system's Reduce motion # (system_animations.rs). Already in Cargo.lock through muda. objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSWorkspace", "NSAccessibility"] } +# The same winit eframe uses (the storytold fork, patched in the workspace): its opt-in that turns +# AppKit's terminate (Cmd+Q, Dock Quit) into a close request, so unsaved changes are asked about. +winit = { version = "0.30", default-features = false } # Rust HID backends: no proprietary SDK or C HID library. [target.'cfg(target_os = "windows")'.dependencies] diff --git a/apps/cadcraft/src/main.rs b/apps/cadcraft/src/main.rs index c591c10b3..98dd3c679 100644 --- a/apps/cadcraft/src/main.rs +++ b/apps/cadcraft/src/main.rs @@ -310,6 +310,16 @@ fn main() -> eframe::Result { b.with_x11(); })); } + // macOS: Quit CADCraft (Cmd+Q), the Dock's Quit and logout end the app through AppKit's + // terminate, which would skip the unsaved-changes prompt (#2157). winit turns each into a + // close request for the window instead, so it asks first, as the close button does. + #[cfg(target_os = "macos")] + { + options.event_loop_builder = Some(Box::new(|b| { + use winit::platform::macos::EventLoopBuilderExtMacOS; + b.with_close_requested_on_terminate(true); + })); + } eframe::run_native( "CADCraft", options, From 067439110048e717b5dc6598528994afeaea824d Mon Sep 17 00:00:00 2001 From: whaslbeck Date: Mon, 2 Nov 2026 22:26:09 +0100 Subject: [PATCH 050/100] fix(io): read drawings with many groups in linear time (#862) (#2645) * fix(io): read drawings with many groups in linear time (#862) Drawings can carry hundreds of thousands of empty anonymous groups (*A1, *A2, ...). Two places in the DXF reader were quadratic in their number: - apply_groups looked up each group by name with a linear search; it now uses an index keyed by the lower-case name, so the case-insensitive merge (the later definition wins) is unchanged. - retained_objects called is_xdict(parent) for every object before consulting its cache; for a group the parent is the ACAD_GROUP dictionary, which head() walks completely. is_xdict is now memoized per object index. Two drawings with 179,124 and 124,362 empty groups go from 388 s and 165 s (DWG to PDF) to 7.3 s and 8.0 s, with byte-identical output. * test(io): check that reading groups scales linearly (#862) The content test passes on the quadratic code as well, so add a scaling check: 16 times the groups (2,500 vs 40,000, best of three reads each) must take less than 64 times as long. With both fixes the ratio is about 21; without the name index it is about 218, without the is_xdict cache about 180. No wall-clock limit is involved. Also document apply_groups. --------- Co-authored-by: Walter Haslbeck <8794773+whaslbeck@users.noreply.github.com> --- crates/io/src/dxf_raw.rs | 4 ++ crates/io/src/dxf_read/objects.rs | 17 +++++-- crates/io/tests/it/groups_roundtrip.rs | 62 ++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/crates/io/src/dxf_raw.rs b/crates/io/src/dxf_raw.rs index 16ea48b6c..ab18284ba 100644 --- a/crates/io/src/dxf_raw.rs +++ b/crates/io/src/dxf_raw.rs @@ -502,6 +502,10 @@ fn retained_objects( let modelled_entry = |tags: &[Tag], handle: &str| { entries(tags).iter().any(|(key, entry_handle)| entry_handle == handle && MODELLED_XDICT_KEYS.contains(&up(key).as_str())) }; + // is_xdict reads the whole record; a dictionary that owns many objects (ACAD_GROUP with + // hundreds of thousands of groups) would be read once per child (#862). + let mut xdict_memo: HashMap = HashMap::new(); + let mut is_xdict = |index: usize| *xdict_memo.entry(index).or_insert_with(|| is_xdict(index)); let mut retained = HashMap::new(); for index in 0..objects.len() { let mut path = Vec::new(); diff --git a/crates/io/src/dxf_read/objects.rs b/crates/io/src/dxf_read/objects.rs index 359dc887a..374313d48 100644 --- a/crates/io/src/dxf_read/objects.rs +++ b/crates/io/src/dxf_read/objects.rs @@ -153,8 +153,16 @@ impl Objects { } } + /// Add the groups named by the ACAD_GROUP dictionary entries, with the members that exist. + /// A name already present (compared case-insensitively) is replaced by the later definition. fn apply_groups(&self, d: &mut Drawing) { - // Groups (named by their ACAD_GROUP dictionary entries); members that exist. + // Names are matched through an index: a drawing can carry hundreds of thousands of + // (often empty) anonymous groups, and a linear search per group made loading quadratic + // (#862). + let mut by_name: HashMap = HashMap::new(); + for (i, g) in d.groups.iter().enumerate() { + by_name.entry(g.name.to_ascii_lowercase()).or_insert(i); + } for (h, tags) in &self.groups { let Some(name) = self.names.get(h).filter(|n| !n.is_empty()) else { continue }; let t = T(tags); @@ -166,9 +174,12 @@ impl Objects { .take(MAX_OBJECTS) .collect(); let g = Group { name: name.clone(), description: t.s(300).unwrap_or_default(), selectable: t.i(71).unwrap_or(1) != 0, members }; - match d.groups.iter_mut().find(|x| x.name.eq_ignore_ascii_case(name)) { + match by_name.get(&name.to_ascii_lowercase()).and_then(|&i| d.groups.get_mut(i)) { Some(x) => *x = g, - None => d.groups.push(g), + None => { + by_name.insert(name.to_ascii_lowercase(), d.groups.len()); + d.groups.push(g); + } } } } diff --git a/crates/io/tests/it/groups_roundtrip.rs b/crates/io/tests/it/groups_roundtrip.rs index 394b3d482..8614dc757 100644 --- a/crates/io/tests/it/groups_roundtrip.rs +++ b/crates/io/tests/it/groups_roundtrip.rs @@ -29,3 +29,65 @@ fn groups_survive_dwg_round_trip() { .collect(); assert_eq!(got, vec![("Doors", "front", false, vec![5.0, 0.0]), ("*A1", "", true, vec![0.0])]); } + +/// A DXF with one LINE (handle 2A) and the given groups in ACAD_GROUP: `n` empty anonymous +/// groups `*A1`..`*An`, then the `named` ones `(name, handle, description)`, each holding the line. +fn groups_dxf(n: usize, named: &[(&str, &str, &str)]) -> String { + let mut dict = String::from("0\nDICTIONARY\n5\nC\n3\nACAD_GROUP\n350\nD\n0\nDICTIONARY\n5\nD\n330\nC\n"); + let mut groups = String::new(); + for i in 1..=n { + let h = format!("{:X}", 0x1000 + i); + dict.push_str(&format!("3\n*A{i}\n350\n{h}\n")); + groups.push_str(&format!("0\nGROUP\n5\n{h}\n330\nD\n300\n\n70\n1\n71\n1\n")); + } + for (name, h, text) in named { + dict.push_str(&format!("3\n{name}\n350\n{h}\n")); + groups.push_str(&format!("0\nGROUP\n5\n{h}\n330\nD\n300\n{text}\n70\n0\n71\n1\n340\n2A\n")); + } + format!( + "0\nSECTION\n2\nENTITIES\n0\nLINE\n5\n2A\n8\n0\n10\n0\n20\n0\n11\n1\n21\n1\n0\nENDSEC\n\ + 0\nSECTION\n2\nOBJECTS\n{dict}{groups}0\nENDSEC\n0\nEOF\n" + ) +} + +/// Drawings can carry hundreds of thousands of empty anonymous groups (#862). Every group +/// arrives, and a name repeated in another case still keeps one group, the later definition. +#[test] +fn many_anonymous_groups_read_and_names_merge_case_insensitively() { + const N: usize = 20_000; + let dxf = groups_dxf(N, &[("Doors", "100", "first"), ("DOORS", "101", "second")]); + let d = cadcraft_io::read_dxf(dxf.as_bytes()).expect("read"); + assert_eq!(d.groups.len(), N + 1); + assert!(d.groups.iter().take(N).enumerate().all(|(i, g)| g.name == format!("*A{}", i + 1) && g.members.is_empty())); + let doors: Vec<(&str, &str, usize)> = d + .groups + .iter() + .filter(|g| g.name.eq_ignore_ascii_case("doors")) + .map(|g| (g.name.as_str(), g.description.as_str(), g.members.len())) + .collect(); + assert_eq!(doors, [("DOORS", "second", 1)]); +} + +/// Reading groups scales linearly (#862): 16 times the groups take about 16 to 20 times as long, +/// where the former quadratic lookups took about 256 times as long. The bound compares the two +/// sizes with each other, not with a wall-clock limit, and leaves room for timing noise; each +/// size is timed as the best of three reads. +#[test] +fn reading_groups_scales_linearly() { + let best_of_three = |n: usize| { + let dxf = groups_dxf(n, &[]); + (0..3) + .map(|_| { + let t = std::time::Instant::now(); + let d = cadcraft_io::read_dxf(dxf.as_bytes()).expect("read"); + assert_eq!(d.groups.len(), n); + t.elapsed() + }) + .min() + .unwrap_or_default() + }; + let small = best_of_three(2_500); + let large = best_of_three(40_000); + let ratio = large.as_secs_f64() / small.as_secs_f64().max(1e-6); + assert!(ratio < 64.0, "16x the groups took {ratio:.1}x as long ({small:?} -> {large:?})"); +} From fbd6e676f8353c9e46a67717d8bc39a4d2b813b3 Mon Sep 17 00:00:00 2001 From: CA Date: Tue, 3 Nov 2026 01:26:14 +0400 Subject: [PATCH 051/100] DWG save keeps OLE objects (OLE2FRAME): stop reporting them as not saved (#2582) * DWG save keeps OLE objects (OLE2FRAME): stop reporting them as not saved acadrust reads OLE2FRAME from our DXF into its own entity and writes it to DWG of every version we save, frame and embedded data unchanged; only the "Not saved in the DWG file" warning (and the QSAVE loss prompt) listed it. not_saved_as now skips the types the DWG bridge writes (DWG_WRITES). Tests: a synthetic OLE2FRAME through DWG (resaved, and as 2000 and 2010 DWG) keeps every byte; OLE objects are not reported at any version. Co-Authored-By: Claude Opus 5.5 * DWG_WRITES matches the type as stored, not case-insensitively The DXF given to the DWG bridge carries an unknown entity's type as stored, and acadrust reads only "OLE2FRAME" into its OLE entity: any other spelling becomes its Unknown and is dropped, so it must stay in the "Not saved" report. The test adds a lower-case "ole2frame" that is still listed. (Review comment on #2582.) Co-Authored-By: Claude Fable 5.1 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/file.rs | 11 +- crates/engine/tests/save_reports_not_saved.rs | 20 ++++ crates/io/tests/it/main.rs | 2 + crates/io/tests/it/ole2frame_roundtrip.rs | 113 ++++++++++++++++++ 4 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 crates/io/tests/it/ole2frame_roundtrip.rs diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 234769ad4..3b24e87bd 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -742,8 +742,14 @@ pub fn not_saved(d: &Drawing) -> std::collections::BTreeMap { /// store section, which is written back with the drawing (`Foreign::data_store`). const MODELER_TYPES: [&str; 3] = ["3DSOLID", "REGION", "BODY"]; +/// Entity types CADCraft doesn't model that the DWG bridge writes all the same: acadrust reads +/// them from our DXF into entities of its own and writes them to DWG of every version we save, +/// an OLE object with its frame and its embedded object's bytes unchanged. Matched exactly: the +/// bridge gets the type as stored, and acadrust knows only this spelling of it. +const DWG_WRITES: [&str; 1] = ["OLE2FRAME"]; + /// [`not_saved`] for a file saved as `version`. From 2013 on, a 3D solid, region or body the data -/// store has a record for keeps its geometry and is not counted. +/// store has a record for keeps its geometry and is not counted; neither are [`DWG_WRITES`]. pub fn not_saved_as(d: &Drawing, version: Version) -> std::collections::BTreeMap { let mut out: std::collections::BTreeMap = std::collections::BTreeMap::new(); let stored = if version >= Version::R2013 { d.foreign.data_store_entities() } else { Default::default() }; @@ -753,6 +759,9 @@ pub fn not_saved_as(d: &Drawing, version: Version) -> std::collections::BTreeMap if MODELER_TYPES.iter().any(|t| u.dxf_type.eq_ignore_ascii_case(t)) && stored.contains(&e.handle) { continue; } + if DWG_WRITES.contains(&u.dxf_type.as_str()) { + continue; + } let n = out.entry(u.dxf_type.clone()).or_insert(0); *n = n.saturating_add(1); } diff --git a/crates/engine/tests/save_reports_not_saved.rs b/crates/engine/tests/save_reports_not_saved.rs index 615028a21..24a2dff92 100644 --- a/crates/engine/tests/save_reports_not_saved.rs +++ b/crates/engine/tests/save_reports_not_saved.rs @@ -82,3 +82,23 @@ fn solids_whose_geometry_the_data_store_keeps_are_not_reported_from_2013() { // Before 2013 a file has no data store: every solid loses its geometry. assert_eq!(not_saved_as(&d, Version::R2010), [("3DSOLID".to_string(), 2), ("REGION".to_string(), 1)].into_iter().collect()); } + +/// The DWG bridge writes OLE objects (`ole2frame_roundtrip` in the io tests), so saving one as +/// DWG reports nothing for them, at any version, while other unmodelled types are still listed. +/// The type is matched as stored: the bridge gets it as stored too, and acadrust reads only the +/// upper-case spelling into its OLE entity, so any other spelling is still reported. +#[test] +fn ole_objects_are_not_reported() { + use cadcraft_engine::cmd::file::{Version, not_saved_as}; + let mut d = Drawing::new_imperial(); + d.add(&Space::Model, Common::default(), unknown("OLE2FRAME")).unwrap(); + d.add(&Space::Model, Common::default(), unknown("MLINE")).unwrap(); + d.add(&Space::Model, Common::default(), unknown("ole2frame")).unwrap(); + let mut b = Block::new("Logo"); + let h = d.new_handle(); + b.entities.push(cadcraft_engine::doc::Entity { handle: h, common: Common::default(), kind: unknown("OLE2FRAME") }); + d.blocks.insert("Logo".into(), std::sync::Arc::new(b)); + for v in [Version::R2000, Version::R2010, Version::R2018] { + assert_eq!(not_saved_as(&d, v), [("MLINE".to_string(), 1), ("ole2frame".to_string(), 1)].into_iter().collect(), "{v:?}"); + } +} diff --git a/crates/io/tests/it/main.rs b/crates/io/tests/it/main.rs index 1601fc0ea..7b49eb6bf 100644 --- a/crates/io/tests/it/main.rs +++ b/crates/io/tests/it/main.rs @@ -46,6 +46,8 @@ mod mtext_columns_roundtrip; mod mtext_embedded_object; mod mtext_line_spacing; mod multiline_attributes; +#[cfg(not(target_arch = "wasm32"))] +mod ole2frame_roundtrip; mod paperview_marker; mod passthrough_image_roundtrip; mod pdf_followups; diff --git a/crates/io/tests/it/ole2frame_roundtrip.rs b/crates/io/tests/it/ole2frame_roundtrip.rs new file mode 100644 index 000000000..2491d30d4 --- /dev/null +++ b/crates/io/tests/it/ole2frame_roundtrip.rs @@ -0,0 +1,113 @@ +//! OLE objects (OLE2FRAME: a picture, a spreadsheet or a document embedded in the drawing) +//! survive a DWG save. CADCraft doesn't model them and keeps them as read; the DWG bridge reads +//! them from our DXF into acadrust's own entity and writes them to DWG with their frame and the +//! embedded object's bytes, so a drawing saved as DWG and reopened still has them. + +use cadcraft_doc::EntityKind; + +/// Stand-in OLE data as a DWG carries it: a two-byte marker, the frame's four corners (upper +/// left, upper right, lower right, lower left; x, y, z doubles), then the object's bytes, here +/// opaque text long enough to take several 310 groups. +fn payload() -> Vec { + let mut data = vec![0x80, 0x00]; + for [x, y] in [[10.0f64, 40.0], [70.0, 40.0], [70.0, 5.0], [10.0, 5.0]] { + for v in [x, y, 0.0] { + data.extend_from_slice(&v.to_le_bytes()); + } + } + for i in 0..12u8 { + data.extend_from_slice(b"synthetic embedded object "); + data.push(i); + } + data +} + +fn hex(data: &[u8]) -> String { + data.iter().map(|b| format!("{b:02X}")).collect() +} + +/// A 2018 DXF with one embedded OLE object in model space on layer `Pictures`. +fn dxf() -> String { + let data = payload(); + let chunks: String = data.chunks(127).map(|c| format!("310\n{}\n", hex(c))).collect(); + format!( + "0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1032\n9\n$HANDSEED\n5\n100\n0\nENDSEC\n\ + 0\nSECTION\n2\nENTITIES\n\ + 0\nOLE2FRAME\n5\n2A\n330\n1F\n100\nAcDbEntity\n8\nPictures\n100\nAcDbOle2Frame\n70\n2\n\ + 10\n10\n20\n40\n30\n0\n11\n70\n21\n5\n31\n0\n71\n2\n72\n0\n73\n2\n90\n{}\n{chunks}1\nOLE\n\ + 0\nENDSEC\n0\nEOF\n", + data.len() + ) +} + +/// Each OLE2FRAME of DXF text as its (code, value) pairs. +fn frames(text: &str) -> Vec> { + let lines: Vec<&str> = text.lines().map(str::trim).collect(); + let mut out: Vec> = Vec::new(); + let mut inside = false; + for pair in lines.chunks(2) { + let [code, value] = pair else { break }; + if *code == "0" { + inside = *value == "OLE2FRAME"; + if inside { + out.push(Vec::new()); + } + } else if inside && let Some(rec) = out.last_mut() { + rec.push((code.to_string(), value.to_string())); + } + } + out +} + +fn group<'a>(rec: &'a [(String, String)], code: &str) -> Option<&'a str> { + rec.iter().find(|(c, _)| c == code).map(|(_, v)| v.as_str()) +} + +fn number(rec: &[(String, String)], code: &str) -> Option { + group(rec, code).and_then(|v| v.parse().ok()) +} + +/// The embedded object's bytes: the 310 groups joined. +fn data(rec: &[(String, String)]) -> String { + rec.iter().filter(|(c, _)| c == "310").map(|(_, v)| v.as_str()).collect() +} + +/// The frame, its object's bytes, type and layer as the DWG keeps them. +fn assert_kept(rec: &[(String, String)]) { + assert_eq!(data(rec), hex(&payload()), "the embedded object's bytes changed"); + assert_eq!(number(rec, "90"), Some(payload().len() as f64)); + let corners: Vec> = ["10", "20", "11", "21"].iter().map(|c| number(rec, c)).collect(); + assert_eq!(corners, [Some(10.0), Some(40.0), Some(70.0), Some(5.0)]); + assert_eq!(group(rec, "71"), Some("2"), "an embedded object"); + assert_eq!(group(rec, "73"), Some("2")); + assert_eq!(group(rec, "8"), Some("Pictures")); +} + +#[test] +fn dwg_keeps_ole_objects() { + let d = cadcraft_io::read_dxf(dxf().as_bytes()).unwrap(); + let ole = |d: &cadcraft_doc::Drawing| d.model.iter().filter(|e| matches!(&e.kind, EntityKind::Unknown(u) if u.dxf_type == "OLE2FRAME")).count(); + assert_eq!(ole(&d), 1); + + let dwg = cadcraft_io::write(&d, "x.dwg").unwrap(); + let back = String::from_utf8(cadcraft_dwg::dwg_to_dxf(&dwg).unwrap()).unwrap(); + let got = frames(&back); + assert_eq!(got.len(), 1, "{got:?}"); + assert_kept(&got[0]); + + // Reopened in CADCraft it is in model space again, and a second DWG save keeps it too. + let again = cadcraft_io::read(&dwg, "x.dwg").unwrap(); + assert_eq!(ole(&again), 1); + let dwg = cadcraft_io::write(&again, "x.dwg").unwrap(); + let got = frames(&String::from_utf8(cadcraft_dwg::dwg_to_dxf(&dwg).unwrap()).unwrap()); + assert_eq!(got.len(), 1, "{got:?}"); + assert_kept(&got[0]); + + // Older DWG versions keep it the same way. + for version in [cadcraft_io::Version::R2000, cadcraft_io::Version::R2010] { + let dwg = cadcraft_io::write_with(&d, "x.dwg", &cadcraft_io::SaveOptions { version: Some(version), binary: false }).unwrap(); + let got = frames(&String::from_utf8(cadcraft_dwg::dwg_to_dxf(&dwg).unwrap()).unwrap()); + assert_eq!(got.len(), 1, "{version:?}: {got:?}"); + assert_kept(&got[0]); + } +} From 25ca90581517a05eef4193fcb995836f91885124 Mon Sep 17 00:00:00 2001 From: austin62jake Date: Mon, 2 Nov 2026 15:26:18 -0600 Subject: [PATCH 052/100] fix(fonts): single-line TEXT runs left to right, as AutoCAD lays it out (#2500) (#2618) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(fonts): single-line TEXT runs left to right, as AutoCAD lays it out (#2500) A TEXT "- פרוייקט" took its direction from its first strong character (Hebrew), so the leading dash went to the right of the word; AutoCAD shows it on the left. complex::shape now takes the base direction (shape_stroke_dir, ttf::shape_dir and shape_line_dir pass it on), and TEXT's layout (fonts::place and its frame) asks for left to right. MTEXT and the existing entry points keep taking the direction from the text. Co-Authored-By: Claude Opus 5.5 * fix(fonts): a right-to-left base reaches LTR-only lines; Fit TEXT is measured as placed From the review of #2618: shape_stroke_dir and ttf::shape_dir took the simple, left-to-right path whenever a line had no right-to-left letter, so an explicit right-to-left base did nothing there; such a line now takes the bidi path. And Fit/Aligned TEXT was measured with the direction from its content but placed left to right, so a line whose last glyph changed (a stroke-font gap) missed its second point; it is measured with the direction it is placed in. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: austin62jake <340374338+austin62jake@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- crates/fonts/src/complex.rs | 23 +++++++------- crates/fonts/src/lib.rs | 60 ++++++++++++++++++++++++++++++++----- crates/fonts/src/ttf.rs | 12 ++++++-- 3 files changed, 73 insertions(+), 22 deletions(-) diff --git a/crates/fonts/src/complex.rs b/crates/fonts/src/complex.rs index 4a4ac461f..ce2fbcb61 100644 --- a/crates/fonts/src/complex.rs +++ b/crates/fonts/src/complex.rs @@ -64,20 +64,21 @@ impl Src { /// Shape one line containing right-to-left text in `primary` (None: the stroke font), with the /// same conventions as the left-to-right paths: baseline at y = 0, x from 0, text height = cap -/// height, `%%` codes decoded. `None` when the line is too long (the caller then lays it out one -/// glyph per character) or shaping failed. -pub(crate) fn shape(primary: Option<&Face>, s: &str, height: f64, width_factor: f64, oblique: f64) -> Option { +/// height, `%%` codes decoded. `base_rtl` is the line's direction (`None`: that of its first +/// strong character). `None` when the line is too long (the caller then lays it out one glyph +/// per character) or shaping failed. +pub(crate) fn shape(primary: Option<&Face>, s: &str, height: f64, width_factor: f64, oblique: f64, base_rtl: Option) -> Option { let chars = crate::decode_controls(s); if chars.is_empty() || chars.len() > MAX_CHARS { return None; } // A last-resort guard around the bidi and shaping libraries: a panic in them must not take // the drawing down with it (the line then falls back to one glyph per character). - catch_unwind(AssertUnwindSafe(|| shape_chars(primary, &chars, height, width_factor, oblique))).ok() + catch_unwind(AssertUnwindSafe(|| shape_chars(primary, &chars, height, width_factor, oblique, base_rtl))).ok() } /// Lay out decoded directional runs, keeping advances and decoration spans for empty glyphs. -fn shape_chars(primary: Option<&Face>, chars: &[(char, bool, bool)], height: f64, width_factor: f64, oblique: f64) -> Shaped { +fn shape_chars(primary: Option<&Face>, chars: &[(char, bool, bool)], height: f64, width_factor: f64, oblique: f64, base_rtl: Option) -> Shaped { let h = if height.is_finite() && height > 0.0 { height } else { 1.0 }; let wf = if width_factor.is_finite() && width_factor.abs() > 1e-6 { width_factor } else { 1.0 }; let shear = oblique.tan().clamp(-10.0, 10.0); @@ -90,7 +91,7 @@ fn shape_chars(primary: Option<&Face>, chars: &[(char, bool, bool)], height: f64 let mut x = 0.0; // Whether the rightmost glyph so far is a stroke-font glyph. let mut stroke_last = false; - for (run, rtl) in bidi::visual_runs(&cs, None) { + for (run, rtl) in bidi::visual_runs(&cs, base_rtl) { let mut pieces = itemize(&cs, run, primary); if rtl { pieces.reverse(); @@ -325,13 +326,13 @@ mod tests { #[test] fn empty_complex_glyph_keeps_advance_without_geometry() { let face = empty_glyph_face(); - let shaped = shape(Some(&face), " ", 1.0, 1.0, 0.0).expect("shapes empty glyph"); + let shaped = shape(Some(&face), " ", 1.0, 1.0, 0.0, None).expect("shapes empty glyph"); assert!(shaped.width > 0.0, "the font's advance is retained"); assert!(shaped.is_empty()); assert!(shaped.glyphs.is_empty()); assert!(shaped.text.is_empty()); - let underlined = shape(Some(&face), "%%u ", 1.0, 1.0, 0.0).expect("shapes decorated empty glyph"); + let underlined = shape(Some(&face), "%%u ", 1.0, 1.0, 0.0, None).expect("shapes decorated empty glyph"); assert_eq!(underlined.width, shaped.width); assert!(underlined.glyphs.is_empty()); assert!(underlined.text.is_empty()); @@ -344,7 +345,7 @@ mod tests { // missing-glyph boxes, the digits stay stroke glyphs, in the middle of the line (between // the two words), and the measured width matches the shaped one. let s = "قیمت 25 تومان"; - let sh = shape(None, s, 1.0, 1.0, 0.0).unwrap_or_default(); + let sh = shape(None, s, 1.0, 1.0, 0.0, None).unwrap_or_default(); assert!(sh.width > 0.0); assert!((crate::line_width(s, 1.0, 1.0) - sh.width).abs() < 1e-9); let digits: Vec = sh.strokes.iter().filter(|st| !(st.len() == 5 && st.first() == st.last())).flatten().map(|p| p.x).collect(); @@ -535,7 +536,7 @@ mod tests { let marks = format!("ก{}", "\u{0E48}".repeat(MAX_CHARS - 1)); assert!(crate::shape_stroke(&marks, 1.0, 1.0, 0.0).width > 0.0); let long = "ก".repeat(MAX_CHARS + 1); - assert!(shape(None, &long, 1.0, 1.0, 0.0).is_none()); + assert!(shape(None, &long, 1.0, 1.0, 0.0, None).is_none()); assert!(crate::shape_stroke(&long, 1.0, 1.0, 0.0).width > 0.0); } @@ -546,7 +547,7 @@ mod tests { let _ = crate::line_width(s, 0.0, 0.0); } let long = "\u{05D0}b ".repeat(MAX_CHARS); - assert!(shape(None, &long, 1.0, 1.0, 0.0).is_none()); + assert!(shape(None, &long, 1.0, 1.0, 0.0, None).is_none()); assert!(crate::shape_stroke(&long, 1.0, 1.0, 0.0).width > 0.0); } } diff --git a/crates/fonts/src/lib.rs b/crates/fonts/src/lib.rs index beb8996ff..983110b4d 100644 --- a/crates/fonts/src/lib.rs +++ b/crates/fonts/src/lib.rs @@ -147,6 +147,12 @@ fn stroke_advance(c: char, h: f64, wf: f64) -> f64 { /// (radians). Characters the stroke font lacks are drawn from an installed font that has them /// (glyph outlines, sized like TrueType text), else as a missing-glyph box. pub fn shape_stroke(s: &str, height: f64, width_factor: f64, oblique: f64) -> Shaped { + shape_stroke_dir(s, height, width_factor, oblique, None) +} + +/// [`shape_stroke`] in the direction `base_rtl` (`None`: that of the line's first strong +/// character). +pub fn shape_stroke_dir(s: &str, height: f64, width_factor: f64, oblique: f64, base_rtl: Option) -> Shaped { let h = if height.is_finite() && height > 0.0 { height } else { 1.0 }; let wf = if width_factor.is_finite() && width_factor.abs() > 1e-6 { width_factor } else { 1.0 }; let sc = h / stroke::CAP; @@ -156,8 +162,10 @@ pub fn shape_stroke(s: &str, height: f64, width_factor: f64, oblique: f64) -> Sh let mut under_start: Option = None; let mut over_start: Option = None; let mut gap = stroke::GAP * sc * wf; - if complex::needed(s) - && let Some(sh) = complex::shape(None, s, h, wf, oblique) + // The simple path lays a line out left to right: one asked to run right to left goes through + // the bidi path even without right-to-left letters. + if (complex::needed(s) || base_rtl == Some(true)) + && let Some(sh) = complex::shape(None, s, h, wf, oblique, base_rtl) { return sh; } @@ -250,7 +258,7 @@ pub fn line_width(s: &str, height: f64, width_factor: f64) -> f64 { if complex::needed(s) { let h = if height.is_finite() && height > 0.0 { height } else { 1.0 }; let wf = if width_factor.is_finite() && width_factor.abs() > 1e-6 { width_factor } else { 1.0 }; - if let Some(sh) = complex::shape(None, s, h, wf, 0.0) { + if let Some(sh) = complex::shape(None, s, h, wf, 0.0, None) { return sh.width; } } @@ -417,14 +425,24 @@ pub(crate) fn decorations(spans: &[(f64, f64, bool, bool)], h: f64) -> Vec Shaped { + shape_line_dir(font, s, height, width_factor, oblique, None) +} + +/// [`shape_line`] in the direction `base_rtl` (`None`: that of the line's first strong +/// character). +pub fn shape_line_dir(font: &TextFont, s: &str, height: f64, width_factor: f64, oblique: f64, base_rtl: Option) -> Shaped { if let TextFont::Outline(face) = font - && let Some(sh) = ttf::shape(face, s, height, width_factor, oblique) + && let Some(sh) = ttf::shape_dir(face, s, height, width_factor, oblique, base_rtl) { return sh; } - shape_stroke(s, height, width_factor, oblique) + shape_stroke_dir(s, height, width_factor, oblique, base_rtl) } +/// Single-line TEXT (and attributes) runs left to right, as AutoCAD lays it out: neutral +/// characters before a right-to-left word ("- " in "- פרוייקט") stay on its left (#2500). +const TEXT_BASE_RTL: Option = Some(false); + /// Width of one line in `font`. pub fn text_width(font: &TextFont, s: &str, height: f64, width_factor: f64) -> f64 { if let TextFont::Outline(face) = font @@ -551,7 +569,7 @@ fn frame(font: &TextFont, s: &str, p: &TextParams) -> (Vec2, Vec2, f64, Vec2) { let rot = p.align_pt.map_or(rot, |p2| p.insert.angle_to(p2)); return (p.insert, Vec2::ZERO, rot, Vec2::new(1.0, 1.0)); } - let sh = shape_line(font, s, height, p.width_factor, p.oblique); + let sh = shape_line_dir(font, s, height, p.width_factor, p.oblique, TEXT_BASE_RTL); let origin = match (p.h, p.v) { (Align::Left, VAlign::Baseline) => p.insert, _ => p.align_pt.unwrap_or(p.insert), @@ -572,7 +590,9 @@ pub fn place(font: &TextFont, s: &str, p: &TextParams) -> (Shaped, Bounds2) { { let len = insert.dist(p2); rot = insert.angle_to(p2); - let w = text_width(font, s, height, wf); + // Measured as it is placed, left to right: the stroke font's trailing gap follows the + // glyph that ends the line. + let w = shape_line_dir(font, s, height, wf, p.oblique, TEXT_BASE_RTL).width; if w > 1e-12 && len > 1e-12 { let k = len / w; if p.h == Align::Aligned { @@ -582,7 +602,7 @@ pub fn place(font: &TextFont, s: &str, p: &TextParams) -> (Shaped, Bounds2) { } } } - let mut sh = shape_line(font, s, height, wf, p.oblique); + let mut sh = shape_line_dir(font, s, height, wf, p.oblique, TEXT_BASE_RTL); let width = sh.width; // Every justification but baseline-left (and aligned/fit, which span both points) is placed by // its alignment point (DXF group 11); the insertion point (group 10) is then a computed point. @@ -625,6 +645,30 @@ pub fn place_text( mod tests { use super::*; + /// A TEXT that starts with neutral characters before a Hebrew word ("- פרוייקט") keeps them + /// on the left, as AutoCAD draws it; the line's own direction (first strong character) would + /// put the dash on the right (#2500). + #[test] + fn text_runs_left_to_right_whatever_its_first_strong_character() { + let s = "- \u{05E4}\u{05E8}\u{05D5}\u{05D9}\u{05E7}\u{05D8}"; + let dash = shape_stroke("-", 1.0, 1.0, 0.0).strokes; + assert!(!dash.is_empty()); + let has_dash_first = |strokes: &[Vec]| dash.iter().all(|d| strokes.iter().any(|st| st == d)); + let (text, _) = place(&TextFont::Stroke, s, &TextParams::new(Vec2::ZERO, 1.0)); + assert!(has_dash_first(&text.strokes), "the dash starts the TEXT"); + // The line on its own (MTEXT pieces) still takes its direction from its first strong + // character: right to left here, the dash after the word. + let line = shape_stroke(s, 1.0, 1.0, 0.0); + assert!(!has_dash_first(&line.strokes), "the dash ends a right-to-left line"); + // A line asked to run right to left does so without right-to-left letters too. + assert!(has_dash_first(&shape_stroke_dir("- abc", 1.0, 1.0, 0.0, None).strokes)); + assert!(!has_dash_first(&shape_stroke_dir("- abc", 1.0, 1.0, 0.0, Some(true)).strokes), "the dash after the word"); + // Fit TEXT is measured as it is placed, so it spans its two points exactly. + let fit = TextParams { h: Align::Fit, align_pt: Some(Vec2::new(10.0, 0.0)), ..TextParams::new(Vec2::ZERO, 1.0) }; + let (text, _) = place(&TextFont::Stroke, s, &fit); + assert!((text.width - 10.0).abs() < 1e-9, "{}", text.width); + } + #[test] fn every_ascii_printable_has_a_glyph() { for c in ' '..='~' { diff --git a/crates/fonts/src/ttf.rs b/crates/fonts/src/ttf.rs index c426db243..fff4ecde7 100644 --- a/crates/fonts/src/ttf.rs +++ b/crates/fonts/src/ttf.rs @@ -894,9 +894,15 @@ pub(crate) fn place_glyph_at(g: &GlyphOutline, origin: Vec2, scale: f64, wf: f64 /// y = 0, x from 0) plus `%%u`/`%%o` decorations as strokes. Text height = cap height. /// Characters the face lacks come from a fallback font, scaled to its own cap height. pub fn shape(face: &Face, s: &str, height: f64, width_factor: f64, oblique: f64) -> Option { + shape_dir(face, s, height, width_factor, oblique, None) +} + +/// [`shape`] in the direction `base_rtl` (`None`: that of the line's first strong character). +pub fn shape_dir(face: &Face, s: &str, height: f64, width_factor: f64, oblique: f64, base_rtl: Option) -> Option { face.font()?; - if crate::complex::needed(s) - && let Some(sh) = crate::complex::shape(Some(face), s, height, width_factor, oblique) + // As in [`crate::shape_stroke_dir`]: a line asked to run right to left takes the bidi path. + if (crate::complex::needed(s) || base_rtl == Some(true)) + && let Some(sh) = crate::complex::shape(Some(face), s, height, width_factor, oblique, base_rtl) { return Some(sh); } @@ -934,7 +940,7 @@ pub fn shape(face: &Face, s: &str, height: f64, width_factor: f64, oblique: f64) pub fn width(face: &Face, s: &str, height: f64, width_factor: f64) -> Option { face.font()?; if crate::complex::needed(s) - && let Some(sh) = crate::complex::shape(Some(face), s, height, width_factor, 0.0) + && let Some(sh) = crate::complex::shape(Some(face), s, height, width_factor, 0.0, None) { return Some(sh.width); } From c0306b1af3d1f6f21c4563045a11963cc3d5dc59 Mon Sep 17 00:00:00 2001 From: Elarionus <29511515+Elarionus@users.noreply.github.com> Date: Mon, 2 Nov 2026 13:26:23 -0800 Subject: [PATCH 053/100] Remember the main window's size, position and maximized state between sessions (#2595) (#2604) * Remember the main window's size, position and maximized state between sessions (#2595) The main window opened at its default size, centred, on every launch: eframe's window persistence was turned off (persist_window: false), and `centered` is applied over any saved position. The window state is now saved; on launch, if app.ron holds a saved window, it is restored (eframe keeps it on the connected monitors) and not centred. Only the first launch, with nothing saved, opens centred (#1129). Wayland places windows itself, so only size and maximized state return there. Co-Authored-By: Claude Opus 5.5 * Docs: leave the parity docs to the maintainers The affected rows are named in the PR description instead (CLAUDE.md: PRs do not edit the parity docs). Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Ethan Dale Co-authored-by: Claude Opus 5.5 --- Cargo.lock | 1 + apps/cadcraft/Cargo.toml | 3 ++ apps/cadcraft/src/main.rs | 61 +++++++++++++++++++++++++++++++-------- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81a772f6e..b5210faea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -632,6 +632,7 @@ dependencies = [ "muda", "objc2-app-kit 0.3.2", "rfd", + "ron", "serde_json", "sys-locale", "thiserror 2.0.21", diff --git a/apps/cadcraft/Cargo.toml b/apps/cadcraft/Cargo.toml index 993668694..ee1b8b9c8 100644 --- a/apps/cadcraft/Cargo.toml +++ b/apps/cadcraft/Cargo.toml @@ -24,6 +24,9 @@ log = { workspace = true } clap = { workspace = true } thiserror = { workspace = true } chrono = { workspace = true } +# Reads eframe's app.ron to tell whether a window state was saved (main.rs `saved_window`). +# Already in Cargo.lock through eframe; same version so nothing is built twice. +ron = "0.12" [build-dependencies] winresource = "0.1" diff --git a/apps/cadcraft/src/main.rs b/apps/cadcraft/src/main.rs index 98dd3c679..3454dc35f 100644 --- a/apps/cadcraft/src/main.rs +++ b/apps/cadcraft/src/main.rs @@ -127,7 +127,8 @@ impl eframe::App for App { self.cad.cancel_screenshots(); self.cad.shutdown_autosave(); } - /// Preferences (the interface theme) survive restarts; window and egui state are not kept. + /// Preferences (the interface theme) survive restarts; eframe saves the window state itself + /// (`persist_window`); egui state is not kept. fn save(&mut self, storage: &mut dyn eframe::Storage) { storage.set_string(cadcraft_ui_egui::options::PROFILE_KEY, self.cad.profile_json()); storage.set_string(cadcraft_ui_egui::PREFS_KEY, self.cad.prefs_json()); @@ -194,8 +195,20 @@ fn filters_for_save(name: &str) -> [(&'static str, &'static str); 6] { filters } -/// The main window: its size, title bar, placement and rendering needs. -fn window_options() -> eframe::NativeOptions { +/// Whether eframe's storage file (`app.ron`, a map of keys to strings) holds a saved main window +/// state. An unreadable or malformed file counts as none, as it does for eframe. +fn saved_window(app_ron: &str) -> bool { + ron::from_str::>(app_ron).is_ok_and(|kv| kv.contains_key(WINDOW_KEY)) +} + +/// The window state from the last session is saved and will be restored (#2595). +fn restores_window() -> bool { + eframe::storage_dir(APP_ID).and_then(|dir| std::fs::read_to_string(dir.join("app.ron")).ok()).is_some_and(|ron| saved_window(&ron)) +} + +/// The main window: its size, title bar, placement and rendering needs. `restoring`: a window state +/// saved by the last session is restored instead of opening centred. +fn window_options(restoring: bool) -> eframe::NativeOptions { eframe::NativeOptions { viewport: egui::ViewportBuilder::default() .with_title("CADCraft") @@ -206,10 +219,13 @@ fn window_options() -> eframe::NativeOptions { .with_titlebar_shown(false) .with_title_shown(false) .with_app_id(APP_ID), - persist_window: false, - // The window opens centred on its screen, not cascaded from the top-left corner (#1129). + // The size, position and maximized state are kept between sessions (#2595); eframe keeps a + // restored window on the monitors that are connected now. + persist_window: true, + // The first launch opens centred on its screen, not cascaded from the top-left corner + // (#1129); later launches go back where the window was (eframe would centre over that). // Wayland compositors place windows themselves. - centered: true, + centered: !restoring, // CADCraft installs its own capped font chain (`theme::install_fonts`); never run // system-font discovery as a hidden fallback. system_font_fallback: false, @@ -221,6 +237,9 @@ fn window_options() -> eframe::NativeOptions { const APP_ID: &str = "ai.storyteller.cadcraft"; +/// The key eframe saves the main window state under in its storage. +const WINDOW_KEY: &str = "window"; + /// Stencil bits of the window's render pass (see `NativeOptions::stencil_buffer`). const STENCIL_BITS: u8 = 8; @@ -281,7 +300,7 @@ fn main() -> eframe::Result { // The crosshair is drawn by the app: present without a queue on Linux/BSD, where X11 // swapchains hold two frames back under vsync (`CADCRAFT_VSYNC=1` restores vsync). wgpu_options: eframe::egui_wgpu::WgpuConfiguration::default().with_surface_config(surface), - ..window_options() + ..window_options(restores_window()) }; if let Some(icon) = app_icon() { options.viewport = options.viewport.with_icon(icon); @@ -403,7 +422,7 @@ fn main() -> eframe::Result { #[cfg(test)] mod tests { - use super::{filters_for_save, follow_desktop, window_options}; + use super::{filters_for_save, follow_desktop, saved_window, window_options}; #[test] fn save_dialog_offers_dwg_with_the_drawing_format_first() { @@ -421,12 +440,30 @@ mod tests { } } - /// #1129: the window opens centred, not cascaded from the top-left corner. + /// #1129: the first launch opens centred, not cascaded from the top-left corner. #[test] - fn the_window_opens_centred() { - let o = window_options(); + fn the_first_launch_opens_centred() { + let o = window_options(false); assert!(o.centered, "centred on launch"); - assert!(!o.persist_window, "no saved position to restore instead"); + assert!(o.persist_window, "the window state is saved for the next launch"); + } + + /// #2595: a saved window state (size, position, maximized) is restored, not centred over. + #[test] + fn a_saved_window_reopens_where_it_was() { + let o = window_options(true); + assert!(!o.centered, "centring would move a restored window"); + assert!(o.persist_window); + } + + /// Only a readable `app.ron` with a window entry counts as a saved window state. + #[test] + fn a_saved_window_is_read_from_app_ron() { + assert!(saved_window(r#"{"cadcraft.prefs": "{}", "window": "(maximized: true)"}"#)); + assert!(!saved_window(r#"{"cadcraft.prefs": "{\"window\":1}"}"#), "a value mentioning it is not the key"); + assert!(!saved_window("")); + assert!(!saved_window("{\"window\": "), "truncated"); + assert!(!saved_window("not ron at all")); } /// View ▸ Theme ▸ System on Linux: winit reports no theme, so the desktop's answer stands in; From 4a17ee78c24407cfc1470bb6a336ea3432ca0a60 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Mon, 2 Nov 2026 22:26:28 +0100 Subject: [PATCH 054/100] Reject invalid fill angles in JSON arraypolar (#2655) Found while testing crates/engine/src/cmd/array.rs:380 (run_arraypolar, `fill: f64_or(p, "angle", 360.0)`) Co-authored-by: Claude Haiku 5.5 --- crates/engine/src/cmd/array.rs | 6 +++++- crates/engine/tests/it/array_commands.rs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/array.rs b/crates/engine/src/cmd/array.rs index 255555a28..da96ccfed 100644 --- a/crates/engine/src/cmd/array.rs +++ b/crates/engine/src/cmd/array.rs @@ -374,10 +374,14 @@ fn run_arraypolar(s: &mut Session, p: &Value) -> Result { check_size(s, n.saturating_mul(rows).saturating_mul(levels), hs.len())?; let base = point_param(p, "base").or_else(|| s.doc().ok().and_then(|d| first_center(d, &hs))).unwrap_or(c); let row_spacing = f64_or(p, "rowSpacing", 1.0); + let fill = f64_or(p, "angle", 360.0); + if !(fill.abs() >= 1e-9 && fill.abs() <= 360.0 + 1e-9) { + return Err(other("Requires a fill angle between -360 and 360 degrees (not 0).")); + } let kind = ArrayKind::Polar(PolarArray { center: c - base, items: count32(n), - fill: f64_or(p, "angle", 360.0), + fill: fill.clamp(-360.0, 360.0), rotate: bool_or(p, "rotate", true), rows: count32(rows), row_spacing, diff --git a/crates/engine/tests/it/array_commands.rs b/crates/engine/tests/it/array_commands.rs index d420c36d6..61d5a0d02 100644 --- a/crates/engine/tests/it/array_commands.rs +++ b/crates/engine/tests/it/array_commands.rs @@ -212,3 +212,20 @@ fn arrays_over_maxarray_are_refused_not_shrunk() { assert!(s.current_prompt().is_none(), "{}", prompt(&s)); assert_eq!(circle_centers(&s).len(), 2000); } + +#[test] +fn json_arraypolar_rejects_bad_fill_angles() { + let mut s = Session::new(); + let h = s.execute("circle", &json!({"center": [5, 0], "radius": 1})).unwrap()["handle"].clone(); + let before = s.doc().unwrap().model.iter().count(); + for bad in [0.0, 720.0, -400.0, 360.5] { + let p = json!({"handles": [h], "center": [0, 0], "count": 4, "angle": bad, "associative": false}); + let e = s.execute("arraypolar", &p).unwrap_err(); + assert!(e.to_string().contains("Requires a fill angle between -360 and 360 degrees"), "{bad}: {e}"); + assert_eq!(s.doc().unwrap().model.iter().count(), before, "{bad}"); + } + for ok in [360.0, -360.0] { + let p = json!({"handles": [h], "center": [0, 0], "count": 4, "angle": ok, "associative": false}); + s.execute("arraypolar", &p).unwrap(); + } +} From e6352d955ee4cc264fd5d26642d666c20cf8d062 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Mon, 2 Nov 2026 22:26:33 +0100 Subject: [PATCH 055/100] Reject control characters in layer names (#2569) Found while testing crates/engine/src/cmd/layer.rs:184 (valid_name, used by run_new) Co-authored-by: Claude Haiku 5.5 --- crates/engine/src/cmd/layer.rs | 2 +- crates/engine/tests/layer_name_control_chars.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 crates/engine/tests/layer_name_control_chars.rs diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index 806641ef6..c5fa89403 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -182,7 +182,7 @@ fn loaded_linetype(d: &cadcraft_doc::Drawing, cmd: &str, p: &Value) -> Result bool { - !n.trim().is_empty() && n.len() <= 255 && !n.chars().any(|c| "<>/\\\":;?*|,=`".contains(c)) + !n.trim().is_empty() && n.len() <= 255 && !n.chars().any(|c| c.is_control() || "<>/\\\":;?*|,=`".contains(c)) } fn run_new(s: &mut Session, p: &Value) -> Result { diff --git a/crates/engine/tests/layer_name_control_chars.rs b/crates/engine/tests/layer_name_control_chars.rs new file mode 100644 index 000000000..4e00a1454 --- /dev/null +++ b/crates/engine/tests/layer_name_control_chars.rs @@ -0,0 +1,14 @@ +//! `layer.new` refuses control characters in a layer name (the DXF writer would turn them into +//! spaces and the name would change on reopen). + +use cadcraft_engine::Session; +use serde_json::json; + +#[test] +fn layer_new_rejects_control_characters() { + let mut s = Session::new(); + for name in ["A\nB", "A\tB", "A\u{0}B"] { + assert!(s.execute("layer.new", &json!({"name": name})).is_err(), "{name:?} was accepted"); + } + assert!(s.execute("layer.new", &json!({"name": "A B"})).is_ok()); +} From ad95791c206a2722294bf3b397f82df90f026a6e Mon Sep 17 00:00:00 2001 From: Ashok Ram Date: Tue, 3 Nov 2026 02:56:38 +0530 Subject: [PATCH 056/100] SVG import: a transform on a shape element moves the shape (#2602) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SVG 1.1 §7.5 lets every graphics element carry a transform. Only groups' transforms were applied, so a rect, circle, path or other shape with its own transform came in where it would be without one. on_element now composes the element's transform after its groups' for that element only, the way a group around it would. https://www.w3.org/TR/SVG11/coords.html#TransformAttribute Co-authored-by: ashokram-si <347905169+ashokram-si@users.noreply.github.com> --- crates/io/src/svg_read.rs | 17 ++++++++++++++-- crates/io/src/tests.rs | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/crates/io/src/svg_read.rs b/crates/io/src/svg_read.rs index 21508fe7b..54ef9d846 100644 --- a/crates/io/src/svg_read.rs +++ b/crates/io/src/svg_read.rs @@ -1,8 +1,8 @@ //! SVG → Drawing: the static W3C SVG subset that drawing tools emit. //! //! Reads shapes (`rect`, `circle`, `ellipse`, `line`, `polyline`, `polygon`), `path` -//! outlines (`M L H V C S Q T A Z`, absolute and relative), `g` grouping with `transform` -//! attributes, `viewBox` and unit-bearing `width`/`height`, and Inkscape layers +//! outlines (`M L H V C S Q T A Z`, absolute and relative), `g` grouping, `transform` on groups +//! and shapes, `viewBox` and unit-bearing `width`/`height`, and Inkscape layers //! (`inkscape:groupmode="layer"`). Geometry becomes centre-line entities in millimetres with //! the page's top-left corner at the origin and Y pointing up — where a laser or pen job //! wants it. @@ -262,6 +262,19 @@ impl Parser { } return Ok(()); } + // A shape's own `transform` (SVG 1.1 §7.5) maps it after its groups', for this element only. + let local = attr(e, b"transform").as_deref().and_then(parse_transform); + if let Some(local) = local { + self.stack.push(self.cur().then_before(local)); + } + let read = self.shape(name, e); + if local.is_some() { + self.stack.pop(); + } + read + } + + fn shape(&mut self, name: &[u8], e: &BytesStart) -> Result<()> { match name { b"use" => { self.left_out(LeftOut::Use); diff --git a/crates/io/src/tests.rs b/crates/io/src/tests.rs index 12d9f0a12..4aa427bdd 100644 --- a/crates/io/src/tests.rs +++ b/crates/io/src/tests.rs @@ -2299,6 +2299,48 @@ fn svg_group_transforms_apply() { assert!((l.b - Vec3::new(15.0, 8.0, 0.0)).len() < 1e-9, "{l:?}"); } +/// The geometry of everything read, for comparing two readings of one drawing. +fn geometry(d: &Drawing) -> String { + format!("{:?}", d.model.iter().map(|e| &e.kind).collect::>()) +} + +/// `transform` on a shape element (SVG 1.1 §7.5, every graphics element takes one) maps it as +/// the same transform on a group around it would: for every shape kind, composed after the +/// parent group's, and not passed on to the next sibling. +#[test] +fn svg_element_transforms_apply_like_a_group_around_the_element() { + let read_svg = |body: &str| read(format!("{SVG_PAGE}{body}").as_bytes(), "t.svg").unwrap(); + let shapes = [ + r#""#, + r#""#, + r#""#, + r#""#, + r#""#, + r#""#, + r#""#, + ]; + for t in ["translate(5,2)", "rotate(30 5 5)", "scale(2,1)", "skewX(20)", "matrix(1 0.2 0 1 3 -1)"] { + for shape in shapes { + let on_element = shape.replacen("/>", &format!(r#" transform="{t}"/>"#), 1); + let in_group = format!(r#"{shape}"#); + assert_eq!(geometry(&read_svg(&on_element)), geometry(&read_svg(&in_group)), "{t} on {shape}"); + } + } + // A rotated circle stays a circle; a skewed one is an ellipse. + assert_eq!(kinds(&read_svg(r#""#)), ["Circle"]); + assert_eq!(kinds(&read_svg(r#""#)), ["Ellipse"]); + // Inside a group: the group's transform, then the element's. + let nested = read_svg(r#""#); + let both = read_svg(r#""#); + assert_eq!(geometry(&nested), geometry(&both)); + // Written with an end tag too, and the next sibling keeps its own place. + let d = read_svg(r#""#); + let lines: Vec = d.model.iter().filter_map(|e| if let EntityKind::Line(l) = &e.kind { Some(l.clone()) } else { None }).collect(); + assert_eq!(lines.len(), 2); + assert!((lines[0].a - Vec3::new(5.0, 10.0, 0.0)).len() < 1e-9, "{:?}", lines[0]); + assert!((lines[1].a - Vec3::new(0.0, 10.0, 0.0)).len() < 1e-9, "{:?}", lines[1]); +} + #[test] fn svg_viewbox_scaling_and_mm_units() { let svg = r#""#; From ea127d470204b2618e9e0fed3bfec0e838910fc4 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Tue, 3 Nov 2026 04:26:43 +0700 Subject: [PATCH 057/100] Dynamic blocks: a point on a frame edge of any length is on it (#2640) * Dynamic blocks: a point on a frame edge of any length is on it The frame edge test used Line::dist, which takes a segment shorter than EPS (1e-9) for its first point: a point halfway along a frame edge that short was neither on the edge nor, by the even-odd test, inside, so its line moved whole instead of stretching. The distance to an edge is now the exact one, clamped to the segment. Tests also cover two rules from #2627 that had none: a point on an edge's line but past its end is outside the frame, and a polyline tilted by only 5e-10 (about a unit off at an elevation of 2e9) keeps its stored vertex list. Co-Authored-By: Claude Opus 5.5 * Dynamic blocks: measure a frame edge's residual from its start segment_dist added the projection back to the edge's start before measuring, so near x = 36 the residual was rounded to that magnitude's spacing (7e-15). A point 1.4e-15 off an edge 5e-10 long, outside the frame, then came out 4e-20 away, within the edge's 5e-19 tolerance, and was taken as on it. The residual is now measured from the edge's start, where those digits are kept. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/doc/src/dynblock/apply.rs | 19 ++++++++--- crates/io/tests/it/dynblock_read.rs | 50 ++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/crates/doc/src/dynblock/apply.rs b/crates/doc/src/dynblock/apply.rs index d5651faac..ecca36257 100644 --- a/crates/doc/src/dynblock/apply.rs +++ b/crates/doc/src/dynblock/apply.rs @@ -415,6 +415,17 @@ fn inside(frame: &[Vec2], points: impl Iterator) -> Option (!idx.is_empty()).then_some(idx) } +/// The distance from `p` to the segment `a`-`b`, however short (`Line::dist` takes one shorter than +/// `EPS` for its first point). It is measured from `a`, so a short segment far from the origin keeps +/// the digits rounding there would drop. +fn segment_dist(a: Vec2, b: Vec2, p: Vec2) -> f64 { + let d = b - a; + let w = p - a; + let l2 = d.len2(); + let t = if l2 > 0.0 { (w.dot(d) / l2).clamp(0.0, 1.0) } else { 0.0 }; + (w - d * t).len() +} + /// Whether point `p` lies inside a stretch frame (two corners, or a polygon). A point on its edge is /// inside, for a polygon as for two corners: within a billionth of the edge's length of it. fn in_frame(frame: &[Vec2], p: Vec2) -> bool { @@ -425,8 +436,7 @@ fn in_frame(frame: &[Vec2], p: Vec2) -> bool { let mut j = f.len() - 1; for i in 0..f.len() { let (Some(pi), Some(pj)) = (f.get(i), f.get(j)) else { break }; - let edge = cadcraft_geom::Line { a: *pj, b: *pi }; - if edge.dist(p) <= 1e-9 * pi.dist(*pj) { + if segment_dist(*pj, *pi, p) <= 1e-9 * pi.dist(*pj) { return true; } if (pi.y > p.y) != (pj.y > p.y) && p.x < (pj.x - pi.x) * (p.y - pi.y) / (pj.y - pi.y) + pi.x { @@ -528,8 +538,9 @@ fn apply( // dynamic block has them, all of them when none is, as AutoCAD 2024 moves them. It // doesn't go by the vertex lists the action stores, which a block from another program // can get wrong. A polyline's vertices are in its own plane: in the block's when its - // extrusion is along z (a mirrored one is read flipped into it), so a tilted one keeps - // the stored list. + // extrusion is exactly along z (a mirrored one is read flipped into it). Any tilt, however + // small, moves them by as much as the elevation times the tilt, so a tilted one keeps the + // stored list. let idx = match def.entities.get(*h).map(|e| (&e.kind, e.common.extrusion)) { Some((EntityKind::Line(l), _)) if framed => inside(frame, [l.a.xy(), l.b.xy()].into_iter()), Some((EntityKind::LwPolyline(pl), n)) if framed && n.x == 0.0 && n.y == 0.0 => inside(frame, pl.vertices.iter().map(|v| v.p)), diff --git a/crates/io/tests/it/dynblock_read.rs b/crates/io/tests/it/dynblock_read.rs index bd9f2d1e1..251029410 100644 --- a/crates/io/tests/it/dynblock_read.rs +++ b/crates/io/tests/it/dynblock_read.rs @@ -482,13 +482,14 @@ fn a_zero_distance_multiplier_moves_as_one_without_its_angle_offset() { fn a_point_on_the_edge_of_a_polygon_frame_is_inside_it() { // Line 30 ends at (36, 0), and its start is outside every frame here. On the edge of the frame // (34,-2)-(36,2) given as two corners, the end is inside and moves alone. The same frame as a - // polygon, and triangles whose slanted edge passes through (36, 0), exactly or up to rounding, - // take it the same way. + // polygon, triangles whose slanted edge passes through (36, 0), exactly or up to rounding, and a + // square 5e-10 across with (36, 0) halfway along its right edge, take it the same way. let frames = [ "72 2\n1011 34\n1021 -2\n1011 36\n1021 2\n", "72 4\n1011 34\n1021 -2\n1011 36\n1021 -2\n1011 36\n1021 2\n1011 34\n1021 2\n", "72 3\n1011 34\n1021 -2\n1011 38\n1021 2\n1011 34\n1021 2\n", "72 3\n1011 35.7\n1021 -0.3\n1011 36.7\n1021 0.7\n1011 30\n1021 10\n", + "72 4\n1011 35.9999999995\n1021 -2.5e-10\n1011 36\n1021 -2.5e-10\n1011 36\n1021 2.5e-10\n1011 35.9999999995\n1021 2.5e-10\n", ]; for frame in frames { let src = fixture().replace("72 2\n1011 34\n1021 -2\n1011 38\n1021 2\n", frame); @@ -512,29 +513,62 @@ fn a_point_just_outside_a_tiny_frame_is_outside_it() { assert_eq!(shapes(&d, r)[0], [(-6.0, 0.0), (30.0, 0.0)]); } +#[test] +fn a_point_less_than_a_rounding_off_a_short_edge_is_outside_it() { + // A triangle frame whose right edge runs from (35.99999999999999, -2e-10) to + // (36.00000000000001, 3e-10): line 30's end (36, 0) is 1.4e-15 off it, past a billionth of the + // edge's length (5e-19), and outside the frame, so the line moves whole. Measured from the + // edge's start, the distance keeps digits that rounding near x = 36 would drop. + let frame = "72 3\n1011 35.99999999999999\n1021 -2e-10\n1011 36.00000000000001\n1021 3e-10\n1011 35.999999999\n1021 0\n"; + let src = fixture().replace("72 2\n1011 34\n1021 -2\n1011 38\n1021 2\n", frame); + assert_ne!(src, fixture()); + let (mut d, r) = door_reference(&src); + cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap(); + assert_eq!(shapes(&d, r)[0], [(-6.0, 0.0), (30.0, 0.0)]); +} + +#[test] +fn a_point_past_the_end_of_a_frame_edge_is_outside_it() { + // The frame's right edge runs along x = 36 from y = -2 to -1: line 30's end (36, 0) is on that + // edge's line but past its end, outside the frame, so the line moves whole. + let frame = "72 4\n1011 34\n1021 -2\n1011 36\n1021 -2\n1011 36\n1021 -1\n1011 34\n1021 -1\n"; + let src = fixture().replace("72 2\n1011 34\n1021 -2\n1011 38\n1021 2\n", frame); + assert_ne!(src, fixture()); + let (mut d, r) = door_reference(&src); + cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap(); + assert_eq!(shapes(&d, r)[0], [(-6.0, 0.0), (30.0, 0.0)]); +} + #[test] fn a_tilted_polyline_keeps_the_stored_vertex_list() { // Polyline 34 lies in a plane tilted about the y axis (extrusion (1,0,0)): its vertices are in // that plane's coordinates, not the block's, so the frame isn't tested against them and the - // stored list (vertex 0) is used, as before. Polyline 35 is mirrored (extrusion (0,0,-1)): it + // stored list (vertex 0) is used, as before; so is polyline 36's, tilted by only 5e-10 (at an + // elevation of 2e9 that moves it about a unit). Polyline 35 is mirrored (extrusion (0,0,-1)): it // is read flipped into the block's plane, (-20, 1) as (20, 1), so the frame is tested. - let polyline = |h: &str, [x0, x1]: [i32; 2], [ex, ey, ez]: [i32; 3]| { + let polyline = |h: &str, [x0, x1]: [i32; 2], [ex, ey, ez]: [f64; 3], elevation: f64| { format!( - "0 LWPOLYLINE\n5 {h}\n330 1D\n100 AcDbEntity\n8 0\n100 AcDbPolyline\n90 2\n70 0\n10 {x0}\n20 1\n10 {x1}\n20 1\n210 {ex}\n220 {ey}\n230 {ez}\n" + "0 LWPOLYLINE\n5 {h}\n330 1D\n100 AcDbEntity\n8 0\n100 AcDbPolyline\n90 2\n70 0\n38 {elevation}\n10 {x0}\n20 1\n10 {x1}\n20 1\n210 {ex}\n220 {ey}\n230 {ez}\n" ) }; let src = fixture() - .replace("71 1\n330 30\n1010 40", "71 3\n330 30\n330 34\n330 35\n1010 40") - .replace("73 1\n331 30\n74 1\n94 1\n75 0", "73 3\n331 30\n74 1\n94 1\n331 34\n74 1\n94 0\n331 35\n74 1\n94 0\n75 0") + .replace("71 1\n330 30\n1010 40", "71 4\n330 30\n330 34\n330 35\n330 36\n1010 40") + .replace("73 1\n331 30\n74 1\n94 1\n75 0", "73 4\n331 30\n74 1\n94 1\n331 34\n74 1\n94 0\n331 35\n74 1\n94 0\n331 36\n74 1\n94 0\n75 0") .replace( "21 25\n31 0\n0 ENDBLK\n", - &format!("21 25\n31 0\n{}{}0 ENDBLK\n", polyline("34", [20, 36], [1, 0, 0]), polyline("35", [-20, -36], [0, 0, -1])), + &format!( + "21 25\n31 0\n{}{}{}0 ENDBLK\n", + polyline("34", [20, 36], [1.0, 0.0, 0.0], 0.0), + polyline("35", [-20, -36], [0.0, 0.0, -1.0], 0.0), + polyline("36", [20, 36], [5e-10, 0.0, 1.0], 2e9) + ), ); let (mut d, r) = door_reference(&src); cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap(); let s = shapes(&d, r); assert_eq!(s[3], [(14.0, 1.0), (36.0, 1.0)]); assert_eq!(s[4], [(20.0, 1.0), (30.0, 1.0)]); + assert_eq!(s[5], [(14.0, 1.0), (36.0, 1.0)]); } #[test] From facae4a9a61ff8b894183cc2354e7d2246f6198d Mon Sep 17 00:00:00 2001 From: SoldierPePe Date: Tue, 3 Nov 2026 00:26:48 +0300 Subject: [PATCH 058/100] plot and exportpdf help name the `device` parameter (#2557) (#2567) * plot and exportpdf help name the `device` parameter (#2557) `cadcraft-cli commands plot` didn't list `device`, so the only way to find it was the `unknown plotter` error. PLOT's help now lists `device` with the built-in devices (CADCraft PDF, PNG, SVG), PLOTTERMANAGER plotters and "None". It also says the default is the page setup's device and that the device picks the output format and the papers. EXPORTPDF's help names `device` too, and notes that its output stays PDF. A test checks that both help texts name `device` and that PLOT's names every built-in device. * plot help: say what Model, "None" and an omitted paper do In model space, and with "None", no device is used and the plot is a PDF. When `paper` is omitted, a layout keeps its page setup's paper and model space takes the device's default. --- crates/engine/src/cmd/layout.rs | 4 ++-- crates/engine/src/cmd/layout/tests.rs | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index efe9bf13f..0c78e19cb 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -84,11 +84,11 @@ pub fn specs() -> Vec { .menu(&["File", "Print..."]) .key("Cmd+P") .alias(&["print"]) - .params("{path? (else returns base64 `data`), layout?: current|\"Model\", paper?, landscape?, fit?: bool, scale?: number | \"1:50\", center?: bool (Model, default true), offset?: [x,y] mm (Model, when center is false; each within ±100000), lineweights?: bool, plotTransparency?: bool (default: the page setup's; PLOTTRANSPARENCYOVERRIDE 0/2 overrides it), plotArea?: extents|display|limits|window (Model) | layout, window?: [[x1,y1],[x2,y2]]} (typed or from the menu: the Plot dialog)") + .params("{path? (else returns base64 `data`), layout?: current|\"Model\", device?: \"CADCraft PDF\"|\"CADCraft PNG\"|\"CADCraft SVG\" | a plotter from PLOTTERMANAGER | \"None\" (default: a layout's page setup device; Model, or \"None\": no device, PDF; a device sets the output format and the papers), paper?: the device's paper (default: a layout keeps its page setup's; Model takes the device's default), landscape?, fit?: bool, scale?: number | \"1:50\", center?: bool (Model, default true), offset?: [x,y] mm (Model, when center is false; each within ±100000), lineweights?: bool, plotTransparency?: bool (default: the page setup's; PLOTTRANSPARENCYOVERRIDE 0/2 overrides it), plotArea?: extents|display|limits|window (Model) | layout, window?: [[x1,y1],[x2,y2]]} (typed or from the menu: the Plot dialog)") .noundo(), CommandSpec::new("exportpdf", "Export to PDF...", |s, p| run_plot(s, &super::plotters::plot_options(s, p, "exportpdf")?, "exportpdf")) .menu(&["File", "Export to PDF..."]) - .params("{path? (else returns base64 `data`), layout?, paper?, landscape?, fit?, scale?, center?: bool (Model, default true), offset?: [x,y] mm (Model, when center is false; each within ±100000), lineweights?, plotTransparency?, plotArea?, window?} (as PLOT)") + .params("{path? (else returns base64 `data`), layout?, device? (its papers; the output stays PDF), paper?, landscape?, fit?, scale?, center?: bool (Model, default true), offset?: [x,y] mm (Model, when center is false; each within ±100000), lineweights?, plotTransparency?, plotArea?, window?} (as PLOT)") .noundo(), CommandSpec::new("mspace", "Model Space (in viewport)", run_mspace) .alias(&["ms"]) diff --git a/crates/engine/src/cmd/layout/tests.rs b/crates/engine/src/cmd/layout/tests.rs index 03356b892..4a631ccb0 100644 --- a/crates/engine/src/cmd/layout/tests.rs +++ b/crates/engine/src/cmd/layout/tests.rs @@ -293,3 +293,17 @@ fn mspace_edits_model_through_viewport() { s.execute("layout.set", &json!({"name": "Model"})).unwrap(); assert!(s.state().unwrap().mspace.is_none()); } + +/// PLOT and EXPORTPDF help name the `device` parameter, and PLOT's help every built-in device (#2557). +#[test] +fn plot_help_names_the_device_and_the_built_in_devices() { + for id in ["plot", "exportpdf"] { + let help = crate::find_command(id).map(|c| c.params).unwrap_or_default(); + assert!(help.contains("device?"), "{id}: {help}"); + } + let help = crate::find_command("plot").map(|c| c.params).unwrap_or_default(); + for name in ["CADCraft PDF", "CADCraft PNG", "CADCraft SVG"] { + assert!(super::super::plotters::find_device(name).is_some(), "{name} is not a device"); + assert!(help.contains(name), "plot help lacks {name}"); + } +} From 0e7c71759cede6da1b2c0df6660e94fdaccf64fe Mon Sep 17 00:00:00 2001 From: Tyler Chiriboga Date: Mon, 2 Nov 2026 15:26:53 -0600 Subject: [PATCH 059/100] test(ui): the IME double-click test passes without craft-fonts (#2652) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `double_clicked_text_takes_ime_composition_and_commit` commits 中文 at the command line. Without a CJK font (no CRAFT_FONTS_DIR, none installed, as on a plain Linux CI image) the kittest harness panics on the first missing glyph: "No glyph for 中 in Proportional". Code and tests must work without craft-fonts. The test now asks the harness's own fonts whether they can draw 中文 and, only when they can't, lets the command line draw boxes for it. The IME path it checks is unchanged: the pre-edit is shown and not saved, the commit is inserted once, Enter stores 中文 in the text. With a CJK font the harness still fails on any missing glyph. Co-authored-by: Tyler Chiriboga <18270301+Tyler-Chiriboga@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/tests/it/text_double_click.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/ui-egui/tests/it/text_double_click.rs b/crates/ui-egui/tests/it/text_double_click.rs index 5cedfbf61..f585ae46a 100644 --- a/crates/ui-egui/tests/it/text_double_click.rs +++ b/crates/ui-egui/tests/it/text_double_click.rs @@ -73,6 +73,16 @@ fn double_clicked_text_takes_ime_composition_and_commit() { let mut h = harness(); double_click(&mut h, Vec2::new(4.0, 1.2)); assert_eq!(h.state().session.current_prompt().unwrap().message, "Enter new text"); + // Without a CJK font (none installed, no craft-fonts) the command line draws 中文 as boxes, + // which the harness takes for a bug. The IME path below is the same either way. + let drawable = h.ctx.fonts_mut(|view| { + [egui::FontFamily::Proportional, egui::FontFamily::Monospace] + .into_iter() + .all(|family| view.has_glyphs(&egui::FontId::new(12.0, family), "中文")) + }); + if !drawable { + h.ctx.set_missing_glyph_policy(egui::MissingGlyphPolicy::Tofu); + } // Composition is shown, not saved; the commit is inserted exactly once (#1761). h.event(Event::Ime(ImeEvent::Preedit { text: "zhong".into(), active_range_chars: None })); h.run_steps(1); From f2cb7e6e8f704c37b299b42d64f8bb9a9724d632 Mon Sep 17 00:00:00 2001 From: Rahul Date: Tue, 3 Nov 2026 02:56:59 +0530 Subject: [PATCH 060/100] i18n(zh): translate the 74 strings the report listed as missing (#2638) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Chinese catalog lacked 74 interface strings: the block editor prompts and dialog, the alias editor sentences, the selection visual effects and preview filter, units of new drawings, and the menu names Dimension, Draw, Modify and View. Adds them in the catalog's TSV format with the AutoCAD zh-CN vocabulary the catalog already uses (夹点, 图案填充, 外部参照, 多行文字, 孤岛). After this the report lists nothing missing for zh. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- crates/ui-egui/src/i18n/zh.tsv | 76 ++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/crates/ui-egui/src/i18n/zh.tsv b/crates/ui-egui/src/i18n/zh.tsv index 21c86d1fe..9ca85fea7 100644 --- a/crates/ui-egui/src/i18n/zh.tsv +++ b/crates/ui-egui/src/i18n/zh.tsv @@ -1381,3 +1381,79 @@ Save Layout as Template 将布局另存为样板 Set Dynamic Property 设置动态特性 Units 单位 + +# The strings the report listed as missing for zh. + Add… 添加… + Alias 别名 + Alias file: {file} 别名文件:{file} + Associative 关联 + AutoLISP (LSP) AutoLISP (LSP) + B B + Backup copy 备份副本 + Block to create or edit 要创建或编辑的块 + Choose the command the alias starts. 选择别名要启动的命令。 + Click the ruler to add a tab stop, right-click to remove one 单击标尺可添加制表位,右键单击可将其删除 + Command 命令 + Create backup copy with each save 每次保存时创建备份副本 + Crossing selection color: 交叉选择颜色: + Custom 自定义 + Define each corner of the selection window with a separate click. 分别单击以定义选择窗口的各个角点。 + Discard 放弃 + Edit Block Definition 编辑块定义 + Edit… 编辑… + Exclude: 排除: + Font 字体 + Grips 夹点 + Hatches 图案填充 + Imperial (inches) 英制(英寸) + Indicate selection area 指示选择区域 + Interface scale 界面缩放 + Island 孤岛 + Italic 斜体 + Metric (millimetres) 公制(毫米) + More than {n} aliases. 超过 {n} 个别名。 + Multiline text 多行文字 + New and the first drawing at startup use these units unless a default template is set (QNEWTEMPLATE). 启动时的新建图形和第一个图形使用这些单位,除非设置了默认样板 (QNEWTEMPLATE)。 + New block 新建块 + No alias file loaded. REINIT replaces these aliases with a file's. 未加载别名文件。REINIT 将用文件中的别名替换这些别名。 + Object grouping 对象编组 + Object selection limit for display of grips 显示夹点时对象选择的数目上限 + Objects on locked layers 锁定图层上的对象 + Open in Block Editor 在块编辑器中打开 + Origin: {x}, {y} 原点:{x}, {y} + Overline 上划线 + Save alias 保存别名 + Save changes to block "{name}"? 保存对块“{name}”的更改吗? + Select boundaries to remove 选择要删除的边界 + Select using two clicks only 仅用两次单击选择 + Selection Area Effect 选择区域效果 + Selection Preview Filter 选择预览过滤器 + Selection area opacity: 选择区域不透明度: + Selection effect color 选择效果颜色 + Selection modes 选择模式 + Selection preview 选择预览 + Show grips 显示夹点 + Tables 表格 + Text color 文字颜色 + This drawing 此图形 + Type an alias name. 输入别名名称。 + Underline 下划线 + Units of new drawings 新图形的单位 + Unsaved changes 未保存的更改 + Visual Effect Settings 视觉效果设置 + Visual Effect Settings... 视觉效果设置... + When a command is active 命令处于活动状态时 + When no command is active 无活动命令时 + Window selection color: 窗口选择颜色: + X X + Xrefs 外部参照 + Y Y + built-in 内置 + contains authoring elements. Open in Block Editor? 包含编写元素。在块编辑器中打开吗? + overrides built-in 覆盖内置 + {alias} is already defined; select it and use Edit. {alias} 已定义;请选择它并使用编辑。 + {command} is an unknown command. {command} 是未知命令。 +menu Dimension 标注 +menu Draw 绘图 +menu Modify 修改 +menu View 视图 From a60d437f0a5c325e462daecaeaaf79f551b91366 Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 14:27:33 -0700 Subject: [PATCH 061/100] WBLOCK: new handles start above those of the copied blocks (#1996) (#2659) Co-authored-by: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> --- crates/engine/src/cmd/blocks/wblock.rs | 5 ++ crates/engine/tests/wblock_units_handles.rs | 57 +++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 crates/engine/tests/wblock_units_handles.rs diff --git a/crates/engine/src/cmd/blocks/wblock.rs b/crates/engine/src/cmd/blocks/wblock.rs index bc2ff711a..e06e3a71a 100644 --- a/crates/engine/src/cmd/blocks/wblock.rs +++ b/crates/engine/src/cmd/blocks/wblock.rs @@ -113,6 +113,11 @@ fn with_objects(src: &Drawing, ents: impl Iterator) -> Drawing { d.scales = src.scales.clone(); d.blocks = src.blocks.clone(); d.header.set("INSBASE", cadcraft_doc::HVal::Point(Vec3::ZERO)); + // The copied blocks keep their objects' handles, so the new ones start above them. + let kept: Vec = d.blocks.values().flat_map(|b| b.entities.iter().map(|e| e.handle)).collect(); + for h in kept { + d.bump_handseed(h); + } for mut e in ents { e.handle = d.new_handle(); d.model.push(e); diff --git a/crates/engine/tests/wblock_units_handles.rs b/crates/engine/tests/wblock_units_handles.rs new file mode 100644 index 000000000..52a4d6053 --- /dev/null +++ b/crates/engine/tests/wblock_units_handles.rs @@ -0,0 +1,57 @@ +//! WBLOCK writes the source's units, only the blocks the written objects use, and handles that +//! never repeat. + +use cadcraft_engine::Session; +use cadcraft_engine::cmd::file::{IoHooks, set_io}; +use cadcraft_engine::doc::Drawing; +use serde_json::json; + +/// Writes "insunits;block names;handles;distinct handles" so the test can read what would be saved. +fn write(d: &Drawing, _path: &str) -> Result, String> { + let mut handles: Vec = d.model.iter().map(|e| e.handle.0).collect(); + let mut names = Vec::new(); + for b in d.blocks.values() { + names.push(b.name.clone()); + handles.extend(b.entities.iter().map(|e| e.handle.0)); + } + let total = handles.len(); + handles.sort_unstable(); + handles.dedup(); + Ok(format!("{};{};{};{}", d.header.i64("INSUNITS", -1), names.join(","), total, handles.len()).into_bytes()) +} + +fn written(s: &mut Session, p: serde_json::Value) -> String { + let path = std::env::temp_dir().join(format!("cadcraft-wblock-units-{}.dxf", std::process::id())); + let mut p = p; + p["path"] = json!(path.to_string_lossy()); + s.execute("wblock", &p).unwrap(); + let text = std::fs::read_to_string(&path).unwrap(); + let _ = std::fs::remove_file(&path); + text +} + +fn line(s: &mut Session, y: f64) -> String { + s.execute("line", &json!({ "points": [[0, y], [1, y]] })).unwrap()["handles"][0].as_str().unwrap().to_string() +} + +#[test] +fn wblock_keeps_units_drops_unused_blocks_and_repeats_no_handle() { + set_io(IoHooks { read: |_, _| Err("no reader in this test".into()), write, plot: None }); + let mut s = Session::new(); + s.execute("units", &json!({ "insunits": 6 })).unwrap(); + // Block "Part" is made first, so its line has one of the drawing's lowest handles. + let p = line(&mut s, 0.0); + let ins = s.execute("block", &json!({ "name": "Part", "handles": [p], "keep": "convert" })).unwrap()["insert"].as_str().unwrap().to_string(); + let mut picked = vec![ins]; + for i in 1..300 { + picked.push(line(&mut s, f64::from(i) * 0.01)); + } + let u = line(&mut s, 5.0); + s.execute("block", &json!({ "name": "Unused", "handles": [u], "keep": "delete" })).unwrap(); + + // 300 objects (the insert and 299 lines) plus Part's line: the source's units, no Unused, + // and every handle distinct although the new objects are numbered from the start again. + assert_eq!(written(&mut s, json!({ "handles": picked })), "6;Part;301;301"); + // A block written by name: its objects go to model space, its definition stays behind. + assert_eq!(written(&mut s, json!({ "name": "Unused" })), "6;;1;1"); +} From 0f56c16d00902d7ea5475fc6858aea924c0e558e Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 14:27:45 -0700 Subject: [PATCH 062/100] EXPORT and WBLOCK say what a DWG file leaves out (#2240) (#2660) Co-authored-by: assamaral <217119886+assamaral@users.noreply.github.com> --- crates/engine/src/cmd/blocks/wblock.rs | 40 ++++++++++++++----- crates/engine/src/cmd/exchange.rs | 14 ++++--- crates/engine/src/cmd/file.rs | 2 +- crates/engine/tests/save_reports_not_saved.rs | 32 +++++++++++++++ 4 files changed, 72 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/cmd/blocks/wblock.rs b/crates/engine/src/cmd/blocks/wblock.rs index e06e3a71a..fb03fe1af 100644 --- a/crates/engine/src/cmd/blocks/wblock.rs +++ b/crates/engine/src/cmd/blocks/wblock.rs @@ -1,11 +1,14 @@ //! WBLOCK (its JSON form) and -WBLOCK at the command line: objects, a block or the whole drawing //! written to a drawing file. +use std::collections::BTreeMap; + use cadcraft_doc::{Drawing, Entity, Handle}; use cadcraft_geom::{Mat3, Vec2, Vec3}; use serde_json::{Value, json}; use super::super::curves::KW; +use super::super::file::{drops_unknown, not_saved, not_saved_message, report_not_saved}; use super::super::machines::{SelOutcome, SelectPhase}; use super::super::modify::erase_kept; use super::super::{CommandSpec, bad, point_param, str_param, targets}; @@ -40,11 +43,13 @@ enum Source<'a> { fn run_wblock(s: &mut Session, p: &Value) -> Result { let path = str_param(p, "path").ok_or_else(|| bad("wblock", "`path` is required"))?.to_string(); let base = point_param(p, "base"); - let (entities, bytes) = match str_param(p, "name") { + let (entities, bytes, lost) = match str_param(p, "name") { Some(name) => write(s, "wblock", &path, Source::Block(name, base))?, None => write(s, "wblock", &path, Source::Objects(&targets(s, p)?, base.unwrap_or(Vec2::ZERO)))?, }; - Ok(json!({ "path": path, "entities": entities, "bytes": bytes })) + let mut r = json!({ "path": path, "entities": entities, "bytes": bytes }); + report_not_saved(s, &mut r, &lost); + Ok(r) } fn run_dash_wblock(s: &mut Session, p: &Value) -> Result { @@ -53,7 +58,7 @@ fn run_dash_wblock(s: &mut Session, p: &Value) -> Result { } let path = str_param(p, "path").ok_or_else(|| bad("-wblock", "`path` is required"))?.to_string(); let base = point_param(p, "base"); - let (entities, bytes) = match str_param(p, "name") { + let (entities, bytes, lost) = match str_param(p, "name") { Some(name) => write(s, "-wblock", &path, Source::Block(name, base))?, None => { let hs = targets(s, p)?; @@ -66,14 +71,17 @@ fn run_dash_wblock(s: &mut Session, p: &Value) -> Result { written } }; - Ok(json!({ "path": path, "entities": entities, "bytes": bytes })) + let mut r = json!({ "path": path, "entities": entities, "bytes": bytes }); + report_not_saved(s, &mut r, &lost); + Ok(r) } -/// Write `what` to `path` through the io hooks; the objects written and the file's size. The +/// Write `what` to `path` through the io hooks; the objects written, the file's size and what a +/// DWG file left out, for the caller to report as a save does. The /// written drawing keeps the source's settings and styles, as AutoCAD's -WBLOCK does: its units /// ($INSUNITS, $MEASUREMENT), so a block from a metric drawing is not declared in inches and /// scaled on insert, and its defaults (text size, current styles, annotation scales). -fn write(s: &Session, cmd: &str, path: &str, what: Source) -> Result<(usize, usize)> { +fn write(s: &Session, cmd: &str, path: &str, what: Source) -> Result<(usize, usize, BTreeMap)> { let src = s.doc()?; // The base point becomes the written drawing's origin: by default 0,0 for objects and the // block's base point for a block. @@ -94,7 +102,15 @@ fn write(s: &Session, cmd: &str, path: &str, what: Source) -> Result<(usize, usi // Through a temporary file, as SAVE writes, so a failed write keeps the file it replaces. #[cfg(not(target_arch = "wasm32"))] crate::atomic_write::write_atomic(std::path::Path::new(path), &bytes).map_err(|e| bad(cmd, format!("{path}: {e}")))?; - Ok((d.model.len(), bytes.len())) + let lost = if drops_unknown(path) { not_saved(&d) } else { BTreeMap::new() }; + Ok((d.model.len(), bytes.len(), lost)) +} + +/// Say on the command line what the written DWG file left out. +fn echo_not_saved(s: &mut Session, lost: &BTreeMap) { + if let Some(msg) = not_saved_message(lost) { + s.echo(msg); + } } /// A new drawing of `ents`, with `src`'s settings and the layers, linetypes, styles and blocks @@ -310,8 +326,9 @@ impl DashWblockM { } fn write(&self, s: &mut Session, what: Source) -> Result { - if let Err(e) = write(s, "-wblock", &self.path, what) { - s.echo(e.to_string()); + match write(s, "-wblock", &self.path, what) { + Ok((.., lost)) => echo_not_saved(s, &lost), + Err(e) => s.echo(e.to_string()), } Ok(Step::Done) } @@ -321,7 +338,10 @@ impl DashWblockM { return Ok(Step::Done); } match write(s, "-wblock", &self.path, Source::Objects(hs, self.base)) { - Ok(_) => erase(s, hs)?, + Ok((.., lost)) => { + echo_not_saved(s, &lost); + erase(s, hs)?; + } Err(e) => s.echo(e.to_string()), } Ok(Step::Done) diff --git a/crates/engine/src/cmd/exchange.rs b/crates/engine/src/cmd/exchange.rs index f6ce1e9b6..d4993566a 100644 --- a/crates/engine/src/cmd/exchange.rs +++ b/crates/engine/src/cmd/exchange.rs @@ -117,14 +117,18 @@ fn run_export(s: &mut Session, p: &Value) -> Result { if !EXPORT_FORMATS.contains(&format.as_str()) { return Err(bad("export", "`format` must be dxf, dwg, svg, png or pdf (or give a `path` with one of those extensions)")); } - let bytes = (hooks.write)(s.doc()?, &format!("export.{format}")).map_err(|e| bad("export", e))?; - match path { + let file = format!("export.{format}"); + let lost = super::file::loss_on_save(s, &file); + let bytes = (hooks.write)(s.doc()?, &file).map_err(|e| bad("export", e))?; + let mut r = match path { Some(path) => { write_file("export", path, &bytes)?; - Ok(json!({ "path": path, "format": format, "bytes": bytes.len(), "message": format!("Exported to {path}") })) + json!({ "path": path, "format": format, "bytes": bytes.len(), "message": format!("Exported to {path}") }) } - None => Ok(json!({ "data": base64_encode(&bytes), "format": format, "bytes": bytes.len() })), - } + None => json!({ "data": base64_encode(&bytes), "format": format, "bytes": bytes.len() }), + }; + super::file::report_not_saved(s, &mut r, &lost); + Ok(r) } // ---------- IMPORT ---------- diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 3b24e87bd..6a8d50384 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -801,7 +801,7 @@ fn is_drawing_file(path: impl AsRef) -> bool { } /// Say on the command line what the file couldn't keep, and list it in the result's `notSaved`. -fn report_not_saved(s: &mut Session, r: &mut Value, lost: &std::collections::BTreeMap) { +pub(super) fn report_not_saved(s: &mut Session, r: &mut Value, lost: &std::collections::BTreeMap) { let Some(msg) = not_saved_message(lost) else { return }; s.echo(msg); if let Some(o) = r.as_object_mut() { diff --git a/crates/engine/tests/save_reports_not_saved.rs b/crates/engine/tests/save_reports_not_saved.rs index 24a2dff92..e31f2ec77 100644 --- a/crates/engine/tests/save_reports_not_saved.rs +++ b/crates/engine/tests/save_reports_not_saved.rs @@ -52,6 +52,38 @@ fn save_lists_the_entities_the_file_does_not_keep() { assert!(r.get("notSaved").is_none() && s.log.len() == n); } +#[test] +fn export_and_wblock_say_what_a_dwg_leaves_out() { + set_io(IoHooks { read: |_, _| Err("no reader in this test".into()), write: |_, _| Ok(b"0\nEOF\n".to_vec()), plot: None }); + let mut d = Drawing::new_imperial(); + let h = d.add(&Space::Model, Common::default(), unknown("ACME_WIDGET")).unwrap().hex(); + let mut s = Session::empty(); + s.open_drawing(d, "foreign.dxf", None); + let file = |ext: &str| std::env::temp_dir().join(format!("cadcraft-leaves-out-{}.{ext}", std::process::id())).to_string_lossy().into_owned(); + let msg = "Not saved in the DWG file (its writer can't write these object types yet; DXF keeps them): 1 ACME_WIDGET"; + + let (dwg, dxf) = (file("dwg"), file("dxf")); + for (cmd, p) in [("export", json!({ "path": dwg })), ("wblock", json!({ "path": dwg, "handles": [h] }))] { + s.log.clear(); + let r = s.execute(cmd, &p).unwrap(); + assert_eq!(r["notSaved"], json!({ "ACME_WIDGET": 1 }), "{cmd}"); + assert!(s.log.iter().any(|l| l == msg), "{cmd}: {:?}", s.log); + } + // -WBLOCK takes the objects out of the drawing, so last. + let r = s.execute("-wblock", &json!({ "path": dwg, "handles": [h] })).unwrap(); + assert_eq!(r["notSaved"], json!({ "ACME_WIDGET": 1 })); + let _ = std::fs::remove_file(&dwg); + + // DXF keeps them. + s.execute("undo", &json!({})).unwrap(); + for (cmd, p) in [("export", json!({ "path": dxf })), ("wblock", json!({ "path": dxf, "handles": [h] }))] { + s.log.clear(); + assert!(s.execute(cmd, &p).unwrap().get("notSaved").is_none(), "{cmd}"); + assert!(!s.log.iter().any(|l| l == msg), "{cmd}: {:?}", s.log); + } + let _ = std::fs::remove_file(&dxf); +} + #[test] fn templates_report_what_dwg_does_not_keep() { use cadcraft_engine::cmd::file::drops_unknown; From 97679d8cffa6d0d57b84d52eb25b18a14d429ebd Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 14:29:51 -0700 Subject: [PATCH 063/100] Ask before a save drops solids from a 2013+ DXF (#2334) The interactive save only knew about DWG, so overwriting a 2013+ DXF lost SAT-text solids on a command-line note. loss_on_save now lists them, and EXPORT reports them with the DXF wording. --- crates/engine/src/cmd/exchange.rs | 3 +- crates/engine/src/cmd/file.rs | 18 ++++++++++- crates/engine/tests/save_reports_not_saved.rs | 32 +++++++++++++++++++ crates/ui-egui/src/save_loss.rs | 10 ++++-- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/exchange.rs b/crates/engine/src/cmd/exchange.rs index d4993566a..1be19a408 100644 --- a/crates/engine/src/cmd/exchange.rs +++ b/crates/engine/src/cmd/exchange.rs @@ -118,7 +118,6 @@ fn run_export(s: &mut Session, p: &Value) -> Result { return Err(bad("export", "`format` must be dxf, dwg, svg, png or pdf (or give a `path` with one of those extensions)")); } let file = format!("export.{format}"); - let lost = super::file::loss_on_save(s, &file); let bytes = (hooks.write)(s.doc()?, &file).map_err(|e| bad("export", e))?; let mut r = match path { Some(path) => { @@ -127,7 +126,7 @@ fn run_export(s: &mut Session, p: &Value) -> Result { } None => json!({ "data": base64_encode(&bytes), "format": format, "bytes": bytes.len() }), }; - super::file::report_not_saved(s, &mut r, &lost); + super::file::report_loss(s, &mut r, &file); Ok(r) } diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 80244bc21..012a1d381 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -794,13 +794,29 @@ pub fn not_saved_message(lost: &std::collections::BTreeMap) -> Op /// when the format keeps everything. The interactive save asks with this; [`save_reporting`] /// reports the same list afterwards to callers that never get a dialog. pub fn loss_on_save(s: &Session, path: &str) -> std::collections::BTreeMap { + let version = drawing_version(s, path); + if is_dxf_file(path) && version >= Version::R2013 { + // The drawing a file holds: the host while the Block Editor is open. + return s.state().map(|st| cadcraft_doc::foreign::sat_text_dropped(st.drawing())).unwrap_or_default(); + } if !drops_unknown(path) { return Default::default(); } - let version = drawing_version(s, path); s.doc().map(|d| not_saved_as(d, version)).unwrap_or_default() } +/// Report what the file `path`, just written at the drawing's own version, left out. +pub(super) fn report_loss(s: &mut Session, r: &mut Value, path: &str) { + if is_dxf_file(path) { + if drawing_version(s, path) >= Version::R2013 { + report_sat_text_dropped(s, r); + } + } else { + let lost = loss_on_save(s, path); + report_not_saved(s, r, &lost); + } +} + /// Whether saving to `path` leaves out the entities [`not_saved`] counts: DWG does (the DWG /// bridge drops types it doesn't know); DXF writes them back as read. pub fn drops_unknown(path: impl AsRef) -> bool { diff --git a/crates/engine/tests/save_reports_not_saved.rs b/crates/engine/tests/save_reports_not_saved.rs index cd73f637d..984d0d635 100644 --- a/crates/engine/tests/save_reports_not_saved.rs +++ b/crates/engine/tests/save_reports_not_saved.rs @@ -150,6 +150,38 @@ fn saving_sat_text_solids_as_dxf_2013_reports_them() { assert!(r.get("notSaved").is_none() && s.log.len() == n); } +/// The interactive save asks with the same list, and EXPORT reports it, so overwriting a 2013+ +/// DXF never drops solids on a command-line note alone (#2334). +#[test] +fn a_dxf_2013_loss_is_known_before_saving() { + use cadcraft_engine::cmd::file::loss_on_save; + set_io(IoHooks { read: |_, _| Err("no reader in this test".into()), write: |_, _| Ok(b"0 +EOF +".to_vec()), plot: None }); + let sat = |d: &mut Drawing| { + let tags = vec![RawTag { code: 8, value: "0".into() }, RawTag { code: 1, value: "400 1 1 0".into() }]; + d.add(&Space::Model, Common::default(), EntityKind::Unknown(Unknown { dxf_type: "REGION".into(), tags })).unwrap(); + }; + let mut modern = Drawing::new_imperial(); + modern.header.set_str("ACADVER", "AC1032"); + sat(&mut modern); + let mut s = Session::empty(); + s.open_drawing(modern, "region.dxf", None); + assert_eq!(loss_on_save(&s, "region.dxf"), [("REGION".to_string(), 1)].into_iter().collect()); + + let path = std::env::temp_dir().join(format!("cadcraft-sat-export-{}.dxf", std::process::id())); + let r = s.execute("export", &json!({ "path": path.to_string_lossy() })).unwrap(); + let _ = std::fs::remove_file(&path); + assert_eq!(r["notSaved"], json!({ "REGION": 1 })); + assert!(s.log.iter().any(|l| l.contains("Not saved in the DXF file")), "{:?}", s.log); + + let mut old = Drawing::new_imperial(); + old.header.set_str("ACADVER", "AC1024"); + sat(&mut old); + s.open_drawing(old, "region.dxf", None); + assert!(loss_on_save(&s, "region.dxf").is_empty(), "2010 keeps SAT text"); +} + /// In the Block Editor the file still holds the host drawing: its solids and version decide the /// note, not the edited block's (#2334). #[test] diff --git a/crates/ui-egui/src/save_loss.rs b/crates/ui-egui/src/save_loss.rs index 50e77978b..2b0a28c57 100644 --- a/crates/ui-egui/src/save_loss.rs +++ b/crates/ui-egui/src/save_loss.rs @@ -1,5 +1,6 @@ //! Asking before a save throws content away. DWG can't yet hold everything a drawing read from -//! another program keeps (`cadcraft_engine::cmd::file::not_saved`), and QSAVE replaces the file +//! another program keeps (`cadcraft_engine::cmd::file::not_saved`), a DXF of 2013 or later can't +//! hold a solid's SAT text (`cadcraft_doc::foreign::sat_text_dropped`), and QSAVE replaces the file //! that still has it, so File ▸ Save, its shortcut or typing QSAVE says what would go first: //! Save Anyway / Save As... / Cancel. Agreeing lasts for that same loss on that same file, so //! later saves in the session don't ask again; the file no longer holds what was agreed to. @@ -120,7 +121,12 @@ pub fn prompt(app: &mut CadApp, ctx: &egui::Context) { ui.label(RichText::new(crate::tf!("Saving {name} will remove content", name = file)).strong()); ui.add_space(4.0); ui.label(crate::tf!("This format can't keep these objects yet: {objects}.", objects = pending.summary())); - ui.label(RichText::new(crate::tl!("DXF keeps them. Use Save As to keep them in the drawing.")).color(t.text_dim)); + let keeps = if path.extension().is_some_and(|e| e.eq_ignore_ascii_case("dxf")) { + crate::tl!("DXF 2010 and earlier keep them. Use Save As to keep them in the drawing.") + } else { + crate::tl!("DXF keeps them. Use Save As to keep them in the drawing.") + }; + ui.label(RichText::new(keeps).color(t.text_dim)); let on_disk = if save_keeps_current(path) { crate::tf!("The current file is kept as {name} until the next save.", name = backup) } else if isavebak() { From 8de925e1d32b397073e50786ac84fa130896a814 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Tue, 3 Nov 2026 04:36:32 +0700 Subject: [PATCH 064/100] Write option prompts without "or", as AutoCAD does (#2540) * Write option prompts without "or", as AutoCAD does AutoCAD writes " or [" only when a prompt asks for something besides its keywords ("Enter block name or [?]"); a prompt asking for one of them has none ("Enter an option [...]", "Enter layout option [...]", "New color [Truecolor/COlorbook]"). The 27 messages read without "or" from AutoCAD 2024's command line are listed in NO_OR and displayed that way. Co-Authored-By: Claude Opus 5.5 * Address review: the window's command line drops "or" as the prompt does The command line in the window draws a prompt's keywords itself and wrote " or [" after every message, so -LAYER still read "Enter an option or [?/Make/...]" there, and questions such as AUDIT's had "or" too. Only the prompt text (the control channel, the state JSON, the tests) had dropped it. Prompt::keyword_lead gives what goes before the keywords; display() and the command line both use it, and the command line translates only " or [". A render test reads what the command line draws, in English and German, for -LAYER, LINE and AUDIT. Suggested in review. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/prompt.rs | 66 +++++++++++++++++++---- crates/engine/tests/it/assoc_array.rs | 2 +- crates/engine/tests/it/layout_prompts.rs | 2 +- crates/engine/tests/it/vplayer_prompts.rs | 4 +- crates/ui-egui/src/cmdline.rs | 7 ++- crates/ui-egui/src/i18n/tests.rs | 29 ++++++++++ 6 files changed, 96 insertions(+), 14 deletions(-) diff --git a/crates/engine/src/prompt.rs b/crates/engine/src/prompt.rs index 1563be702..e3498dcf0 100644 --- a/crates/engine/src/prompt.rs +++ b/crates/engine/src/prompt.rs @@ -26,6 +26,41 @@ impl Accept { pub const SELECT: Accept = Accept { point: true, number: false, text: false, select: true, enter: true }; } +/// Prompts that ask for one of their keywords, which AutoCAD 2024 writes without "or" before them: +/// "Enter an option [?/Make/Set/…]:", "Enter layout option [Copy/Delete/…/?] :", "New color +/// [Truecolor/COlorbook] :". A prompt asking for something else besides its keywords has +/// it: "Enter block name or [?]:", "Specify insertion base point or [Basepoint/…]:". Read from +/// AutoCAD's command line over measured runs, where no prompt was written both ways. +const NO_OR: [&str; 27] = [ + "Bring to front", + "Calculate rectangle dimensions based on", + "Choose mode for selected object conversion", + "Create annotative block", + "Enter a configuration option", + "Enter a plotting preference", + "Enter an annotation option", + "Enter an option", + "Enter an option to change", + "Enter attribute visibility setting", + "Enter justification", + "Enter layout option", + "Enter leader format option", + "Enter line spacing type", + "Enter object ordering option", + "Enter polyline linetype generation option", + "Enter property to change", + "Enter the type of array", + "Enter type of unused objects to purge", + "Enter viewport arrangement", + "Explode block", + "New color", + "Orient relative to sheet in paper space viewports", + "Select a content type", + "Select a leader type", + "Use landing", + "Viewport View Locking", +]; + /// A prompt shown on the command line. #[derive(Clone, Debug, Default, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] @@ -148,15 +183,7 @@ impl Prompt { } let mut s = self.message.clone(); if !self.keywords.is_empty() { - // A question offers its answers ("Fix any errors detected? [Yes/No]"); anything else - // offers options besides what it asks for ("Specify next point or [Undo]"). - s.push_str(if self.message.is_empty() { - "[" - } else if self.message.ends_with('?') { - " [" - } else { - " or [" - }); + s.push_str(self.keyword_lead()); s.push_str(&self.keywords.join("/")); s.push(']'); } @@ -166,6 +193,20 @@ impl Prompt { s.push(':'); s } + /// What goes between the message and its keywords. A question offers its answers ("Fix any + /// errors detected? [Yes/No]"), and so does a prompt for an option ([`NO_OR`]): " [". Anything + /// else offers options besides what it asks for ("Specify next point or [Undo]"): " or [". With + /// no message, "[". The command line in the window draws the keywords itself and takes this + /// too. + pub fn keyword_lead(&self) -> &'static str { + if self.message.is_empty() { + "[" + } else if self.message.ends_with('?') || NO_OR.contains(&self.message.as_str()) { + " [" + } else { + " or [" + } + } /// Match typed text against keywords: full word or the capitalised shortcut, case-insensitive. pub fn match_keyword(&self, text: &str) -> Option { let t = text.trim(); @@ -338,6 +379,13 @@ mod tests { assert_eq!(p.display(), "Specify next point or [Undo/Close]:"); let q = Prompt::new("Erase polyline?", Accept::TEXT).kw(&["Yes", "No"]).default("No"); assert_eq!(q.display(), "Erase polyline? [Yes/No] :"); + // A prompt for an option has no "or", as AutoCAD writes it. + let o = Prompt::new("Enter an option", Accept::TEXT).kw(&["Close", "Join"]); + assert_eq!(o.display(), "Enter an option [Close/Join]:"); + let c = Prompt::new("New color", Accept::TEXT).kw(&["Truecolor", "COlorbook"]).default("BYLAYER"); + assert_eq!(c.display(), "New color [Truecolor/COlorbook] :"); + let b = Prompt::new("Enter block name", Accept::TEXT).kw(&["?"]); + assert_eq!(b.display(), "Enter block name or [?]:"); assert_eq!(p.match_keyword("u").as_deref(), Some("Undo")); assert_eq!(p.match_keyword("CL").as_deref(), Some("Close")); assert_eq!(p.match_keyword("x"), None); diff --git a/crates/engine/tests/it/assoc_array.rs b/crates/engine/tests/it/assoc_array.rs index 131210fa1..f672176b3 100644 --- a/crates/engine/tests/it/assoc_array.rs +++ b/crates/engine/tests/it/assoc_array.rs @@ -336,7 +336,7 @@ fn typed_arrayedit() { let a = rect_array(&mut s); typed(&mut s, &["arrayedit", "l"]); let p = s.prompt_text(); - assert!(p.contains("Enter an option or [Source/REPlace/Base point/Rows/Columns/Levels/RESet/eXit] "), "{p}"); + assert!(p.contains("Enter an option [Source/REPlace/Base point/Rows/Columns/Levels/RESet/eXit] "), "{p}"); typed(&mut s, &["c", "4", "t", "9", "r", "2", "5", ""]); assert!(s.prompt_text().contains("Enter an option"), "{}", s.prompt_text()); assert_eq!(centres(&s, &a).len(), 8); diff --git a/crates/engine/tests/it/layout_prompts.rs b/crates/engine/tests/it/layout_prompts.rs index afa784e0b..8b6ea6637 100644 --- a/crates/engine/tests/it/layout_prompts.rs +++ b/crates/engine/tests/it/layout_prompts.rs @@ -17,7 +17,7 @@ fn typed_layout_options_prompt_for_names() { s.cmdline("LAYOUT").unwrap(); let p = s.current_prompt().expect("LAYOUT prompts").display(); - assert_eq!(p, "Enter layout option or [Copy/Delete/New/Template/Rename/SAveas/Set/?] :"); + assert_eq!(p, "Enter layout option [Copy/Delete/New/Template/Rename/SAveas/Set/?] :"); s.cmdline("n").unwrap(); assert_eq!(s.current_prompt().unwrap().display(), "Enter name of new layout :"); s.cmdline("Sheet9").unwrap(); diff --git a/crates/engine/tests/it/vplayer_prompts.rs b/crates/engine/tests/it/vplayer_prompts.rs index f7f7e4a1a..393ad0958 100644 --- a/crates/engine/tests/it/vplayer_prompts.rs +++ b/crates/engine/tests/it/vplayer_prompts.rs @@ -33,9 +33,9 @@ fn typed_vplayer_changes_layers_in_chosen_viewports() { // Freeze in the current viewport (the default inside a viewport). s.cmdline("VPLAYER").unwrap(); - assert_eq!(s.current_prompt().unwrap().display(), "Enter an option or [?/Color/Freeze/Thaw/Reset/Newfrz/Vpvisdflt]:"); + assert_eq!(s.current_prompt().unwrap().display(), "Enter an option [?/Color/Freeze/Thaw/Reset/Newfrz/Vpvisdflt]:"); s.script("F\n0\n").unwrap(); - assert_eq!(s.current_prompt().unwrap().display(), "Enter an option or [All/Select/Current/Except current] :"); + assert_eq!(s.current_prompt().unwrap().display(), "Enter an option [All/Select/Current/Except current] :"); s.script("\n").unwrap(); assert_eq!(frozen(&s, &a), ["0"]); assert!(frozen(&s, &b).is_empty()); diff --git a/crates/ui-egui/src/cmdline.rs b/crates/ui-egui/src/cmdline.rs index 40397df99..fc25555e2 100644 --- a/crates/ui-egui/src/cmdline.rs +++ b/crates/ui-egui/src/cmdline.rs @@ -494,7 +494,12 @@ pub fn show(app: &mut CadApp, ui: &mut egui::Ui, canvas: Rect) { p.galley(pos2(x, bar.center().y - g.size().y / 2.0), g, t.text); x += gw; if !pr.raw && !pr.keywords.is_empty() { - let g = p.layout_no_wrap(if pr.message.is_empty() { " [".into() } else { crate::tl!(" or [").into() }, font.clone(), t.text); + // As the engine's prompt text says it; only " or [" has words to translate. + let lead = match pr.keyword_lead() { + " or [" => crate::tl!(" or ["), + lead => lead, + }; + let g = p.layout_no_wrap(lead.into(), font.clone(), t.text); let gw = g.size().x; p.galley(pos2(x, bar.center().y - g.size().y / 2.0), g, t.text); x += gw; diff --git a/crates/ui-egui/src/i18n/tests.rs b/crates/ui-egui/src/i18n/tests.rs index 034f261e1..492b9e595 100644 --- a/crates/ui-egui/src/i18n/tests.rs +++ b/crates/ui-egui/src/i18n/tests.rs @@ -357,6 +357,35 @@ fn render(app: &mut CadApp, ctx: &egui::Context) -> String { text } +/// The command line in the window draws a prompt's keywords itself; it puts the same words before +/// them as the engine's prompt text: " [" after a prompt for an option ("Enter an option [?/Make/…") +/// and after a question ("Fix any errors detected? [Yes/No]"), " or [" where the keywords are +/// offered besides what is asked ("Specify next point or [Undo]"), " or [" translated. +#[test] +fn the_command_line_says_or_before_keywords_as_the_prompt_does() { + let ctx = egui::Context::default(); + for lang in ["en", "de"] { + let mut app = CadApp::new(Session::new(), Services::default()); + app.run("ui.language", json!({ "lang": lang })).unwrap(); + app.logic(&ctx); + app.session.cmdline("-layer").unwrap(); + let text = render(&mut app, &ctx); + let option = format!("\n{}\n [\n", tr(lang, "Enter an option")); + assert!(text.contains(&option), "{lang}: {option:?} in {text:?}"); + app.session.cancel(); + app.session.cmdline("line").unwrap(); + app.session.cmdline("0,0").unwrap(); + let text = render(&mut app, &ctx); + let besides = format!("\n{}\n{}\n", tr(lang, "Specify next point"), tr(lang, " or [")); + assert!(text.contains(&besides), "{lang}: {besides:?} in {text:?}"); + app.session.cancel(); + app.session.cmdline("audit").unwrap(); + let text = render(&mut app, &ctx); + let question = format!("\n{}\n [\n", tr(lang, "Fix any errors detected?")); + assert!(text.contains(&question), "{lang}: {question:?} in {text:?}"); + } +} + #[test] fn live_switch_translates_the_shell_without_mutating_drawing_or_automation() { let mut app = CadApp::new(Session::new(), Services::default()); From 990ff2ce9f43b8e38fde9266c6975061a91348cd Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Tue, 3 Nov 2026 04:36:37 +0700 Subject: [PATCH 065/100] Say "or" once in prompts that offer options (#2563) * Say "or" once in prompts that offer options Twelve prompt messages ended in "or", and the command line adds "or" before a prompt's options, so they read "or or": HELIX height, XLINE angle and offset, BLEND, four LENGTHEN prompts, two NCOPY prompts, PEDIT and table breaks. They now read as AutoCAD's do. The interface catalogs are keyed by these messages: each key loses its "or" and each translation its trailing word for "or"; where the shorter key already had a translation, the longer row is removed. Co-Authored-By: Claude Opus 5.5 * Say "or" once in HELIX's radius prompts; rename the Vietnamese keys HELIX's base and top radius prompts ended in "or" before their Diameter option too, so they read "Specify base radius or or [Diameter]". They now read "Specify base radius or [Diameter] <1.0000>:", and their keys lose "or" in all 17 catalogs, as the other twelve did. The Vietnamese catalog, merged from main, gets the same renames as the other catalogs; where the shorter key already had a translation, the longer row goes. The unused "Specify rotation angle or" row goes from every catalog too: RECTANG has asked "Specify rotation angle" since #2554. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/draw2/curves3d.rs | 14 ++--- crates/engine/src/cmd/draw2/xline.rs | 4 +- crates/engine/src/cmd/modify2/blend.rs | 2 +- crates/engine/src/cmd/modify2/lengthen.rs | 8 +-- crates/engine/src/cmd/modify2/ncopy.rs | 4 +- crates/engine/src/cmd/modify2/pedit.rs | 2 +- crates/engine/src/cmd/table/breaks.rs | 2 +- crates/engine/tests/it/main.rs | 1 + crates/engine/tests/it/prompt_or_once.rs | 77 +++++++++++++++++++++++ crates/ui-egui/src/i18n/ar.tsv | 24 +++---- crates/ui-egui/src/i18n/bs.tsv | 24 +++---- crates/ui-egui/src/i18n/cs.tsv | 24 +++---- crates/ui-egui/src/i18n/de.tsv | 24 +++---- crates/ui-egui/src/i18n/el.tsv | 24 +++---- crates/ui-egui/src/i18n/es.tsv | 24 +++---- crates/ui-egui/src/i18n/fr.tsv | 24 +++---- crates/ui-egui/src/i18n/hu.tsv | 24 +++---- crates/ui-egui/src/i18n/it.tsv | 24 +++---- crates/ui-egui/src/i18n/ko.tsv | 24 +++---- crates/ui-egui/src/i18n/pt_br.tsv | 24 +++---- crates/ui-egui/src/i18n/ru.tsv | 24 +++---- crates/ui-egui/src/i18n/sv.tsv | 24 +++---- crates/ui-egui/src/i18n/tests.rs | 11 ++++ crates/ui-egui/src/i18n/tr.tsv | 24 +++---- crates/ui-egui/src/i18n/uk.tsv | 24 +++---- crates/ui-egui/src/i18n/vi.tsv | 24 +++---- crates/ui-egui/src/i18n/zh.tsv | 24 +++---- 27 files changed, 276 insertions(+), 257 deletions(-) create mode 100644 crates/engine/tests/it/prompt_or_once.rs diff --git a/crates/engine/src/cmd/draw2/curves3d.rs b/crates/engine/src/cmd/draw2/curves3d.rs index 005c49f07..faad5442c 100644 --- a/crates/engine/src/cmd/draw2/curves3d.rs +++ b/crates/engine/src/cmd/draw2/curves3d.rs @@ -153,19 +153,17 @@ impl Interactive for HelixM { } match (self.center, self.rb, self.rt) { (None, ..) => Prompt::new("Specify center point of base", Accept::POINT), - (Some(c), None, _) => { - Prompt::new(if self.diameter { "Specify base diameter" } else { "Specify base radius or" }, Accept::POINT_OR_NUMBER) - .kw(if self.diameter { &[] } else { &["Diameter"] }) - .default("1.0000") - .base(c) - } + (Some(c), None, _) => Prompt::new(if self.diameter { "Specify base diameter" } else { "Specify base radius" }, Accept::POINT_OR_NUMBER) + .kw(if self.diameter { &[] } else { &["Diameter"] }) + .default("1.0000") + .base(c), (Some(c), Some(rb), None) => { - Prompt::new(if self.diameter { "Specify top diameter" } else { "Specify top radius or" }, Accept::POINT_OR_NUMBER) + Prompt::new(if self.diameter { "Specify top diameter" } else { "Specify top radius" }, Accept::POINT_OR_NUMBER) .kw(if self.diameter { &[] } else { &["Diameter"] }) .default(format!("{rb:.4}")) .base(c) } - (Some(c), Some(_), Some(_)) => Prompt::new("Specify helix height or", Accept::POINT_OR_NUMBER) + (Some(c), Some(_), Some(_)) => Prompt::new("Specify helix height", Accept::POINT_OR_NUMBER) .kw(&["Axis endpoint", "Turns", "turn Height", "tWist"]) .default("1.0000") .base(c), diff --git a/crates/engine/src/cmd/draw2/xline.rs b/crates/engine/src/cmd/draw2/xline.rs index 31f152d16..78ab5c610 100644 --- a/crates/engine/src/cmd/draw2/xline.rs +++ b/crates/engine/src/cmd/draw2/xline.rs @@ -119,7 +119,7 @@ impl Interactive for XlineM2 { Some(b) => Prompt::new("Specify through point", Accept::POINT).base(b), }, XMode::Fixed(_) => Prompt::new("Specify through point", Accept::POINT), - XMode::AngAsk => Prompt::new("Enter angle of xline (0) or", Accept::NUMBER).kw(&["Reference"]), + XMode::AngAsk => Prompt::new("Enter angle of xline (0)", Accept::NUMBER).kw(&["Reference"]), XMode::RefSelect => Prompt::new("Select a line object", Accept::POINT), XMode::RefAngle(_) => Prompt::new("Enter angle of xline", Accept::NUMBER).default("0"), XMode::Bisect => match self.bis.len() { @@ -129,7 +129,7 @@ impl Interactive for XlineM2 { }, XMode::OffDist => { let d = s.doc().map(|d| d.header.f64("OFFSETDIST", 1.0)).unwrap_or(1.0); - Prompt::new("Specify offset distance or", Accept::NUMBER).kw(&["Through"]).default(if d < 0.0 { + Prompt::new("Specify offset distance", Accept::NUMBER).kw(&["Through"]).default(if d < 0.0 { "Through".into() } else { format!("{d:.4}") diff --git a/crates/engine/src/cmd/modify2/blend.rs b/crates/engine/src/cmd/modify2/blend.rs index 32b477eea..ef2258969 100644 --- a/crates/engine/src/cmd/modify2/blend.rs +++ b/crates/engine/src/cmd/modify2/blend.rs @@ -81,7 +81,7 @@ impl Interactive for BlendM { return Prompt::new("Enter continuity", curves::KW).kw(&["Tangent", "Smooth"]).default("Tangent"); } match self.first { - None => Prompt::new("Select first object or", Accept::POINT).kw(&["CONtinuity"]), + None => Prompt::new("Select first object", Accept::POINT).kw(&["CONtinuity"]), Some(_) => Prompt::new("Select second point", Accept::POINT), } } diff --git a/crates/engine/src/cmd/modify2/lengthen.rs b/crates/engine/src/cmd/modify2/lengthen.rs index 0c86c2c9c..7597ca771 100644 --- a/crates/engine/src/cmd/modify2/lengthen.rs +++ b/crates/engine/src/cmd/modify2/lengthen.rs @@ -194,14 +194,14 @@ impl Interactive for LengthenM { fn prompt(&self, _s: &Session) -> Prompt { match self.phase { LPhase::Main => { - Prompt::new("Select an object to measure or", Accept::POINT).kw(&["DElta", "Percent", "Total", "DYnamic"]).default("DYnamic") + Prompt::new("Select an object to measure", Accept::POINT).kw(&["DElta", "Percent", "Total", "DYnamic"]).default("DYnamic") } - LPhase::Delta => Prompt::new("Enter delta length or", Accept::NUMBER).kw(&["Angle"]).default("0.0000"), + LPhase::Delta => Prompt::new("Enter delta length", Accept::NUMBER).kw(&["Angle"]).default("0.0000"), LPhase::DeltaAngle => Prompt::new("Enter delta angle", Accept::NUMBER).default("0"), LPhase::Percent => Prompt::new("Enter percentage length", Accept::NUMBER).default("100.0000"), - LPhase::Total => Prompt::new("Specify total length or", Accept::NUMBER).kw(&["Angle"]).default("1.0000"), + LPhase::Total => Prompt::new("Specify total length", Accept::NUMBER).kw(&["Angle"]).default("1.0000"), LPhase::TotalAngle => Prompt::new("Specify total angle", Accept::NUMBER).default("57"), - LPhase::Change => Prompt::new("Select an object to change or", Accept::POINT).kw(&["Undo"]), + LPhase::Change => Prompt::new("Select an object to change", Accept::POINT).kw(&["Undo"]), LPhase::DynEnd(..) => Prompt::new("Specify new end point", Accept::POINT), } } diff --git a/crates/engine/src/cmd/modify2/ncopy.rs b/crates/engine/src/cmd/modify2/ncopy.rs index 475256e84..9b6f9eeb2 100644 --- a/crates/engine/src/cmd/modify2/ncopy.rs +++ b/crates/engine/src/cmd/modify2/ncopy.rs @@ -87,8 +87,8 @@ impl Interactive for NcopyM { } fn prompt(&self, _s: &Session) -> Prompt { match (self.selecting_done, self.base) { - (false, _) => Prompt::new("Select nested objects to copy or", Accept::POINT).kw(&["Settings"]), - (true, None) => Prompt::new("Specify base point or", Accept::POINT).kw(&["Displacement", "Multiple"]).default("Displacement"), + (false, _) => Prompt::new("Select nested objects to copy", Accept::POINT).kw(&["Settings"]), + (true, None) => Prompt::new("Specify base point", Accept::POINT).kw(&["Displacement", "Multiple"]).default("Displacement"), (true, Some(b)) => Prompt::new("Specify second point or ", Accept::POINT).base(b), } } diff --git a/crates/engine/src/cmd/modify2/pedit.rs b/crates/engine/src/cmd/modify2/pedit.rs index 0c1b64879..59d614323 100644 --- a/crates/engine/src/cmd/modify2/pedit.rs +++ b/crates/engine/src/cmd/modify2/pedit.rs @@ -398,7 +398,7 @@ impl Interactive for PeditM { fn prompt(&self, s: &Session) -> Prompt { match self.phase { PPhase::MSelect | PPhase::MConvert | PPhase::MMain | PPhase::MWidth | PPhase::MFuzz => self.multi_prompt(), - PPhase::Select => Prompt::new("Select polyline or", Accept::POINT).kw(&["Multiple"]), + PPhase::Select => Prompt::new("Select polyline", Accept::POINT).kw(&["Multiple"]), PPhase::Convert => { Prompt::new("Object selected is not a polyline. Do you want to turn it into one?", curves::KW).kw(&["Yes", "No"]).default("Y") } diff --git a/crates/engine/src/cmd/table/breaks.rs b/crates/engine/src/cmd/table/breaks.rs index 38ed33bce..6f207ec50 100644 --- a/crates/engine/src/cmd/table/breaks.rs +++ b/crates/engine/src/cmd/table/breaks.rs @@ -112,7 +112,7 @@ impl Interactive for BreakM { fn prompt(&self, _s: &Session) -> Prompt { match self.table { None => Prompt::new("Select a table", Accept::SELECT), - Some(_) => Prompt::new("Enter maximum part height or", Accept::POINT_OR_NUMBER).kw(&["Off"]), + Some(_) => Prompt::new("Enter maximum part height", Accept::POINT_OR_NUMBER).kw(&["Off"]), } } fn input(&mut self, s: &mut Session, i: Input) -> Result { diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 54ea40675..d3c8d030d 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -107,6 +107,7 @@ mod point_modifiers; mod polyface_explode; mod post_command_passes; mod prompt_keywords; +mod prompt_or_once; mod props_bad_lineweight; mod props_insert_scale_attribs; mod purge_mlinestyles; diff --git a/crates/engine/tests/it/prompt_or_once.rs b/crates/engine/tests/it/prompt_or_once.rs new file mode 100644 index 000000000..c84c58b1c --- /dev/null +++ b/crates/engine/tests/it/prompt_or_once.rs @@ -0,0 +1,77 @@ +//! Prompts that offer options say "or" once, as AutoCAD 2024's do: "Enter angle of xline (0) or +//! [Reference]:". The prompt adds "or" before its options, so a message that ended in "or" read +//! "or or". + +use cadcraft_engine::Session; +use serde_json::json; + +fn shows(s: &Session, text: &str) { + let p = s.prompt_text(); + assert!(p.ends_with(text), "{p}"); + assert!(!p.contains(" or or "), "{p}"); +} + +#[test] +fn xline_angle_and_offset() { + let mut s = Session::new(); + s.cmdline("xline").unwrap(); + s.cmdline("a").unwrap(); + shows(&s, "Enter angle of xline (0) or [Reference]:"); + s.cancel(); + s.cmdline("xline").unwrap(); + s.cmdline("o").unwrap(); + shows(&s, "Specify offset distance or [Through] <1.0000>:"); +} + +#[test] +fn helix_radii_and_height() { + let mut s = Session::new(); + s.script("helix\n0,0\n").unwrap(); + shows(&s, "Specify base radius or [Diameter] <1.0000>:"); + s.cmdline("5").unwrap(); + shows(&s, "Specify top radius or [Diameter] <5.0000>:"); + s.cmdline("").unwrap(); + shows(&s, "Specify helix height or [Axis endpoint/Turns/turn Height/tWist] <1.0000>:"); +} + +#[test] +fn blend_first_object() { + let mut s = Session::new(); + s.cmdline("blend").unwrap(); + shows(&s, "Select first object or [CONtinuity]:"); +} + +#[test] +fn lengthen_options() { + let mut s = Session::new(); + s.cmdline("lengthen").unwrap(); + shows(&s, "Select an object to measure or [DElta/Percent/Total/DYnamic] :"); + s.cmdline("de").unwrap(); + shows(&s, "Enter delta length or [Angle] <0.0000>:"); + s.cmdline("2").unwrap(); + shows(&s, "Select an object to change or [Undo]:"); + s.cancel(); + s.cmdline("lengthen").unwrap(); + s.cmdline("t").unwrap(); + shows(&s, "Specify total length or [Angle] <1.0000>:"); +} + +#[test] +fn ncopy_pedit_and_table_break() { + let mut s = Session::new(); + s.script("line 25,-5 25,5\n\nline 0,0 10,0\n\n-block B 0,0 L\n\n").unwrap(); + s.execute("insert", &json!({ "name": "B", "at": [20, 0] })).unwrap(); + s.cmdline("zoom e").unwrap(); + s.cmdline("ncopy").unwrap(); + shows(&s, "Select nested objects to copy or [Settings]:"); + s.script("22,0\n\n").unwrap(); + shows(&s, "Specify base point or [Displacement/Multiple] :"); + s.cancel(); + s.cmdline("pedit").unwrap(); + shows(&s, "Select polyline or [Multiple]:"); + s.cancel(); + s.execute("table", &json!({ "at": [0, 20], "cells": [["1", ""], ["2", ""]], "rowHeight": 1.0, "colWidth": 3.0 })).unwrap(); + s.cmdline("table.break").unwrap(); + s.cmdline("1,19.5").unwrap(); + shows(&s, "Enter maximum part height or [Off]:"); +} diff --git a/crates/ui-egui/src/i18n/ar.tsv b/crates/ui-egui/src/i18n/ar.tsv index ccc9b72d5..7713378d9 100644 --- a/crates/ui-egui/src/i18n/ar.tsv +++ b/crates/ui-egui/src/i18n/ar.tsv @@ -305,7 +305,7 @@ Enter a vertex editing option أدخل خيار تحرير الرؤوس Enter an option أدخل خياراً Enter angle of xline أدخل زاوية الخط الإنشائي - Enter angle of xline (0) or أدخل زاوية الخط الإنشائي (0) أو + Enter angle of xline (0) أدخل زاوية الخط الإنشائي (0) Enter area of rectangle in current units أدخل مساحة المستطيل بالوحدات الحالية Enter attribute prompt أدخل موجه السمة Enter attribute tag name أدخل اسم وسم السمة @@ -316,7 +316,7 @@ Enter default attribute value أدخل قيمة السمة الافتراضية Enter degree of spline أدخل درجة السبلاين Enter delta angle أدخل زاوية دلتا - Enter delta length or أدخل طول دلتا أو + Enter delta length أدخل طول دلتا Enter dimension text أدخل نص التأشير Enter dimension text (<> = measurement) أدخل نص التأشير (<> = القياس) Enter dimension value or [name=expression] أدخل قيمة التأشير أو [name=expression] @@ -852,8 +852,8 @@ Select an arc or circle حدد قوساً أو دائرة Select an arc. حدد قوساً. Select an object حدد كائناً - Select an object to change or حدد كائناً لتغييره أو - Select an object to measure or حدد كائناً لقياسه أو + Select an object to change حدد كائناً لتغييره + Select an object to measure حدد كائناً لقياسه Select an object. حدد كائنًا. Select an open curve. حدد منحنى مفتوحاً. Select an open line, arc, polyline, ellipse arc or spline. حدد خطاً مفتوحاً أو قوساً أو متعدد خطوط أو قوس إهليلج أو سبلاين. @@ -867,9 +867,8 @@ Select first line حدد الخط الأول Select first line or arc حدد الخط الأول أو قوساً Select first object حدد الكائن الأول - Select first object or حدد الكائن الأول أو Select first point حدد النقطة الأولى - Select nested objects to copy or حدد الكائنات المتداخلة لنسخها أو + Select nested objects to copy حدد الكائنات المتداخلة لنسخها Select object حدد كائناً Select object to dimension حدد كائناً لتأشيره Select object to divide حدد كائناً لتقسيمه @@ -886,7 +885,7 @@ Select one polyline (Multiple is available through the JSON form). حدد متعدد خطوط واحداً (يتوفر الوضع المتعدد عبر نموذج JSON). Select path curve حدد منحنى المسار Select point or object حدد نقطة أو كائناً - Select polyline or حدد متعدد خطوط أو + Select polyline حدد متعدد خطوط Select second line حدد الخط الثاني Select second line or shift-select to apply corner حدد الخط الثاني أو حدد مع Shift لتطبيق الزاوية Select second object حدد الكائن الثاني @@ -956,9 +955,8 @@ Specify axis endpoint of ellipse حدد نقطة نهاية محور الإهليلج Specify base diameter حدد القطر الأساسي Specify base point حدد نقطة الأساس - Specify base point or حدد نقطة الأساس أو Specify base point or displacement (drag with the middle mouse button for realtime pan) حدد نقطة الأساس أو الإزاحة (اسحب بزر الفأرة الأوسط للإزاحة الفورية) - Specify base radius or حدد نصف القطر الأساسي أو + Specify base radius حدد نصف القطر الأساسي Specify center of donut حدد مركز الحلقة Specify center of ellipse حدد مركز الإهليلج Specify center of polygon حدد مركز المضلع @@ -1000,7 +998,7 @@ Specify first point on circle: _tan to حدد النقطة الأولى على الدائرة: _تماس إلى Specify first source point حدد نقطة المصدر الأولى Specify height حدد الارتفاع - Specify helix height or حدد ارتفاع الحلزون أو + Specify helix height حدد ارتفاع الحلزون Specify included angle حدد الزاوية المحصورة Specify insertion base point حدد نقطة الأساس للإدراج Specify insertion point حدد نقطة الإدراج @@ -1019,7 +1017,6 @@ Specify new width for all segments حدد عرضاً جديداً لكل المقاطع Specify next point حدد النقطة التالية Specify offset distance حدد مسافة الإزاحة - Specify offset distance or حدد مسافة الإزاحة أو Specify opposite corner حدد الزاوية المقابلة Specify other corner point حدد نقطة الزاوية الأخرى Specify other endpoint of axis حدد نقطة النهاية الأخرى للمحور @@ -1032,7 +1029,6 @@ Specify radius of circle حدد نصف قطر الدائرة Specify rotation angle حدد زاوية التدوير Specify rotation angle of text حدد زاوية تدوير النص - Specify rotation angle or حدد زاوية التدوير أو Specify scale factor حدد معامل المقياس Specify second angle endpoint حدد نقطة نهاية الزاوية الثانية Specify second break point حدد نقطة الفصل الثانية @@ -1065,9 +1061,9 @@ Specify third source point or حدد نقطة المصدر الثالثة أو <متابعة> Specify through point حدد نقطة العبور Specify top diameter حدد القطر العلوي - Specify top radius or حدد نصف القطر العلوي أو + Specify top radius حدد نصف القطر العلوي Specify total angle حدد الزاوية الكلية - Specify total length or حدد الطول الكلي أو + Specify total length حدد الطول الكلي Specify width for rectangles حدد العرض للمستطيلات Sphere كرة Spline سبلاين diff --git a/crates/ui-egui/src/i18n/bs.tsv b/crates/ui-egui/src/i18n/bs.tsv index 460959a33..b349db278 100644 --- a/crates/ui-egui/src/i18n/bs.tsv +++ b/crates/ui-egui/src/i18n/bs.tsv @@ -281,7 +281,7 @@ Enter a vertex editing option Unesite opciju uređivanja tjemena Enter an option Unesite opciju Enter angle of xline Unesite ugao konstrukcijske linije - Enter angle of xline (0) or Unesite ugao konstrukcijske linije (0) ili + Enter angle of xline (0) Unesite ugao konstrukcijske linije (0) Enter area of rectangle in current units Unesite površinu pravougaonika u trenutnim jedinicama Enter attribute prompt Unesite upit atributa Enter attribute tag name Unesite naziv oznake atributa @@ -292,7 +292,7 @@ Enter default attribute value Unesite zadanu vrijednost atributa Enter degree of spline Unesite stepen splajna Enter delta angle Unesite delta ugao - Enter delta length or Unesite delta dužinu ili + Enter delta length Unesite delta dužinu Enter dimension text Unesite tekst kote Enter dimension text (<> = measurement) Unesite tekst kote (<> = mjera) Enter dimension value or [name=expression] Unesite vrijednost kote ili [naziv=izraz] @@ -788,8 +788,8 @@ Select an arc or circle Odaberite luk ili krug Select an arc. Odaberite luk. Select an object Odaberite objekat - Select an object to change or Odaberite objekat za promjenu ili - Select an object to measure or Odaberite objekat za mjerenje ili + Select an object to change Odaberite objekat za promjenu + Select an object to measure Odaberite objekat za mjerenje Select an object. Odaberite objekat. Select an open curve. Odaberite otvorenu krivu. Select an open line, arc, polyline, ellipse arc or spline. Odaberite otvorenu liniju, luk, poliliniju, eliptični luk ili splajn. @@ -803,14 +803,13 @@ Select first line Odaberite prvu liniju Select first line or arc Odaberite prvu liniju ili luk Select first object Odaberite prvi objekat - Select first object or Odaberite prvi objekat ili Select first point Odaberite prvu tačku Add to Working Set Dodaj u radni skup Edit Reference In-Place Uredi referencu na mjestu Save Reference Edits Sačuvaj izmjene reference Working Set Radni skup Xref and Block In-place Editing Uređivanje vanjskih referenci i blokova na mjestu - Select nested objects to copy or Odaberite ugniježđene objekte za kopiranje ili + Select nested objects to copy Odaberite ugniježđene objekte za kopiranje Select object Odaberite objekat Select object to dimension Odaberite objekat za kotiranje Select object to divide Odaberite objekat za dijeljenje @@ -826,7 +825,7 @@ Select one polyline (Multiple is available through the JSON form). Odaberite jednu poliliniju (Višestruko je dostupno putem JSON oblika). Select path curve Odaberite krivu putanje Select point or object Odaberite tačku ili objekat - Select polyline or Odaberite poliliniju ili + Select polyline Odaberite poliliniju Select second line Odaberite drugu liniju Select second line or shift-select to apply corner Odaberite drugu liniju ili Shift+odabir za primjenu ugla Select second object Odaberite drugi objekat @@ -884,9 +883,8 @@ Specify axis endpoint of ellipse Navedite krajnju tačku ose elipse Specify base diameter Navedite prečnik baze Specify base point Navedite baznu tačku - Specify base point or Navedite baznu tačku ili Specify base point or displacement (drag with the middle mouse button for realtime pan) Navedite baznu tačku ili pomak (prevucite srednjim dugmetom miša za pomicanje pogleda u stvarnom vremenu) - Specify base radius or Navedite poluprečnik baze ili + Specify base radius Navedite poluprečnik baze Specify center of donut Navedite središte prstena Specify center of ellipse Navedite središte elipse Specify center of polygon Navedite središte mnogougla @@ -928,7 +926,7 @@ Specify first point on circle: _tan to Navedite prvu tačku na krugu: _tan na Specify first source point Navedite prvu izvornu tačku Specify height Navedite visinu - Specify helix height or Navedite visinu spirale ili + Specify helix height Navedite visinu spirale Specify included angle Navedite obuhvaćeni ugao Specify insertion base point Navedite baznu tačku umetanja Specify insertion point Navedite tačku umetanja @@ -982,7 +980,6 @@ Specify new width for all segments Navedite novu širinu za sve segmente Specify next point Navedite sljedeću tačku Specify offset distance Navedite udaljenost odmaka - Specify offset distance or Navedite udaljenost odmaka ili Specify opposite corner Navedite suprotni ugao Specify other corner point Navedite tačku drugog ugla Specify other endpoint of axis Navedite drugu krajnju tačku ose @@ -995,7 +992,6 @@ Specify radius of circle Navedite poluprečnik kruga Specify rotation angle Navedite ugao rotacije Specify rotation angle of text Navedite ugao rotacije teksta - Specify rotation angle or Navedite ugao rotacije ili Specify scale factor Navedite faktor skale Specify second angle endpoint Navedite drugu krajnju tačku ugla Specify second break point Navedite drugu tačku prekida @@ -1028,9 +1024,9 @@ Specify third source point or Navedite treću izvornu tačku ili Specify through point Navedite tačku kroz koju prolazi Specify top diameter Navedite gornji prečnik - Specify top radius or Navedite gornji poluprečnik ili + Specify top radius Navedite gornji poluprečnik Specify total angle Navedite ukupni ugao - Specify total length or Navedite ukupnu dužinu ili + Specify total length Navedite ukupnu dužinu Specify width for rectangles Navedite širinu za pravougaonike Sphere Sfera Spline Splajn diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsv index 2419bf41f..61dc1b2b6 100644 --- a/crates/ui-egui/src/i18n/cs.tsv +++ b/crates/ui-egui/src/i18n/cs.tsv @@ -279,7 +279,7 @@ Enter a vertex editing option Zadejte možnost úpravy vrcholů Enter an option Zadejte možnost Enter angle of xline Zadejte úhel přímky - Enter angle of xline (0) or Zadejte úhel přímky (0) nebo + Enter angle of xline (0) Zadejte úhel přímky (0) Enter area of rectangle in current units Zadejte plochu obdélníku v aktuálních jednotkách Enter attribute prompt Zadejte výzvu atributu Enter attribute tag name Zadejte označení atributu @@ -290,7 +290,7 @@ Enter default attribute value Zadejte výchozí hodnotu atributu Enter degree of spline Zadejte stupeň spline Enter delta angle Zadejte změnu úhlu - Enter delta length or Zadejte změnu délky nebo + Enter delta length Zadejte změnu délky Enter dimension text Zadejte text kóty Enter dimension text (<> = measurement) Zadejte text kóty (<> = naměřená hodnota) Enter dimension value or [name=expression] Zadejte hodnotu kóty nebo [name=expression] @@ -786,8 +786,8 @@ Select an arc or circle Vyberte oblouk nebo kružnici Select an arc. Vyberte oblouk. Select an object Vyberte objekt - Select an object to change or Vyberte objekt ke změně nebo - Select an object to measure or Vyberte objekt k měření nebo + Select an object to change Vyberte objekt ke změně + Select an object to measure Vyberte objekt k měření Select an object. Vyberte objekt. Select an open curve. Vyberte otevřenou křivku. Select an open line, arc, polyline, ellipse arc or spline. Vyberte otevřenou úsečku, oblouk, křivku, eliptický oblouk nebo spline. @@ -801,14 +801,13 @@ Select first line Vyberte první úsečku Select first line or arc Vyberte první úsečku nebo oblouk Select first object Vyberte první objekt - Select first object or Vyberte první objekt nebo Select first point Vyberte první bod Add to Working Set Přidat do pracovní sady Edit Reference In-Place Upravit referenci na místě Save Reference Edits Uložit úpravy reference Working Set Pracovní sada Xref and Block In-place Editing Úpravy xref a bloků na místě - Select nested objects to copy or Vyberte vnořené objekty ke kopírování nebo + Select nested objects to copy Vyberte vnořené objekty ke kopírování Select object Vyberte objekt Select object to dimension Vyberte objekt ke kótování Select object to divide Vyberte objekt k dělení @@ -824,7 +823,7 @@ Select one polyline (Multiple is available through the JSON form). Vyberte jednu křivku (možnost Multiple je dostupná ve formě JSON). Select path curve Vyberte křivku trajektorie Select point or object Vyberte bod nebo objekt - Select polyline or Vyberte křivku nebo + Select polyline Vyberte křivku Select second line Vyberte druhou úsečku Select second line or shift-select to apply corner Vyberte druhou úsečku; s klávesou Shift vytvoříte roh Select second object Vyberte druhý objekt @@ -882,9 +881,8 @@ Specify axis endpoint of ellipse Určete koncový bod osy elipsy Specify base diameter Určete průměr základny Specify base point Určete základní bod - Specify base point or Určete základní bod nebo Specify base point or displacement (drag with the middle mouse button for realtime pan) Určete základní bod nebo posunutí (tažením prostředním tlačítkem myši posunete pohled v reálném čase) - Specify base radius or Určete poloměr základny nebo + Specify base radius Určete poloměr základny Specify center of donut Určete střed prstence Specify center of ellipse Určete střed elipsy Specify center of polygon Určete střed mnohoúhelníku @@ -926,7 +924,7 @@ Specify first point on circle: _tan to Určete první bod na kružnici: _tan k Specify first source point Určete první zdrojový bod Specify height Určete výšku - Specify helix height or Určete výšku šroubovice nebo + Specify helix height Určete výšku šroubovice Specify included angle Určete středový úhel Specify insertion base point Určete základní bod vložení Specify insertion point Určete bod vložení @@ -980,7 +978,6 @@ Specify new width for all segments Určete novou šířku všech segmentů Specify next point Určete další bod Specify offset distance Určete vzdálenost ekvidistanty - Specify offset distance or Určete vzdálenost ekvidistanty nebo Specify opposite corner Určete protilehlý roh Specify other corner point Určete bod druhého rohu Specify other endpoint of axis Určete druhý koncový bod osy @@ -993,7 +990,6 @@ Specify radius of circle Určete poloměr kružnice Specify rotation angle Určete úhel otočení Specify rotation angle of text Určete úhel otočení textu - Specify rotation angle or Určete úhel otočení nebo Specify scale factor Určete faktor měřítka Specify second angle endpoint Určete druhý koncový bod úhlu Specify second break point Určete druhý bod přerušení @@ -1026,9 +1022,9 @@ Specify third source point or Určete třetí zdrojový bod nebo Specify through point Určete bod, kterým má procházet Specify top diameter Určete horní průměr - Specify top radius or Určete horní poloměr nebo + Specify top radius Určete horní poloměr Specify total angle Určete celkový úhel - Specify total length or Určete celkovou délku nebo + Specify total length Určete celkovou délku Specify width for rectangles Určete šířku obdélníků Sphere Koule Spline Spline diff --git a/crates/ui-egui/src/i18n/de.tsv b/crates/ui-egui/src/i18n/de.tsv index 2186fc743..f36e9ed02 100644 --- a/crates/ui-egui/src/i18n/de.tsv +++ b/crates/ui-egui/src/i18n/de.tsv @@ -222,7 +222,7 @@ Enter a vertex editing option Geben Sie eine Option zur Scheitelpunktbearbeitung ein Enter an option Geben Sie eine Option ein Enter angle of xline Geben Sie den Winkel der Konstruktionslinie ein - Enter angle of xline (0) or Geben Sie den Winkel der Konstruktionslinie ein (0) oder + Enter angle of xline (0) Geben Sie den Winkel der Konstruktionslinie ein (0) Enter area of rectangle in current units Geben Sie die Fläche des Rechtecks in den aktuellen Einheiten ein Enter attribute prompt Geben Sie die Attributaufforderung ein Enter attribute tag name Geben Sie den Attribut-Tag-Namen ein @@ -233,7 +233,7 @@ Enter default attribute value Geben Sie den Standard-Attributwert ein Enter degree of spline Geben Sie den Grad des Splines ein Enter delta angle Geben Sie den Delta-Winkel ein - Enter delta length or Geben Sie die Delta-Länge ein oder + Enter delta length Geben Sie die Delta-Länge ein Enter dimension text Geben Sie den Bemaßungstext ein Enter dimension text (<> = measurement) Geben Sie den Bemaßungstext ein (<> = Messwert) Enter dimension value or [name=expression] Geben Sie einen Bemaßungswert oder [name=expression] ein @@ -676,8 +676,8 @@ Select an arc or circle Wählen Sie einen Bogen oder Kreis Select an arc. Wählen Sie einen Bogen. Select an object Wählen Sie ein Objekt - Select an object to change or Wählen Sie ein zu änderndes Objekt oder - Select an object to measure or Wählen Sie ein zu messendes Objekt oder + Select an object to change Wählen Sie ein zu änderndes Objekt + Select an object to measure Wählen Sie ein zu messendes Objekt Select an object. Wählen Sie ein Objekt. Add to Working Set Zum Arbeitssatz hinzufügen Edit Reference In-Place Referenz an Ort und Stelle bearbeiten @@ -696,9 +696,8 @@ Select first line Wählen Sie die erste Linie Select first line or arc Wählen Sie die erste Linie oder den ersten Bogen Select first object Wählen Sie das erste Objekt - Select first object or Wählen Sie das erste Objekt oder Select first point Wählen Sie den ersten Punkt - Select nested objects to copy or Wählen Sie zu kopierende verschachtelte Objekte oder + Select nested objects to copy Wählen Sie zu kopierende verschachtelte Objekte Select object Wählen Sie ein Objekt Select object to dimension Wählen Sie das zu bemaßende Objekt Select object to divide Wählen Sie das zu teilende Objekt @@ -714,7 +713,7 @@ Select one polyline (Multiple is available through the JSON form). Wählen Sie eine Polylinie (Multiple ist über die JSON-Form verfügbar). Select path curve Wählen Sie die Pfadkurve Select point or object Wählen Sie einen Punkt oder ein Objekt - Select polyline or Wählen Sie eine Polylinie oder + Select polyline Wählen Sie eine Polylinie Select second line Wählen Sie die zweite Linie Select second line or shift-select to apply corner Wählen Sie die zweite Linie oder wählen Sie mit der Umschalttaste, um eine Ecke anzuwenden Select second object Wählen Sie das zweite Objekt @@ -767,9 +766,8 @@ Specify axis endpoint of ellipse Geben Sie den Achsenendpunkt der Ellipse an Specify base diameter Geben Sie den Basisdurchmesser an Specify base point Geben Sie den Basispunkt an - Specify base point or Geben Sie den Basispunkt an oder Specify base point or displacement (drag with the middle mouse button for realtime pan) Geben Sie den Basispunkt oder die Verschiebung an (ziehen Sie mit der mittleren Maustaste zum Schwenken in Echtzeit) - Specify base radius or Geben Sie den Basisradius an oder + Specify base radius Geben Sie den Basisradius an Specify center of donut Geben Sie den Mittelpunkt des Rings an Specify center of ellipse Geben Sie den Mittelpunkt der Ellipse an Specify center of polygon Geben Sie den Mittelpunkt des Polygons an @@ -811,7 +809,7 @@ Specify first point on circle: _tan to Geben Sie den ersten Punkt auf dem Kreis an: _tan zu Specify first source point Geben Sie den ersten Quellpunkt an Specify height Geben Sie die Höhe an - Specify helix height or Geben Sie die Höhe der Helix an oder + Specify helix height Geben Sie die Höhe der Helix an Specify included angle Geben Sie den eingeschlossenen Winkel an Specify insertion base point Geben Sie den Einfügebasispunkt an Specify insertion point Geben Sie den Einfügepunkt an @@ -830,7 +828,6 @@ Specify new width for all segments Geben Sie die neue Breite für alle Segmente an Specify next point Geben Sie den nächsten Punkt an Specify offset distance Geben Sie den Versatzabstand an - Specify offset distance or Geben Sie den Versatzabstand an oder Specify opposite corner Geben Sie die gegenüberliegende Ecke an Specify other corner point Geben Sie den anderen Eckpunkt an Specify other endpoint of axis Geben Sie den anderen Endpunkt der Achse an @@ -843,7 +840,6 @@ Specify radius of circle Geben Sie den Radius des Kreises an Specify rotation angle Geben Sie den Drehwinkel an Specify rotation angle of text Geben Sie den Drehwinkel des Textes an - Specify rotation angle or Geben Sie den Drehwinkel an oder Specify scale factor Geben Sie den Skalierfaktor an Specify second angle endpoint Geben Sie den zweiten Endpunkt des Winkels an Specify second break point Geben Sie den zweiten Unterbrechungspunkt an @@ -876,9 +872,9 @@ Specify third source point or Geben Sie den dritten Quellpunkt an oder Specify through point Geben Sie den Durchgangspunkt an Specify top diameter Geben Sie den oberen Durchmesser an - Specify top radius or Geben Sie den oberen Radius an oder + Specify top radius Geben Sie den oberen Radius an Specify total angle Geben Sie den Gesamtwinkel an - Specify total length or Geben Sie die Gesamtlänge an oder + Specify total length Geben Sie die Gesamtlänge an Specify width for rectangles Geben Sie die Breite für Rechtecke an Sphere Kugel Spline Spline diff --git a/crates/ui-egui/src/i18n/el.tsv b/crates/ui-egui/src/i18n/el.tsv index 16fcecfeb..528dacf55 100644 --- a/crates/ui-egui/src/i18n/el.tsv +++ b/crates/ui-egui/src/i18n/el.tsv @@ -337,7 +337,7 @@ Enter a vertex editing option Εισαγάγετε επιλογή επεξεργασίας κορυφής Enter an option Εισαγάγετε επιλογή Enter angle of xline Εισαγάγετε γωνία βοηθητικής ευθείας - Enter angle of xline (0) or Εισαγάγετε γωνία βοηθητικής ευθείας (0) ή + Enter angle of xline (0) Εισαγάγετε γωνία βοηθητικής ευθείας (0) Enter area of rectangle in current units Εισαγάγετε το εμβαδόν ορθογωνίου στις τρέχουσες μονάδες Enter attribute prompt Εισαγάγετε μήνυμα προτροπής ιδιότητας Enter attribute tag name Εισαγάγετε όνομα ετικέτας ιδιότητας @@ -348,7 +348,7 @@ Enter default attribute value Εισαγάγετε προεπιλεγμένη τιμή ιδιότητας Enter degree of spline Εισαγάγετε βαθμό καμπύλης spline Enter delta angle Εισαγάγετε μεταβολή γωνίας - Enter delta length or Εισαγάγετε μεταβολή μήκους ή + Enter delta length Εισαγάγετε μεταβολή μήκους Enter dimension text Εισαγάγετε κείμενο διάστασης Enter dimension text (<> = measurement) Εισαγάγετε κείμενο διάστασης (<> = μέτρηση) Enter dimension value or [name=expression] Εισαγάγετε τιμή διάστασης ή [όνομα=έκφραση] @@ -919,8 +919,8 @@ Select an arc or circle Επιλέξτε τόξο ή κύκλο Select an arc. Επιλέξτε τόξο. Select an object Επιλέξτε αντικείμενο - Select an object to change or Επιλέξτε αντικείμενο για αλλαγή ή - Select an object to measure or Επιλέξτε αντικείμενο για μέτρηση ή + Select an object to change Επιλέξτε αντικείμενο για αλλαγή + Select an object to measure Επιλέξτε αντικείμενο για μέτρηση Select an object. Επιλέξτε αντικείμενο. Select an open curve. Επιλέξτε ανοικτή καμπύλη. Select an open line, arc, polyline, ellipse arc or spline. Επιλέξτε ανοικτή γραμμή, τόξο, πολυγραμμή, ελλειπτικό τόξο ή καμπύλη spline. @@ -934,9 +934,8 @@ Select first line Επιλέξτε πρώτη γραμμή Select first line or arc Επιλέξτε πρώτη γραμμή ή τόξο Select first object Επιλέξτε πρώτο αντικείμενο - Select first object or Επιλέξτε πρώτο αντικείμενο ή Select first point Επιλέξτε πρώτο σημείο - Select nested objects to copy or Επιλέξτε ένθετα αντικείμενα για αντιγραφή ή + Select nested objects to copy Επιλέξτε ένθετα αντικείμενα για αντιγραφή Select object Επιλέξτε αντικείμενο Select object to dimension Επιλέξτε αντικείμενο για διαστασιολόγηση Select object to divide Επιλέξτε αντικείμενο για διαίρεση @@ -954,7 +953,7 @@ Select one polyline (Multiple is available through the JSON form). Επιλέξτε μία πολυγραμμή (η επιλογή Multiple διατίθεται μέσω JSON). Select path curve Επιλέξτε καμπύλη διαδρομής Select point or object Επιλέξτε σημείο ή αντικείμενο - Select polyline or Επιλέξτε πολυγραμμή ή + Select polyline Επιλέξτε πολυγραμμή Select second line Επιλέξτε δεύτερη γραμμή Select second line or shift-select to apply corner Επιλέξτε δεύτερη γραμμή ή κρατήστε Shift για εφαρμογή γωνίας Select second object Επιλέξτε δεύτερο αντικείμενο @@ -1036,9 +1035,8 @@ Specify axis endpoint of ellipse Καθορίστε τελικό σημείο άξονα έλλειψης Specify base diameter Καθορίστε διάμετρο βάσης Specify base point Καθορίστε σημείο βάσης - Specify base point or Καθορίστε σημείο βάσης ή Specify base point or displacement (drag with the middle mouse button for realtime pan) Καθορίστε σημείο βάσης ή μετατόπιση (σύρετε με το μεσαίο πλήκτρο του ποντικιού για μετατόπιση όψης σε πραγματικό χρόνο) - Specify base radius or Καθορίστε ακτίνα βάσης ή + Specify base radius Καθορίστε ακτίνα βάσης Specify center of donut Καθορίστε κέντρο δακτυλίου Specify center of ellipse Καθορίστε κέντρο έλλειψης Specify center of polygon Καθορίστε κέντρο πολυγώνου @@ -1080,7 +1078,7 @@ Specify first point on circle: _tan to Καθορίστε πρώτο σημείο επάνω στον κύκλο: _tan προς Specify first source point Καθορίστε πρώτο σημείο προέλευσης Specify height Καθορίστε ύψος - Specify helix height or Καθορίστε ύψος έλικας ή + Specify helix height Καθορίστε ύψος έλικας Specify included angle Καθορίστε περιεχόμενη γωνία Specify insertion base point Καθορίστε σημείο βάσης εισαγωγής Specify insertion point Καθορίστε σημείο εισαγωγής @@ -1099,7 +1097,6 @@ Specify new width for all segments Καθορίστε νέο πλάτος για όλα τα τμήματα Specify next point Καθορίστε επόμενο σημείο Specify offset distance Καθορίστε απόσταση παράλληλης μετατόπισης (Offset) - Specify offset distance or Καθορίστε απόσταση παράλληλης μετατόπισης ή Specify opposite corner Καθορίστε απέναντι γωνία Specify other corner point Καθορίστε το άλλο γωνιακό σημείο Specify other endpoint of axis Καθορίστε το άλλο τελικό σημείο άξονα @@ -1112,7 +1109,6 @@ Specify radius of circle Καθορίστε ακτίνα κύκλου Specify rotation angle Καθορίστε γωνία περιστροφής Specify rotation angle of text Καθορίστε γωνία περιστροφής κειμένου - Specify rotation angle or Καθορίστε γωνία περιστροφής ή Specify scale factor Καθορίστε συντελεστή κλίμακας Specify second angle endpoint Καθορίστε δεύτερο τελικό σημείο γωνίας Specify second break point Καθορίστε δεύτερο σημείο διακοπής @@ -1145,9 +1141,9 @@ Specify third source point or Καθορίστε τρίτο σημείο προέλευσης ή <συνέχεια> Specify through point Καθορίστε σημείο διέλευσης Specify top diameter Καθορίστε άνω διάμετρο - Specify top radius or Καθορίστε άνω ακτίνα ή + Specify top radius Καθορίστε άνω ακτίνα Specify total angle Καθορίστε συνολική γωνία - Specify total length or Καθορίστε συνολικό μήκος ή + Specify total length Καθορίστε συνολικό μήκος Specify width for rectangles Καθορίστε πλάτος ορθογωνίων Sphere Σφαίρα Spherical Σφαιρικό diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsv index 8c45b7f97..457341c36 100644 --- a/crates/ui-egui/src/i18n/es.tsv +++ b/crates/ui-egui/src/i18n/es.tsv @@ -295,7 +295,7 @@ Enter a vertex editing option Introduce una opción de edición de vértices Enter an option Introduce una opción Enter angle of xline Introduce el ángulo de la línea auxiliar - Enter angle of xline (0) or Introduce el ángulo de la línea auxiliar (0) o + Enter angle of xline (0) Introduce el ángulo de la línea auxiliar (0) Enter area of rectangle in current units Introduce el área del rectángulo en las unidades actuales Enter attribute prompt Introduce la solicitud del atributo Enter attribute tag name Introduce la etiqueta del atributo @@ -306,7 +306,7 @@ Enter default attribute value Introduce el valor predeterminado del atributo Enter degree of spline Introduce el grado de la spline Enter delta angle Introduce el incremento angular - Enter delta length or Introduce el incremento de longitud o + Enter delta length Introduce el incremento de longitud Enter dimension text Introduce el texto de cota Enter dimension text (<> = measurement) Introduce el texto de cota (<> = medida) Enter dimension value or [name=expression] Introduce el valor de cota o [nombre=expresión] @@ -885,8 +885,8 @@ Select an arc or circle Selecciona un arco o círculo Select an arc. Selecciona un arco. Select an object Selecciona un objeto - Select an object to change or Selecciona un objeto que modificar o - Select an object to measure or Selecciona un objeto que medir o + Select an object to change Selecciona un objeto que modificar + Select an object to measure Selecciona un objeto que medir Select an object. Selecciona un objeto. Select an open curve. Selecciona una curva abierta. Select an open line, arc, polyline, ellipse arc or spline. Selecciona una línea, arco, polilínea, arco elíptico o spline abierta. @@ -900,9 +900,8 @@ Select first line Selecciona la primera línea Select first line or arc Selecciona la primera línea o arco Select first object Selecciona el primer objeto - Select first object or Selecciona el primer objeto o Select first point Selecciona el primer punto - Select nested objects to copy or Selecciona objetos anidados para copiar o + Select nested objects to copy Selecciona objetos anidados para copiar Select object Selecciona un objeto Select object to dimension Selecciona un objeto para acotar Select object to divide Selecciona un objeto para dividir @@ -918,7 +917,7 @@ Select one polyline (Multiple is available through the JSON form). Selecciona una polilínea (la opción Multiple está disponible en la forma JSON). Select path curve Selecciona la curva del camino Select point or object Selecciona un punto u objeto - Select polyline or Selecciona una polilínea o + Select polyline Selecciona una polilínea Select second line Selecciona la segunda línea Select second line or shift-select to apply corner Selecciona la segunda línea o usa Mayús para aplicar una esquina Select second object Selecciona el segundo objeto @@ -971,9 +970,8 @@ Specify axis endpoint of ellipse Indica el extremo del eje de la elipse Specify base diameter Indica el diámetro de la base Specify base point Indica el punto base - Specify base point or Indica el punto base o Specify base point or displacement (drag with the middle mouse button for realtime pan) Indica el punto base o el desplazamiento (arrastra con el botón central para encuadrar) - Specify base radius or Indica el radio de la base o + Specify base radius Indica el radio de la base Specify center of donut Indica el centro de la arandela Specify center of ellipse Indica el centro de la elipse Specify center of polygon Indica el centro del polígono @@ -1015,7 +1013,7 @@ Specify first point on circle: _tan to Indica el primer punto del círculo: _tan a Specify first source point Indica el primer punto de origen Specify height Indica la altura - Specify helix height or Indica la altura de la hélice o + Specify helix height Indica la altura de la hélice Specify included angle Indica el ángulo incluido Specify insertion base point Indica el punto base de inserción Specify insertion point Indica el punto de inserción @@ -1034,7 +1032,6 @@ Specify new width for all segments Indica la nueva anchura de todos los segmentos Specify next point Indica el siguiente punto Specify offset distance Indica la distancia de desfase - Specify offset distance or Indica la distancia de desfase o Specify opposite corner Indica la esquina opuesta Specify other corner point Indica el punto de la otra esquina Specify other endpoint of axis Indica el otro extremo del eje @@ -1047,7 +1044,6 @@ Specify radius of circle Indica el radio del círculo Specify rotation angle Indica el ángulo de giro Specify rotation angle of text Indica el ángulo de giro del texto - Specify rotation angle or Indica el ángulo de giro o Specify scale factor Indica el factor de escala Specify second angle endpoint Indica el segundo extremo del ángulo Specify second break point Indica el segundo punto de ruptura @@ -1080,9 +1076,9 @@ Specify third source point or Indica el tercer punto de origen o Specify through point Indica el punto a atravesar Specify top diameter Indica el diámetro superior - Specify top radius or Indica el radio superior o + Specify top radius Indica el radio superior Specify total angle Indica el ángulo total - Specify total length or Indica la longitud total o + Specify total length Indica la longitud total Specify width for rectangles Indica la anchura de los rectángulos Sphere Esfera Spline Spline diff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsv index 548ed6a66..62c77c3f9 100644 --- a/crates/ui-egui/src/i18n/fr.tsv +++ b/crates/ui-egui/src/i18n/fr.tsv @@ -221,7 +221,7 @@ Enter a vertex editing option Entrez une option d'édition de sommet Enter an option Entrez une option Enter angle of xline Entrez l'angle de la ligne de construction - Enter angle of xline (0) or Entrez l'angle de la ligne de construction (0) ou + Enter angle of xline (0) Entrez l'angle de la ligne de construction (0) Enter area of rectangle in current units Entrez l'aire du rectangle dans les unités courantes Enter attribute prompt Entrez l'invite de l'attribut Enter attribute tag name Entrez le nom de l'étiquette d'attribut @@ -232,7 +232,7 @@ Enter default attribute value Entrez la valeur par défaut de l'attribut Enter degree of spline Entrez le degré de la spline Enter delta angle Entrez l'écart d'angle - Enter delta length or Entrez l'écart de longueur ou + Enter delta length Entrez l'écart de longueur Enter dimension text Entrez le texte de cotation Enter dimension text (<> = measurement) Entrez le texte de cotation (<> = mesure) Enter dimension value or [name=expression] Entrez la valeur de cotation ou [name=expression] @@ -675,8 +675,8 @@ Select an arc or circle Sélectionner un arc ou un cercle Select an arc. Sélectionner un arc. Select an object Sélectionner un objet - Select an object to change or Sélectionner un objet à modifier ou - Select an object to measure or Sélectionner un objet à mesurer ou + Select an object to change Sélectionner un objet à modifier + Select an object to measure Sélectionner un objet à mesurer Select an object. Sélectionner un objet. Select an open curve. Sélectionner une courbe ouverte. Select an open line, arc, polyline, ellipse arc or spline. Sélectionner une ligne, un arc, une polyligne, un arc elliptique ou une spline ouverts. @@ -690,9 +690,8 @@ Select first line Sélectionner la première ligne Select first line or arc Sélectionner la première ligne ou le premier arc Select first object Sélectionner le premier objet - Select first object or Sélectionner le premier objet ou Select first point Sélectionner le premier point - Select nested objects to copy or Sélectionner des objets imbriqués à copier ou + Select nested objects to copy Sélectionner des objets imbriqués à copier Select object Sélectionner un objet Select object to dimension Sélectionner un objet à coter Select object to divide Sélectionner un objet à diviser @@ -708,7 +707,7 @@ Select one polyline (Multiple is available through the JSON form). Sélectionner une polyligne (l'option Multiple est disponible via le formulaire JSON). Select path curve Sélectionner la courbe de trajectoire Select point or object Sélectionner un point ou un objet - Select polyline or Sélectionner une polyligne ou + Select polyline Sélectionner une polyligne Select second line Sélectionner la deuxième ligne Select second line or shift-select to apply corner Sélectionner la deuxième ligne ou sélectionner avec Maj pour former le coin Select second object Sélectionner le deuxième objet @@ -761,9 +760,8 @@ Specify axis endpoint of ellipse Spécifier l'extrémité de l'axe de l'ellipse Specify base diameter Spécifiez le diamètre de la base Specify base point Spécifiez le point de base - Specify base point or Spécifiez le point de base ou Specify base point or displacement (drag with the middle mouse button for realtime pan) Spécifiez le point de base ou le déplacement (faites glisser avec le bouton central de la souris pour un panoramique en temps réel) - Specify base radius or Spécifiez le rayon de base ou + Specify base radius Spécifiez le rayon de base Specify center of donut Spécifiez le centre de l'anneau Specify center of ellipse Spécifiez le centre de l'ellipse Specify center of polygon Spécifiez le centre du polygone @@ -805,7 +803,7 @@ Specify first point on circle: _tan to Spécifiez le premier point sur le cercle : _tan à Specify first source point Spécifiez le premier point source Specify height Spécifiez la hauteur - Specify helix height or Spécifiez la hauteur de l'hélice ou + Specify helix height Spécifiez la hauteur de l'hélice Specify included angle Spécifiez l'angle inclus Specify insertion base point Spécifiez le point de base d'insertion Specify insertion point Spécifiez le point d'insertion @@ -824,7 +822,6 @@ Specify new width for all segments Spécifiez la nouvelle largeur de tous les segments Specify next point Spécifiez le point suivant Specify offset distance Spécifiez la distance de décalage - Specify offset distance or Spécifiez la distance de décalage ou Specify opposite corner Spécifiez le coin opposé Specify other corner point Spécifiez l'autre point d'angle Specify other endpoint of axis Spécifiez l'autre point final de l'axe @@ -837,7 +834,6 @@ Specify radius of circle Spécifiez le rayon du cercle Specify rotation angle Spécifiez l'angle de rotation Specify rotation angle of text Spécifiez l'angle de rotation du texte - Specify rotation angle or Spécifiez l'angle de rotation ou Specify scale factor Spécifiez le facteur d'échelle Specify second angle endpoint Spécifiez le deuxième point final de l'angle Specify second break point Spécifiez le deuxième point de rupture @@ -870,9 +866,9 @@ Specify third source point or Spécifiez le troisième point source ou Specify through point Spécifiez le point de passage Specify top diameter Spécifiez le diamètre supérieur - Specify top radius or Spécifiez le rayon supérieur ou + Specify top radius Spécifiez le rayon supérieur Specify total angle Spécifiez l'angle total - Specify total length or Spécifiez la longueur totale ou + Specify total length Spécifiez la longueur totale Specify width for rectangles Spécifiez la largeur des rectangles Sphere Sphère Spline Spline diff --git a/crates/ui-egui/src/i18n/hu.tsv b/crates/ui-egui/src/i18n/hu.tsv index 926ad0e6c..e486f6337 100644 --- a/crates/ui-egui/src/i18n/hu.tsv +++ b/crates/ui-egui/src/i18n/hu.tsv @@ -350,7 +350,7 @@ Enter a vertex editing option Válassz egy csúcspont-szerkesztési lehetőséget Enter an option Válassz egy lehetőséget Enter angle of xline Add meg a szerkesztővonal szögét - Enter angle of xline (0) or Add meg a szerkesztővonal szögét (0) + Enter angle of xline (0) Add meg a szerkesztővonal szögét (0) Enter area of rectangle in current units Add meg a téglalap területét az aktuális mértékegységben Enter attribute prompt Add meg az attribútum bekérő szövegét Enter attribute tag name Add meg az attribútum címkéjét @@ -361,7 +361,7 @@ Enter default attribute value Add meg az attribútum alapértelmezett értékét Enter degree of spline Add meg a spline fokszámát Enter delta angle Add meg a szögváltozást - Enter delta length or Add meg a hosszváltozást + Enter delta length Add meg a hosszváltozást Enter dimension text Add meg a méretszámot Enter dimension text (<> = measurement) Add meg a méretszámot (<> = mért érték) Enter dimension value or [name=expression] Add meg a méret értékét vagy [név=kifejezés] @@ -931,8 +931,8 @@ Select an arc or circle Válassz egy ívet vagy kört Select an arc. Válassz egy ívet. Select an object Válassz egy objektumot - Select an object to change or Válaszd ki a módosítandó objektumot - Select an object to measure or Válaszd ki a mérendő objektumot + Select an object to change Válaszd ki a módosítandó objektumot + Select an object to measure Válaszd ki a mérendő objektumot Select an object. Válassz egy objektumot. Select an open curve. Válassz egy nyitott görbét. Select an open line, arc, polyline, ellipse arc or spline. Válassz egy nyitott vonalat, ívet, vonalláncot, ellipszisívet vagy spline-t. @@ -946,9 +946,8 @@ Select first line Válaszd ki az első vonalat Select first line or arc Válaszd ki az első vonalat vagy ívet Select first object Válaszd ki az első objektumot - Select first object or Válaszd ki az első objektumot Select first point Válaszd ki az első pontot - Select nested objects to copy or Válaszd ki a másolandó beágyazott objektumokat + Select nested objects to copy Válaszd ki a másolandó beágyazott objektumokat Select object Válassz objektumot Select object to dimension Válaszd ki a méretezendő objektumot Select object to divide Válaszd ki a felosztandó objektumot @@ -965,7 +964,7 @@ Select one polyline (Multiple is available through the JSON form). Egyetlen vonalláncot válassz (a Multiple a parancs JSON-hívásával érhető el). Select path curve Válaszd ki az útvonalgörbét Select point or object Válassz pontot vagy objektumot - Select polyline or Válassz vonalláncot + Select polyline Válassz vonalláncot Select second line Válaszd ki a második vonalat Select second line or shift-select to apply corner Válaszd ki a második vonalat, Shift+kijelöléssel éles sarkot képezhetsz Select second object Válaszd ki a második objektumot @@ -1046,9 +1045,8 @@ Specify axis endpoint of ellipse Add meg az ellipszis tengelyének végpontját Specify base diameter Add meg az alap átmérőjét Specify base point Add meg a bázispontot - Specify base point or Add meg a bázispontot Specify base point or displacement (drag with the middle mouse button for realtime pan) Add meg a bázispontot vagy az elmozdulást (a középső egérgombbal húzva a nézetet valós időben mozgathatod) - Specify base radius or Add meg az alap sugarát + Specify base radius Add meg az alap sugarát Specify center of donut Add meg a gyűrű középpontját Specify center of ellipse Add meg az ellipszis középpontját Specify center of polygon Add meg a sokszög középpontját @@ -1090,7 +1088,7 @@ Specify first point on circle: _tan to Add meg a kör első pontját: _tan to Specify first source point Add meg az első forráspontot Specify height Add meg a magasságot - Specify helix height or Add meg a csavarvonal magasságát + Specify helix height Add meg a csavarvonal magasságát Specify included angle Add meg a középponti szöget Specify insertion base point Add meg a beszúrási bázispontot Specify insertion point Add meg a beszúrási pontot @@ -1109,7 +1107,6 @@ Specify new width for all segments Add meg az összes szakasz új szélességét Specify next point Add meg a következő pontot Specify offset distance Add meg az eltolási távolságot - Specify offset distance or Add meg az eltolási távolságot Specify opposite corner Add meg az átellenes sarkot Specify other corner point Add meg a másik sarokpontot Specify other endpoint of axis Add meg a tengely másik végpontját @@ -1122,7 +1119,6 @@ Specify radius of circle Add meg a kör sugarát Specify rotation angle Add meg az elforgatási szöget Specify rotation angle of text Add meg a szöveg elforgatási szögét - Specify rotation angle or Add meg az elforgatási szöget Specify scale factor Add meg a léptéktényezőt Specify second angle endpoint Add meg a szög második végpontját Specify second break point Add meg a második megszakítási pontot @@ -1155,9 +1151,9 @@ Specify third source point or Add meg a harmadik forráspontot vagy Specify through point Add meg az átmenő pontot Specify top diameter Add meg a felső átmérőt - Specify top radius or Add meg a felső sugarat + Specify top radius Add meg a felső sugarat Specify total angle Add meg a teljes szöget - Specify total length or Add meg a teljes hosszt + Specify total length Add meg a teljes hosszt Specify width for rectangles Add meg a téglalapok szélességét Sphere Gömb Spherical Gömbszerű diff --git a/crates/ui-egui/src/i18n/it.tsv b/crates/ui-egui/src/i18n/it.tsv index cec28d857..b437397b6 100644 --- a/crates/ui-egui/src/i18n/it.tsv +++ b/crates/ui-egui/src/i18n/it.tsv @@ -288,7 +288,7 @@ menu Modify Edita Enter a vertex editing option Digitare un'opzione di modifica dei vertici Enter an option Digitare un'opzione Enter angle of xline Digitare l'angolo della linea di costruzione - Enter angle of xline (0) or Digitare l'angolo della linea di costruzione (0) o + Enter angle of xline (0) Digitare l'angolo della linea di costruzione (0) Enter area of rectangle in current units Digitare l'area del rettangolo nelle unità correnti Enter attribute prompt Digitare il messaggio dell'attributo Enter attribute tag name Digitare il nome dell'etichetta dell'attributo @@ -299,7 +299,7 @@ menu Modify Edita Enter default attribute value Digitare il valore predefinito dell'attributo Enter degree of spline Digitare il grado della spline Enter delta angle Digitare la variazione dell'angolo - Enter delta length or Digitare la variazione di lunghezza o + Enter delta length Digitare la variazione di lunghezza Enter dimension text Digitare il testo della quota Enter dimension text (<> = measurement) Digitare il testo della quota (<> = misura) Enter dimension value or [name=expression] Digitare il valore della quota o [nome=espressione] @@ -797,8 +797,8 @@ menu Modify Edita Select an arc or circle Selezionare un arco o un cerchio Select an arc. Selezionare un arco. Select an object Selezionare un oggetto - Select an object to change or Selezionare l'oggetto da modificare o - Select an object to measure or Selezionare l'oggetto da misurare o + Select an object to change Selezionare l'oggetto da modificare + Select an object to measure Selezionare l'oggetto da misurare Select an object. Selezionare un oggetto. Select an open curve. Selezionare una curva aperta. Select an open line, arc, polyline, ellipse arc or spline. Selezionare una linea, un arco, una polilinea, un arco ellittico o una spline aperti. @@ -811,9 +811,8 @@ menu Modify Edita Select first line Selezionare la prima linea Select first line or arc Selezionare la prima linea o il primo arco Select first object Selezionare il primo oggetto - Select first object or Selezionare il primo oggetto o Select first point Selezionare il primo punto - Select nested objects to copy or Selezionare gli oggetti nidificati da copiare o + Select nested objects to copy Selezionare gli oggetti nidificati da copiare Select object Selezionare l'oggetto Select object to dimension Selezionare l'oggetto da quotare Select object to divide Selezionare l'oggetto da dividere @@ -829,7 +828,7 @@ menu Modify Edita Select one polyline (Multiple is available through the JSON form). Selezionare una polilinea (Multiple è disponibile tramite il modulo JSON). Select path curve Selezionare la curva della traiettoria Select point or object Selezionare un punto o un oggetto - Select polyline or Selezionare la polilinea o + Select polyline Selezionare la polilinea Select second line Selezionare la seconda linea Select second line or shift-select to apply corner Selezionare la seconda linea o selezionare con Maiusc per applicare l'angolo Select second object Selezionare il secondo oggetto @@ -889,9 +888,8 @@ menu Modify Edita Specify axis endpoint of ellipse Specificare l'estremo dell'asse dell'ellisse Specify base diameter Specificare il diametro di base Specify base point Specificare il punto base - Specify base point or Specificare il punto base o Specify base point or displacement (drag with the middle mouse button for realtime pan) Specificare il punto base o lo spostamento (trascinare con il tasto centrale del mouse per il pan in tempo reale) - Specify base radius or Specificare il raggio di base o + Specify base radius Specificare il raggio di base Specify center of donut Specificare il centro dell'anello Specify center of ellipse Specificare il centro dell'ellisse Specify center of polygon Specificare il centro del poligono @@ -933,7 +931,7 @@ menu Modify Edita Specify first point on circle: _tan to Specificare il primo punto sul cerchio: _tan a Specify first source point Specificare il primo punto di origine Specify height Specificare l'altezza - Specify helix height or Specificare l'altezza dell'elica o + Specify helix height Specificare l'altezza dell'elica Specify included angle Specificare l'angolo compreso Specify insertion base point Specificare il punto base di inserimento Specify insertion point Specificare il punto di inserimento @@ -987,7 +985,6 @@ menu Modify Edita Specify new width for all segments Specificare la nuova larghezza di tutti i segmenti Specify next point Specificare il punto successivo Specify offset distance Specificare la distanza di offset - Specify offset distance or Specificare la distanza di offset o Specify opposite corner Specificare l'angolo opposto Specify other corner point Specificare l'altro angolo Specify other endpoint of axis Specificare l'altro estremo dell'asse @@ -1000,7 +997,6 @@ menu Modify Edita Specify radius of circle Specificare il raggio del cerchio Specify rotation angle Specificare l'angolo di rotazione Specify rotation angle of text Specificare l'angolo di rotazione del testo - Specify rotation angle or Specificare l'angolo di rotazione o Specify scale factor Specificare il fattore di scala Specify second angle endpoint Specificare il secondo estremo dell'angolo Specify second break point Specificare il secondo punto di interruzione @@ -1033,9 +1029,9 @@ menu Modify Edita Specify third source point or Specificare il terzo punto di origine o Specify through point Specificare il punto di passaggio Specify top diameter Specificare il diametro superiore - Specify top radius or Specificare il raggio superiore o + Specify top radius Specificare il raggio superiore Specify total angle Specificare l'angolo totale - Specify total length or Specificare la lunghezza totale o + Specify total length Specificare la lunghezza totale Specify width for rectangles Specificare la larghezza dei rettangoli Sphere Sfera Spline Spline diff --git a/crates/ui-egui/src/i18n/ko.tsv b/crates/ui-egui/src/i18n/ko.tsv index f41128ba1..2f269b051 100644 --- a/crates/ui-egui/src/i18n/ko.tsv +++ b/crates/ui-egui/src/i18n/ko.tsv @@ -281,7 +281,7 @@ Enter a vertex editing option 정점 편집 옵션 입력 Enter an option 옵션 입력 Enter angle of xline 구성선 각도 입력 - Enter angle of xline (0) or 구성선 각도 입력(0) 또는 + Enter angle of xline (0) 구성선 각도 입력(0) Enter area of rectangle in current units 현재 단위로 직사각형 면적 입력 Enter attribute prompt 속성 프롬프트 입력 Enter attribute tag name 속성 태그 이름 입력 @@ -292,7 +292,7 @@ Enter default attribute value 기본 속성 값 입력 Enter degree of spline 스플라인 차수 입력 Enter delta angle 각도 변화량 입력 - Enter delta length or 길이 변화량 입력 또는 + Enter delta length 길이 변화량 입력 Enter dimension text 치수 문자 입력 Enter dimension text (<> = measurement) 치수 문자 입력(<> = 측정값) Enter dimension value or [name=expression] 치수 값 또는 [name=expression] 입력 @@ -788,8 +788,8 @@ Select an arc or circle 호 또는 원 선택 Select an arc. 호를 선택하세요. Select an object 객체 선택 - Select an object to change or 변경할 객체 선택 또는 - Select an object to measure or 측정할 객체 선택 또는 + Select an object to change 변경할 객체 선택 + Select an object to measure 측정할 객체 선택 Select an object. 객체를 선택하세요. Select an open curve. 열린 곡선을 선택하세요. Select an open line, arc, polyline, ellipse arc or spline. 열린 선, 호, 폴리선, 타원 호 또는 스플라인을 선택하세요. @@ -803,9 +803,8 @@ Select first line 첫 번째 선 선택 Select first line or arc 첫 번째 선 또는 호 선택 Select first object 첫 번째 객체 선택 - Select first object or 첫 번째 객체 선택 또는 Select first point 첫 번째 점 선택 - Select nested objects to copy or 복사할 내포된 객체 선택 또는 + Select nested objects to copy 복사할 내포된 객체 선택 Select object 객체 선택 Select object to dimension 치수를 기입할 객체 선택 Select object to divide 등분할할 객체 선택 @@ -821,7 +820,7 @@ Select one polyline (Multiple is available through the JSON form). 폴리선 하나를 선택하세요(다중 선택은 JSON 형식으로 사용할 수 있습니다). Select path curve 경로 곡선 선택 Select point or object 점 또는 객체 선택 - Select polyline or 폴리선 선택 또는 + Select polyline 폴리선 선택 Select second line 두 번째 선 선택 Select second line or shift-select to apply corner 두 번째 선 선택 또는 Shift를 누른 채 선택하여 각진 모서리 적용 Select second object 두 번째 객체 선택 @@ -881,9 +880,8 @@ Specify axis endpoint of ellipse 타원 축 끝점 지정 Specify base diameter 밑면 지름 지정 Specify base point 기준점 지정 - Specify base point or 기준점 지정 또는 Specify base point or displacement (drag with the middle mouse button for realtime pan) 기준점 또는 변위 지정(마우스 가운데 버튼으로 드래그하여 실시간 초점 이동) - Specify base radius or 밑면 반지름 지정 또는 + Specify base radius 밑면 반지름 지정 Specify center of donut 도넛 중심 지정 Specify center of ellipse 타원 중심 지정 Specify center of polygon 다각형 중심 지정 @@ -925,7 +923,7 @@ Specify first point on circle: _tan to 원 위의 첫 번째 점 지정: _tan 접점 Specify first source point 첫 번째 원본점 지정 Specify height 높이 지정 - Specify helix height or 나선 높이 지정 또는 + Specify helix height 나선 높이 지정 Specify included angle 사이각 지정 Specify insertion base point 삽입 기준점 지정 Specify insertion point 삽입점 지정 @@ -979,7 +977,6 @@ Specify new width for all segments 모든 구간의 새 폭 지정 Specify next point 다음 점 지정 Specify offset distance 간격띄우기 거리 지정 - Specify offset distance or 간격띄우기 거리 지정 또는 Specify opposite corner 반대 구석 지정 Specify other corner point 다른 구석점 지정 Specify other endpoint of axis 축의 다른 끝점 지정 @@ -992,7 +989,6 @@ Specify radius of circle 원 반지름 지정 Specify rotation angle 회전 각도 지정 Specify rotation angle of text 문자 회전 각도 지정 - Specify rotation angle or 회전 각도 지정 또는 Specify scale factor 축척 비율 지정 Specify second angle endpoint 두 번째 각도 끝점 지정 Specify second break point 두 번째 끊기점 지정 @@ -1025,9 +1021,9 @@ Specify third source point or 세 번째 원본점 지정 또는 <계속> Specify through point 통과점 지정 Specify top diameter 윗면 지름 지정 - Specify top radius or 윗면 반지름 지정 또는 + Specify top radius 윗면 반지름 지정 Specify total angle 전체 각도 지정 - Specify total length or 전체 길이 지정 또는 + Specify total length 전체 길이 지정 Specify width for rectangles 직사각형 폭 지정 Sphere 구 Spline 스플라인 diff --git a/crates/ui-egui/src/i18n/pt_br.tsv b/crates/ui-egui/src/i18n/pt_br.tsv index 3e16b65cf..a000d9b09 100644 --- a/crates/ui-egui/src/i18n/pt_br.tsv +++ b/crates/ui-egui/src/i18n/pt_br.tsv @@ -291,7 +291,7 @@ Enter a vertex editing option Digite uma opção de edição de vértice Enter an option Digite uma opção Enter angle of xline Digite o ângulo da linha infinita - Enter angle of xline (0) or Digite o ângulo da linha infinita (0) ou + Enter angle of xline (0) Digite o ângulo da linha infinita (0) Enter area of rectangle in current units Digite a área do retângulo nas unidades atuais Enter attribute prompt Digite o texto de solicitação do atributo Enter attribute tag name Digite o nome do rótulo do atributo @@ -302,7 +302,7 @@ Enter default attribute value Digite o valor padrão do atributo Enter degree of spline Digite o grau da spline Enter delta angle Digite o ângulo delta - Enter delta length or Digite o comprimento delta ou + Enter delta length Digite o comprimento delta Enter dimension text Digite o texto da cota Enter dimension text (<> = measurement) Digite o texto da cota (<> = medida) Enter dimension value or [name=expression] Digite o valor da cota ou [nome=expressão] @@ -880,8 +880,8 @@ Select an arc or circle Selecione um arco ou círculo Select an arc. Selecione um arco. Select an object Selecione um objeto - Select an object to change or Selecione um objeto para alterar ou - Select an object to measure or Selecione um objeto para medir ou + Select an object to change Selecione um objeto para alterar + Select an object to measure Selecione um objeto para medir Select an object. Selecione um objeto. Select an open curve. Selecione uma curva aberta. Select an open line, arc, polyline, ellipse arc or spline. Selecione uma linha aberta, arco, polilinha, arco elíptico ou spline. @@ -895,9 +895,8 @@ Select first line Selecione a primeira linha Select first line or arc Selecione a primeira linha ou arco Select first object Selecione o primeiro objeto - Select first object or Selecione o primeiro objeto ou Select first point Selecione o primeiro ponto - Select nested objects to copy or Selecione objetos aninhados para copiar ou + Select nested objects to copy Selecione objetos aninhados para copiar Select object Selecione o objeto Select object to dimension Selecione o objeto a cotar Select object to divide Selecione o objeto a dividir @@ -913,7 +912,7 @@ Select one polyline (Multiple is available through the JSON form). Selecione uma polilinha (a opção Multiple está disponível pela forma JSON). Select path curve Selecione a curva da trajetória Select point or object Selecione um ponto ou objeto - Select polyline or Selecione uma polilinha ou + Select polyline Selecione uma polilinha Select second line Selecione a segunda linha Select second line or shift-select to apply corner Selecione a segunda linha ou use Shift para aplicar o canto Select second object Selecione o segundo objeto @@ -966,9 +965,8 @@ Specify axis endpoint of ellipse Especifique a extremidade do eixo da elipse Specify base diameter Especifique o diâmetro da base Specify base point Especifique o ponto base - Specify base point or Especifique o ponto base ou Specify base point or displacement (drag with the middle mouse button for realtime pan) Especifique o ponto base ou o deslocamento (arraste com o botão do meio do mouse para pan em tempo real) - Specify base radius or Especifique o raio da base ou + Specify base radius Especifique o raio da base Specify center of donut Especifique o centro do anel Specify center of ellipse Especifique o centro da elipse Specify center of polygon Especifique o centro do polígono @@ -1010,7 +1008,7 @@ Specify first point on circle: _tan to Especifique o primeiro ponto no círculo: _tan para Specify first source point Especifique o primeiro ponto de origem Specify height Especifique a altura - Specify helix height or Especifique a altura da hélice ou + Specify helix height Especifique a altura da hélice Specify included angle Especifique o ângulo incluído Specify insertion base point Especifique o ponto base de inserção Specify insertion point Especifique o ponto de inserção @@ -1029,7 +1027,6 @@ Specify new width for all segments Especifique a nova largura para todos os segmentos Specify next point Especifique o próximo ponto Specify offset distance Especifique a distância de offset - Specify offset distance or Especifique a distância de offset ou Specify opposite corner Especifique o canto oposto Specify other corner point Especifique o outro ponto de canto Specify other endpoint of axis Especifique a outra extremidade do eixo @@ -1042,7 +1039,6 @@ Specify radius of circle Especifique o raio do círculo Specify rotation angle Especifique o ângulo de rotação Specify rotation angle of text Especifique o ângulo de rotação do texto - Specify rotation angle or Especifique o ângulo de rotação ou Specify scale factor Especifique o fator de escala Specify second angle endpoint Especifique a segunda extremidade do ângulo Specify second break point Especifique o segundo ponto de quebra @@ -1075,9 +1071,9 @@ Specify third source point or Especifique o terceiro ponto de origem ou Specify through point Especifique o ponto de passagem Specify top diameter Especifique o diâmetro superior - Specify top radius or Especifique o raio superior ou + Specify top radius Especifique o raio superior Specify total angle Especifique o ângulo total - Specify total length or Especifique o comprimento total ou + Specify total length Especifique o comprimento total Specify width for rectangles Especifique a largura para retângulos Sphere Esfera Spline Spline diff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsv index 7e4ba21d5..619988f45 100644 --- a/crates/ui-egui/src/i18n/ru.tsv +++ b/crates/ui-egui/src/i18n/ru.tsv @@ -221,7 +221,7 @@ Enter a vertex editing option Введите параметр редактирования вершины Enter an option Введите параметр Enter angle of xline Введите угол вспомогательной прямой - Enter angle of xline (0) or Введите угол вспомогательной прямой (0) или + Enter angle of xline (0) Введите угол вспомогательной прямой (0) Enter area of rectangle in current units Введите площадь прямоугольника в текущих единицах Enter attribute prompt Введите подсказку атрибута Enter attribute tag name Введите имя тега атрибута @@ -232,7 +232,7 @@ Enter default attribute value Введите значение атрибута по умолчанию Enter degree of spline Введите степень сплайна Enter delta angle Введите изменение угла - Enter delta length or Введите изменение длины или + Enter delta length Введите изменение длины Enter dimension text Введите размерный текст Enter dimension text (<> = measurement) Введите размерный текст (<> = измеренное значение) Enter dimension value or [name=expression] Введите размерное значение или [имя=выражение] @@ -675,8 +675,8 @@ Select an arc or circle Выберите дугу или круг Select an arc. Выберите дугу. Select an object Выберите объект - Select an object to change or Выберите объект для изменения или - Select an object to measure or Выберите объект для измерения или + Select an object to change Выберите объект для изменения + Select an object to measure Выберите объект для измерения Select an object. Выберите объект. Select an open curve. Выберите незамкнутую кривую. Select an open line, arc, polyline, ellipse arc or spline. Выберите незамкнутый отрезок, дугу, полилинию, эллиптическую дугу или сплайн. @@ -690,9 +690,8 @@ Select first line Выберите первый отрезок Select first line or arc Выберите первый отрезок или дугу Select first object Выберите первый объект - Select first object or Выберите первый объект или Select first point Выберите первую точку - Select nested objects to copy or Выберите вложенные объекты для копирования или + Select nested objects to copy Выберите вложенные объекты для копирования Select object Выберите объект Select object to dimension Выберите объект для нанесения размера Select object to divide Выберите объект для деления @@ -708,7 +707,7 @@ Select one polyline (Multiple is available through the JSON form). Выберите одну полилинию (Multiple доступно через JSON). Select path curve Выберите кривую траектории Select point or object Выберите точку или объект - Select polyline or Выберите полилинию или + Select polyline Выберите полилинию Select second line Выберите второй отрезок Select second line or shift-select to apply corner Выберите второй отрезок или выберите с Shift, чтобы построить угол Select second object Выберите второй объект @@ -761,9 +760,8 @@ Specify axis endpoint of ellipse Укажите конечную точку оси эллипса Specify base diameter Укажите базовый диаметр Specify base point Укажите базовую точку - Specify base point or Укажите базовую точку или Specify base point or displacement (drag with the middle mouse button for realtime pan) Укажите базовую точку или смещение (перетаскивайте средней кнопкой мыши для панорамирования) - Specify base radius or Укажите базовый радиус или + Specify base radius Укажите базовый радиус Specify center of donut Укажите центр кольца Specify center of ellipse Укажите центр эллипса Specify center of polygon Укажите центр многоугольника @@ -805,7 +803,7 @@ Specify first point on circle: _tan to Укажите первую точку на круге: _tan к Specify first source point Укажите первую исходную точку Specify height Укажите высоту - Specify helix height or Укажите высоту спирали или + Specify helix height Укажите высоту спирали Specify included angle Укажите угол между сторонами Specify insertion base point Укажите базовую точку вставки Specify insertion point Укажите точку вставки @@ -824,7 +822,6 @@ Specify new width for all segments Укажите новую ширину всех сегментов Specify next point Укажите следующую точку Specify offset distance Укажите расстояние смещения - Specify offset distance or Укажите расстояние смещения или Specify opposite corner Укажите противоположный угол Specify other corner point Укажите другую угловую точку Specify other endpoint of axis Укажите другую конечную точку оси @@ -837,7 +834,6 @@ Specify radius of circle Укажите радиус круга Specify rotation angle Укажите угол поворота Specify rotation angle of text Укажите угол поворота текста - Specify rotation angle or Укажите угол поворота или Specify scale factor Укажите масштабный коэффициент Specify second angle endpoint Укажите вторую конечную точку угла Specify second break point Укажите вторую точку разрыва @@ -870,9 +866,9 @@ Specify third source point or Укажите третью исходную точку или <продолжить> Specify through point Укажите точку, через которую проходит прямая Specify top diameter Укажите диаметр вершины - Specify top radius or Укажите радиус вершины или + Specify top radius Укажите радиус вершины Specify total angle Укажите полный угол - Specify total length or Укажите общую длину или + Specify total length Укажите общую длину Specify width for rectangles Укажите ширину прямоугольников Sphere Сфера Spline Сплайн diff --git a/crates/ui-egui/src/i18n/sv.tsv b/crates/ui-egui/src/i18n/sv.tsv index 96114f857..ecfe35b40 100644 --- a/crates/ui-egui/src/i18n/sv.tsv +++ b/crates/ui-egui/src/i18n/sv.tsv @@ -295,7 +295,7 @@ Enter a vertex editing option Ange ett alternativ för hörnpunktsredigering Enter an option Ange ett alternativ Enter angle of xline Ange konstruktionslinjens vinkel - Enter angle of xline (0) or Ange konstruktionslinjens vinkel (0) eller + Enter angle of xline (0) Ange konstruktionslinjens vinkel (0) Enter area of rectangle in current units Ange rektangelns area i aktuella enheter Enter attribute prompt Ange attributets fråga Enter attribute tag name Ange attributets taggnamn @@ -306,7 +306,7 @@ Enter default attribute value Ange attributets standardvärde Enter degree of spline Ange splinens grad Enter delta angle Ange vinkeländring - Enter delta length or Ange längdändring eller + Enter delta length Ange längdändring Enter dimension text Ange måttal Enter dimension text (<> = measurement) Ange måttal (<> = mätvärde) Enter dimension value or [name=expression] Ange måttvärde eller [namn=uttryck] @@ -878,8 +878,8 @@ Select an arc or circle Markera en båge eller cirkel Select an arc. Markera en båge. Select an object Markera ett objekt - Select an object to change or Markera ett objekt att ändra eller - Select an object to measure or Markera ett objekt att mäta eller + Select an object to change Markera ett objekt att ändra + Select an object to measure Markera ett objekt att mäta Select an object. Markera ett objekt. Select an open curve. Markera en öppen kurva. Select an open line, arc, polyline, ellipse arc or spline. Markera en öppen linje, båge, polylinje, ellipsbåge eller spline. @@ -893,9 +893,8 @@ Select first line Markera första linjen Select first line or arc Markera första linjen eller bågen Select first object Markera första objektet - Select first object or Markera första objektet eller Select first point Ange första punkten - Select nested objects to copy or Markera nästlade objekt att kopiera eller + Select nested objects to copy Markera nästlade objekt att kopiera Select object Markera objekt Select object to dimension Markera objekt att måttsätta Select object to divide Markera objekt att dela in @@ -911,7 +910,7 @@ Select one polyline (Multiple is available through the JSON form). Markera en polylinje (Multiple finns i JSON-formen). Select path curve Markera banans kurva Select point or object Ange punkt eller markera objekt - Select polyline or Markera polylinje eller + Select polyline Markera polylinje Select second line Markera andra linjen Select second line or shift-select to apply corner Markera andra linjen, eller Shift-markera för att skapa hörn Select second object Markera andra objektet @@ -964,9 +963,8 @@ Specify axis endpoint of ellipse Ange ellipsens axeländpunkt Specify base diameter Ange basdiameter Specify base point Ange baspunkt - Specify base point or Ange baspunkt eller Specify base point or displacement (drag with the middle mouse button for realtime pan) Ange baspunkt eller förskjutning (dra med mittenknappen för att panorera i realtid) - Specify base radius or Ange basradie eller + Specify base radius Ange basradie Specify center of donut Ange ringens centrum Specify center of ellipse Ange ellipsens centrum Specify center of polygon Ange polygonens centrum @@ -1008,7 +1006,7 @@ Specify first point on circle: _tan to Ange första punkten på cirkeln: _tan to Specify first source point Ange första källpunkten Specify height Ange höjd - Specify helix height or Ange helixens höjd eller + Specify helix height Ange helixens höjd Specify included angle Ange medelpunktsvinkel Specify insertion base point Ange insättningens baspunkt Specify insertion point Ange insättningspunkt @@ -1027,7 +1025,6 @@ Specify new width for all segments Ange ny bredd för alla segment Specify next point Ange nästa punkt Specify offset distance Ange förskjutningsavstånd - Specify offset distance or Ange förskjutningsavstånd eller Specify opposite corner Ange motsatt hörn Specify other corner point Ange andra hörnpunkten Specify other endpoint of axis Ange axelns andra ändpunkt @@ -1040,7 +1037,6 @@ Specify radius of circle Ange cirkelns radie Specify rotation angle Ange rotationsvinkel Specify rotation angle of text Ange textens rotationsvinkel - Specify rotation angle or Ange rotationsvinkel eller Specify scale factor Ange skalfaktor Specify second angle endpoint Ange vinkelns andra ändpunkt Specify second break point Ange andra brytpunkten @@ -1073,9 +1069,9 @@ Specify third source point or Ange tredje källpunkten eller Specify through point Ange genomgångspunkt Specify top diameter Ange toppdiameter - Specify top radius or Ange toppradie eller + Specify top radius Ange toppradie Specify total angle Ange total vinkel - Specify total length or Ange total längd eller + Specify total length Ange total längd Specify width for rectangles Ange bredd för rektanglar Sphere Sfär Spline Spline diff --git a/crates/ui-egui/src/i18n/tests.rs b/crates/ui-egui/src/i18n/tests.rs index 492b9e595..24fac6a79 100644 --- a/crates/ui-egui/src/i18n/tests.rs +++ b/crates/ui-egui/src/i18n/tests.rs @@ -40,6 +40,17 @@ fn catalog_is_valid_and_preserves_placeholders_and_ellipses() { assert_eq!(tr("unknown", "Open"), "Open"); } +/// A prompt with options is translated without its own "or": the command line adds the +/// translated " or [" before the options ("Select polyline or [Multiple]"). +#[test] +fn prompts_with_options_leave_or_to_the_command_line() { + assert_eq!(format!("{}{}", tr("de", "Select polyline"), tr("de", " or [")), "Wählen Sie eine Polylinie oder ["); + assert_eq!(format!("{}{}", tr("ko", "Specify helix height"), tr("ko", " or [")), "나선 높이 지정 또는 ["); + assert_eq!(format!("{}{}", tr("zh", "Enter delta length"), tr("zh", " or [")), "输入增量长度 或 ["); + assert_eq!(format!("{}{}", tr("vi", "Select polyline"), tr("vi", " or [")), "Chọn polyline hoặc ["); + assert_eq!(format!("{}{}", tr("vi", "Specify base radius"), tr("vi", " or [")), "Chỉ định bán kính đáy hoặc ["); +} + /// What each language shows in English for now. Missing translations never fail a build: /// `cargo xtask i18n`, a step of `cargo xtask ci`, runs this to print the list. #[test] diff --git a/crates/ui-egui/src/i18n/tr.tsv b/crates/ui-egui/src/i18n/tr.tsv index 3fed6bc3b..cc8576aa7 100644 --- a/crates/ui-egui/src/i18n/tr.tsv +++ b/crates/ui-egui/src/i18n/tr.tsv @@ -351,7 +351,7 @@ Enter a vertex editing option Bir köşe noktası düzenleme seçeneği girin Enter an option Bir seçenek girin Enter angle of xline Yapı çizgisinin açısını girin - Enter angle of xline (0) or Yapı çizgisinin açısını girin (0) veya + Enter angle of xline (0) Yapı çizgisinin açısını girin (0) Enter area of rectangle in current units Dikdörtgenin alanını geçerli birimlerle girin Enter attribute prompt Öznitelik istemini girin Enter attribute tag name Öznitelik etiket adını girin @@ -362,7 +362,7 @@ Enter default attribute value Varsayılan öznitelik değerini girin Enter degree of spline Spline derecesini girin Enter delta angle Açı farkını girin - Enter delta length or Uzunluk farkını girin veya + Enter delta length Uzunluk farkını girin Enter dimension text Ölçü metnini girin Enter dimension text (<> = measurement) Ölçü metnini girin (<> = ölçülen değer) Enter dimension value or [name=expression] Ölçü değerini girin veya [name=expression] @@ -932,8 +932,8 @@ Select an arc or circle Bir yay veya daire seçin Select an arc. Bir yay seçin. Select an object Bir nesne seçin - Select an object to change or Değiştirilecek nesneyi seçin veya - Select an object to measure or Ölçülecek nesneyi seçin veya + Select an object to change Değiştirilecek nesneyi seçin + Select an object to measure Ölçülecek nesneyi seçin Select an object. Bir nesne seçin. Select an open curve. Açık bir eğri seçin. Select an open line, arc, polyline, ellipse arc or spline. Açık bir çizgi, yay, çoklu çizgi, elips yayı veya spline seçin. @@ -947,9 +947,8 @@ Select first line İlk çizgiyi seçin Select first line or arc İlk çizgiyi veya yayı seçin Select first object İlk nesneyi seçin - Select first object or İlk nesneyi seçin veya Select first point İlk noktayı seçin - Select nested objects to copy or Kopyalanacak iç içe nesneleri seçin veya + Select nested objects to copy Kopyalanacak iç içe nesneleri seçin Select object Nesne seçin Select object to dimension Ölçülendirilecek nesneyi seçin Select object to divide Bölünecek nesneyi seçin @@ -966,7 +965,7 @@ Select one polyline (Multiple is available through the JSON form). Bir çoklu çizgi seçin (Multiple seçeneği JSON biçimiyle kullanılabilir). Select path curve Yol eğrisini seçin Select point or object Nokta veya nesne seçin - Select polyline or Çoklu çizgi seçin veya + Select polyline Çoklu çizgi seçin Select second line İkinci çizgiyi seçin Select second line or shift-select to apply corner İkinci çizgiyi seçin ya da köşe uygulamak için Shift ile seçin Select second object İkinci nesneyi seçin @@ -1047,9 +1046,8 @@ Specify axis endpoint of ellipse Elipsin eksen uç noktasını belirtin Specify base diameter Taban çapını belirtin Specify base point Taban noktasını belirtin - Specify base point or Taban noktasını belirtin veya Specify base point or displacement (drag with the middle mouse button for realtime pan) Taban noktasını veya yer değiştirmeyi belirtin (gerçek zamanlı kaydırma için orta fare düğmesiyle sürükleyin) - Specify base radius or Taban yarıçapını belirtin veya + Specify base radius Taban yarıçapını belirtin Specify center of donut Halkanın merkezini belirtin Specify center of ellipse Elipsin merkezini belirtin Specify center of polygon Çokgenin merkezini belirtin @@ -1091,7 +1089,7 @@ Specify first point on circle: _tan to Daire üzerindeki ilk noktayı belirtin: _tan to Specify first source point İlk kaynak noktayı belirtin Specify height Yüksekliği belirtin - Specify helix height or Helezon yüksekliğini belirtin veya + Specify helix height Helezon yüksekliğini belirtin Specify included angle İç açıyı belirtin Specify insertion base point Ekleme taban noktasını belirtin Specify insertion point Ekleme noktasını belirtin @@ -1110,7 +1108,6 @@ Specify new width for all segments Tüm parçalar için yeni genişliği belirtin Specify next point Sonraki noktayı belirtin Specify offset distance Öteleme uzaklığını belirtin - Specify offset distance or Öteleme uzaklığını belirtin veya Specify opposite corner Karşı köşeyi belirtin Specify other corner point Diğer köşe noktasını belirtin Specify other endpoint of axis Eksenin diğer uç noktasını belirtin @@ -1123,7 +1120,6 @@ Specify radius of circle Dairenin yarıçapını belirtin Specify rotation angle Döndürme açısını belirtin Specify rotation angle of text Metnin döndürme açısını belirtin - Specify rotation angle or Döndürme açısını belirtin veya Specify scale factor Ölçek faktörünü belirtin Specify second angle endpoint Açının ikinci uç noktasını belirtin Specify second break point İkinci kırma noktasını belirtin @@ -1156,9 +1152,9 @@ Specify third source point or Üçüncü kaynak noktayı belirtin veya Specify through point Geçiş noktasını belirtin Specify top diameter Üst çapı belirtin - Specify top radius or Üst yarıçapı belirtin veya + Specify top radius Üst yarıçapı belirtin Specify total angle Toplam açıyı belirtin - Specify total length or Toplam uzunluğu belirtin veya + Specify total length Toplam uzunluğu belirtin Specify width for rectangles Dikdörtgenler için genişliği belirtin Sphere Küre Spherical Küresel diff --git a/crates/ui-egui/src/i18n/uk.tsv b/crates/ui-egui/src/i18n/uk.tsv index bbb70e489..8d8c6b875 100644 --- a/crates/ui-egui/src/i18n/uk.tsv +++ b/crates/ui-egui/src/i18n/uk.tsv @@ -286,7 +286,7 @@ Enter a vertex editing option Введіть параметр редагування вершини Enter an option Введіть параметр Enter angle of xline Введіть кут допоміжної прямої - Enter angle of xline (0) or Введіть кут допоміжної прямої (0) або + Enter angle of xline (0) Введіть кут допоміжної прямої (0) Enter area of rectangle in current units Введіть площу прямокутника в поточних одиницях Enter attribute prompt Введіть підказку атрибута Enter attribute tag name Введіть ім'я тегу атрибута @@ -297,7 +297,7 @@ Enter default attribute value Введіть типове значення атрибута Enter degree of spline Введіть степінь сплайна Enter delta angle Введіть зміну кута - Enter delta length or Введіть зміну довжини або + Enter delta length Введіть зміну довжини Enter dimension text Введіть розмірний текст Enter dimension text (<> = measurement) Введіть розмірний текст (<> = виміряне значення) Enter dimension value or [name=expression] Введіть розмірне значення або [name=expression] @@ -803,8 +803,8 @@ Select an arc or circle Виберіть дугу або коло Select an arc. Виберіть дугу. Select an object Виберіть об'єкт - Select an object to change or Виберіть об'єкт для зміни або - Select an object to measure or Виберіть об'єкт для вимірювання або + Select an object to change Виберіть об'єкт для зміни + Select an object to measure Виберіть об'єкт для вимірювання Select an object. Виберіть об'єкт. Select an open curve. Виберіть незамкнену криву. Select an open line, arc, polyline, ellipse arc or spline. Виберіть незамкнений відрізок, дугу, полілінію, еліптичну дугу або сплайн. @@ -818,9 +818,8 @@ Select first line Виберіть перший відрізок Select first line or arc Виберіть перший відрізок або дугу Select first object Виберіть перший об'єкт - Select first object or Виберіть перший об'єкт або Select first point Виберіть першу точку - Select nested objects to copy or Виберіть вкладені об'єкти для копіювання або + Select nested objects to copy Виберіть вкладені об'єкти для копіювання Select object Виберіть об'єкт Select object to dimension Виберіть об'єкт для нанесення розміру Select object to divide Виберіть об'єкт для поділу @@ -841,7 +840,7 @@ Select one polyline (Multiple is available through the JSON form). Виберіть одну полілінію (Multiple доступно через JSON). Select path curve Виберіть криву траєкторії Select point or object Виберіть точку або об'єкт - Select polyline or Виберіть полілінію або + Select polyline Виберіть полілінію Select second line Виберіть другий відрізок Select second line or shift-select to apply corner Виберіть другий відрізок або виберіть із Shift, щоб утворити кут Select second object Виберіть другий об'єкт @@ -901,9 +900,8 @@ Specify axis endpoint of ellipse Укажіть кінцеву точку осі еліпса Specify base diameter Укажіть діаметр основи Specify base point Укажіть базову точку - Specify base point or Укажіть базову точку або Specify base point or displacement (drag with the middle mouse button for realtime pan) Укажіть базову точку або зміщення (перетягуйте середньою кнопкою миші для панорамування) - Specify base radius or Укажіть радіус основи або + Specify base radius Укажіть радіус основи Specify center of donut Укажіть центр кільця Specify center of ellipse Укажіть центр еліпса Specify center of polygon Укажіть центр багатокутника @@ -945,7 +943,7 @@ Specify first point on circle: _tan to Укажіть першу точку на колі: _tan до Specify first source point Укажіть першу вихідну точку Specify height Укажіть висоту - Specify helix height or Укажіть висоту спіралі або + Specify helix height Укажіть висоту спіралі Specify included angle Укажіть кут між сторонами Specify insertion base point Укажіть базову точку вставлення Specify insertion point Укажіть точку вставлення @@ -1029,7 +1027,6 @@ Specify new width for all segments Укажіть нову ширину всіх сегментів Specify next point Укажіть наступну точку Specify offset distance Укажіть відстань зміщення - Specify offset distance or Укажіть відстань зміщення або Specify opposite corner Укажіть протилежний кут Specify other corner point Укажіть іншу кутову точку Specify other endpoint of axis Укажіть іншу кінцеву точку осі @@ -1042,7 +1039,6 @@ Specify radius of circle Укажіть радіус кола Specify rotation angle Укажіть кут повороту Specify rotation angle of text Укажіть кут повороту тексту - Specify rotation angle or Укажіть кут повороту або Specify scale factor Укажіть коефіцієнт масштабу Specify second angle endpoint Укажіть другу кінцеву точку кута Specify second break point Укажіть другу точку розриву @@ -1075,9 +1071,9 @@ Specify third source point or Укажіть третю вихідну точку або <продовжити> Specify through point Укажіть точку, через яку проходить пряма Specify top diameter Укажіть діаметр вершини - Specify top radius or Укажіть радіус вершини або + Specify top radius Укажіть радіус вершини Specify total angle Укажіть загальний кут - Specify total length or Укажіть загальну довжину або + Specify total length Укажіть загальну довжину Specify width for rectangles Укажіть ширину прямокутників Sphere Сфера Spline Сплайн diff --git a/crates/ui-egui/src/i18n/vi.tsv b/crates/ui-egui/src/i18n/vi.tsv index 8887bb45b..f1b8dca7b 100644 --- a/crates/ui-egui/src/i18n/vi.tsv +++ b/crates/ui-egui/src/i18n/vi.tsv @@ -280,7 +280,7 @@ Enter a vertex editing option Nhập tùy chọn sửa đỉnh Enter an option Nhập một tùy chọn Enter angle of xline Nhập góc của đường dựng - Enter angle of xline (0) or Nhập góc của đường dựng (0) hoặc + Enter angle of xline (0) Nhập góc của đường dựng (0) Enter area of rectangle in current units Nhập diện tích hình chữ nhật theo đơn vị hiện hành Enter attribute prompt Nhập lời nhắc attribute Enter attribute tag name Nhập tên thẻ attribute @@ -291,7 +291,7 @@ Enter default attribute value Nhập giá trị attribute mặc định Enter degree of spline Nhập bậc của spline Enter delta angle Nhập góc chênh lệch - Enter delta length or Nhập chiều dài chênh lệch hoặc + Enter delta length Nhập chiều dài chênh lệch Enter dimension text Nhập chữ kích thước Enter dimension text (<> = measurement) Nhập chữ kích thước (<> = số đo) Enter dimension value or [name=expression] Nhập giá trị kích thước hoặc [name=expression] @@ -787,8 +787,8 @@ Select an arc or circle Chọn một cung tròn hoặc đường tròn Select an arc. Chọn một cung tròn. Select an object Chọn một đối tượng - Select an object to change or Chọn một đối tượng để thay đổi hoặc - Select an object to measure or Chọn một đối tượng để đo hoặc + Select an object to change Chọn một đối tượng để thay đổi + Select an object to measure Chọn một đối tượng để đo Select an object. Chọn một đối tượng. Select an open curve. Chọn một đường cong hở. Select an open line, arc, polyline, ellipse arc or spline. Chọn một đoạn thẳng, cung tròn, polyline, cung elip hoặc spline hở. @@ -802,14 +802,13 @@ Select first line Chọn đoạn thẳng thứ nhất Select first line or arc Chọn đoạn thẳng hoặc cung tròn thứ nhất Select first object Chọn đối tượng thứ nhất - Select first object or Chọn đối tượng thứ nhất hoặc Select first point Chọn điểm thứ nhất Add to Working Set Thêm vào Working Set Edit Reference In-Place Chỉnh sửa tham chiếu tại chỗ Save Reference Edits Lưu chỉnh sửa tham chiếu Working Set Working Set Xref and Block In-place Editing Chỉnh sửa Xref và Block tại chỗ - Select nested objects to copy or Chọn đối tượng lồng nhau để sao chép hoặc + Select nested objects to copy Chọn đối tượng lồng nhau để sao chép Select object Chọn đối tượng Select object to dimension Chọn đối tượng để ghi kích thước Select object to divide Chọn đối tượng để chia @@ -825,7 +824,7 @@ Select one polyline (Multiple is available through the JSON form). Chọn một polyline (có thể chọn nhiều qua dạng JSON). Select path curve Chọn đường cong làm đường dẫn Select point or object Chọn điểm hoặc đối tượng - Select polyline or Chọn polyline hoặc + Select polyline Chọn polyline Select second line Chọn đoạn thẳng thứ hai Select second line or shift-select to apply corner Chọn đoạn thẳng thứ hai hoặc chọn kèm Shift để áp dụng góc Select second object Chọn đối tượng thứ hai @@ -883,9 +882,8 @@ Specify axis endpoint of ellipse Chỉ định điểm cuối trục của elip Specify base diameter Chỉ định đường kính đáy Specify base point Chỉ định điểm gốc - Specify base point or Chỉ định điểm gốc hoặc Specify base point or displacement (drag with the middle mouse button for realtime pan) Chỉ định điểm gốc hoặc độ dời (kéo bằng nút giữa chuột để pan theo thời gian thực) - Specify base radius or Chỉ định bán kính đáy hoặc + Specify base radius Chỉ định bán kính đáy Specify center of donut Chỉ định tâm vành tròn Specify center of ellipse Chỉ định tâm elip Specify center of polygon Chỉ định tâm đa giác @@ -927,7 +925,7 @@ Specify first point on circle: _tan to Chỉ định điểm thứ nhất trên đường tròn: _tan to Specify first source point Chỉ định điểm nguồn thứ nhất Specify height Chỉ định chiều cao - Specify helix height or Chỉ định chiều cao đường xoắn ốc hoặc + Specify helix height Chỉ định chiều cao đường xoắn ốc Specify included angle Chỉ định góc chắn của cung Specify insertion base point Chỉ định điểm gốc chèn Specify insertion point Chỉ định điểm chèn @@ -981,7 +979,6 @@ Specify new width for all segments Chỉ định bề rộng mới cho tất cả các đoạn Specify next point Chỉ định điểm tiếp theo Specify offset distance Chỉ định khoảng cách offset - Specify offset distance or Chỉ định khoảng cách offset hoặc Specify opposite corner Chỉ định góc đối diện Specify other corner point Chỉ định điểm góc đối diện Specify other endpoint of axis Chỉ định đầu kia của trục @@ -994,7 +991,6 @@ Specify radius of circle Chỉ định bán kính đường tròn Specify rotation angle Chỉ định góc xoay Specify rotation angle of text Chỉ định góc xoay của chữ - Specify rotation angle or Chỉ định góc xoay hoặc Specify scale factor Chỉ định hệ số tỉ lệ Specify second angle endpoint Chỉ định đầu thứ hai của góc Specify second break point Chỉ định điểm ngắt thứ hai @@ -1027,9 +1023,9 @@ Specify third source point or Chỉ định điểm nguồn thứ ba hoặc Specify through point Chỉ định điểm đi qua Specify top diameter Chỉ định đường kính trên - Specify top radius or Chỉ định bán kính trên hoặc + Specify top radius Chỉ định bán kính trên Specify total angle Chỉ định tổng góc - Specify total length or Chỉ định tổng chiều dài hoặc + Specify total length Chỉ định tổng chiều dài Specify width for rectangles Chỉ định bề rộng cho hình chữ nhật Sphere Hình cầu Spline Spline diff --git a/crates/ui-egui/src/i18n/zh.tsv b/crates/ui-egui/src/i18n/zh.tsv index 9ca85fea7..de4e6a633 100644 --- a/crates/ui-egui/src/i18n/zh.tsv +++ b/crates/ui-egui/src/i18n/zh.tsv @@ -222,7 +222,7 @@ Enter a vertex editing option 输入顶点编辑选项 Enter an option 输入选项 Enter angle of xline 输入构造线角度 - Enter angle of xline (0) or 输入构造线角度 (0) 或 + Enter angle of xline (0) 输入构造线角度 (0) Enter area of rectangle in current units 在当前单位中输入矩形面积 Enter attribute prompt 输入属性提示 Enter attribute tag name 输入属性标记名称 @@ -233,7 +233,7 @@ Enter default attribute value 输入默认属性值 Enter degree of spline 输入样条曲线次数 Enter delta angle 输入增量角度 - Enter delta length or 输入增量长度或 + Enter delta length 输入增量长度 Enter dimension text 输入标注文字 Enter dimension text (<> = measurement) 输入标注文字 (<> = 测量值) Enter dimension value or [name=expression] 输入标注值或 [名称=表达式] @@ -669,8 +669,8 @@ Select an arc or circle 选择圆弧或圆 Select an arc. 选择一段圆弧。 Select an object 选择对象 - Select an object to change or Select an object to change or - Select an object to measure or Select an object to measure or + Select an object to change Select an object to change + Select an object to measure Select an object to measure Add to Working Set 添加到工作集 Edit Reference In-Place 就地编辑参照 Save Reference Edits 保存参照编辑 @@ -689,9 +689,8 @@ Select first line 选择第一条直线 Select first line or arc 选择第一条直线或圆弧 Select first object 选择第一个对象 - Select first object or Select first object or Select first point 指定第一点 - Select nested objects to copy or Select nested objects to copy or + Select nested objects to copy Select nested objects to copy Select object 选择对象 Select object to dimension 选择要标注的对象 Select object to divide 选择要定数等分的对象 @@ -707,7 +706,7 @@ Select one polyline (Multiple is available through the JSON form). 选择一条多段线(可通过 JSON 表单选择多个)。 Select path curve 选择路径曲线 Select point or object 选择点或对象 - Select polyline or 选择多段线或 + Select polyline 选择多段线 Select second line 选择第二条直线 Select second line or shift-select to apply corner 选择第二条直线,或按 Shift 键应用角点 Select second object 选择第二个对象 @@ -760,9 +759,8 @@ Specify axis endpoint of ellipse 指定椭圆的轴端点 Specify base diameter 指定底面直径 Specify base point 指定基点 - Specify base point or Specify base point or Specify base point or displacement (drag with the middle mouse button for realtime pan) 指定基点或位移(按住鼠标中键拖动以进行实时平移) - Specify base radius or Specify base radius or + Specify base radius Specify base radius Specify center of donut 指定圆环中心 Specify center of ellipse 指定椭圆中心 Specify center of polygon 指定多边形中心 @@ -804,7 +802,7 @@ Specify first point on circle: _tan to 指定圆上的第一点: _tan 到 Specify first source point 指定第一个源点 Specify height 指定高度 - Specify helix height or Specify helix height or + Specify helix height Specify helix height Specify included angle 指定夹角 Specify insertion base point 指定插入基点 Specify insertion point 指定插入点 @@ -823,7 +821,6 @@ Specify new width for all segments 指定所有段的新宽度 Specify next point 指定下一点 Specify offset distance 指定偏移距离 - Specify offset distance or Specify offset distance or Specify opposite corner 指定对角点 Specify other corner point 指定另一角点 Specify other endpoint of axis 指定轴的另一端点 @@ -836,7 +833,6 @@ Specify radius of circle 指定圆半径 Specify rotation angle 指定旋转角度 Specify rotation angle of text 指定文字旋转角度 - Specify rotation angle or Specify rotation angle or Specify scale factor 指定比例因子 Specify second angle endpoint 指定第二角度端点 Specify second break point 指定第二打断点 @@ -869,9 +865,9 @@ Specify third source point or 指定第三个源点或 <继续> Specify through point 指定通过点 Specify top diameter 指定顶面直径 - Specify top radius or Specify top radius or + Specify top radius Specify top radius Specify total angle 指定总角度 - Specify total length or Specify total length or + Specify total length Specify total length Specify width for rectangles 指定矩形宽度 Sphere 球体 Spline 样条曲线 From beb6c48a591d499f3b6125147ca550eeb4ae546a Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Tue, 3 Nov 2026 04:36:42 +0700 Subject: [PATCH 066/100] AREA, PERIMETER and DISTANCE system variables, as AutoCAD sets them (#2552) * feat(inquiry): AREA, PERIMETER and DISTANCE system variables, as AutoCAD sets them AREA, DIST and MEASUREGEOM now leave AREA, PERIMETER and DISTANCE for getvar, read-only and not saved: the area just measured (the running total in Add and Subtract mode) with the last area's perimeter, and the distance (the total of Multiple points). After DIST's two points LASTPOINT stays at the first point, as measured. AREA's Object option measures splines, closed by their chord like open polylines. Co-Authored-By: Claude Opus 5.5 * Address review: spline area over narrow spans, finite distances, list order - AREA of a spline samples it span by span (Spline::tessellate_spans), so a narrow knot span no longer drops out of the area. The spread over the spans is one helper shared with Spline::bounds, whose boxes are unchanged. - DIST refuses a distance that isn't finite on all three paths and leaves DISTANCE as it was. - sysvars::list puts the session's values after the drawing's header, so it shows what get returns (AREA, ORTHOMODE, LWDISPLAY...). - LASTPOINT after DIST's Multiple points is pinned to AutoCAD 2024's measured behaviour: the last point entered, also after Length and Undo. Co-Authored-By: Claude Opus 5.5 * Address review: measure every knot span of a spline in AREA A spline with more knot spans than half the sample cap fell back to an even grid over the domain, which can step over a narrow span and measure the wrong area: a 100 x 100 square as a degree-1 spline, its right and top edges two spans a millionth wide after 4,500 spans along the bottom, measured 4,998.75. Spread the samples over the spans at any span count: at least one segment per span, within the cap up to MAX_SAMPLES spans and one each past it, as Spline::length integrates every span. One evaluation per span costs 85-285 ms on a million spans in a release build, against 32-52 s for the length AREA already takes. Co-Authored-By: Claude Opus 5.5 * Address review: share AREA's spline samples by how much each span bends Past the sample cap, tessellate_spans gave each knot span one segment, so a bent span among many straight ones was measured by its chord. Example: a quadratic spline along the x-axis, one control point 1000 above it, past 4,500 spans. It measured 750; the area is 1750/3. Each span now keeps one segment. A span whose control points are collinear is straight, so its chord is exact. tessellate's count is shared among the spans that bend, by the cube root of a measure of their control points' convex hull, which holds the span and its chord. The shares are rounded on the running total, so they use the whole count however many spans share it. The example now measures 583.33343, below the cap and past it. The spreading by parameter width that this replaces goes, and span_params with it: bounds is main's code again. Co-Authored-By: Claude Opus 5.5 * DIST: a measurement refused as too large leaves LASTPOINT at the first point Session::feed sets LASTPOINT to the second point before DIST checks the distance, and the refusal returned before DIST put the first point back, as it does after a measurement. It now does so before refusing. Co-Authored-By: Claude Opus 5.5 * AREA: a spline too long to measure is too large, not a perimeter of 0 Spline::length reads a length past f64's range as 0, and AREA took that as the spline's perimeter. Spline::checked_length returns None there, and AREA reports the perimeter too large, as it does a polyline's. The overflow was hidden twice: in the sum, and before it in the derivative, which read as zero speed once past f64's range. The length integral now takes the derivative before that (Spline::velocity), so an overflowing speed counts too. param_at_length keeps the speed it had, and finite lengths are unchanged. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/inquiry.rs | 37 ++- crates/engine/src/lib.rs | 9 + crates/engine/src/sysvars.rs | 21 +- crates/engine/tests/it/inquiry_variables.rs | 271 ++++++++++++++++++++ crates/engine/tests/it/main.rs | 1 + crates/geom/src/nurbs.rs | 68 +++-- crates/geom/src/spline.rs | 61 ++++- crates/geom/src/spline/tests.rs | 77 ++++++ 8 files changed, 521 insertions(+), 24 deletions(-) create mode 100644 crates/engine/tests/it/inquiry_variables.rs diff --git a/crates/engine/src/cmd/inquiry.rs b/crates/engine/src/cmd/inquiry.rs index 119d727e1..364fa6796 100644 --- a/crates/engine/src/cmd/inquiry.rs +++ b/crates/engine/src/cmd/inquiry.rs @@ -217,6 +217,11 @@ fn run_dist(s: &mut Session, p: &Value) -> Result { let a = point3_req(s, "dist", p, "p1")?; let b = point3_req(s, "dist", p, "p2")?; let d = b - a; + if !d.len().is_finite() { + return Err(bad("dist", "the distance is too large")); + } + s.distance = d.len(); + s.last_point = a.xy(); Ok(json!({ "distance": d.len(), "angle": d.xy().angle().to_degrees(), @@ -430,6 +435,8 @@ fn run_area(s: &mut Session, p: &Value) -> Result { if let Some(what) = too_large(a, per) { return Err(bad("area", format!("the {what} is too large"))); } + s.area = a; + s.perimeter = per; Ok(json!({ "area": a, "perimeter": per, "message": area_text(s, a, per) })) } @@ -516,6 +523,13 @@ pub(super) fn object_area(s: &Session, h: Handle) -> Result<(f64, f64)> { total } EntityKind::Region(r) => (r.area(), r.perimeter()), + // Like an open polyline: the area closed by the chord, the perimeter its own length. + EntityKind::Spline(sp) if sp.is_valid() => { + let b = sp.bounds(); + let pts = sp.tessellate_spans((b.min.dist(b.max) * 1e-7).max(1e-12)); + // A length past f64's range is too large to report, as a polyline's is, not 0. + (cadcraft_geom::shoelace(&pts).abs(), sp.checked_length().unwrap_or(f64::INFINITY)) + } _ => return Err(bad("area", "object has no area")), }) } @@ -872,16 +886,25 @@ impl Interactive for DistM { self.first_source = s.picked_viewport; Ok(Step::Continue) } - (Some(a), Input::Point(b)) => { + (Some(first), Input::Point(b)) => { let (a, b) = match (self.first_source, s.picked_viewport) { (Some(first), Some(second)) if first.viewport == second.viewport => (first.point, second.point), - _ => (a, b), + _ => (first, b), }; let (za, zb) = if self.first_source.is_some() || s.picked_viewport.is_some() { (0.0, 0.0) } else { (z_at(s, a), z_at(s, b)) }; - let t = dist_text(s, Vec3::new(a.x, a.y, za), Vec3::new(b.x, b.y, zb)); + let (a, b) = (Vec3::new(a.x, a.y, za), Vec3::new(b.x, b.y, zb)); + if !(b - a).len().is_finite() { + // The first point stays LASTPOINT, as after a measurement. + s.last_point = first; + return Err(bad("dist", "the distance is too large")); + } + let t = dist_text(s, a, b); for l in t.lines() { s.echo(l.to_string()); } + // As in AutoCAD, the second point doesn't become LASTPOINT: the first stays. + s.distance = (b - a).len(); + s.last_point = first; Ok(Step::Done) } (Some(a), Input::Keyword(k)) if k == "Multiple points" => { @@ -909,7 +932,11 @@ impl DistM { .as_deref() .or(self.multi.as_deref()) .map_or(0.0, |pts| if closed { arcs::polyline(pts, &self.bulges, true).len() } else { path_len(pts, &self.bulges) }); + if !d.is_finite() { + return Err(bad("dist", "the distance is too large")); + } s.echo(format!("Distance = {}", fmt_d(s, d))); + s.distance = d; Ok(Step::Done) } } @@ -1123,10 +1150,14 @@ impl AreaM { return if self.mode == AreaMode::Single { Step::Done } else { Step::Continue }; } s.echo(area_text(s, a, per)); + // AREA: this area, or in Add and Subtract mode the running total; PERIMETER: this area's. + s.perimeter = per; if self.mode == AreaMode::Single { + s.area = a; return Step::Done; } self.total = total; + s.area = total; s.echo(format!("Total area = {}", fmt_d(s, self.total))); Step::Continue } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 8f84b35d7..d1fa2b03d 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -92,6 +92,12 @@ pub struct Session { pub last_command: Option, /// LASTPOINT. pub last_point: Vec2, + /// AREA and PERIMETER: the last area AREA or MEASUREGEOM measured (in their Add and Subtract + /// modes the running total) and that area's perimeter. Not saved, as in AutoCAD. + pub area: f64, + pub perimeter: f64, + /// DISTANCE: the last distance DIST or MEASUREGEOM measured (with Multiple points, the total). + pub distance: f64, /// The last segment given at a point prompt (its base point and the point): polar angles /// relative to the last segment (POLARMODE bit 1) are measured from it. pub last_segment: Option<(Vec2, Vec2)>, @@ -173,6 +179,9 @@ impl Session { log: Vec::new(), last_command: None, last_point: Vec2::ZERO, + area: 0.0, + perimeter: 0.0, + distance: 0.0, last_segment: None, last_polar: None, point_mods: Vec::new(), diff --git a/crates/engine/src/sysvars.rs b/crates/engine/src/sysvars.rs index 3fe47c052..fcecbd2cf 100644 --- a/crates/engine/src/sysvars.rs +++ b/crates/engine/src/sysvars.rs @@ -67,6 +67,9 @@ const SESSION_VARS: &[&str] = &[ "ZOOMFACTOR", "MEASUREINIT", "LASTPOINT", + "AREA", + "PERIMETER", + "DISTANCE", "FONTALT", "FONTFALLBACK", "SAVETIME", @@ -145,6 +148,9 @@ pub fn get(s: &Session, name: &str) -> Option { "ZOOMFACTOR" => json!(st.zoomfactor), "MEASUREINIT" => json!(i32::from(st.measureinit)), "LASTPOINT" => json!([s.last_point.x, s.last_point.y, 0.0]), + "AREA" => json!(s.area), + "PERIMETER" => json!(s.perimeter), + "DISTANCE" => json!(s.distance), // Held in radians (`$ANGBASE` in DXF is in degrees); reported in degrees, like POLARANG. "ANGBASE" => json!(s.doc().ok()?.header.f64("ANGBASE", 0.0).to_degrees()), "CONSTRAINTINFER" => json!(i32::from(s.doc().ok()?.parametric.settings.infer)), @@ -249,6 +255,9 @@ const READ_ONLY: &[&str] = &[ "BLOCKEDITOR", "CTAB", "LASTPOINT", + "AREA", + "PERIMETER", + "DISTANCE", "VIEWCTR", "VIEWSIZE", "EXTMIN", @@ -464,11 +473,6 @@ pub fn set(s: &mut Session, name: &str, v: &Value) -> Result<()> { pub fn list(s: &Session) -> Value { let mut m = serde_json::Map::new(); - for n in SESSION_VARS { - if let Some(v) = get(s, n) { - m.insert((*n).to_string(), v); - } - } if let Ok(d) = s.doc() { for (k, v) in &d.header.vars { if matches!(k.as_str(), "ANGBASE" | "SNAPUNIT" | "GRIDUNIT" | "CECOLOR") { @@ -478,6 +482,13 @@ pub fn list(s: &Session) -> Value { } } } + // The session's values last: a drawing's header can carry the same names (ORTHOMODE, + // LWDISPLAY, an AREA), and `get` reads the session's. + for n in SESSION_VARS { + if let Some(v) = get(s, n) { + m.insert((*n).to_string(), v); + } + } table::list(s, &mut m); Value::Object(m) } diff --git a/crates/engine/tests/it/inquiry_variables.rs b/crates/engine/tests/it/inquiry_variables.rs new file mode 100644 index 000000000..f42749458 --- /dev/null +++ b/crates/engine/tests/it/inquiry_variables.rs @@ -0,0 +1,271 @@ +//! AREA, PERIMETER and DISTANCE as AutoCAD 2024 leaves them after AREA, DIST and MEASUREGEOM +//! (measured with accoreconsole, running these scripts on these drawings): AREA is the area just +//! measured, or the running total in Add and Subtract mode, PERIMETER the perimeter of the last +//! area; DISTANCE the distance measured, or the total of DIST's Multiple points. After DIST's two +//! points LASTPOINT is still the first; after Multiple points it is the last. + +use cadcraft_engine::doc::{Common, EntityKind, Handle}; +use cadcraft_engine::geom::{Spline, Vec2}; +use cadcraft_engine::{Input, Session, sysvars}; +use serde_json::{Value, json}; + +fn var(s: &Session, name: &str) -> f64 { + sysvars::get(s, name).and_then(|v| v.as_f64()).unwrap_or_else(|| panic!("{name} not a number")) +} + +fn lastpoint(s: &Session) -> (f64, f64) { + let v = sysvars::get(s, "LASTPOINT").unwrap(); + (v[0].as_f64().unwrap(), v[1].as_f64().unwrap()) +} + +fn close(got: f64, want: f64) -> bool { + (got - want).abs() <= 1e-9 * (1.0 + want.abs()) +} + +fn handle(v: &Value) -> Handle { + Handle::parse_hex(v.get("handle").unwrap_or(&v["handles"][0]).as_str().unwrap()).unwrap() +} + +fn lisp(s: &mut Session, expr: &str) { + s.cmdline(expr).unwrap(); + assert!(s.running.is_none(), "{expr}: {}", s.prompt_text()); +} + +/// The closed polyline with an arc and the circle the Add-mode scripts pick. +fn p_and_c(s: &mut Session) -> (Handle, Handle) { + let p = handle(&s.execute("pline", &json!({ "vertices": [[0, 0], { "p": [40, 0], "bulge": 0.5 }, [40, 30], [0, 30]], "closed": true })).unwrap()); + let c = handle(&s.execute("circle", &json!({ "center": [150, 15], "radius": 10 })).unwrap()); + (p, c) +} + +#[test] +fn area_of_points_sets_area_and_perimeter() { + let mut s = Session::new(); + lisp(&mut s, "(command \"_.AREA\" '(0.0 0.0) '(40.0 0.0) '(40.0 30.0) '(0.0 30.0) \"\")"); + assert!(close(var(&s, "AREA"), 1200.0) && close(var(&s, "PERIMETER"), 140.0)); +} + +#[test] +fn add_mode_totals_the_area_and_keeps_the_last_perimeter() { + // Points, then the circle as an object. + let mut s = Session::new(); + let (_, c) = p_and_c(&mut s); + lisp( + &mut s, + &format!( + "(command \"_.AREA\" \"_A\" '(0.0 0.0) '(40.0 0.0) '(40.0 30.0) '(0.0 30.0) \"\" \"_O\" (list (handent \"{}\") '(160.0 15.0)) \"\" \"\")", + c.hex() + ), + ); + assert!(close(var(&s, "AREA"), 1514.159265358979), "{}", var(&s, "AREA")); + assert!(close(var(&s, "PERIMETER"), 62.83185307179586), "{}", var(&s, "PERIMETER")); + // Both as objects. + let mut s = Session::new(); + let (p, c) = p_and_c(&mut s); + lisp( + &mut s, + &format!( + "(command \"_.AREA\" \"_A\" \"_O\" (list (handent \"{}\") '(0.0 15.0)) (list (handent \"{}\") '(160.0 15.0)) \"\" \"\")", + p.hex(), + c.hex() + ), + ); + assert!(close(var(&s, "AREA"), 1671.411490437671), "{}", var(&s, "AREA")); + assert!(close(var(&s, "PERIMETER"), 62.83185307179586), "{}", var(&s, "PERIMETER")); +} + +#[test] +fn subtract_mode_takes_the_area_off_the_total() { + // At large coordinates: the polyline added, the circle subtracted. + let mut s = Session::new(); + let p = handle( + &s.execute( + "pline", + &json!({ "vertices": [[-35000, 81000], { "p": [-33000, 81000], "bulge": 0.5 }, [-33000, 82500], [-35000, 82500]], "closed": true }), + ) + .unwrap(), + ); + let c = handle(&s.execute("circle", &json!({ "center": [-27000, 81750], "radius": 500 })).unwrap()); + lisp( + &mut s, + &format!( + "(command \"_.AREA\" \"_A\" \"_O\" (list (handent \"{}\") '(-35000.0 81750.0)) \"\" \"_S\" \"_O\" (list (handent \"{}\") '(-27000.0 81750.0)) \"\" \"\")", + p.hex(), + c.hex() + ), + ); + assert!(close(var(&s, "AREA"), 2607732.399299283), "{}", var(&s, "AREA")); + assert!(close(var(&s, "PERIMETER"), 3141.592653589793), "{}", var(&s, "PERIMETER")); +} + +#[test] +fn area_of_an_open_spline_closes_it_by_its_chord() { + let control = [(0.8, 0.25), (0.82, 0.24), (0.84, 0.25), (0.84, 0.28), (0.82, 0.29), (0.8, 0.28), (0.8, 0.25)]; + let spline = Spline { + degree: 3, + knots: vec![0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 4.0, 4.0, 4.0], + control: control.iter().map(|&(x, y)| Vec2::new(x, y)).collect(), + weights: Vec::new(), + fit: Vec::new(), + closed: false, + fit_opts: Default::default(), + }; + let mut s = Session::new(); + let space = s.space(); + let h = s.doc_mut().unwrap().add(&space, Common::default(), EntityKind::Spline(spline)).unwrap(); + lisp(&mut s, &format!("(command \"_.AREA\" \"_O\" (list (handent \"{}\") '(0.8 0.265)))", h.hex())); + assert!(close(var(&s, "AREA"), 0.001308333333333334), "{}", var(&s, "AREA")); + // The spline's length, 0.13046952991870 by an independent integration. AutoCAD reported + // 0.1304733129441191, 2.9e-5 longer than that: its own approximation of the length. + assert!(close(var(&s, "PERIMETER"), 0.1304695299187), "{}", var(&s, "PERIMETER")); +} + +#[test] +fn area_of_a_spline_counts_its_narrow_knot_spans() { + // A degree-1 spline is its control polygon, here the triangle (0,0) (1,0) (0,1): area 0.5, + // perimeter 2 + sqrt(2), though two of its three knot spans are a billionth of the domain. + let control = [(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (0.0, 0.0)]; + let spline = Spline { + degree: 1, + knots: vec![0.0, 0.0, 1e-9, 2e-9, 1.0, 1.0], + control: control.iter().map(|&(x, y)| Vec2::new(x, y)).collect(), + ..Spline::default() + }; + let mut s = Session::new(); + let space = s.space(); + let h = s.doc_mut().unwrap().add(&space, Common::default(), EntityKind::Spline(spline)).unwrap(); + lisp(&mut s, &format!("(command \"_.AREA\" \"_O\" (list (handent \"{}\") '(0.5 0.0)))", h.hex())); + assert!(close(var(&s, "AREA"), 0.5), "{}", var(&s, "AREA")); + assert!(close(var(&s, "PERIMETER"), 2.0 + 2f64.sqrt()), "{}", var(&s, "PERIMETER")); +} + +#[test] +fn area_of_a_spline_with_more_spans_than_samples_counts_each() { + // The square (0,0) (100,0) (100,100) (0,100) as a degree-1 spline: 5000 spans along the bottom, + // then two a millionth wide up the right side and across the top. Area 10000, perimeter (the + // open spline's length) 300. + let mut control: Vec = (0..=5000).map(|i| Vec2::new(i as f64 / 50.0, 0.0)).collect(); + control.extend([Vec2::new(100.0, 100.0), Vec2::new(0.0, 100.0)]); + let mut knots: Vec = (0..=5000).map(f64::from).collect(); + knots.insert(0, 0.0); + knots.extend([5000.0 + 1e-6, 5000.0 + 2e-6, 5000.0 + 2e-6]); + let spline = Spline { degree: 1, knots, control, ..Spline::default() }; + let mut s = Session::new(); + let space = s.space(); + let h = s.doc_mut().unwrap().add(&space, Common::default(), EntityKind::Spline(spline)).unwrap(); + lisp(&mut s, &format!("(command \"_.AREA\" \"_O\" (list (handent \"{}\") '(50.0 0.0)))", h.hex())); + assert!(close(var(&s, "AREA"), 1e4), "{}", var(&s, "AREA")); + assert!(close(var(&s, "PERIMETER"), 300.0), "{}", var(&s, "PERIMETER")); +} + +#[test] +fn a_spline_too_long_to_measure_is_refused_and_not_kept() { + let mut s = Session::new(); + lisp(&mut s, "(command \"_.AREA\" '(0.0 0.0) '(10.0 0.0) '(10.0 10.0) \"\")"); + // Finite ends 2e308 apart: its chord closes no area, but its length is past f64's range. That is + // too large to report, as a polyline's is, not a perimeter of 0. + let spline = Spline::from_line(Vec2::new(-1e308, 0.0), Vec2::new(1e308, 0.0), 1); + let space = s.space(); + let h = s.doc_mut().unwrap().add(&space, Common::default(), EntityKind::Spline(spline)).unwrap(); + let err = s.execute("area", &json!({ "handle": h.hex() })).unwrap_err(); + assert!(err.to_string().contains("the perimeter is too large"), "{err}"); + lisp(&mut s, &format!("(command \"_.AREA\" \"_O\" (list (handent \"{}\") '(0.0 0.0)))", h.hex())); + assert!(s.log.iter().any(|l| l.contains("The perimeter is too large.")), "{:?}", s.log); + assert!(close(var(&s, "AREA"), 50.0), "{}", var(&s, "AREA")); + assert!(close(var(&s, "PERIMETER"), 20.0 + 200f64.sqrt()), "{}", var(&s, "PERIMETER")); +} + +#[test] +fn dist_sets_the_distance_and_leaves_lastpoint_at_the_first_point() { + let mut s = Session::new(); + lisp(&mut s, "(command \"_.DIST\" '(0.5 0.25) '(0.53 0.29))"); + assert!(close(var(&s, "DISTANCE"), 0.05), "{}", var(&s, "DISTANCE")); + let (x, y) = lastpoint(&s); + assert!(close(x, 0.5) && close(y, 0.25), "{x}, {y}"); +} + +#[test] +fn dist_with_multiple_points_sets_the_total_and_lastpoint_at_the_last() { + let mut s = Session::new(); + lisp(&mut s, "(command \"_.DIST\" '(0.0 0.0) \"_M\" '(40.0 0.0) '(40.0 30.0) '(0.0 30.0) \"\")"); + assert!(close(var(&s, "DISTANCE"), 110.0), "{}", var(&s, "DISTANCE")); + let (x, y) = lastpoint(&s); + assert!(close(x, 0.0) && close(y, 30.0), "{x}, {y}"); +} + +#[test] +fn measuregeom_sets_them_as_dist_and_area_do() { + let mut s = Session::new(); + // (AutoCAD's MEASUREGEOM asks for the next option after each measurement until eXit; here it + // ends after one.) + lisp(&mut s, "(command \"_.MEASUREGEOM\" \"_D\" '(0.0 0.0) '(30.0 40.0))"); + assert!(close(var(&s, "DISTANCE"), 50.0), "{}", var(&s, "DISTANCE")); + lisp(&mut s, "(command \"_.MEASUREGEOM\" \"_AR\" '(0.5 0.25) '(0.54 0.25) '(0.54 0.28) '(0.5 0.28) \"\")"); + assert!(close(var(&s, "AREA"), 0.0012) && close(var(&s, "PERIMETER"), 0.14), "{} {}", var(&s, "AREA"), var(&s, "PERIMETER")); +} + +#[test] +fn the_json_forms_set_them_too_and_they_are_read_only() { + let mut s = Session::new(); + s.execute("area", &json!({ "points": [[0, 0], [40, 0], [40, 30], [0, 30]] })).unwrap(); + s.execute("dist", &json!({ "p1": [0, 0], "p2": [30, 40] })).unwrap(); + assert_eq!((var(&s, "AREA"), var(&s, "PERIMETER"), var(&s, "DISTANCE")), (1200.0, 140.0, 50.0)); + for name in ["AREA", "PERIMETER", "DISTANCE"] { + assert!(s.execute("setvar", &json!({ "name": name, "value": 1 })).is_err(), "{name}"); + } +} + +#[test] +fn lastpoint_after_multiple_points_is_the_last_point_entered() { + // A length typed with Length extends the path, but LASTPOINT stays at the last point picked; + // after Undo it stays at the point undone (AutoCAD 2024 leaves LASTPOINT at 10,0 and 10,10). + let mut s = Session::new(); + lisp(&mut s, "(command \"_.DIST\" '(0.0 0.0) \"_M\" '(10.0 0.0) \"_L\" 5.0 \"\")"); + assert!(close(var(&s, "DISTANCE"), 15.0), "{}", var(&s, "DISTANCE")); + let (x, y) = lastpoint(&s); + assert!(close(x, 10.0) && close(y, 0.0), "{x}, {y}"); + let mut s = Session::new(); + lisp(&mut s, "(command \"_.DIST\" '(0.0 0.0) \"_M\" '(10.0 0.0) '(10.0 10.0) \"_U\" \"_T\")"); + assert!(close(var(&s, "DISTANCE"), 10.0), "{}", var(&s, "DISTANCE")); + let (x, y) = lastpoint(&s); + assert!(close(x, 10.0) && close(y, 10.0), "{x}, {y}"); +} + +#[test] +fn a_distance_too_large_to_measure_is_refused_and_not_kept() { + let mut s = Session::new(); + lisp(&mut s, "(command \"_.DIST\" '(0.5 0.25) '(0.53 0.29))"); + assert!(close(var(&s, "DISTANCE"), 0.05)); + // Finite points whose distance overflows: the JSON form, two picked points, Multiple points. + assert!(s.execute("dist", &json!({ "p1": [-1e308, 0], "p2": [1e308, 0] })).is_err()); + s.cmdline("dist").unwrap(); + s.input(Input::Point(Vec2::new(-1e308, 0.0))).unwrap(); + s.input(Input::Point(Vec2::new(1e308, 0.0))).unwrap(); + assert!(s.log.last().is_some_and(|l| l.contains("the distance is too large")), "{:?}", s.log.last()); + // LASTPOINT stays at the first point, as after a measurement. + assert_eq!(lastpoint(&s), (-1e308, 0.0)); + s.cancel(); + s.cmdline("dist").unwrap(); + s.input(Input::Point(Vec2::new(-1e308, 0.0))).unwrap(); + s.cmdline("m").unwrap(); + s.input(Input::Point(Vec2::new(1e308, 0.0))).unwrap(); + s.input(Input::Enter).unwrap(); + assert!(s.log.last().is_some_and(|l| l.contains("the distance is too large")), "{:?}", s.log.last()); + s.cancel(); + assert!(close(var(&s, "DISTANCE"), 0.05), "{}", var(&s, "DISTANCE")); +} + +#[test] +fn the_variable_list_shows_the_sessions_values() { + // A drawing's header can carry these names; GETVAR reads the session's, and so does the list. + let mut s = Session::new(); + s.doc_mut().unwrap().header.set_f64("AREA", 99.0); + // Every DXF header carries ORTHOMODE; the session's ortho is off. + s.doc_mut().unwrap().header.set_i64("ORTHOMODE", 1); + lisp(&mut s, "(command \"_.AREA\" '(0.0 0.0) '(10.0 0.0) '(10.0 10.0) \"\")"); + let list = s.execute("sysvars", &json!({})).unwrap(); + assert!(close(var(&s, "AREA"), 50.0)); + assert_eq!(list["AREA"].as_f64(), Some(var(&s, "AREA")), "{}", list["AREA"]); + assert_eq!(list["ORTHOMODE"], sysvars::get(&s, "ORTHOMODE").unwrap()); + assert_eq!(list["ORTHOMODE"], 0); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index d3c8d030d..38fb4d005 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -60,6 +60,7 @@ mod hatch_numeric_validation; mod hatch_options; mod helix; mod inquiry_keywords; +mod inquiry_variables; mod inquiry_z; mod insert_attribute_prompts; mod insert_prompts; diff --git a/crates/geom/src/nurbs.rs b/crates/geom/src/nurbs.rs index 104ba8d3c..e07852340 100644 --- a/crates/geom/src/nurbs.rs +++ b/crates/geom/src/nurbs.rs @@ -131,6 +131,12 @@ impl Spline { /// [`Spline::is_valid`] already; integration calls this once per sample, and re-running the /// O(knots) validity scan each time would make measuring a valid spline quadratic (CWE-407). fn derivative_unchecked(&self, t: f64) -> Vec2 { + let d = self.velocity(t); + if d.is_finite() { d } else { Vec2::ZERO } + } + + /// [`Spline::derivative_unchecked`] before a value past f64's range is read as zero. + fn velocity(&self, t: f64) -> Vec2 { let p = self.degree; let Some(n) = self.control.len().checked_sub(1) else { return Vec2::ZERO }; let (lo, hi) = self.domain(); @@ -158,8 +164,7 @@ impl Spline { if w.abs() < 1e-300 { return Vec2::ZERO; } - let d = (da - a / w * dw) / w; - if d.is_finite() { d } else { Vec2::ZERO } + (da - a / w * dw) / w } /// Unit tangent at `t` in the direction of increasing parameter; a central difference stands @@ -477,22 +482,18 @@ impl Spline { out } - /// Arc length of one integration panel. The spline is validated once by the public callers - /// ([`Spline::length_between`]/[`Spline::length`]) before any panel is measured, so the speed - /// closure uses the unchecked derivative to avoid re-scanning the knots per sample. - fn panel_length(&self, a: f64, b: f64) -> f64 { - let speed = |t: f64| self.derivative_unchecked(t).len(); + /// Arc length of one integration panel, at the speed of `derivative`. The spline is validated + /// once by the callers ([`Spline::integrated_length`]/[`Spline::param_at_length`]) before any + /// panel is measured, so the derivative is an unchecked one, to avoid re-scanning the knots per + /// sample. + fn panel_length(&self, a: f64, b: f64, derivative: fn(&Spline, f64) -> Vec2) -> f64 { + let speed = |t: f64| derivative(self, t).len(); adaptive_gauss(a, b, gauss(a, b, speed), &speed, MAX_GAUSS_DEPTH) } /// Arc length between parameters `a` and `b` (adaptive Gauss–Legendre over each knot span). pub fn length_between(&self, a: f64, b: f64) -> f64 { - if !(self.is_valid() && a.is_finite() && b.is_finite()) { - return 0.0; - } - let (lo, hi) = self.domain(); - let (a, b) = (a.min(b).clamp(lo, hi), a.max(b).clamp(lo, hi)); - let l: f64 = self.length_intervals(a, b).into_iter().map(|(x, y)| self.panel_length(x, y)).sum(); + let l = self.integrated_length(a, b); if l.is_finite() { l } else { 0.0 } } @@ -502,6 +503,25 @@ impl Spline { self.length_between(lo, hi) } + /// Arc length of the whole spline, or `None` when it is past f64's range, where + /// [`Spline::length`] reads 0. An invalid spline's is 0. + pub fn checked_length(&self) -> Option { + let (lo, hi) = self.domain(); + let l = self.integrated_length(lo, hi); + l.is_finite().then_some(l) + } + + /// Arc length between `a` and `b`, infinite or NaN when it overflows (the length, or the speed + /// along the way); 0 for an invalid spline or a non-finite parameter. + fn integrated_length(&self, a: f64, b: f64) -> f64 { + if !(self.is_valid() && a.is_finite() && b.is_finite()) { + return 0.0; + } + let (lo, hi) = self.domain(); + let (a, b) = (a.min(b).clamp(lo, hi), a.max(b).clamp(lo, hi)); + self.length_intervals(a, b).into_iter().map(|(x, y)| self.panel_length(x, y, Spline::velocity)).sum() + } + /// The parameter `len` along the curve from parameter `from` (forward for positive `len`, /// backward for negative), clamped to the domain. pub fn param_at_length(&self, from: f64, len: f64) -> f64 { @@ -521,7 +541,7 @@ impl Spline { } let mut acc = 0.0; for (x0, x1) in ivs { - let l = self.panel_length(x0, x1); + let l = self.panel_length(x0, x1, Spline::derivative_unchecked); if acc + l >= target && l > 0.0 { // Bisect inside this interval, measuring from its near end. let rest = target - acc; @@ -529,7 +549,7 @@ impl Spline { let (mut s0, mut s1) = (near, far); for _ in 0..60 { let m = (s0 + s1) / 2.0; - let lm = self.panel_length(near.min(m), near.max(m)); + let lm = self.panel_length(near.min(m), near.max(m), Spline::derivative_unchecked); if lm < rest { s0 = m; } else { @@ -735,6 +755,24 @@ mod tests { assert_eq!(q.param_at_length(lo, 1e9), hi); } + #[test] + fn checked_length_tells_an_overflow_from_a_zero_length() { + let l = Spline::from_line(Vec2::ZERO, Vec2::new(3.0, 4.0), 3); + assert_eq!(l.checked_length(), Some(l.length())); + let dot = Spline::from_line(Vec2::new(1.0, 1.0), Vec2::new(1.0, 1.0), 1); + assert_eq!((dot.length(), dot.checked_length()), (0.0, Some(0.0))); + // Finite ends 2e308 apart: over a knot span of 4 the speed is in f64's range and the length + // past it; over a span of 1 the speed is past it too. `length` reads both as 0. + let ends = vec![Vec2::new(-1e308, 0.0), Vec2::new(1e308, 0.0)]; + for span in [4.0, 1.0] { + let huge = Spline { degree: 1, knots: vec![0.0, 0.0, span, span], control: ends.clone(), ..Spline::default() }; + assert_eq!((huge.length(), huge.checked_length()), (0.0, None), "{span}"); + } + let mut invalid = l.clone(); + invalid.knots.reverse(); + assert_eq!(invalid.checked_length(), Some(0.0)); + } + #[test] fn spline_arc_length_on_a_tight_bend() { // One Bezier span that turns sharply: four fixed Gauss panels come out 0.3% short, and diff --git a/crates/geom/src/spline.rs b/crates/geom/src/spline.rs index d954e66b1..1d5922d37 100644 --- a/crates/geom/src/spline.rs +++ b/crates/geom/src/spline.rs @@ -205,10 +205,69 @@ impl Spline { out } + /// Points along the curve for measuring it (AREA). Each knot span has a segment, so a narrow + /// span, which one grid over the whole domain can step over entirely, is measured too. The + /// count [`Spline::tessellate`] takes at `tol` is then shared out among the spans that bend + /// ([`Spline::span_bend`]), by the cube root of their bend: a span's chord error falls with + /// the square of its segments, so that share keeps the total error least. A straight span + /// keeps its one segment, which measures it exactly. The points number one per span plus + /// that count, past `MAX_SAMPLES` when the spans are that many, as [`Spline::length`] + /// integrates every span. An invalid spline gives its control points, as `tessellate` does. + pub fn tessellate_spans(&self, tol: f64) -> Vec { + if !self.is_valid() { + return self.control.clone(); + } + let (lo, hi) = self.domain(); + let n = self.sample_count(tol); + let spans: Vec<(f64, f64, f64)> = self.indexed_knot_spans().map(|(j, a, b)| (a, b, self.span_bend(j).cbrt())).collect(); + if spans.is_empty() { + return self.samples(n).collect(); + } + let total: f64 = spans.iter().map(|s| s.2).sum(); + let shared = total.is_finite() && total > 0.0; + let mut out = Vec::with_capacity(spans.len() + n + 1); + let (mut acc, mut used) = (0.0, 0); + for (a, b, share) in spans { + // Rounded on the running total, so the extra samples add up to `n` however thinly the + // shares are spread; the fraction first, at most 1, so a huge bend can't overflow. + acc += share; + let upto = if shared { (n as f64 * (acc / total).min(1.0)) as usize } else { 0 }; + let seg = 1 + upto.saturating_sub(used); + used = used.max(upto); + out.extend((0..seg).map(|i| self.eval_valid(a + (b - a) * i as f64 / seg as f64, lo, hi))); + } + out.push(self.eval_valid(hi, lo, hi)); + out + } + + /// How far knot span `j` (as [`Spline::indexed_knot_spans`] numbers it) can stray from its + /// chord, as an area: twice the largest triangle its first control point, the one farthest + /// from it and another make. That is between a quarter of the area of their convex hull and + /// twice it. With positive weights the span and its chord lie in that hull, so the area + /// between them is no more; it is zero when the points are collinear, and then the span is + /// straight. A weight that isn't positive lets the span leave the hull, so the square of the + /// points' polygon length counts instead. + fn span_bend(&self, j: usize) -> f64 { + let Some(pts) = self.control.get(j..=j + self.degree) else { return 0.0 }; + let Some(&a) = pts.first() else { return 0.0 }; + if !self.weights.get(j..=j + self.degree).is_none_or(|w| w.iter().all(|w| *w > 0.0)) { + let len: f64 = pts.windows(2).filter_map(|w| w.first().zip(w.get(1)).map(|(p, q)| p.dist(*q))).sum(); + return len * len; + } + let far = pts.iter().copied().max_by(|p, q| a.dist(*p).total_cmp(&a.dist(*q))).unwrap_or(a); + pts.iter().map(|p| (*p - a).cross(far - a).abs()).fold(0.0, f64::max) + } + /// The non-empty knot spans the curve is defined over, in parameter order. fn knot_spans(&self) -> impl Iterator + '_ { + self.indexed_knot_spans().map(|(_, a, b)| (a, b)) + } + + /// The non-empty knot spans with the index of the first of the `degree + 1` control points + /// that shape each. + fn indexed_knot_spans(&self) -> impl Iterator + '_ { let knots = self.knots.get(self.degree..=self.control.len()).unwrap_or(&[]); - knots.windows(2).filter_map(|w| w.first().zip(w.get(1)).map(|(a, b)| (*a, *b)).filter(|(a, b)| b > a)) + knots.windows(2).enumerate().filter_map(|(j, w)| w.first().zip(w.get(1)).map(|(a, b)| (j, *a, *b)).filter(|(_, a, b)| b > a)) } } diff --git a/crates/geom/src/spline/tests.rs b/crates/geom/src/spline/tests.rs index d953cfabd..820490d89 100644 --- a/crates/geom/src/spline/tests.rs +++ b/crates/geom/src/spline/tests.rs @@ -202,6 +202,83 @@ fn bounds_keep_their_cap_over_huge_knot_spans() { } } +#[test] +fn tessellate_spans_measures_narrow_knot_spans() { + // A degree-1 spline is its control polygon: the triangle (0,0) (1,0) (0,1), though two of its + // three spans are a billionth of the domain. One grid over the domain steps over them and + // sees no area; spread over the spans, the corners are there. + let control = vec![Vec2::ZERO, Vec2::new(1.0, 0.0), Vec2::new(0.0, 1.0), Vec2::ZERO]; + let s = Spline { degree: 1, knots: vec![0.0, 0.0, 1e-9, 2e-9, 1.0, 1.0], control, ..Spline::default() }; + assert!(s.is_valid()); + assert!(crate::shoelace(&s.tessellate(1e-3)).abs() < 1e-6); + let pts = s.tessellate_spans(1e-3); + assert!(pts.iter().any(|p| p.near(Vec2::new(1.0, 0.0), 1e-12)), "{pts:?}"); + assert!((crate::shoelace(&pts).abs() - 0.5).abs() < 1e-12); + // An ordinary spline measures as `tessellate` does, within the sampling's sag. + let w = wiggly(false); + let (a, b) = (crate::shoelace(&w.tessellate(1e-4)), crate::shoelace(&w.tessellate_spans(1e-4))); + assert!((a - b).abs() <= 1e-4 * a.abs().max(1.0), "{a} {b}"); +} + +/// The square (0,0) (100,0) (100,100) (0,100) as a degree-1 spline: `edge` spans of width 1 +/// along the bottom, then two spans a millionth wide up the right side and across the top. +fn square_with_narrow_spans(edge: usize) -> Spline { + let mut control: Vec = (0..=edge).map(|i| Vec2::new(100.0 * i as f64 / edge as f64, 0.0)).collect(); + control.extend([Vec2::new(100.0, 100.0), Vec2::new(0.0, 100.0)]); + let end = edge as f64; + let mut knots: Vec = (0..=edge).map(|i| i as f64).collect(); + knots.insert(0, 0.0); + knots.extend([end + 1e-6, end + 2e-6, end + 2e-6]); + Spline { degree: 1, knots, control, ..Spline::default() } +} + +#[test] +fn tessellate_spans_covers_every_span_however_many() { + // A degree-1 spline's spans are straight: one segment each, its control points, however many + // there are. The narrow spans are measured, not stepped over by a grid. + for edge in [3000, MAX_SAMPLES + 500] { + let s = square_with_narrow_spans(edge); + assert!(s.is_valid()); + let pts = s.tessellate_spans(1e-9); + assert_eq!(pts.len(), s.control.len()); + assert!(pts.iter().zip(&s.control).all(|(p, c)| p.near(*c, 1e-9))); + assert!((crate::shoelace(&pts).abs() - 1e4).abs() < 1e-6, "{}", crate::shoelace(&pts)); + } + let s = square_with_narrow_spans(MAX_SAMPLES + 500); + assert!(crate::shoelace(&s.samples(MAX_SAMPLES).collect::>()).abs() < 0.6e4); +} + +#[test] +fn tessellate_spans_samples_where_the_curve_bends() { + // A quadratic spline along the x-axis but for one control point 1000 above it: three spans + // bend, and the area under them is 1750/3 (a fine grid gives 583.3333334). Their chords alone + // would give 750. Below the sample cap and past it, the straight spans keep one segment each + // (their samples are the only ones on the axis but the curve's end) and the bent spans take + // all the rest. + for edge in [40, MAX_SAMPLES + 500] { + let mut control: Vec = (0..=edge).map(|i| Vec2::new(i as f64, 0.0)).collect(); + control.extend([Vec2::new(edge as f64 + 0.5, 1000.0), Vec2::new(edge as f64 + 1.0, 0.0), Vec2::new(edge as f64 + 2.0, 0.0)]); + let s = Spline::from_control(control, 2); + let pts = s.tessellate_spans(1e-9); + let area = crate::shoelace(&pts).abs(); + assert!((area - 1750.0 / 3.0).abs() < 1e-6 * area, "{edge}: {area}"); + assert_eq!(pts.iter().filter(|p| p.y == 0.0).count(), edge + 1, "{edge}"); + assert_eq!(pts.len(), s.control.len() - 2 + s.sample_count(1e-9) + 1, "{edge}"); + } +} + +#[test] +fn span_bend_bounds_the_area_off_the_chord() { + let mut s = Spline::from_control(vec![Vec2::ZERO, Vec2::new(1.0, 0.0), Vec2::new(3.0, 0.0), Vec2::new(4.0, 2.0)], 2); + // Collinear control points: a straight span. + assert_eq!(s.span_bend(0), 0.0); + // (1,0) (3,0) (4,2): twice their triangle's area. + assert!((s.span_bend(1) - 4.0).abs() < 1e-12, "{}", s.span_bend(1)); + // A weight that isn't positive: the polygon's length squared. + s.weights = vec![1.0, -0.5, 1.0, 1.0]; + assert!((s.span_bend(0) - 9.0).abs() < 1e-12, "{}", s.span_bend(0)); +} + fn scaled(s: &Spline, k: f64) -> Spline { Spline { control: s.control.iter().map(|c| *c * k).collect(), fit: s.fit.iter().map(|c| *c * k).collect(), ..s.clone() } } From 6660d887855f872314151a61c6898a321b3e8de7 Mon Sep 17 00:00:00 2001 From: RicSchonfelder Date: Mon, 2 Nov 2026 18:36:48 -0300 Subject: [PATCH 067/100] i18n(pt-BR): translate the rows the sibling catalogs cover (#2581) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * i18n(pt-BR): translate the rows the sibling catalogs cover The Brazilian Portuguese catalog covered the reference catalog and every marked UI literal, but 191 strings that the German, French, Swedish and Ukrainian catalogs already translate still fell back to English: the Hatch and Gradient dialog, the Block Editor and dynamic-block grips, the plot and drawing units dialogs, multileader editing, selection preview settings and the unit names. Rows follow the catalog's vocabulary (dimension = cota, hatch = hachura, grip = pinça, layout = apresentação, multileader = multilinha, xref = referência externa), keep the prompt keywords, SPDX tokens and technology names in English, and are appended after the existing entries. * docs: leave localization parity updates to maintainers --- crates/ui-egui/src/i18n/pt_br.tsv | 197 ++++++++++++++++++++++++++++++ docs/localization.md | 4 +- 2 files changed, 199 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/i18n/pt_br.tsv b/crates/ui-egui/src/i18n/pt_br.tsv index a000d9b09..3d8f03449 100644 --- a/crates/ui-egui/src/i18n/pt_br.tsv +++ b/crates/ui-egui/src/i18n/pt_br.tsv @@ -1253,3 +1253,200 @@ Check for Updates… Verificar atualizações… CADCraft is up to date. O CADCraft está atualizado. Could not check for updates. Não foi possível verificar atualizações. +# Strings the sibling catalogs translate that pt-BR did not yet cover (translated 2026-10-31). + Add: Pick points Adicionar: indicar pontos + Add: Select objects Adicionar: selecionar objetos + Add… Adicionar… + Alias Alias + Alias file: {file} Arquivo de aliases: {file} + Align Multileaders Alinhar multilinhas + Analyze Usage Analisar uso + Angstroms Angströms + Apply to Layout Aplicar à apresentação + Architectural Arquitetônico + Arrow size Tamanho da seta + Associate Associar + Associative Associativo + Astronomical Units Unidades astronômicas + AutoLISP (LSP) AutoLISP (LSP) + Backup copy Cópia de backup + Block to create or edit Bloco a criar ou editar + Bottom left Inferior esquerdo + Bottom right Inferior direito + Boundaries Contornos + Boundaries picked: {n} Contornos selecionados: {n} + Break-line Symbol Símbolo de linha de quebra + built-in integrado + Center the plot Centralizar a plotagem + Centered Centralizado + Centimeters Centímetros + Change Properties Alterar propriedades + Choose the command the alias starts. Escolha o comando que o alias inicia. + Clear Recent Files Limpar arquivos recentes + Click the ruler to add a tab stop, right-click to remove one Clique na régua para adicionar uma parada de tabulação; clique com o botão direito para remover + Click to set new origin Clique para definir a nova origem + Close Block Editor Fechar o editor de blocos + Collect Coletar + Collect Multileaders Coletar multilinhas + Color 1 Cor 1 + Color 2 Cor 2 + Command Comando + contains authoring elements. Open in Block Editor? contém elementos de autoria. Abrir no editor de blocos? + Convert Text to Mtext Converter texto em Mtexto + Create backup copy with each save Criar cópia de backup a cada salvamento + Create separate hatches Criar hachuras separadas + Crossing selection color: Cor da seleção por cruzamento: + Curved Curvo + Custom Personalizado + Customize Personalizar + Decameters Decâmetros + Decimal Decimal + Decimal Degrees Graus decimais + Decimeters Decímetros + Default to boundary extents Padrão para a extensão do contorno + Deg/Min/Sec Gra/Min/Seg + Dim scale overall Escala geral de cotas + Discard Descartar + Don't Save Não salvar + Drag Dynamic Grip Arrastar pinça dinâmica + Drawing Units Unidades do desenho + Dynamic Block Data Dados de bloco dinâmico + Dynamic Block Grips Pinças de bloco dinâmico + Dynamic Block Reference Referência de bloco dinâmico + Edit Block Definition Editar definição de bloco + Edit Command Aliases (PGP)... Editar aliases de comandos (PGP)... + Edit Multileader Editar multilinha + Edit… Editar… + Engineering Engenharia + Enter block names to extract Insira os nomes dos blocos a extrair + Enter geometric characteristic Insira a característica geométrica + Enter name of LISP file to load Insira o nome do arquivo LISP a carregar + Evaluate LISP Avaliar LISP + Exclude: Excluir: + Explode Attributes to Text Explodir atributos em texto + Express Tools Ferramentas Express + Feet Pés + First corner Primeiro canto + Fit to paper Ajustar ao papel + Font Fonte + Fractional Fracionário + Gigameters Gigâmetros + Grads Grados + Grips Pinças + Groups Grupos + Hatch and Gradient Hachura e gradiente + Hatch origin Origem da hachura + Hatch Pick Seleção de hachura + Hatches Hachuras + Hectometers Hectômetros + Hemispherical Hemisférico + Ignore Ignorar + Imperial (inches) Imperial (polegadas) + Implied Loops Laços implícitos + Inches Polegadas + Indicate selection area Indicar área de seleção + Insertion scale Escala de inserção + Interface scale Escala da interface + Inverted Invertido + Island Ilha + Island detection Detecção de ilhas + Italic Itálico + Justify Justificar + Kilometers Quilômetros + Landscape Paisagem + Layouts Apresentações + Light Years Anos-luz + Limits Limites + Load Application... Carregar aplicativo... + Meters Metros + Metric (millimetres) Métrico (milímetros) + Microinches Micropolegadas + Microns Mícrons + Miles Milhas + Millimeters Milímetros + Mils Mils + More than {n} aliases. Mais de {n} aliases. + Move/Copy/Rotate Mover/Copiar/Girar + Multiline text Texto multilinha + Nanometers Nanômetros + New and the first drawing at startup use these units unless a default template is set (QNEWTEMPLATE). Novos desenhos e o primeiro desenho na inicialização usam estas unidades, a menos que um modelo padrão esteja definido (QNEWTEMPLATE). + New block Novo bloco + No alias file loaded. REINIT replaces these aliases with a file's. Nenhum arquivo de aliases carregado. O REINIT substitui estes aliases pelos de um arquivo. + Object grouping Agrupamento de objetos + Object selection limit for display of grips Limite de seleção de objetos para exibir pinças + Objects on locked layers Objetos em camadas bloqueadas + Open in Block Editor Abrir no editor de blocos + Open Recent Abrir recentes + Opposite corner Canto oposto + Orientation Orientação + Origin: {x}, {y} Origem: {x}, {y} + Outer Externo + Overline Sobrelinha + overrides built-in substitui o integrado + Paper size Tamanho do papel + Parsecs Parsecs + Plot area Área de plotagem + Plot object lineweights Plotar espessuras de linha dos objetos + Plot offset Deslocamento da plotagem + Plot transparency Plotar transparência + Portrait Retrato + Precision Precisão + Preview Visualizar + Printer/plotter Impressora/plotter + Radians Radianos + Sample output Amostra de saída + Save alias Salvar alias + Save Block Salvar bloco + Save Block As Salvar bloco como + Save changes to block "{name}"? Salvar alterações no bloco "{name}"? + Save changes to {name}? Salvar alterações em {name}? + Save Layout as Template Salvar apresentação como modelo + Scientific Científico + Select boundaries to remove Selecione contornos para remover + Selection Area Effect Efeito da área de seleção + Selection area opacity: Opacidade da área de seleção: + Selection effect color Cor do efeito de seleção + Selection modes Modos de seleção + Selection preview Visualização da seleção + Selection Preview Filter Filtro de visualização da seleção + Set Dynamic Property Definir propriedade dinâmica + Shape Forma + Sheet Books to PDF... Livros de folhas em PDF... + Show grips Mostrar pinças + Specified origin Origem especificada + Spherical Esférico + Surveyor's Units Unidades de topografia + Tables Tabelas + Text color Cor do texto + This drawing Este desenho + Top left Superior esquerdo + Top right Superior direito + Type an alias name. Digite um nome de alias. + Underline Sublinhado + Unitless Adimensional + Units of new drawings Unidades de novos desenhos + Units to scale inserted content Unidades para dimensionar conteúdo inserido + Unsaved changes Alterações não salvas + US Survey Feet Pés topográficos norte-americanos + US Survey Inch Polegada topográfica norte-americana + US Survey Mile Milha topográfica norte-americana + US Survey Yard Jarda topográfica norte-americana + Use current origin Usar origem atual + Visual Effect Settings Configurações de efeitos visuais + Visual Effect Settings... Configurações de efeitos visuais... + When a command is active Quando um comando está ativo + When no command is active Quando nenhum comando está ativo + Window < Janela < + Window selection color: Cor da seleção por janela: + X offset (mm) Deslocamento X (mm) + Xrefs Referências externas + Y offset (mm) Deslocamento Y (mm) + Yards Jardas + Your changes will be lost if you don't save them. Suas alterações serão perdidas se você não salvá-las. + {alias} is already defined; select it and use Edit. {alias} já está definido; selecione-o e use Editar. + {command} is an unknown command. {command} é um comando desconhecido. +# Strings the sibling catalogs translate that pt-BR did not yet cover (translated 2026-10-31). +menu Dimension Cota +menu Draw Desenho +menu Modify Modificar +menu View Exibir diff --git a/docs/localization.md b/docs/localization.md index 909fad9f8..998fbe0a0 100644 --- a/docs/localization.md +++ b/docs/localization.md @@ -46,8 +46,8 @@ The translations are original, AI-assisted Korean CAD terminology (for example, 치수, 모깎기, 모따기, 간격띄우기 and 객체 스냅). Independent native proofreading and platform-specific layout/IME validation remain follow-up work. -Brazilian Portuguese has 1,231 catalog rows and also covers the reference catalog and every marked UI -literal: commands, menus, tool groups and tooltips, object snaps, drafting prompts, dialogs and +Brazilian Portuguese has 1,447 catalog rows and also covers the reference catalog, every marked UI +literal and the strings the sibling catalogs translate: commands, menus, tool groups and tooltips, object snaps, drafting prompts, dialogs and Preferences; its gaps are now zero. Its plural rule is `one|other` (0 and 1 are `one`), as CLDR defines for `pt`, and the catalog's accented letters (`Á Â Í Ó Ú à á â ã ç é ê í ó ô õ ú`) render in the bundled fonts. The original catalog was contributed by @WladmirJunior; the remaining rows were completed From 7bc76f94ca080275eb866567e45f3110a6525c6c Mon Sep 17 00:00:00 2001 From: Tyler Chiriboga Date: Mon, 2 Nov 2026 15:36:53 -0600 Subject: [PATCH 068/100] OVERKILL: objects within the tolerance are duplicates wherever their numbers fall (#2653) * OVERKILL: objects within the tolerance are duplicates wherever their numbers fall OVERKILL compared objects by rounding every number to a multiple of the tolerance. Two numbers closer than the tolerance on either side of a rounding step round apart, so the objects were kept: at 0.01, circles at x = 80.001 and 80.003 were merged but circles at 50.004 and 50.006 were not. Arcs on one circle were bucketed the same way. Duplicates are now found by shape (the JSON without its floating-point numbers; counts and flags must match exactly) and a weighted sum of the numbers on a grid of the tolerance, looking in the neighbouring cells too, then compared pairwise to within the tolerance. Arcs and circles look in the neighbouring cells of centre and radius and are grouped with a union-find. Lines were already grouped by sorting and are unchanged. Co-Authored-By: Claude Opus 5.5 * OVERKILL: within the tolerance doesn't chain Arcs and circles were grouped with a union-find over pairwise matches, so A ~ B and B ~ C put A and C in one group even when A and C are farther apart than the tolerance: a circle C was deleted for matching only the arc between it and the kept circle A, and arcs were combined across the chain. Each object now joins the first group whose every member is within the tolerance of it, or starts its own. In the duplicates pass, a followed object displaced an earlier keeper that later objects had already matched, leaving them deleted up to twice the tolerance from what stays. Followed objects are now looked at first, so every deletion is within the tolerance of an object that stays. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Tyler Chiriboga <18270301+Tyler-Chiriboga@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/overkill.rs | 142 +++++++++++++++++++++-------- crates/engine/tests/it/overkill.rs | 86 +++++++++++++++++ 2 files changed, 190 insertions(+), 38 deletions(-) diff --git a/crates/engine/src/cmd/overkill.rs b/crates/engine/src/cmd/overkill.rs index abfdff2eb..632001461 100644 --- a/crates/engine/src/cmd/overkill.rs +++ b/crates/engine/src/cmd/overkill.rs @@ -146,20 +146,49 @@ impl Opts { } } -/// Round every number of a JSON value to a multiple of `q`. -fn quantize(v: &mut Value, q: f64) { +/// Take the measurements (floating-point numbers) out of a JSON value, in order, leaving a marker +/// in their place: two objects are equal when what is left matches, counts and flags included, +/// and each pair of measurements is within the tolerance. +fn split_numbers(v: &mut Value, nums: &mut Vec) { match v { Value::Number(n) => { - if let Some(x) = n.as_f64() { - *v = json!((x / q).round()); + if n.is_f64() + && let Some(x) = n.as_f64() + { + nums.push(x); + *v = Value::String(NUMBER.into()); } } - Value::Array(a) => a.iter_mut().for_each(|x| quantize(x, q)), - Value::Object(o) => o.values_mut().for_each(|x| quantize(x, q)), + Value::Array(a) => a.iter_mut().for_each(|x| split_numbers(x, nums)), + Value::Object(o) => o.values_mut().for_each(|x| split_numbers(x, nums)), _ => {} } } +/// What [`split_numbers`] leaves where a number was. +const NUMBER: &str = "\u{0}#"; + +/// Whether two lists of numbers match pairwise to within `tol`. +fn within(a: &[f64], b: &[f64], tol: f64) -> bool { + a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() <= tol) +} + +/// The numbers summed with weights in [1, 2) that no simple pattern shares (golden-ratio steps), +/// so points on a diagonal of a grid don't all get the same sum; and the sum of the weights. +fn weighted_sum(nums: &[f64]) -> (f64, f64) { + nums.iter().enumerate().fold((0.0, 0.0), |(sum, total), (j, x)| { + let w = 1.0 + (j as f64 * 0.618_033_988_749_895).fract(); + (sum + w * x, total + w) + }) +} + +/// The cell of `x` on a grid of `step`. Values within one step of each other land in the same or +/// neighbouring cells; values too large for the grid share the outermost ones. +fn cell_of(x: f64, step: f64) -> i64 { + let k = (x / step).floor(); + if k.is_finite() { k.clamp(-9e15, 9e15) as i64 } else { i64::MIN } +} + #[derive(Default)] struct Report { duplicates: usize, @@ -234,33 +263,37 @@ fn overkill(s: &mut Session, hs: Option>, o: &Opts) -> Result = HashMap::new(); - for (i, c) in cands.iter().enumerate() { + // Keepers so far: (properties, shape) → cell of the sum → their numbers. + type Keepers = HashMap>>; + let mut seen: HashMap<(u32, String), Keepers> = HashMap::new(); + // Followed objects first, so a keeper is never displaced: everything deleted is within the + // tolerance of an object that stays. + let (followed, rest): (Vec, Vec) = (0..cands.len()).partition(|i| cands.get(*i).is_some_and(|c| c.protected)); + for i in followed.into_iter().chain(rest) { + let Some(c) = cands.get(i) else { continue }; let Ok(mut v) = serde_json::to_value(&c.e.kind) else { continue }; - if o.tol > 0.0 { - quantize(&mut v, o.q()); + let mut nums = Vec::new(); + split_numbers(&mut v, &mut nums); + let (sum, weights) = weighted_sum(&nums); + let cell = cell_of(sum, o.q() * weights.max(1.0)); + let cells = seen.entry((c.prop, v.to_string())).or_default(); + let found = [cell.saturating_sub(1), cell, cell.saturating_add(1)] + .into_iter() + .any(|k| cells.get(&k).is_some_and(|keepers| keepers.iter().any(|n| within(n, &nums, o.tol)))); + if !found || c.protected { + // A followed object stays even when it matches another. + cells.entry(cell).or_default().push(nums); + continue; } - let key = (c.prop, v.to_string()); - match seen.get(&key).copied() { - None => { - seen.insert(key, i); - } - Some(k) => { - let keeper_protected = cands.get(k).is_some_and(|x| x.protected); - let victim = match (keeper_protected, c.protected) { - (_, false) => i, - (false, true) => { - seen.insert(key, i); - k - } - (true, true) => continue, - }; - if let Some(g) = gone.get_mut(victim) { - *g = true; - rep.duplicates += 1; - } - } + if let Some(g) = gone.get_mut(i) { + *g = true; + rep.duplicates += 1; } } } @@ -421,8 +454,8 @@ fn collinear(members: &[&LineRec], cands: &[Cand], o: &Opts, gone: &mut [bool], fn arcs(cands: &[Cand], o: &Opts, gone: &mut [bool], changed: &mut HashMap, rep: &mut Report) { let q = o.q(); let g = q.max(1e-9); - let cell = |x: f64| (x / g).round() as i64; - let mut buckets: HashMap<(u32, i64, i64, i64, i64), Vec> = HashMap::new(); + // (index, properties, centre and radius); cells of `g` hold them. + let mut recs: Vec<(usize, u32, [f64; 4])> = Vec::new(); for (i, c) in cands.iter().enumerate() { if gone.get(i).copied().unwrap_or(true) || c.protected { continue; @@ -442,12 +475,45 @@ fn arcs(cands: &[Cand], o: &Opts, gone: &mut [bool], changed: &mut HashMap = buckets.keys().copied().collect(); - keys.sort_unstable(); - for key in keys { - let Some(group) = buckets.get(&key) else { continue }; + // Those within `g` of each other in centre and radius are on one circle. Each object joins the + // first group whose every member is within `g` of it, or starts one: matches don't chain, so + // nothing is deleted for matching only an object that itself matches the keeper. Members are + // looked for in the neighbouring cells too, so values on either side of a cell boundary meet. + let mut grid: HashMap<(u32, [i64; 4]), Vec> = HashMap::new(); + let mut group_of: Vec = Vec::with_capacity(recs.len()); + // Each group's records, in draw order; groups in the order of their first object. + let mut members: Vec> = Vec::new(); + for (ri, (_, prop, v)) in recs.iter().enumerate() { + let cells = v.map(|x| cell_of(x, g)); + let mut near_groups: Vec = Vec::new(); + for n in 0..81_i64 { + let near = [n % 3 - 1, n / 3 % 3 - 1, n / 9 % 3 - 1, n / 27 % 3 - 1]; + let mut key = cells; + for (k, d) in key.iter_mut().zip(near) { + *k = k.saturating_add(d); + } + near_groups.extend(grid.get(&(*prop, key)).map(Vec::as_slice).unwrap_or_default().iter().filter_map(|rj| group_of.get(*rj))); + } + near_groups.sort_unstable(); + near_groups.dedup(); + let fits = |gi: &usize| members.get(*gi).is_some_and(|m| m.iter().all(|rk| recs.get(*rk).is_some_and(|(_, _, w)| within(v, w, g)))); + let gi = match near_groups.into_iter().find(fits) { + Some(gi) => gi, + None => { + members.push(Vec::new()); + members.len() - 1 + } + }; + if let Some(m) = members.get_mut(gi) { + m.push(ri); + } + group_of.push(gi); + grid.entry((*prop, cells)).or_default().push(ri); + } + let groups = members.iter().map(|m| m.iter().filter_map(|ri| recs.get(*ri).map(|r| r.0)).collect::>()); + for group in groups { if group.len() < 2 { continue; } @@ -465,7 +531,7 @@ fn arcs(cands: &[Cand], o: &Opts, gone: &mut [bool], changed: &mut HashMap>(); + let mut s = Session::new(); + circle(&mut s, 50.0); + arc_at(&mut s, 50.008, 0.0, 90.0); + circle(&mut s, 50.016); + let r = ok(&mut s, json!({ "tolerance": 0.01 })); + assert_eq!((r["duplicates"].as_u64(), r["overlapping"].as_u64()), (Some(0), Some(1)), "{r}"); + assert_eq!(kinds(&s), ["Circle", "Circle"]); + let mut s = Session::new(); + arc_at(&mut s, 50.008, 0.0, 90.0); + circle(&mut s, 50.0); + circle(&mut s, 50.016); + let r = ok(&mut s, json!({ "tolerance": 0.01 })); + assert_eq!(r["deleted"], 1, "{r}"); + assert_eq!(kinds(&s), ["Circle", "Circle"]); + // Arcs alone: the third is 0.016 from the first, so it is not combined with it. + let mut s = Session::new(); + arc_at(&mut s, 0.0, 0.0, 90.0); + arc_at(&mut s, 0.008, 45.0, 180.0); + arc_at(&mut s, 0.016, 100.0, 200.0); + let r = ok(&mut s, json!({ "tolerance": 0.01 })); + assert_eq!(r["overlapping"], 1, "{r}"); + assert_eq!(kinds(&s), ["Arc", "Arc"]); + // A followed line displaces the first one, which a third line matched: the third is 0.016 from + // the followed line, so it stays. + let mut s = Session::new(); + let first = line(&mut s, [0.0, 0.0], [10.0, 0.0]); + let third = line(&mut s, [0.0, 0.008], [10.0, 0.008]); + let followed = line(&mut s, [0.0, -0.008], [10.0, -0.008]); + s.execute("dimlinear", &json!({ "object": followed.hex(), "at": [5, 5] })).unwrap(); + let r = ok(&mut s, json!({ "tolerance": 0.01 })); + assert_eq!(r["deleted"], 1, "{r}"); + let d = s.doc().unwrap(); + assert!(d.entity(first).is_none() && d.entity(third).is_some() && d.entity(followed).is_some()); +} From 7690d36bb0aa43c73d266c56af828e985f2afb10 Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 14:31:06 -0700 Subject: [PATCH 069/100] fmt --- crates/engine/tests/save_reports_not_saved.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/engine/tests/save_reports_not_saved.rs b/crates/engine/tests/save_reports_not_saved.rs index 984d0d635..d38e13e0b 100644 --- a/crates/engine/tests/save_reports_not_saved.rs +++ b/crates/engine/tests/save_reports_not_saved.rs @@ -155,9 +155,16 @@ fn saving_sat_text_solids_as_dxf_2013_reports_them() { #[test] fn a_dxf_2013_loss_is_known_before_saving() { use cadcraft_engine::cmd::file::loss_on_save; - set_io(IoHooks { read: |_, _| Err("no reader in this test".into()), write: |_, _| Ok(b"0 + set_io(IoHooks { + read: |_, _| Err("no reader in this test".into()), + write: |_, _| { + Ok(b"0 EOF -".to_vec()), plot: None }); +" + .to_vec()) + }, + plot: None, + }); let sat = |d: &mut Drawing| { let tags = vec![RawTag { code: 8, value: "0".into() }, RawTag { code: 1, value: "400 1 1 0".into() }]; d.add(&Space::Model, Common::default(), EntityKind::Unknown(Unknown { dxf_type: "REGION".into(), tags })).unwrap(); From 86f3fdc9e99454fe096f94b33e8aa864d62fc158 Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 14:34:47 -0700 Subject: [PATCH 070/100] Test: the DWG note's wording, and one-line fake bytes --- crates/engine/tests/save_reports_not_saved.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/crates/engine/tests/save_reports_not_saved.rs b/crates/engine/tests/save_reports_not_saved.rs index d38e13e0b..80192bf53 100644 --- a/crates/engine/tests/save_reports_not_saved.rs +++ b/crates/engine/tests/save_reports_not_saved.rs @@ -60,14 +60,14 @@ fn export_and_wblock_say_what_a_dwg_leaves_out() { let mut s = Session::empty(); s.open_drawing(d, "foreign.dxf", None); let file = |ext: &str| std::env::temp_dir().join(format!("cadcraft-leaves-out-{}.{ext}", std::process::id())).to_string_lossy().into_owned(); - let msg = "Not saved in the DWG file (its writer can't write these object types yet; DXF keeps them): 1 ACME_WIDGET"; + let said = |l: &String| l.starts_with("Not saved in the DWG file") && l.ends_with(": 1 ACME_WIDGET"); let (dwg, dxf) = (file("dwg"), file("dxf")); for (cmd, p) in [("export", json!({ "path": dwg })), ("wblock", json!({ "path": dwg, "handles": [h] }))] { s.log.clear(); let r = s.execute(cmd, &p).unwrap(); assert_eq!(r["notSaved"], json!({ "ACME_WIDGET": 1 }), "{cmd}"); - assert!(s.log.iter().any(|l| l == msg), "{cmd}: {:?}", s.log); + assert!(s.log.iter().any(said), "{cmd}: {:?}", s.log); } // -WBLOCK takes the objects out of the drawing, so last. let r = s.execute("-wblock", &json!({ "path": dwg, "handles": [h] })).unwrap(); @@ -79,7 +79,7 @@ fn export_and_wblock_say_what_a_dwg_leaves_out() { for (cmd, p) in [("export", json!({ "path": dxf })), ("wblock", json!({ "path": dxf, "handles": [h] }))] { s.log.clear(); assert!(s.execute(cmd, &p).unwrap().get("notSaved").is_none(), "{cmd}"); - assert!(!s.log.iter().any(|l| l == msg), "{cmd}: {:?}", s.log); + assert!(!s.log.iter().any(said), "{cmd}: {:?}", s.log); } let _ = std::fs::remove_file(&dxf); } @@ -155,16 +155,7 @@ fn saving_sat_text_solids_as_dxf_2013_reports_them() { #[test] fn a_dxf_2013_loss_is_known_before_saving() { use cadcraft_engine::cmd::file::loss_on_save; - set_io(IoHooks { - read: |_, _| Err("no reader in this test".into()), - write: |_, _| { - Ok(b"0 -EOF -" - .to_vec()) - }, - plot: None, - }); + set_io(IoHooks { read: |_, _| Err("no reader in this test".into()), write: |_, _| Ok(b"0\nEOF\n".to_vec()), plot: None }); let sat = |d: &mut Drawing| { let tags = vec![RawTag { code: 8, value: "0".into() }, RawTag { code: 1, value: "400 1 1 0".into() }]; d.add(&Space::Model, Common::default(), EntityKind::Unknown(Unknown { dxf_type: "REGION".into(), tags })).unwrap(); From 0bc025965ae80d09ecb6ec1ebd487ce901585b64 Mon Sep 17 00:00:00 2001 From: Zaxaerith Date: Mon, 2 Nov 2026 22:01:21 +0000 Subject: [PATCH 071/100] fix(dwg): preserve MTEXT caret-adjacent whitespace (#2250) (#2314) * fix(dwg): preserve literal spaces after MTEXT carets (#2250) * test(dwg): fix MTEXT fixture ownership and boxed entity --------- Co-authored-by: Zaxaerith <189452125+Zaxaerith@users.noreply.github.com> --- crates/dwg/src/lib.rs | 95 +++++++++++++++++- crates/io/tests/it/main.rs | 1 + crates/io/tests/it/mtext_caret_spaces.rs | 118 +++++++++++++++++++++++ 3 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 crates/io/tests/it/mtext_caret_spaces.rs diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index 037aa26b5..ffde17ee8 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -110,8 +110,11 @@ mod native { notes.push(format!("DWG: {orphans} DGN line style component{s} that no line style definition uses {were} left out")); } let mut out = CappedWriter { buf: Vec::new(), limit: limits.max_dxf_bytes, exceeded: false }; - let written = std::panic::catch_unwind(AssertUnwindSafe(|| acadrust::DxfWriter::new(&doc).write_to_writer(&mut out))) - .map_err(|_| "the DWG→DXF conversion failed on this file".to_string())?; + let written = std::panic::catch_unwind(AssertUnwindSafe(|| { + acadrust::DxfWriter::new(&doc).write_to_writer(&mut out)?; + mtext_contents(&doc, &mut out) + })) + .map_err(|_| "the DWG→DXF conversion failed on this file".to_string())?; if out.exceeded { return Err(format!( "DWG: converting this {} file produced more than {} of drawing data (the limit); \ @@ -124,6 +127,94 @@ mod native { Ok((add_header_vars(out.buf, &doc.header), notes)) } + /// acadrust 0.6.6's ASCII writer treats a caret followed by a literal space as already + /// escaped, and turns line feeds into paragraph codes. Serialize affected MTEXT contents + /// from the native values instead, using its lossless string writer. All other groups, + /// including embedded-object data, remain byte-for-byte as the DXF writer emitted them. + fn mtext_contents(doc: &acadrust::CadDocument, out: &mut CappedWriter) -> acadrust::Result<()> { + use acadrust::io::dxf::{DxfStreamWriter, DxfTextWriter}; + let contents: std::collections::HashMap<_, _> = doc + .entities() + .filter_map(|e| match e { + acadrust::EntityType::MText(t) if t.value.contains(['^', '\t', '\n']) => Some((t.common.handle.value(), t.value.as_str())), + _ => None, + }) + .collect(); + if contents.is_empty() { + return Ok(()); + } + let dxf = std::mem::take(&mut out.buf); + let mut lines = dxf.split_inclusive(|b| *b == b'\n'); + let (mut mtext, mut text, mut written) = (false, None, false); + while let Some(code) = lines.next() { + let value = lines.next().ok_or_else(|| std::io::Error::other("incomplete DWG bridge DXF pair"))?; + match code.trim_ascii() { + b"0" => { + mtext = value.trim_ascii() == b"MTEXT"; + text = None; + written = false; + } + b"5" if mtext => { + text = std::str::from_utf8(value.trim_ascii()).ok().and_then(|s| u64::from_str_radix(s, 16).ok()).and_then(|h| contents.get(&h)); + } + b"101" => mtext = false, + b"1" | b"3" if mtext && text.is_some() => { + if let Some(&text) = text + && !written + { + let mut writer = DxfTextWriter::new(&mut *out); + if doc.version < acadrust::DxfVersion::AC1021 + && let Some(encoding) = acadrust::io::dxf::code_page::encoding_from_code_page(&doc.header.code_page) + { + writer.set_encoding(encoding); + } + // Each character can expand to two caret-escape characters. Keep + // those pairs together and each group within 250 characters. + let mut rest = text; + while let Some((end, _)) = rest.char_indices().nth(125) { + let (chunk, tail) = rest.split_at(end); + writer.write_xrecord_string(3, chunk)?; + rest = tail; + } + writer.write_xrecord_string(1, rest)?; + written = true; + } + continue; + } + _ => {} + } + out.write_all(code)?; + out.write_all(value)?; + } + Ok(()) + } + + #[cfg(test)] + #[test] + fn mtext_contents_keeps_other_groups_and_the_output_limit() -> acadrust::Result<()> { + for version in [acadrust::DxfVersion::AC1015, acadrust::DxfVersion::AC1032] { + let mut doc = acadrust::CadDocument::new(); + doc.version = version; + doc.header.code_page = "ANSI_1252".into(); + let mut t = acadrust::MText::with_value("é ^ C", acadrust::Vector3::ZERO); + t.common.handle = acadrust::Handle::new(0x10); + doc.add_entity(acadrust::EntityType::MText(Box::new(t)))?; + // Only the MTEXT's own 1/3 groups change. The embedded object's groups and the + // following TEXT record must survive byte-for-byte, including their spaces. + let prefix = b" 0\r\nMTEXT\r\n 5\r\n10\r\n 8\r\n0\r\n"; + let suffix = b"101\r\nEmbedded Object\r\n 1\r\n^ embedded\r\n 3\r\n^ tail\r\n 0\r\nTEXT\r\n 1\r\n^ plain\r\n"; + let raw = [prefix.as_slice(), b" 3\r\nold\r\n 1\r\ntext\r\n", suffix.as_slice()].concat(); + let mut out = CappedWriter { buf: raw.clone(), limit: 4096, exceeded: false }; + mtext_contents(&doc, &mut out)?; + let value: &[u8] = if version == acadrust::DxfVersion::AC1015 { b"\xe9 ^ C" } else { "é ^ C".as_bytes() }; + assert_eq!(out.buf, [prefix.as_slice(), b" 1\r\n", value, b"\r\n", suffix.as_slice()].concat()); + let mut limited = CappedWriter { buf: raw, limit: 1, exceeded: false }; + assert!(mtext_contents(&doc, &mut limited).is_err()); + assert!(limited.exceeded); + } + Ok(()) + } + /// Most record types named in the skipped-records note. const NOTE_TYPES: usize = 6; diff --git a/crates/io/tests/it/main.rs b/crates/io/tests/it/main.rs index 7b49eb6bf..289e87806 100644 --- a/crates/io/tests/it/main.rs +++ b/crates/io/tests/it/main.rs @@ -42,6 +42,7 @@ mod mleader_roundtrip; mod mline_roundtrip; #[cfg(not(target_arch = "wasm32"))] mod model_layout_plot_type; +mod mtext_caret_spaces; mod mtext_columns_roundtrip; mod mtext_embedded_object; mod mtext_line_spacing; diff --git a/crates/io/tests/it/mtext_caret_spaces.rs b/crates/io/tests/it/mtext_caret_spaces.rs new file mode 100644 index 000000000..8bb1ed071 --- /dev/null +++ b/crates/io/tests/it/mtext_caret_spaces.rs @@ -0,0 +1,118 @@ +//! Literal MTEXT carets and their adjacent whitespace survive DXF and DWG saves (#2250). + +use cadcraft_doc::{Common, Drawing, EntityKind, Handle, MText, Space}; +use cadcraft_geom::Vec3; +use cadcraft_io::{SaveOptions, Version, read, write_with}; + +type TestResult = Result<(), Box>; + +fn drawing() -> Result<(Drawing, Vec<(Handle, String)>), Box> { + let mut d = Drawing::new_metric(); + let mut expected = Vec::new(); + let mut cases: Vec = [ + "ordinary text", + "^ C", + "A ^ C B", + "^ C", + "A ^ C B", + "^ ", + "^^ C ^ ^ D^", + "^I ^J ^ C", + "\t^ C\nD\tE", + "^\t ^\n C", + r"A\P^ C\P^I ^J", + r"\S+0.1^-0.1; ^ C", + ] + .into_iter() + .map(str::to_string) + .collect(); + // Exercise multiple group-3 chunks and a caret at the output chunk boundary. + cases.push(format!("{}^ C", "A".repeat(124))); + cases.push(format!("{}^ C B", "A".repeat(260))); + for contents in cases { + let kind = EntityKind::MText(MText { + insert: Vec3::new(1.0, 2.0, 0.0), + height: 0.5, + width: 12.5, + attach: 1, + rotation: 0.0, + style: "Standard".into(), + contents: contents.clone(), + line_spacing: 1.0, + line_spacing_exact: false, + columns: None, + }); + let handle = d.add(&Space::Model, Common::default(), kind)?; + expected.push((handle, contents)); + } + Ok((d, expected)) +} + +fn assert_contents(d: &Drawing, expected: &[(Handle, String)], context: &str) -> TestResult { + assert_eq!(d.model.len(), expected.len(), "{context}: entity count"); + for (handle, contents) in expected { + let e = d.entity(*handle).ok_or("MTEXT handle lost")?; + let EntityKind::MText(t) = &e.kind else { return Err("MTEXT entity type changed".into()) }; + assert_eq!(&t.contents, contents, "{context}: {handle:?}"); + } + Ok(()) +} + +#[test] +fn dxf_mtext_caret_spaces_round_trip() -> TestResult { + let (original, expected) = drawing()?; + for version in Version::ALL { + for binary in [false, true] { + let mut d = original.clone(); + for pass in 1..=2 { + let bytes = write_with(&d, "carets.dxf", &SaveOptions { version: Some(version), binary })?; + d = read(&bytes, "carets.dxf")?; + assert_contents(&d, &expected, &format!("{version:?}, binary={binary}, pass={pass}"))?; + } + } + } + Ok(()) +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn dwg_mtext_caret_spaces_round_trip() -> TestResult { + let (original, expected) = drawing()?; + for version in Version::ALL { + let mut d = original.clone(); + for pass in 1..=2 { + let bytes = write_with(&d, "carets.dwg", &SaveOptions { version: Some(version), binary: false })?; + assert_eq!(cadcraft_dwg::version(&bytes).as_deref(), Some(version.code())); + // Inspect the stored DWG text before its DXF rendition: a compensating reader + // must not hide corruption on save. This uses the same codec, not an external oracle. + let native = acadrust::DwgReader::from_stream(std::io::Cursor::new(&bytes)).read()?; + for (handle, contents) in &expected { + let text = native.entities().find_map(|e| match e { + acadrust::EntityType::MText(t) if t.common.handle.value() == handle.0 => Some(&t.value), + _ => None, + }); + assert_eq!(text, Some(contents), "native DWG {version:?}, pass={pass}, {handle:?}"); + } + let dxf = cadcraft_dwg::dwg_to_dxf(&bytes)?; + let converted = read(&dxf, "bridge.dxf")?; + assert_contents(&converted, &expected, &format!("DWG bridge DXF {version:?}, pass={pass}"))?; + d = read(&bytes, "carets.dwg")?; + assert_contents(&d, &expected, &format!("DWG {version:?}, pass={pass}"))?; + } + } + Ok(()) +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn bridge_caret_space_is_escaped_once_in_each_direction() -> TestResult { + let (d, _) = drawing()?; + let dxf = cadcraft_io::write_dxf(&d); + assert!(dxf.contains(" 1\r\n^ C\r\n"), "literal caret plus authored space needs two wire spaces"); + let native = acadrust::DxfReader::from_reader(std::io::Cursor::new(dxf.into_bytes()))?.read()?; + assert!(native.entities().any(|e| matches!(e, acadrust::EntityType::MText(t) if t.value == "^ C"))); + let dwg = write_with(&d, "carets.dwg", &SaveOptions { version: Some(Version::R2018), binary: false })?; + let returned = String::from_utf8(cadcraft_dwg::dwg_to_dxf(&dwg)?)?; + assert!(returned.contains(" 1\r\n^ C\r\n"), "the return bridge must encode the separator independently of authored whitespace"); + Ok(()) +} From 03101e8d77b49a4c847f182dd8c98c0cbaca0b25 Mon Sep 17 00:00:00 2001 From: Rahul Date: Tue, 3 Nov 2026 03:31:26 +0530 Subject: [PATCH 072/100] fix(io): a moved DIMENSION draws its block at its insertion point (group 12) (#2634) (#2636) A DIMENSION moved by an older AutoCAD keeps its *D block contents where they were and the move in DXF group 12, and is drawn translated by the insertion point. Group 12 was never read into Dimension, so the block rendered at its pre-move position and the extents reached there too. Read group 12 into a new Dimension::insertion_point, draw the block translated by it, move its bounds with it in the extents, test the block's text where it draws, and write group 12 back whenever the block written is the one read from the file. Regenerated geometry (edits drop the block link) sits at its definition points, so no offset is kept or written for it. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com> --- crates/doc/src/entity/dim.rs | 5 +++ crates/doc/src/extents.rs | 4 ++ crates/doc/src/tests.rs | 39 +++++++++++++++++ crates/engine/src/cmd/annotate/common.rs | 1 + crates/engine/src/sample.rs | 3 ++ crates/engine/src/select.rs | 14 ++++--- crates/engine/tests/compare.rs | 1 + crates/engine/tests/it/compare_export.rs | 4 ++ crates/io/src/dxf_read/entities.rs | 1 + crates/io/src/dxf_write/entity.rs | 6 +++ crates/io/src/tests.rs | 2 + crates/io/tests/dimassoc_angular_radial.rs | 1 + crates/io/tests/it/acad_interop.rs | 1 + .../io/tests/it/dim_block_resave_roundtrip.rs | 42 +++++++++++++++++++ crates/io/tests/it/dim_block_text.rs | 2 + crates/io/tests/it/dim_marks_roundtrip.rs | 1 + crates/io/tests/it/dimstyle_vars_roundtrip.rs | 1 + crates/io/tests/it/dimtfill_roundtrip.rs | 1 + crates/io/tests/it/roundtrip/cases/complex.rs | 1 + crates/io/tests/it/roundtrip_audit.rs | 1 + crates/io/tests/it/text_carets_roundtrip.rs | 1 + crates/render/src/dim/tests.rs | 1 + crates/render/src/draw.rs | 9 +++- crates/render/src/tests.rs | 1 + crates/render/tests/it/dim_angular_centre.rs | 1 + crates/render/tests/it/dim_post_tolerance.rs | 1 + crates/render/tests/it/dim_text_fill.rs | 1 + crates/render/tests/it/dim_text_format.rs | 1 + crates/render/tests/it/render_props.rs | 1 + 29 files changed, 142 insertions(+), 6 deletions(-) diff --git a/crates/doc/src/entity/dim.rs b/crates/doc/src/entity/dim.rs index d10237b26..dc4635c7a 100644 --- a/crates/doc/src/entity/dim.rs +++ b/crates/doc/src/entity/dim.rs @@ -52,6 +52,11 @@ pub struct Dimension { /// Anonymous block with the rendered geometry (from files); regenerated when edited. #[serde(default)] pub block: Option, + /// Insertion point (DXF group 12): a dimension moved by an older AutoCAD keeps its offset + /// here with the `*D` block's contents left where they were, and is drawn translated by it. + /// Zero once the geometry is regenerated from the definition points. + #[serde(default)] + pub insertion_point: Vec3, /// Per-dimension style overrides (DIMOVERRIDE): DimStyle field names (camelCase) → values, /// merged over the named style. #[serde(default, skip_serializing_if = "serde_json::Map::is_empty")] diff --git a/crates/doc/src/extents.rs b/crates/doc/src/extents.rs index 353a42b53..d87cdc9a2 100644 --- a/crates/doc/src/extents.rs +++ b/crates/doc/src/extents.rs @@ -212,6 +212,10 @@ fn bounds_in<'a>(d: &'a Drawing, e: &Entity, depth: usize, blocks: &mut HashMap< inner } }; + // The block draws translated by the insertion point (group 12), so its bounds + // move with it (#2634); the cache above keeps the block's own bounds. + let off = dm.insertion_point.xy(); + let inner = if off == Vec2::ZERO { inner } else { Bounds2 { min: inner.min + off, max: inner.max + off } }; b = b.union(&inner); } b diff --git a/crates/doc/src/tests.rs b/crates/doc/src/tests.rs index f0f979f12..29d55e30f 100644 --- a/crates/doc/src/tests.rs +++ b/crates/doc/src/tests.rs @@ -55,6 +55,44 @@ fn frozen_layers_are_left_out_of_extents_and_layers_turned_off_are_not() { assert_eq!(d.extents(&Space::Model).max, Vec2::new(1.0, 1.0)); } +/// DXF group 12: a dimension moved by an older AutoCAD keeps its `*D` block where it was and +/// the move in the insertion point, and the block draws translated by it. Extents follow the +/// drawing, not the stale block contents (#2634). +#[test] +fn dimension_insertion_point_moves_its_block_in_extents() { + let moved = |ins: Vec3| { + let mut d = Drawing::new_metric(); + let mut blk = Block::new("*D1"); + blk.entities.push(on_layer("0", line((850.0, 920.0), (860.0, 920.0)), 2)); + d.blocks.insert("*D1".into(), std::sync::Arc::new(blk)); + let dm = Dimension { + kind: DimKind::Aligned, + defpt: Vec3::new(5.0, 1.0, 0.0), + text_mid: Vec3::ZERO, + p13: Vec3::ZERO, + p14: Vec3::new(10.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: Some("*D1".into()), + insertion_point: ins, + overrides: Default::default(), + assoc: Vec::new(), + marks: Default::default(), + }; + d.add(&Space::Model, Common::default(), EntityKind::Dimension(dm)).unwrap(); + d + }; + // Without the offset the block's far-away contents reach the extents. + assert!(moved(Vec3::ZERO).extents(&Space::Model).max.x > 800.0); + // With it, the block draws at the definition points and the extents stay at the drawing. + assert!(moved(Vec3::new(-850.0, -920.0, 0.0)).extents(&Space::Model).max.x < 20.0); +} + /// ZOOM Extents in AutoCAD 2024 (accoreconsole, EXTMIN/EXTMAX read back; AutoCAD pads them by a /// few hundredths): a layer turned off counts and a frozen one does not, for an entity, for an /// entity inside a block, and for a block reference itself. @@ -637,6 +675,7 @@ fn invisible_dimblock_entity_is_ignored_in_dimension_extents() { overrides: Default::default(), assoc: vec![], marks: Default::default(), + insertion_point: Vec3::ZERO, }), ) .unwrap(); diff --git a/crates/engine/src/cmd/annotate/common.rs b/crates/engine/src/cmd/annotate/common.rs index 398102797..6854b4fb4 100644 --- a/crates/engine/src/cmd/annotate/common.rs +++ b/crates/engine/src/cmd/annotate/common.rs @@ -22,6 +22,7 @@ pub(super) fn dim(s: &Session, kind: DimKind, defpt: Vec2, p13: Vec2, p14: Vec2, overrides: s.doc().map(|d| d.dim_overrides()).unwrap_or_default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }) } diff --git a/crates/engine/src/sample.rs b/crates/engine/src/sample.rs index 85a222f64..4e615a4f2 100644 --- a/crates/engine/src/sample.rs +++ b/crates/engine/src/sample.rs @@ -32,6 +32,7 @@ fn dim(kind: DimKind, p13: Vec2, p14: Vec2, defpt: Vec2) -> EntityKind { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }) } @@ -182,6 +183,7 @@ pub fn bracket() -> Drawing { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }), ); add( @@ -203,6 +205,7 @@ pub fn bracket() -> Drawing { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }), ); diff --git a/crates/engine/src/select.rs b/crates/engine/src/select.rs index 8047412fd..6721cdad8 100644 --- a/crates/engine/src/select.rs +++ b/crates/engine/src/select.rs @@ -117,11 +117,15 @@ fn in_text(d: &Drawing, e: &Entity, p: Vec2) -> bool { // A dimension with a block (as AutoCAD saves them) is drawn from that block: its text is // the text there, not the text the dimension's own geometry would place. EntityKind::Dimension(dm) => match dm.block.as_ref().and_then(|n| d.block(n)) { - Some(blk) => blk.entities.iter().any(|be| match &be.kind { - EntityKind::MText(t) => in_box(cadcraft_render::layout_mtext_entity(d, t).strokes.into_iter().flatten(), t.rotation, p), - EntityKind::Text(_) => entity_bounds(d, be, 0).contains(p), - _ => false, - }), + // The block draws translated by the insertion point (group 12): test there (#2634). + Some(blk) => { + let p = p - dm.insertion_point.xy(); + blk.entities.iter().any(|be| match &be.kind { + EntityKind::MText(t) => in_box(cadcraft_render::layout_mtext_entity(d, t).strokes.into_iter().flatten(), t.rotation, p), + EntityKind::Text(_) => entity_bounds(d, be, 0).contains(p), + _ => false, + }) + } None => { let g = cadcraft_render::dimension_in(d, dm); in_box(g.text.iter().flatten().chain(g.text_glyphs.iter().flatten().flatten()).copied(), g.text_angle, p) diff --git a/crates/engine/tests/compare.rs b/crates/engine/tests/compare.rs index 0e4486ccb..a346fbba2 100644 --- a/crates/engine/tests/compare.rs +++ b/crates/engine/tests/compare.rs @@ -208,6 +208,7 @@ fn derived_dimension_fields_are_not_differences() { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }; let mut s = Session::new(); s.doc_mut().unwrap().add(&Space::Model, Common::default(), EntityKind::Dimension(dim(30.0, 7.0))).unwrap(); diff --git a/crates/engine/tests/it/compare_export.rs b/crates/engine/tests/it/compare_export.rs index 1aee1b9db..740133c2b 100644 --- a/crates/engine/tests/it/compare_export.rs +++ b/crates/engine/tests/it/compare_export.rs @@ -314,6 +314,7 @@ fn a_named_block_whose_dimension_draws_another_block_shows_the_other_definition( overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }; let blocks = |dim_end: f64, first: u64| { let mut part = Block::new("PART"); @@ -400,6 +401,7 @@ fn a_named_block_whose_dimension_block_has_another_generated_name_keeps_this_dra overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }; let mut part = Block::new("PART"); part.entities.push(Entity::new(Handle(first), EntityKind::Dimension(dim))); @@ -514,6 +516,7 @@ fn a_dimension_block_copy_keeps_its_geometry_when_the_snapshot_is_saved() { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }; let mut s = Session::new(); let mut drawn = Block::new("*D1"); @@ -833,6 +836,7 @@ fn a_dimension_whose_block_is_past_the_nesting_limit_is_drawn_again() { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }; for k in 1..=MAX_BLOCK_DEPTH { let mut b = Block::new(&format!("B{k}")); diff --git a/crates/io/src/dxf_read/entities.rs b/crates/io/src/dxf_read/entities.rs index 2011c3d63..6ca0f30d1 100644 --- a/crates/io/src/dxf_read/entities.rs +++ b/crates/io/src/dxf_read/entities.rs @@ -197,6 +197,7 @@ pub(super) fn entity_with_table_budget(kind: &str, tags: &[Tag], table_cells_lef text_rotation: t.fd(53, 0.0).to_radians(), user_text_pos: t.i(70).unwrap_or(0) & 128 != 0, block: t.s(2).filter(|s| !s.is_empty()), + insertion_point: t.p(12), overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), diff --git a/crates/io/src/dxf_write/entity.rs b/crates/io/src/dxf_write/entity.rs index 51b6b4388..ee9c18d33 100644 --- a/crates/io/src/dxf_write/entity.rs +++ b/crates/io/src/dxf_write/entity.rs @@ -399,6 +399,12 @@ fn write_dimension_entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper let style = d.dim_style(&dm.style).cloned().unwrap_or_default(); let g = cadcraft_render::dimension_geometry(dm, &style, d.header.f64("DIMSCALE", 1.0)); w.p(11, if dm.user_text_pos { dm.text_mid } else { g.text_pos.to3(0.0) }); + // Group 12: a block read from a file keeps the dimension's moved-by offset (AutoCAD + // draws the block translated by it); regenerated geometry already sits where the + // definition points place it, so it writes no offset. + if dm.block.is_some() && dm.insertion_point != Vec3::ZERO { + w.p(12, dm.insertion_point); + } let ty: i64 = match dm.kind { DimKind::Linear { .. } => 0, DimKind::Aligned => 1, diff --git a/crates/io/src/tests.rs b/crates/io/src/tests.rs index 4aa427bdd..5d08817c5 100644 --- a/crates/io/src/tests.rs +++ b/crates/io/src/tests.rs @@ -98,6 +98,7 @@ fn sample() -> Drawing { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }), ) .unwrap(); @@ -558,6 +559,7 @@ fn dim(kind: DimKind, p13: Vec3, p14: Vec3) -> Dimension { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, } } diff --git a/crates/io/tests/dimassoc_angular_radial.rs b/crates/io/tests/dimassoc_angular_radial.rs index f955ad7b5..1df635320 100644 --- a/crates/io/tests/dimassoc_angular_radial.rs +++ b/crates/io/tests/dimassoc_angular_radial.rs @@ -27,6 +27,7 @@ fn dim(kind: DimKind, defpt: Vec3, p13: Vec3, p14: Vec3, p15: Vec3, p16: Vec3, a overrides: Default::default(), assoc, marks: Default::default(), + insertion_point: Vec3::ZERO, }) } diff --git a/crates/io/tests/it/acad_interop.rs b/crates/io/tests/it/acad_interop.rs index 387f23334..bc9cc90e1 100644 --- a/crates/io/tests/it/acad_interop.rs +++ b/crates/io/tests/it/acad_interop.rs @@ -98,6 +98,7 @@ fn s25(kind: DimKind, p13: Vec3, p14: Vec3, defpt: Vec3) -> Drawing { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }; d.add(&Space::Model, Common::default(), EntityKind::Dimension(dm)).unwrap(); d diff --git a/crates/io/tests/it/dim_block_resave_roundtrip.rs b/crates/io/tests/it/dim_block_resave_roundtrip.rs index f0705d6fd..195706157 100644 --- a/crates/io/tests/it/dim_block_resave_roundtrip.rs +++ b/crates/io/tests/it/dim_block_resave_roundtrip.rs @@ -27,6 +27,7 @@ fn linear(a: [f64; 2], b: [f64; 2], at: [f64; 2]) -> Dimension { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, } } @@ -158,6 +159,47 @@ fn reopened_dimension_reuses_its_block_name_and_handles() { assert_eq!(again.handseed, back.handseed); } +#[test] +fn a_moved_dimension_keeps_its_group_12_offset() { + let mut d = Drawing::new_metric(); + // What an older AutoCAD left: the block contents where the dimension was measured, the + // move kept in group 12 (insertion point), as #2634's drawing has it. + let mut b = Block::new("*D3"); + let h = d.new_handle(); + b.entities.push(Entity::new(h, EntityKind::Line(Line { a: Vec3::new(150.0, 80.0, 0.0), b: Vec3::new(160.0, 80.0, 0.0) }))); + d.blocks.insert("*D3".into(), Arc::new(b)); + let mut dm = linear([0.0, 0.0], [10.0, 0.0], [5.0, 3.0]); + dm.block = Some("*D3".into()); + dm.insertion_point = Vec3::new(-150.0, -80.0, 0.0); + let h = d.add(&Space::Model, Common::default(), EntityKind::Dimension(dm)).unwrap(); + + // The block is written as read and the offset round-trips as group 12. + let back = cadcraft_io::read_dxf(cadcraft_io::write_dxf(&d).as_bytes()).unwrap(); + let Some(EntityKind::Dimension(dm)) = back.entity(h).map(|e| &e.kind) else { panic!("dimension lost") }; + assert_eq!(dm.insertion_point, Vec3::new(-150.0, -80.0, 0.0)); + assert_eq!(dm.block.as_deref(), Some("*D3")); + let p = pairs(&cadcraft_io::write_dxf(&d)); + let at = p.iter().position(|(c, v)| *c == 0 && v == "DIMENSION").unwrap(); + let own = &p[at..p[at + 1..].iter().position(|(c, _)| *c == 0).map(|k| at + 1 + k).unwrap()]; + assert!(own.iter().any(|(c, v)| *c == 12 && v.parse::() == Ok(-150.0)), "no group 12 x"); + assert!(own.iter().any(|(c, v)| *c == 22 && v.parse::() == Ok(-80.0)), "no group 12 y"); + + // Edited (edits drop the block link): geometry regenerated from the definition points, + // so the save writes no offset. + let mut edited = back.clone(); + edited + .modify_entity(h, |e| { + if let EntityKind::Dimension(dm) = &mut e.kind { + dm.block = None; + } + }) + .unwrap(); + let q = pairs(&cadcraft_io::write_dxf(&edited)); + let at = q.iter().position(|(c, v)| *c == 0 && v == "DIMENSION").unwrap(); + let own = &q[at..q[at + 1..].iter().position(|(c, _)| *c == 0).map(|k| at + 1 + k).unwrap()]; + assert!(!own.iter().any(|(c, _)| *c == 12), "regenerated geometry keeps a stale offset"); +} + #[test] fn hostile_handle_seed_does_not_panic() { let mut d = Drawing::new_metric(); diff --git a/crates/io/tests/it/dim_block_text.rs b/crates/io/tests/it/dim_block_text.rs index 0c5fa0cab..244e9401c 100644 --- a/crates/io/tests/it/dim_block_text.rs +++ b/crates/io/tests/it/dim_block_text.rs @@ -34,6 +34,7 @@ fn height_written(style_scale: f64, overrides: serde_json::Value) -> f64 { overrides: overrides.as_object().cloned().unwrap_or_default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }; d.add(&Space::Model, Common::default(), EntityKind::Dimension(dm)).unwrap(); block_mtext_height(&cadcraft_io::write_dxf(&d)) @@ -73,6 +74,7 @@ fn angular_drawing(separator_override: Option<&str>) -> Drawing { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }; if let Some(separator) = separator_override { dm.overrides.insert("DIMDSEP".into(), serde_json::json!(separator)); diff --git a/crates/io/tests/it/dim_marks_roundtrip.rs b/crates/io/tests/it/dim_marks_roundtrip.rs index 40a85d89a..c442376de 100644 --- a/crates/io/tests/it/dim_marks_roundtrip.rs +++ b/crates/io/tests/it/dim_marks_roundtrip.rs @@ -24,6 +24,7 @@ fn dim(kind: DimKind, marks: DimMarks) -> EntityKind { overrides: Default::default(), assoc: Vec::new(), marks, + insertion_point: Vec3::ZERO, }) } diff --git a/crates/io/tests/it/dimstyle_vars_roundtrip.rs b/crates/io/tests/it/dimstyle_vars_roundtrip.rs index 061054bbc..b2beac3e2 100644 --- a/crates/io/tests/it/dimstyle_vars_roundtrip.rs +++ b/crates/io/tests/it/dimstyle_vars_roundtrip.rs @@ -60,6 +60,7 @@ fn dimension(overrides: serde_json::Value) -> EntityKind { overrides: overrides.as_object().cloned().unwrap_or_default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }) } diff --git a/crates/io/tests/it/dimtfill_roundtrip.rs b/crates/io/tests/it/dimtfill_roundtrip.rs index 5e1b4f1bf..48f7d9e32 100644 --- a/crates/io/tests/it/dimtfill_roundtrip.rs +++ b/crates/io/tests/it/dimtfill_roundtrip.rs @@ -174,6 +174,7 @@ fn linear() -> Dimension { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, } } diff --git a/crates/io/tests/it/roundtrip/cases/complex.rs b/crates/io/tests/it/roundtrip/cases/complex.rs index ab13f4800..163effa49 100644 --- a/crates/io/tests/it/roundtrip/cases/complex.rs +++ b/crates/io/tests/it/roundtrip/cases/complex.rs @@ -118,6 +118,7 @@ pub(crate) fn dimensions() -> Vec { overrides: Default::default(), assoc: Vec::new(), marks: DimMarks::default(), + insertion_point: Vec3::ZERO, }), ); let mut block = Block::new("*D42"); diff --git a/crates/io/tests/it/roundtrip_audit.rs b/crates/io/tests/it/roundtrip_audit.rs index c2a326935..57f99dbe2 100644 --- a/crates/io/tests/it/roundtrip_audit.rs +++ b/crates/io/tests/it/roundtrip_audit.rs @@ -1034,6 +1034,7 @@ fn add_associated_dimension(d: &mut Drawing, a: cadcraft_doc::Handle, b: cadcraf overrides: Default::default(), assoc: vec![at("p13", a), at("p14", b)], marks: Default::default(), + insertion_point: Vec3::ZERO, }; d.add(&Space::Model, Common::default(), EntityKind::Dimension(dm)).expect("add dimension"); } diff --git a/crates/io/tests/it/text_carets_roundtrip.rs b/crates/io/tests/it/text_carets_roundtrip.rs index 1c30dd1a3..d327c6694 100644 --- a/crates/io/tests/it/text_carets_roundtrip.rs +++ b/crates/io/tests/it/text_carets_roundtrip.rs @@ -38,6 +38,7 @@ fn dimension(label: &str) -> Dimension { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, } } diff --git a/crates/render/src/dim/tests.rs b/crates/render/src/dim/tests.rs index fe3b3c74e..15001c6b7 100644 --- a/crates/render/src/dim/tests.rs +++ b/crates/render/src/dim/tests.rs @@ -19,6 +19,7 @@ fn lin(p1: (f64, f64), p2: (f64, f64), on: (f64, f64), rot: f64) -> Dimension { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, } } diff --git a/crates/render/src/draw.rs b/crates/render/src/draw.rs index 39b0ddfcc..e73659675 100644 --- a/crates/render/src/draw.rs +++ b/crates/render/src/draw.rs @@ -313,7 +313,10 @@ fn draw_dimension(b: &mut Builder, ctx: &Ctx, e: &Entity, dm: &cadcraft_doc::Dim if let Some(blk) = dm.block.as_ref().and_then(|n| ctx.d.block(n)) && ctx.depth < cadcraft_doc::MAX_BLOCK_DEPTH { - let sub = Ctx { block_pstyle: b.named, block_transparency: b.transparency, ..sub_ctx(ctx, e, Mat3::IDENTITY) }; + // DXF group 12: a moved dimension keeps its `*D` block where it was and the offset in + // the insertion point, so the block draws translated by it (#2634). + let off = dm.insertion_point.xy(); + let sub = Ctx { block_pstyle: b.named, block_transparency: b.transparency, ..sub_ctx(ctx, e, Mat3::translate(off)) }; // DIMTFILL: the block's MTEXT carries no background mask here, so a background // around each of the block's texts is drawn before the block's first text or first // line of the text's marks, except for a text that a SOLID of the block covers. @@ -333,6 +336,10 @@ fn draw_dimension(b: &mut Builder, ctx: &Ctx, e: &Entity, dm: &cadcraft_doc::Dim .take(MAX_FILL_TEXTS) .filter(|be| !covered(be)) .filter_map(|be| block_text_box(ctx.d, be, gap)) + .map(|mut corners| { + corners.iter_mut().for_each(|c| *c += off); + corners + }) .collect() }; let mut fill_pending = !boxes.is_empty(); diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index fe2dc6470..e4b314ff5 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -370,6 +370,7 @@ fn lin_dim() -> cadcraft_doc::Dimension { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, } } diff --git a/crates/render/tests/it/dim_angular_centre.rs b/crates/render/tests/it/dim_angular_centre.rs index 9e9c42bdc..149638e0c 100644 --- a/crates/render/tests/it/dim_angular_centre.rs +++ b/crates/render/tests/it/dim_angular_centre.rs @@ -23,6 +23,7 @@ fn dim(kind: DimKind) -> Dimension { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, } } diff --git a/crates/render/tests/it/dim_post_tolerance.rs b/crates/render/tests/it/dim_post_tolerance.rs index 907271a7c..83177d6ec 100644 --- a/crates/render/tests/it/dim_post_tolerance.rs +++ b/crates/render/tests/it/dim_post_tolerance.rs @@ -22,6 +22,7 @@ fn text(vars: serde_json::Value) -> (String, String) { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }; let mut st = DimStyle::default(); let rejected = st.apply_fields(vars.as_object().unwrap()); diff --git a/crates/render/tests/it/dim_text_fill.rs b/crates/render/tests/it/dim_text_fill.rs index b90e1595d..46028a576 100644 --- a/crates/render/tests/it/dim_text_fill.rs +++ b/crates/render/tests/it/dim_text_fill.rs @@ -26,6 +26,7 @@ fn dimension() -> Dimension { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, } } diff --git a/crates/render/tests/it/dim_text_format.rs b/crates/render/tests/it/dim_text_format.rs index e70deea08..779667e5b 100644 --- a/crates/render/tests/it/dim_text_format.rs +++ b/crates/render/tests/it/dim_text_format.rs @@ -23,6 +23,7 @@ fn linear(len: f64) -> Dimension { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, } } diff --git a/crates/render/tests/it/render_props.rs b/crates/render/tests/it/render_props.rs index f210dc4e1..a40b1988d 100644 --- a/crates/render/tests/it/render_props.rs +++ b/crates/render/tests/it/render_props.rs @@ -47,6 +47,7 @@ fn dimensions_take_the_viewport_layer_colour() { overrides: Default::default(), assoc: Vec::new(), marks: Default::default(), + insertion_point: Vec3::ZERO, }; d.add(&Space::Model, on("A"), EntityKind::Dimension(dim)).unwrap(); d.add(&Space::Model, on("A"), EntityKind::Line(Line { a: Vec3::new(0.0, -1.0, 0.0), b: Vec3::new(10.0, -1.0, 0.0) })).unwrap(); From e4cc7ffc77d691589d134c3a3ad8481833af127a Mon Sep 17 00:00:00 2001 From: Coast Date: Mon, 2 Nov 2026 16:01:31 -0600 Subject: [PATCH 073/100] DWG: scale actions keep the byte AutoCAD reads after their connections (acadrust 0.6.6) (#1725) * DWG: scale actions keep the byte AutoCAD reads after their connections (acadrust 0.6.5) AutoCAD's DWG scale action (BLOCKSCALEACTION) ends with one byte after its three connections that DXF doesn't carry. acadrust 0.6.4 neither read nor wrote it, so AutoCAD read past the object and refused every DWG CADCraft wrote with a dynamic block that scales ("Error Decrypting Data"), even an unchanged one. acadrust 0.6.5 keeps the byte (dwg_tail_byte). 0.6.5 also maps the dimension flags byte's bit 0 to text_user_positioned both ways, so the bridge no longer inverts it (it would now double-invert); it still sets bit 1, which acadrust keeps no DXF source for. Co-Authored-By: Claude Opus 5.5 * DWG: acadrust 0.6.6; multi-layout DWGs get unique paper space names 0.6.6 keeps the scale action byte and the dimension flag mapping this branch relies on, and writes only the active layout as *Paper_Space (others get unique *Paper_SpaceN names; hakanaktt/acadrust#143), so ACadSharp opens a DWG CADCraft saves with more than one layout. Co-Authored-By: Claude Opus 5.5 * io tests: a scale action survives CADCraft's own DWG save The cadcraft-dwg test builds its DXF with acadrust's writer; this one goes through cadcraft_io::write (the bridge DXF, then acadrust) with a scale action added to the synthetic Door fixture, and checks the DWG holds it with its connections and reads back as the same action. Co-Authored-By: Claude Opus 5.5 * ui: block description field ids from Ui::scope_id (egui 0.37 deprecates Ui::id) main's gate fails clippy -D warnings on the one Ui::id() call (#2113); Ui::scope_id is its new name. Co-Authored-By: Claude Opus 5.5 * DWG: the scale action byte doc names acadrust 0.6.6, the version in use Co-Authored-By: Claude Sonnet 5.5 * Docs: leave the parity docs to the maintainers (AGENTS.md: PRs don't edit them) Co-Authored-By: Claude Sonnet 5.5 --------- Co-authored-by: Cody Vogt <18035288+codyvogt@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- crates/dwg/Cargo.toml | 2 +- crates/dwg/src/lib.rs | 49 ++++++++++++++++++++++++++++- crates/io/Cargo.toml | 2 +- crates/io/tests/it/dynblock_read.rs | 42 +++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 3 deletions(-) diff --git a/crates/dwg/Cargo.toml b/crates/dwg/Cargo.toml index 0be9fdf40..acb8d0aa7 100644 --- a/crates/dwg/Cargo.toml +++ b/crates/dwg/Cargo.toml @@ -13,4 +13,4 @@ workspace = true # 0.6.7 and 0.6.8 fix that but read some damaged DWGs as empty with no note (tests.rs, #800). [target.'cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))'.dependencies] # MPL-2.0: used as an unmodified dependency only (see plan/adr/0001 and NOTICE). -acadrust = "0.6.5" +acadrust = "0.6.6" diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index ffde17ee8..a5de9bffe 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -435,7 +435,7 @@ mod native { /// The DWG dimension flags byte. Its bit 1 is DXF group 70's bit 32 (ODA DWG specification, /// common dimension data), which acadrust keeps no copy of from DXF: it is set here as /// CADCraft's DXF writer always sets it (#1094). Bit 0, the opposite of group 70's bit 128, - /// acadrust 0.6.5 reads and writes itself. + /// acadrust 0.6.6 reads and writes itself. fn dimension_flags(doc: &mut acadrust::CadDocument) { for e in doc.entities_mut() { let acadrust::entities::EntityType::Dimension(d) = e else { continue }; @@ -1167,4 +1167,51 @@ mod tests { assert!(ls.contains(&"LSDEFINITION") && !ls.contains(&"LSINTERNALCOMPONENT"), "{ls:?}"); assert_eq!(notes, ["DWG: 3 DGN line style components that no line style definition uses were left out"]); } + + /// AutoCAD's DWG scale action (BLOCKSCALEACTION) ends with a byte after its three + /// connections that DXF doesn't carry. acadrust 0.6.4 neither read nor wrote it, so AutoCAD + /// read past the object and refused every DWG CADCraft wrote with a dynamic block that scales + /// ("Error Decrypting Data"); 0.6.5 added it and 0.6.6, which this uses, still keeps it (0.6.6 also + /// fixes multi-layout DWGs, #1463). The byte reaches the DWG, and a scale action + /// from DXF is written whole, with the byte AutoCAD writes (0). + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn scale_actions_keep_the_byte_autocad_reads_after_their_connections() { + use acadrust::objects::*; + let scale_action = |doc: &acadrust::CadDocument| { + doc.objects.values().find_map(|o| match o { + ObjectType::DynamicBlock(DynamicBlockObject { data: DynamicBlockData::ScaleAction(s), .. }) => Some(s.clone()), + _ => None, + }) + }; + let mut doc = acadrust::CadDocument::new(); + let h = doc.allocate_handle(); + let conn = |code, name: &str| BlockConnection { code, name: name.into() }; + let mut action = DynamicBlockObject::new("BLOCKSCALEACTION", "AcDbBlockScaleAction"); + action.handle = h; + action.owner = doc.header.named_objects_dict_handle; + let mut a = BlockBasePointAction::default(); + a.action.action.element.name = "Scale".into(); + a.action.connections = vec![conn(2, "UpdatedBaseX"), conn(2, "UpdatedBaseY")]; + a.action.dependent = true; + a.connections = vec![conn(2, "Scale"), conn(2, "XScale"), conn(2, "YScale")]; + a.dwg_tail_byte = 7; + action.data = DynamicBlockData::ScaleAction(a.clone()); + doc.objects.insert(h, ObjectType::DynamicBlock(action)); + + let dwg = acadrust::DwgWriter::write_to_vec(&doc).unwrap(); + let back = acadrust::DwgReader::from_stream(std::io::Cursor::new(dwg)).read().unwrap(); + assert_eq!(scale_action(&back).map(|s| s.dwg_tail_byte), Some(7), "the byte isn't written"); + + let mut dxf = Vec::new(); + acadrust::DxfWriter::new(&doc).write_to_writer(&mut dxf).unwrap(); + for version in ["AC1018", "AC1032"] { + let dwg = dxf_to_dwg_as(&dxf, version).unwrap(); + let back = acadrust::DwgReader::from_stream(std::io::Cursor::new(dwg)).read().unwrap(); + let read = scale_action(&back).unwrap_or_else(|| panic!("{version}: the scale action wasn't written")); + assert_eq!(read.connections, a.connections, "{version}"); + assert_eq!(read.action.connections, a.action.connections, "{version}"); + assert_eq!(read.dwg_tail_byte, 0, "{version}"); + } + } } diff --git a/crates/io/Cargo.toml b/crates/io/Cargo.toml index 2e8b3cc80..49464e06c 100644 --- a/crates/io/Cargo.toml +++ b/crates/io/Cargo.toml @@ -25,7 +25,7 @@ quick-xml = { workspace = true } [dev-dependencies] # Reads CADCraft's DWG output as written (tests only; the same version as cadcraft-dwg's). -acadrust = "0.6.5" +acadrust = "0.6.6" tiny-skia = { workspace = true } # Reads plotted PDFs back. craft-pdf = { workspace = true, features = ["read"] } diff --git a/crates/io/tests/it/dynblock_read.rs b/crates/io/tests/it/dynblock_read.rs index 251029410..189a2305b 100644 --- a/crates/io/tests/it/dynblock_read.rs +++ b/crates/io/tests/it/dynblock_read.rs @@ -86,6 +86,48 @@ fn a_saved_drawing_keeps_the_dynamic_block_as_read() { assert_eq!(by_node(dynamic_block(&back, "Door").unwrap()), by_node(before)); } +/// The Door fixture with a scale action on the Width parameter: graph node 6, object 68. +fn with_scale_action() -> String { + let node = "91 6\n93 32\n95 7\n360 68\n92 -1\n92 -1\n92 -1\n92 -1\n"; + let action = "0 BLOCKSCALEACTION\n5 68\n330 61\n100 AcDbEvalExpr\n90 7\n98 33\n99 232\n\ + 100 AcDbBlockElement\n300 Scale\n98 33\n99 232\n1071 0\n\ + 100 AcDbBlockAction\n70 0\n71 1\n330 30\n1010 0\n1020 0\n1030 0\n\ + 100 AcDbBlockActionWithBasePt\n92 2\n93 2\n301 UpdatedBaseX\n302 UpdatedBaseY\n\ + 1011 0\n1021 0\n1031 0\n280 1\n1012 0\n1022 0\n1032 0\n\ + 100 AcDbBlockScaleAction\n94 2\n95 2\n96 2\n303 Scale\n304 XScale\n305 YScale\n"; + fixture() + .replacen("96 6\n97 6\n", "96 7\n97 7\n", 1) + .replacen("0 BLOCKVISIBILITYPARAMETER\n", &format!("{node}0 BLOCKVISIBILITYPARAMETER\n"), 1) + .replacen("0 BLOCKLOOKUPPARAMETER\n", &format!("{action}0 BLOCKLOOKUPPARAMETER\n"), 1) +} + +/// A scale action goes through CADCraft's own DWG save (the bridge DXF, then acadrust) whole: +/// the DWG holds it with its connections, and it reads back as the same action. AutoCAD refused +/// such DWGs while acadrust left out the byte after its connections (cadcraft-dwg's +/// `scale_actions_keep_the_byte_autocad_reads_after_their_connections` covers the byte). +#[test] +fn a_scale_action_survives_a_dwg_save() { + use acadrust::objects::{DynamicBlockData, DynamicBlockObject, ObjectType}; + let d = read_dxf(dxf(&with_scale_action()).as_bytes()).unwrap(); + let before = dynamic_block(&d, "Door").unwrap(); + assert!(before.actions.iter().any(|a| a.node == 7 && matches!(a.kind, ActionKind::Scale)), "{:?}", before.actions); + let dwg = cadcraft_io::write(&d, "x.dwg").unwrap(); + + let doc = acadrust::DwgReader::from_stream(std::io::Cursor::new(&dwg)).read().unwrap(); + let scale = doc.objects.values().find_map(|o| match o { + ObjectType::DynamicBlock(DynamicBlockObject { data: DynamicBlockData::ScaleAction(s), .. }) => Some(s.clone()), + _ => None, + }); + let scale = scale.expect("the DWG has no scale action"); + let names = |c: &[acadrust::objects::BlockConnection]| c.iter().map(|c| (c.code, c.name.clone())).collect::>(); + assert_eq!(names(&scale.connections), [(2, "Scale".to_string()), (2, "XScale".into()), (2, "YScale".into())]); + assert_eq!(names(&scale.action.connections), [(2, "UpdatedBaseX".to_string()), (2, "UpdatedBaseY".into())]); + assert_eq!(scale.dwg_tail_byte, 0); + + let back = cadcraft_io::read(&dwg, "x.dwg").unwrap(); + assert_eq!(by_node(dynamic_block(&back, "Door").unwrap()), by_node(before)); +} + /// The model with each parameter and action object named by its graph id instead of its handle. fn by_node(mut m: DynamicBlock) -> DynamicBlock { let ids: std::collections::HashMap = m From 3bb515485d0699a7c25ac34db0d77e26309c13d8 Mon Sep 17 00:00:00 2001 From: Newton Kilgore Sightsinger Date: Mon, 2 Nov 2026 14:01:36 -0800 Subject: [PATCH 074/100] Xrefs: a saved path is found regardless of letter case on case-sensitive file systems (#1982) --- crates/engine/src/cmd/blocks/xref/load.rs | 39 ++++++++++++++++++++++- crates/engine/tests/xref.rs | 17 ++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/blocks/xref/load.rs b/crates/engine/src/cmd/blocks/xref/load.rs index fac289f23..b47aa00e0 100644 --- a/crates/engine/src/cmd/blocks/xref/load.rs +++ b/crates/engine/src/cmd/blocks/xref/load.rs @@ -208,7 +208,7 @@ pub(crate) fn find(path: &str, host: Option<&Path>, chosen: bool) -> Result = { let _ = files; @@ -217,6 +217,43 @@ pub(crate) fn find(path: &str, host: Option<&Path>, chosen: bool) -> Result Option { + use std::path::Component; + let mut out = PathBuf::new(); + for c in p.components() { + let Component::Normal(name) = c else { + out.push(c.as_os_str()); + continue; + }; + let exact = out.join(name); + if exact.exists() { + out = exact; + continue; + } + let want = name.to_str()?.to_lowercase(); + let mut hits = std::fs::read_dir(if out.as_os_str().is_empty() { Path::new(".") } else { &out }) + .ok()? + .take(MAX_CASE_SCAN) + .flatten() + .filter(|e| e.file_name().to_str().is_some_and(|n| n.to_lowercase() == want)); + let hit = hits.next()?; + if hits.next().is_some() { + return None; + } + out.push(hit.file_name()); + } + out.is_file().then_some(out) +} + /// Read a drawing file through the host's io hooks, counting its bytes against the load's total. fn read(file: &Path, cx: &mut Ctx) -> Result { let hooks = super::super::super::file::io().ok_or("file formats are not available in this build")?; diff --git a/crates/engine/tests/xref.rs b/crates/engine/tests/xref.rs index c67c627b2..27fa505c4 100644 --- a/crates/engine/tests/xref.rs +++ b/crates/engine/tests/xref.rs @@ -506,3 +506,20 @@ fn saved_xref_paths_are_followed_beside_the_host_or_on_this_computer_only() { s.execute("-xref", &json!({ "option": "path", "names": "unc", "path": far.to_str().unwrap() })).unwrap(); assert_eq!(status(&s, "unc"), Some(XrefStatus::Loaded)); } + +/// A reference saved on Windows with different letter case (`..\XR\PLAN.DWG` for +/// `../xr/plan.dxf`) is still found on case-sensitive file systems. +#[test] +fn saved_xref_paths_ignore_letter_case() { + hooks(); + let base = folder("case"); + std::fs::create_dir_all(base.join("host")).unwrap(); + std::fs::create_dir_all(base.join("Xr")).unwrap(); + save(&site(), &base.join("Xr").join("Plan.dxf")); + let host = base.join("host").join("host.dxf"); + save(&host_with_paths(&[("plan", r"..\XR\PLAN.DXF")]), &host); + let mut s = Session::new(); + open(&mut s, &host); + assert_eq!(status(&s, "plan"), Some(XrefStatus::Loaded), "{:?}", s.log); + assert!(objects(&s, "plan") > 0); +} From 4e874d089a10562a454b38d8d3ee9c34dfa1662c Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Mon, 2 Nov 2026 23:01:41 +0100 Subject: [PATCH 075/100] Persist current table style (CTABLESTYLE) in DXF header (#2117) Found while testing crates/io/src/dxf_write.rs:1964-1970 (xrecords written) and crates/io/src/dxf_read.rs:2049-2052 (apply) Co-authored-by: Claude Haiku 5.5 --- crates/io/src/dxf_write/header.rs | 1 + crates/io/tests/it/tables_objects_roundtrip.rs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/io/src/dxf_write/header.rs b/crates/io/src/dxf_write/header.rs index 354c293c8..9b8c1f800 100644 --- a/crates/io/src/dxf_write/header.rs +++ b/crates/io/src/dxf_write/header.rs @@ -19,6 +19,7 @@ const HEADER_VARS: &[(&str, i32)] = &[ ("CMLJUST", 70), ("CMLSCALE", 40), ("TEXTSTYLE", 7), + ("CTABLESTYLE", 7), ("CLAYER", 8), ("CELTYPE", 6), ("CECOLOR", 62), diff --git a/crates/io/tests/it/tables_objects_roundtrip.rs b/crates/io/tests/it/tables_objects_roundtrip.rs index 0f75e4cd3..ec2fe7726 100644 --- a/crates/io/tests/it/tables_objects_roundtrip.rs +++ b/crates/io/tests/it/tables_objects_roundtrip.rs @@ -2,7 +2,9 @@ //! multileader styles, layout page setups, named views, UCSs and groups. use cadcraft_doc::geom::{Vec2, Vec3}; -use cadcraft_doc::{Common, DashElement, Drawing, EntityKind, Group, Line, Linetype, MLeaderStyle, NamedView, Space, TextStyle, Ucs, library}; +use cadcraft_doc::{ + Common, DashElement, Drawing, EntityKind, Group, Line, Linetype, MLeaderStyle, NamedView, Space, TableStyle, TextStyle, Ucs, library, +}; use cadcraft_io::{read_dxf, write_dxf}; fn round_trip(d: &Drawing) -> Drawing { @@ -82,6 +84,15 @@ fn multileader_styles_and_current_style_round_trip() { assert_eq!(r.header.str("CMLEADERSTYLE", ""), "Callout"); } +#[test] +fn current_table_style_round_trips() { + let mut d = Drawing::new_imperial(); + d.table_styles.push(TableStyle { name: "TS".into(), ..TableStyle::default() }); + d.header.set_str("CTABLESTYLE", "TS"); + let r = round_trip(&d); + assert_eq!(r.header.str("CTABLESTYLE", ""), "TS"); +} + #[test] fn layout_page_setup_and_paper_view_round_trip() { let mut d = Drawing::new_imperial(); From 3f57f8d3d49ca22e83bae85e4d1a8b445e385bc2 Mon Sep 17 00:00:00 2001 From: midasmatterhorn <248507632+midasmatterhorn@users.noreply.github.com> Date: Tue, 3 Nov 2026 01:01:46 +0300 Subject: [PATCH 076/100] render: a display list past the block-expansion budget says what it left out (#1278) * render: a display list past the block-expansion budget says what it left out * Report viewport truncation only after the expansion budget is exceeded * fmt: keep ui-egui control.rs rustfmt-clean --- crates/io/tests/it/block_expansion.rs | 65 ++++++++++++++++++++++++++- crates/mcp/src/backend.rs | 2 +- crates/render/src/display.rs | 4 ++ crates/render/src/lib.rs | 6 ++- crates/render/src/viewports.rs | 1 + crates/ui-egui/src/control.rs | 20 +++++---- 6 files changed, 87 insertions(+), 11 deletions(-) diff --git a/crates/io/tests/it/block_expansion.rs b/crates/io/tests/it/block_expansion.rs index b190156f8..64fca189e 100644 --- a/crates/io/tests/it/block_expansion.rs +++ b/crates/io/tests/it/block_expansion.rs @@ -1,7 +1,10 @@ //! Nested, self-referencing and arrayed block references in a hostile DXF must not hang drawing //! or extents (issue #255): the expansion is bounded, not only its depth. -use cadcraft_doc::{Drawing, EntityKind, Space}; +use std::sync::Arc; + +use cadcraft_doc::{Block, Common, Drawing, Entity, EntityKind, Handle, Insert, Line, Space, Viewport}; +use cadcraft_geom::{Vec2, Vec3}; use cadcraft_render::{MAX_BLOCK_EXPANSION, Options, build}; fn read(text: &str) -> Drawing { @@ -63,3 +66,63 @@ fn ordinary_nested_and_arrayed_blocks_are_drawn_whole() { assert!(matches!(d.model.iter().next().map(|e| &e.kind), Some(EntityKind::Insert(_)))); assert_eq!(build(&d, &Space::Model, &Options::default()).prims.len(), cadcraft_doc::MAX_BLOCK_DEPTH); } + +#[test] +fn a_build_that_spends_the_expansion_budget_says_so() { + // 10,000 x 10,000 copies of a one-line block: far past the budget. The list stops at the + // budget and says so, so a screen, plot or export missing contents is not silent (#1070). + let d = read(&minsert(10_000, 10_000)); + let list = build(&d, &Space::Model, &Options::default()); + assert!(list.truncated, "block contents were left out without a word"); + assert!(list.prims.len() <= MAX_BLOCK_EXPANSION, "{}", list.prims.len()); + // A drawing within the budget is whole and says nothing. + let d = read(&minsert(3, 2)); + let list = build(&d, &Space::Model, &Options::default()); + assert!(!list.truncated); + assert_eq!(list.prims.len(), 6); +} + +#[test] +fn an_empty_followup_viewport_does_not_mark_an_exact_budget_truncated() { + let mut d = Drawing::new_imperial(); + d.ensure_layer("FROZEN"); + let mut block = Block::new("A"); + block.entities.push(Entity { + handle: Handle(0x200), + common: Common { layer: "FROZEN".into(), ..Common::default() }, + kind: EntityKind::Line(Line { a: Vec3::ZERO, b: Vec3::new(1.0, 1.0, 0.0) }), + }); + d.blocks.insert(block.name.clone(), Arc::new(block)); + let insert = Insert { + block: "A".into(), + insert: Vec3::ZERO, + scale: Vec3::new(1.0, 1.0, 1.0), + rotation: 0.0, + attribs: Vec::new(), + cols: 1_000, + rows: 1_000, + col_spacing: 0.0, + row_spacing: 0.0, + }; + d.add(&Space::Model, Common::default(), EntityKind::Insert(insert)).unwrap(); + let viewport = |center: Vec3, view_center: Vec2, frozen_layers| Viewport { + center, + width: 100.0, + height: 100.0, + view_center, + view_height: 100.0, + id: 2, + locked: false, + frozen_layers, + layer_colors: Vec::new(), + clip: None, + on: true, + }; + let paper = Space::Paper("Layout1".into()); + d.add(&paper, Common::default(), EntityKind::Viewport(viewport(Vec3::new(50.0, 50.0, 0.0), Vec2::ZERO, vec!["FROZEN".into()]))).unwrap(); + d.add(&paper, Common::default(), EntityKind::Viewport(viewport(Vec3::new(200.0, 50.0, 0.0), Vec2::new(10_000.0, 10_000.0), Vec::new()))).unwrap(); + + let list = build(&d, &paper, &Options::default()); + assert_eq!(list.expanded, MAX_BLOCK_EXPANSION); + assert!(!list.truncated, "the first viewport consumed the budget exactly and the second saw no model content"); +} diff --git a/crates/mcp/src/backend.rs b/crates/mcp/src/backend.rs index 20a86fce7..1a36144ca 100644 --- a/crates/mcp/src/backend.rs +++ b/crates/mcp/src/backend.rs @@ -418,7 +418,7 @@ impl Backend for Headless { if let Some(path) = str_p("path") { std::fs::write(&path, &png).map_err(|e| format!("{path}: {e}"))?; } - Ok(json!({"pngBase64": cadcraft_engine::cmd::file::base64_encode(&png), "width": w, "height": h})) + Ok(json!({"pngBase64": cadcraft_engine::cmd::file::base64_encode(&png), "width": w, "height": h, "truncated": list.truncated})) } "app.open" => { let path = str_p("path").ok_or("missing path")?; diff --git a/crates/render/src/display.rs b/crates/render/src/display.rs index e6ba6b7e9..9731ecbf7 100644 --- a/crates/render/src/display.rs +++ b/crates/render/src/display.rs @@ -59,6 +59,10 @@ pub struct DisplayList { pub sheet: Option, /// Block contents expanded while building (see [`MAX_BLOCK_EXPANSION`]). pub expanded: usize, + /// The expansion budget was spent: block contents past it are not in the list. The screen, + /// plots and exports draw what is here; the CLI and the control channel report this, so an + /// output that leaves contents out is never silent (#1070). + pub truncated: bool, /// Plot style end, join and fill style per primitive (parallel to `prims`; missing entries /// are "use object"), turned into geometry by [`pen::apply`]. pub pens: Vec, diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 6fa236e4c..e93c9fbff 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -291,10 +291,14 @@ impl Builder<'_> { /// Take one unit of the block-expansion budget; false once it is spent. fn expand(&mut self) -> bool { self.list.expanded = self.list.expanded.saturating_add(1); - match self.shared_budget { + let ok = match self.shared_budget { Some(spent) => spent.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < MAX_BLOCK_EXPANSION, None => self.list.expanded <= MAX_BLOCK_EXPANSION, + }; + if !ok { + self.list.truncated = true; } + ok } } diff --git a/crates/render/src/viewports.rs b/crates/render/src/viewports.rs index 6f1e57517..67c31c694 100644 --- a/crates/render/src/viewports.rs +++ b/crates/render/src/viewports.rs @@ -174,6 +174,7 @@ fn contents(b: &mut Builder, d: &Drawing, vp: &cadcraft_doc::Viewport, area: Bou // The contents are in paper units now: draw their pens before clipping. pen::apply(&mut sub.list, unit_mm); b.list.expanded = b.list.expanded.saturating_add(sub.list.expanded); + b.list.truncated |= sub.list.truncated; append_clipped(&mut b.list, &sub.list, clip); } diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 8d0a39bf4..4abc6b1f4 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -269,7 +269,7 @@ pub fn inspect(app: &CadApp, ctx: &egui::Context) -> Value { "snap": app.canvas.snap.map(|s| json!({"point": [s.point.x, s.point.y], "mode": s.name})), "session": app.session.summary(), "closePrompt": app.close_prompt(), - "perf": {"frameMs": app.frame_ms, "buildMs": app.canvas.build_ms, "drawMs": app.canvas.draw_ms, "meshMs": app.canvas.mesh_ms, "building": app.canvas.building(), "renderer": if app.canvas.gpu.is_some() { "gpu" } else { "cpu" }, "prims": app.canvas.list.as_ref().map(|l| l.prims.len()), "uiFrames": app.ui_frames, "passIndex": ctx.current_pass_index()}, + "perf": {"frameMs": app.frame_ms, "buildMs": app.canvas.build_ms, "drawMs": app.canvas.draw_ms, "meshMs": app.canvas.mesh_ms, "building": app.canvas.building(), "renderer": if app.canvas.gpu.is_some() { "gpu" } else { "cpu" }, "prims": app.canvas.list.as_ref().map(|l| l.prims.len()), "truncated": app.canvas.list.as_ref().map(|l| l.truncated), "uiFrames": app.ui_frames, "passIndex": ctx.current_pass_index()}, }) } @@ -492,9 +492,9 @@ pub fn handle(app: &mut CadApp, ctx: &egui::Context, req: &ControlRequest) -> Ou if let Err(e) = std::fs::write(path, &png) { return err(e); } - return ok(render_reply(&png, w, h, Some(path))); + return ok(render_reply(&png, w, h, Some(path), list.truncated)); } - ok(render_reply(&png, w, h, None)) + ok(render_reply(&png, w, h, None, list.truncated)) } "app.open" => { let Some(path) = s("path") else { return err("missing path") }; @@ -537,9 +537,11 @@ fn render_view(bounds: &cadcraft_geom::Bounds2, center: Vec2, height: f64, fit: } } -/// The `ui.render` reply, matching the headless backend (`pngBase64`, `width`, `height`), plus `bytes` and `path`. -fn render_reply(png: &[u8], w: u32, h: u32, path: Option<&str>) -> Value { - let mut v = json!({"pngBase64": cadcraft_engine::cmd::file::base64_encode(png), "width": w, "height": h, "bytes": png.len()}); +/// The `ui.render` reply, matching the headless backend (`pngBase64`, `width`, `height`), plus +/// `bytes`, `path` and `truncated` (the build left block contents out, see #1070). +fn render_reply(png: &[u8], w: u32, h: u32, path: Option<&str>, truncated: bool) -> Value { + let mut v = + json!({"pngBase64": cadcraft_engine::cmd::file::base64_encode(png), "width": w, "height": h, "bytes": png.len(), "truncated": truncated}); if let (Some(path), Some(o)) = (path, v.as_object_mut()) { o.insert("path".to_string(), json!(path)); } @@ -816,12 +818,14 @@ mod tests { #[test] fn render_reply_has_base64_width_height_and_keeps_old_keys() { - let r = render_reply(b"abc", 7, 5, Some("x.png")); + let r = render_reply(b"abc", 7, 5, Some("x.png"), false); assert_eq!(r["pngBase64"], "YWJj"); assert_eq!(r["width"], 7); assert_eq!(r["height"], 5); assert_eq!(r["bytes"], 3); assert_eq!(r["path"], "x.png"); - assert!(render_reply(b"abc", 7, 5, None).get("path").is_none()); + assert_eq!(r["truncated"], false); + assert!(render_reply(b"abc", 7, 5, None, true).get("path").is_none()); + assert_eq!(render_reply(b"abc", 7, 5, None, true)["truncated"], true); } } From 5ec0d215d826108c2bb3132e3edb71220f28fa84 Mon Sep 17 00:00:00 2001 From: STE1200 <319441566+STE1200@users.noreply.github.com> Date: Mon, 2 Nov 2026 23:01:51 +0100 Subject: [PATCH 077/100] CLI: info and convert report what a damaged DWG left out (read notes, #859) (#2337) A damaged DWG opens partially since #859 and the drawing's read notes say how many object records were left out. OPEN echoes them and MCP `open` returns them as `warnings`, but `cadcraft-cli info` and `convert` stayed silent: a corpus round trip wrote thousands of skipped records away without a word. - convert: each read note goes to stderr as `IN: `, next to the skipped-records warning (#872). The exit code stays 0. - drawing.inspect (and so info, MCP inspect_drawing and the control channel): new field `readNotes`, next to `skipped`. Existing fields are unchanged. - run already prints OPEN's notes with the session log. - README and docs/control-protocol.md say where the notes appear. Part of #1039. Co-authored-by: Claude Opus 5.5 --- README.md | 5 ++ apps/cadcraft-cli/src/convert.rs | 5 +- apps/cadcraft-cli/src/output.rs | 23 +++++++++ apps/cadcraft-cli/tests/read_notes.rs | 69 +++++++++++++++++++++++++++ crates/engine/src/cmd/inquiry.rs | 2 + docs/control-protocol.md | 2 +- 6 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 apps/cadcraft-cli/tests/read_notes.rs diff --git a/README.md b/README.md index a24b33980..611110c3c 100644 --- a/README.md +++ b/README.md @@ -230,6 +230,11 @@ How image exports (PNG 2400×1600, SVG, PDF) are framed: `convert` fits the mode rectangle, centred and scaled without distortion; `--view extents` or `--view current` forces either framing. To frame a selection set, `zoom {"mode":"object"}` after selecting. +When reading a file leaves something out (records a DXF reader can't place, object records of a +damaged DWG), `info` reports it (`skipped`, `readNotes`), `convert` warns on stderr and `run` +prints the DWG notes with its log; the exit code stays 0. MCP `open` replies with the notes as +`warnings`. + MCP tools include `command_line` (type at the prompt), `execute` (any command with JSON), `inspect_drawing`, `query_entities`, `render` (returns a PNG) and, when connected to the app, `screenshot` and `ui_click`. The desktop app's JSON control channel is documented in diff --git a/apps/cadcraft-cli/src/convert.rs b/apps/cadcraft-cli/src/convert.rs index 6a9c4edbf..35ffc6aed 100644 --- a/apps/cadcraft-cli/src/convert.rs +++ b/apps/cadcraft-cli/src/convert.rs @@ -1,6 +1,6 @@ use super::args::ConvertArgs; use super::error::Result; -use super::output::{Streams, export_framed, export_window, skipped_message, undrawn_message}; +use super::output::{Streams, export_framed, export_window, read_note_messages, skipped_message, undrawn_message}; use super::{open, unicode}; use cadcraft_engine::Session; @@ -9,7 +9,8 @@ pub(crate) fn convert(a: &ConvertArgs, io: &mut Streams) -> Result { let framing = a.framing.framing(); let mut s = Session::empty(); open(&mut s, &a.input)?; - if let Some(m) = skipped_message(s.doc()?, i) { + let d = s.doc()?; + for m in read_note_messages(d, i).into_iter().chain(skipped_message(d, i)) { io.errln(format_args!("{m}")); } if cadcraft_io::is_image(&a.output) diff --git a/apps/cadcraft-cli/src/output.rs b/apps/cadcraft-cli/src/output.rs index d783267dc..5716c486c 100644 --- a/apps/cadcraft-cli/src/output.rs +++ b/apps/cadcraft-cli/src/output.rs @@ -165,6 +165,12 @@ pub(crate) fn undrawn_message(d: &cadcraft_engine::doc::Drawing, path: &str) -> Some(format!("{path}: {n} {entities} not drawn ({})", list.join(", "))) } +/// The reader's notes on what it left out of `path` (a damaged DWG's unreadable object records, +/// #859), one warning line each; empty for a complete read. +pub(crate) fn read_note_messages(d: &cadcraft_engine::doc::Drawing, path: &str) -> Vec { + d.read_notes.iter().map(|note| format!("{path}: {note}")).collect() +} + /// The warning for the records of `path` the reader could not place, or `None` when there are none. pub(crate) fn skipped_message(d: &cadcraft_engine::doc::Drawing, path: &str) -> Option { let n = d.skipped_records.values().fold(0usize, |a, n| a.saturating_add(*n)); @@ -175,3 +181,20 @@ pub(crate) fn skipped_message(d: &cadcraft_engine::doc::Drawing, path: &str) -> let records = if n == 1 { "record" } else { "records" }; Some(format!("{path}: {n} {records} could not be read and are left out ({})", list.join(", "))) } + +#[cfg(test)] +mod tests { + use super::read_note_messages; + + #[test] + fn read_notes_become_one_warning_each() { + let mut d = cadcraft_engine::doc::Drawing::new_imperial(); + assert!(read_note_messages(&d, "a.dwg").is_empty()); + d.read_notes = + vec!["DWG: 3 of 10 object records couldn't be read and were left out (LINE × 3); saving over the original file would lose them".into()]; + assert_eq!( + read_note_messages(&d, "a.dwg"), + vec!["a.dwg: DWG: 3 of 10 object records couldn't be read and were left out (LINE × 3); saving over the original file would lose them"] + ); + } +} diff --git a/apps/cadcraft-cli/tests/read_notes.rs b/apps/cadcraft-cli/tests/read_notes.rs new file mode 100644 index 000000000..2f443f03a --- /dev/null +++ b/apps/cadcraft-cli/tests/read_notes.rs @@ -0,0 +1,69 @@ +//! A damaged DWG opens partially (#859); `info` lists the reader's notes on what it left out +//! (`readNotes`) and `convert` warns with them before writing the copy, so a script or agent sees +//! the loss. The exit code stays 0. + +use std::process::{Command, Output}; + +fn cli(args: &[&str]) -> Output { + Command::new(env!("CARGO_BIN_EXE_cadcraft-cli")).args(args).output().unwrap() +} + +/// A DWG of 200 lines, and a copy damaged so that the reader skips some of its records (found the +/// way the reader's own test finds one: flipping a few bytes at a time across the objects area). +fn dwg_and_damaged_copy() -> (Vec, Vec) { + let mut dxf = String::from("0\nSECTION\n2\nENTITIES\n"); + for i in 0..200 { + dxf += &format!("0\nLINE\n8\n0\n10\n{i}\n20\n0\n11\n{i}\n21\n10\n"); + } + dxf += "0\nENDSEC\n0\nEOF\n"; + let d = cadcraft_io::read(dxf.as_bytes(), "lines.dxf").unwrap(); + let opts = cadcraft_io::SaveOptions { version: Some(cadcraft_io::Version::R2000), binary: false }; + let dwg = cadcraft_io::write_with(&d, "lines.dwg", &opts).unwrap(); + let damaged = (dwg.len() / 4..dwg.len() * 3 / 5) + .step_by(97) + .map(|at| { + let mut b = dwg.clone(); + b[at..at + 6].iter_mut().for_each(|x| *x ^= 0xA5); + b + }) + .find(|b| cadcraft_io::read(b, "damaged.dwg").is_ok_and(|d| !d.read_notes.is_empty())) + .expect("no damaged copy read partially"); + (dwg, damaged) +} + +#[test] +fn info_lists_and_convert_warns_about_read_notes() { + let dir = std::env::temp_dir().join(format!("cc-cli-read-notes-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let (whole, damaged) = dwg_and_damaged_copy(); + let path = dir.join("damaged.dwg"); + std::fs::write(&path, &damaged).unwrap(); + let path = path.to_str().unwrap(); + + let out = cli(&["info", path]); + assert!(out.status.success(), "{:?}: {}", out.status, String::from_utf8_lossy(&out.stderr)); + let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); + let notes = v["readNotes"].as_array().unwrap(); + assert_eq!(notes.len(), 1, "{v}"); + assert!(notes[0].as_str().unwrap().contains(" couldn't be read and "), "{v}"); + + let copy = dir.join("copy.dxf"); + let out = cli(&["convert", path, copy.to_str().unwrap()]); + assert!(out.status.success(), "{:?}: {}", out.status, String::from_utf8_lossy(&out.stderr)); + let err = String::from_utf8_lossy(&out.stderr); + let warnings: Vec<&str> = err.lines().filter(|l| l.contains(" couldn't be read and ")).collect(); + assert_eq!(warnings.len(), 1, "{err}"); + assert!(warnings[0].starts_with(&format!("{path}: DWG: ")), "{err}"); + assert!(copy.exists()); + + // A complete read: no notes, and no warning. + let intact = dir.join("whole.dwg"); + std::fs::write(&intact, &whole).unwrap(); + let out = cli(&["info", intact.to_str().unwrap()]); + let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); + assert_eq!(v["readNotes"], serde_json::json!([])); + let out = cli(&["convert", intact.to_str().unwrap(), copy.to_str().unwrap()]); + assert!(out.status.success()); + assert!(!String::from_utf8_lossy(&out.stderr).contains("couldn't be read"), "{}", String::from_utf8_lossy(&out.stderr)); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/crates/engine/src/cmd/inquiry.rs b/crates/engine/src/cmd/inquiry.rs index 364fa6796..283551dfd 100644 --- a/crates/engine/src/cmd/inquiry.rs +++ b/crates/engine/src/cmd/inquiry.rs @@ -596,6 +596,8 @@ fn run_inspect(s: &mut Session, p: &Value) -> Result { "view": { "center": [st.view().center.x, st.view().center.y], "height": st.view().height }, // Records of the file the drawing came from that the reader could not place. "skipped": { "count": d.skipped_records.values().fold(0usize, |a, n| a.saturating_add(*n)), "sections": d.skipped_records }, + // What reading the file left out, in words (a damaged DWG's unreadable object records). + "readNotes": d.read_notes, }); if bool_or(p, "entities", true) && let (Some(o), Some(store)) = (out.as_object_mut(), d.space(&space)) diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 98db28330..d10d43abb 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -49,7 +49,7 @@ field. `engine.commands` returns a separate command catalog, not a list of addit | `cmdline.state` | | Current prompt, keywords, accepted input kinds, history tail. | | `engine.execute` | `{command, params}` | Run any command with JSON parameters. Never opens a dialog. `command` is an alias of this method, with the same parameters and result. | | `engine.commands` | | Every command: id, label, menu path, shortcut, aliases, params doc, enabled. | -| `drawing.inspect` | `{entities?, limit?}` | Drawing summary (counts, extents, layers, styles, blocks, layouts, selection, undo) and entities with handles and geometry. | +| `drawing.inspect` | `{entities?, limit?}` | Drawing summary (counts, extents, layers, styles, blocks, layouts, selection, undo, and what reading the file left out: `skipped` records by section, `readNotes` in words) and entities with handles and geometry. | | `ui.inspect` | | UI state, canvas rect, view, cursor and snap, performance counters (`uiFrames`, `passIndex` included); `closePrompt`: the drawing a "Save changes?" prompt is asking about (null when none); and actual egui keyboard focus (`focus.id`, `focus.label`, `focus.role`). | | `ui.elements` | `{label?, role?, focused?}` | Observed toolbar, tab, drafting and layer-palette controls from the latest UI frame. Filters match exactly. Each entry has its real egui `id`, label, role, rectangle `[x,y,width,height]`, enabled/visible/selected state and current focus. | | `ui.menu.list` / `ui.menu.invoke` | `{command}` | The menu tree; invoke an item like a click (interactive). | From 2ac500925b6b53b6a3f00dc30e4428caf565ebbe Mon Sep 17 00:00:00 2001 From: Books Date: Tue, 3 Nov 2026 09:01:57 +1100 Subject: [PATCH 078/100] DWG: a table cell's text height is an override only when the file flags it (part of #2501) (#2588) * DWG: a table cell's text height is an override only when the file flags it (#2501) acadrust's DXF writer writes group 140 for any cell with a style record, even a default 0.18 with no override flag, and CADCraft read it as a per-cell height, so cells ignored the table style's height. The bridge now clears the height of a cell style that has neither the TEXT_HEIGHT property flag nor the content's height override bit (0 reads as no override). * Parity checklist: regenerated after merging main * Tests: inherited table cell heights must stay unset (review on #2588) A cell that inherits its height must read back as None, not an explicit 0.0625 override. * Parity checklist: regenerated after merging main * Test: a cell style with the TEXT_HEIGHT flag, content override bit 128 and a cell with no style record keep the right height (review on #2588) * DWG: a table cell's content height override (group 144) is kept (review on #2588) * Parity checklist: regenerated after merging main * DWG: table cell height takes the first content's override and the style's override flags (review on #2588) Only the first content's bit-128 height stands for the cell (one height for its text); an invalid content height no longer keeps the writer's default 0.18; a height marked only in the style's override flags (newer files) is kept. --- crates/dwg/src/lib.rs | 67 +++++++++ crates/io/tests/dwg_table_cell_height.rs | 173 +++++++++++++++++++++++ docs/parity-checklist.md | 4 +- 3 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 crates/io/tests/dwg_table_cell_height.rs diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index a5de9bffe..e5463aaec 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -332,12 +332,51 @@ mod native { /// can cover rows × cols cells, so the total is bounded too. const MAX_MERGE_CELLS: usize = 1 << 24; + /// A table cell's own text height only when the file says it overrides the table style's. + /// acadrust's DXF writer writes a cell style's height (group 140) whenever the cell has a + /// style record, even a default 0.18 with no override flag, and CADCraft reads a 140 as an + /// override (#2501). The override is the cell style's TEXT_HEIGHT property flag or the + /// content's format override bit 128; without either the height is cleared (0 is no + /// override), so the table style's height applies. + fn table_cell_heights(doc: &mut acadrust::CadDocument) { + use acadrust::entities::table::CellStylePropertyFlags; + const CONTENT_TEXT_HEIGHT: i32 = 128; + for e in doc.entities_mut() { + let acadrust::entities::EntityType::Table(t) = e else { continue }; + for cell in t.rows.iter_mut().flat_map(|r| r.cells.iter_mut()) { + // The first content's own height (group 144) when its format override sets bit 128. + // CADCraft keeps one height for the cell's text, so a later content's override + // must not stand in for the first content's. + let content_height = cell + .contents + .first() + .filter(|c| c.format_override_flags & CONTENT_TEXT_HEIGHT != 0) + .map(|c| c.text_height) + .filter(|h| h.is_finite() && *h > 0.0); + if let Some(h) = content_height { + // CADCraft reads only the style's height (group 140), so carry it there. + cell.style.get_or_insert_with(acadrust::entities::table::CellStyle::new).text_height = h; + continue; + } + // A style overrides through its property flags or, in newer files, its override + // flags; anything else is the writer's default and is cleared. + if let Some(style) = cell.style.as_mut() + && !style.property_flags.contains(CellStylePropertyFlags::TEXT_HEIGHT) + && style.override_flags & CellStylePropertyFlags::TEXT_HEIGHT.bits() as i32 == 0 + { + style.text_height = 0.0; + } + } + } + } + /// The fix-ups made to a document after the DWG read, returning how many orphaned DGN /// components were dropped. The document comes from a third-party reader and is hostile. fn post_process(doc: &mut acadrust::CadDocument) -> usize { super::fail_in_test("post_process"); current_style_names(doc); table_merges(doc); + table_cell_heights(doc); table_style_rows(doc, false); keep_sort_handles(doc); drop_orphaned_dgn_components(doc) @@ -821,6 +860,34 @@ mod native { assert_eq!(table_merges_within(&mut doc, usize::MAX), 100); } + /// A height marked only in the style's override flags (as newer files store it) is kept, + /// and the same height with no flag at all is cleared (review on #2588). Tested on the + /// read document because acadrust's DWG writer doesn't write the override flags. + #[test] + fn a_style_override_flag_keeps_the_cell_height() { + use acadrust::entities::table::{CellStyle, CellStylePropertyFlags}; + let height = |override_flags: i32| { + let mut doc = doc_with_table(1, 1, 0); + for e in doc.entities_mut() { + if let EntityType::Table(t) = e { + for cell in t.rows.iter_mut().flat_map(|r| r.cells.iter_mut()) { + let mut s = CellStyle::new(); + s.text_height = 0.5; + s.override_flags = override_flags; + cell.style = Some(s); + } + } + } + table_cell_heights(&mut doc); + doc.entities().find_map(|e| match e { + EntityType::Table(t) => t.rows.first()?.cells.first()?.style.as_ref().map(|s| s.text_height), + _ => None, + }) + }; + assert_eq!(height(CellStylePropertyFlags::TEXT_HEIGHT.bits() as i32), Some(0.5)); + assert_eq!(height(0), Some(0.0)); + } + /// A panic in the post-read fix-ups is an error, not a crash (#1934). #[test] fn a_panic_in_post_processing_is_an_error() { diff --git a/crates/io/tests/dwg_table_cell_height.rs b/crates/io/tests/dwg_table_cell_height.rs new file mode 100644 index 000000000..11ee98c9d --- /dev/null +++ b/crates/io/tests/dwg_table_cell_height.rs @@ -0,0 +1,173 @@ +//! TABLE heights inherit from the table style unless the file records a cell override. +#![cfg(not(target_arch = "wasm32"))] + +use cadcraft_doc::{Common, Drawing, EntityKind, Space, Table, TableCell, TableStyle}; + +fn drawing() -> Drawing { + let mut d = Drawing::new_imperial(); + d.table_styles.push(TableStyle { name: "Small".into(), text_height: 0.0625, title: false, header: false, ..TableStyle::default() }); + let table = Table { + style: "Small".into(), + text_height: 0.0625, + title: false, + header: false, + row_heights: vec![0.2], + col_widths: vec![2.0; 3], + cells: vec![vec![ + TableCell { text: "plain".into(), ..TableCell::default() }, + TableCell { text: "{\\C256;formatted}".into(), ..TableCell::default() }, + TableCell { text: "explicit".into(), text_height: Some(0.125), ..TableCell::default() }, + ]], + ..Table::default() + }; + d.add(&Space::Model, Common::default(), EntityKind::Table(table)).unwrap(); + d +} + +fn check(d: &Drawing) { + let t = d + .model + .iter() + .find_map(|e| match &e.kind { + EntityKind::Table(t) => Some(t), + _ => None, + }) + .unwrap(); + assert_eq!(t.style, "Small"); + assert_eq!(t.text_height, 0.0625); + for cell in &t.cells[0][..2] { + assert!(cell.text_height.is_none(), "unexpected override: {:?}", cell.text_height); + } + assert_eq!(t.cells[0][2].text_height, Some(0.125)); +} + +#[test] +fn table_cell_heights_keep_inheritance_through_dwg() { + let bytes = cadcraft_io::write(&drawing(), "height.dwg").unwrap(); + // A style record can be present for metadata without overriding its text height. + // Write that synthetic representation directly so no earlier DXF conversion can + // turn the unused default into an explicit override before this import. + let mut doc = acadrust::DwgReader::from_stream(std::io::Cursor::new(bytes)).read().unwrap(); + let table = doc + .entities_mut() + .find_map(|e| match e { + acadrust::entities::EntityType::Table(t) => Some(t), + _ => None, + }) + .unwrap(); + table.rows[0].cells[0].style = Some(acadrust::entities::table::CellStyle::new()); + let bytes = acadrust::DwgWriter::write_to_vec(&doc).unwrap(); + check(&cadcraft_io::read(&bytes, "height.dwg").unwrap()); +} + +#[test] +fn table_cell_heights_keep_inheritance_through_dxf() { + let bytes = cadcraft_io::write(&drawing(), "height.dxf").unwrap(); + check(&cadcraft_io::read(&bytes, "height.dxf").unwrap()); +} + +// ---- cells with their own style record (review on #2588) ---- + +use acadrust::entities::table::{CellContent, CellStyle, CellStylePropertyFlags}; + +/// Write the drawing as a DWG, edit its first cell through acadrust (`edit`), write it again as a +/// DWG (or, with `as_dxf`, as a DXF by acadrust's writer) and read it with CADCraft. Returns the first cell's +/// text height override. +fn first_cell_height(as_dxf: bool, edit: impl FnOnce(&mut acadrust::entities::table::TableCell)) -> Option { + let bytes = cadcraft_io::write(&drawing(), "height.dwg").unwrap(); + let mut doc = acadrust::DwgReader::from_stream(std::io::Cursor::new(bytes)).read().unwrap(); + let table = doc + .entities_mut() + .find_map(|e| match e { + acadrust::entities::EntityType::Table(t) => Some(t), + _ => None, + }) + .unwrap(); + edit(&mut table.rows[0].cells[0]); + let d = if as_dxf { + let bytes = acadrust::DxfWriter::new(&doc).write_to_vec().unwrap(); + cadcraft_io::read(&bytes, "height.dxf").unwrap() + } else { + let bytes = acadrust::DwgWriter::write_to_vec(&doc).unwrap(); + cadcraft_io::read(&bytes, "height.dwg").unwrap() + }; + d.model + .iter() + .find_map(|e| match &e.kind { + EntityKind::Table(t) => Some(t.cells[0][0].text_height), + _ => None, + }) + .unwrap() +} + +fn styled(height: f64, flags: CellStylePropertyFlags) -> CellStyle { + let mut s = CellStyle::new(); + s.text_height = height; + s.property_flags = flags; + s +} + +#[test] +fn a_cell_style_with_the_text_height_flag_keeps_its_height() { + let h = first_cell_height(false, |c| c.style = Some(styled(0.5, CellStylePropertyFlags::TEXT_HEIGHT))); + assert_eq!(h, Some(0.5)); +} + +#[test] +fn content_with_format_override_bit_128_keeps_its_own_height() { + // acadrust writes the content's height as group 144 and the style's as 140; they differ here + // so the result shows which one CADCraft honoured. + let h = first_cell_height(false, |c| { + c.style = Some(styled(0.5, CellStylePropertyFlags::NONE)); + if c.contents.is_empty() { + c.contents.push(CellContent::new()); + } + c.contents[0].format_override_flags |= 128; + c.contents[0].text_height = 0.3125; + }); + assert_eq!(h, Some(0.3125)); +} + +/// Only the first content's override stands for the cell: a later content's height must not +/// replace the first content's inherited one (review on #2588). +#[test] +fn a_later_contents_height_does_not_replace_the_first_contents() { + let h = first_cell_height(false, |c| { + c.style = Some(styled(0.18, CellStylePropertyFlags::NONE)); + c.contents.clear(); + c.contents.push(CellContent::new()); + let mut later = CellContent::new(); + later.format_override_flags |= 128; + later.text_height = 0.3125; + c.contents.push(later); + }); + assert_eq!(h, None); +} + +/// Bit 128 with an unusable content height copies nothing, so the writer's default style height +/// is still cleared (review on #2588). +#[test] +fn bit_128_with_an_invalid_height_still_clears_the_default() { + for bad in [0.0, -1.0, f64::NAN] { + let h = first_cell_height(false, |c| { + c.style = Some(styled(0.18, CellStylePropertyFlags::NONE)); + if c.contents.is_empty() { + c.contents.push(CellContent::new()); + } + c.contents[0].format_override_flags |= 128; + c.contents[0].text_height = bad; + }); + assert_eq!(h, None, "content height {bad}"); + } +} + +#[test] +fn a_cell_with_no_style_record_inherits_the_table_style() { + assert_eq!(first_cell_height(false, |c| c.style = None), None); +} + +/// A style record with a height and neither override is not an override, through DWG. +#[test] +fn a_style_height_without_an_override_is_cleared() { + assert_eq!(first_cell_height(false, |c| c.style = Some(styled(0.18, CellStylePropertyFlags::NONE))), None); +} diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index 53ffffc96..a91ee2f25 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -1,10 +1,10 @@ # Parity checklist -> **Generated:** 2026-10-30 by `cargo xtask parity`. Do not edit by hand. +> **Generated:** 2026-11-01 by `cargo xtask parity`. Do not edit by hand. Menu-breadth parity: a reference menu item counts as covered when a CADCraft command is registered under the same menu label. It measures breadth, not depth; see [target-app-parity.md](target-app-parity.md) for the weighted assessment. -**Menu breadth: 298 / 491 items (61%)** · 444 registered commands. +**Menu breadth: 298 / 491 items (61%)** · 449 registered commands. | Menu | Items | Covered | % | |---|---|---|---| From eacdbe43eef1b58b7436317830751c8b6e93bf6e Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 17:10:53 -0700 Subject: [PATCH 079/100] Save: a folder that can't be made says so, not that the backup failed (#2669) The backup ran before the folders were made. On Linux, a path under a file gave 'the backup copy could not be written' instead of naming the folder. --- crates/engine/src/cmd/file.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 72d4ef692..76a53da46 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -440,7 +440,8 @@ fn save_as(s: &mut Session, cmd: &str, path: &str, opts: &SaveAs) -> Result Result<()> { #[cfg(not(target_arch = "wasm32"))] { - if is_drawing_file(path) && crate::autosave::isavebak() { + // A file in a folder still to be made has nothing to back up. + if is_drawing_file(path) && crate::autosave::isavebak() && crate::atomic_write::missing_folder(std::path::Path::new(path)).is_none() { backup(std::path::Path::new(path)) .map_err(|e| bad("save", format!("{path}: the backup copy could not be written ({e}); nothing was saved")))?; } From f389d6a72e3fc160ea125b4f264d68f64cbe2aba Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 2 Nov 2026 19:26:12 -0500 Subject: [PATCH 080/100] Release 0.17.0 Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 32 ++++++++++++++++---------------- Cargo.toml | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b5210faea..98d98880f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -614,7 +614,7 @@ checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" [[package]] name = "cadcraft" -version = "0.16.0" +version = "0.17.0" dependencies = [ "cadcraft-engine", "cadcraft-io", @@ -644,7 +644,7 @@ dependencies = [ [[package]] name = "cadcraft-cli" -version = "0.16.0" +version = "0.17.0" dependencies = [ "cadcraft-engine", "cadcraft-io", @@ -660,14 +660,14 @@ dependencies = [ [[package]] name = "cadcraft-color" -version = "0.16.0" +version = "0.17.0" dependencies = [ "serde", ] [[package]] name = "cadcraft-constraints" -version = "0.16.0" +version = "0.17.0" dependencies = [ "cadcraft-doc", "cadcraft-geom", @@ -677,7 +677,7 @@ dependencies = [ [[package]] name = "cadcraft-doc" -version = "0.16.0" +version = "0.17.0" dependencies = [ "cadcraft-color", "cadcraft-geom", @@ -688,14 +688,14 @@ dependencies = [ [[package]] name = "cadcraft-dwg" -version = "0.16.0" +version = "0.17.0" dependencies = [ "acadrust", ] [[package]] name = "cadcraft-dxf" -version = "0.16.0" +version = "0.17.0" dependencies = [ "encoding_rs", "log", @@ -704,7 +704,7 @@ dependencies = [ [[package]] name = "cadcraft-engine" -version = "0.16.0" +version = "0.17.0" dependencies = [ "cadcraft-color", "cadcraft-constraints", @@ -725,7 +725,7 @@ dependencies = [ [[package]] name = "cadcraft-fonts" -version = "0.16.0" +version = "0.17.0" dependencies = [ "cadcraft-geom", "harfrust 0.14.0", @@ -736,14 +736,14 @@ dependencies = [ [[package]] name = "cadcraft-geom" -version = "0.16.0" +version = "0.17.0" dependencies = [ "serde", ] [[package]] name = "cadcraft-io" -version = "0.16.0" +version = "0.17.0" dependencies = [ "acadrust", "cadcraft-color", @@ -764,7 +764,7 @@ dependencies = [ [[package]] name = "cadcraft-mcp" -version = "0.16.0" +version = "0.17.0" dependencies = [ "cadcraft-engine", "cadcraft-io", @@ -777,7 +777,7 @@ dependencies = [ [[package]] name = "cadcraft-render" -version = "0.16.0" +version = "0.17.0" dependencies = [ "cadcraft-color", "cadcraft-doc", @@ -791,7 +791,7 @@ dependencies = [ [[package]] name = "cadcraft-ui-egui" -version = "0.16.0" +version = "0.17.0" dependencies = [ "cadcraft-color", "cadcraft-doc", @@ -817,7 +817,7 @@ dependencies = [ [[package]] name = "cadcraft-web" -version = "0.16.0" +version = "0.17.0" dependencies = [ "cadcraft-engine", "cadcraft-io", @@ -5928,7 +5928,7 @@ checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee" [[package]] name = "xtask" -version = "0.16.0" +version = "0.17.0" dependencies = [ "chrono", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index b84651eee..f51aa8909 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"] # default-members [workspace.package] -version = "0.16.0" +version = "0.17.0" edition = "2024" license = "MIT OR Apache-2.0" rust-version = "1.95" From fdfe4d95a8e588dab40a174a45ec9e2586753f68 Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 18:06:35 -0700 Subject: [PATCH 081/100] Autosave: failures are shown, and a crash is recovered at the first relaunch (#2670) * Autosave: a failed write is shown until one works (E6) A failure only reached the log, so someone could work for hours with no recovery copy. The autosaver keeps why each drawing's write failed; the app says it once at the command line and shows a notice that doesn't block saving. It ends when a write works or the drawing no longer needs a copy. * Crash recovery: offered at the first relaunch, not 5 minutes later (E5) A crashed app's lock file looked alive until it went stale, so a relaunch within 5 minutes skipped its autosaves, and a suspended app's looked dead. A running app now holds a system lock on the file, which the OS drops however the process ends. File age still decides where locks don't work and for lock files of older versions, which don't record holding one. --- crates/engine/src/autosave.rs | 112 +++++++++++++++++++---- crates/engine/tests/autosave.rs | 94 +++++++++++++++++++ crates/ui-egui/src/autosave.rs | 58 ++++++++++++ crates/ui-egui/src/lib.rs | 4 + crates/ui-egui/tests/autosave_failure.rs | 82 +++++++++++++++++ 5 files changed, 331 insertions(+), 19 deletions(-) create mode 100644 crates/ui-egui/tests/autosave_failure.rs diff --git a/crates/engine/src/autosave.rs b/crates/engine/src/autosave.rs index 9ff7eab0b..92e97afeb 100644 --- a/crates/engine/src/autosave.rs +++ b/crates/engine/src/autosave.rs @@ -4,15 +4,18 @@ //! autosave is written as DXF to `/__.sv$`. The file goes //! away when the drawing is saved or closed and when the app exits normally, so a file left behind //! means the app (or the machine) went down; the next start offers it for recovery. A running app -//! keeps a lock file (`.lock`) fresh, so another copy running at the same time doesn't -//! offer its files. SAVEFILEPATH may be a folder other programs use too, so only names built from -//! an instance id ([`is_instance_id`]) are ever read as ours or deleted. +//! holds an operating-system lock on its lock file (`.lock`) and keeps the file fresh, so +//! another copy running at the same time doesn't offer its files. The system drops the lock the +//! moment a process ends, however it ended, so a relaunch after a crash can offer recovery at once. +//! Where locks aren't available, and for lock files of older versions, a file not refreshed for +//! [`STALE_LOCK_SECS`] counts as stopped. SAVEFILEPATH may be a folder other programs use too, so +//! only names built from an instance id ([`is_instance_id`]) are ever read as ours or deleted. //! //! [`Autosaver::plan`] decides what to write and delete (no I/O, so it is testable); on the desktop //! [`Autosaver::tick`] carries it out, writing on a background thread. SAVETIME and SAVEFILEPATH //! are profile settings: process-wide, not saved in the drawing. -use std::collections::{HashMap, HashSet}; +use std::collections::{BTreeMap, HashMap, HashSet}; use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::{Arc, Mutex, Weak}; @@ -29,6 +32,10 @@ pub const DEFAULT_SAVETIME: u32 = 10; pub const MAX_SAVETIME: u32 = 600; /// A lock file not refreshed for this long belongs to an app that is no longer running. pub const STALE_LOCK_SECS: u64 = 5 * 60; +/// How a lock file ends when its app holds the system lock on it, so a free lock proves the app +/// stopped. Older versions write only the process id and never hold one. +#[cfg(not(target_arch = "wasm32"))] +const LOCK_HELD: &str = " held"; /// How often a running app refreshes its lock file. #[cfg(not(target_arch = "wasm32"))] const HEARTBEAT_SECS: f64 = 60.0; @@ -149,13 +156,19 @@ pub struct Autosaver { docs: HashMap, /// Drawings whose autosave is being written right now. busy: HashSet, - /// Writes in progress; each tells whether its file was written. + /// Why the last autosave of each drawing failed, until one succeeds or the drawing no longer + /// needs it. Ordered, so [`Autosaver::failure`] doesn't change between frames. + failed: BTreeMap, + /// Writes in progress; each ends with why its file wasn't written, if it wasn't. #[cfg(not(target_arch = "wasm32"))] - running: Vec<(u64, std::thread::JoinHandle)>, + running: Vec<(u64, std::thread::JoinHandle>)>, #[cfg(not(target_arch = "wasm32"))] start: std::time::Instant, #[cfg(not(target_arch = "wasm32"))] last_beat: Option, + /// The lock file this app holds the system lock on, open for as long as the lock is wanted. + #[cfg(not(target_arch = "wasm32"))] + lock: Option<(std::path::PathBuf, std::fs::File)>, } impl Autosaver { @@ -164,12 +177,15 @@ impl Autosaver { instance: instance.into(), docs: HashMap::new(), busy: HashSet::new(), + failed: BTreeMap::new(), #[cfg(not(target_arch = "wasm32"))] running: Vec::new(), #[cfg(not(target_arch = "wasm32"))] start: std::time::Instant::now(), #[cfg(not(target_arch = "wasm32"))] last_beat: None, + #[cfg(not(target_arch = "wasm32"))] + lock: None, } } @@ -187,6 +203,11 @@ impl Autosaver { self.docs.values().filter_map(|t| t.file.clone()).collect() } + /// Why a drawing currently has no up-to-date recovery copy, for the host to show the user. + pub fn failure(&self) -> Option<&str> { + self.failed.values().next().map(String::as_str) + } + /// What to do at `now` (seconds, any fixed origin) with SAVETIME `minutes` and autosave folder /// `dir`: write drawings changed `minutes` ago or earlier and not autosaved since, delete the /// files of drawings that were saved (no unsaved changes left) or closed. @@ -220,6 +241,7 @@ impl Autosaver { jobs.push(Job::Delete { path }); } *t = Tracked::default(); + self.failed.remove(&st.uid); continue; } let same = |w: &Option>, d: &Arc| w.as_ref().is_some_and(|w| std::ptr::eq(w.as_ptr(), Arc::as_ptr(d))); @@ -248,6 +270,8 @@ impl Autosaver { t.since = None; jobs.push(Job::Write { uid: st.uid, path, doc }); } + let docs = &self.docs; + self.failed.retain(|uid, _| docs.contains_key(uid)); jobs } @@ -277,12 +301,14 @@ pub use native::*; #[cfg(not(target_arch = "wasm32"))] mod native { + use std::fs::{File, OpenOptions, TryLockError}; + use std::io::{Read, Write}; use std::path::Path; use std::time::{Duration, SystemTime}; use super::{ - Autosaver, Drawing, HEARTBEAT_SECS, HashMap, Job, Recoverable, STALE_LOCK_SECS, Session, is_instance_id, parse_file_name, parse_lock_name, - savefilepath, savetime, + Autosaver, Drawing, HEARTBEAT_SECS, HashMap, Job, LOCK_HELD, Recoverable, STALE_LOCK_SECS, Session, is_instance_id, parse_file_name, + parse_lock_name, savefilepath, savetime, }; /// A new instance id: the process id and the start time. @@ -307,6 +333,28 @@ mod native { Some(SystemTime::now().duration_since(m).unwrap_or_default()) } + /// Whether the app instance named by a lock file in `dir` may still be running: its system + /// lock answers at once, and the file's age decides when it can't (no lock support, a lock + /// file from an older version). + fn owner_alive(dir: &Path, instance: &str) -> bool { + let by_age = || instance_alive(lock_age(dir, instance)); + let path = lock_path(dir, instance); + // Only a plain file is opened: opening a FIFO planted in a shared folder would block. + if !std::fs::symlink_metadata(&path).is_ok_and(|m| m.is_file()) { + return by_age(); + } + let Ok(file) = File::open(&path) else { return by_age() }; + match file.try_lock() { + Err(TryLockError::WouldBlock) => true, + Err(TryLockError::Error(_)) => by_age(), + Ok(()) => { + let mut text = String::new(); + let held = file.take(64).read_to_string(&mut text).is_ok() && text.ends_with(LOCK_HELD); + !held && by_age() + } + } + } + /// The autosave files in `dir` left behind by apps that are no longer running (never those of /// `own`, this app's instance), oldest first. pub fn recoverable(dir: &Path, own: &str) -> Vec { @@ -316,7 +364,7 @@ mod native { for e in entries.flatten().take(10_000) { let name = e.file_name().to_string_lossy().to_string(); let Some((title, instance, _)) = parse_file_name(&name) else { continue }; - if instance == own || *alive.entry(instance.to_string()).or_insert_with(|| instance_alive(lock_age(dir, instance))) { + if instance == own || *alive.entry(instance.to_string()).or_insert_with(|| owner_alive(dir, instance)) { continue; } let Ok(meta) = e.metadata() else { continue }; @@ -358,7 +406,7 @@ mod native { for n in &names { let Some(instance) = parse_lock_name(n) else { continue }; let has_files = names.iter().any(|f| parse_file_name(f).is_some_and(|(_, i, _)| i == instance)); - if !has_files && !instance_alive(lock_age(dir, instance)) { + if !has_files && !owner_alive(dir, instance) { let _ = std::fs::remove_file(dir.join(n)); } } @@ -425,22 +473,27 @@ mod native { /// Take in the writes that have finished ([`Autosaver::tick`] does this first). A drawing /// whose write failed is no longer counted as autosaved: the next plan restarts its clock, - /// so it is tried again SAVETIME later even if it doesn't change. + /// so it is tried again SAVETIME later even if it doesn't change, and [`Autosaver::failure`] + /// says why until a write succeeds. pub fn reap(&mut self) { let (done, running) = std::mem::take(&mut self.running).into_iter().partition::, _>(|(_, h)| h.is_finished()); self.running = running; for (uid, h) in done { self.busy.remove(&uid); - if !h.join().unwrap_or(false) { - self.unwritten(uid); + match h.join().unwrap_or_else(|_| Err("the autosave writer stopped unexpectedly".to_string())) { + Ok(()) => { + self.failed.remove(&uid); + } + Err(e) => self.unwritten(uid, e), } } } - fn unwritten(&mut self, uid: u64) { + fn unwritten(&mut self, uid: u64, why: String) { if let Some(t) = self.docs.get_mut(&uid) { t.written = None; } + self.failed.insert(uid, why); } /// Carry out jobs from [`Autosaver::plan`]: writes on a background thread, deletes now. @@ -451,16 +504,17 @@ mod native { Job::Write { uid, path, doc } => { let Some(write) = crate::cmd::file::io().map(|h| h.write) else { log::warn!("autosave: file formats are not available in this build"); + self.unwritten(uid, "file formats are not available in this build".to_string()); continue; }; let spawned = std::thread::Builder::new().name("autosave".into()).spawn(move || match write_file(write, &doc, &path) { Ok(()) => { log::info!("autosaved {path}"); - true + Ok(()) } Err(e) => { log::warn!("autosave {path}: {e}"); - false + Err(format!("{path}: {e}")) } }); match spawned { @@ -470,7 +524,7 @@ mod native { } Err(e) => { log::warn!("autosave: {e}"); - self.unwritten(uid); + self.unwritten(uid, e.to_string()); } } } @@ -485,12 +539,31 @@ mod native { } self.last_beat = Some(now); let dir = Path::new(dir); - let r = create_folder(dir).and_then(|()| std::fs::write(lock_path(dir, &self.instance), std::process::id().to_string())); - if let Err(e) = r { + if let Err(e) = create_folder(dir).and_then(|()| self.refresh_lock(&lock_path(dir, &self.instance))) { log::warn!("autosave: lock file in {}: {e}", dir.display()); } } + /// Refresh the lock file at `path`, taking the system lock on it first if this app doesn't + /// hold it there yet. Without one the file still works by its age. + fn refresh_lock(&mut self, path: &Path) -> std::io::Result<()> { + if let Some((held, file)) = &self.lock + && held == path + { + return file.set_modified(SystemTime::now()); + } + self.lock = None; + let id = std::process::id(); + let mut file = OpenOptions::new().write(true).create(true).truncate(false).open(path)?; + if file.try_lock().is_err() { + return std::fs::write(path, id.to_string()); + } + file.set_len(0)?; + file.write_all(format!("{id}{LOCK_HELD}").as_bytes())?; + self.lock = Some((path.to_path_buf(), file)); + Ok(()) + } + /// The app is closing normally: finish writes in progress, then delete this app's autosave /// files and its lock file. pub fn shutdown(&mut self) { @@ -502,6 +575,7 @@ mod native { discard(&path); } self.docs.clear(); + self.lock = None; let dir = savefilepath(); if !dir.is_empty() && is_instance_id(&self.instance) { let _ = std::fs::remove_file(lock_path(Path::new(&dir), &self.instance)); diff --git a/crates/engine/tests/autosave.rs b/crates/engine/tests/autosave.rs index f44be5231..32d21fd05 100644 --- a/crates/engine/tests/autosave.rs +++ b/crates/engine/tests/autosave.rs @@ -169,6 +169,7 @@ fn recovery_offers_files_of_apps_that_stopped() { #[test] fn autosave_files_are_written_in_the_background_and_removed_on_exit() { + let _turn = folder_turn(); hooks(); let dir = temp_dir("write"); let mut s = Session::new(); @@ -255,6 +256,68 @@ fn a_new_autosave_folder_is_owner_only() { let _ = std::fs::remove_dir_all(&base); } +/// SAVEFILEPATH is process-wide: the tests that set it take turns. +static FOLDER: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +fn folder_turn() -> std::sync::MutexGuard<'static, ()> { + FOLDER.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} + +/// Run autosave once in `dir`, so `a` writes a file and takes its lock, and wait for the file. +fn autosave_once(a: &mut Autosaver, s: &Session, dir: &std::path::Path) -> String { + autosave::set_savefilepath(&dir.to_string_lossy()); + assert_eq!(a.save_now(s), 1); + for _ in 0..500 { + a.reap(); + if !a.writing() && !a.files().is_empty() { + break; + } + std::thread::sleep(Duration::from_millis(10)); + } + a.files().pop().unwrap() +} + +#[test] +fn a_crashed_app_is_offered_at_once_and_a_running_one_never() { + hooks(); + let _turn = folder_turn(); + let dir = temp_dir("instant"); + let mut s = Session::new(); + draw(&mut s); + let mut a = Autosaver::new("41-e"); + let file = autosave_once(&mut a, &s, &dir); + let lock = dir.join("41-e.lock"); + assert!(std::fs::metadata(&lock).is_ok()); + + // Running: not offered, even once its lock file looks old (a suspended laptop). + std::fs::File::options().write(true).open(&lock).unwrap().set_modified(SystemTime::now() - Duration::from_secs(3600)).ok(); + assert!(autosave::recoverable(&dir, "9-f").is_empty()); + autosave::prune_locks(&dir); + assert!(lock.exists(), "a running app's lock stays"); + + // Gone without closing, as in a crash: the file is fresh, and it is offered right away. + std::fs::File::options().write(true).open(&lock).unwrap().set_modified(SystemTime::now()).ok(); + drop(a); + let found = autosave::recoverable(&dir, "9-f"); + assert_eq!(found.iter().map(|r| r.path.as_str()).collect::>(), vec![file.as_str()]); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn a_normal_exit_removes_the_lock_file() { + hooks(); + let _turn = folder_turn(); + let dir = temp_dir("exit"); + let mut s = Session::new(); + draw(&mut s); + let mut a = Autosaver::new("42-e"); + autosave_once(&mut a, &s, &dir); + assert!(dir.join("42-e.lock").exists()); + a.shutdown(); + assert!(!dir.join("42-e.lock").exists()); + let _ = std::fs::remove_dir_all(&dir); +} + #[test] fn a_failed_autosave_is_tried_again_without_another_change() { hooks(); @@ -283,6 +346,7 @@ fn a_failed_autosave_is_tried_again_without_another_change() { std::thread::sleep(Duration::from_millis(10)); } assert!(rearmed, "a failed autosave must be tried again"); + assert!(a.failure().is_some_and(|m| m.contains("out")), "{:?}", a.failure()); std::fs::remove_file(&out).unwrap(); let jobs = a.plan(&s, 180.0, 1, &dir); @@ -303,6 +367,36 @@ fn a_failed_autosave_is_tried_again_without_another_change() { std::thread::sleep(Duration::from_millis(10)); } assert_eq!(a.next_due(1e4, 1), None); + assert_eq!(a.failure(), None, "a write that worked clears the failure"); + a.shutdown(); + let _ = std::fs::remove_dir_all(&root); +} + +#[test] +fn a_failure_ends_when_the_drawing_no_longer_needs_a_copy() { + hooks(); + let root = temp_dir("failed-close"); + let out = root.join("out"); + std::fs::write(&out, b"not a folder").unwrap(); + let dir = out.to_string_lossy().to_string(); + let mut s = Session::new(); + draw(&mut s); + let mut a = Autosaver::new("32-e"); + a.plan(&s, 0.0, 1, &dir); + let jobs = a.plan(&s, 60.0, 1, &dir); + a.perform(jobs); + for _ in 0..500 { + a.reap(); + if a.failure().is_some() { + break; + } + std::thread::sleep(Duration::from_millis(10)); + } + assert!(a.failure().is_some()); + let i = s.active; + s.execute("close", &json!({ "index": i })).unwrap(); + a.plan(&s, 120.0, 1, &dir); + assert_eq!(a.failure(), None); a.shutdown(); let _ = std::fs::remove_dir_all(&root); } diff --git a/crates/ui-egui/src/autosave.rs b/crates/ui-egui/src/autosave.rs index 012231683..d09d95770 100644 --- a/crates/ui-egui/src/autosave.rs +++ b/crates/ui-egui/src/autosave.rs @@ -32,6 +32,15 @@ pub fn load_prefs(json: &str) { } } +/// What the autosave failure notice says (see [`failure_notice`]). +#[derive(Clone, Debug, PartialEq)] +pub struct AutosaveNotice { + pub message: String, + /// The user closed it. The failure stays recorded so the notice doesn't come back on every + /// retry; it does for the next failure after a write has worked. + pub dismissed: bool, +} + /// What the GPU error notice says (see [`gpu_notice`]). #[derive(Clone, Debug, PartialEq)] pub struct GpuNotice { @@ -90,6 +99,24 @@ impl CadApp { if let Some(wait) = self.autosave.as_mut().and_then(|s| s.tick(&self.session)) { _ctx.request_repaint_after(wait); } + #[cfg(not(target_arch = "wasm32"))] + self.note_autosave_failure(_ctx); + } + + /// Keep [`Self::autosave_notice`] in step with autosave: a new failure is said once at the + /// command line and shown until it is dismissed or a write works. + #[cfg(not(target_arch = "wasm32"))] + fn note_autosave_failure(&mut self, ctx: &egui::Context) { + let failure = self.autosave.as_ref().and_then(|s| s.failure()).map(str::to_string); + match (failure, &mut self.autosave_notice) { + (None, notice) => *notice = None, + (Some(message), Some(n)) => n.message = message, + (Some(message), notice) => { + self.session.echo(format!("Autosave failed: {message}")); + *notice = Some(AutosaveNotice { message, dismissed: false }); + ctx.request_repaint(); + } + } } /// Every frame: after a GPU error or device loss, autosave unsaved drawings at once (the @@ -242,6 +269,37 @@ pub fn gpu_notice(app: &mut CadApp, ctx: &egui::Context) { } } +/// The notice that autosave isn't working, so unsaved changes have no recovery copy. It doesn't +/// block the drawing: the user needs to be able to save. +pub fn failure_notice(app: &mut CadApp, ctx: &egui::Context) { + let Some(n) = app.autosave_notice.as_ref().filter(|n| !n.dismissed) else { return }; + let t = Tokens::get(); + let mut dismiss = false; + egui::Window::new("Autosave failed") + .id(egui::Id::unique("cc_autosave_notice")) + .collapsible(false) + .resizable(false) + .anchor(egui::Align2::CENTER_TOP, vec2(0.0, 48.0)) + .show(ctx, |ui| { + ui.set_width(440.0); + ui.label("CADCraft could not write a recovery copy, so recent changes would be lost if the app stopped now."); + ui.label(RichText::new("Save your work. Autosave tries again after the SAVETIME interval; its folder is in Options.").strong()); + ui.add_space(4.0); + ui.collapsing("Details", |ui| { + ui.label(RichText::new(&n.message).color(t.text_dim).monospace()); + }); + // A row just as tall as the button (a plain right-to-left layout takes the rest). + ui.allocate_ui_with_layout(vec2(ui.available_width(), 0.0), egui::Layout::right_to_left(egui::Align::Center), |ui| { + if ui.button("Dismiss").clicked() { + dismiss = true; + } + }); + }); + if dismiss && let Some(n) = &mut app.autosave_notice { + n.dismissed = true; + } +} + #[cfg(test)] mod tests { #[test] diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 142a589a3..03fa5285b 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -305,6 +305,8 @@ pub struct CadApp { pub gpu_faults: std::sync::Arc, /// The notice shown after one (see [`autosave::gpu_notice`]). pub gpu_notice: Option, + /// The notice shown while autosave fails (see [`autosave::failure_notice`]). + pub autosave_notice: Option, /// The Command Aliases dialog while open ([`aliasdlg`]). pub alias_dlg: Option, /// The Hatch and Gradient dialog while open or picking ([`hatchdlg`]). @@ -364,6 +366,7 @@ impl CadApp { recovery: autosave::Recovery::default(), gpu_faults: std::sync::Arc::default(), gpu_notice: None, + autosave_notice: None, alias_dlg: None, hatch_dlg: None, bedit: bedit::State::default(), @@ -903,6 +906,7 @@ impl CadApp { save_loss::prompt(self, ui.ctx()); autosave::show(self, ui.ctx()); autosave::gpu_notice(self, ui.ctx()); + autosave::failure_notice(self, ui.ctx()); self.frame_ms = now_ms() - t0; } diff --git a/crates/ui-egui/tests/autosave_failure.rs b/crates/ui-egui/tests/autosave_failure.rs new file mode 100644 index 000000000..28b6a66d0 --- /dev/null +++ b/crates/ui-egui/tests/autosave_failure.rs @@ -0,0 +1,82 @@ +//! A failed autosave reaches the user: a notice and one command-line message, gone once a write +//! works. SAVEFILEPATH is process-wide, so everything runs in one test. + +use cadcraft_engine::Session; +use cadcraft_engine::cmd::file::{IoHooks, set_io}; +use cadcraft_engine::doc::Drawing; +use cadcraft_ui_egui::{CadApp, Services}; + +fn wait_for(app: &mut CadApp, ctx: &egui::Context, done: impl Fn(&CadApp) -> bool) { + for _ in 0..500 { + app.autosave_tick(ctx); + if done(app) { + return; + } + std::thread::sleep(std::time::Duration::from_millis(10)); + } +} + +/// The text the notice paints (the second frame: a new window sizes itself in the first). +fn painted_text(app: &mut CadApp, ctx: &egui::Context) -> String { + fn collect(shape: &egui::Shape, text: &mut String) { + match shape { + egui::Shape::Text(t) => text.push_str(t.galley.text()), + egui::Shape::Vec(v) => v.iter().for_each(|s| collect(s, text)), + _ => {} + } + } + let mut text = String::new(); + for _ in 0..2 { + let mut out = ctx.run_ui(egui::RawInput::default(), |ui| cadcraft_ui_egui::autosave::failure_notice(app, ui.ctx())); + out.textures_delta.clear(); // no renderer here + text.clear(); + out.shapes.iter().for_each(|s| collect(&s.shape, &mut text)); + } + text +} + +#[test] +fn a_failed_autosave_is_shown_until_a_write_works() { + set_io(IoHooks { read: |_, _| Ok(Drawing::new_metric()), write: |_, _| Ok(b"0\nEOF\n".to_vec()), plot: None }); + let root = std::env::temp_dir().join(format!("cadcraft-autosave-failure-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + // The folder can't be created while a file holds its name. + let dir = root.join("out"); + std::fs::write(&dir, b"not a folder").unwrap(); + cadcraft_engine::autosave::set_savefilepath(&dir.to_string_lossy()); + + let mut app = CadApp::new(Session::new(), Services::default()); + app.enable_autosave(&dir); + let ctx = egui::Context::default(); + app.autosave_tick(&ctx); + assert!(app.autosave_notice.is_none(), "nothing failed yet"); + + app.run("circle", serde_json::json!({"center": [0, 0], "radius": 1})).unwrap(); + app.gpu_faults.error("forces an autosave now".into()); + app.gpu_fault_tick(&ctx); + wait_for(&mut app, &ctx, |a| a.autosave_notice.is_some()); + let n = app.autosave_notice.clone().expect("the failure reaches the notice"); + assert!(!n.dismissed && n.message.contains("out"), "{n:?}"); + let said = |app: &CadApp| app.session.log.iter().filter(|l| l.starts_with("Autosave failed:")).count(); + assert_eq!(said(&app), 1); + assert!(painted_text(&mut app, &ctx).contains("could not write a recovery copy")); + + // Dismissed, it stays away through retries and isn't repeated at the command line. + app.autosave_notice.as_mut().unwrap().dismissed = true; + app.autosave_tick(&ctx); + assert!(app.autosave_notice.as_ref().is_some_and(|n| n.dismissed)); + assert!(painted_text(&mut app, &ctx).is_empty()); + assert_eq!(said(&app), 1); + + // The folder comes back: the next write clears it. + std::fs::remove_file(&dir).unwrap(); + app.run("circle", serde_json::json!({"center": [5, 0], "radius": 1})).unwrap(); + app.gpu_faults.error("forces an autosave now".into()); + app.gpu_fault_tick(&ctx); + wait_for(&mut app, &ctx, |a| a.autosave_notice.is_none()); + assert!(app.autosave_notice.is_none(), "a write that worked ends the notice"); + + app.shutdown_autosave(); + let _ = std::fs::remove_dir_all(&root); +} From 6c4984f5ad05ef0cbd9b42028ce06521c7f29341 Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 18:14:29 -0700 Subject: [PATCH 082/100] A panic in the interface saves the work and reports it (E4) (#2671) The frame now runs under a guard. A panic autosaves unsaved drawings at once, even with SAVETIME 0, and shows a notice that doesn't block saving. A panic hook writes a local report for every panic. Panics that keep coming stop the interface and leave a window that closes, with the recovery copies kept. --- apps/cadcraft/src/main.rs | 7 +- crates/engine/src/autosave.rs | 39 ++++- crates/engine/tests/autosave.rs | 20 +++ crates/ui-egui/src/autosave.rs | 5 +- crates/ui-egui/src/fault.rs | 235 ++++++++++++++++++++++++++++++ crates/ui-egui/src/lib.rs | 4 + crates/ui-egui/tests/ui_faults.rs | 97 ++++++++++++ 7 files changed, 396 insertions(+), 11 deletions(-) create mode 100644 crates/ui-egui/src/fault.rs create mode 100644 crates/ui-egui/tests/ui_faults.rs diff --git a/apps/cadcraft/src/main.rs b/apps/cadcraft/src/main.rs index 3454dc35f..4b124ad1d 100644 --- a/apps/cadcraft/src/main.rs +++ b/apps/cadcraft/src/main.rs @@ -86,7 +86,7 @@ impl eframe::App for App { fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { self.cad.system_animations = self.system_animations.current(); // Consume control/open/screenshot queues and time-based autosave, including hidden ticks. - self.cad.logic(ctx); + self.cad.guarded_logic(ctx); // Host frame work runs here, once per frame, and so also on a hidden logic-only tick (which // never reaches `App::ui`): native menus stay current, the spacemouse still navigates and a // workspace changed while hidden is persisted. `current_pass_index()` is zero on the first @@ -116,7 +116,7 @@ impl eframe::App for App { cadcraft_ui_egui::cmdline::capture_tab(ctx, raw); } fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { - self.cad.ui(ui); + self.cad.guarded_ui(ui); #[cfg(target_os = "macos")] craft_window::title_bar::sync_movable(ui.ctx(), &mut self.movable); } @@ -289,6 +289,9 @@ fn main() -> eframe::Result { } install_io(); cadcraft_engine::settings_dir::set(vars.config_dir.clone()); + if let Some(dir) = &vars.config_dir { + cadcraft_ui_egui::fault::install_report_hook(dir.join(cadcraft_ui_egui::fault::REPORT_DIR)); + } if let Some(name) = &vars.font_alt { cadcraft_engine::fonts::ttf::set_font_alt(name); } diff --git a/crates/engine/src/autosave.rs b/crates/engine/src/autosave.rs index 92e97afeb..ca6d6ede4 100644 --- a/crates/engine/src/autosave.rs +++ b/crates/engine/src/autosave.rs @@ -479,16 +479,41 @@ mod native { let (done, running) = std::mem::take(&mut self.running).into_iter().partition::, _>(|(_, h)| h.is_finished()); self.running = running; for (uid, h) in done { - self.busy.remove(&uid); - match h.join().unwrap_or_else(|_| Err("the autosave writer stopped unexpectedly".to_string())) { - Ok(()) => { - self.failed.remove(&uid); - } - Err(e) => self.unwritten(uid, e), - } + self.settle(uid, h); } } + fn settle(&mut self, uid: u64, h: std::thread::JoinHandle>) { + self.busy.remove(&uid); + match h.join().unwrap_or_else(|_| Err("the autosave writer stopped unexpectedly".to_string())) { + Ok(()) => { + self.failed.remove(&uid); + } + Err(e) => self.unwritten(uid, e), + } + } + + /// Write every drawing with changes not yet autosaved and wait until it is done, because + /// the app has gone wrong and may not get another frame. Unlike [`Autosaver::save_now`] + /// this also runs when SAVETIME is 0: unsaved work comes before that preference. Returns + /// how many drawings were written. + pub fn emergency_save(&mut self, s: &Session) -> usize { + for (uid, h) in std::mem::take(&mut self.running) { + self.settle(uid, h); + } + let now = self.start.elapsed().as_secs_f64(); + let dir = savefilepath(); + let jobs = self.plan_with(s, now, savetime().max(1), &dir, true); + let uids: Vec = jobs.iter().filter_map(|j| if let Job::Write { uid, .. } = j { Some(*uid) } else { None }).collect(); + self.perform(jobs); + self.last_beat = None; + self.heartbeat(&dir, now); + for (uid, h) in std::mem::take(&mut self.running) { + self.settle(uid, h); + } + uids.iter().filter(|uid| !self.failed.contains_key(uid)).count() + } + fn unwritten(&mut self, uid: u64, why: String) { if let Some(t) = self.docs.get_mut(&uid) { t.written = None; diff --git a/crates/engine/tests/autosave.rs b/crates/engine/tests/autosave.rs index 32d21fd05..a878faae7 100644 --- a/crates/engine/tests/autosave.rs +++ b/crates/engine/tests/autosave.rs @@ -303,6 +303,26 @@ fn a_crashed_app_is_offered_at_once_and_a_running_one_never() { let _ = std::fs::remove_dir_all(&dir); } +#[test] +fn an_emergency_save_finishes_before_it_returns_even_with_savetime_off() { + hooks(); + let _turn = folder_turn(); + let dir = temp_dir("emergency"); + autosave::set_savefilepath(&dir.to_string_lossy()); + let before = autosave::savetime(); + autosave::set_savetime(0); + let mut s = Session::new(); + draw(&mut s); + let mut a = Autosaver::new("43-e"); + assert_eq!(a.save_now(&s), 0, "an ordinary save honours SAVETIME 0"); + assert_eq!(a.emergency_save(&s), 1); + autosave::set_savetime(before); + let file = a.files().pop().unwrap(); + assert!(std::fs::read(&file).unwrap().ends_with(b"EOF\n"), "the file is whole on return"); + assert_eq!(a.emergency_save(&s), 0, "nothing changed since"); + let _ = std::fs::remove_dir_all(&dir); +} + #[test] fn a_normal_exit_removes_the_lock_file() { hooks(); diff --git a/crates/ui-egui/src/autosave.rs b/crates/ui-egui/src/autosave.rs index d09d95770..0d50e2dcc 100644 --- a/crates/ui-egui/src/autosave.rs +++ b/crates/ui-egui/src/autosave.rs @@ -134,8 +134,9 @@ impl CadApp { /// The app closes normally: this app's autosave files are not needed any more. pub fn shutdown_autosave(&mut self) { + // After the interface gave up, the recovery copies are all that's left of the work. #[cfg(not(target_arch = "wasm32"))] - if let Some(s) = &mut self.autosave { + if let Some(s) = self.autosave.as_mut().filter(|_| !self.fault.broken) { s.shutdown(); } } @@ -143,7 +144,7 @@ impl CadApp { /// `secs` since 1970 as `YYYY-MM-DD HH:MM` (UTC). Past chrono's year 262143 range the proleptic /// Gregorian date is kept via the 400-year cycle rather than falling back to the epoch. -fn utc(secs: u64) -> String { +pub(crate) fn utc(secs: u64) -> String { let (h, m) = ((secs % 86_400) / 3600, (secs % 3600) / 60); let (y, mo, d) = civil_from_days(secs / 86_400); // `{y:04}` rather than `%Y`, which writes a `+` before years past 9999. diff --git a/crates/ui-egui/src/fault.rs b/crates/ui-egui/src/fault.rs new file mode 100644 index 000000000..7d087ed23 --- /dev/null +++ b/crates/ui-egui/src/fault.rs @@ -0,0 +1,235 @@ +//! What happens when the interface panics. Commands already run under their own guard +//! (`cadcraft_engine::guard`); this one covers the rest of the frame, so a panic in a panel keeps +//! the user's work: every drawing with unsaved changes is autosaved at once and the app says so. +//! A panic that keeps coming back leaves a plain window that only closes, with the recovery copies +//! kept for the next start. A panic hook writes a report for each, which stays on this machine. +//! None of this helps on the web, where a panic aborts. + +use std::panic::{AssertUnwindSafe, catch_unwind}; + +use egui::{RichText, vec2}; + +use crate::theme::Tokens; +use crate::{CadApp, now_ms}; + +/// Where crash reports go, under the settings folder. +pub const REPORT_DIR: &str = "crash-reports"; +/// This many panics, each within [`QUIET_MS`] of the last, and the interface is given up on. +const MAX_STRIKES: u32 = 3; +/// A stretch this long without a panic means the app has recovered. +const QUIET_MS: f64 = 5_000.0; + +/// Panics caught around the frame. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct UiFault { + /// The last panic's message, until the user dismisses the notice. + pub message: Option, + /// Drawings autosaved because of panics so far. + pub autosaved: usize, + /// Panics in the current run of them. + pub strikes: u32, + /// When the last one happened. + pub last_ms: f64, + /// The interface keeps panicking: it no longer runs, and recovery copies are kept on exit. + pub broken: bool, +} + +impl CadApp { + /// [`CadApp::logic`] under the panic guard. + pub fn guarded_logic(&mut self, ctx: &egui::Context) { + self.guarded(ctx, |app| app.logic(ctx)); + } + + /// [`CadApp::ui`] under the panic guard, then the notice about any panic. Once the interface + /// has been given up on, only the window that closes the app is drawn. + pub fn guarded_ui(&mut self, ui: &mut egui::Ui) { + let ctx = ui.ctx().clone(); + self.guarded(&ctx, |app| app.ui(ui)); + self.show_fault(&ctx); + } + + /// The notice about a caught panic, or the window that closes the app. + pub fn show_fault(&mut self, ctx: &egui::Context) { + if self.fault.broken { + broken_window(self, ctx); + } else { + notice(self, ctx); + } + } + + /// Run `frame`; a panic in it autosaves unsaved drawings instead of ending the app. + pub fn guarded(&mut self, ctx: &egui::Context, frame: impl FnOnce(&mut CadApp)) { + if self.fault.broken { + return; + } + if let Err(p) = catch_unwind(AssertUnwindSafe(|| frame(self))) { + let message = p.downcast_ref::<&str>().map(ToString::to_string).or_else(|| p.downcast_ref::().cloned()); + self.on_panic(ctx, message.unwrap_or_else(|| "panic".into())); + } + } + + fn on_panic(&mut self, ctx: &egui::Context, message: String) { + let now = now_ms(); + let fault = &mut self.fault; + fault.strikes = if now - fault.last_ms < QUIET_MS { fault.strikes + 1 } else { 1 }; + fault.last_ms = now; + fault.broken = fault.strikes >= MAX_STRIKES; + log::error!("the interface panicked: {message}"); + #[cfg(not(target_arch = "wasm32"))] + if let Some(saver) = &mut self.autosave { + let session = &self.session; + // The save is the point of being here: a panic inside it must not end the app either. + self.fault.autosaved += catch_unwind(AssertUnwindSafe(|| saver.emergency_save(session))).unwrap_or(0); + } + self.fault.message = Some(message); + ctx.request_repaint(); + } +} + +/// The notice after a panic. It doesn't block the drawing: the user needs to be able to save. +fn notice(app: &mut CadApp, ctx: &egui::Context) { + let Some(message) = app.fault.message.clone() else { return }; + let t = Tokens::get(); + let mut dismiss = false; + egui::Window::new("Internal error") + .id(egui::Id::unique("cc_fault_notice")) + .collapsible(false) + .resizable(false) + .anchor(egui::Align2::CENTER_TOP, vec2(0.0, 48.0)) + .show(ctx, |ui| { + ui.set_width(440.0); + ui.label("Part of CADCraft failed, and the app kept running."); + ui.label(saved_text(app.fault.autosaved)); + ui.label(RichText::new("Save your work and restart CADCraft.").strong()); + ui.add_space(4.0); + details(ui, t, &message); + // A row just as tall as the button (a plain right-to-left layout takes the rest). + ui.allocate_ui_with_layout(vec2(ui.available_width(), 0.0), egui::Layout::right_to_left(egui::Align::Center), |ui| { + if ui.button("Dismiss").clicked() { + dismiss = true; + } + }); + }); + if dismiss { + app.fault.message = None; + } +} + +/// All that's left once the interface keeps panicking. +fn broken_window(app: &CadApp, ctx: &egui::Context) { + let t = Tokens::get(); + egui::Window::new("CADCraft has to close") + .id(egui::Id::unique("cc_fault_broken")) + .collapsible(false) + .resizable(false) + .anchor(egui::Align2::CENTER_CENTER, vec2(0.0, 0.0)) + .show(ctx, |ui| { + ui.set_width(440.0); + ui.label("Part of CADCraft keeps failing, so it can't continue."); + ui.label(saved_text(app.fault.autosaved)); + ui.label(RichText::new("Drawing Recovery offers them at the next start.").strong()); + ui.add_space(4.0); + if let Some(message) = &app.fault.message { + details(ui, t, message); + } + ui.allocate_ui_with_layout(vec2(ui.available_width(), 0.0), egui::Layout::right_to_left(egui::Align::Center), |ui| { + if ui.button("Close CADCraft").clicked() { + ctx.send_viewport_cmd(egui::ViewportCommand::Close); + } + }); + }); +} + +fn saved_text(autosaved: usize) -> String { + match autosaved { + 0 => "No drawing needed a new recovery copy.".to_string(), + 1 => "A recovery copy of the drawing with unsaved changes was autosaved.".to_string(), + k => format!("Recovery copies of {k} drawings with unsaved changes were autosaved."), + } +} + +fn details(ui: &mut egui::Ui, t: Tokens, message: &str) { + ui.collapsing("Details", |ui| { + ui.label(RichText::new(message).color(t.text_dim).monospace()); + #[cfg(not(target_arch = "wasm32"))] + if let Some(path) = report::last() { + ui.label(RichText::new(format!("Report: {}", path.display())).color(t.text_dim).monospace()); + } + }); +} + +#[cfg(not(target_arch = "wasm32"))] +pub use report::install_report_hook; + +#[cfg(not(target_arch = "wasm32"))] +mod report { + use std::fmt::Write as _; + use std::panic::PanicHookInfo; + use std::path::{Path, PathBuf}; + use std::sync::Mutex; + use std::sync::atomic::{AtomicU32, Ordering}; + use std::time::{SystemTime, UNIX_EPOCH}; + + /// Reports one run may write: a panic every frame must not fill the disk. + const MAX_PER_RUN: u32 = 5; + /// Reports kept in the folder. + const KEEP: usize = 20; + + static WRITTEN: AtomicU32 = AtomicU32::new(0); + static LAST: Mutex> = Mutex::new(None); + + /// The report of the latest panic, once one was written. + pub(super) fn last() -> Option { + LAST.lock().ok().and_then(|g| g.clone()) + } + + /// Write a report into `dir` for every panic, on any thread, before the hook already in + /// place runs. Panics that a guard then catches get one too: they are still bugs. A panic that + /// can't be reported goes on as it would have: a report never makes things worse. + pub fn install_report_hook(dir: PathBuf) { + let previous = std::panic::take_hook(); + std::panic::set_hook(Box::new(move |info| { + if WRITTEN.fetch_add(1, Ordering::Relaxed) < MAX_PER_RUN + && let Ok(path) = write_report(&dir, info) + && let Ok(mut last) = LAST.lock() + { + *last = Some(path); + } + previous(info); + })); + } + + fn write_report(dir: &Path, info: &PanicHookInfo<'_>) -> std::io::Result { + std::fs::create_dir_all(dir)?; + let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default(); + let mut text = String::new(); + let _ = writeln!(text, "CADCraft {} on {} {}", env!("CARGO_PKG_VERSION"), std::env::consts::OS, std::env::consts::ARCH); + let _ = writeln!(text, "Time: {} UTC", crate::autosave::utc(now.as_secs())); + let _ = writeln!(text, "Thread: {}", std::thread::current().name().unwrap_or("unnamed")); + if let Some(l) = info.location() { + let _ = writeln!(text, "Location: {}:{}:{}", l.file(), l.line(), l.column()); + } + let _ = writeln!(text, "Message: {}", info.payload_as_str().unwrap_or("(not text)")); + let _ = writeln!(text, "\n{}", std::backtrace::Backtrace::force_capture()); + let path = dir.join(format!("crash-{:012x}-{}.txt", now.as_millis(), std::process::id())); + cadcraft_engine::atomic_write::write_atomic(&path, text.as_bytes())?; + prune(dir); + Ok(path) + } + + /// Keep the newest [`KEEP`] reports. Names start with the time, so they sort by age. + fn prune(dir: &Path) { + let Ok(entries) = std::fs::read_dir(dir) else { return }; + let mut reports: Vec = entries + .flatten() + .take(10_000) + .map(|e| e.path()) + .filter(|p| p.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with("crash-") && n.ends_with(".txt"))) + .collect(); + reports.sort(); + let extra = reports.len().saturating_sub(KEEP); + for old in reports.drain(..extra) { + let _ = std::fs::remove_file(old); + } + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 03fa5285b..3319e639c 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -32,6 +32,7 @@ pub mod control; pub mod credits; pub mod dialogs; pub mod docking; +pub mod fault; pub mod hatchdlg; mod mtext_editor; mod panel_state; @@ -305,6 +306,8 @@ pub struct CadApp { pub gpu_faults: std::sync::Arc, /// The notice shown after one (see [`autosave::gpu_notice`]). pub gpu_notice: Option, + /// Panics caught around the frame ([`fault`]). + pub fault: fault::UiFault, /// The notice shown while autosave fails (see [`autosave::failure_notice`]). pub autosave_notice: Option, /// The Command Aliases dialog while open ([`aliasdlg`]). @@ -366,6 +369,7 @@ impl CadApp { recovery: autosave::Recovery::default(), gpu_faults: std::sync::Arc::default(), gpu_notice: None, + fault: fault::UiFault::default(), autosave_notice: None, alias_dlg: None, hatch_dlg: None, diff --git a/crates/ui-egui/tests/ui_faults.rs b/crates/ui-egui/tests/ui_faults.rs new file mode 100644 index 000000000..3c382e38f --- /dev/null +++ b/crates/ui-egui/tests/ui_faults.rs @@ -0,0 +1,97 @@ +//! A panic in the interface keeps the user's work: unsaved drawings are autosaved at once, the +//! app says so and writes a report, and a panic that keeps coming back leaves only a way to close +//! with the recovery copies kept. The panic hook is process-wide, so everything runs in one test. + +use cadcraft_engine::Session; +use cadcraft_engine::cmd::file::{IoHooks, set_io}; +use cadcraft_engine::doc::Drawing; +use cadcraft_ui_egui::{CadApp, Services}; + +fn panic_in_a_nested_ui(app: &mut CadApp, ctx: &egui::Context, text: &'static str) -> String { + let mut painted = String::new(); + let mut out = ctx.run_ui(egui::RawInput::default(), |ui| { + let ctx = ui.ctx().clone(); + app.guarded(&ctx, |_| { + egui::CentralPanel::default().show(ui, |ui| { + ui.vertical(|ui| { + ui.label("before"); + std::panic::panic_any(text); + }); + }); + }); + app.show_fault(ui.ctx()); + }); + out.textures_delta.clear(); // no renderer here + out.shapes.iter().for_each(|s| collect(&s.shape, &mut painted)); + painted +} + +fn painted_text(app: &mut CadApp, ctx: &egui::Context) -> String { + let mut painted = String::new(); + for _ in 0..2 { + let mut out = ctx.run_ui(egui::RawInput::default(), |ui| app.show_fault(ui.ctx())); + out.textures_delta.clear(); // no renderer here + painted.clear(); + out.shapes.iter().for_each(|s| collect(&s.shape, &mut painted)); + } + painted +} + +fn collect(shape: &egui::Shape, text: &mut String) { + match shape { + egui::Shape::Text(t) => { + text.push_str(t.galley.text()); + text.push('\n'); + } + egui::Shape::Vec(v) => v.iter().for_each(|s| collect(s, text)), + _ => {} + } +} + +fn recovery_copies(dir: &std::path::Path) -> usize { + std::fs::read_dir(dir).map(|d| d.flatten().filter(|e| e.path().extension().is_some_and(|x| x == "sv$")).count()).unwrap_or(0) +} + +#[test] +fn a_panic_saves_the_work_and_reports_and_repeated_ones_close_safely() { + set_io(IoHooks { read: |_, _| Ok(Drawing::new_metric()), write: |_, _| Ok(b"0\nEOF\n".to_vec()), plot: None }); + let root = std::env::temp_dir().join(format!("cadcraft-ui-faults-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + let (auto, reports) = (root.join("autosave"), root.join("crash-reports")); + cadcraft_ui_egui::fault::install_report_hook(reports.clone()); + + let mut app = CadApp::new(Session::new(), Services::default()); + app.enable_autosave(&auto); + app.run("circle", serde_json::json!({"center": [0, 0], "radius": 1})).unwrap(); + let ctx = egui::Context::default(); + + // One panic: the drawing has a whole recovery copy by the time the frame goes on. + panic_in_a_nested_ui(&mut app, &ctx, "a bug in a panel"); + assert_eq!(app.fault.message.as_deref(), Some("a bug in a panel")); + assert_eq!((app.fault.autosaved, app.fault.broken), (1, false)); + assert_eq!(recovery_copies(&auto), 1); + let text = painted_text(&mut app, &ctx); + assert!(text.contains("kept running") && text.contains("recovery copy"), "{text}"); + + let report = std::fs::read_dir(&reports).unwrap().flatten().next().expect("a report").path(); + let report = std::fs::read_to_string(report).unwrap(); + assert!(report.contains("Message: a bug in a panel") && report.contains("ui_faults.rs"), "{report}"); + assert!(report.starts_with("CADCraft "), "{report}"); + + // The app still works after it. + app.run("circle", serde_json::json!({"center": [5, 0], "radius": 1})).unwrap(); + + // Panics that keep coming: the interface stops running and only offers to close. + panic_in_a_nested_ui(&mut app, &ctx, "again"); + panic_in_a_nested_ui(&mut app, &ctx, "and again"); + assert!(app.fault.broken); + let mut ran = false; + app.guarded(&ctx, |_| ran = true); + assert!(!ran, "a broken interface isn't run again"); + assert!(painted_text(&mut app, &ctx).contains("Drawing Recovery offers them")); + + // Closing now keeps the copies for the next start. + app.shutdown_autosave(); + assert_eq!(recovery_copies(&auto), 1); + let _ = std::fs::remove_dir_all(&root); +} From 806654aedfea7bd9a7dcc2e978b7a2d46ccacc0d Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 18:24:51 -0700 Subject: [PATCH 083/100] Add a security policy (E33) (#2672) --- SECURITY.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 SECURITY.md diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 000000000..635ab902e --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,34 @@ +# Security policy + +## Reporting a vulnerability + +Please report it privately through [GitHub's private reporting](https://github.com/storytold/cadcraft/security/advisories/new), not in a public issue or pull request. Say which version you ran, on which platform, and how to reproduce it. A small sample file helps, as long as it holds nothing private. + +Maintainers read reports as they arrive, but there is no fixed response time yet. + +## Supported versions + +Only the latest release gets security fixes. CADCraft is pre-alpha and moves quickly, so please check that a problem still exists there before reporting it. + +## What counts + +CADCraft treats these as untrusted, and a way past these defences is a vulnerability: + +- **Drawing files.** Opening a DXF, DWG or other file must not run code, write outside the paths the user chose, or crash the app. Panics are bugs too, but memory exhaustion or a decoder abort on a crafted file is the more serious case. +- **AutoLISP in a drawing's folder.** A drawing must not run code that the user didn't choose to load. +- **The control channel.** It listens on loopback only and needs the session token from an owner-only file on every request. Another local account reaching it without that token is a vulnerability. +- **Temporary and autosave files.** Autosave files are unsaved drawings: another account must not be able to read them or redirect a write through them. +- **Updates.** A release must not be replaceable by someone other than the project. + +## Known limits + +These are known and being worked on, so they don't need a report: + +- Agents connected through MCP or the control channel can read and write any path the user can. There are no workspace roots yet. +- A local process can make the control channel buffer up to 512 MiB before its token is checked. The total is capped, so this can slow the app but not exhaust memory without limit. +- The DWG decoder runs inside the app, so a decoder abort ends every open drawing. Autosave limits what's lost. +- The web build has no autosave yet. + +## What stays on your machine + +Crash reports are written to the `crash-reports` folder in the settings folder and are never uploaded. They hold the version, the platform, the panic message and a stack trace, and a message can include a file path, so read a report before you attach it anywhere. From f6d84e28f31ff98224a2b63b04e5153f4899ae2b Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 18:47:29 -0700 Subject: [PATCH 084/100] DWG reader and writer run in a child process (E7) (#2673) * DWG reader and writer run in a child process (E7) An allocation failure or stack overflow in the third-party code ends its process, which catch_unwind can't stop, so one bad file took every open drawing with it. The app and CLI now run them in a copy of themselves over pipes. A worker that dies or runs past 30 minutes costs that one open or save. If no worker can be started, the work stays in the process, as before. * DWG worker: keep the reply's bytes instead of copying them; skip the test without a bridge --- Cargo.lock | 1 + SECURITY.md | 2 +- apps/cadcraft-cli/src/main.rs | 5 + apps/cadcraft-cli/tests/dwg_worker.rs | 33 +++ apps/cadcraft/src/main.rs | 5 + crates/dwg/Cargo.toml | 3 + crates/dwg/src/lib.rs | 29 ++- crates/dwg/src/worker.rs | 311 ++++++++++++++++++++++++++ crates/io/src/lib.rs | 11 +- 9 files changed, 393 insertions(+), 7 deletions(-) create mode 100644 apps/cadcraft-cli/tests/dwg_worker.rs create mode 100644 crates/dwg/src/worker.rs diff --git a/Cargo.lock b/Cargo.lock index 98d98880f..c2f4ce914 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -691,6 +691,7 @@ name = "cadcraft-dwg" version = "0.17.0" dependencies = [ "acadrust", + "log", ] [[package]] diff --git a/SECURITY.md b/SECURITY.md index 635ab902e..fbad5435b 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -26,7 +26,7 @@ These are known and being worked on, so they don't need a report: - Agents connected through MCP or the control channel can read and write any path the user can. There are no workspace roots yet. - A local process can make the control channel buffer up to 512 MiB before its token is checked. The total is capped, so this can slow the app but not exhaust memory without limit. -- The DWG decoder runs inside the app, so a decoder abort ends every open drawing. Autosave limits what's lost. +- The DWG worker process has no memory limit of its own yet, so a crafted file can use a lot of memory before it stops. It ends that one open or save, not the app. - The web build has no autosave yet. ## What stays on your machine diff --git a/apps/cadcraft-cli/src/main.rs b/apps/cadcraft-cli/src/main.rs index 7561b96b5..866923c9a 100644 --- a/apps/cadcraft-cli/src/main.rs +++ b/apps/cadcraft-cli/src/main.rs @@ -1,6 +1,11 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] fn main() { + // A copy of this program started to read or write a DWG file does only that. + if let Some(code) = cadcraft_io::dwg_worker::serve_if_asked() { + std::process::exit(code); + } + cadcraft_io::dwg_worker::use_own_worker(); // `args_os`, not `args`: a non-Unicode argument (a Latin-1 file name on Linux) must be // reported as an error, not panic before anything is printed. let code = cadcraft_cli::run(std::env::args_os().skip(1), &mut std::io::stdout(), &mut std::io::stderr()); diff --git a/apps/cadcraft-cli/tests/dwg_worker.rs b/apps/cadcraft-cli/tests/dwg_worker.rs new file mode 100644 index 000000000..8656b84ff --- /dev/null +++ b/apps/cadcraft-cli/tests/dwg_worker.rs @@ -0,0 +1,33 @@ +//! The DWG reader and writer run in a child process, so a decoder that dies ends one open or save, +//! not the app. The worker setting is process-wide, so everything runs in one test. + +#![cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))] + +use cadcraft_io::dwg_worker; + +#[test] +fn a_real_worker_reads_and_writes_and_its_death_is_an_error_here() { + let drawing = cadcraft_engine::sample::bracket(); + let in_process = cadcraft_io::write(&drawing, "a.dwg").unwrap(); + + dwg_worker::use_worker(env!("CARGO_BIN_EXE_cadcraft-cli").into(), vec![dwg_worker::WORKER_ARG.into()]); + let written = cadcraft_io::write(&drawing, "a.dwg").unwrap(); + assert_eq!(written.get(..6), in_process.get(..6), "the same version of file"); + let read = cadcraft_io::read(&written, "a.dwg").unwrap(); + assert_eq!(read.model.len(), drawing.model.len()); + + // A decode error comes back as that error, not as the worker stopping. + let mut bad = in_process.clone(); + bad.truncate(64); + let e = cadcraft_io::read(&bad, "a.dwg").unwrap_err().to_string(); + assert!(!e.contains("worker"), "{e}"); + + // The worker aborts as the third-party code can. This process goes on. + let e = dwg_worker::crash_worker_for_test().unwrap_err(); + assert!(e.contains("worker stopped"), "{e}"); + assert!(cadcraft_io::read(&written, "a.dwg").is_ok(), "the next file gets a new worker"); + + // No worker to start: the work happens here, as without one. + dwg_worker::use_worker("no-such-program-here".into(), Vec::new()); + assert!(cadcraft_io::read(&written, "a.dwg").is_ok()); +} diff --git a/apps/cadcraft/src/main.rs b/apps/cadcraft/src/main.rs index 4b124ad1d..71700095d 100644 --- a/apps/cadcraft/src/main.rs +++ b/apps/cadcraft/src/main.rs @@ -258,6 +258,11 @@ fn version_string() -> String { /// Parse CLI options (`--control`, `--sample`, files, `--version`), install logging and I/O hooks, build the services and app, and run eframe. fn main() -> eframe::Result { + // A copy of this program started to read or write a DWG file does only that. + if let Some(code) = cadcraft_io::dwg_worker::serve_if_asked() { + std::process::exit(code); + } + cadcraft_io::dwg_worker::use_own_worker(); // First, so every start-up record is captured; see `logging`. let logger = logging::install(); // The update helper relaunches this binary after a self-update; it runs and exits here. diff --git a/crates/dwg/Cargo.toml b/crates/dwg/Cargo.toml index acb8d0aa7..23d1c82d2 100644 --- a/crates/dwg/Cargo.toml +++ b/crates/dwg/Cargo.toml @@ -8,6 +8,9 @@ license.workspace = true [lints] workspace = true +[dependencies] +log = { workspace = true } + # The same targets as build.rs's `no_dwg_bridge` leave acadrust out: wasm32, and FreeBSD aarch64, # where acadrust 0.6.6 passes a `*const c_char` (u8 there) to its own `*const i8` sysctlbyname. # 0.6.7 and 0.6.8 fix that but read some damaged DWGs as empty with no note (tests.rs, #800). diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index e5463aaec..70646a19a 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -19,10 +19,8 @@ //! crashing. //! //! These bound the input- and output-size variants. A small file that declares a huge internal -//! table or section can still make the dependency allocate before any size is known; fully fixing -//! that requires an upstream fix in `acadrust` (validate declared sizes against a decoded-memory -//! budget) or running the decode in a separate worker with an OS memory limit. The limits here -//! keep the blast radius bounded in the meantime. +//! table or section can still make the dependency allocate before any size is known, so the app +//! runs both directions in a child process ([`worker`]): what that kills is the child. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] #![forbid(unsafe_code)] @@ -64,6 +62,11 @@ fn mb(n: u64) -> String { if m >= 100.0 { format!("{m:.0} MB") } else { format!("{m:.1} MB") } } +#[cfg(not(no_dwg_bridge))] +pub mod worker; +#[cfg(not(no_dwg_bridge))] +pub use worker::{WORKER_ARG, crash_worker_for_test, decode, encode_as, serve_if_asked, use_own_worker, use_worker}; + #[cfg(not(no_dwg_bridge))] mod native { use std::io::{Cursor, Write}; @@ -930,6 +933,24 @@ fn unsupported(version: &str) -> String { #[cfg(no_dwg_bridge)] const NO_BRIDGE_BUILD: &str = if cfg!(target_arch = "wasm32") { "web" } else { "FreeBSD aarch64" }; +/// There's no worker without a bridge. +#[cfg(no_dwg_bridge)] +pub const WORKER_ARG: &str = "--dwg-worker"; +#[cfg(no_dwg_bridge)] +pub fn serve_if_asked() -> Option { + None +} +#[cfg(no_dwg_bridge)] +pub fn use_own_worker() {} +#[cfg(no_dwg_bridge)] +pub fn decode(bytes: &[u8], limits: Limits) -> Result<(Vec, Vec), String> { + dwg_to_dxf_noted(bytes, limits) +} +#[cfg(no_dwg_bridge)] +pub fn encode_as(dxf: &[u8], version: &str) -> Result, String> { + dxf_to_dwg_as(dxf, version) +} + #[cfg(no_dwg_bridge)] pub fn dwg_to_dxf(_bytes: &[u8]) -> Result, String> { Err(format!("DWG files can't be opened in the {NO_BRIDGE_BUILD} build yet; save as DXF")) diff --git a/crates/dwg/src/worker.rs b/crates/dwg/src/worker.rs new file mode 100644 index 000000000..5ccf5cf6a --- /dev/null +++ b/crates/dwg/src/worker.rs @@ -0,0 +1,311 @@ +//! The DWG reader and writer in a child process. +//! +//! An allocation failure or stack overflow in the third-party code ends the process it runs in, and +//! `catch_unwind` can't stop that. So the app runs them in a copy of itself: [`use_own_worker`] says +//! how to start one, and [`serve_if_asked`], first thing in `main`, turns the copy into a worker +//! that reads one request from standard input and writes one reply to standard output. A file that +//! kills the worker then costs that one open or save, and every other open drawing stays safe. +//! +//! Without a worker (libraries, tests, a build that can't start one) [`decode`] and [`encode_as`] +//! run in the calling process, as they always did. There's no memory limit on the worker yet: that +//! takes per-platform system calls, which `unsafe_code = "forbid"` keeps out of this crate. +//! +//! A reply comes from a process that just handled a possibly hostile file, so it is parsed as +//! untrusted input. + +use std::ffi::OsString; +use std::io::{Read, Write}; +use std::path::PathBuf; +use std::process::{Command, Stdio}; +use std::sync::Mutex; +use std::sync::mpsc::{RecvTimeoutError, channel}; +use std::time::Duration; + +use super::Limits; + +/// The argument that makes the app a worker. +pub const WORKER_ARG: &str = "--dwg-worker"; +/// Bumped when the messages change: an app updated while running may start a newer copy. +const PROTOCOL: u8 = 1; +const DECODE: u8 = 1; +const ENCODE: u8 = 2; +const CRASH: u8 = 0xff; +/// How long one file may take before the worker is stopped. It can't be interrupted otherwise. +const TIMEOUT: Duration = Duration::from_secs(30 * 60); +/// Most bytes accepted for what a reply carries besides the drawing. +const MAX_NOTES_BYTES: u64 = 64 << 20; +/// Most bytes accepted as a written DWG. +const MAX_DWG_REPLY: u64 = 2 << 30; +/// Most bytes one request holds: the largest input, whichever the direction, and its header. +const MAX_REQUEST: u64 = (2 << 30) + 256; + +struct Worker { + program: PathBuf, + args: Vec, +} + +static WORKER: Mutex> = Mutex::new(None); + +/// Start `program args` as the worker from now on. `program` must call [`serve_if_asked`]. +pub fn use_worker(program: PathBuf, args: Vec) { + if let Ok(mut w) = WORKER.lock() { + *w = Some(Worker { program, args }); + } +} + +/// Use a copy of this program as the worker, started with [`WORKER_ARG`]. If its path can't be +/// found, the reader and writer stay in this process. +pub fn use_own_worker() { + match std::env::current_exe() { + Ok(program) => use_worker(program, vec![WORKER_ARG.into()]), + Err(e) => log::warn!("DWG worker: can't find this program: {e}"), + } +} + +/// Serve one request and return the exit code if this process was started as a worker. +pub fn serve_if_asked() -> Option { + (std::env::args_os().nth(1)? == WORKER_ARG).then(|| serve(std::io::stdin().lock(), std::io::stdout().lock())) +} + +/// [`super::dwg_to_dxf_noted`] in the worker, when there is one. +pub fn decode(bytes: &[u8], limits: Limits) -> Result<(Vec, Vec), String> { + // Before starting a process, and so the same limit holds with or without one. + if bytes.len() > limits.max_dwg_bytes { + return super::dwg_to_dxf_noted(bytes, limits); + } + let reply_limit = (limits.max_dxf_bytes as u64).saturating_add(MAX_NOTES_BYTES); + match call(&request(DECODE, limits, ""), bytes, reply_limit, "reading this file") { + Some(reply) => reply?, + None => super::dwg_to_dxf_noted(bytes, limits), + } +} + +/// [`super::dxf_to_dwg_as`] in the worker, when there is one. +pub fn encode_as(dxf: &[u8], version: &str) -> Result, String> { + if u8::try_from(version.len()).is_err() { + return Err(super::unsupported(version)); + } + match call(&request(ENCODE, Limits::DEFAULT, version), dxf, MAX_DWG_REPLY, "writing this file") { + Some(reply) => reply?.map(|(dwg, _)| dwg), + None => super::dxf_to_dwg_as(dxf, version), + } +} + +/// Make the worker abort, as the third-party code can: what the tests prove isolation with. +#[doc(hidden)] +pub fn crash_worker_for_test() -> Result<(), String> { + call(&request(CRASH, Limits::DEFAULT, ""), &[], 1024, "testing").ok_or("no worker")??.map(|_| ()) +} + +fn request(op: u8, limits: Limits, version: &str) -> Vec { + let mut r = vec![PROTOCOL, op]; + r.extend((limits.max_dwg_bytes as u64).to_le_bytes()); + r.extend((limits.max_dxf_bytes as u64).to_le_bytes()); + r.push(u8::try_from(version.len()).unwrap_or(0)); + r.extend(version.as_bytes()); + r +} + +type Reply = Result<(Vec, Vec), String>; + +/// Run one request in a worker. `None`: none could be started, so the caller does the work. +/// `Err`: the worker went wrong, and `doing` says at what, for the user. +fn call(header: &[u8], payload: &[u8], reply_limit: u64, doing: &str) -> Option> { + let mut command = { + let worker = WORKER.lock().ok()?; + let worker = worker.as_ref()?; + let mut c = Command::new(&worker.program); + c.args(&worker.args); + c + }; + command.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()); + #[cfg(windows)] + { + use std::os::windows::process::CommandExt; + const CREATE_NO_WINDOW: u32 = 0x0800_0000; + command.creation_flags(CREATE_NO_WINDOW); + } + let mut child = match command.spawn() { + Ok(c) => c, + Err(e) => { + log::warn!("DWG worker: can't start it: {e}"); + return None; + } + }; + let (Some(mut to_child), Some(from_child)) = (child.stdin.take(), child.stdout.take()) else { + let _ = child.kill(); + let _ = child.wait(); + return None; + }; + let received = std::thread::scope(|scope| { + // Written beside the read: a worker that replies before it has read everything, or dies, + // must block neither side. A failed write shows up as the reply going wrong. + scope.spawn(move || { + let _ = to_child.write_all(header).and_then(|()| to_child.write_all(payload)); + }); + let (tx, rx) = channel(); + scope.spawn(move || { + let mut reply = Vec::new(); + let read = from_child.take(reply_limit).read_to_end(&mut reply); + let _ = tx.send(read.map(|_| reply)); + }); + let received = rx.recv_timeout(TIMEOUT); + if matches!(received, Err(RecvTimeoutError::Timeout)) { + let _ = child.kill(); + } + received + }); + let status = child.wait(); + let stopped = || format!("the DWG worker stopped while {doing}; nothing else is affected"); + Some(match (received, status) { + (Err(RecvTimeoutError::Timeout), _) => Err(format!("{doing} took too long, so the DWG worker was stopped; nothing else is affected")), + (Ok(Ok(reply)), Ok(status)) if status.success() => parse_reply(reply).ok_or_else(stopped), + _ => Err(stopped()), + }) +} + +/// Take `n` bytes off the front of `b`. +fn take<'a>(b: &mut &'a [u8], n: usize) -> Option<&'a [u8]> { + let (head, tail) = b.split_at_checked(n)?; + *b = tail; + Some(head) +} + +fn take_u32(b: &mut &[u8]) -> Option { + let bytes: [u8; 4] = take(b, 4)?.try_into().ok()?; + usize::try_from(u32::from_le_bytes(bytes)).ok() +} + +fn take_u64(b: &mut &[u8]) -> Option { + Some(u64::from_le_bytes(take(b, 8)?.try_into().ok()?)) +} + +/// A reply: 0, the note count, each note with its length, then the bytes with their length; or 1 +/// and a message. `None` when it isn't one, whole. Takes the reply, because the bytes can be most +/// of the memory the app may use: they are kept, not copied. +fn parse_reply(mut reply: Vec) -> Option { + let total = reply.len(); + let mut b = &reply[..]; + match *take(&mut b, 1)?.first()? { + 0 => { + let count = take_u32(&mut b)?; + let mut notes = Vec::new(); + for _ in 0..count { + let n = take_u32(&mut b)?; + notes.push(String::from_utf8_lossy(take(&mut b, n)?).into_owned()); + } + let n = usize::try_from(take_u64(&mut b)?).ok()?; + if b.len() != n { + return None; + } + reply.drain(..total - n); + Some(Ok((reply, notes))) + } + 1 => Some(Err(String::from_utf8_lossy(b).into_owned())), + _ => None, + } +} + +fn write_reply(out: &mut impl Write, reply: Reply) -> std::io::Result<()> { + match reply { + Ok((bytes, notes)) => { + out.write_all(&[0])?; + out.write_all(&u32::try_from(notes.len()).unwrap_or(0).to_le_bytes())?; + for note in ¬es { + out.write_all(&u32::try_from(note.len()).unwrap_or(0).to_le_bytes())?; + if u32::try_from(note.len()).is_ok() { + out.write_all(note.as_bytes())?; + } + } + out.write_all(&(bytes.len() as u64).to_le_bytes())?; + out.write_all(&bytes)?; + } + Err(e) => { + out.write_all(&[1])?; + out.write_all(e.as_bytes())?; + } + } + out.flush() +} + +/// What a request asks for, or why it can't be served. +fn handle(mut b: &[u8]) -> Reply { + let bad = || "the DWG worker got a request it can't read (a different CADCraft version?)".to_string(); + if take(&mut b, 1).and_then(<[u8]>::first) != Some(&PROTOCOL) { + return Err(bad()); + } + let op = take(&mut b, 1).and_then(<[u8]>::first).copied().ok_or_else(bad)?; + let max_dwg_bytes = take_u64(&mut b).and_then(|n| usize::try_from(n).ok()).ok_or_else(bad)?; + let max_dxf_bytes = take_u64(&mut b).and_then(|n| usize::try_from(n).ok()).ok_or_else(bad)?; + let version_len = take(&mut b, 1).and_then(<[u8]>::first).copied().ok_or_else(bad)?; + let version = take(&mut b, usize::from(version_len)).and_then(|v| std::str::from_utf8(v).ok()).ok_or_else(bad)?; + match op { + DECODE => super::dwg_to_dxf_noted(b, Limits { max_dwg_bytes, max_dxf_bytes }), + ENCODE => super::dxf_to_dwg_as(b, version).map(|dwg| (dwg, Vec::new())), + CRASH => std::process::abort(), + _ => Err(bad()), + } +} + +/// Serve the one request on `input`. The exit code is 0 unless the reply couldn't be sent. +fn serve(input: impl Read, mut output: impl Write) -> i32 { + let mut request = Vec::new(); + let reply = match input.take(MAX_REQUEST).read_to_end(&mut request) { + Ok(_) => handle(&request), + Err(e) => Err(format!("the DWG worker could not read its request: {e}")), + }; + i32::from(write_reply(&mut output, reply).is_err()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn wire(reply: Reply) -> Vec { + let mut out = Vec::new(); + write_reply(&mut out, reply).unwrap(); + out + } + + #[test] + fn replies_round_trip() { + let ok: Reply = Ok((b"bytes".to_vec(), vec!["one".into(), "two é".into()])); + assert_eq!(parse_reply(wire(ok.clone())), Some(ok)); + let failed: Reply = Err("DWG: no".into()); + assert_eq!(parse_reply(wire(failed.clone())), Some(failed)); + } + + #[test] + fn a_reply_cut_short_or_made_up_is_refused() { + let whole = wire(Ok((b"bytes".to_vec(), vec!["note".into()]))); + for n in 0..whole.len() { + assert_eq!(parse_reply(whole[..n].to_vec()), None, "cut at {n}"); + } + assert_eq!(parse_reply(vec![7, 0, 0]), None); + // A note count far beyond what follows ends at the first missing note. + assert_eq!(parse_reply(vec![0, 0xff, 0xff, 0xff, 0xff]), None); + assert_eq!(parse_reply(vec![0, 1, 0, 0, 0, 0xff, 0xff, 0xff, 0xff]), None); + } + + #[test] + fn a_request_it_cannot_read_is_an_error_not_a_crash() { + for request in [ + &[][..], + &[PROTOCOL + 1, DECODE], + &[PROTOCOL], + &[PROTOCOL, DECODE, 1, 2, 3], + &[PROTOCOL, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + ] { + assert!(handle(request).is_err(), "{request:?}"); + } + } + + #[test] + fn serving_reports_a_decode_failure_as_a_reply() { + let mut request = self::request(DECODE, Limits::DEFAULT, ""); + request.extend(b"AC1032 not really a drawing"); + let mut out = Vec::new(); + assert_eq!(serve(&request[..], &mut out), 0); + assert!(matches!(parse_reply(out.clone()), Some(Err(_))), "{out:?}"); + } +} diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rs index ccfba201e..4863ddb34 100644 --- a/crates/io/src/lib.rs +++ b/crates/io/src/lib.rs @@ -55,6 +55,13 @@ pub enum IoError { pub type Result = std::result::Result; +/// How a program runs the DWG reader and writer in a child process (`cadcraft_dwg::worker`). +pub mod dwg_worker { + pub use cadcraft_dwg::{WORKER_ARG, serve_if_asked, use_own_worker}; + #[cfg(all(not(target_arch = "wasm32"), not(all(target_os = "freebsd", target_arch = "aarch64"))))] + pub use cadcraft_dwg::{crash_worker_for_test, use_worker}; +} + fn ext(name: impl AsRef) -> String { name.as_ref().extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default() } @@ -64,7 +71,7 @@ fn ext(name: impl AsRef) -> String { pub fn read(bytes: &[u8], name: impl AsRef) -> Result { let name = name.as_ref(); if cadcraft_dwg::is_dwg(bytes) { - let (dxf, notes) = cadcraft_dwg::dwg_to_dxf_noted(bytes, cadcraft_dwg::Limits::DEFAULT).map_err(IoError::Format)?; + let (dxf, notes) = cadcraft_dwg::decode(bytes, cadcraft_dwg::Limits::DEFAULT).map_err(IoError::Format)?; let mut d = dxf_read::read(&dxf).map_err(|e| IoError::Format(format!("{e} (in the DXF converted from this DWG)")))?; d.read_notes = notes; return Ok(d); @@ -106,7 +113,7 @@ pub fn write_with(d: &Drawing, name: impl AsRef, opts: &SaveOptions) -> Re // gradients, so a drawing from an R2000 DXF doesn't lose them by default. Some(Format::Dwg) if !opts.binary => { let version = opts.version.unwrap_or_else(|| version.max(Version::R2004)); - cadcraft_dwg::dxf_to_dwg_as(dxf_write::dwg_bridge(d, version).as_bytes(), version.code()).map_err(IoError::Format) + cadcraft_dwg::encode_as(dxf_write::dwg_bridge(d, version).as_bytes(), version.code()).map_err(IoError::Format) } _ if opts.binary => Err(IoError::Format(format!("binary output is for DXF files, not `{}`", ext(name)))), _ => write_framed(d, name, None), From 44823163063ad33dc9593dfe381f87cdf0b1c629 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 2 Nov 2026 21:16:44 -0500 Subject: [PATCH 085/100] constraints: find the next default parameter name in one pass (#2674) next_name tested up to 100,000 candidate names, each against every parameter and constraint name, so a drawing with many occupied names made adding one dimensional constraint quadratic. Collect the numeric suffixes in use once and take the first free one. --- crates/constraints/src/lib.rs | 20 ++++++++++++++++++-- crates/constraints/src/tests.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/crates/constraints/src/lib.rs b/crates/constraints/src/lib.rs index 89c6276cd..0bf230323 100644 --- a/crates/constraints/src/lib.rs +++ b/crates/constraints/src/lib.rs @@ -30,7 +30,7 @@ mod lm; mod model; mod residual; -use std::collections::{BTreeMap, HashMap}; +use std::collections::{BTreeMap, HashMap, HashSet}; use cadcraft_doc::{Constraint, ConstraintKind, Drawing, GeomRef, Handle}; use cadcraft_geom::Vec2; @@ -169,10 +169,26 @@ pub fn name_in_use(d: &Drawing, name: &str) -> bool { d.parametric.parameters.iter().any(|p| p.name == name) || d.constraints.iter().any(|c| c.name == name) } +/// Highest numbered default name [`next_name`] tries. +const MAX_NAME_SUFFIX: u32 = 100_000; + /// Next free parameter name for a dimensional constraint kind (`d1`, `rad2`, `ang1`, …). pub fn next_name(d: &Drawing, kind: ConstraintKind) -> String { let prefix = kind.name_prefix(); - (1..=100_000).map(|i| format!("{prefix}{i}")).find(|n| !name_in_use(d, n)).unwrap_or_else(|| format!("{prefix}_x")) + // Collect the suffixes in use in one pass, then take the first free one: checking every + // candidate against every name is quadratic in the number of names (CWE-407). Only a + // suffix spelled as `format!("{prefix}{i}")` spells it counts (`d07` does not take `d7`). + let taken: HashSet = d + .parametric + .parameters + .iter() + .map(|p| p.name.as_str()) + .chain(d.constraints.iter().map(|c| c.name.as_str())) + .filter_map(|n| n.strip_prefix(prefix)) + .filter(|s| !s.starts_with('0') && s.bytes().all(|b| b.is_ascii_digit())) + .filter_map(|s| s.parse::().ok()) + .collect(); + (1..=MAX_NAME_SUFFIX).find(|i| !taken.contains(i)).map_or_else(|| format!("{prefix}_x"), |i| format!("{prefix}{i}")) } /// Next free constraint id. diff --git a/crates/constraints/src/tests.rs b/crates/constraints/src/tests.rs index 35b73fea2..eadc7a3c2 100644 --- a/crates/constraints/src/tests.rs +++ b/crates/constraints/src/tests.rs @@ -489,3 +489,30 @@ fn arc_solved_with_a_negative_radius_keeps_its_points() { } } } + +/// The default name is the first free numbered one, found in one pass over many names in use. +#[test] +fn next_name_skips_names_in_use() { + let mut d = Drawing::default(); + assert_eq!(next_name(&d, ConstraintKind::Radius), "rad1"); + for i in (1..=20_000).filter(|i| *i != 15_000) { + d.parametric.parameters.push(cadcraft_doc::Parameter { name: format!("d{i}"), expr: "1".into(), description: String::new() }); + } + assert_eq!(next_name(&d, ConstraintKind::Distance(DistAxis::Aligned)), "d15000"); + d.parametric.parameters.push(cadcraft_doc::Parameter { name: "d15000".into(), expr: "1".into(), description: String::new() }); + assert_eq!(next_name(&d, ConstraintKind::Distance(DistAxis::Aligned)), "d20001"); + // A differently spelled suffix does not occupy the plain name. + let mut d = Drawing::default(); + for name in ["d01", "d+1", "d1x", "D1"] { + d.parametric.parameters.push(cadcraft_doc::Parameter { name: name.into(), expr: "1".into(), description: String::new() }); + } + assert_eq!(next_name(&d, ConstraintKind::Distance(DistAxis::Aligned)), "d1"); + d.constraints.push(Constraint { + id: 1, + kind: ConstraintKind::Distance(DistAxis::Aligned), + refs: Vec::new(), + name: "d1".into(), + expr: "1".into(), + }); + assert_eq!(next_name(&d, ConstraintKind::Distance(DistAxis::Aligned)), "d2"); +} From 0d3b3d21c0d872b1ae6720d75ef4909f3be2e51d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 2 Nov 2026 21:16:51 -0500 Subject: [PATCH 086/100] Regions: test a loop's probe only against loops whose box holds it, within a budget (#2680) Region::renest tested every loop's probe point against every other tessellated loop, scanning all of its edges each time, so the work was the loop count times the total vertex count. It now skips loops whose bounding box does not hold the probe, and stops once MAX_RENEST_EDGE_VISITS edge tests have been spent, leaving the flags unchanged and returning false; UNION, SUBTRACT and INTERSECT then report the regions as too complex to combine, as they already do for too many boundary pairs (CWE-407). --- crates/doc/src/entity/region.rs | 80 +++++++++++++++++++++--- crates/engine/src/cmd/inquiry/regions.rs | 4 +- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/crates/doc/src/entity/region.rs b/crates/doc/src/entity/region.rs index 0d2594ea8..8c63b43bf 100644 --- a/crates/doc/src/entity/region.rs +++ b/crates/doc/src/entity/region.rs @@ -9,6 +9,9 @@ use super::HatchLoop; /// Most loops one region has (hostile files and edits). pub const MAX_REGION_LOOPS: usize = 10_000; +/// Most polygon edges [`Region::renest`] tests, over all its point-in-loop tests. +pub const MAX_RENEST_EDGE_VISITS: usize = 50_000_000; + #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct Region { /// Closed loops with bulges; `outer` false for islands. @@ -76,20 +79,49 @@ impl Region { } /// Set each loop's `outer` flag from how deeply other loops enclose it: an even depth is - /// an outer loop, odd an island. - pub fn renest(&mut self) { + /// an outer loop, odd an island. `false`, with the flags left as they were, when that takes + /// more than [`MAX_RENEST_EDGE_VISITS`] polygon edge tests. + #[must_use] + pub fn renest(&mut self) -> bool { let polys = self.tessellate(self.fine_tol()); - let depth: Vec = polys + // A loop's probe can only be inside loops whose box holds it: test only those. A box with + // a non-finite corner says nothing (an edge through a non-finite point never counts, so + // such a polygon need not be closed), so its loop is always tested. + let boxes: Vec> = polys .iter() - .enumerate() - .map(|(i, l)| { - let Some(p) = probe(l) else { return 0 }; - polys.iter().enumerate().filter(|(j, o)| *j != i && point_in_polygon(o, p)).count() + .map(|l| { + let b = Bounds2::from_points(l.iter().copied()); + (l.iter().all(|p| p.is_finite()) && b.min.is_finite() && b.max.is_finite()).then_some(b) }) .collect(); + let mut visits = 0usize; + let mut depth = Vec::with_capacity(polys.len()); + for (i, l) in polys.iter().enumerate() { + let Some(p) = probe(l) else { + depth.push(0); + continue; + }; + let mut d = 0; + for (j, (o, b)) in polys.iter().zip(&boxes).enumerate() { + if j == i || b.is_some_and(|b| !b.contains(p)) { + continue; + } + // Each loop against every other is loops x vertices: bounded so a region of many + // large nested loops cannot hold the thread (CWE-407). + visits = visits.saturating_add(o.len()); + if visits > MAX_RENEST_EDGE_VISITS { + return false; + } + if point_in_polygon(o, p) { + d += 1; + } + } + depth.push(d); + } for (l, d) in self.loops.iter_mut().zip(depth) { l.outer = d % 2 == 0; } + true } /// Area, perimeter, bounding box, centroid and second moments (outer loops counted @@ -128,3 +160,37 @@ fn probe(l: &[Vec2]) -> Option { (a.dist(b) > 1e-12).then(|| (a + b) * 0.5).filter(|m| m.is_finite()) }) } + +#[cfg(test)] +mod tests { + use super::*; + + fn square(c: f64, half: f64, n: usize, outer: bool) -> HatchLoop { + // `n` vertices along the bottom edge, then the other three corners. + let mut vertices: Vec = + (0..n).map(|k| PolyVertex::new(Vec2::new(c - half + 2.0 * half * k as f64 / n as f64, c - half))).collect(); + vertices.extend([Vec2::new(c + half, c - half), Vec2::new(c + half, c + half), Vec2::new(c - half, c + half)].map(PolyVertex::new)); + HatchLoop { vertices, outer, sources: Vec::new() } + } + + #[test] + fn renest_sets_depth_parity_and_skips_loops_far_away() { + // Three nested squares, and one beside them. + let mut r = + Region { loops: vec![square(0.0, 3.0, 1, false), square(0.0, 2.0, 1, false), square(0.0, 1.0, 1, false), square(10.0, 1.0, 1, false)] }; + assert!(r.renest()); + assert_eq!(r.loops.iter().map(|l| l.outer).collect::>(), [true, false, true, true]); + } + + #[test] + fn renest_of_many_large_nested_loops_stops_at_its_budget() { + // Concentric loops of many vertices each: each probe is inside the box of every larger + // loop (about loops² / 2 pairs), so each of those tests is a full polygon scan. + let loops = 300; + let n = 3 * MAX_RENEST_EDGE_VISITS / (loops * loops); + let mut r = Region { loops: (0..loops).map(|i| square(0.0, 1.0 + i as f64, n, false)).collect() }; + let before = r.clone(); + assert!(!r.renest()); + assert_eq!(r, before, "flags left as they were"); + } +} diff --git a/crates/engine/src/cmd/inquiry/regions.rs b/crates/engine/src/cmd/inquiry/regions.rs index 9e6b11a49..033d6e0c2 100644 --- a/crates/engine/src/cmd/inquiry/regions.rs +++ b/crates/engine/src/cmd/inquiry/regions.rs @@ -128,7 +128,9 @@ fn combine(a: &Region, b: &Region, op: Op) -> Result> { } loops.retain(|l| l.vertices.len() >= 2); let mut r = Region { loops }; - r.renest(); + if !r.renest() { + return Err(other("These regions are too complex to combine.")); + } Ok(Some(r)) } From 997f152cea9e3ece7f62f0cf19a866f8e03d706f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 2 Nov 2026 21:16:56 -0500 Subject: [PATCH 087/100] Extents: share block-bounds memoization across all the entities measured (#2681) Each top-level bounds call started with an empty block cache, so measuring a space's extents or building a spatial-index chunk measured a block definition again for every reference to it: R references to a block of B entities cost R x B. BlockBoundsCache keeps the per-depth block bounds (separately for drawn bounds and extents, which differ on layers turned off) across one bulk operation; Drawing::extents and the spatial index use it (CWE-407). --- crates/doc/src/extents.rs | 82 +++++++++++++++++++++++++++++++++--- crates/doc/src/lib.rs | 7 ++- crates/engine/src/spatial.rs | 8 ++-- 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/crates/doc/src/extents.rs b/crates/doc/src/extents.rs index d87cdc9a2..e44809df4 100644 --- a/crates/doc/src/extents.rs +++ b/crates/doc/src/extents.rs @@ -94,25 +94,48 @@ fn mtext_box(t: &crate::MText) -> Bounds2 { /// The box of what `e` draws (block contents on layers turned off left out). pub fn entity_bounds(d: &Drawing, e: &Entity, depth: usize) -> Bounds2 { - bounds_of(d, e, depth, false) + bounds_of(d, e, depth, false, &mut HashMap::new()) } /// The box `e` takes in the drawing's extents: as [`entity_bounds`], but block contents on a layer /// turned off count, as AutoCAD 2024's ZOOM Extents counts them (a frozen layer's do not). pub fn extent_bounds(d: &Drawing, e: &Entity) -> Bounds2 { - bounds_of(d, e, 0, true) + bounds_of(d, e, 0, true, &mut HashMap::new()) } -fn bounds_of(d: &Drawing, e: &Entity, depth: usize, off_counts: bool) -> Bounds2 { +/// The bounds of block contents measured so far, kept across the entities of one unchanged +/// drawing. Measuring many entities with [`entity_bounds_cached`] or [`extent_bounds_cached`] +/// and one cache measures each block definition once per depth for all of them; a fresh cache per +/// entity would measure a block again for every reference to it, R references to a block of B +/// entities costing R x B (CWE-407). Use a cache only while the drawing it measured is unchanged. +#[derive(Default)] +pub struct BlockBoundsCache<'a> { + /// By block name and depth, block contents on layers turned off left out ([`entity_bounds`]). + drawn: HashMap<(&'a str, usize), Bounds2>, + /// The same with those contents counted ([`extent_bounds`]). + extents: HashMap<(&'a str, usize), Bounds2>, +} + +/// [`entity_bounds`] reusing the block bounds `cache` holds from earlier calls on drawing `d`. +pub fn entity_bounds_cached<'a>(d: &'a Drawing, e: &Entity, depth: usize, cache: &mut BlockBoundsCache<'a>) -> Bounds2 { + bounds_of(d, e, depth, false, &mut cache.drawn) +} + +/// [`extent_bounds`] reusing the block bounds `cache` holds from earlier calls on drawing `d`. +pub fn extent_bounds_cached<'a>(d: &'a Drawing, e: &Entity, cache: &mut BlockBoundsCache<'a>) -> Bounds2 { + bounds_of(d, e, 0, true, &mut cache.extents) +} + +fn bounds_of<'a>(d: &'a Drawing, e: &Entity, depth: usize, off_counts: bool, blocks: &mut HashMap<(&'a str, usize), Bounds2>) -> Bounds2 { // A top-level annotative object measures as it draws at the current annotation scale. if depth == 0 { match d.anno_draw(e) { crate::AnnoDraw::Plain => {} crate::AnnoDraw::Hidden => return Bounds2::EMPTY, - crate::AnnoDraw::Scaled(f) => return bounds_in(d, &crate::anno_scaled(e, f), 0, &mut HashMap::new(), off_counts), + crate::AnnoDraw::Scaled(f) => return bounds_in(d, &crate::anno_scaled(e, f), 0, blocks, off_counts), } } - bounds_in(d, e, depth, &mut HashMap::new(), off_counts) + bounds_in(d, e, depth, blocks, off_counts) } /// [`entity_bounds`] with the bounds of block contents (in block coordinates) already computed, @@ -442,4 +465,53 @@ mod tests { empty.text = Some(crate::MText { contents: " ".into(), ..mtext }); assert!(bounds(EntityKind::MLeader(empty)).max.x < 26.0); } + + #[test] + fn a_shared_block_cache_measures_as_a_fresh_one_for_each_entity() { + let line = |a: (f64, f64), b: (f64, f64), layer: &str| { + let mut e = Entity::new(crate::Handle(1), EntityKind::Line(crate::Line { a: Vec3::new(a.0, a.1, 0.0), b: Vec3::new(b.0, b.1, 0.0) })); + e.common.layer = layer.into(); + e + }; + let ins = |block: &str, at: f64| { + Entity::new( + crate::Handle(2), + EntityKind::Insert(crate::Insert { + block: block.into(), + insert: Vec3::new(at, at, 0.0), + scale: Vec3::new(1.0, 1.0, 1.0), + rotation: 0.0, + attribs: Vec::new(), + cols: 1, + rows: 1, + col_spacing: 0.0, + row_spacing: 0.0, + }), + ) + }; + let mut d = Drawing::new_imperial(); + d.ensure_layer("OFF"); + if let Some(l) = d.layer_mut("OFF") { + l.on = false; + } + // A block with a part on a layer turned off (left out when drawn, counted in extents), + // and a block that nests it. + let mut b = crate::Block::new("B"); + b.entities.push(line((0.0, 0.0), (1.0, 1.0), "0")); + b.entities.push(line((0.0, 0.0), (5.0, 5.0), "OFF")); + d.blocks.insert("B".into(), std::sync::Arc::new(b)); + let mut n = crate::Block::new("N"); + n.entities.push(ins("B", 10.0)); + d.blocks.insert("N".into(), std::sync::Arc::new(n)); + let ents: Vec = (0..50).map(|i| ins(if i % 2 == 0 { "B" } else { "N" }, f64::from(i))).collect(); + // One cache for both kinds of measure, interleaved: neither may answer for the other. + let mut cache = BlockBoundsCache::default(); + for e in &ents { + let (drawn, extents) = (entity_bounds_cached(&d, e, 0, &mut cache), extent_bounds_cached(&d, e, &mut cache)); + let (want_drawn, want_extents) = (entity_bounds(&d, e, 0), extent_bounds(&d, e)); + assert_eq!((drawn.min, drawn.max), (want_drawn.min, want_drawn.max)); + assert_eq!((extents.min, extents.max), (want_extents.min, want_extents.max)); + assert!(extents.width() > drawn.width()); + } + } } diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index 8f2a1e2e4..19f486240 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -27,7 +27,7 @@ pub use cadcraft_color as color; pub use cadcraft_geom as geom; pub use constraint::*; pub use entity::*; -pub use extents::{MAX_BLOCK_DEPTH, entity_bounds, extent_bounds}; +pub use extents::{BlockBoundsCache, MAX_BLOCK_DEPTH, entity_bounds, entity_bounds_cached, extent_bounds, extent_bounds_cached}; pub use format::Format; pub use gdt::*; pub use header::{HVal, Header}; @@ -383,10 +383,13 @@ impl Drawing { /// Bounds of the entities in a space that count in its extents ([`Drawing::in_extents`]). pub fn extents(&self, space: &Space) -> cadcraft_geom::Bounds2 { let mut b = cadcraft_geom::Bounds2::EMPTY; + // One block-bounds cache for the whole space: every reference to a block reuses its + // measured contents instead of measuring the definition again. + let mut cache = BlockBoundsCache::default(); if let Some(store) = self.space(space) { for e in store.iter() { if self.in_extents(e) { - b = b.union(&extent_bounds(self, e)); + b = b.union(&extent_bounds_cached(self, e, &mut cache)); } } } diff --git a/crates/engine/src/spatial.rs b/crates/engine/src/spatial.rs index dd2e0d593..a62537262 100644 --- a/crates/engine/src/spatial.rs +++ b/crates/engine/src/spatial.rs @@ -15,7 +15,7 @@ use std::collections::BTreeMap; use std::sync::{Arc, Mutex}; -use cadcraft_doc::{Block, DimStyle, Drawing, Entity, EntityKind, EntityStore, Space, entity_bounds}; +use cadcraft_doc::{Block, BlockBoundsCache, DimStyle, Drawing, Entity, EntityKind, EntityStore, Space, entity_bounds_cached}; use cadcraft_geom::Bounds2; use rstar::primitives::{GeomWithData, Rectangle}; use rstar::{AABB, RTree}; @@ -44,6 +44,8 @@ struct ChunkIx { impl ChunkIx { fn build(d: &Drawing, key: usize, items: &[Arc]) -> ChunkIx { let mut infinite = Vec::new(); + // One block-bounds cache for the chunk: references to the same block measure it once. + let mut cache = BlockBoundsCache::default(); let bounds = items .iter() .enumerate() @@ -51,7 +53,7 @@ impl ChunkIx { if matches!(e.kind, EntityKind::XLine(_) | EntityKind::Ray(_)) { infinite.push(u32::try_from(i).unwrap_or(u32::MAX)); } - entity_bounds(d, e, 0) + entity_bounds_cached(d, e, 0, &mut cache) }) .collect(); let serial = NEXT_SERIAL.fetch_add(1, std::sync::atomic::Ordering::Relaxed); @@ -200,7 +202,7 @@ impl SpatialIndex { let (c, l) = self.locate(i)?; c.ents.get(l) } - /// The bounds of the entity at draw index `i` (as [`entity_bounds`] computes them). + /// The bounds of the entity at draw index `i` (as [`cadcraft_doc::entity_bounds`] computes them). pub fn bounds(&self, i: u32) -> Bounds2 { self.locate(i).and_then(|(c, l)| c.bounds.get(l).copied()).unwrap_or(Bounds2::EMPTY) } From c956baa3493743b83c2a389869e0308cd7130f87 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 2 Nov 2026 21:17:23 -0500 Subject: [PATCH 088/100] constraints: share geometry preparation across an operation and budget spline rebuilds (#2677) Endpoint residuals rebuilt a fit spline through a dense interpolation solve on every evaluation, so many Tangent/Smooth constraints on one spline, or numerical differentiation along other objects' parameters, repeated identical O(n^3) rebuilds; solving had no budget for them. purge_invalid built a fresh model per constraint, re-sampling the same block for every reference, and an Insert naming no block scanned every block definition case-insensitively. - Models built for one operation share a Prep: block samples are taken once per block (in block coordinates, transformed per Insert), block names resolve through a lazily built case-insensitive index, and the last rebuild of each spline is reused at the same defining points. - Comp::run and the Fix redundancy check refuse a system whose per-iteration residual work, including spline rebuilds, exceeds the limit DOF analysis already applied (MAX_EVAL_WORK). - Spline templates keep only what a rebuild reads: no fit points for a control spline, no control net for a fit spline. - purge_invalid validates every constraint with one Prep. --- crates/constraints/src/dof.rs | 44 +++++- crates/constraints/src/lib.rs | 65 ++++++-- crates/constraints/src/model.rs | 255 ++++++++++++++++++++++++-------- 3 files changed, 285 insertions(+), 79 deletions(-) diff --git a/crates/constraints/src/dof.rs b/crates/constraints/src/dof.rs index 559452223..e6128665c 100644 --- a/crates/constraints/src/dof.rs +++ b/crates/constraints/src/dof.rs @@ -11,16 +11,13 @@ use std::collections::HashSet; use cadcraft_doc::{Constraint, Drawing, Handle}; use serde::Serialize; -use crate::model::Model; -use crate::{Comp, MAX_CONSTRAINTS, SolveError, SolveOptions, build_budgeted, lm}; +use crate::model::{Model, Prep}; +use crate::{Comp, MAX_CONSTRAINTS, MAX_EVAL_WORK, SolveError, SolveOptions, build_with, lm}; /// Most free parameters for which per-object freedom is analysed (null-space basis size). const MAX_NULL_COLS: usize = 600; /// Largest Jacobian (rows × columns × min) whose rank is computed. const MAX_RANK_WORK: f64 = 4e8; -/// Largest residual-evaluation work (rows × free × per-residual cost, including spline -/// reconstruction) allowed before building the Jacobian during numerical differentiation. -const MAX_EVAL_WORK: f64 = 2e9; /// Most objects listed in one report. const MAX_OBJECTS: usize = 10_000; @@ -139,8 +136,8 @@ fn plural(n: usize, one: &str, many: &str) -> String { /// (constrained or not) and the constraint components they belong to. pub fn dof(d: &Drawing, handles: &[Handle]) -> Result { let cons: Vec<&Constraint> = d.constraints.iter().take(MAX_CONSTRAINTS).collect(); - let mut members_left = crate::model::MAX_RIGID_WORK; - let (comps, _) = build_budgeted(d, &cons, &SolveOptions::default(), &mut members_left)?; + let mut prep = Prep::new(crate::model::MAX_RIGID_WORK); + let (comps, _) = build_with(d, &cons, &SolveOptions::default(), &mut prep)?; let want = |h: Handle| handles.is_empty() || handles.contains(&h); let mut objects = Vec::new(); let mut seen = HashSet::new(); @@ -179,7 +176,7 @@ pub fn dof(d: &Drawing, handles: &[Handle]) -> Result { if seen.contains(&h) { continue; } - let m = Model::build_budgeted(d, &[h], &mut members_left); + let m = Model::build_with(d, &[h], &mut prep); let Some(s) = m.slots.first() else { continue }; seen.insert(h); free_objects = true; @@ -237,5 +234,36 @@ mod budget_tests { }; let comp = Comp { model, built: vec![endpoint], weights: vec![1.0; 2], held: Vec::new(), tol: 1e-9 }; assert!(analyse(&comp).dof.is_none()); + // Solving it is refused for the same reason, before any Jacobian is built. + assert!(matches!(comp.run(None), Err(SolveError::TooLarge))); + } + + /// Many Tangent constraints on one large fit spline rebuild it once while being interpreted, + /// and the solve is refused by its rebuild budget instead of running. + #[test] + fn repeated_spline_tangents_rebuild_once_and_solve_is_bounded() { + use cadcraft_doc::{Constraint, Line}; + use cadcraft_geom::Vec3; + let mut d = Drawing::default(); + let pts: Vec<_> = (0..400).map(|i| Vec2::new(i as f64, (i % 3) as f64)).collect(); + let h = d.add(&Space::Model, Common::default(), EntityKind::Spline(Spline::fit_with(&pts, false, Default::default()))).unwrap(); + let l = + d.add(&Space::Model, Common::default(), EntityKind::Line(Line { a: Vec3::new(-1.0, 0.0, 0.0), b: Vec3::new(-1.0, 5.0, 0.0) })).unwrap(); + for id in 1..=200 { + d.constraints.push(Constraint { + id, + kind: ConstraintKind::Tangent, + refs: vec![GeomRef::whole(h), GeomRef::whole(l)], + name: String::new(), + expr: String::new(), + }); + } + let cons: Vec<&Constraint> = d.constraints.iter().collect(); + let (comps, skipped) = crate::build(&d, &cons, &SolveOptions::default()).unwrap(); + assert!(skipped.is_empty()); + let comp = comps.first().unwrap(); + assert_eq!(comp.built.len(), 200); + assert!(matches!(comp.run(None), Err(SolveError::TooLarge))); + assert!(crate::purge_invalid(&mut d).is_empty()); } } diff --git a/crates/constraints/src/lib.rs b/crates/constraints/src/lib.rs index 0bf230323..5b01fc022 100644 --- a/crates/constraints/src/lib.rs +++ b/crates/constraints/src/lib.rs @@ -41,7 +41,7 @@ pub use dof::{DofReport, DofStatus, ObjectDof, dof}; pub use expr::{Env, ExprError}; pub use model::{ref_points, rigid_points, supported}; -use model::Model; +use model::{Model, Prep}; use residual::{Built, Form}; /// Most constraints one drawing may hold. @@ -50,6 +50,11 @@ pub const MAX_CONSTRAINTS: usize = 5000; const MAX_CONFLICT_PROBES: usize = 48; /// Solver iterations. const MAX_ITER: usize = 200; +/// Largest residual-evaluation work (including spline rebuilds) one numerical Jacobian may take, +/// shared by solving, redundancy checks and DOF analysis. +pub(crate) const MAX_EVAL_WORK: f64 = 2e9; +/// Residual evaluations a solver iteration makes besides its Jacobian (damped trial steps). +const TRIAL_EVALS: f64 = 14.0; #[derive(Clone, Debug, PartialEq, thiserror::Error)] pub enum SolveError { @@ -267,12 +272,41 @@ impl Comp { pub(crate) fn rows(&self, skip: Option) -> usize { self.built.iter().enumerate().filter(|(i, _)| Some(*i) != skip).map(|(_, b)| b.rows).sum() } + /// Work of one solver iteration's residual evaluations over `free`: every residual twice per + /// free parameter, plus a spline rebuild each time a spline's own defining points move (rebuilds + /// are shared by residuals at the same points, see [`Model`]). + pub(crate) fn eval_work(&self, free: &[usize], skip: Option) -> f64 { + let rows = self.rows(skip) as f64; + let evals = 2.0 * free.len() as f64 + TRIAL_EVALS; + let mut splines: Vec = Vec::new(); + for (i, b) in self.built.iter().enumerate() { + if Some(i) != skip && b.spline_reconstruct_cost(&self.model) > 0.0 { + splines.extend(b.refs.iter().map(|r| r.handle).filter(|h| self.model.spline_reconstruct_cost(*h) > 0.0)); + } + } + splines.sort_unstable(); + splines.dedup(); + let rebuilds: f64 = splines + .iter() + .filter_map(|h| self.model.slot(*h)) + .map(|s| { + let own = free.iter().filter(|j| (s.off..s.off + s.len).contains(*j)).count() as f64; + (2.0 * own + TRIAL_EVALS + 1.0) * self.model.spline_reconstruct_cost(s.handle) + }) + .sum(); + evals * rows + rebuilds + } fn run(&self, skip: Option) -> Result { let free = self.free(skip); let rows = self.rows(skip); if rows > lm::MAX_ROWS || (rows as f64) * (rows as f64) * (free.len() as f64) > 4e8 { return Err(SolveError::TooLarge); } + // Numerical differentiation rebuilds a fit spline through a dense solve for every move of + // its own fit points: refuse before iterating when that alone is too much (CWE-407). + if self.eval_work(&free, skip) > MAX_EVAL_WORK { + return Err(SolveError::TooLarge); + } let f = |x: &[f64], out: &mut Vec| self.eval(x, out, skip); let mut o = lm::solve(&f, &self.model.x0, &free, &self.weights, self.tol, MAX_ITER); // A solution the ellipses would be stored differently from (axes swapped, minor axis @@ -426,24 +460,24 @@ fn slot_scales(model: &Model, s: &model::Slot) -> Vec<(usize, f64)> { /// Build the component systems. Constraints that cannot be interpreted are returned as skipped. pub(crate) fn build(d: &Drawing, cons: &[&Constraint], opts: &SolveOptions) -> Result<(Vec, Vec<(u32, String)>), SolveError> { - let mut members_left = model::MAX_RIGID_WORK; - build_budgeted(d, cons, opts, &mut members_left) + build_with(d, cons, opts, &mut Prep::new(model::MAX_RIGID_WORK)) } -/// Build constraint components with one caller-owned rigid-member sampling budget shared by every model. +/// Build constraint components sharing one caller-owned geometry preparation (block sampling and +/// its member budget, spline rebuilds) between every model. /// Return constraints that cannot be interpreted as skipped entries. -pub(crate) fn build_budgeted( +pub(crate) fn build_with( d: &Drawing, cons: &[&Constraint], opts: &SolveOptions, - members_left: &mut usize, + prep: &mut Prep, ) -> Result<(Vec, Vec<(u32, String)>), SolveError> { let mut e = env(d); let mut skipped = Vec::new(); let mut comps = Vec::new(); for group in components(cons) { let handles: Vec = group.iter().flat_map(|c| c.refs.iter().map(|r| r.handle)).collect(); - let mut model = Model::build_budgeted(d, &handles, members_left); + let mut model = Model::build_with(d, &handles, prep); let mut built = Vec::new(); for c in group { let target = if c.kind.is_dimensional() { constraint_value(&mut e, c)? } else { 0.0 }; @@ -656,6 +690,11 @@ pub fn all_satisfied(d: &Drawing) -> bool { /// Check that a constraint's references make sense for its type. pub fn validate(d: &Drawing, c: &Constraint) -> Result<(), String> { + validate_with(d, c, &mut Prep::new(model::MAX_RIGID_WORK)) +} + +/// [`validate`] sharing block samples and spline rebuilds with other checks of the same drawing. +fn validate_with(d: &Drawing, c: &Constraint, prep: &mut Prep) -> Result<(), String> { let handles: Vec = c.refs.iter().map(|r| r.handle).collect(); for h in &handles { match d.entity(*h) { @@ -664,7 +703,7 @@ pub fn validate(d: &Drawing, c: &Constraint) -> Result<(), String> { _ => {} } } - let mut model = Model::build(d, &handles); + let mut model = Model::build_with(d, &handles, prep); interpret(c, &mut model, 1.0).map(|_| ()) } @@ -681,7 +720,12 @@ pub fn measure(d: &Drawing, kind: ConstraintKind, refs: &[GeomRef]) -> Result Vec { let mut gone = Vec::new(); - let keep: Vec = d.constraints.iter().map(|c| validate(d, c).is_ok()).collect(); + // One preparation for every check: each block is sampled and each spline rebuilt once, + // however many constraints refer to them (CWE-407). Its member budget is not shared, so + // a constraint is never dropped because earlier ones spent it: every block is still + // examined at most once, up to its own member limit. + let mut prep = Prep::new(usize::MAX); + let keep: Vec = d.constraints.iter().map(|c| validate_with(d, c, &mut prep).is_ok()).collect(); let mut i = 0; d.constraints.retain(|c| { let k = keep.get(i).copied().unwrap_or(true); @@ -768,6 +812,9 @@ pub fn add(d: &mut Drawing, mut c: Constraint, opts: &SolveOptions) -> Result MAX_EVAL_WORK { + return Err(SolveError::TooLarge); + } let after = comp.free(None); let locked: Vec = before.iter().enumerate().filter(|(_, j)| !after.contains(j)).map(|(k, _)| k).collect(); let rows_old = comp.rows(Some(pos)); diff --git a/crates/constraints/src/model.rs b/crates/constraints/src/model.rs index d18646c32..f9ff51fe5 100644 --- a/crates/constraints/src/model.rs +++ b/crates/constraints/src/model.rs @@ -14,9 +14,11 @@ //! Constraints may append auxiliary unknowns after the entity parameters (contact parameters of //! a tangency between curves); they are never written back. +use std::cell::RefCell; use std::collections::HashMap; +use std::rc::Rc; -use cadcraft_doc::{Drawing, EntityKind, GeomRef, Handle, Insert, Sub}; +use cadcraft_doc::{Block, Drawing, EntityKind, GeomRef, Handle, Insert, Sub}; use cadcraft_geom::{Spline as GSpline, Vec2, ccw_sweep}; use crate::curves::{Ell, Frame, clamped, spline_end}; @@ -70,6 +72,9 @@ pub(crate) struct Slot { pub len: usize, } +/// The spline last rebuilt for each handle, with the defining points it was rebuilt from. +type SplineCache = HashMap, Rc)>; + #[derive(Clone, Debug, Default)] pub(crate) struct Model { pub slots: Vec, @@ -79,20 +84,75 @@ pub(crate) struct Model { splines: HashMap, /// Block reference rigid points relative to the insertion point at rotation 0. rigid: HashMap>, + /// Rebuilt splines, shared with every model of the same [`Prep`]: residuals that ask for the + /// same spline at the same defining points (several constraints on one spline, or derivatives + /// taken along other objects' parameters) reuse one rebuild instead of a dense solve each. + rebuilt: Rc>, +} + +/// Geometry preparation shared by every model built for one operation (a solve, a DOF report, a +/// purge): rigid samples per block, a case-insensitive block-name index and rebuilt splines. A +/// block referenced by many Inserts is sampled once, an unknown block name costs one hashed +/// lookup, and a spline referenced by many constraints is rebuilt once (CWE-407). +#[derive(Debug)] +pub(crate) struct Prep { + /// Block members that may still be examined across the operation. + members_left: usize, + /// Block-local rigid samples by the name an Insert gives (`None`: no such block). + blocks: HashMap>>, + /// Lower-cased block name to the stored name it resolves to, built on the first exact miss. + folded: Option>, + rebuilt: Rc>, +} + +impl Prep { + /// Preparation that may examine at most `members` block members in all. + pub(crate) fn new(members: usize) -> Prep { + Prep { members_left: members, blocks: HashMap::new(), folded: None, rebuilt: Rc::default() } + } + + /// The block an Insert names, as [`Drawing::block`] resolves it (exact name, else the first + /// stored name equal ignoring ASCII case), without scanning every block on each miss. + fn resolve<'d>(&mut self, d: &'d Drawing, name: &str) -> Option<&'d Block> { + if let Some(b) = d.blocks.get(name) { + return Some(b); + } + let folded = self.folded.get_or_insert_with(|| { + let mut m = HashMap::with_capacity(d.blocks.len()); + for k in d.blocks.keys() { + m.entry(k.to_ascii_lowercase()).or_insert_with(|| k.clone()); + } + m + }); + d.blocks.get(folded.get(&name.to_ascii_lowercase())?).map(|b| &**b) + } + + /// Finite characteristic points of a block in its own coordinates, sampled once per name. + fn block_points(&mut self, d: &Drawing, name: &str) -> Option> { + if let Some(s) = self.blocks.get(name) { + return s.clone(); + } + let sampled = self.resolve(d, name).map(|b| sample_block(b, &mut self.members_left)); + self.blocks.insert(name.to_string(), sampled.clone()); + sampled + } + + /// World positions of an Insert's rigid points, at most [`MAX_RIGID_POINTS`]. + fn rigid_points(&mut self, d: &Drawing, ins: &Insert) -> Vec { + let Some(local) = self.block_points(d, &ins.block) else { return Vec::new() }; + let Some(block) = self.resolve(d, &ins.block) else { return Vec::new() }; + let m = ins.transform(block.base.xy()); + local.iter().map(|p| m.apply(*p)).filter(|p| p.is_finite()).take(MAX_RIGID_POINTS).collect() + } } impl Model { /// Work for a spline that an endpoint residual actually reconstructs. Fit interpolation /// uses dense Gaussian elimination (cubic); control splines clone and evaluate their template. pub(crate) fn spline_reconstruct_cost(&self, handle: Handle) -> f64 { - let Some(s) = self.splines.get(&handle) else { return 0.0 }; - let n = s.fit.len() as f64; - if fit_mode(s) { - n * n * n - } else { - let n = s.control.len() as f64; - n * n - } + let Some(Shape::Spline { n, fit, .. }) = self.slot(handle).map(|s| s.shape) else { return 0.0 }; + let n = n as f64; + if fit { n * n * n } else { n * n } } } @@ -115,23 +175,18 @@ pub fn supported(kind: &EntityKind) -> bool { /// centres, vertices, nested insertion points) of the block's own objects, at most /// [`MAX_RIGID_POINTS`]. Nested blocks are not entered. pub fn rigid_points(d: &Drawing, ins: &Insert) -> Vec { - let mut members_left = MAX_RIGID_MEMBERS; - rigid_points_budgeted(d, ins, &mut members_left) + Prep::new(MAX_RIGID_MEMBERS).rigid_points(d, ins) } -/// Sample finite block points within per-insert and caller-shared member limits. -/// Members that contribute no points still consume traversal work; nested blocks are not entered. -fn rigid_points_budgeted(d: &Drawing, ins: &Insert, members_left: &mut usize) -> Vec { - let Some(block) = d.block(&ins.block) else { return Vec::new() }; - let m = ins.transform(block.base.xy()); +/// Sample a block's finite characteristic points in block coordinates, within the per-block +/// member limit and the caller-shared `members_left`. Members that contribute no points still +/// consume traversal work; nested blocks are not entered. +fn sample_block(block: &Block, members_left: &mut usize) -> Rc<[Vec2]> { let mut out = Vec::new(); // Bound the members examined, not only the points collected: entities that contribute no - // points (Text, Ray, …) still cost traversal, so a block full of them — scanned again for - // every Insert — would otherwise be unbounded (CWE-407). - let mut examined = 0usize; - for e in block.entities.iter() { - examined += 1; - if out.len() >= MAX_RIGID_POINTS || examined > MAX_RIGID_MEMBERS || *members_left == 0 { + // points (Text, Ray, …) still cost traversal (CWE-407). + for (examined, e) in block.entities.iter().enumerate() { + if out.len() >= MAX_RIGID_POINTS || examined >= MAX_RIGID_MEMBERS || *members_left == 0 { break; } *members_left -= 1; @@ -148,10 +203,10 @@ fn rigid_points_budgeted(d: &Drawing, ins: &Insert, members_left: &mut usize) -> EntityKind::LwPolyline(p) => p.vertices.iter().take(MAX_RIGID_POINTS).map(|v| v.p).collect(), _ => Vec::new(), }; - out.extend(pts.into_iter().map(|p| m.apply(p)).filter(|p| p.is_finite())); + out.extend(pts.into_iter().filter(|p| p.is_finite())); } out.truncate(MAX_RIGID_POINTS); - out + out.into() } /// Constraint points of an object as the solver sees them (`Sub`, position): end points, @@ -187,13 +242,12 @@ fn fit_mode(s: &GSpline) -> bool { impl Model { /// Build a model for the given entities. Unsupported or missing entities are skipped. pub fn build(d: &Drawing, handles: &[Handle]) -> Model { - let mut members_left = MAX_RIGID_WORK; - Self::build_budgeted(d, handles, &mut members_left) + Self::build_with(d, handles, &mut Prep::new(MAX_RIGID_WORK)) } /// Add the parameters of one supported entity to `m`, and return its shape; `None` when it is /// left out (too large, degenerate or unsupported). - fn add_params(m: &mut Model, d: &Drawing, h: Handle, kind: &EntityKind, off: usize, members_left: &mut usize) -> Option { + fn add_params(m: &mut Model, d: &Drawing, h: Handle, kind: &EntityKind, off: usize, prep: &mut Prep) -> Option { let shape = match kind { EntityKind::Line(l) => { m.x0.extend([l.a.x, l.a.y, l.b.x, l.b.y]); @@ -243,15 +297,22 @@ impl Model { // fit point; a control polygon's ends are its first and last points when the // knots are clamped. let ends = !sp.closed && (fit || (clamped(sp, false) && clamped(sp, true))); - // Missing weights default to 1 in eval; only excess weights are unused. - let mut kept = sp.clone(); + // Keep only what rebuilding reads, so each rebuild copies no unused data: missing + // weights default to 1 in eval and only excess weights are unused; a control + // spline's fit points are dropped when its control points move (write_back), and + // a fit spline is rebuilt from its parameters, not from its old control net. + let mut kept = if fit { + GSpline { fit_opts: sp.fit_opts, closed: sp.closed, ..GSpline::default() } + } else { + GSpline { fit: Vec::new(), ..sp.clone() } + }; kept.weights.truncate(kept.control.len()); m.splines.insert(h, kept); Shape::Spline { n, fit, ends } } EntityKind::Insert(ins) => { let base = Vec2::new(ins.insert.x, ins.insert.y); - let pts: Vec = rigid_points_budgeted(d, ins, members_left).into_iter().map(|p| (p - base).rotate(-ins.rotation)).collect(); + let pts: Vec = prep.rigid_points(d, ins).into_iter().map(|p| (p - base).rotate(-ins.rotation)).collect(); m.x0.extend([ins.insert.x, ins.insert.y, ins.rotation]); let n = pts.len(); m.rigid.insert(h, pts); @@ -262,16 +323,17 @@ impl Model { Some(shape) } - /// Build solver parameters for supported entities while sharing rigid-member sampling work with other models. - pub(crate) fn build_budgeted(d: &Drawing, handles: &[Handle], members_left: &mut usize) -> Model { - let mut m = Model::default(); + /// Build solver parameters for supported entities, sharing block sampling, its member budget + /// and spline rebuilds with every other model built from `prep`. + pub(crate) fn build_with(d: &Drawing, handles: &[Handle], prep: &mut Prep) -> Model { + let mut m = Model { rebuilt: Rc::clone(&prep.rebuilt), ..Model::default() }; for &h in handles { if m.index.contains_key(&h) { continue; } let Some(e) = d.entity(h) else { continue }; let off = m.x0.len(); - let Some(shape) = Self::add_params(&mut m, d, h, &e.kind, off, members_left) else { continue }; + let Some(shape) = Self::add_params(&mut m, d, h, &e.kind, off, prep) else { continue }; if m.x0.len() > MAX_PARAMS || m.x0.get(off..).unwrap_or(&[]).iter().any(|v| !v.is_finite()) { m.x0.truncate(off); continue; @@ -373,18 +435,20 @@ impl Model { self.slot(r.handle).is_some_and(|s| matches!(s.shape, Shape::Spline { .. })) } - /// The spline of a reference with the defining points taken from `x`. - fn spline(&self, x: &[f64], s: &Slot) -> Option { + /// The spline of a reference with the defining points taken from `x`. The last rebuild of + /// each spline is kept, so asking again at the same points costs a comparison, not a rebuild. + fn spline(&self, x: &[f64], s: &Slot) -> Option> { let Shape::Spline { n, fit, .. } = s.shape else { return None }; let t = self.splines.get(&s.handle)?; let pts = (0..n).map(|i| p2(x, s.off + 2 * i)).collect::>>()?; - Some(if fit { - t.refit(&pts, t.closed) - } else { - let mut sp = t.clone(); - sp.control = pts; - sp - }) + if let Some((at, sp)) = self.rebuilt.borrow().get(&s.handle) + && *at == pts + { + return Some(Rc::clone(sp)); + } + let sp = Rc::new(if fit { t.refit(&pts, t.closed) } else { GSpline { control: pts.clone(), ..t.clone() } }); + self.rebuilt.borrow_mut().insert(s.handle, (pts, Rc::clone(&sp))); + Some(sp) } /// The start (`at_end` false) or end of a curve reference, in the curve's own direction: @@ -421,7 +485,7 @@ impl Model { let e = self.ell_at(x, o, true)?; e.frame(if at_end { e.t1 } else { e.t0 }) } - (Shape::Spline { ends: true, .. }, Sub::Whole | Sub::Start | Sub::End) => spline_end(&self.spline(x, s)?, at_end), + (Shape::Spline { ends: true, .. }, Sub::Whole | Sub::Start | Sub::End) => spline_end(&*self.spline(x, s)?, at_end), _ => None, } } @@ -590,7 +654,8 @@ impl Model { } let shape = s.shape; // A control-point spline loses its fit data once its control points move. - let spline = self.spline(x, s).map(|mut sp| { + let spline = self.spline(x, s).map(|sp| { + let mut sp = GSpline::clone(&sp); if matches!(shape, Shape::Spline { fit: false, .. }) { sp.fit.clear(); } @@ -712,21 +777,21 @@ mod budget_tests { } } - /// A second model receives no sampled block points after the first model spends the shared member budget. - #[test] - fn rigid_sampling_budget_is_shared_between_models() { - let mut d = Drawing::default(); - let mut block = Block::new("BAR"); - for _ in 0..3 { + fn add_block(d: &mut Drawing, name: &str, lines: usize) { + let mut block = Block::new(name); + for _ in 0..lines { block.entities.push(Entity { handle: d.new_handle(), common: Common::default(), kind: EntityKind::Line(Line { a: Vec3::ZERO, b: Vec3::new(1.0, 0.0, 0.0) }), }); } - d.blocks.insert("BAR".into(), Arc::new(block)); - let ins = Insert { - block: "BAR".into(), + d.blocks.insert(name.into(), Arc::new(block)); + } + + fn insert(block: &str) -> Insert { + Insert { + block: block.into(), insert: Vec3::ZERO, scale: Vec3::new(1.0, 1.0, 1.0), rotation: 0.0, @@ -735,13 +800,79 @@ mod budget_tests { rows: 1, col_spacing: 0.0, row_spacing: 0.0, - }; - let h = d.add(&Space::Model, Common::default(), EntityKind::Insert(ins)).unwrap(); - let mut budget = 2; - let first = Model::build_budgeted(&d, &[h], &mut budget); - assert_eq!(first.rigid[&h].len(), 4); - assert_eq!(budget, 0); - let next = Model::build_budgeted(&d, &[h], &mut budget); - assert!(next.rigid[&h].is_empty()); + } + } + + fn add_insert(d: &mut Drawing, block: &str) -> Handle { + d.add(&Space::Model, Common::default(), EntityKind::Insert(insert(block))).unwrap() + } + + /// The member budget is shared by every model of one preparation, and a block it already + /// sampled is reused by later Inserts without examining its members again. + #[test] + fn rigid_sampling_budget_is_shared_and_blocks_sampled_once() { + let mut d = Drawing::default(); + add_block(&mut d, "BAR", 3); + add_block(&mut d, "BAZ", 3); + let (bar, baz, bar2) = (add_insert(&mut d, "BAR"), add_insert(&mut d, "BAZ"), add_insert(&mut d, "BAR")); + let mut prep = Prep::new(2); + let first = Model::build_with(&d, &[bar], &mut prep); + assert_eq!(first.rigid[&bar].len(), 4); + assert_eq!(prep.members_left, 0); + let other = Model::build_with(&d, &[baz], &mut prep); + assert!(other.rigid[&baz].is_empty()); + let again = Model::build_with(&d, &[bar2], &mut prep); + assert_eq!(again.rigid[&bar2].len(), 4); + } + + /// Block names resolve ignoring ASCII case, as `Drawing::block` does, through one index. + #[test] + fn block_names_resolve_ignoring_case() { + let mut d = Drawing::default(); + add_block(&mut d, "Bar", 2); + let (lower, missing) = (add_insert(&mut d, "bAR"), add_insert(&mut d, "nope")); + let mut prep = Prep::new(MAX_RIGID_WORK); + let m = Model::build_with(&d, &[lower, missing], &mut prep); + assert_eq!(m.rigid[&lower].len(), 4); + assert!(m.rigid[&missing].is_empty()); + assert!(prep.folded.as_ref().is_some_and(|f| f.len() == 1)); + assert_eq!(rigid_points(&d, &insert("BAR")).len(), 4); + } + + /// A spline asked for again at the same defining points is not rebuilt, also from another + /// model of the same preparation; moved points rebuild it. + #[test] + fn spline_rebuilds_are_reused_at_the_same_points() { + let mut d = Drawing::default(); + let pts: Vec = (0..6).map(|i| Vec2::new(i as f64, (i % 2) as f64)).collect(); + let h = d.add(&Space::Model, Common::default(), EntityKind::Spline(GSpline::fit_with(&pts, false, Default::default()))).unwrap(); + let mut prep = Prep::new(MAX_RIGID_WORK); + let a = Model::build_with(&d, &[h], &mut prep); + let b = Model::build_with(&d, &[h], &mut prep); + let (sa, sb) = (a.slot(h).unwrap(), b.slot(h).unwrap()); + assert!(matches!(sa.shape, Shape::Spline { fit: true, .. })); + let first = a.spline(&a.x0, sa).unwrap(); + assert!(Rc::ptr_eq(&first, &a.spline(&a.x0, sa).unwrap())); + assert!(Rc::ptr_eq(&first, &b.spline(&b.x0, sb).unwrap())); + let mut moved = a.x0.clone(); + moved[1] += 0.5; + let other = a.spline(&moved, sa).unwrap(); + assert!(!Rc::ptr_eq(&first, &other)); + assert_eq!(other.fit[0], Vec2::new(0.0, 0.5)); + } + + /// A control spline's template keeps no fit points and no excess weights. + #[test] + fn control_template_drops_unused_data() { + let mut d = Drawing::default(); + let mut sp = GSpline::from_control(vec![Vec2::ZERO, Vec2::new(1.0, 2.0), Vec2::new(3.0, 0.0), Vec2::new(4.0, 1.0)], 3); + sp.fit = vec![Vec2::ZERO; 10_000]; + sp.fit_opts.tolerance = 0.5; + sp.weights = vec![1.0; 10_000]; + let h = d.add(&Space::Model, Common::default(), EntityKind::Spline(sp)).unwrap(); + let m = Model::build(&d, &[h]); + let t = &m.splines[&h]; + assert!(t.fit.is_empty()); + assert_eq!(t.weights.len(), 4); } } From 2e9761249a023d45b97436ffe8a31f74595c17d7 Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 19:22:30 -0700 Subject: [PATCH 089/100] Autosave: keep every folder's lock, and don't let a failure hide or wait (#2676) From review of #2670. Changing SAVEFILEPATH released the old folder's lock while files remained there, so another app could offer a running app's drawings. A write failing while the app was idle waited for the next input to be shown. A second drawing's failure stayed hidden behind a dismissed notice. --- crates/engine/src/autosave.rs | 52 ++++++++++++++++-------- crates/engine/tests/autosave.rs | 30 +++++++++++--- crates/ui-egui/src/autosave.rs | 33 +++++++++++---- crates/ui-egui/tests/autosave_failure.rs | 7 +++- 4 files changed, 93 insertions(+), 29 deletions(-) diff --git a/crates/engine/src/autosave.rs b/crates/engine/src/autosave.rs index ca6d6ede4..0caac6143 100644 --- a/crates/engine/src/autosave.rs +++ b/crates/engine/src/autosave.rs @@ -166,9 +166,11 @@ pub struct Autosaver { start: std::time::Instant, #[cfg(not(target_arch = "wasm32"))] last_beat: Option, - /// The lock file this app holds the system lock on, open for as long as the lock is wanted. + /// This app's lock files: one per folder, because SAVEFILEPATH can change while files remain + /// in the old folder. Each with the file it holds open for the system lock, or `None` where + /// locks don't work and only the file's age counts. #[cfg(not(target_arch = "wasm32"))] - lock: Option<(std::path::PathBuf, std::fs::File)>, + locks: Vec<(std::path::PathBuf, Option)>, } impl Autosaver { @@ -185,7 +187,7 @@ impl Autosaver { #[cfg(not(target_arch = "wasm32"))] last_beat: None, #[cfg(not(target_arch = "wasm32"))] - lock: None, + locks: Vec::new(), } } @@ -203,9 +205,9 @@ impl Autosaver { self.docs.values().filter_map(|t| t.file.clone()).collect() } - /// Why a drawing currently has no up-to-date recovery copy, for the host to show the user. - pub fn failure(&self) -> Option<&str> { - self.failed.values().next().map(String::as_str) + /// The drawings with no up-to-date recovery copy and why, for the host to show the user. + pub fn failures(&self) -> impl Iterator { + self.failed.iter().map(|(uid, why)| (*uid, why.as_str())) } /// What to do at `now` (seconds, any fixed origin) with SAVETIME `minutes` and autosave folder @@ -471,6 +473,12 @@ mod native { self.running.iter().any(|(_, h)| !h.is_finished()) } + /// Whether a write's result hasn't been taken in yet, finished or not: the host needs + /// another [`Autosaver::tick`] to learn whether it failed. + pub fn results_pending(&self) -> bool { + !self.running.is_empty() + } + /// Take in the writes that have finished ([`Autosaver::tick`] does this first). A drawing /// whose write failed is no longer counted as autosaved: the next plan restarts its clock, /// so it is tried again SAVETIME later even if it doesn't change, and [`Autosaver::failure`] @@ -569,23 +577,32 @@ mod native { } } - /// Refresh the lock file at `path`, taking the system lock on it first if this app doesn't - /// hold it there yet. Without one the file still works by its age. + /// Refresh this app's lock files, and start one at `path` if there isn't one yet, taking + /// the system lock on it if it can. Without that lock the file still works by its age. + /// Only trouble at `path` is an error: an older folder may be gone by now. fn refresh_lock(&mut self, path: &Path) -> std::io::Result<()> { - if let Some((held, file)) = &self.lock - && held == path - { - return file.set_modified(SystemTime::now()); - } - self.lock = None; let id = std::process::id(); + let mut current = None; + for (held, file) in &self.locks { + let refreshed = match file { + Some(file) => file.set_modified(SystemTime::now()), + None => std::fs::write(held, id.to_string()), + }; + if held == path { + current = Some(refreshed); + } + } + if let Some(refreshed) = current { + return refreshed; + } let mut file = OpenOptions::new().write(true).create(true).truncate(false).open(path)?; if file.try_lock().is_err() { + self.locks.push((path.to_path_buf(), None)); return std::fs::write(path, id.to_string()); } file.set_len(0)?; file.write_all(format!("{id}{LOCK_HELD}").as_bytes())?; - self.lock = Some((path.to_path_buf(), file)); + self.locks.push((path.to_path_buf(), Some(file))); Ok(()) } @@ -600,7 +617,10 @@ mod native { discard(&path); } self.docs.clear(); - self.lock = None; + for (path, file) in self.locks.drain(..) { + drop(file); + let _ = std::fs::remove_file(path); + } let dir = savefilepath(); if !dir.is_empty() && is_instance_id(&self.instance) { let _ = std::fs::remove_file(lock_path(Path::new(&dir), &self.instance)); diff --git a/crates/engine/tests/autosave.rs b/crates/engine/tests/autosave.rs index a878faae7..8fd70b6ae 100644 --- a/crates/engine/tests/autosave.rs +++ b/crates/engine/tests/autosave.rs @@ -323,6 +323,26 @@ fn an_emergency_save_finishes_before_it_returns_even_with_savetime_off() { let _ = std::fs::remove_dir_all(&dir); } +#[test] +fn a_new_autosave_folder_keeps_the_old_folder_locked() { + hooks(); + let _turn = folder_turn(); + let (old, new) = (temp_dir("moved-old"), temp_dir("moved-new")); + let mut s = Session::new(); + draw(&mut s); + let mut a = Autosaver::new("44-e"); + autosave_once(&mut a, &s, &old); + autosave::set_savefilepath(&new.to_string_lossy()); + draw(&mut s); + assert_eq!(a.emergency_save(&s), 1); + assert!(new.join("44-e.lock").exists()); + assert!(autosave::recoverable(&old, "9-f").is_empty(), "this app is still running"); + a.shutdown(); + assert!(!old.join("44-e.lock").exists() && !new.join("44-e.lock").exists()); + let _ = std::fs::remove_dir_all(&old); + let _ = std::fs::remove_dir_all(&new); +} + #[test] fn a_normal_exit_removes_the_lock_file() { hooks(); @@ -366,7 +386,7 @@ fn a_failed_autosave_is_tried_again_without_another_change() { std::thread::sleep(Duration::from_millis(10)); } assert!(rearmed, "a failed autosave must be tried again"); - assert!(a.failure().is_some_and(|m| m.contains("out")), "{:?}", a.failure()); + assert!(a.failures().next().is_some_and(|(_, why)| why.contains("out"))); std::fs::remove_file(&out).unwrap(); let jobs = a.plan(&s, 180.0, 1, &dir); @@ -387,7 +407,7 @@ fn a_failed_autosave_is_tried_again_without_another_change() { std::thread::sleep(Duration::from_millis(10)); } assert_eq!(a.next_due(1e4, 1), None); - assert_eq!(a.failure(), None, "a write that worked clears the failure"); + assert_eq!(a.failures().next(), None, "a write that worked clears the failure"); a.shutdown(); let _ = std::fs::remove_dir_all(&root); } @@ -407,16 +427,16 @@ fn a_failure_ends_when_the_drawing_no_longer_needs_a_copy() { a.perform(jobs); for _ in 0..500 { a.reap(); - if a.failure().is_some() { + if a.failures().next().is_some() { break; } std::thread::sleep(Duration::from_millis(10)); } - assert!(a.failure().is_some()); + assert!(a.failures().next().is_some()); let i = s.active; s.execute("close", &json!({ "index": i })).unwrap(); a.plan(&s, 120.0, 1, &dir); - assert_eq!(a.failure(), None); + assert_eq!(a.failures().next(), None); a.shutdown(); let _ = std::fs::remove_dir_all(&root); } diff --git a/crates/ui-egui/src/autosave.rs b/crates/ui-egui/src/autosave.rs index 0d50e2dcc..716c27371 100644 --- a/crates/ui-egui/src/autosave.rs +++ b/crates/ui-egui/src/autosave.rs @@ -35,6 +35,9 @@ pub fn load_prefs(json: &str) { /// What the autosave failure notice says (see [`failure_notice`]). #[derive(Clone, Debug, PartialEq)] pub struct AutosaveNotice { + /// The drawings it has told the user about. Another drawing's failure is news, even after + /// the notice was dismissed. + pub reported: std::collections::BTreeSet, pub message: String, /// The user closed it. The failure stays recorded so the notice doesn't come back on every /// retry; it does for the next failure after a write has worked. @@ -100,23 +103,39 @@ impl CadApp { _ctx.request_repaint_after(wait); } #[cfg(not(target_arch = "wasm32"))] - self.note_autosave_failure(_ctx); + { + // A write that fails while the app is idle must still reach the notice. + if self.autosave.as_ref().is_some_and(cadcraft_engine::autosave::Autosaver::results_pending) { + _ctx.request_repaint_after(std::time::Duration::from_secs(1)); + } + self.note_autosave_failure(_ctx); + } } /// Keep [`Self::autosave_notice`] in step with autosave: a new failure is said once at the /// command line and shown until it is dismissed or a write works. #[cfg(not(target_arch = "wasm32"))] fn note_autosave_failure(&mut self, ctx: &egui::Context) { - let failure = self.autosave.as_ref().and_then(|s| s.failure()).map(str::to_string); - match (failure, &mut self.autosave_notice) { - (None, notice) => *notice = None, - (Some(message), Some(n)) => n.message = message, - (Some(message), notice) => { + let failures: Vec<(u64, String)> = self.autosave.iter().flat_map(|s| s.failures()).map(|(uid, why)| (uid, why.to_string())).collect(); + if failures.is_empty() { + self.autosave_notice = None; + return; + } + let notice = + self.autosave_notice.get_or_insert_with(|| AutosaveNotice { reported: Default::default(), message: String::new(), dismissed: false }); + notice.reported.retain(|uid| failures.iter().any(|(failed, _)| failed == uid)); + for (uid, message) in &failures { + if notice.reported.insert(*uid) { self.session.echo(format!("Autosave failed: {message}")); - *notice = Some(AutosaveNotice { message, dismissed: false }); + notice.message.clone_from(message); + notice.dismissed = false; ctx.request_repaint(); } } + // The drawing it names recovered while another still fails. + if let Some((_, message)) = failures.first().filter(|_| !failures.iter().any(|(_, m)| *m == notice.message)) { + notice.message.clone_from(message); + } } /// Every frame: after a GPU error or device loss, autosave unsaved drawings at once (the diff --git a/crates/ui-egui/tests/autosave_failure.rs b/crates/ui-egui/tests/autosave_failure.rs index 28b6a66d0..6e266fedd 100644 --- a/crates/ui-egui/tests/autosave_failure.rs +++ b/crates/ui-egui/tests/autosave_failure.rs @@ -67,7 +67,12 @@ fn a_failed_autosave_is_shown_until_a_write_works() { app.autosave_tick(&ctx); assert!(app.autosave_notice.as_ref().is_some_and(|n| n.dismissed)); assert!(painted_text(&mut app, &ctx).is_empty()); - assert_eq!(said(&app), 1); + + // Another drawing's failure is news, even after the first was dismissed. + app.autosave_notice.as_mut().unwrap().reported.clear(); + app.autosave_tick(&ctx); + assert!(app.autosave_notice.as_ref().is_some_and(|n| !n.dismissed)); + assert_eq!(said(&app), 2); // The folder comes back: the next write clears it. std::fs::remove_file(&dir).unwrap(); From d180b28ef61ca3a5a7dd796611b0b218249ea77a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 2 Nov 2026 21:33:55 -0500 Subject: [PATCH 090/100] Multilines: find each segment's usable neighbour direction in linear time (#2679) * Multilines: find each segment's usable neighbour direction in two linear passes MLine::miters searched the direction array forwards and backwards from every vertex for the nearest non-degenerate segment, so a long run of coincident vertices made it quadratic. The nearest usable directions ahead and behind are now computed once, in one backward and one forward pass (CWE-407). * Multilines: test that a run of coincident vertices at a corner takes the corner's miter --- crates/doc/src/entity/mline.rs | 108 ++++++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 10 deletions(-) diff --git a/crates/doc/src/entity/mline.rs b/crates/doc/src/entity/mline.rs index 62ab1d8f3..f6ceef519 100644 --- a/crates/doc/src/entity/mline.rs +++ b/crates/doc/src/entity/mline.rs @@ -207,6 +207,41 @@ pub struct MLineGeometry { pub fill: Vec>, } +/// For each segment direction in `dirs`, the first non-zero direction at or after it and the +/// first at or before it, both wrapping around (`Vec2::X` when every direction is zero). +/// +/// Two linear passes: searching from every segment separately is quadratic in a run of +/// coincident vertices, which a hostile file can make as long as it likes (CWE-407). +fn usable_directions(dirs: &[Vec2]) -> (Vec, Vec) { + let n = dirs.len(); + let (Some(first), Some(last)) = (dirs.iter().position(|d| *d != Vec2::ZERO), dirs.iter().rposition(|d| *d != Vec2::ZERO)) else { + return (vec![Vec2::X; n], vec![Vec2::X; n]); + }; + let (mut ahead, mut behind) = (vec![Vec2::X; n], vec![Vec2::X; n]); + // Walking backwards from the last usable direction, the latest one seen is the next ahead. + let mut cur = Vec2::X; + for k in 0..n { + let i = (last + n - k) % n; + if let Some(d) = dirs.get(i).filter(|d| **d != Vec2::ZERO) { + cur = *d; + } + if let Some(a) = ahead.get_mut(i) { + *a = cur; + } + } + // Walking forwards from the first usable direction, the latest one seen is the nearest behind. + for k in 0..n { + let i = (first + k) % n; + if let Some(d) = dirs.get(i).filter(|d| **d != Vec2::ZERO) { + cur = *d; + } + if let Some(b) = behind.get_mut(i) { + *b = cur; + } + } + (ahead, behind) +} + impl MLine { /// A multiline through `pts` in style `def`. pub fn new(def: &MLineStyle, pts: &[Vec2], closed: bool, just: MLineJust, scale: f64) -> MLine { @@ -268,16 +303,11 @@ impl MLine { let dirs = self.directions(); let n = self.vertices.len(); let nseg = dirs.len(); - // A degenerate segment takes its neighbour's direction. - let dir = |i: usize| -> Vec2 { - (0..nseg).map(|k| dirs.get((i + k) % nseg.max(1)).copied().unwrap_or_default()).find(|d| *d != Vec2::ZERO).unwrap_or(Vec2::X) - }; - let dir_back = |i: usize| -> Vec2 { - (0..nseg) - .map(|k| dirs.get((i + nseg - k % nseg) % nseg.max(1)).copied().unwrap_or_default()) - .find(|d| *d != Vec2::ZERO) - .unwrap_or(Vec2::X) - }; + // A degenerate segment takes its neighbour's direction: the first usable one at or after + // it (`ahead`), or at or before it (`behind`), wrapping around. + let (ahead, behind) = usable_directions(&dirs); + let dir = |i: usize| -> Vec2 { ahead.get(i).copied().unwrap_or(Vec2::X) }; + let dir_back = |i: usize| -> Vec2 { behind.get(i).copied().unwrap_or(Vec2::X) }; let end = |d: Vec2, angle: f64| { let u = d.rotate(angle); let s = angle.sin(); @@ -426,3 +456,61 @@ impl crate::Drawing { self.mline_style(&name).or_else(|| self.mline_style(STANDARD_MLINE_STYLE)).cloned().unwrap_or_default() } } + +#[cfg(test)] +mod tests { + use super::*; + + /// The nearest usable directions found by searching from each segment, as miters did before. + fn searched(dirs: &[Vec2]) -> (Vec, Vec) { + let n = dirs.len(); + let ahead = (0..n).map(|i| (0..n).filter_map(|k| dirs.get((i + k) % n).copied()).find(|d| *d != Vec2::ZERO).unwrap_or(Vec2::X)); + let behind = (0..n).map(|i| (0..n).filter_map(|k| dirs.get((i + n - k) % n).copied()).find(|d| *d != Vec2::ZERO).unwrap_or(Vec2::X)); + (ahead.collect(), behind.collect()) + } + + #[test] + fn usable_directions_match_a_search_from_each_segment() { + let (z, a, b, c) = (Vec2::ZERO, Vec2::Y, -Vec2::X, Vec2::new(0.6, 0.8)); + for dirs in [ + vec![], + vec![z], + vec![a], + vec![z, z, z], + vec![a, z, z, b], + vec![z, a, z, b, z], + vec![z, z, c], + vec![c, z, z], + vec![a, b, c], + vec![z, a, z, z, b, c, z], + ] { + assert_eq!(usable_directions(&dirs), searched(&dirs), "{dirs:?}"); + } + } + + #[test] + fn coincident_vertices_at_a_corner_take_the_corner_miter() { + // Every vertex of a run on one point mitres between the segment before the run and the one after. + let style = MLineStyle::default(); + let corner = MLine::new(&style, &[Vec2::ZERO, Vec2::Y, Vec2::new(-1.0, 1.0)], false, MLineJust::Zero, 1.0); + let pts = [Vec2::ZERO, Vec2::Y, Vec2::Y, Vec2::Y, Vec2::new(-1.0, 1.0)]; + let run = MLine::new(&style, &pts, false, MLineJust::Zero, 1.0); + let (expected, got) = (corner.miters(), run.miters()); + assert_eq!(got.get(1..4), Some(&[expected[1]; 3][..])); + assert_eq!((got.first(), got.last()), (expected.first(), expected.last())); + } + + #[test] + fn a_long_run_of_coincident_vertices_takes_linear_time() { + // 200 000 vertices on one point but the last: the miters must not search the run once per + // vertex (quadratic, minutes) to find the one usable direction. + let mut pts = vec![Vec2::ZERO; 200_000]; + pts.push(Vec2::new(1.0, 0.0)); + let m = MLine::new(&MLineStyle::default(), &pts, false, MLineJust::Zero, 1.0); + let t = std::time::Instant::now(); + let miters = m.miters(); + assert_eq!(miters.len(), pts.len()); + assert!(miters.iter().all(|v| v.is_finite())); + assert!(t.elapsed() < std::time::Duration::from_secs(5), "{:?}", t.elapsed()); + } +} From e24c4553d2ca715073383720c9e2d7aa0e98f8dd Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 19:47:36 -0700 Subject: [PATCH 091/100] DXF: 100 million group codes by default, and memory for them that can be refused The tag list for a counted ASCII file is reserved once with a fallible allocation, so where the system refuses the memory, opening fails with an error instead of ending the app. Growing it would also take half as much again for a moment. 100 million rather than 150 bounds a crafted 512 MB file to about 4 GB of tags; CADCRAFT_MAX_DXF_TAGS raises it, and the error says so. --- crates/dxf/src/lib.rs | 33 +++++++++++++++++++++++++-------- crates/io/src/tests.rs | 2 +- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/crates/dxf/src/lib.rs b/crates/dxf/src/lib.rs index ab2597a21..c31defc21 100644 --- a/crates/dxf/src/lib.rs +++ b/crates/dxf/src/lib.rs @@ -23,8 +23,11 @@ pub enum DxfError { Eof, /// More group codes than [`MAX_TAGS`]: `tags` is the count found (or the count read when the /// limit was reached), `limit` the maximum. - #[error("drawing too large: {tags} DXF group codes, more than the {limit} CADCraft reads")] + #[error("drawing too large: {tags} DXF group codes, more than the {limit} CADCraft reads (set CADCRAFT_MAX_DXF_TAGS to read more)")] TooLarge { tags: usize, limit: usize }, + /// The memory for `tags` group codes couldn't be had: an error, not the process ending. + #[error("not enough memory to read this drawing's {tags} DXF group codes")] + OutOfMemory { tags: usize }, } pub type Result = std::result::Result; @@ -164,10 +167,11 @@ fn typed(code: i32, text: &str) -> Value { const BINARY_SENTINEL: &[u8] = b"AutoCAD Binary DXF\r\n\x1a\0"; /// Most group codes read from one file by default. A `Tag` takes 40 bytes plus its text, so this -/// bounds the tag list to roughly 6 GB plus text. It matches the 1 GB DXF rendition a DWG may -/// convert to (a 1 GB DXF holds well over 50 million group codes, so a lower cap rejected large -/// DWGs that converted fine, issue #1948). Set `CADCRAFT_MAX_DXF_TAGS` to raise or lower it. -pub const MAX_TAGS: usize = 150_000_000; +/// bounds the tag list to about 4 GB plus text. 50 million rejected large DWGs that converted fine +/// (a 172 MB drawing has 56 million, issue #1948). The file-size limits bound memory too: 512 MB +/// for a DXF opened directly, 1 GB for a DWG's DXF rendition. Set `CADCRAFT_MAX_DXF_TAGS` to +/// raise or lower it. +pub const MAX_TAGS: usize = 100_000_000; /// Environment variable overriding [`MAX_TAGS`] (a positive integer). pub const MAX_TAGS_ENV: &str = "CADCRAFT_MAX_DXF_TAGS"; @@ -214,7 +218,7 @@ pub fn parse_with_limit(bytes: &[u8], max_tags: usize) -> Result> { if tags > max_tags { return Err(DxfError::TooLarge { tags, limit: max_tags }); } - parse_ascii_with_limit(&codepage::decode_ascii_file(bytes), max_tags) + parse_ascii_counted(&codepage::decode_ascii_file(bytes), max_tags, tags) } /// The four hex digits of a `\U+XXXX` escape starting at `chars[i]`, if there is one. @@ -286,12 +290,17 @@ fn unescape_unicode(s: &str) -> String { } pub fn parse_ascii(text: &str) -> Result> { - parse_ascii_with_limit(text, max_tags()) + parse_ascii_counted(text, max_tags(), 0) } +/// [`parse_ascii`] of a file known to hold `expected` group codes. Their list is reserved first, +/// with an allocation that can fail: where the system refuses that much memory, opening the file +/// is an error instead of the end of the app. Growing the list instead could also briefly take +/// half as much again. #[allow(clippy::while_let_loop)] -fn parse_ascii_with_limit(text: &str, max_tags: usize) -> Result> { +fn parse_ascii_counted(text: &str, max_tags: usize, expected: usize) -> Result> { let mut tags = Vec::new(); + tags.try_reserve_exact(expected.min(max_tags)).map_err(|_| DxfError::OutOfMemory { tags: expected })?; let mut lines = text.strip_prefix('\u{feff}').unwrap_or(text).lines().enumerate(); loop { let Some((ln, code_line)) = lines.next() else { break }; @@ -807,4 +816,12 @@ mod limit_tests { // Issue #1948: the default must admit a DXF rendition well above 50 million group codes. const { assert!(MAX_TAGS >= 100_000_000) }; } + + #[test] + fn the_tag_list_is_reserved_once_and_a_refusal_is_an_error() { + let tags = parse_ascii_counted("0\nSECTION\n0\nENDSEC\n0\nEOF\n", MAX_TAGS, 3).unwrap(); + assert_eq!((tags.len(), tags.capacity()), (3, 3)); + let e = parse_ascii_counted("0\nEOF\n", usize::MAX, usize::MAX).unwrap_err(); + assert!(matches!(e, DxfError::OutOfMemory { .. }), "{e}"); + } } diff --git a/crates/io/src/tests.rs b/crates/io/src/tests.rs index 5d08817c5..fca680385 100644 --- a/crates/io/src/tests.rs +++ b/crates/io/src/tests.rs @@ -1039,7 +1039,7 @@ fn oversized_dwg_and_dxf_are_rejected_early() { // Lines that aren't even group codes: the size is reported before anything is parsed. let text = "not a group code\nx\n".repeat(60); let e = cadcraft_dxf::parse_with_limit(text.as_bytes(), 50).unwrap_err(); - assert_eq!(e.to_string(), "drawing too large: 60 DXF group codes, more than the 50 CADCraft reads"); + assert_eq!(e.to_string(), "drawing too large: 60 DXF group codes, more than the 50 CADCraft reads (set CADCRAFT_MAX_DXF_TAGS to read more)"); } /// Issue #800: one unreadable object record doesn't fail the whole DWG. The reader skips it, the From 9d83f219102b89cbb10df56146ae6395827b6726 Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 20:36:01 -0700 Subject: [PATCH 092/100] Round-trip harness: typed tolerance error, report folders it couldn't read, --out=DIR From review of #2685. The ast-grep string-error rule refused the tolerance parser's String error. A folder the walk couldn't read was skipped silently, so a strict run could pass on part of the corpus; it is now said on stderr and fails a strict run. xtask corpus resolves --out=DIR against the caller's folder, as --out DIR. --- apps/cadcraft-cli/src/args.rs | 6 ++- apps/cadcraft-cli/src/roundtrip.rs | 25 ++++++++++-- crates/io/src/roundtrip/corpus.rs | 62 +++++++++++++++++++++++++----- crates/io/src/roundtrip/tests.rs | 7 ++++ xtask/src/main.rs | 4 ++ 5 files changed, 89 insertions(+), 15 deletions(-) diff --git a/apps/cadcraft-cli/src/args.rs b/apps/cadcraft-cli/src/args.rs index 1df9bacbe..f3b70fdc8 100644 --- a/apps/cadcraft-cli/src/args.rs +++ b/apps/cadcraft-cli/src/args.rs @@ -336,10 +336,10 @@ pub(crate) struct RoundtripArgs { } /// `--tolerance`: a finite number, 0 or more. -fn tolerance(v: &str) -> Result { +fn tolerance(v: &str) -> Result { match v.parse::() { Ok(t) if t.is_finite() && t >= 0.0 => Ok(t), - _ => Err(format!("must be a number >= 0, got `{v}`")), + _ => Err(ValueError::Tolerance(v.to_string())), } } @@ -395,6 +395,8 @@ pub(crate) enum ValueError { CmdId, #[error(transparent)] CmdJson(#[from] serde_json::Error), + #[error("expected a number of 0 or more, got `{0}`")] + Tolerance(String), } /// Why `parse` refuses flags clap accepted: clap prints it as a usage error (exit 2). diff --git a/apps/cadcraft-cli/src/roundtrip.rs b/apps/cadcraft-cli/src/roundtrip.rs index 00f7595ea..8cc4bb11e 100644 --- a/apps/cadcraft-cli/src/roundtrip.rs +++ b/apps/cadcraft-cli/src/roundtrip.rs @@ -19,13 +19,28 @@ pub(crate) enum RoundtripError { Strict(Vec), } +/// What the walk couldn't look at, so the report isn't taken for the whole corpus: said on +/// stderr, and failures of a strict run. +fn left_out(w: &cadcraft_io::roundtrip::corpus::Walk) -> Vec { + let mut out: Vec = w.unreadable.iter().map(|u| format!("not read: {u}")).collect(); + if w.truncated { + out.push("left out: files or folders past the harness limits".to_string()); + } + out +} + pub(crate) fn roundtrip(a: &RoundtripArgs, io: &mut Streams) -> Result { use cadcraft_io::roundtrip::corpus; let path = a.path.clone().or_else(|| std::env::var_os("CADCRAFT_CORPUS_DIR").map(Into::into)).ok_or(RoundtripError::NoPath)?; + let mut missed = Vec::new(); let (root, files, out) = if path.is_dir() { let out = a.out.clone().unwrap_or_else(|| path.join("report")); - let files = corpus::corpus_files(&path, std::slice::from_ref(&out)); - (path, files, out) + let walk = corpus::walk(&path, std::slice::from_ref(&out)); + missed = left_out(&walk); + for m in &missed { + io.errln(format_args!("roundtrip: {m}")); + } + (path, walk.files, out) } else if path.is_file() { let root = path.parent().map(std::path::Path::to_path_buf).unwrap_or_default(); let out = a.out.clone().unwrap_or_else(|| root.join("report")); @@ -34,7 +49,8 @@ pub(crate) fn roundtrip(a: &RoundtripArgs, io: &mut Streams) -> Result { return Err(RoundtripError::Missing(path).into()); }; if files.is_empty() { - return Err(RoundtripError::Empty(root).into()); + // A folder it couldn't read isn't an empty one. + return Err(if missed.is_empty() { RoundtripError::Empty(root) } else { RoundtripError::Strict(missed) }.into()); } let mut opts = corpus::CorpusOptions { cross_format: !a.same_format, census: !a.no_census, ..Default::default() }; opts.compare.abs_tol = a.tolerance; @@ -66,7 +82,8 @@ pub(crate) fn roundtrip(a: &RoundtripArgs, io: &mut Streams) -> Result { // CADCRAFT_CORPUS_STRICT=1: a file that doesn't open, save or reopen, or records a writer // leaves out, fail the run (the report is written first). if std::env::var("CADCRAFT_CORPUS_STRICT").is_ok_and(|v| v == "1") { - let failed = report.strict_failures(); + let mut failed = report.strict_failures(); + failed.extend(missed); if !failed.is_empty() { return Err(CliError::from(RoundtripError::Strict(failed))); } diff --git a/crates/io/src/roundtrip/corpus.rs b/crates/io/src/roundtrip/corpus.rs index 36895527e..fe5f2abdf 100644 --- a/crates/io/src/roundtrip/corpus.rs +++ b/crates/io/src/roundtrip/corpus.rs @@ -347,27 +347,71 @@ pub fn is_drawing_file(path: &Path) -> bool { /// Every DXF and DWG file under `dir`, sorted, leaving out hidden folders and the folders in /// `skip` (the report folder). Symbolic links are not followed. pub fn corpus_files(dir: &Path, skip: &[PathBuf]) -> Vec { + walk(dir, skip).files +} + +/// What [`walk`] found, and what it couldn't look at, so a report never passes for a whole one. +#[derive(Debug, Default)] +pub struct Walk { + pub files: Vec, + /// Folders and entries that couldn't be read, with why. + pub unreadable: Vec, + /// Files past [`MAX_FILES`] or folders past the depth limit were left out. + pub truncated: bool, +} + +/// [`corpus_files`], also saying what it had to leave out. +pub fn walk(dir: &Path, skip: &[PathBuf]) -> Walk { let skip: Vec = skip.iter().map(|p| p.canonicalize().unwrap_or_else(|_| p.clone())).collect(); - let mut out = Vec::new(); + let mut w = Walk::default(); let mut stack = vec![(dir.to_path_buf(), 0usize)]; while let Some((d, depth)) = stack.pop() { - let Ok(entries) = std::fs::read_dir(&d) else { continue }; - for entry in entries.flatten() { + let entries = match std::fs::read_dir(&d) { + Ok(entries) => entries, + Err(e) => { + w.unreadable.push(format!("{}: {e}", d.display())); + continue; + } + }; + for entry in entries { + let entry = match entry { + Ok(entry) => entry, + Err(e) => { + w.unreadable.push(format!("{}: {e}", d.display())); + continue; + } + }; let path = entry.path(); - let Ok(ft) = entry.file_type() else { continue }; + let ft = match entry.file_type() { + Ok(ft) => ft, + Err(e) => { + w.unreadable.push(format!("{}: {e}", path.display())); + continue; + } + }; if ft.is_dir() { let hidden = entry.file_name().to_string_lossy().starts_with('.'); let canon = path.canonicalize().unwrap_or_else(|_| path.clone()); - if !hidden && depth < MAX_DEPTH && !skip.contains(&canon) { + if hidden || skip.contains(&canon) { + continue; + } + if depth < MAX_DEPTH { stack.push((path, depth + 1)); + } else { + w.truncated = true; + } + } else if ft.is_file() && is_drawing_file(&path) { + if w.files.len() < MAX_FILES { + w.files.push(path); + } else { + w.truncated = true; } - } else if ft.is_file() && is_drawing_file(&path) && out.len() < MAX_FILES { - out.push(path); } } } - out.sort(); - out + w.files.sort(); + w.unreadable.sort(); + w } /// `path` relative to `root`, with forward slashes. diff --git a/crates/io/src/roundtrip/tests.rs b/crates/io/src/roundtrip/tests.rs index 57b77a74a..2107fb48f 100644 --- a/crates/io/src/roundtrip/tests.rs +++ b/crates/io/src/roundtrip/tests.rs @@ -462,6 +462,13 @@ fn report_renders_as_markdown_and_json() { } #[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_folder_it_cannot_read_is_reported_not_skipped() { + let w = corpus::walk(std::path::Path::new("no/such/corpus/folder"), &[]); + assert!(w.files.is_empty()); + assert_eq!(w.unreadable.len(), 1, "{:?}", w.unreadable); +} + #[test] fn corpus_files_walks_folders_and_skips_the_report() { use corpus::corpus_files; diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 8d8d78631..5d8bd5111 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -247,6 +247,10 @@ fn cmd_corpus(args: &[&str]) -> Result<(), String> { let v = it.next().ok_or_else(|| format!("{a} needs a value"))?; pass.extend([a.to_string(), v.to_string()]); } + s if s.starts_with("--out=") => { + let v = s.trim_start_matches("--out="); + pass.extend(["--out".to_string(), here.join(v).display().to_string()]); + } s if s.starts_with("--") => pass.push(s.to_string()), s if dir.is_none() => dir = Some(s.to_string()), _ => return Err("corpus takes one DIR".into()), From a4ff30d11eb97c9a34556f37a8b68e7d2399864a Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Mon, 2 Nov 2026 20:49:27 -0700 Subject: [PATCH 093/100] Round-trip harness: never read the kept copies back as corpus files --- apps/cadcraft-cli/src/roundtrip.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/cadcraft-cli/src/roundtrip.rs b/apps/cadcraft-cli/src/roundtrip.rs index 8cc4bb11e..dba9c8d04 100644 --- a/apps/cadcraft-cli/src/roundtrip.rs +++ b/apps/cadcraft-cli/src/roundtrip.rs @@ -35,7 +35,8 @@ pub(crate) fn roundtrip(a: &RoundtripArgs, io: &mut Streams) -> Result { let mut missed = Vec::new(); let (root, files, out) = if path.is_dir() { let out = a.out.clone().unwrap_or_else(|| path.join("report")); - let walk = corpus::walk(&path, std::slice::from_ref(&out)); + // The report folder, and the saved copies in it, are never read back as corpus files. + let walk = corpus::walk(&path, &[out.clone(), out.join("saved")]); missed = left_out(&walk); for m in &missed { io.errln(format_args!("roundtrip: {m}")); From 31e6e16bc0bc9dc5eed9f87ee6dd7f6a3fb4526d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 2 Nov 2026 22:51:07 -0500 Subject: [PATCH 094/100] constraints: evaluate a named expression without copying its text (#2675) * constraints: evaluate a named expression without copying its text Env::value cloned the stored expression before parse rejected it as too long, and failures are not cached, so an oversized imported expression referenced from many dimensional constraints was copied on every reference. Parse the stored text in place, and keep only a bounded prefix of an over-long expression when it is inserted (it still fails as too long). * constraints: test repeated evaluation of an oversized named expression --- crates/constraints/src/expr.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/constraints/src/expr.rs b/crates/constraints/src/expr.rs index c71ed3486..057ef3e1c 100644 --- a/crates/constraints/src/expr.rs +++ b/crates/constraints/src/expr.rs @@ -284,7 +284,14 @@ impl Env { pub fn new() -> Self { Env::default() } + /// Add or replace a named expression. Text longer than [`MAX_LEN`] could never parse, so + /// only a bounded prefix of it is kept: it still fails (as too long), without the environment + /// holding or copying the whole text. pub fn insert(&mut self, name: &str, expr: &str) { + let expr = match expr.char_indices().map(|(i, _)| i).find(|i| *i > MAX_LEN) { + Some(end) => expr.get(..end).unwrap_or(expr), + None => expr, + }; self.exprs.insert(name.to_string(), expr.to_string()); self.cache.clear(); } @@ -309,8 +316,9 @@ impl Env { if stack.len() >= MAX_DEPTH { return Err(ExprError::TooDeep); } - let text = self.exprs.get(name).cloned().ok_or_else(|| ExprError::Unknown(name.to_string()))?; - let e = parse(&text)?; + // Parse the stored text in place: `parse` refuses an oversized expression before reading + // it, so a long imported expression referenced many times is never copied (CWE-400). + let e = parse(self.exprs.get(name).ok_or_else(|| ExprError::Unknown(name.to_string()))?)?; stack.push(name.to_string()); let r = eval_at_depth(&e, &mut |n, depth| self.value_inner(n, stack, depth), depth); stack.pop(); @@ -400,6 +408,22 @@ mod tests { .unwrap(); } + /// An oversized named expression is kept only as a bounded prefix and still fails, however + /// often it is referenced. + #[test] + fn oversized_named_expression_is_bounded_and_fails() { + let mut env = Env::new(); + let long = format!("1+{}", "é".repeat(500_000)); + env.insert("big", &long); + assert!(env.exprs.get("big").is_some_and(|t| t.len() > MAX_LEN && t.len() <= MAX_LEN + 4)); + assert_eq!(env.value("big"), Err(ExprError::TooDeep)); + for _ in 0..200 { + assert_eq!(env.eval("big"), Err(ExprError::TooDeep)); + } + env.insert("ok", "1+1+1"); + assert_eq!(env.value("ok"), Ok(3.0)); + } + #[test] fn rename_replaces_whole_names_only() { assert_eq!(rename("d1+d10*d1 / (d1_x+2e1)", "d1", "w"), "w+d10*w / (d1_x+2e1)"); From 77142aa79ecaa9c6b9c49120658b4dea93e09dda Mon Sep 17 00:00:00 2001 From: qshh <101165772+qinshah@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:52:31 +0800 Subject: [PATCH 095/100] fonts: scan the Android and HarmonyOS system font directories (#2650) * fonts: scan the Android system font directories System fonts are the only fallback for text whose own font lacks a glyph (CJK in an SHX big font, for example), and Android was falling through to the XDG directories it does not have: a drawing came out in the stroke font with missing-glyph boxes for Chinese. Search /system/fonts and the product and vendor partitions too. The directory list moves out of scan() so the platform's main location is tested directly. * fonts: scan the HarmonyOS system font directories HarmonyOS targets are target_os = "linux" with target_env = "ohos", so they fell into the XDG branch and found no fonts at all: Chinese text in a drawing came out as missing-glyph boxes while the stroke font drew the ASCII around it. Search /system/fonts there too, as on Android. * contributors: add qinshah Credits them by the name on their public GitHub profile. * fonts: document the platform font directories --- contributors/people.toml | 3 +++ crates/fonts/src/ttf.rs | 46 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/contributors/people.toml b/contributors/people.toml index 994c68bb1..de6d22923 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -102,3 +102,6 @@ sync_github_name = true [people.starise] real_name = "Andrea Brandi" + +[people.qinshah] +sync_github_name = true diff --git a/crates/fonts/src/ttf.rs b/crates/fonts/src/ttf.rs index fff4ecde7..bcf610e0a 100644 --- a/crates/fonts/src/ttf.rs +++ b/crates/fonts/src/ttf.rs @@ -5,6 +5,11 @@ //! (`Noto Sans CJK TC`) among installed files with a similar name. TrueType collections //! (`.ttc` / `.otc`) are scanned too; a stem lookup takes their first face. //! +//! System fonts are searched in the platform's own directories: `/System/Library/Fonts` and +//! friends on macOS, `C:\Windows\Fonts` on Windows, `/system/fonts` and the product/vendor +//! partitions on Android and HarmonyOS, and the XDG directories elsewhere. Web builds have no +//! system fonts. +//! //! Two process-wide settings (like AutoCAD's profile variables) control substitution: //! - [`font_alt`] (`FONTALT`): the font used for a text style font that can't be found (an SHX //! file we don't read, a missing TTF). Empty = the built-in stroke font. @@ -270,15 +275,20 @@ fn lock() -> MutexGuard<'static, Db> { g } +/// The platform's system font directories, in search order. #[cfg(not(target_arch = "wasm32"))] -fn scan() -> HashMap { - let mut out = HashMap::new(); +fn font_dirs() -> Vec { let mut dirs: Vec = Vec::new(); if cfg!(target_os = "macos") { dirs.extend(["/System/Library/Fonts", "/System/Library/Fonts/Supplemental", "/Library/Fonts"].map(Into::into)); if let Some(h) = std::env::var_os("HOME") { dirs.push(PathBuf::from(h).join("Library/Fonts")); } + } else if cfg!(any(target_os = "android", target_env = "ohos")) { + // Android and HarmonyOS keep system fonts under /system/fonts; Android devices also use + // the product and vendor partitions for faces of their own. An app may read all of them, + // and directories a device lacks are skipped. + dirs.extend(["/system/fonts", "/product/fonts", "/system_ext/fonts", "/vendor/fonts"].map(Into::into)); } else if cfg!(windows) { dirs.push("C:\\Windows\\Fonts".into()); if let Some(l) = std::env::var_os("LOCALAPPDATA") { @@ -291,7 +301,14 @@ fn scan() -> HashMap { dirs.push(PathBuf::from(h).join(".local/share/fonts")); } } - let mut stack = dirs; + dirs +} + +/// Every font file under [`font_dirs`], by lower-case file stem. +#[cfg(not(target_arch = "wasm32"))] +fn scan() -> HashMap { + let mut out = HashMap::new(); + let mut stack = font_dirs(); let mut seen = 0usize; while let Some(d) = stack.pop() { let Ok(rd) = std::fs::read_dir(&d) else { continue }; @@ -1080,3 +1097,26 @@ mod geometry_budget_tests { assert!(shaped.text.is_empty()); } } + +#[cfg(all(test, not(target_arch = "wasm32")))] +mod scan_tests { + use super::*; + + /// Each platform searches its own system font directories. A branch that is missing or + /// misnamed leaves a device with no fonts at all, so the platform's main location is checked + /// here rather than discovered by a drawing full of missing-glyph boxes. + #[test] + fn the_platforms_system_font_directory_is_searched() { + let dirs = font_dirs(); + #[cfg(target_os = "macos")] + assert!(dirs.iter().any(|d| d == std::path::Path::new("/System/Library/Fonts")), "{dirs:?}"); + #[cfg(target_os = "android")] + assert!(dirs.iter().any(|d| d == std::path::Path::new("/system/fonts")), "{dirs:?}"); + #[cfg(target_env = "ohos")] + assert!(dirs.iter().any(|d| d == std::path::Path::new("/system/fonts")), "{dirs:?}"); + #[cfg(windows)] + assert!(dirs.iter().any(|d| d == std::path::Path::new("C:\\Windows\\Fonts")), "{dirs:?}"); + #[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_env = "ohos"))))] + assert!(dirs.iter().any(|d| d == std::path::Path::new("/usr/share/fonts")), "{dirs:?}"); + } +} From 1b7c705face307317158f8afe5600de97647ef4b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 2 Nov 2026 22:52:35 -0500 Subject: [PATCH 096/100] Dynamic blocks: index kept objects once per listing; copy each value record once (#2678) * Dynamic blocks: index kept objects once per listing and copy each value record once Listing blocks rebuilt a handle index of every kept foreign object for each block with an extension dictionary, multiplying the block count by the object count. The listing now builds one ObjectIndex and asks is_dynamic_block_in per block; dynamic_block answers None before indexing when the block has no extension dictionary. templates() copied a value record's tags once per dictionary key, so a record listed under many keys was copied many times, and it looked up each dictionary entry by scanning every object. It now resolves entries through its handle index and copies each record at most once (CWE-400, CWE-407). * Dynamic blocks: list a block as dynamic from its graph object without walking the graph's members --- crates/doc/src/dynblock.rs | 47 ++++++++++++++--- crates/doc/src/dynblock/apply.rs | 53 +++++++++++++++++--- crates/engine/src/cmd/blocks.rs | 5 +- crates/engine/tests/it/dynamic_block_data.rs | 4 ++ 4 files changed, 96 insertions(+), 13 deletions(-) diff --git a/crates/doc/src/dynblock.rs b/crates/doc/src/dynblock.rs index 392861388..409d403dd 100644 --- a/crates/doc/src/dynblock.rs +++ b/crates/doc/src/dynblock.rs @@ -323,8 +323,9 @@ impl<'a> Cursor<'a> { } } -/// The objects `xdict`'s `ACAD_ENHANCEDBLOCK` evaluation graph lists, when the dictionary has one. -fn graph_members<'a>(objects: &HashMap, xdict: Handle) -> Option> { +/// `xdict`'s `ACAD_ENHANCEDBLOCK` evaluation graph object, when the dictionary has one. Only the +/// dictionary's own tags are read, so asking whether a block is dynamic never walks the graph. +fn evaluation_graph<'a>(objects: &HashMap, xdict: Handle) -> Option<&'a ForeignObject> { let dict = objects.get(&xdict)?; let mut graph = None; let mut tags = dict.tags.iter(); @@ -334,7 +335,12 @@ fn graph_members<'a>(objects: &HashMap, xdict: Handle break; } } - let graph = objects.get(&graph?).filter(|o| o.kind.eq_ignore_ascii_case("ACAD_EVALUATION_GRAPH"))?; + objects.get(&graph?).copied().filter(|o| o.kind.eq_ignore_ascii_case("ACAD_EVALUATION_GRAPH")) +} + +/// The objects `xdict`'s `ACAD_ENHANCEDBLOCK` evaluation graph lists, when the dictionary has one. +fn graph_members<'a>(objects: &HashMap, xdict: Handle) -> Option> { + let graph = evaluation_graph(objects, xdict)?; let members = section(§ions(&graph.tags), "AcDbEvalGraph").iter().filter(|t| t.code == 360).filter_map(|t| Handle::parse_hex(&t.value)); // Deduplicate member handles before resolving them: a graph can list the same member many // times, and parsing each occurrence into its own owned parameter multiplies a single large @@ -343,13 +349,42 @@ fn graph_members<'a>(objects: &HashMap, xdict: Handle Some(members.take(MAX_NODES).filter(|h| seen.insert(*h)).filter_map(|h| objects.get(&h).copied()).collect()) } +/// The kept foreign objects of a drawing by handle. Building it walks every object, so a caller +/// that asks about many blocks (listing them) builds it once and passes it to +/// [`dynamic_block_in`] and [`is_dynamic_block_in`] instead of rebuilding it per block, which +/// multiplies the block count by the object count (CWE-407). +pub struct ObjectIndex<'a>(HashMap); + +impl<'a> ObjectIndex<'a> { + pub fn new(d: &'a Drawing) -> Self { + ObjectIndex(d.foreign.objects.iter().map(|o| (o.handle, o)).collect()) + } +} + +/// The extension dictionary of block `name`'s record, if it has one. +fn block_xdict(d: &Drawing, name: &str) -> Option { + d.foreign.records.get(&("BLOCK_RECORD".to_string(), name.trim().to_ascii_uppercase()))?.xdict +} + /// The dynamic block data of block `name` (any case; `*Model_Space` for a drawing that is a /// block's own drawing), or `None` when it has none. pub fn dynamic_block(d: &Drawing, name: &str) -> Option { - let xdict = d.foreign.records.get(&("BLOCK_RECORD".to_string(), name.trim().to_ascii_uppercase()))?.xdict?; - let objects: HashMap = d.foreign.objects.iter().map(|o| (o.handle, o)).collect(); + // A block without an extension dictionary is never dynamic: answer before indexing objects. + block_xdict(d, name)?; + dynamic_block_in(d, &ObjectIndex::new(d), name) +} + +/// Whether block `name` is a dynamic block (has an evaluation graph), without reading its +/// parameters and actions; `objects` is the drawing's [`ObjectIndex`]. +pub fn is_dynamic_block_in(d: &Drawing, objects: &ObjectIndex<'_>, name: &str) -> bool { + block_xdict(d, name).and_then(|x| evaluation_graph(&objects.0, x)).is_some() +} + +/// [`dynamic_block`] with the drawing's [`ObjectIndex`] already built. +pub fn dynamic_block_in(d: &Drawing, objects: &ObjectIndex<'_>, name: &str) -> Option { + let xdict = block_xdict(d, name)?; let mut out = DynamicBlock::default(); - for o in graph_members(&objects, xdict)? { + for o in graph_members(&objects.0, xdict)? { let secs = sections(&o.tags); let node = Cursor::new(section(&secs, "AcDbEvalExpr")).int(90); let element = Cursor::new(section(&secs, "AcDbBlockElement")).string(300); diff --git a/crates/doc/src/dynblock/apply.rs b/crates/doc/src/dynblock/apply.rs index ecca36257..c22a6df86 100644 --- a/crates/doc/src/dynblock/apply.rs +++ b/crates/doc/src/dynblock/apply.rs @@ -54,7 +54,11 @@ fn record_handle(d: &Drawing, name: &str) -> Option { /// The object entry `key` of dictionary `dict` refers to. fn entry(d: &Drawing, dict: Handle, key: &str) -> Option { - let o = d.foreign.objects.iter().find(|o| o.handle == dict)?; + dictionary_entry(d.foreign.objects.iter().find(|o| o.handle == dict)?, key) +} + +/// The object entry `key` (any case) of dictionary object `o` names. +fn dictionary_entry(o: &ForeignObject, key: &str) -> Option { let mut tags = o.tags.iter(); while let Some(t) = tags.next() { if t.code == 3 && t.value.eq_ignore_ascii_case(key) { @@ -66,7 +70,11 @@ fn entry(d: &Drawing, dict: Handle, key: &str) -> Option { /// Every entry of dictionary `dict`: (key, object). fn entries(d: &Drawing, dict: Handle) -> Vec<(String, Handle)> { - let Some(o) = d.foreign.objects.iter().find(|o| o.handle == dict) else { return Vec::new() }; + d.foreign.objects.iter().find(|o| o.handle == dict).map(dictionary_entries).unwrap_or_default() +} + +/// Every entry of dictionary object `o`: (key, object). +fn dictionary_entries(o: &ForeignObject) -> Vec<(String, Handle)> { let mut out = Vec::new(); let mut key: Option<&str> = None; for t in &o.tags { @@ -692,19 +700,33 @@ fn refill_hatches(b: &mut Block) { /// The value records of the references to the dynamic block whose record is `def_record` (from /// any one of them, by graph id): AutoCAD's layout of each record, class ids included. +/// +/// Each record is copied once at most: a key naming a record an earlier key already took is +/// skipped. Records are copied per key (and copied again per key when a new reference's data is +/// made), so a file listing one large record under many keys would otherwise multiply its size by +/// the key count (CWE-400); AutoCAD gives every key its own record. fn templates(d: &Drawing, def_record: Handle) -> HashMap> { let objects: HashMap = d.foreign.objects.iter().map(|o| (o.handle, o)).collect(); + // Dictionary entries looked up through the index, not by scanning every object per + // dictionary (which is quadratic in the object count). + let entry = |dict: Handle, key: &str| objects.get(&dict).and_then(|o| dictionary_entry(o, key)); let mut out: HashMap> = HashMap::new(); + let mut taken: HashSet = HashSet::new(); for rep in d.foreign.objects.iter().filter(|o| o.kind.eq_ignore_ascii_case("DICTIONARY")) { // A representation dictionary whose data names this dynamic block. - let Some(rd) = entry(d, rep.handle, "AcDbRepData").and_then(|h| objects.get(&h)) else { continue }; + let Some(rd) = dictionary_entry(rep, "AcDbRepData").and_then(|h| objects.get(&h)) else { continue }; if !rd.tags.iter().any(|t| t.code == 340 && Handle::parse_hex(&t.value) == Some(def_record)) { continue; } - let Some(values) = entry(d, rep.handle, "AppDataCache").and_then(|c| entry(d, c, "ACAD_ENHANCEDBLOCKDATA")) else { continue }; - for (k, h) in entries(d, values) { + let Some(values) = dictionary_entry(rep, "AppDataCache").and_then(|c| entry(c, "ACAD_ENHANCEDBLOCKDATA")) else { continue }; + let Some(values) = objects.get(&values) else { continue }; + for (k, h) in dictionary_entries(values) { + if out.contains_key(&k) || taken.contains(&h) { + continue; + } if let Some(x) = objects.get(&h).filter(|x| x.kind.eq_ignore_ascii_case("XRECORD")) { - out.entry(k).or_insert_with(|| x.tags.clone()); + taken.insert(h); + out.insert(k, x.tags.clone()); } } } @@ -1044,4 +1066,23 @@ mod tests { assert_eq!(by_tag(&d, &def), vec![(10, Handle(1)), (20, Handle(2))]); assert_eq!(by_tag(&d, &anon), vec![(20, Handle(5)), (1_000_001, Handle(6))]); } + + #[test] + fn a_value_record_listed_under_many_keys_is_copied_once() { + // One large XRECORD named by many keys of a reference's value dictionary: the templates + // keep one copy, not one per key. + let mut d = Drawing::new_imperial(); + let (def_record, rep, rd, cache, values, record) = (Handle(0x10), Handle(0x20), Handle(0x21), Handle(0x22), Handle(0x23), Handle(0x24)); + let keys: Vec = (0..1000).map(|i| i.to_string()).collect(); + let entries: Vec<(&str, Handle)> = keys.iter().map(|k| (k.as_str(), record)).collect(); + let f = std::sync::Arc::make_mut(&mut d.foreign); + f.objects.push(dictionary(rep, Handle(1), &[("AcDbRepData", rd), ("AppDataCache", cache)])); + f.objects.push(ForeignObject { handle: rd, kind: "ACDB_BLOCKREPRESENTATION_DATA".into(), tags: vec![tag(340, def_record.hex())] }); + f.objects.push(dictionary(cache, rep, &[("ACAD_ENHANCEDBLOCKDATA", values)])); + f.objects.push(dictionary(values, cache, &entries)); + f.objects.push(ForeignObject { handle: record, kind: "XRECORD".into(), tags: (0..1000).map(|_| tag(1, "x")).collect() }); + let t = templates(&d, def_record); + assert_eq!(t.len(), 1, "one record, one template"); + assert_eq!(t.get("0").map(Vec::len), Some(1000)); + } } diff --git a/crates/engine/src/cmd/blocks.rs b/crates/engine/src/cmd/blocks.rs index 258594ff0..45ab8ae15 100644 --- a/crates/engine/src/cmd/blocks.rs +++ b/crates/engine/src/cmd/blocks.rs @@ -379,8 +379,11 @@ fn run_base(s: &mut Session, p: &Value) -> Result { fn run_list(s: &mut Session, _p: &Value) -> Result { let d = s.doc()?; + // One object index for the whole listing: rebuilding it per block multiplies the block count + // by the drawing's kept object count (CWE-407). + let objects = cadcraft_doc::dynblock::ObjectIndex::new(d); let block = |b: &Block| { - let dynamic = cadcraft_doc::dynblock::dynamic_block(d, &b.name).is_some(); + let dynamic = cadcraft_doc::dynblock::is_dynamic_block_in(d, &objects, &b.name); json!({"name": b.name, "entities": b.entities.len(), "base": [b.base.x, b.base.y], "description": b.description, "dynamic": dynamic}) }; Ok(json!({ "blocks": d.blocks.values().filter(|b| !b.anonymous).map(|b| block(b)).collect::>() })) diff --git a/crates/engine/tests/it/dynamic_block_data.rs b/crates/engine/tests/it/dynamic_block_data.rs index 5b4cb11e3..2e3f0baaa 100644 --- a/crates/engine/tests/it/dynamic_block_data.rs +++ b/crates/engine/tests/it/dynamic_block_data.rs @@ -83,6 +83,10 @@ fn blocks_without_dynamic_data_and_bad_names() { s.execute("block", &json!({ "name": "Window", "base": [0, 0], "handles": [l] })).unwrap(); assert_eq!(s.execute("blocks.dynamic", &json!({ "name": "Window" })).unwrap(), json!(null)); assert_eq!(s.execute("blocks.dynamic", &json!({ "name": "*Model_Space" })).unwrap(), json!(null)); + let list = s.execute("blocks.list", &json!({})).unwrap(); + let flags: Vec<_> = list["blocks"].as_array().unwrap().iter().map(|b| (b["name"].as_str().unwrap().to_string(), b["dynamic"].clone())).collect(); + assert!(flags.contains(&("Window".into(), json!(false))), "{flags:?}"); + assert!(flags.contains(&("Door".into(), json!(true))), "{flags:?}"); for p in [json!({}), json!({ "name": 3 }), json!({ "name": "Nope" }), json!(null)] { assert!(s.execute("blocks.dynamic", &p).is_err(), "{p}"); } From 455b2a87cb2df9cbc67b7edec09c2c94e9c8358f Mon Sep 17 00:00:00 2001 From: Tyler Chiriboga Date: Mon, 2 Nov 2026 21:52:39 -0600 Subject: [PATCH 097/100] EXTEND: a polyline's arc end segment runs on round its circle (#2656) EXTEND on an open polyline ran a ray along the chord of an arc end segment and kept the old bulge, so the arc changed radius and centre, or the boundary was reported missed when only the circle reached it. The arc end segment now runs on round its own circle to the nearest crossing ahead, turning the same way, and its bulge becomes that of the total turn, as EXTEND does with an ARC. The polyline also keeps its constant width, elevation and linetype generation, which the rebuilt polyline dropped. Co-authored-by: Tyler Chiriboga <18270301+Tyler-Chiriboga@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/modify/trim.rs | 60 ++++++-- .../tests/it/extend_polyline_arc_end.rs | 137 ++++++++++++++++++ crates/engine/tests/it/main.rs | 1 + 3 files changed, 187 insertions(+), 11 deletions(-) create mode 100644 crates/engine/tests/it/extend_polyline_arc_end.rs diff --git a/crates/engine/src/cmd/modify/trim.rs b/crates/engine/src/cmd/modify/trim.rs index 66eab0be8..9f8e856dd 100644 --- a/crates/engine/src/cmd/modify/trim.rs +++ b/crates/engine/src/cmd/modify/trim.rs @@ -283,6 +283,28 @@ pub(super) fn param_on(s: &Segment, p: Vec2) -> f64 { } } +/// A polyline arc segment on `ga`'s circle that ends at `end` after turning through `sweep` +/// (signed, positive counterclockwise), run on round the circle to the nearest boundary edge: the +/// new end point and the segment's new bulge. `None` when no edge crosses the rest of the circle. +fn extend_arc_segment(ga: &Arc, end: Vec2, sweep: f64, cut: &[Segment]) -> Option<(Vec2, f64)> { + let full = Segment::Arc { arc: Arc { start: 0.0, end: TAU - 1e-12, ..*ga }, ccw: true }; + let a_end = ga.center.angle_to(end); + let ccw = sweep > 0.0; + // How far past `end` each crossing is, turning the way the segment turns; crossings on the + // segment itself (the last |sweep| of a turn) are not ahead of it. + let (x, more) = cut + .iter() + .flat_map(|c| intersect_ext(&full, c, false)) + .map(|x| { + let a = ga.center.angle_to(x); + (x, norm_angle(if ccw { a - a_end } else { a_end - a })) + }) + .filter(|(_, more)| *more > 1e-9 && *more + sweep.abs() < TAU - 1e-9) + .min_by(|a, b| a.1.total_cmp(&b.1))?; + let total = sweep.abs() + more; + Some((x, cadcraft_geom::arc_to_bulge(if ccw { total } else { -total }))) +} + /// Extend the end of `h` nearest `pick` to the nearest boundary edge. pub(crate) fn extend(s: &mut Session, h: Handle, pick: Vec2, edges: Edges) -> Result<()> { if crate::cmd::curves::is_locked(s, h) { @@ -326,18 +348,34 @@ pub(crate) fn extend(s: &mut Session, h: Handle, pick: Vec2, edges: Edges) -> Re let (i_end, i_prev) = if from_end { (n - 1, n - 2) } else { (0, 1) }; let end = vs.get(i_end).map(|v| v.p).unwrap_or_default(); let prev = vs.get(i_prev).map(|v| v.p).unwrap_or_default(); - let dir = (end - prev).normalized(); - let ray = Segment::Line(Line::new(end, end + dir * 1e8)); - let np = cut - .iter() - .flat_map(|c| intersect_ext(&ray, c, false)) - .filter(|x| (*x - end).dot(dir) > 1e-9) - .min_by(|x, y| x.dist(end).total_cmp(&y.dist(end))) - .ok_or_else(|| EngineError::Other("Object does not intersect an edge.".into()))?; - if let Some(v) = vs.get_mut(i_end) { - v.p = np; + // The end segment's bulge, signed for travel from `prev` out to `end`. + let i_seg = if from_end { i_prev } else { i_end }; + let bulge = vs.get(i_seg).map_or(0.0, |v| if from_end { v.bulge } else { -v.bulge }); + let none = || EngineError::Other("Object does not intersect an edge.".into()); + if let Some((ga, _)) = cadcraft_geom::bulge_to_arc(prev, end, bulge) { + // An arc end segment runs on round its circle, as an arc does. + let (np, b) = extend_arc_segment(&ga, end, 4.0 * bulge.atan(), &cut).ok_or_else(none)?; + if let Some(v) = vs.get_mut(i_end) { + v.p = np; + } + if let Some(v) = vs.get_mut(i_seg) { + v.bulge = if from_end { b } else { -b }; + } + } else { + let dir = (end - prev).normalized(); + let ray = Segment::Line(Line::new(end, end + dir * 1e8)); + let np = cut + .iter() + .flat_map(|c| intersect_ext(&ray, c, false)) + .filter(|x| (*x - end).dot(dir) > 1e-9) + .min_by(|x, y| x.dist(end).total_cmp(&y.dist(end))) + .ok_or_else(none)?; + if let Some(v) = vs.get_mut(i_end) { + v.p = np; + } } - lwpoly(vs, false) + // Width, elevation and linetype generation stay as they were. + EntityKind::LwPolyline(cadcraft_doc::LwPolyline { vertices: vs, ..p.clone() }) } k => crate::cmd::modify2::extend_other(k, &cut, pick)?, }; diff --git a/crates/engine/tests/it/extend_polyline_arc_end.rs b/crates/engine/tests/it/extend_polyline_arc_end.rs new file mode 100644 index 000000000..496187767 --- /dev/null +++ b/crates/engine/tests/it/extend_polyline_arc_end.rs @@ -0,0 +1,137 @@ +//! EXTEND on an open polyline whose end segment is an arc: the arc runs on round its own circle +//! to the boundary, as EXTEND does with an ARC, instead of the end vertex sliding along the chord +//! with the old bulge (which changed the arc's radius and centre, or missed the boundary). The +//! polyline keeps its width, elevation and linetype generation. + +use cadcraft_engine::Session; +use cadcraft_engine::doc::{Common, Entity, EntityKind, Handle, LwPolyline}; +use cadcraft_engine::geom::{PolyVertex, Vec2}; +use serde_json::json; + +const TOL: f64 = 1e-9; + +fn add(s: &mut Session, kind: EntityKind) -> String { + let space = s.space(); + let d = s.doc_mut().unwrap(); + let handle = d.new_handle(); + d.space_mut(&space).unwrap().push(Entity { handle, common: Common::default(), kind }); + handle.hex() +} + +fn v(x: f64, y: f64, bulge: f64) -> PolyVertex { + PolyVertex { p: Vec2::new(x, y), bulge, ..Default::default() } +} + +fn pline(vertices: Vec) -> LwPolyline { + LwPolyline { vertices, closed: false, const_width: 0.0, elevation: 0.0, plinegen: false } +} + +fn edge(s: &mut Session, a: [f64; 2], b: [f64; 2]) -> String { + let r = s.execute("line", &json!({ "points": [a, b] })).unwrap(); + r["handles"][0].as_str().unwrap().to_string() +} + +fn polyline(s: &Session, h: &str) -> LwPolyline { + match &s.doc().unwrap().entity(Handle::parse_hex(h).unwrap()).unwrap().kind { + EntityKind::LwPolyline(p) => p.clone(), + k => panic!("not a polyline: {k:?}"), + } +} + +fn near(a: Vec2, x: f64, y: f64) -> bool { + (a.x - x).abs() < 1e-6 && (a.y - y).abs() < 1e-6 +} + +/// tan(22.5°): the bulge of a quarter circle. +fn quarter() -> f64 { + (std::f64::consts::PI / 8.0).tan() +} + +/// The bulge of an arc turning through `deg` degrees (positive counterclockwise). +fn bulge(deg: f64) -> f64 { + (deg.to_radians() / 4.0).tan() +} + +#[test] +fn a_counterclockwise_arc_at_the_end_runs_on_round_its_circle() { + let mut s = Session::new(); + let e = edge(&mut s, [-50.0, -5.0], [50.0, -5.0]); + // A line in from (20,0), then a quarter circle about the origin from (10,0) to (0,10). + let mut pl = pline(vec![v(20.0, 0.0, 0.0), v(10.0, 0.0, quarter()), v(0.0, 10.0, 0.0)]); + pl.const_width = 0.5; + pl.elevation = 3.0; + pl.plinegen = true; + let h = add(&mut s, EntityKind::LwPolyline(pl)); + s.execute("extend", &json!({ "handle": h, "pick": [0.5, 9.9], "edges": [e] })).unwrap(); + let p = polyline(&s, &h); + assert_eq!(p.vertices.len(), 3); + // Round the circle (radius 10) through (-10,0) to y = -5: a 210° turn in all. + let end = p.vertices[2].p; + assert!(near(end, -(75.0f64.sqrt()), -5.0), "end {end:?}"); + assert!((p.vertices[1].bulge - bulge(210.0)).abs() < TOL, "bulge {}", p.vertices[1].bulge); + assert!(near(p.vertices[0].p, 20.0, 0.0) && near(p.vertices[1].p, 10.0, 0.0)); + assert_eq!((p.const_width, p.elevation, p.plinegen), (0.5, 3.0, true)); +} + +#[test] +fn an_arc_at_the_start_runs_on_backwards_round_its_circle() { + let mut s = Session::new(); + let e = edge(&mut s, [-50.0, -5.0], [50.0, -5.0]); + // The same path drawn the other way: clockwise from (0,10) to (10,0), then out to (20,0). + let h = add(&mut s, EntityKind::LwPolyline(pline(vec![v(0.0, 10.0, -quarter()), v(10.0, 0.0, 0.0), v(20.0, 0.0, 0.0)]))); + s.execute("extend", &json!({ "handle": h, "pick": [0.5, 9.9], "edges": [e] })).unwrap(); + let p = polyline(&s, &h); + assert!(near(p.vertices[0].p, -(75.0f64.sqrt()), -5.0), "start {:?}", p.vertices[0].p); + assert!((p.vertices[0].bulge + bulge(210.0)).abs() < TOL, "bulge {}", p.vertices[0].bulge); + assert!(near(p.vertices[1].p, 10.0, 0.0) && near(p.vertices[2].p, 20.0, 0.0)); +} + +#[test] +fn a_clockwise_arc_at_the_end_runs_on_clockwise() { + let mut s = Session::new(); + let e = edge(&mut s, [-50.0, 5.0], [50.0, 5.0]); + // Clockwise about the origin from (10,0) to (0,-10); on round through (-10,0) to y = 5. + let h = add(&mut s, EntityKind::LwPolyline(pline(vec![v(20.0, 0.0, 0.0), v(10.0, 0.0, -quarter()), v(0.0, -10.0, 0.0)]))); + s.execute("extend", &json!({ "handle": h, "pick": [0.5, -9.9], "edges": [e] })).unwrap(); + let p = polyline(&s, &h); + assert!(near(p.vertices[2].p, -(75.0f64.sqrt()), 5.0), "end {:?}", p.vertices[2].p); + assert!((p.vertices[1].bulge + bulge(210.0)).abs() < TOL, "bulge {}", p.vertices[1].bulge); +} + +#[test] +fn the_nearest_crossing_ahead_on_the_circle_wins() { + let mut s = Session::new(); + // A boundary crossing the circle twice ahead of the end: x = -6 at about 127° and 233°. + let e = edge(&mut s, [-6.0, -50.0], [-6.0, 50.0]); + let h = add(&mut s, EntityKind::LwPolyline(pline(vec![v(20.0, 0.0, 0.0), v(10.0, 0.0, quarter()), v(0.0, 10.0, 0.0)]))); + s.execute("extend", &json!({ "handle": h, "pick": [0.5, 9.9], "edges": [e] })).unwrap(); + let p = polyline(&s, &h); + assert!(near(p.vertices[2].p, -6.0, 8.0), "end {:?}", p.vertices[2].p); + let turn = 8.0f64.atan2(-6.0).to_degrees(); + assert!((p.vertices[1].bulge - bulge(turn)).abs() < TOL, "bulge {}", p.vertices[1].bulge); +} + +#[test] +fn an_arc_end_whose_circle_misses_every_edge_is_left_alone() { + let mut s = Session::new(); + // The chord (10,0)→(0,10) runs on to this edge, but the circle never reaches it. + let e = edge(&mut s, [-50.0, 30.0], [50.0, 30.0]); + let before = pline(vec![v(20.0, 0.0, 0.0), v(10.0, 0.0, quarter()), v(0.0, 10.0, 0.0)]); + let h = add(&mut s, EntityKind::LwPolyline(before.clone())); + assert!(s.execute("extend", &json!({ "handle": h, "pick": [0.5, 9.9], "edges": [e] })).is_err()); + assert_eq!(polyline(&s, &h), before); +} + +#[test] +fn a_straight_end_keeps_the_polyline_width_and_elevation() { + let mut s = Session::new(); + let e = edge(&mut s, [50.0, -50.0], [50.0, 50.0]); + let mut pl = pline(vec![v(0.0, 0.0, 0.0), v(10.0, 0.0, 0.0)]); + pl.const_width = 0.5; + pl.elevation = 3.0; + let h = add(&mut s, EntityKind::LwPolyline(pl)); + s.execute("extend", &json!({ "handle": h, "pick": [9.0, 0.0], "edges": [e] })).unwrap(); + let p = polyline(&s, &h); + assert!(near(p.vertices[1].p, 50.0, 0.0), "end {:?}", p.vertices[1].p); + assert_eq!((p.const_width, p.elevation), (0.5, 3.0)); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 38fb4d005..b6ec02d60 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -46,6 +46,7 @@ mod draworder; mod dyn_input; mod dynamic_block_data; mod dynamic_grips; +mod extend_polyline_arc_end; mod fillet_chamfer; mod fix_holds_grips; mod followups_626; From 7b6be7835f29e427aa2c785e8afcbe01c9082996 Mon Sep 17 00:00:00 2001 From: Thiago Matheus Campelo Ferreira <133534183+euthiferreira@users.noreply.github.com> Date: Tue, 3 Nov 2026 00:53:20 -0300 Subject: [PATCH 098/100] fix(io): a solid HATCH keeps its angle, scale and double flag (#2665) * fix(io): a solid HATCH keeps its angle, scale and double flag The writer wrote groups 52 (angle), 41 (scale) and 77 (double) for pattern fills only, so a SOLID hatch read with them lost them on save. Write them for solid fills too, as read. Co-Authored-By: Claude Opus 5.5 * test(io): fixture for a solid HATCH keeps angle, scale and double flag Add a synthetic DXF with a SOLID and an ANSI31 hatch and a test that saves it and compares groups 52, 41 and 77 of each hatch by handle. Co-Authored-By: Claude Haiku 5.5 * docs(io): document the functions this change touches Add doc comments to the functions the diff changes whose complexity is above one, so the docstring coverage check passes. Co-Authored-By: Claude Haiku 5.5 * Drop the DXF fixture: the inline test covers the fix; test data belongs in a corpus repo Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Brandon Thomas --- crates/io/src/dxf_write/entity.rs | 11 ++++++++--- crates/io/src/tests.rs | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/io/src/dxf_write/entity.rs b/crates/io/src/dxf_write/entity.rs index ee9c18d33..6e1d84270 100644 --- a/crates/io/src/dxf_write/entity.rs +++ b/crates/io/src/dxf_write/entity.rs @@ -600,6 +600,9 @@ fn write_annotations_entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, pap } } +/// Writes a HATCH: its pattern name, solid and associative flags, its boundary loops, and the +/// pattern angle, scale and double flag (written for solid hatches too). Pattern lines are written +/// only for non-solid hatches. fn write_hatch_entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: bool, _cx: &Ctx) { if let EntityKind::Hatch(h) = &e.kind { w.s(0, "HATCH"); @@ -634,10 +637,12 @@ fn write_hatch_entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: bo } w.i(75, i64::from(h.style)); w.i(76, 1); + // Angle, scale and double flag are written for solid fills too, as read (AutoCAD and + // other writers keep them on SOLID hatches; readers ignore them there). + w.f(52, h.angle.to_degrees()); + w.f(41, h.scale); + w.i(77, 0); if !h.solid { - w.f(52, h.angle.to_degrees()); - w.f(41, h.scale); - w.i(77, 0); // The lines read from the file, else the library's pattern of that name. let lines = if h.pattern_lines.is_empty() { cadcraft_doc::library::pattern_for(d, &h.pattern).map(|p| p.lines).unwrap_or_default() diff --git a/crates/io/src/tests.rs b/crates/io/src/tests.rs index fca680385..46536f24e 100644 --- a/crates/io/src/tests.rs +++ b/crates/io/src/tests.rs @@ -2896,3 +2896,19 @@ fn non_unicode_names_pick_the_format_by_extension() { assert!(write(&d, name("png")).unwrap().starts_with(b"\x89PNG")); assert!(write(&d, name("dwt")).is_ok(), "a template is written as DWG"); } + +/// A SOLID hatch keeps its angle (52), scale (41) and double flag (77) through a save: the writer +/// wrote them for pattern fills only. +#[test] +fn solid_hatch_keeps_angle_scale_and_double_flag() { + let src = "0\nSECTION\n2\nENTITIES\n0\nHATCH\n5\n40\n8\n0\n100\nAcDbHatch\n10\n0\n20\n0\n30\n0\n2\nSOLID\n70\n1\n71\n0\n\ +91\n1\n92\n2\n72\n0\n73\n1\n93\n4\n10\n0\n20\n0\n10\n40\n20\n0\n10\n40\n20\n40\n10\n0\n20\n40\n97\n0\n\ +75\n0\n76\n1\n52\n30\n41\n2\n77\n0\n98\n0\n0\nENDSEC\n0\nEOF\n"; + let out = write_dxf(&read_dxf(src.as_bytes()).unwrap()); + let pairs = dxf_pairs(&out); + let h = dxf_records(&pairs, "HATCH")[0]; + let f = |code: &str| group(h, code).and_then(|v| v.parse::().ok()); + assert!(f("52").is_some_and(|a| (a - 30.0).abs() < 1e-9), "{h:?}"); + assert!(f("41").is_some_and(|s| (s - 2.0).abs() < 1e-12), "{h:?}"); + assert_eq!(group(h, "77"), Some("0"), "{h:?}"); +} From 4c00f8b9e3fa2515986f62809a5cf80f038a46d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9C=81=E4=BC=AA?= <56018602+r4x2t7@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:53:24 +0800 Subject: [PATCH 099/100] FIND: a Find and Replace window, not only the command line (#2607) * FIND: report the text around each match The result list needs to show what matched, not just its handle, so each entry now carries a snippet of the text around the first hit, the hit's offset in it and its length. Offsets count characters, not bytes, so a Chinese or Vietnamese drawing never splits a glyph. Two optional parameters let a window search part of the space: `types` narrows the search by object type name and `handles` limits it to the objects a caller has already listed, which is what makes replacing a single row possible. Both default to the old behaviour, so scripts, LISP and the MCP server get the answer they always got. * FIND: a Find and Replace window FIND only had a command line: it asked for the text, ran, and printed a count, and the palette's Find and Replace tile opened the same prompt. The answer lived in the drawing and the place to read it was the command line. This adds a window behind the same command. The result list shows each object with the hit lit up, a click selects that object and zooms to it, Replace edits the row under the cursor and Replace All the rows on the list. The window stays open while the drawing is edited, because finding and replacing is a loop: look, locate, edit, look again. A JSON call carries its parameters, so scripts, LISP and the MCP server still get the engine's answer; only the bare command opens the window. Cmd+F reaches it from anywhere, and the twenty labels it adds are in every interface language. * FIND: clear the window's state when it closes or the drawing changes The result rows hold handles, and a handle only means something in the drawing it was found in. The state now lives exactly as long as the window does, and an epoch change while the window sits open clears the rows instead of letting Replace All edit an object the search never matched. A successful replacement keeps the engine's replacement report rather than letting the re-search overwrite it with a match count. * FIND: an empty replacement is a deletion, and stale rows die on any change Replacing with nothing deletes the matched text, which is a legitimate replacement, so the buttons no longer require text in the Replace field. The result rows answer one question - this text, with these switches, in this drawing and space. Editing the search term or either switch now clears the rows and owes a fresh search, and so does switching between model space and a layout: a handle means nothing outside the space it was found in. * rustfmt: sort the module list after the merge Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Brandon Thomas Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/utility.rs | 107 ++++++++++- crates/ui-egui/src/dialogs.rs | 1 + crates/ui-egui/src/finddlg.rs | 294 ++++++++++++++++++++++++++++++ crates/ui-egui/src/i18n/ar.tsv | 12 ++ crates/ui-egui/src/i18n/bs.tsv | 12 ++ crates/ui-egui/src/i18n/cs.tsv | 12 ++ crates/ui-egui/src/i18n/de.tsv | 12 ++ crates/ui-egui/src/i18n/el.tsv | 12 ++ crates/ui-egui/src/i18n/es.tsv | 12 ++ crates/ui-egui/src/i18n/fr.tsv | 12 ++ crates/ui-egui/src/i18n/hu.tsv | 12 ++ crates/ui-egui/src/i18n/it.tsv | 12 ++ crates/ui-egui/src/i18n/ko.tsv | 12 ++ crates/ui-egui/src/i18n/pt_br.tsv | 12 ++ crates/ui-egui/src/i18n/ru.tsv | 12 ++ crates/ui-egui/src/i18n/sv.tsv | 12 ++ crates/ui-egui/src/i18n/tests.rs | 1 + crates/ui-egui/src/i18n/tr.tsv | 12 ++ crates/ui-egui/src/i18n/uk.tsv | 12 ++ crates/ui-egui/src/i18n/vi.tsv | 12 ++ crates/ui-egui/src/i18n/zh.tsv | 12 ++ crates/ui-egui/src/lib.rs | 2 + crates/ui-egui/src/menus.rs | 11 +- 23 files changed, 616 insertions(+), 4 deletions(-) create mode 100644 crates/ui-egui/src/finddlg.rs diff --git a/crates/engine/src/cmd/utility.rs b/crates/engine/src/cmd/utility.rs index 97678ae24..8faab4f9c 100644 --- a/crates/engine/src/cmd/utility.rs +++ b/crates/engine/src/cmd/utility.rs @@ -26,7 +26,10 @@ pub fn specs() -> Vec { .params("{mode: point|distance|angle|intersection, points: [[x,y],...]} → {value} | {point}") .noundo() .interactive(|_| Ok(Box::::default())), - CommandSpec::new("find", "Find...", run_find).menu(&["Edit", "Find..."]).params("{find, replace?, matchCase?: bool, wholeWord?: bool}"), + CommandSpec::new("find", "Find...", run_find).menu(&["Edit", "Find..."]).params( + "{find, replace?, matchCase?: bool, wholeWord?: bool, types?: [\"Text\", \"MText\", ...], handles?: [...]} → \ + {matches: [{handle, type, count, snippet, hit, hitLen}], total, message}", + ), CommandSpec::new("measuregeom", "Measure Geometry", run_measuregeom) .params("{mode: distance|radius|angle|area|volume, ...}") .noundo() @@ -194,6 +197,46 @@ fn run_quickcalc_pick(s: &mut Session, p: &Value) -> Result { // ---------------- FIND ---------------- +/// Characters of context kept on each side of a hit in the Find dialog's result list. +const SNIPPET_CONTEXT: usize = 18; + +/// The text around the first hit of `find` in `text`, for the result list: at most +/// [`SNIPPET_CONTEXT`] characters before the hit and as many after it, the hit's offset in the +/// snippet (in characters) and its length (in characters). +/// +/// The offset and length are character counts, not bytes: the dialog slices the snippet by +/// character so a Chinese or Vietnamese drawing never splits a glyph. Lower-casing can change a +/// string's character count in rare scripts, so every slice goes through `get` and a snippet that +/// cannot be cut is reported as absent rather than guessed at. +fn snippet_at(text: &str, find: &str, case: bool, whole: bool) -> Option<(String, usize, usize)> { + if find.is_empty() { + return None; + } + let (hay, needle) = if case { (text.to_string(), find.to_string()) } else { (text.to_lowercase(), find.to_lowercase()) }; + let mut at = 0; + let pos = loop { + let rest = hay.get(at..)?; + let p = rest.find(&needle)? + at; + let end = p + needle.len(); + let boundary = |i: usize| hay.get(..i).and_then(|s| s.chars().last()).is_none_or(|c| !c.is_alphanumeric()); + let after = hay.get(end..).and_then(|s| s.chars().next()).is_none_or(|c| !c.is_alphanumeric()); + if !whole || (boundary(p) && after) { + break p; + } + at = end.max(p + 1); + if at >= hay.len() { + return None; + } + }; + let start = hay.get(..pos)?.chars().count(); + let hit_len = needle.chars().count(); + let chars: Vec = text.chars().collect(); + let from = start.saturating_sub(SNIPPET_CONTEXT); + let to = start.saturating_add(hit_len).saturating_add(SNIPPET_CONTEXT).min(chars.len()); + let snippet: String = chars.get(from..to)?.iter().collect(); + Some((snippet, start.saturating_sub(from), hit_len)) +} + fn replace_in(text: &str, find: &str, rep: &str, case: bool, whole: bool) -> (String, usize) { if find.is_empty() { return (text.to_string(), 0); @@ -228,21 +271,40 @@ fn run_find(s: &mut Session, p: &Value) -> Result { let rep = str_param(p, "replace").map(str::to_string); let case = bool_or(p, "matchCase", false); let whole = bool_or(p, "wholeWord", false); + // Only the named object types, when the dialog narrows the search; `type_name()` is the same + // name the result list shows, so a caller never has to guess the spelling. + let types: Option> = + p.get("types").and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_str).map(str::to_ascii_lowercase).collect()); let space = s.space(); let d = s.doc()?; - let hs: Vec = d.space(&space).map(|st| st.handles()).unwrap_or_default(); + // The whole current space by default; the Find window narrows it to the objects its result + // list already holds, so Replace All touches exactly what the user can see. + let hs: Vec = match p.get("handles").and_then(Value::as_array) { + Some(a) => a.iter().filter_map(|v| v.as_str().and_then(Handle::parse_hex)).collect(), + None => d.space(&space).map(|st| st.handles()).unwrap_or_default(), + }; let mut found: Vec = Vec::new(); let mut total = 0; let mut edits: Vec<(Handle, EntityKind)> = Vec::new(); for h in hs { let Some(e) = d.entity(h) else { continue }; + if let Some(names) = &types + && !names.iter().any(|n| n == &e.kind.type_name().to_ascii_lowercase()) + { + continue; + } let mut k = e.kind.clone(); let mut hits = 0; + // The first hit's surroundings, taken before a replacement rewrites the text. + let mut snippet: Option<(String, usize, usize)> = None; let r = rep.as_deref().unwrap_or(&find); let mut apply = |t: &mut String| { let (nt, n) = replace_in(t, &find, r, case, whole); if n > 0 { hits += n; + if snippet.is_none() { + snippet = snippet_at(t, &find, case, whole); + } if rep.is_some() { *t = nt; } @@ -263,7 +325,15 @@ fn run_find(s: &mut Session, p: &Value) -> Result { } if hits > 0 { total += hits; - found.push(json!({ "handle": h.hex(), "type": e.kind.type_name(), "count": hits })); + let (text, at, len) = snippet.unwrap_or_default(); + found.push(json!({ + "handle": h.hex(), + "type": e.kind.type_name(), + "count": hits, + "snippet": text, + "hit": at, + "hitLen": len, + })); if rep.is_some() { edits.push((h, k)); } @@ -577,4 +647,35 @@ mod tests { assert_eq!(replace_in("aaa", "a", "b", true, false), ("bbb".to_string(), 3)); assert_eq!(replace_in("x", "", "y", true, false).1, 0); } + + /// `snippet_at` returns the whole text when the hit fills it, with the hit at offset 0. + #[test] + fn snippet_reports_the_hit_offset() { + assert_eq!(snippet_at("BOLT-01", "BOLT-01", true, false), Some(("BOLT-01".to_string(), 0, 7))); + } + + /// Matching ignores case but the snippet keeps the text as it is written, so the result list + /// shows the drawing's own spelling. + #[test] + fn snippet_keeps_the_original_case() { + assert_eq!(snippet_at("法兰 bolt-01 安装孔", "BOLT-01", false, false), Some(("法兰 bolt-01 安装孔".to_string(), 3, 7))); + } + + /// A long text keeps [`SNIPPET_CONTEXT`] characters on each side of the hit, counted in + /// characters so a CJK hit is never split. + #[test] + fn snippet_is_windowed_in_characters() { + let text = format!("{}命中{}", "前".repeat(30), "后".repeat(30)); + let (snippet, hit, len) = snippet_at(&text, "命中", true, false).expect("a hit"); + assert_eq!((hit, len), (SNIPPET_CONTEXT, 2)); + assert_eq!(snippet.chars().count(), SNIPPET_CONTEXT * 2 + 2); + } + + /// A whole-word search skips an embedded hit, and text without a hit has no snippet. + #[test] + fn snippet_respects_whole_word() { + assert_eq!(snippet_at("BOLTS", "BOLT", true, true), None); + assert_eq!(snippet_at("a BOLT b", "BOLT", true, true).map(|s| s.1), Some(2)); + assert_eq!(snippet_at("nothing", "BOLT", true, false), None); + } } diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index acdddb552..1f8035434 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -23,6 +23,7 @@ pub fn show(app: &mut CadApp, ctx: &egui::Context) { "units" => units(app, ctx, &mut open), "hatch" => crate::hatchdlg::dialog(app, ctx, &mut open), "aliasedit" => crate::aliasdlg::dialog(app, ctx, &mut open), + "find" => crate::finddlg::dialog(app, ctx, &mut open), "about" => about(ctx, &mut open), "preview" => crate::exchange::dialog(app, ctx, &mut open), "commands" => commands(app, ctx, &mut open), diff --git a/crates/ui-egui/src/finddlg.rs b/crates/ui-egui/src/finddlg.rs new file mode 100644 index 000000000..7341f3cfa --- /dev/null +++ b/crates/ui-egui/src/finddlg.rs @@ -0,0 +1,294 @@ +//! Find and Replace: the window behind the FIND command. +//! +//! FIND used to be a command line that asked for its parameters and then printed a count. The +//! parameters are still what the engine takes; this window is the surface where a user can see +//! *what* matched, jump to it and replace one object at a time. It stays open while the drawing +//! is edited — find-and-replace in a drawing is a loop (look, locate, edit, look again), so a +//! window that closed on the first result would be closed and reopened all afternoon. +//! +//! Everything the engine cannot answer is left out rather than faked: the search covers the +//! current space, and the note under the switches says so in as many words. + +use egui::{Color32, RichText, Sense, TextFormat}; +use serde_json::{Value, json}; + +use crate::CadApp; +use crate::theme::{self, Tokens}; + +/// One object the search hit, as the engine reports it. +#[derive(Clone, Default)] +struct Row { + handle: String, + kind: String, + count: u64, + /// The text around the first hit, and where the hit sits inside it (characters, not bytes). + snippet: String, + hit: usize, + hit_len: usize, +} + +/// A `u64` from the reply as a `usize`, or 0 when it does not fit: an offset past the end of a +/// snippet is a bad reply, not a reason to fail the search that produced it. +fn as_index(v: Option<&Value>) -> usize { + v.and_then(Value::as_u64).and_then(|n| usize::try_from(n).ok()).unwrap_or(0) +} + +impl Row { + /// Read one `matches` entry; anything missing keeps its default so a partially understood + /// reply still lists something. + fn from_json(v: &Value) -> Self { + Self { + handle: v.get("handle").and_then(Value::as_str).unwrap_or_default().to_string(), + kind: v.get("type").and_then(Value::as_str).unwrap_or_default().to_string(), + count: v.get("count").and_then(Value::as_u64).unwrap_or(0), + snippet: v.get("snippet").and_then(Value::as_str).unwrap_or_default().to_string(), + hit: as_index(v.get("hit")), + hit_len: as_index(v.get("hitLen")), + } + } + + /// The snippet in three pieces: before the hit, the hit, after it. Every slice is by + /// character and every range is clamped, so a reply with a nonsensical offset still draws. + fn pieces(&self) -> (String, String, String) { + let n = self.snippet.chars().count(); + let at = self.hit.min(n); + let end = at.saturating_add(self.hit_len).min(n); + let before: String = self.snippet.chars().take(at).collect(); + let hit: String = self.snippet.chars().skip(at).take(end.saturating_sub(at)).collect(); + let after: String = self.snippet.chars().skip(end).collect(); + (before, hit, after) + } +} + +/// The window's state between frames: kept in egui's temporary store, like the other dialogs, +/// so it lives exactly as long as the window does and never reaches the preferences file. +#[derive(Default, Clone)] +struct State { + find: String, + replace: String, + match_case: bool, + whole_word: bool, + /// The `doc_epoch` and space the rows were found in. A row's handle is only meaningful in + /// the drawing and the space it was found in, so when either moves the rows must go. + epoch: u64, + space: cadcraft_doc::Space, + rows: Vec, + /// The row Enter and Replace act on. + current: Option, + message: String, +} + +/// Run FIND and keep the parsed matches, or the engine's complaint in `message`. +fn search(app: &mut CadApp, st: &mut State) { + if st.find.trim().is_empty() { + st.rows.clear(); + st.current = None; + st.message = crate::i18n::t("Type something to find").to_string(); + return; + } + let params = json!({ "find": st.find, "matchCase": st.match_case, "wholeWord": st.whole_word }); + match app.run("find", params) { + Ok(v) => { + st.rows = v.get("matches").and_then(Value::as_array).map(|a| a.iter().map(Row::from_json).collect()).unwrap_or_default(); + st.current = if st.rows.is_empty() { None } else { Some(0) }; + let total = v.get("total").and_then(Value::as_u64).unwrap_or(0); + let objects = v.get("matches").and_then(Value::as_array).map_or(0, Vec::len); + st.message = format!("{total} match(es) in {objects} object(s)"); + } + Err(e) => { + st.rows.clear(); + st.current = None; + st.message = e; + } + } +} + +/// Select `row` in the drawing and zoom to it: the result list's rows navigate, they do not just +/// report. +fn reveal(app: &mut CadApp, st: &State, index: usize) { + let Some(row) = st.rows.get(index) else { return }; + let Some(h) = cadcraft_doc::Handle::parse_hex(&row.handle) else { return }; + app.session.set_selection(vec![h]); + let _ = app.run("zoom", json!({ "mode": "object" })); +} + +/// Replace inside the objects the result list already holds. `only` limits it to one row, so +/// Replace touches the object the user is looking at and Replace All touches the whole list. +fn replace(app: &mut CadApp, st: &mut State, only: Option) { + if st.find.trim().is_empty() { + return; + } + let handles: Vec = match only { + Some(i) => st.rows.get(i).map(|r| r.handle.clone()).into_iter().collect(), + None => st.rows.iter().map(|r| r.handle.clone()).collect(), + }; + if handles.is_empty() { + return; + } + let params = json!({ + "find": st.find, + "replace": st.replace, + "matchCase": st.match_case, + "wholeWord": st.whole_word, + "handles": handles, + }); + match app.run("find", params) { + Ok(v) => { + // The engine's reply reports what the replacement did; the re-search below would + // overwrite that message with a match count, so the replacement report is what + // the user ends up reading. + let replaced = v.get("message").and_then(Value::as_str).unwrap_or_default().to_string(); + search(app, st); + if !replaced.is_empty() { + st.message = replaced; + } + } + Err(e) => st.message = e, + } +} + +/// The Find and Replace window. +pub fn dialog(app: &mut CadApp, ctx: &egui::Context, open: &mut bool) { + let t = Tokens::get(); + let sid = egui::Id::unique("find_state"); + let mut st: State = ctx.data_mut(|d| d.get_temp(sid)).unwrap_or_default(); + // A handle from an earlier drawing or space now names whatever object took its place; if + // Replace All ran on such a row it would edit an object the search never matched. Clearing + // the rows on an epoch or space change costs a re-click on Find All, which is the cheap + // side of that trade. + if *open && (st.epoch != app.session.doc_epoch || st.space != app.session.space()) { + st.epoch = app.session.doc_epoch; + st.space = app.session.space(); + st.rows.clear(); + st.current = None; + st.message.clear(); + } + // The rows answer one question — this text, with these switches, in this drawing. Editing + // any of them makes the rows stale: Replace All would rewrite with the old switches while + // the user is looking at the new ones, so the rows go and a fresh search is owed. + let criteria = (st.find.clone(), st.match_case, st.whole_word); + let mut do_search = false; + let mut do_replace: Option> = None; + let mut do_select_all = false; + let mut pick: Option = None; + egui::Window::new(crate::tl!("Find and Replace")) + .id(egui::Id::unique("Find and Replace")) + .open(open) + .collapsible(false) + .resizable(true) + .default_width(480.0) + .default_height(400.0) + .show(ctx, |ui| { + egui::Grid::new("find_fields").num_columns(2).spacing(egui::vec2(10.0, 6.0)).show(ui, |ui| { + ui.label(crate::tl!("Find what:")); + let r = ui.add(egui::TextEdit::singleline(&mut st.find).desired_width(f32::INFINITY).hint_text(crate::tl!("Text to find"))); + if r.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) { + do_search = true; + } + ui.end_row(); + ui.label(crate::tl!("Replace with:")); + ui.add(egui::TextEdit::singleline(&mut st.replace).desired_width(f32::INFINITY).hint_text(crate::tl!("Replacement text"))); + ui.end_row(); + }); + ui.horizontal(|ui| { + ui.checkbox(&mut st.match_case, crate::tl!("Match case")); + ui.checkbox(&mut st.whole_word, crate::tl!("Whole words")); + }); + ui.label(RichText::new(crate::tl!("Searches the current space.")).color(t.text_faint).small()); + ui.add_space(2.0); + ui.separator(); + let found = if st.rows.is_empty() { crate::tl!("Nothing found").to_string() } else { st.rows.len().to_string() }; + ui.label(RichText::new(format!("{} · {found}", crate::tl!("Results"))).color(t.text_dim).small()); + let body_h = (ui.available_height() - 76.0).clamp(80.0, 280.0); + egui::Frame::NONE.fill(t.field).corner_radius(3).inner_margin(egui::Margin::same(2)).show(ui, |ui| { + egui::ScrollArea::vertical().max_height(body_h).auto_shrink([false, false]).show(ui, |ui| { + ui.set_width(ui.available_width()); + for (i, row) in st.rows.iter().enumerate() { + let bg = if st.current == Some(i) { t.select_bg } else { Color32::TRANSPARENT }; + let inner = egui::Frame::NONE.fill(bg).corner_radius(2).inner_margin(egui::Margin::symmetric(6, 3)).show(ui, |ui| { + ui.set_width(ui.available_width()); + ui.horizontal(|ui| { + ui.label(RichText::new(crate::i18n::t(&row.kind)).color(t.text_dim).small()); + ui.label(RichText::new(row.handle.as_str()).color(t.text_faint).small().monospace()); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + ui.label(RichText::new(format!("×{}", row.count)).color(t.text_faint).small()); + }); + }); + let (before, hit, after) = row.pieces(); + let mut job = egui::text::LayoutJob::default(); + let plain = TextFormat { font_id: theme::small(), color: t.text, ..Default::default() }; + let marked = TextFormat { font_id: theme::mono(), color: accent(), ..Default::default() }; + job.append(&before, 0.0, plain.clone()); + job.append(&hit, 0.0, marked); + job.append(&after, 0.0, plain); + ui.add(egui::Label::new(job).wrap()); + }); + let click = ui.interact(inner.response.rect, ui.scope_id().with(("find_row", i)), Sense::click()); + if click.clicked() { + st.current = Some(i); + pick = Some(i); + } + } + }); + }); + ui.add_space(4.0); + ui.horizontal(|ui| { + if ui.button(crate::tl!("Find All")).clicked() { + do_search = true; + } + let has = !st.rows.is_empty(); + if ui.add_enabled(has, egui::Button::new(crate::tl!("Select All"))).clicked() { + do_select_all = true; + } + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + // An empty replacement is deleting the matched text, which is a legitimate + // replacement, so the buttons care only about having something to act on. + if ui.add_enabled(has, egui::Button::new(crate::tl!("Replace All"))).clicked() { + do_replace = Some(None); + } + if ui.add_enabled(st.current.is_some(), egui::Button::new(crate::tl!("Replace"))).clicked() { + do_replace = Some(st.current); + } + }); + }); + if !st.message.is_empty() { + ui.label(RichText::new(st.message.as_str()).color(t.text_faint).small()); + } + }); + if let Some(i) = pick { + reveal(app, &st, i); + } + if criteria != (st.find.clone(), st.match_case, st.whole_word) { + st.rows.clear(); + st.current = None; + if !do_search { + st.message.clear(); + } + } + if do_search { + search(app, &mut st); + } + if do_select_all { + let hs: Vec = st.rows.iter().filter_map(|r| cadcraft_doc::Handle::parse_hex(&r.handle)).collect(); + if !hs.is_empty() { + app.session.set_selection(hs); + st.message = format!("{} object(s) selected", st.rows.len()); + } + } + if let Some(only) = do_replace { + replace(app, &mut st, only); + } + // The state lives exactly as long as the window does: a closed window leaves nothing behind, + // so reopening it reads the drawing instead of a stale result list. + if *open { + ctx.data_mut(|d| d.insert_temp(sid, st)); + } else { + ctx.data_mut(|d| d.remove_temp::(sid)); + } +} + +/// The colour a hit is drawn in: the theme's point colour, the same orange the drawing uses for +/// points and grips, so the highlight belongs to the drawing rather than to the interface. +fn accent() -> Color32 { + Tokens::get().icon_point +} diff --git a/crates/ui-egui/src/i18n/ar.tsv b/crates/ui-egui/src/i18n/ar.tsv index 7713378d9..cf5741cdd 100644 --- a/crates/ui-egui/src/i18n/ar.tsv +++ b/crates/ui-egui/src/i18n/ar.tsv @@ -1368,3 +1368,15 @@ menu View عرض overrides built-in يتجاوز المدمج {alias} is already defined; select it and use Edit. {alias} معرَّف مسبقاً؛ حدده واستخدم «تحرير». {command} is an unknown command. {command} أمر غير معروف. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/bs.tsv b/crates/ui-egui/src/i18n/bs.tsv index b349db278..957c48993 100644 --- a/crates/ui-egui/src/i18n/bs.tsv +++ b/crates/ui-egui/src/i18n/bs.tsv @@ -1444,3 +1444,15 @@ menu Modify Izmijeni Save changes to block "{name}"? Sačuvati promjene u bloku „{name}”? Layouts Rasporedi Associate Poveži + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsv index 61dc1b2b6..67a6e90f6 100644 --- a/crates/ui-egui/src/i18n/cs.tsv +++ b/crates/ui-egui/src/i18n/cs.tsv @@ -1224,3 +1224,15 @@ menu Modify Modifikace Check for Updates… Zkontrolovat aktualizace… CADCraft is up to date. CADCraft je aktuální. Could not check for updates. Aktualizace se nepodařilo zkontrolovat. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/de.tsv b/crates/ui-egui/src/i18n/de.tsv index f36e9ed02..0925cb6af 100644 --- a/crates/ui-egui/src/i18n/de.tsv +++ b/crates/ui-egui/src/i18n/de.tsv @@ -1041,3 +1041,15 @@ Check for Updates… Nach Updates suchen… CADCraft is up to date. CADCraft ist auf dem neuesten Stand. Could not check for updates. Die Suche nach Updates ist fehlgeschlagen. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/el.tsv b/crates/ui-egui/src/i18n/el.tsv index 528dacf55..3bcf59c0c 100644 --- a/crates/ui-egui/src/i18n/el.tsv +++ b/crates/ui-egui/src/i18n/el.tsv @@ -1335,3 +1335,15 @@ menu View Προβολή Check for Updates… Έλεγχος για ενημερώσεις… CADCraft is up to date. Το CADCraft είναι ενημερωμένο. Could not check for updates. Δεν ήταν δυνατός ο έλεγχος για ενημερώσεις. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsv index 457341c36..f0e6471ee 100644 --- a/crates/ui-egui/src/i18n/es.tsv +++ b/crates/ui-egui/src/i18n/es.tsv @@ -1255,3 +1255,15 @@ Check for Updates… Buscar actualizaciones… CADCraft is up to date. CADCraft está actualizado. Could not check for updates. No se pudo buscar actualizaciones. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsv index 62c77c3f9..2f391809e 100644 --- a/crates/ui-egui/src/i18n/fr.tsv +++ b/crates/ui-egui/src/i18n/fr.tsv @@ -1014,3 +1014,15 @@ Check for Updates… Rechercher des mises à jour… CADCraft is up to date. CADCraft est à jour. Could not check for updates. Impossible de rechercher les mises à jour. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/hu.tsv b/crates/ui-egui/src/i18n/hu.tsv index e486f6337..b87edd3c3 100644 --- a/crates/ui-egui/src/i18n/hu.tsv +++ b/crates/ui-egui/src/i18n/hu.tsv @@ -1346,3 +1346,15 @@ menu View Nézet Check for Updates… Frissítések keresése… CADCraft is up to date. A CADCraft naprakész. Could not check for updates. A frissítések keresése nem sikerült. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/it.tsv b/crates/ui-egui/src/i18n/it.tsv index b437397b6..d69b4fa6c 100644 --- a/crates/ui-egui/src/i18n/it.tsv +++ b/crates/ui-egui/src/i18n/it.tsv @@ -1364,3 +1364,15 @@ menu Modify Edita Check for Updates… Verifica aggiornamenti… CADCraft is up to date. CADCraft è aggiornato. Could not check for updates. Impossibile verificare gli aggiornamenti. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/ko.tsv b/crates/ui-egui/src/i18n/ko.tsv index 2f269b051..3f01bb695 100644 --- a/crates/ui-egui/src/i18n/ko.tsv +++ b/crates/ui-egui/src/i18n/ko.tsv @@ -1244,3 +1244,15 @@ menu Modify 수정 Check for Updates… 업데이트 확인… CADCraft is up to date. CADCraft가 최신 버전입니다. Could not check for updates. 업데이트를 확인하지 못했습니다. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/pt_br.tsv b/crates/ui-egui/src/i18n/pt_br.tsv index 3d8f03449..3d1791c1b 100644 --- a/crates/ui-egui/src/i18n/pt_br.tsv +++ b/crates/ui-egui/src/i18n/pt_br.tsv @@ -1253,6 +1253,18 @@ Check for Updates… Verificar atualizações… CADCraft is up to date. O CADCraft está atualizado. Could not check for updates. Não foi possível verificar atualizações. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found # Strings the sibling catalogs translate that pt-BR did not yet cover (translated 2026-10-31). Add: Pick points Adicionar: indicar pontos Add: Select objects Adicionar: selecionar objetos diff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsv index 619988f45..f1a5124f0 100644 --- a/crates/ui-egui/src/i18n/ru.tsv +++ b/crates/ui-egui/src/i18n/ru.tsv @@ -1015,3 +1015,15 @@ Check for Updates… Проверить обновления… CADCraft is up to date. У вас последняя версия CADCraft. Could not check for updates. Не удалось проверить обновления. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/sv.tsv b/crates/ui-egui/src/i18n/sv.tsv index ecfe35b40..4d86a21be 100644 --- a/crates/ui-egui/src/i18n/sv.tsv +++ b/crates/ui-egui/src/i18n/sv.tsv @@ -1448,3 +1448,15 @@ menu Modify Ändra Check for Updates… Sök efter uppdateringar… CADCraft is up to date. CADCraft är uppdaterat. Could not check for updates. Det gick inte att söka efter uppdateringar. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/tests.rs b/crates/ui-egui/src/i18n/tests.rs index 24fac6a79..f9f41c798 100644 --- a/crates/ui-egui/src/i18n/tests.rs +++ b/crates/ui-egui/src/i18n/tests.rs @@ -113,6 +113,7 @@ fn missing_marked_ui_literals(language: &str) -> BTreeSet { "quick", "vpscale", "options/dialog", + "finddlg", ]; let src = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); let mut files = Vec::new(); diff --git a/crates/ui-egui/src/i18n/tr.tsv b/crates/ui-egui/src/i18n/tr.tsv index cc8576aa7..26308975f 100644 --- a/crates/ui-egui/src/i18n/tr.tsv +++ b/crates/ui-egui/src/i18n/tr.tsv @@ -1349,3 +1349,15 @@ menu View Görünüm Check for Updates… Güncellemeleri denetle… CADCraft is up to date. CADCraft güncel. Could not check for updates. Güncellemeler denetlenemedi. + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/uk.tsv b/crates/ui-egui/src/i18n/uk.tsv index 8d8c6b875..5d0660112 100644 --- a/crates/ui-egui/src/i18n/uk.tsv +++ b/crates/ui-egui/src/i18n/uk.tsv @@ -1373,3 +1373,15 @@ CADCraft is up to date. Встановлено найновішу версію CADCraft. Could not check for updates. Не вдалося перевірити оновлення. Save changes to block "{name}"? Зберегти зміни в блоці «{name}»? + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/vi.tsv b/crates/ui-egui/src/i18n/vi.tsv index f1b8dca7b..34eb084eb 100644 --- a/crates/ui-egui/src/i18n/vi.tsv +++ b/crates/ui-egui/src/i18n/vi.tsv @@ -1448,3 +1448,15 @@ menu Modify Hiệu chỉnh Change Properties Đổi thuộc tính Font Phông chữ Save Layout as Template Lưu layout thành mẫu + Find All Find All + Find what: Find what: + Match case Match case + Replace Replace + Replace All Replace All + Replace with: Replace with: + Replacement text Replacement text + Results Results + Searches the current space. Searches the current space. + Text to find Text to find + Whole words Whole words + Nothing found Nothing found diff --git a/crates/ui-egui/src/i18n/zh.tsv b/crates/ui-egui/src/i18n/zh.tsv index de4e6a633..a8d979efa 100644 --- a/crates/ui-egui/src/i18n/zh.tsv +++ b/crates/ui-egui/src/i18n/zh.tsv @@ -1377,6 +1377,18 @@ Save Layout as Template 将布局另存为样板 Set Dynamic Property 设置动态特性 Units 单位 + Find All 查找全部 + Find what: 查找内容: + Match case 区分大小写 + Replace 替换 + Replace All 全部替换 + Replace with: 替换为: + Replacement text 替换文本 + Results 结果 + Searches the current space. 仅搜索当前空间。 + Text to find 要查找的文字 + Whole words 全字匹配 + Nothing found 未找到 # The strings the report listed as missing for zh. Add… 添加… diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 3319e639c..3e635ea2f 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -33,6 +33,8 @@ pub mod credits; pub mod dialogs; pub mod docking; pub mod fault; +// The Find and Replace window behind the FIND command. +pub mod finddlg; pub mod hatchdlg; mod mtext_editor; mod panel_state; diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 55d058811..438c06d97 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -73,6 +73,7 @@ pub const UI_COMMANDS: &[(&str, &str, &[&str], Option<&str>)] = &[ ("ui.cmdline.lines", "Command Line History Lines", &[], None), ("ui.cmdline.clear", "Clear Command History", &[], None), ("ui.dialog.language", "Interface Language", &["Edit", "Interface Language"], None), + ("ui.dialog.find", "Find and Replace", &[], Some("Cmd+F")), ("ui.language", "Language", &[], None), ("ui.noop", "", &[], None), ("ui.quit", "Quit CADCraft", &[], Some("Cmd+Q")), @@ -332,6 +333,12 @@ fn run_ui_command_inner(app: &mut CadApp, id: &str, params: &Value) -> Option { let picked = app.services.pick_open.as_ref().and_then(|f| f()); @@ -456,7 +463,8 @@ fn run_ui_command_inner(app: &mut CadApp, id: &str, params: &Value) -> Option { + | "ui.dialog.parameters" + | "ui.dialog.find" => { app.ui.dialog = Some(id.trim_start_matches("ui.dialog.").to_string()); Ok(Value::Null) } @@ -816,6 +824,7 @@ pub(crate) fn builtin_shortcuts() -> Vec<(egui::KeyboardShortcut, &'static str)> (sc(cmd, Key::S), "qsave"), (sc(cmd, Key::P), "plot"), (sc(cmd, Key::A), "selectall"), + (sc(cmd, Key::F), "ui.dialog.find"), (sc(cmd_shift, Key::C), "copybase"), (sc(cmd, Key::C), "copyclip"), (sc(cmd, Key::X), "cutclip"), From 5ed192555ca8c3e0fd5c7092163ed3cf0c59ce9a Mon Sep 17 00:00:00 2001 From: Tyler Chiriboga Date: Mon, 2 Nov 2026 21:54:03 -0600 Subject: [PATCH 100/100] EXPLODE: a block reference with different X and Y scales gives ellipses, not circles (#2649) * EXPLODE: a block reference with different X and Y scales gives ellipses, not circles EXPLODE of a block reference whose X and Y scales differ kept its circles as circles with the mean scale, its arcs as circular arcs, its ellipses with their old ratio and its polyline arcs as circular bulges: not the shape the reference draws. BURST and INSERT with explode go the same way. When the reference's transform isn't a similarity, a circle now becomes a full ELLIPSE, an arc an elliptical arc, an ellipse gets its true axes, and a polyline with arc segments (and no widths) its lines and elliptical arcs. Everything else, and every evenly scaled or mirrored reference, transforms as before. The exact ellipse transform and the similarity test move unchanged from snap.rs to cadcraft-geom (Ellipse::transformed, Mat3::similarity_scale) and are shared by object snaps and EXPLODE. Co-Authored-By: Claude Opus 5.5 * contributors: add Tyler-Chiriboga Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Tyler Chiriboga <18270301+Tyler-Chiriboga@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 Co-authored-by: Brandon Thomas --- contributors/people.toml | 4 + crates/engine/src/cmd/modify/explode.rs | 61 ++++++++- crates/engine/src/snap.rs | 37 +----- crates/engine/tests/it/explode_stretched.rs | 140 ++++++++++++++++++++ crates/engine/tests/it/main.rs | 1 + crates/geom/src/curve.rs | 19 ++- crates/geom/src/mat.rs | 8 ++ 7 files changed, 234 insertions(+), 36 deletions(-) create mode 100644 crates/engine/tests/it/explode_stretched.rs diff --git a/contributors/people.toml b/contributors/people.toml index de6d22923..8bb73b9ea 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -100,6 +100,10 @@ sync_github_name = true [people.GULERTOLGA] sync_github_name = true +[people.Tyler-Chiriboga] +real_name = "Tyler Chiriboga" +sync_github_name = true + [people.starise] real_name = "Andrea Brandi" diff --git a/crates/engine/src/cmd/modify/explode.rs b/crates/engine/src/cmd/modify/explode.rs index d7db29d35..b4edc2e80 100644 --- a/crates/engine/src/cmd/modify/explode.rs +++ b/crates/engine/src/cmd/modify/explode.rs @@ -1,19 +1,74 @@ +use cadcraft_geom::{Ellipse as GEllipse, Mat3}; + use super::{Entity, EntityKind, Handle, PolyVertex, Polyline, Result, Segment, Session, Value, Vec2, arc, json, line, lwpoly, targets, v3}; // ---------------- explode / array / order / break / join ---------------- +/// `ell` placed by `m` as an ELLIPSE at elevation `z`; `None` when `m` flattens it. +fn placed_ellipse(ell: GEllipse, m: &Mat3, z: f64) -> Option { + let g = ell.transformed(m); + let len = g.major.len(); + (len.is_finite() && len > 0.0 && g.ratio.is_finite() && g.ratio > 0.0).then(|| { + EntityKind::Ellipse(cadcraft_doc::Ellipse { center: g.center.to3(z), major: g.major.to3(0.0), ratio: g.ratio, start: g.start, end: g.end }) + }) +} + +/// The pieces of block object `be` placed by `m`, a transform that stretches unevenly (a block +/// reference whose X and Y scales differ), where its own kind can't hold the result: a circle +/// or an arc becomes an ellipse or an elliptical arc, a polyline with arc segments (and no +/// widths) its lines and elliptical arcs. `None`: the object transforms as itself. +fn stretched_pieces(be: &Entity, m: &Mat3) -> Option> { + let circular = |center: Vec2, radius: f64, start: f64, end: f64| GEllipse { center, major: Vec2::X * radius, ratio: 1.0, start, end }; + let kinds = match &be.kind { + EntityKind::Circle(c) => vec![placed_ellipse(GEllipse::full(c.center.xy(), Vec2::X * c.radius, 1.0), m, c.center.z)?], + EntityKind::Arc(a) => vec![placed_ellipse(circular(a.center.xy(), a.radius, a.start, a.end), m, a.center.z)?], + EntityKind::Ellipse(el) => { + let g = GEllipse { center: el.center.xy(), major: el.major.xy(), ratio: el.ratio, start: el.start, end: el.end }; + vec![placed_ellipse(g, m, el.center.z)?] + } + EntityKind::LwPolyline(p) + if p.vertices.iter().any(|v| v.bulge != 0.0) + && p.const_width == 0.0 + && p.vertices.iter().all(|v| v.start_width == 0.0 && v.end_width == 0.0) => + { + let z = p.elevation; + let pl = Polyline { vertices: p.vertices.clone(), closed: p.closed }; + pl.segments() + .into_iter() + .map(|s| match s { + Segment::Line(l) => Some(EntityKind::Line(cadcraft_doc::Line { a: m.apply(l.a).to3(z), b: m.apply(l.b).to3(z) })), + Segment::Arc { arc: a, .. } => placed_ellipse(circular(a.center, a.radius, a.start, a.end), m, z), + }) + .collect::>>()? + } + _ => return None, + }; + // The pieces of a polyline are new objects, as when it explodes itself: its xdata goes. + let mut common = be.common.clone(); + if matches!(be.kind, EntityKind::LwPolyline(_)) { + common.xdata.clear(); + } + Some(kinds.into_iter().map(|kind| Entity { handle: Handle(0), common: common.clone(), kind }).collect()) +} + fn explode_block_ref(d: &cadcraft_doc::Drawing, e: &Entity, ins: &cadcraft_doc::Insert) -> Option> { if let Some(items) = crate::cmd::array::explode_array(d, e) { return Some(items); } let blk = d.block(&ins.block)?; let m = ins.transform(blk.base.xy()); + let stretched = m.similarity_scale().is_none(); Some( blk.entities .iter() - .map(|be| { - let mut ne = (**be).clone(); - ne.kind.transform(&m); + .flat_map(|be| { + stretched.then(|| stretched_pieces(be, &m)).flatten().unwrap_or_else(|| { + let mut ne = (**be).clone(); + ne.kind.transform(&m); + vec![ne] + }) + }) + .map(|mut ne| { if ne.common.layer == "0" { ne.common.layer = e.common.layer.clone(); } diff --git a/crates/engine/src/snap.rs b/crates/engine/src/snap.rs index 8def404d2..e2e24d9bc 100644 --- a/crates/engine/src/snap.rs +++ b/crates/engine/src/snap.rs @@ -813,22 +813,13 @@ fn prim_near(p: &Prim, probe: &Bounds2, aperture: f64) -> bool { b.expand(aperture).intersects(probe) } -/// The rotation-and-uniform-scale factor of `m`'s linear part (mirroring allowed), or `None` -/// when it distorts shapes (non-uniform scale or shear). -fn similarity_scale(m: &Mat3) -> Option { - let (u, v) = (Vec2::new(m.a, m.b), Vec2::new(m.c, m.d)); - let (lu, lv) = (u.len(), v.len()); - let tol = 1e-9 * lu.max(lv); - ((lu - lv).abs() <= tol && u.dot(v).abs() <= tol * lu.max(lv) && lu > 0.0).then_some(lu) -} - /// `p` placed by the affine transform `m`, exactly: circles and arcs under a non-uniform scale /// become ellipses and elliptical arcs. fn xform_prim(p: &Prim, m: &Mat3) -> Prim { let mirror = m.is_mirroring(); match p { Prim::Seg(Segment::Line(l)) => Prim::Seg(Segment::Line(cadcraft_geom::Line::new(m.apply(l.a), m.apply(l.b)))), - Prim::Seg(Segment::Arc { arc, ccw }) => match similarity_scale(m) { + Prim::Seg(Segment::Arc { arc, ccw }) => match m.similarity_scale() { Some(s) => { let c = m.apply(arc.center); let (a0, a1) = (c.angle_to(m.apply(arc.start_point())), c.angle_to(m.apply(arc.end_point()))); @@ -837,14 +828,14 @@ fn xform_prim(p: &Prim, m: &Mat3) -> Prim { } None => { let e = Ellipse { center: arc.center, major: Vec2::X * arc.radius, ratio: 1.0, start: arc.start, end: arc.end }; - Prim::Ellipse(xform_ellipse(&e, m)) + Prim::Ellipse(e.transformed(m)) } }, - Prim::Circle(c) => match similarity_scale(m) { + Prim::Circle(c) => match m.similarity_scale() { Some(s) => Prim::Circle(Circle::new(m.apply(c.center), c.radius * s)), - None => Prim::Ellipse(xform_ellipse(&Ellipse::full(c.center, Vec2::X * c.radius, 1.0), m)), + None => Prim::Ellipse(Ellipse::full(c.center, Vec2::X * c.radius, 1.0).transformed(m)), }, - Prim::Ellipse(e) => Prim::Ellipse(xform_ellipse(e, m)), + Prim::Ellipse(e) => Prim::Ellipse(e.transformed(m)), Prim::Spline(s) => { let mut s = s.clone(); s.control.iter_mut().chain(s.fit.iter_mut()).for_each(|q| *q = m.apply(*q)); @@ -856,24 +847,6 @@ fn xform_prim(p: &Prim, m: &Mat3) -> Prim { } } -/// The image of an ellipse (or elliptical arc) under an affine transform: the conjugate -/// semi-diameters `A·major`, `A·minor` give the new principal axes; parameters shift with them -/// (and reverse under a mirroring transform). -fn xform_ellipse(e: &Ellipse, m: &Mat3) -> Ellipse { - let center = m.apply(e.center); - let (am, an) = (m.apply_vec(e.major), m.apply_vec(e.minor())); - // |am·cos t + an·sin t| is largest at t0: the new major axis. - let t0 = 0.5 * (2.0 * am.dot(an)).atan2(am.dot(am) - an.dot(an)); - let (s0, c0) = t0.sin_cos(); - let (u, v) = (am * c0 + an * s0, an * c0 - am * s0); - let ratio = if u.len() > 0.0 { (v.len() / u.len()).min(1.0) } else { 1.0 }; - if e.is_full() { - return Ellipse::full(center, u, ratio); - } - let (start, end) = if u.cross(v) >= 0.0 { (e.start - t0, e.end - t0) } else { (t0 - e.end, t0 - e.start) }; - Ellipse { center, major: u, ratio, start: cadcraft_geom::norm_angle(start), end: cadcraft_geom::norm_angle(end) } -} - /// The nearest deferred tangent/perpendicular within `aperture` of `cursor`. fn deferred_hit(prims: &[Prim], cursor: Vec2, aperture: f64, osmode: u32, clip: Option) -> Option { let mut best: Option<(f64, SnapHit)> = None; diff --git a/crates/engine/tests/it/explode_stretched.rs b/crates/engine/tests/it/explode_stretched.rs new file mode 100644 index 000000000..d7a7ac843 --- /dev/null +++ b/crates/engine/tests/it/explode_stretched.rs @@ -0,0 +1,140 @@ +//! EXPLODE of a block reference whose X and Y scales differ: its circles, arcs, ellipses and +//! polyline arcs come out as the ellipses and elliptical arcs the reference draws, not as circles +//! scaled by the mean of the two scales. + +use cadcraft_engine::Session; +use cadcraft_engine::doc::{Entity, EntityKind}; +use serde_json::json; + +const AT: [f64; 2] = [100.0, 50.0]; +const ROTATION: f64 = 30.0; + +/// Block S (base 0,0): a circle (r 5 at 0,0), an arc (r 4 at 20,0, 0°..90°), an elliptical arc +/// (centre 40,0, major 6 along X, ratio 0.5, 0°..180°), a slot polyline (two lines, two +/// semicircles of r 2 at 60,0 and 70,0) and a line. One reference to it at `AT`, rotated by +/// `ROTATION`, scaled by `scale`, exploded. +fn exploded(scale: [f64; 2]) -> Vec { + let mut s = Session::new(); + let mut hs = Vec::new(); + for (id, p) in [ + ("circle", json!({"center": [0, 0], "radius": 5})), + ("arc", json!({"center": [20, 0], "radius": 4, "start": 0, "end": 90})), + ("ellipse", json!({"center": [40, 0], "major": [6, 0], "ratio": 0.5, "start": 0, "end": 180})), + ("pline", json!({"vertices": [[60, -2], {"p": [70, -2], "bulge": 1}, [70, 2], {"p": [60, 2], "bulge": 1}], "closed": true})), + ("line", json!({"points": [[0, 10], [10, 10]]})), + ] { + s.execute(id, &p).unwrap(); + hs.push(s.doc().unwrap().model.iter().last().unwrap().handle.hex()); + } + s.execute("block", &json!({"name": "S", "base": [0, 0], "keep": "delete", "handles": hs})).unwrap(); + let r = s.execute("insert", &json!({"name": "S", "at": AT, "scale": scale, "rotation": ROTATION})).unwrap(); + s.execute("explode", &json!({"handles": [r["handle"]]})).unwrap(); + s.doc().unwrap().model.iter().map(|e| (**e).clone()).collect() +} + +/// A drawing point back in block coordinates. +fn to_block(p: [f64; 2], scale: [f64; 2]) -> [f64; 2] { + let (s, c) = (-ROTATION.to_radians()).sin_cos(); + let (dx, dy) = (p[0] - AT[0], p[1] - AT[1]); + [(dx * c - dy * s) / scale[0], (dx * s + dy * c) / scale[1]] +} + +/// Points along an ELLIPSE from its start to its end parameter. +fn samples(e: &cadcraft_engine::doc::Ellipse) -> Vec<[f64; 2]> { + let mut sweep = e.end - e.start; + if sweep <= 0.0 { + sweep += std::f64::consts::TAU; + } + let (mx, my) = (e.major.x, e.major.y); + let (nx, ny) = (-my * e.ratio, mx * e.ratio); + (0..=16) + .map(|i| { + let t = e.start + sweep * f64::from(i) / 16.0; + [e.center.x + mx * t.cos() + nx * t.sin(), e.center.y + my * t.cos() + ny * t.sin()] + }) + .collect() +} + +fn ellipses(v: &[Entity]) -> Vec { + v.iter() + .filter_map(|e| match &e.kind { + EntityKind::Ellipse(el) => Some(el.clone()), + _ => None, + }) + .collect() +} + +fn near(a: [f64; 2], b: [f64; 2]) -> bool { + (a[0] - b[0]).hypot(a[1] - b[1]) < 1e-9 +} + +/// Every point of `el`, taken back into the block, lies on the circle of radius `r` about `c` +/// in block coordinates, and its ends are `ends` (the whole circle when `None`). +fn on_circle(el: &cadcraft_engine::doc::Ellipse, scale: [f64; 2], c: [f64; 2], r: f64, ends: Option<([f64; 2], [f64; 2])>) { + let pts: Vec<[f64; 2]> = samples(el).into_iter().map(|p| to_block(p, scale)).collect(); + for q in &pts { + let d = (q[0] - c[0]).hypot(q[1] - c[1]); + assert!((d - r).abs() < 1e-9, "{q:?} is {d} from {c:?}, not {r}: {el:?}"); + } + if let Some((a, b)) = ends { + let (first, last) = (pts[0], pts[pts.len() - 1]); + assert!(near(first, a) && near(last, b) || near(first, b) && near(last, a), "ends {first:?} {last:?}, expected {a:?} {b:?}"); + } +} + +#[test] +fn explode_of_a_stretched_reference_draws_circles_and_arcs_as_ellipses() { + for scale in [[2.0, 0.5], [-2.0, 0.5], [1.0, 3.0]] { + let v = exploded(scale); + assert!(!v.iter().any(|e| matches!(e.kind, EntityKind::Circle(_) | EntityKind::Arc(_) | EntityKind::LwPolyline(_))), "{scale:?}: {v:?}"); + let els = ellipses(&v); + // Circle, arc, elliptical arc and the slot's two semicircles. + assert_eq!(els.len(), 5, "{scale:?}: {els:?}"); + let at = |c: [f64; 2]| els.iter().filter(move |e| near(to_block([e.center.x, e.center.y], scale), c)); + let circle = at([0.0, 0.0]).next().unwrap(); + on_circle(circle, scale, [0.0, 0.0], 5.0, None); + // Its axes are the scaled radii: 10 and 2.5 for 2 x 0.5. + let major = circle.major.x.hypot(circle.major.y); + let (big, small) = (5.0 * scale[0].abs().max(scale[1].abs()), 5.0 * scale[0].abs().min(scale[1].abs())); + assert!((major - big).abs() < 1e-9 && (major * circle.ratio - small).abs() < 1e-9, "{circle:?}"); + on_circle(at([20.0, 0.0]).next().unwrap(), scale, [20.0, 0.0], 4.0, Some(([24.0, 0.0], [20.0, 4.0]))); + on_circle(at([60.0, 0.0]).next().unwrap(), scale, [60.0, 0.0], 2.0, Some(([60.0, 2.0], [60.0, -2.0]))); + on_circle(at([70.0, 0.0]).next().unwrap(), scale, [70.0, 0.0], 2.0, Some(([70.0, -2.0], [70.0, 2.0]))); + // The elliptical arc: its points, back in the block, are on the block's ellipse + // ((x-40)/6)^2 + (y/3)^2 = 1, from (46,0) over the top to (34,0). + let ell = at([40.0, 0.0]).next().unwrap(); + let pts: Vec<[f64; 2]> = samples(ell).into_iter().map(|p| to_block(p, scale)).collect(); + for q in &pts { + let f = ((q[0] - 40.0) / 6.0).powi(2) + (q[1] / 3.0).powi(2); + assert!((f - 1.0).abs() < 1e-9 && q[1] > -1e-9, "{q:?}: {ell:?}"); + } + let (first, last) = (pts[0], pts[pts.len() - 1]); + assert!(near(first, [46.0, 0.0]) && near(last, [34.0, 0.0]) || near(first, [34.0, 0.0]) && near(last, [46.0, 0.0]), "{pts:?}"); + // The slot's straight sides and the line are lines, where the reference draws them. + let lines: Vec<[[f64; 2]; 2]> = v + .iter() + .filter_map(|e| match &e.kind { + EntityKind::Line(l) => Some([to_block([l.a.x, l.a.y], scale), to_block([l.b.x, l.b.y], scale)]), + _ => None, + }) + .collect(); + assert_eq!(lines.len(), 3); + for [a, b] in [[[60.0, -2.0], [70.0, -2.0]], [[70.0, 2.0], [60.0, 2.0]], [[0.0, 10.0], [10.0, 10.0]]] { + assert!(lines.iter().any(|l| near(l[0], a) && near(l[1], b)), "{a:?}-{b:?} not in {lines:?}"); + } + } +} + +#[test] +fn explode_of_an_evenly_scaled_or_mirrored_reference_keeps_circles_arcs_and_polylines() { + for scale in [[2.0, 2.0], [-1.0, 1.0]] { + let v = exploded(scale); + let count = |f: fn(&EntityKind) -> bool| v.iter().filter(|e| f(&e.kind)).count(); + assert_eq!(count(|k| matches!(k, EntityKind::Circle(_))), 1, "{scale:?}"); + assert_eq!(count(|k| matches!(k, EntityKind::Arc(_))), 1, "{scale:?}"); + assert_eq!(count(|k| matches!(k, EntityKind::LwPolyline(_))), 1, "{scale:?}"); + assert_eq!(count(|k| matches!(k, EntityKind::Ellipse(_))), 1, "{scale:?}"); + let r = v.iter().find_map(|e| if let EntityKind::Circle(c) = &e.kind { Some(c.radius) } else { None }).unwrap(); + assert!((r - 5.0 * scale[0].abs()).abs() < 1e-9); + } +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index b6ec02d60..9e014d4b1 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -46,6 +46,7 @@ mod draworder; mod dyn_input; mod dynamic_block_data; mod dynamic_grips; +mod explode_stretched; mod extend_polyline_arc_end; mod fillet_chamfer; mod fix_holds_grips; diff --git a/crates/geom/src/curve.rs b/crates/geom/src/curve.rs index 2b799e433..04f665f88 100644 --- a/crates/geom/src/curve.rs +++ b/crates/geom/src/curve.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::{Bounds2, EPS, PI, TAU, Vec2, angle_in_sweep, arc_segments, ccw_sweep, norm_angle}; +use crate::{Bounds2, EPS, Mat3, PI, TAU, Vec2, angle_in_sweep, arc_segments, ccw_sweep, norm_angle}; /// A straight segment. #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] @@ -247,6 +247,23 @@ impl Ellipse { pub fn sweep(&self) -> f64 { ccw_sweep(self.start, self.end) } + /// The image under the affine transform `m`, exactly: the conjugate semi-diameters + /// `A·major`, `A·minor` give the new principal axes; parameters shift with them (and + /// reverse under a mirroring transform). + pub fn transformed(&self, m: &Mat3) -> Ellipse { + let center = m.apply(self.center); + let (am, an) = (m.apply_vec(self.major), m.apply_vec(self.minor())); + // |am·cos t + an·sin t| is largest at t0: the new major axis. + let t0 = 0.5 * (2.0 * am.dot(an)).atan2(am.dot(am) - an.dot(an)); + let (s0, c0) = t0.sin_cos(); + let (u, v) = (am * c0 + an * s0, an * c0 - am * s0); + let ratio = if u.len() > 0.0 { (v.len() / u.len()).min(1.0) } else { 1.0 }; + if self.is_full() { + return Ellipse::full(center, u, ratio); + } + let (start, end) = if u.cross(v) >= 0.0 { (self.start - t0, self.end - t0) } else { (t0 - self.end, t0 - self.start) }; + Ellipse { center, major: u, ratio, start: norm_angle(start), end: norm_angle(end) } + } pub fn tessellate(&self, tol: f64, out: &mut Vec) { let r = self.major.len(); let sweep = self.sweep(); diff --git a/crates/geom/src/mat.rs b/crates/geom/src/mat.rs index 5f2613540..5f28b068d 100644 --- a/crates/geom/src/mat.rs +++ b/crates/geom/src/mat.rs @@ -98,6 +98,14 @@ impl Mat3 { pub fn is_mirroring(&self) -> bool { self.det() < 0.0 } + /// The rotation-and-uniform-scale factor of the linear part (mirroring allowed), or `None` + /// when it distorts shapes (non-uniform scale or shear). + pub fn similarity_scale(&self) -> Option { + let (u, v) = (Vec2::new(self.a, self.b), Vec2::new(self.c, self.d)); + let (lu, lv) = (u.len(), v.len()); + let tol = 1e-9 * lu.max(lv); + ((lu - lv).abs() <= tol && u.dot(v).abs() <= tol * lu.max(lv) && lu > 0.0).then_some(lu) + } } /// A 4x4 transform, row-major, for 3D views and block transforms with elevation.