Write read
Read and write: chain STEP and BRep round-trips with progressive rotation.
//! Read and write: chain STEP and BRep round-trips with progressive rotation. use cadrum::{DVec3, Solid}; use std::f64::consts::FRAC_PI_8; fn main() -> Result<(), cadrum::Error> { let example_name = std::path::Path::new(file!()).file_stem().unwrap().to_str().unwrap(); let step_path = format!("{example_name}.step"); let brep_path = format!("{example_name}.brep"); // 0. Original: read colored_box.step let manifest_dir = env!("CARGO_MANIFEST_DIR"); let original = Solid::read_step(&mut std::fs::File::open(format!("{manifest_dir}/steps/colored_box.step")).expect("open file"))?; // 1. STEP round-trip: rotate 30° → write → read let a_written: Vec<Solid> = original.clone().into_iter().map(|s| s.rotate_x(FRAC_PI_8)).collect(); Solid::write_step(&a_written, &mut std::fs::File::create(&step_path).expect("create file"))?; let a = Solid::read_step(&mut std::fs::File::open(&step_path).expect("open file"))?; // 2. BRep round-trip: rotate another 30° → write → read let b_written: Vec<Solid> = a.clone().into_iter().map(|s| s.rotate_x(FRAC_PI_8)).collect(); Solid::write_brep(&b_written, &mut std::fs::File::create(&brep_path).expect("create file"))?; let b = Solid::read_brep(&mut std::fs::File::open(&brep_path).expect("open file"))?; // 3. Arrange side by side and export SVG + STL let [min, max] = original[0].bounding_box(); let spacing = (max - min).length() * 1.5; let all: Vec<Solid> = [original, a, b].into_iter().enumerate().flat_map(|(i, solids)| solids.into_iter().map(move |s| s.translate(DVec3::X * spacing * i as f64))).collect(); let mesh = Solid::mesh(&all, Default::default())?; let scene = mesh.scene(cadrum::SceneOption { view: DVec3::new(1.0, 1.0, 2.0), ..Default::default() }); scene.write_svg(&mut std::fs::File::create(format!("{example_name}.svg")).unwrap())?; scene.write_png([640, 640], &mut std::fs::File::create(format!("{example_name}.png")).unwrap())?; mesh.write_stl(&mut std::fs::File::create(format!("{example_name}.stl")).unwrap())?; mesh.write_gltf_binary(&mut std::fs::File::create(format!("{example_name}.glb")).unwrap())?; // 4. Print summary let stl_path = format!("{example_name}.stl"); for (label, path) in [("STEP", &step_path), ("BRep", &brep_path), ("STL", &stl_path)] { let size = std::fs::metadata(path).map(|m| m.len()).unwrap_or(0); println!("{label:12} {path:30} {size:>8} bytes"); } Ok(()) }
Output: 02_write_read.png | 02_write_read.step | 02_write_read.glb | 02_write_read.brep | 02_write_read.stl | 02_write_read.svg