Skip to main content

geop_ops_pattern/
examples.rs

1//! The examples of patterns an editor offers: a plate drilled with a row
2//! of holes, one hole patterned; and two drawn in the editor, a turbine
3//! and a flange, each patterned round its axis.
4
5use geop_core_editor::registry::ExampleSet;
6use geop_core_math::primitives::FrameAxis;
7use geop_core_program::program::Program;
8use geop_ops_extrude_revolve::{Extents, ExtrudeArgs, Output};
9use geop_ops_model::Combine;
10use geop_ops_model::datums::{origin_axis, origin_plane};
11use geop_ops_model::sketch::{
12    Sketch, SketchPlane,
13    figures::{circle, rectangle, solved},
14    whole_sketch,
15};
16use geop_ops_sketch::AddSketchArgs;
17
18use crate::{Direction, LinearPatternArgs, PatternDirection, Seeds, feature};
19use geop_ops_model::spacing::Spacing;
20
21/// The examples of this crate, by name.
22pub fn set() -> ExampleSet {
23    ExampleSet {
24        programs: vec![
25            ("patterned_plate", patterned_plate),
26            ("turbine_blade", turbine_blade),
27            ("cool_flange", cool_flange),
28        ],
29        workspaces: Vec::new(),
30    }
31}
32
33/// An 8 x 3 x 0.5 plate with a row of four holes: a circle sketched on the
34/// Z plane and cut through the plate (`hole`), and that feature done again
35/// three more times 2 apart along `x` (`holes`) — the drilled plate is
36/// `linear_pattern(holes)`, and the holes follow the hole when it changes.
37pub fn patterned_plate() -> Program {
38    let mut program = Program::new();
39    let z_plane = || SketchPlane::Datum(origin_plane(FrameAxis::Z));
40    let mut outline = Sketch::new();
41    rectangle(&mut outline, [0.0, 0.0], 8.0, 3.0);
42    program.push("outline", AddSketchArgs::on(z_plane(), solved(outline)));
43    program.push(
44        "plate",
45        ExtrudeArgs {
46            sketch: whole_sketch("outline"),
47            extent: Extents::blind(0.5),
48            output: Output::Solid(Combine::NewBody),
49            draft: 0.0,
50        },
51    );
52    let mut hole = Sketch::new();
53    circle(&mut hole, [1.0, 1.5], 0.3);
54    program.push("hole_sketch", AddSketchArgs::on(z_plane(), solved(hole)));
55    let mut through = Extents::blind(2.0);
56    through.symmetric = true;
57    program.push(
58        "hole",
59        ExtrudeArgs {
60            sketch: whole_sketch("hole_sketch"),
61            extent: through,
62            output: Output::Solid(Combine::cut("extrude(plate)")),
63            draft: 0.0,
64        },
65    );
66    program.push(
67        "holes",
68        LinearPatternArgs {
69            seeds: Seeds::features(vec![feature("hole")]),
70            first: Direction {
71                along: Some(PatternDirection::Datum(origin_axis(FrameAxis::X))),
72                reversed: false,
73                count: 4.0.into(),
74                spacing: Spacing::step(2.0),
75            },
76            second: None,
77        },
78    );
79    program
80}
81
82/// A turbine: a blade lofted through sections on three offset datum
83/// planes, patterned six times round a revolved hub and joined to it, as
84/// drawn in the editor.
85///
86/// An example drawn in the editor and kept as the program it saved (in
87/// `src/drawn/`): its sketches as they were drawn, which no code says
88/// better. Its tests build it like every other example, so a change to the
89/// format that breaks it is found there: save it again from the editor.
90pub fn turbine_blade() -> Program {
91    Program::from_json(include_str!("drawn/turbine_blade.geop"))
92        .expect("a drawn example is a program")
93}
94
95/// A revolved flange with a slot cut through it, the cut patterned sixteen
96/// times round its axis, as drawn in the editor (see [`turbine_blade`]).
97pub fn cool_flange() -> Program {
98    Program::from_json(include_str!("drawn/cool_flange.geop"))
99        .expect("a drawn example is a program")
100}