1use cadrum::{Color, Compound, DVec3, Edge, ProfileOrient, Solid};
4use std::f64::consts::PI;
5
6pub fn chijin() -> Result<Solid, cadrum::Error> {
7 let cylinder = Solid::cylinder(15.0, DVec3::Z, 8.0)
9 .translate(DVec3::Z * -4.0)
10 .color("#999");
11
12 let cross_section = Edge::polygon(&[
19 DVec3::new(0.0, 0.0, 5.0),
20 DVec3::new(15.0, 0.0, 5.0),
21 DVec3::new(17.0, 0.0, 3.0),
22 DVec3::new(15.0, 0.0, 4.0),
23 DVec3::new(0.0, 0.0, 4.0),
24 ])?;
25 let spine = Edge::circle(1.0, DVec3::Z)?;
26 let sheet = Solid::sweep(&cross_section, &[spine], ProfileOrient::Up(DVec3::Z))?
27 .color("#fff");
28 let sheets = [sheet.clone().mirror(DVec3::ZERO, DVec3::Z), sheet];
29
30 let block_proto = Solid::cube(2.0, 8.0, 1.0)
32 .translate(DVec3::new(-1.0, -4.0, -0.5))
33 .rotate_z(60.0_f64.to_radians())
34 .translate(DVec3::Y * 15.0);
35
36 let hole_proto = Solid::cylinder(0.7, DVec3::X * 10.0 + DVec3::Z * 30.0, 30.0)
38 .translate(DVec3::new(-5.0, 16.0, -15.0));
39
40 const N: usize = 20;
43 let angle = |i: usize| 2.0 * PI * (i as f64) / (N as f64);
44 let color = |i: usize| Color::from_hsv(i as f32 / N as f32, 1.0, 1.0);
45 let blocks: [Solid; N] = std::array::from_fn(|i| block_proto.clone().rotate_z(angle(i)).color(color(i)));
46 let holes: [Solid; N] = std::array::from_fn(|i| hole_proto.clone().rotate_z(angle(i)));
47 let result = [cylinder]
49 .union(&sheets)?
50 .subtract(&holes)?
51 .union(&blocks)?;
52 assert!(result.len() == 1);
53 Ok(result.into_iter().next().unwrap())
54}
55
56fn main() -> Result<(), cadrum::Error> {
57 let example_name = std::path::Path::new(file!()).file_stem().unwrap().to_str().unwrap();
58 let result = [chijin()?];
59
60 let mut f = std::fs::File::create(format!("{example_name}.step")).expect("failed to create STEP file");
61 cadrum::write_step(&result, &mut f).expect("failed to write STEP");
62
63 let mut f = std::fs::File::create(format!("{example_name}.svg")).expect("failed to create SVG file");
64 cadrum::mesh(&result, 0.5).and_then(|m| m.write_svg(DVec3::ONE, DVec3::Z, true, false, &mut f)).expect("failed to write SVG");
65
66 println!("wrote {example_name}.step / {example_name}.svg");
67 Ok(())
68}