File formats: a converted DWG's OK checks the document it is placed in

The DXF Import Options of a DWG drawing converted for Place checked its
document only before opening. When that document was closed, reverted
or no longer active by the time of OK (a script or the control channel
can switch documents while the dialog is open), OK placed the drawing in
whatever document was active. OK now refuses with the reason, changes
nothing and keeps the dialog open; the choices are remembered only when
OK goes ahead.
This commit is contained in:
dotCooCoo committed 2026-12-04 05:01:40 -08:00
1 parent 826c92f7b6
commit 2c7f37723c
2 files changed
+55 -15

No files matched your search

+35 -3
View File
@@ -491,12 +491,16 @@ fn options(c: &DxfChoices) -> Value {
/// OK: open or place the drawing with the choices; an error keeps the dialog open.
fn confirm(app: &mut VectorcraftApp, source: &Source, choices: &DxfChoices) -> Result<(), String> {
let dxf = options(choices);
app.ui.dxf_import = Value::Object(REMEMBERED.iter().map(|k| (k.to_string(), dxf[*k].clone())).collect());
let remembered = Value::Object(REMEMBERED.iter().map(|k| (k.to_string(), dxf[*k].clone())).collect());
match &source.place {
Some(place) => {
let placement = source.converted.as_ref().and_then(|c| c.placement.as_ref());
if let Some(placement) = placement {
placement.check(app)?;
}
app.ui.dxf_import = remembered;
let mut p = with_file(place, source.path.as_deref(), &source.name, &source.file);
p["dxf"] = dxf;
let placement = source.converted.as_ref().and_then(|c| c.placement.as_ref());
// Where new art goes now is taken before Replace selects the object it swaps again.
let current = placement.and_then(|_| crate::placing::Insertion::of(app));
// A drawing converted off the UI thread replaces the object selected when Place
@@ -527,7 +531,10 @@ fn confirm(app: &mut VectorcraftApp, source: &Source, choices: &DxfChoices) -> R
}
r
}
None => io::open_document(app, &source.name, &source.file.bytes, source.path.clone(), &json!({ "dxf": dxf })).map(|_| ()),
None => {
app.ui.dxf_import = remembered;
io::open_document(app, &source.name, &source.file.bytes, source.path.clone(), &json!({ "dxf": dxf })).map(|_| ())
}
}
}
@@ -1017,6 +1024,31 @@ mod tests {
});
}
/// Switching documents while the import dialog is open leaves both documents unchanged.
#[cfg(not(any(target_arch = "wasm32", all(target_os = "freebsd", target_arch = "aarch64"))))]
#[test]
fn a_dwg_drawing_cannot_be_placed_after_switching_documents_in_its_dialog() {
vectorcraft_engine::dwg::allow_in_process();
let (_dir, plan) = dwg_file("dxf-import-dwg-place-dialog-switch");
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();
let first = app.session.doc().unwrap().uid;
let before = app.session.doc().unwrap().history.undo.len();
crate::place::run_for_user(&mut app, &json!({ "path": plan })).unwrap();
finish_conversions(&mut app);
assert!(host::is_open(&app, KIND));
app.run("file.new", json!({"width": 600, "height": 400})).unwrap();
let other_before = app.session.doc().unwrap().history.undo.len();
let error = host::confirm(&mut app, KIND).unwrap_err();
assert!(error.contains("not the active document"), "{error}");
assert_eq!(app.session.doc().unwrap().history.undo.len(), other_before);
let first = app.session.documents().iter().find(|st| st.uid == first).unwrap();
assert_eq!(first.history.undo.len(), before);
});
}
#[test]
fn placing_a_drawing_asks_then_places_one_group() {
let mut app = host::app();
+20 -12
View File
@@ -80,6 +80,20 @@ pub(crate) struct Placement {
pub(crate) replaced: Option<(vectorcraft_doc::NodeId, vectorcraft_doc::Node)>,
}
impl Placement {
/// Is the document it was placed in still open, unreverted and active?
pub(crate) fn check(&self, app: &VectorcraftApp) -> Result<(), String> {
let st = app.session.documents().iter().find(|st| st.uid == self.doc).ok_or("the document it was placed in was closed")?;
if st.reverts != self.reverts {
return Err("the document it was placed in was reverted".into());
}
if !app.session.doc().is_ok_and(|st| st.uid == self.doc) {
return Err("the document it was placed in is not the active document".into());
}
Ok(())
}
}
/// The files being read.
#[derive(Default)]
pub struct Opening {
@@ -292,18 +306,12 @@ pub fn poll(app: &mut VectorcraftApp) {
// placed when that document was closed or reverted meanwhile.
if let Some(placement) = &c.placed_in {
let name = fileio::file_name(&c.name);
match app.session.documents().iter().position(|st| st.uid == placement.doc) {
None => {
app.status(format!("Couldn't place {name}: the document it was placed in was closed"));
continue;
}
Some(d) if app.session.documents()[d].reverts != placement.reverts => {
app.status(format!("Couldn't place {name}: the document it was placed in was reverted"));
continue;
}
Some(d) => {
app.session.set_active(d);
}
if let Some(d) = app.session.documents().iter().position(|st| st.uid == placement.doc && st.reverts == placement.reverts) {
app.session.set_active(d);
}
if let Err(e) = placement.check(app) {
app.status(format!("Couldn't place {name}: {e}"));
continue;
}
}
c.scope.run(|| crate::dialogs::dxf_import::converted(app, &c.name, r, c.path, c.place.as_ref(), c.placed_in));