开源上游:acadrust 0.6.8 · https://github.com/hakanaktt/acadrust/tree/20ce21253fabe0868b576c41e801ba9d248a554d 构建:cargo build --locked --release --target wasm32-wasip1 --manifest-path scripts/batch-dwg-wasm/Cargo.toml Cargo.toml [package] name = "muxin-batch-dwg" version = "1.0.0" edition = "2021" publish = false [lib] crate-type = ["cdylib"] [dependencies] acadrust = { git = "https://github.com/hakanaktt/acadrust", rev = "20ce21253fabe0868b576c41e801ba9d248a554d", default-features = false } [profile.release] opt-level = 3 codegen-units = 1 strip = true panic = "abort" Cargo.lock # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "acadrust" version = "0.6.8" source = "git+https://github.com/hakanaktt/acadrust?rev=20ce21253fabe0868b576c41e801ba9d248a554d#20ce21253fabe0868b576c41e801ba9d248a554d" dependencies = [ "anyhow", "bitflags", "byteorder", "encoding_rs", "flate2", "foldhash", "indexmap", "itoa", "memchr", "memmap2", "nom", "oem_cp", "once_cell", "ryu", "thiserror", "web-time", ] [[package]] name = "adler2" version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" [[package]] name = "anyhow" version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" [[package]] name = "bitflags" version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" [[package]] name = "bumpalo" version = "3.20.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87a0cf66fd1eb2fb7b822cb78370d82303750809bc6ba855f3f58e517d3238db" [[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cfg-if" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" [[package]] name = "core_detect" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" [[package]] name = "crc32fast" version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b929dd2a135771f40afabd1073f85eed59c800b7c61901e5c254270497dad64" dependencies = [ "cfg-if", ] [[package]] name = "encoding_rs" version = "0.8.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89253175e704ab869b04f9db224d38630e0211a53841a79181deae0889e169f5" dependencies = [ "cfg-if", "core_detect", "multiversion_no_op", "rustversion", "scopeguard", "simdutf8", ] [[package]] name = "equivalent" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" [[package]] name = "flate2" version = "1.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "626d4928326b646e70f65ed36fc7cf9f206bdb68a0019ee7c6b4709da4e084c4" dependencies = [ "crc32fast", "miniz_oxide", "zlib-rs", ] [[package]] name = "foldhash" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" [[package]] name = "futures-core" version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" [[package]] name = "futures-task" version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" [[package]] name = "futures-util" version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" dependencies = [ "futures-core", "futures-task", "pin-project-lite", "slab", ] [[package]] name = "hashbrown" version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" [[package]] name = "indexmap" version = "2.14.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" dependencies = [ "equivalent", "hashbrown", ] [[package]] name = "itoa" version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" [[package]] name = "js-sys" version = "0.3.107" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" dependencies = [ "cfg-if", "futures-util", "wasm-bindgen", ] [[package]] name = "libc" version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" [[package]] name = "memchr" version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" [[package]] name = "memmap2" version = "0.9.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b0ca1155a9ecc25aa12e8313a119f32edf0255fea2f44839c4ac0a12b2ac157" dependencies = [ "libc", ] [[package]] name = "minimal-lexical" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "miniz_oxide" version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" dependencies = [ "adler2", "simd-adler32", ] [[package]] name = "multiversion_no_op" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d" [[package]] name = "muxin-batch-dwg" version = "1.0.0" dependencies = [ "acadrust", ] [[package]] name = "nom" version = "7.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" dependencies = [ "memchr", "minimal-lexical", ] [[package]] name = "oem_cp" version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c7959d3349f484aec36462f98226983578bf7d33f9b034c32967489c7000e1f" dependencies = [ "phf", ] [[package]] name = "once_cell" version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" [[package]] name = "phf" version = "0.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078" dependencies = [ "phf_shared", ] [[package]] name = "phf_shared" version = "0.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" dependencies = [ "siphasher", ] [[package]] name = "pin-project-lite" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" [[package]] name = "proc-macro2" version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" dependencies = [ "proc-macro2", ] [[package]] name = "rustversion" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" [[package]] name = "ryu" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "simd-adler32" version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" [[package]] name = "simdutf8" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" [[package]] name = "siphasher" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" [[package]] name = "slab" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" [[package]] name = "syn" version = "3.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "thiserror" version = "2.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" version = "2.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "unicode-ident" version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" [[package]] name = "wasm-bindgen" version = "0.2.130" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" dependencies = [ "cfg-if", "rustversion", "wasm-bindgen-macro", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" version = "0.2.130" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" dependencies = [ "quote", "wasm-bindgen-macro-support", ] [[package]] name = "wasm-bindgen-macro-support" version = "0.2.130" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" dependencies = [ "bumpalo", "proc-macro2", "quote", "syn", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" version = "0.2.130" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" dependencies = [ "unicode-ident", ] [[package]] name = "web-time" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" dependencies = [ "js-sys", "wasm-bindgen", ] [[package]] name = "zlib-rs" version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" src/lib.rs use acadrust::{objects::ObjectType, DwgWriter, DxfReader}; use std::{cell::RefCell, io::{self, Read, Seek, SeekFrom}}; thread_local! { static OUTPUT: RefCell> = const { RefCell::new(Vec::new()) }; } #[link(wasm_import_module = "cad_input")] extern "C" { fn read_at(offset: u32, destination: *mut u8, length: u32) -> u32; } // Seekable input backed by the worker's Blob. The combined DXF stays out of // linear memory while the reader builds the native database. struct DrawingInput { length: u32, position: u32, } impl Read for DrawingInput { fn read(&mut self, buffer: &mut [u8]) -> io::Result { let length = buffer.len().min((self.length - self.position) as usize) as u32; if length == 0 { return Ok(0); } let count = unsafe { read_at(self.position, buffer.as_mut_ptr(), length) }; if count != length { return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "Incomplete batch drawing")); } self.position += count; Ok(count as usize) } } impl Seek for DrawingInput { fn seek(&mut self, seek: SeekFrom) -> io::Result { let next = match seek { SeekFrom::Start(n) => i64::try_from(n).ok(), SeekFrom::Current(n) => (self.position as i64).checked_add(n), SeekFrom::End(n) => (self.length as i64).checked_add(n), }; let next = next.filter(|n| (0..=self.length as i64).contains(n)) .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid drawing offset"))?; self.position = next as u32; Ok(self.position as u64) } } #[no_mangle] pub extern "C" fn convert_batch(length: u32) -> i32 { let result = DxfReader::from_reader(DrawingInput { length, position: 0 }) .and_then(|reader| reader.read()) .and_then(|mut doc| { // DXF import has no usable DWG decomposition cache. Remove the // stale link that AutoCAD AUDIT reports as eWasErased. ACIS bodies // and their records remain owned by the actual REGION objects. for object in doc.objects.values_mut() { if let ObjectType::Dictionary(dictionary) = object { dictionary.remove("AcDsDecomposeData"); } } DwgWriter::write_to_vec(&doc) }); let (status, output) = match result { Ok(bytes) => (0, bytes), Err(error) => (1, error.to_string().into_bytes()), }; OUTPUT.with(|slot| *slot.borrow_mut() = output); status } #[no_mangle] pub extern "C" fn output_ptr() -> *const u8 { OUTPUT.with(|slot| slot.borrow().as_ptr()) } #[no_mangle] pub extern "C" fn output_len() -> usize { OUTPUT.with(|slot| slot.borrow().len()) }