Skip to content
cadkithome

Scene and GPU rendering

Two Rust crates turn drawings into pixels, separately from file I/O:

  • cadkit-scene is scene data that needs no GPU. Plan2d stores 2D lines, polylines, arcs and circles in fixed-size chunks, encodes each chunk once for the GPU in f32 coordinates relative to the chunk centre, and checks every frame whether that precision still meets a screen-space error budget. Chunks that do not, and objects you mark as overlays, are handed back for your CPU drawing path. Mesh3d holds f64 triangle meshes.
  • cadkit-gpu draws them with wgpu 30 (re-exported as cadkit_gpu::wgpu): analytic antialiased strokes with mitred corners, lit depth-tested meshes with an orbit camera, and offscreen rendering with readback. The renderers take your device, queue and render pass; they open no window, so any UI toolkit can host them.

Neither is exposed by the Python, JavaScript or C packages; cadkit-wasm renders through SVG.

Cargo.toml
[dependencies]
cadkit = "0.5.3"
cadkit-geom = "0.5.3"
cadkit-scene = { version = "0.5.3", features = ["document"] }
cadkit-gpu = "0.5.3"

From a drawing to plan data, without a GPU

Section titled “From a drawing to plan data, without a GPU”

The document feature of cadkit-scene bridges a read drawing to the plan: plan_document expands blocks and arrays, projects one model onto XY, makes one group per layer, resolves colours and lineweight widths, and flattens curves that do not stay circular.

src/main.rs
use std::collections::BTreeSet;
use cadkit_geom::Point2;
use cadkit_scene::document::{DocumentPlanOptions, plan_document};
use cadkit_scene::{Plan2d, Plan2dOptions, PlanView};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let doc = cadkit::open("plan.dxf")?;
// Model 0 to plan items: blocks expanded, XY projection, one group per layer.
let planned = plan_document(&doc, &DocumentPlanOptions::new());
let report = &planned.report;
println!("drawn {}, flattened {}, skipped {:?}", report.drawn, report.flattened, report.skipped);
let mut plan = Plan2d::new(Plan2dOptions::new());
for (id, item) in planned.items {
plan.upsert(id, item);
}
// A 1280 x 800 canvas centred on (6, 4.5) at 80 logical pixels per drawing unit.
let view = PlanView::new(Point2::new(6.0, 4.5), 80.0, [1280.0, 800.0], 1280.0);
plan.prepare(&view, |_| true);
let frame = plan.frame(&view, |_| true, &BTreeSet::new(), true);
println!("{} chunk draws for the GPU, {} objects for the CPU path", frame.draws.len(), frame.cpu.len());
Ok(())
}

On the sample floor plan it reports 135 entities drawn and 26 skipped (text) and splits the view into GPU chunk draws. The DocumentPlanReport tells you what was not drawn as stored: skipped kinds, flattened curves, dashed linetypes drawn solid and whether the traversal budget ran out. Text, hatches and fills, points, images and viewports are not drawn by this bridge; draw them through your own path if you need them.

src/plan_paint.rs
use cadkit_gpu::{PlanRenderer, PlanTarget, wgpu};
use cadkit_scene::PlanFrame;
/// Draws the GPU part of a plan frame into a render pass the host application owns.
/// `frame_id` must increase for every painted canvas and frame.
pub fn paint_plan(
renderer: &mut PlanRenderer,
device: &wgpu::Device,
queue: &wgpu::Queue,
pass: &mut wgpu::RenderPass<'_>,
frame: &PlanFrame,
frame_id: u64,
) {
// Viewport x, y, width, height in physical pixels, and physical pixels per logical pixel.
let target = PlanTarget::new([0.0, 0.0, 1280.0, 800.0], 1.0);
renderer.prepare(device, queue, frame_id, &frame.draws, target);
renderer.paint(pass, frame_id);
// frame.cpu and frame.labels are left to the host's CPU drawing path.
}

Create one PlanRenderer::new(&device, surface_format) and call prepare and paint every frame inside a render pass you own. Chunk buffers are uploaded once per content change; moving the camera rewrites only a few bytes per chunk. Objects in frame.cpu (chunks over the precision budget, overlays such as a selection) are yours to draw, for example highlighted on top. GPU validation and out-of-memory errors are returned as GpuErrors.

From a checkout, an example renders a DWG, DXF or DGN plan to PNG offscreen (it reports CPU handoffs but does not draw them):

Terminal window
cargo run -p cadkit-gpu --example render_png -- plan.dxf plan.png 1600 1000