diff --git a/src/scene/mod.rs b/src/scene/mod.rs index d5ce4fb80..7468248f3 100644 --- a/src/scene/mod.rs +++ b/src/scene/mod.rs @@ -3889,6 +3889,38 @@ impl Scene { // Remove the Layout object itself. self.document.objects.remove(&layout_handle); + // Drop the layout's entry from the ACAD_LAYOUT dictionary so it does not + // dangle (and so AutoCAD doesn't try to recover a now-missing layout). + let dict_handle = self.document.header.acad_layout_dict_handle; + if let Some(ObjectType::Dictionary(d)) = self.document.objects.get_mut(&dict_handle) { + d.entries.retain(|(k, _)| k != name); + } + + // Remove the now-empty paper-space block record. + let block_name = self + .document + .block_records + .iter() + .find(|b| b.handle == block_handle) + .map(|b| b.name.clone()); + if let Some(bn) = block_name { + self.document.block_records.remove(&bn); + } + + // Drop any standalone PlotSettings page setup tied to this layout. + let ps_handles: Vec = self + .document + .objects + .iter() + .filter_map(|(h, o)| match o { + ObjectType::PlotSettings(ps) if ps.page_name == name => Some(*h), + _ => None, + }) + .collect(); + for h in ps_handles { + self.document.objects.remove(&h); + } + // If the deleted layout was active, fall back to Model space. if self.current_layout == name { self.current_layout = "Model".to_string();