cad-editor/examples/inspect_insert.rs
Hakan Seven 03555ce8bd chore: expose lib target + open-path profiling examples
Examples can't import from a bin-only crate, so add a thin lib.rs that
re-exports the existing modules. Used by the new examples that helped
diagnose the freeze fixed in the previous commit.

- examples/time_open: times parse / purge / cache / first-wires phases
- examples/inspect_insert: lists Insert blocks with sub-entity counts
- examples/find_raster: dumps RasterImage path + dimensions

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-13 23:02:54 +03:00

43 lines
1.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Lists every Insert in the doc (after xref resolve) with sub-entity counts.
// cargo run --release --example inspect_insert -- <dwg>
use acadrust::entities::EntityType;
use acadrust::io::dwg::DwgReader;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let path = std::env::args().nth(1).unwrap();
let mut doc = DwgReader::from_file(&path)?.read()?;
if let Some(base) = std::path::Path::new(&path).parent() {
let infos = H7CAD::io::xref::resolve_xrefs(&mut doc, base);
for i in &infos {
println!("XREF: {} -> {:?}", i.name, i.status);
}
}
println!("total entities: {}", doc.entities().count());
let inserts: Vec<_> = doc
.entities()
.filter_map(|e| if let EntityType::Insert(i) = e { Some(i.clone()) } else { None })
.collect();
println!("total inserts: {}", inserts.len());
let mut by_block: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
for i in &inserts {
*by_block.entry(i.block_name.clone()).or_insert(0) += 1;
}
let mut by_block: Vec<_> = by_block.into_iter().collect();
by_block.sort_by(|a, b| b.1.cmp(&a.1));
println!("\ninserts grouped by block_name:");
for (name, n) in &by_block {
let sub_count = inserts.iter().find(|i| &i.block_name == name)
.map(|i| i.explode_from_document(&doc).len())
.unwrap_or(0);
let nested = doc.block_records.get(name)
.map(|br| br.entity_handles.iter()
.filter(|h| matches!(doc.get_entity(**h), Some(EntityType::Insert(_))))
.count())
.unwrap_or(0);
println!(" {:>4} × {} (sub={}, nested-inserts={})", n, name, sub_count, nested);
}
Ok(())
}