kcl_lib/std/sweep.rs
1//! Standard library sweep.
2
3use anyhow::Result;
4use kcl_derive_docs::stdlib;
5use kcmc::{each_cmd as mcmd, length_unit::LengthUnit, ModelingCmd};
6use kittycad_modeling_cmds::{self as kcmc};
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9
10use crate::{
11 errors::KclError,
12 execution::{
13 kcl_value::{ArrayLen, RuntimeType},
14 ExecState, Helix, KclValue, PrimitiveType, Sketch, Solid,
15 },
16 parsing::ast::types::TagNode,
17 std::{extrude::do_post_extrude, fillet::default_tolerance, Args},
18};
19
20/// A path to sweep along.
21#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
22#[ts(export)]
23#[serde(untagged)]
24pub enum SweepPath {
25 Sketch(Sketch),
26 Helix(Box<Helix>),
27}
28
29/// Extrude a sketch along a path.
30pub async fn sweep(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
31 let sketches = args.get_unlabeled_kw_arg_typed(
32 "sketches",
33 &RuntimeType::Array(PrimitiveType::Sketch, ArrayLen::NonEmpty),
34 exec_state,
35 )?;
36 let path: SweepPath = args.get_kw_arg("path")?;
37 let sectional = args.get_kw_arg_opt("sectional")?;
38 let tolerance = args.get_kw_arg_opt("tolerance")?;
39 let tag_start = args.get_kw_arg_opt("tagStart")?;
40 let tag_end = args.get_kw_arg_opt("tagEnd")?;
41
42 let value = inner_sweep(
43 sketches, path, sectional, tolerance, tag_start, tag_end, exec_state, args,
44 )
45 .await?;
46 Ok(value.into())
47}
48
49/// Extrude a sketch along a path.
50///
51/// This, like extrude, is able to create a 3-dimensional solid from a
52/// 2-dimensional sketch. However, unlike extrude, this creates a solid
53/// by using the extent of the sketch as its path. This is useful for
54/// creating more complex shapes that can't be created with a simple
55/// extrusion.
56///
57/// You can provide more than one sketch to sweep, and they will all be
58/// swept along the same path.
59///
60/// ```no_run
61/// // Create a pipe using a sweep.
62///
63/// // Create a path for the sweep.
64/// sweepPath = startSketchOn('XZ')
65/// |> startProfileAt([0.05, 0.05], %)
66/// |> line(end = [0, 7])
67/// |> tangentialArc({
68/// offset: 90,
69/// radius: 5
70/// }, %)
71/// |> line(end = [-3, 0])
72/// |> tangentialArc({
73/// offset: -90,
74/// radius: 5
75/// }, %)
76/// |> line(end = [0, 7])
77///
78/// // Create a hole for the pipe.
79/// pipeHole = startSketchOn('XY')
80/// |> circle(
81/// center = [0, 0],
82/// radius = 1.5,
83/// )
84///
85/// sweepSketch = startSketchOn('XY')
86/// |> circle(
87/// center = [0, 0],
88/// radius = 2,
89/// )
90/// |> hole(pipeHole, %)
91/// |> sweep(path = sweepPath)
92/// ```
93///
94/// ```no_run
95/// // Create a spring by sweeping around a helix path.
96///
97/// // Create a helix around the Z axis.
98/// helixPath = helix(
99/// angleStart = 0,
100/// ccw = true,
101/// revolutions = 4,
102/// length = 10,
103/// radius = 5,
104/// axis = 'Z',
105/// )
106///
107///
108/// // Create a spring by sweeping around the helix path.
109/// springSketch = startSketchOn('YZ')
110/// |> circle( center = [0, 0], radius = 1)
111/// |> sweep(path = helixPath)
112/// ```
113///
114/// ```
115/// // Sweep two sketches along the same path.
116///
117/// sketch001 = startSketchOn('XY')
118/// rectangleSketch = startProfileAt([-200, 23.86], sketch001)
119/// |> angledLine([0, 73.47], %, $rectangleSegmentA001)
120/// |> angledLine([
121/// segAng(rectangleSegmentA001) - 90,
122/// 50.61
123/// ], %)
124/// |> angledLine([
125/// segAng(rectangleSegmentA001),
126/// -segLen(rectangleSegmentA001)
127/// ], %)
128/// |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
129/// |> close()
130///
131/// circleSketch = circle(sketch001, center = [200, -30.29], radius = 32.63)
132///
133/// sketch002 = startSketchOn('YZ')
134/// sweepPath = startProfileAt([0, 0], sketch002)
135/// |> yLine(length = 231.81)
136/// |> tangentialArc({
137/// radius = 80,
138/// offset = -90,
139/// }, %)
140/// |> xLine(length = 384.93)
141///
142/// sweep([rectangleSketch, circleSketch], path = sweepPath)
143/// ```
144#[stdlib {
145 name = "sweep",
146 feature_tree_operation = true,
147 keywords = true,
148 unlabeled_first = true,
149 args = {
150 sketches = { docs = "The sketch or set of sketches that should be swept in space" },
151 path = { docs = "The path to sweep the sketch along" },
152 sectional = { docs = "If true, the sweep will be broken up into sub-sweeps (extrusions, revolves, sweeps) based on the trajectory path components." },
153 tolerance = { docs = "Tolerance for this operation" },
154 tag_start = { docs = "A named tag for the face at the start of the sweep, i.e. the original sketch" },
155 tag_end = { docs = "A named tag for the face at the end of the sweep" },
156 }
157}]
158#[allow(clippy::too_many_arguments)]
159async fn inner_sweep(
160 sketches: Vec<Sketch>,
161 path: SweepPath,
162 sectional: Option<bool>,
163 tolerance: Option<f64>,
164 tag_start: Option<TagNode>,
165 tag_end: Option<TagNode>,
166 exec_state: &mut ExecState,
167 args: Args,
168) -> Result<Vec<Solid>, KclError> {
169 let trajectory = match path {
170 SweepPath::Sketch(sketch) => sketch.id.into(),
171 SweepPath::Helix(helix) => helix.value.into(),
172 };
173
174 let mut solids = Vec::new();
175 for sketch in &sketches {
176 let id = exec_state.next_uuid();
177 args.batch_modeling_cmd(
178 id,
179 ModelingCmd::from(mcmd::Sweep {
180 target: sketch.id.into(),
181 trajectory,
182 sectional: sectional.unwrap_or(false),
183 tolerance: LengthUnit(tolerance.unwrap_or(default_tolerance(&args.ctx.settings.units))),
184 }),
185 )
186 .await?;
187
188 solids.push(
189 do_post_extrude(
190 sketch,
191 id.into(),
192 0.0,
193 &super::extrude::NamedCapTags {
194 start: tag_start.as_ref(),
195 end: tag_end.as_ref(),
196 },
197 exec_state,
198 &args,
199 )
200 .await?,
201 );
202 }
203
204 Ok(solids)
205}