From ca20b965f2c8afb9f9da9987fa65a88ab04f432d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 16:31:32 -0800 Subject: [PATCH] DXF Import: OK refuses to place a drawing changed on disk since the preview Placing a DXF or DWG drawing by path through DXF Import Options read the file again at OK, so a file written over it while the dialog was open was placed with the options chosen for the drawing the dialog showed. OK now compares the file with the bytes the dialog previewed and keeps the dialog open with the reason when they differ. --- crates/ui-egui/src/dialogs/dxf_import.rs | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/crates/ui-egui/src/dialogs/dxf_import.rs b/crates/ui-egui/src/dialogs/dxf_import.rs index a01076a838..55e50c8972 100644 --- a/crates/ui-egui/src/dialogs/dxf_import.rs +++ b/crates/ui-egui/src/dialogs/dxf_import.rs @@ -498,6 +498,12 @@ fn confirm(app: &mut VectorcraftApp, source: &Source, choices: &DxfChoices) -> R if let Some(placement) = placement { placement.check(app)?; } + // Place reads the file again: it must still be the drawing the dialog previewed. + if let (Some(path), Some(read)) = (source.path.as_deref(), app.services.read.as_ref()) + && read(path)? != source.file.bytes + { + return Err(tl!("The drawing changed on disk. Place it again to use the new version.").to_owned()); + } app.ui.dxf_import = remembered; let mut p = with_file(place, source.path.as_deref(), &source.name, &source.file); p["dxf"] = dxf; @@ -917,6 +923,54 @@ mod tests { }); } + /// A DWG drawing placed through its dialog is the drawing the dialog previewed: a file written + /// over it on disk while the dialog is open is not placed, and OK keeps the dialog open. + #[cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))] + #[test] + fn ok_places_the_drawing_the_dialog_previewed() { + fn placed_size(app: &mut VectorcraftApp) -> (f64, f64) { + while !app.placing.pending.is_empty() { + crate::placing::wait(app); + crate::placing::poll(app); + } + let st = app.session.doc().unwrap(); + let mut out: Option = None; + st.doc.node(st.selection.objects[0]).unwrap().walk(&mut |c| { + if let Some(b) = c.path_data().and_then(vectorcraft_geom::PathData::bounds) { + out = Some(out.map_or(b, |o| o.union(b))); + } + }); + let r = out.unwrap(); + (r.width(), r.height()) + } + vectorcraft_engine::dwg::allow_in_process(); + let (_dir, plan) = dwg_file("dxf-import-dwg-previewed"); + let flatter = String::from_utf8(drawing()).unwrap().replace(" 21\n50\n", " 21\n10\n"); + crate::opening::tests::with_threads(|| { + let mut app = host::app(); + app.services.read = Some(Box::new(|p: &str| std::fs::read(p).map_err(|e| e.to_string()))); + app.run("file.new", json!({"width": 600, "height": 400})).unwrap(); + crate::place::run_for_user(&mut app, &json!({ "path": plan })).unwrap(); + finish_conversions(&mut app); + host::confirm(&mut app, KIND).unwrap(); + let previewed = placed_size(&mut app); + crate::place::run_for_user(&mut app, &json!({ "path": plan })).unwrap(); + finish_conversions(&mut app); + assert_eq!(host::fields(&app, KIND)["mode"], "place"); + std::fs::write(&plan, craft_dwg::dxf_to_dwg_as(flatter.as_bytes(), "AC1018").unwrap()).unwrap(); + let steps = app.session.doc().unwrap().history.undo.len(); + let refused = host::confirm(&mut app, KIND); + assert_eq!(refused, Err("The drawing changed on disk. Place it again to use the new version.".to_owned())); + assert!(host::is_open(&app, KIND), "the dialog stays open"); + assert!(app.placing.pending.is_empty()); + assert_eq!(app.session.doc().unwrap().history.undo.len(), steps, "nothing is placed"); + // Written back as it was, the drawing the dialog previewed is placed. + std::fs::write(&plan, craft_dwg::dxf_to_dwg_as(&drawing(), "AC1018").unwrap()).unwrap(); + host::confirm(&mut app, KIND).unwrap(); + assert_eq!(placed_size(&mut app), previewed, "OK places the drawing the dialog opened on"); + }); + } + /// A DWG drawing converted off the UI thread and placed keeps its conversion until the place /// read off the UI thread is done. #[cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))]