feat(io): persist grid/snap display state to the active VPort

Mirror the in-app grid and grid-snap toggles (plus the adaptive grid
spacing) onto the *Active VPort entries so a saved DWG/DXF reflects what
the user actually has on, instead of always writing VPort defaults. The
flags are stamped when the toggles change and again before a save, and
carried across the camera-sync VPort rebuild so they survive an orbit/pan.

Only grid_on, snap_on and grid_spacing have an OCS counterpart; the other
VPort view fields (view twist, clip planes, lens length, etc.) have no
in-app source and stay at their faithful defaults.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Hakan Seven 2026-06-18 22:05:25 +03:00
commit cd17096f65
2 changed files with 50 additions and 0 deletions

View file

@ -342,6 +342,19 @@ impl OpenCADStudio {
self.persist_settings_if_changed(); self.persist_settings_if_changed();
} }
/// Mirror the current grid/snap toggles onto tab `i`'s `*Active` VPort
/// entries so a save writes the real display state rather than VPort
/// defaults (#121). Cheap; safe to call every frame.
fn sync_vport_display(&mut self, i: usize) {
let grid_on = self.show_grid;
let snap_on =
self.snapper.snap_enabled && self.snapper.is_on(crate::snap::SnapType::Grid);
let grid_spacing = self.snapper.grid_spacing as f64;
self.tabs[i]
.scene
.set_active_vport_display(grid_on, snap_on, grid_spacing);
}
fn update_inner(&mut self, msg: Message) -> Task<Message> { fn update_inner(&mut self, msg: Message) -> Task<Message> {
match msg { match msg {
Message::Tick(t) => { Message::Tick(t) => {
@ -924,6 +937,10 @@ impl OpenCADStudio {
Message::SaveFile => { Message::SaveFile => {
let i = self.active_tab; let i = self.active_tab;
// Stamp the live grid/snap toggles onto the VPort so the file
// reflects them even if they came from settings with no
// in-session toggle (#121).
self.sync_vport_display(i);
// Native: save straight to the known path. Web has no path // Native: save straight to the known path. Web has no path
// (downloads instead), so always go through the Save dialog. // (downloads instead), so always go through the Save dialog.
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
@ -4295,14 +4312,17 @@ impl OpenCADStudio {
// ── Snap / mode toggles ─────────────────────────────────────── // ── Snap / mode toggles ───────────────────────────────────────
Message::ToggleSnapEnabled => { Message::ToggleSnapEnabled => {
self.snapper.toggle_global(); self.snapper.toggle_global();
self.sync_vport_display(self.active_tab);
Task::none() Task::none()
} }
Message::ToggleGridSnap => { Message::ToggleGridSnap => {
self.snapper.toggle(crate::snap::SnapType::Grid); self.snapper.toggle(crate::snap::SnapType::Grid);
self.sync_vport_display(self.active_tab);
Task::none() Task::none()
} }
Message::ToggleGrid => { Message::ToggleGrid => {
self.show_grid ^= true; self.show_grid ^= true;
self.sync_vport_display(self.active_tab);
Task::none() Task::none()
} }
Message::ToggleOrtho => { Message::ToggleOrtho => {

View file

@ -5573,6 +5573,15 @@ impl Scene {
} }
let table_handle = self.document.vports.handle(); let table_handle = self.document.vports.handle();
// Carry the grid/snap display state from the existing `*Active` entry
// across the rebuild — these flags track the in-app toggles and must
// not reset to VPort defaults each camera sync. (#121)
let display = self
.document
.vports
.iter()
.find(|v| v.name == "*Active")
.map(|v| (v.grid_on, v.snap_on, v.grid_spacing, v.snap_spacing, v.snap_base));
let preserved_vps: Vec<acadrust::tables::VPort> = self let preserved_vps: Vec<acadrust::tables::VPort> = self
.document .document
.vports .vports
@ -5604,11 +5613,32 @@ impl Scene {
let (ll, ur) = Self::tile_rect_to_vport(tile.rect); let (ll, ur) = Self::tile_rect_to_vport(tile.rect);
let mut entry = self.vport_from_camera("*Active", &tile.camera, ll, ur); let mut entry = self.vport_from_camera("*Active", &tile.camera, ll, ur);
entry.render_mode = tile.render_mode; entry.render_mode = tile.render_mode;
if let Some((grid_on, snap_on, grid_spacing, snap_spacing, snap_base)) = display {
entry.grid_on = grid_on;
entry.snap_on = snap_on;
entry.grid_spacing = grid_spacing;
entry.snap_spacing = snap_spacing;
entry.snap_base = snap_base;
}
entry.handle = self.document.allocate_handle(); entry.handle = self.document.allocate_handle();
self.document.vports.add_allow_duplicate(entry); self.document.vports.add_allow_duplicate(entry);
} }
} }
/// Stamp the grid/snap display state onto every `*Active` VPort entry so a
/// saved file reflects the in-app grid/snap toggles (otherwise other CAD
/// apps read VPort defaults). `grid_spacing` is applied to both axes and
/// mirrored to the snap spacing. (#121)
pub fn set_active_vport_display(&mut self, grid_on: bool, snap_on: bool, grid_spacing: f64) {
let spacing = acadrust::types::Vector2 { x: grid_spacing, y: grid_spacing };
for vp in self.document.vports.iter_mut().filter(|v| v.name == "*Active") {
vp.grid_on = grid_on;
vp.snap_on = snap_on;
vp.grid_spacing = spacing;
vp.snap_spacing = spacing;
}
}
/// Set the paper-space camera from the sheet viewport's stored view. /// Set the paper-space camera from the sheet viewport's stored view.
/// Returns true if a valid sheet viewport was found and the camera was set. /// Returns true if a valid sheet viewport was found and the camera was set.
/// ///