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.
This commit is contained in:
dotCooCoo committed 2026-12-04 16:31:32 -08:00
1 parent 60759e4575
commit ca20b965f2
1 file changed
+54
+54
View File
@@ -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<vectorcraft_geom::Rect> = 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"))))]