From f2c4a1736af8f662717f1ba47fd90dc8cb2331f3 Mon Sep 17 00:00:00 2001 From: Hakan Seven Date: Tue, 16 Jun 2026 17:15:01 +0300 Subject: [PATCH] =?UTF-8?q?feat(wasm):=20load=5Fbytes=20=E2=80=94=20parse?= =?UTF-8?q?=20CAD=20documents=20from=20memory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a cross-platform `io::load_bytes(name, bytes)` that parses DWG/DXF from an in-memory buffer via acadrust's stream readers (Cursor), picking the format from the name's extension. This is the foundation for opening files on the web, where the browser delivers bytes rather than a path. Native keeps the streaming `load_file` (lower memory on large drawings); both share the post-load fixups. Builds on native and wasm. Co-Authored-By: Claude Opus 4.8 --- src/io/mod.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/io/mod.rs b/src/io/mod.rs index 956ed924b..8d4bd8cce 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -86,6 +86,37 @@ pub async fn open_path_with_phase( Ok((name, path, doc, caches)) } +/// Parse a CAD document from in-memory bytes, choosing the format from +/// `name`'s extension. Used by the web build, where files arrive as bytes from +/// a browser file picker (no filesystem path). Shares the post-load fixups with +/// [`load_file`]; raster-image path resolution is skipped (there is no sibling +/// directory to search on the web). +pub fn load_bytes(name: &str, bytes: Vec) -> Result { + use std::io::Cursor; + let ext = name.rsplit('.').next().unwrap_or_default().to_lowercase(); + match ext.as_str() { + "dwg" => { + let mut doc = DwgReader::from_stream(Cursor::new(bytes)) + .read() + .map_err(|e| e.to_string())?; + fix_viewport_status_flags(&mut doc); + fix_current_style_names(&mut doc); + Ok(doc) + } + "dxf" => { + let mut doc = DxfReader::from_reader(Cursor::new(bytes)) + .map_err(|e| e.to_string())? + .read() + .map_err(|e| e.to_string())?; + fix_dxf_dimension_rotations(&mut doc); + fix_viewport_status_flags(&mut doc); + fix_current_style_names(&mut doc); + Ok(doc) + } + _ => Err(format!("Unsupported file format: .{ext}")), + } +} + /// Load a DWG or DXF file directly from a path (auto-detect by extension). pub fn load_file(path: &Path) -> Result { let ext = path