Skip to main content

05_extrude/
05_extrude.rs

1//! Demo of `Solid::extrude`: push a closed 2D profile along a direction vector.
2//!
3//! - **Box**: square polygon extruded along Z
4//! - **Oblique cylinder**: circle extruded at a steep angle
5//! - **L-beam**: L-shaped polygon extruded along Z
6//! - **Heart**: BSpline heart-shaped profile extruded along Z
7
8use cadrum::{BSplineEnd, DVec3, Edge, Error, Solid};
9
10/// Square polygon → box (simplest extrude).
11fn build_box() -> Result<Solid, Error> {
12	let profile = Edge::polygon(&[
13		DVec3::new(0.0, 0.0, 0.0),
14		DVec3::new(5.0, 0.0, 0.0),
15		DVec3::new(5.0, 5.0, 0.0),
16		DVec3::new(0.0, 5.0, 0.0),
17	])?;
18	Solid::extrude(&profile, DVec3::Z * 8.0)
19}
20
21/// Circle extruded at a steep angle → oblique cylinder.
22fn build_oblique_cylinder() -> Result<Solid, Error> {
23	let profile = [Edge::circle(3.0, DVec3::Z)?];
24	Solid::extrude(&profile, DVec3::new(-4.0, -6.0, 8.0))
25}
26
27/// L-shaped polygon → L-beam.
28fn build_l_beam() -> Result<Solid, Error> {
29	let profile = Edge::polygon(&[
30		DVec3::new(0.0, 0.0, 0.0),
31		DVec3::new(4.0, 0.0, 0.0),
32		DVec3::new(4.0, 1.0, 0.0),
33		DVec3::new(1.0, 1.0, 0.0),
34		DVec3::new(1.0, 3.0, 0.0),
35		DVec3::new(0.0, 3.0, 0.0),
36	])?;
37	Solid::extrude(&profile, DVec3::Z * 12.0)
38}
39
40/// Heart-shaped BSpline profile extruded along Z.
41fn build_heart() -> Result<Solid, Error> {
42	let profile = [Edge::bspline(
43		&[
44			DVec3::new(0.0, -4.0, 0.0),   // bottom tip
45			DVec3::new(2.0, -1.5, 0.0),
46			DVec3::new(4.0, 1.5, 0.0),
47			DVec3::new(2.5, 3.5, 0.0),    // right lobe top
48			DVec3::new(0.0, 2.0, 0.0),    // center dip
49			DVec3::new(-2.5, 3.5, 0.0),   // left lobe top
50			DVec3::new(-4.0, 1.5, 0.0),
51			DVec3::new(-2.0, -1.5, 0.0),
52		],
53		BSplineEnd::Periodic,
54	)?];
55	Solid::extrude(&profile, DVec3::Z * 7.0)
56}
57
58fn main() -> Result<(), Error> {
59	let example_name = std::path::Path::new(file!()).file_stem().unwrap().to_str().unwrap();
60
61	let box_solid = build_box()?.color("#b0d4f1");
62	let oblique = build_oblique_cylinder()?.color("#f1c8b0").translate(DVec3::X * 10.0);
63	let l_beam = build_l_beam()?.color("#b0f1c8").translate(DVec3::X * 20.0);
64	let heart = build_heart()?.color("#f1b0b0").translate(DVec3::X * 30.0);
65
66	let result = [box_solid, oblique, l_beam, heart];
67
68	let mut f = std::fs::File::create(format!("{example_name}.step")).expect("failed to create STEP file");
69	cadrum::write_step(&result, &mut f).expect("failed to write STEP");
70
71	let mut f = std::fs::File::create(format!("{example_name}.svg")).expect("failed to create SVG file");
72	cadrum::mesh(&result, 0.5).and_then(|m| m.write_svg(DVec3::ONE, DVec3::Z, true, false, &mut f)).expect("failed to write SVG");
73
74	println!("wrote {example_name}.step / {example_name}.svg");
75	Ok(())
76}