Files
OneCAD-Draft/crates/ocs_doc_api/examples/e1_primitives_and_queries.rs
T
Hakan Seven 3385160493 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.
2026-09-06 21:43:02 +03:00

46 lines
1.5 KiB
Rust

//! Example 1 — Create solid primitives and query their geometry.
//!
//! Run: `cargo run --example e1_primitives_and_queries --features host`
//!
//! Shows: `doc.solids().create_*` for the supported primitives, and the read
//! queries `bounds()` / `volume()` / `centroid()` on a `Solid`.
mod common;
use ocs_doc_api::DocApi;
fn main() -> ocs_doc_api::ApiResult<()> {
// In-process document over the mock backend (no host binary needed).
let api = DocApi::in_process(common::MockBackend::default(), 0);
let doc = api.document(api.active_tab());
// The supported solid primitives.
let block = doc
.solids()
.create_cuboid([0.0, 0.0, 0.0], [10.0, 10.0, 10.0])?;
let ball = doc.solids().create_sphere([5.0, 5.0, 5.0], 6.0)?;
let cyl = doc.solids().create_cylinder([0.0, 0.0, 0.0], 4.0, 12.0)?;
// Geometric queries are entity methods — one host call each.
let bb = block.bounds()?;
let vol = ball.volume()?;
let cg = cyl.centroid()?;
println!("cuboid bounds : {bb:?}");
println!("sphere volume : {vol:.2} (analytic ~904.78)");
println!("cylinder centroid: {cg:?}");
// Read-only batching: same queries, ONE round-trip.
let res = doc.query_batch(|q| {
q.bounds(&block);
q.volume(&ball);
q.centroid(&cyl);
})?;
let (bb2, vol2, cg2) = (res.bounds(0)?, res.volume(1)?, res.centroid(2)?);
assert_eq!(bb, bb2);
assert!((vol - vol2).abs() < 1e-9);
assert_eq!(cg, cg2);
println!("query_batch matches the per-call results");
Ok(())
}