Merge PR #1028: add document API with atomic edits

Add the typed document facade and plugin IPC adapter. Prepare bulk edits
before mutation, preserve undo and dirty state, keep profile elevations,
validate numeric inputs and session bindings, and generate Python constructors
with typed results.

Reuse existing entity transforms and profile conversion. Compute mass
properties and closed-surface bounds in cadkernel; pin its main revision
6a5e6fc2822f6af6b2611cd605b44437b35158f0. Keep plugin API metadata at version 6.

Validation: 646 native application tests, 597 kernel tests, 12 document API
tests, 91 plugin API tests, 3 Python tests, default/IPC feature builds and
wasm32 check passed.
This commit is contained in:
Hakan Seven committed 2026-09-06 21:43:02 +03:00
commit 3385160493
50 files changed
+11481 -6

No files matched your search

+3484
View File
File diff suppressed because it is too large. Load diff
+2
View File
@@ -14,6 +14,8 @@ pub(crate) mod expr_eval;
mod find_replace;
pub(crate) mod helpers;
mod history;
#[cfg(not(target_arch = "wasm32"))]
mod doc_api;
mod layers;
mod model_ops;
mod mtext_editor;
+40
View File
@@ -16,6 +16,8 @@ pub(crate) struct HostSession<'a> {
app: &'a mut OpenCADStudio,
tab: usize,
doc_store: Option<DocumentSnapshotStore<DocumentViewData>>,
/// In-flight DocApi v2 undo delta (between `push_undo` and `finalize_op`).
doc_api_pending: Option<super::history::PendingDelta>,
}
impl<'a> HostSession<'a> {
@@ -24,6 +26,7 @@ impl<'a> HostSession<'a> {
app,
tab,
doc_store: None,
doc_api_pending: None,
}
}
@@ -242,6 +245,39 @@ impl<'a> HostSession<'a> {
self.app.push_undo_snapshot(self.tab, label);
}
// ── DocApi v2 accessors (used by `app::doc_api` backend) ───────────────
pub(crate) fn scene(&self) -> &crate::scene::Scene {
&self.app.tabs[self.tab].scene
}
pub(crate) fn scene_mut(&mut self) -> &mut crate::scene::Scene {
&mut self.app.tabs[self.tab].scene
}
/// DocApi `push_undo`: capture entity and document-structure changes for one op.
pub(crate) fn begin_doc_api_undo(&mut self, label: &str) {
self.doc_api_pending = self.app.begin_undo(self.tab, label, 1, false);
}
pub(crate) fn cancel_doc_api_undo(&mut self) {
if self.doc_api_pending.take().is_some() {
self.scene_mut().take_undo_recording();
}
}
/// DocApi `finalize_op`: close the delta + republish the document view.
/// The underlying entity op (add/update/remove/register_solid_model) already
/// bumped `geometry_epoch`, so we do NOT bump again here — exactly one undo
/// step is recorded, and the epoch has advanced for this op.
pub(crate) fn commit_doc_api_undo(&mut self) {
self.set_dirty();
if let Some(pending) = self.doc_api_pending.take() {
self.app.commit_undo_delta(self.tab, pending);
}
self.publish_document_view();
}
pub fn set_dirty(&mut self) {
self.app.tabs[self.tab].dirty = true;
}
@@ -360,6 +396,10 @@ impl HostApi for HostSession<'_> {
fn close_document_view_v4(&mut self, tab_id: u64) {
self.close_document_view_v4(tab_id)
}
#[cfg(not(target_arch = "wasm32"))]
fn doc_api_dispatch(&mut self, tab_id: u64, bytes: &[u8]) -> Result<Vec<u8>, String> {
super::doc_api::execute_doc_api(self, tab_id, bytes)
}
}
/// Bridges a plugin's [`InteractiveCommand`](ocs_plugin_api::host::InteractiveCommand)
+11
View File
@@ -1807,6 +1807,13 @@ pub struct Scene {
pub block_meshes: HashMap<Handle, MeshLodSet>,
/// Kernel B-reps used by solid operations and exact-geometry saves.
pub solid_models: HashMap<Handle, cadkernel::brep::Body>,
/// Memoized DocApi volume/centroid per (handle, geometry_epoch) so repeated
/// mass queries on the same solid are O(1). Persists across DocApi dispatches.
#[cfg(not(target_arch = "wasm32"))]
pub doc_api_mass_cache: std::collections::HashMap<Handle, (u64, f64, [f64; 3])>,
/// Bounds uncached mass computations in one API request.
#[cfg(not(target_arch = "wasm32"))]
pub doc_api_cold_tess_used: usize,
/// GPU render data for raster images (RasterImage entities), keyed by handle.
pub images: HashMap<Handle, ImageModel>,
/// The viewport that is currently "entered" (MSPACE mode).
@@ -2068,6 +2075,10 @@ impl Scene {
material_base_dir: None,
block_meshes: HashMap::default(),
solid_models: HashMap::default(),
#[cfg(not(target_arch = "wasm32"))]
doc_api_mass_cache: std::collections::HashMap::default(),
#[cfg(not(target_arch = "wasm32"))]
doc_api_cold_tess_used: 0,
images: HashMap::default(),
active_viewport: None,
bg_color: [33.0 / 255.0, 40.0 / 255.0, 48.0 / 255.0, 1.0],