kcl_lib/std/
sweep.rs

1//! Standard library sweep.
2
3use anyhow::Result;
4use kcmc::{each_cmd as mcmd, length_unit::LengthUnit, ModelingCmd};
5use kittycad_modeling_cmds::{self as kcmc, shared::RelativeTo};
6use schemars::JsonSchema;
7use serde::Serialize;
8
9use super::{args::TyF64, DEFAULT_TOLERANCE};
10use crate::{
11    errors::KclError,
12    execution::{
13        types::{NumericType, RuntimeType},
14        ExecState, Helix, KclValue, Sketch, Solid,
15    },
16    parsing::ast::types::TagNode,
17    std::{extrude::do_post_extrude, Args},
18};
19
20/// A path to sweep along.
21#[derive(Debug, Clone, 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("sketches", &RuntimeType::sketches(), exec_state)?;
32    let path: SweepPath = args.get_kw_arg_typed(
33        "path",
34        &RuntimeType::Union(vec![RuntimeType::sketch(), RuntimeType::helix()]),
35        exec_state,
36    )?;
37    let sectional = args.get_kw_arg_opt_typed("sectional", &RuntimeType::bool(), exec_state)?;
38    let tolerance: Option<TyF64> = args.get_kw_arg_opt_typed("tolerance", &RuntimeType::length(), exec_state)?;
39    let relative_to: Option<String> = args.get_kw_arg_opt_typed("relativeTo", &RuntimeType::string(), exec_state)?;
40    let tag_start = args.get_kw_arg_opt("tagStart")?;
41    let tag_end = args.get_kw_arg_opt("tagEnd")?;
42
43    let value = inner_sweep(
44        sketches,
45        path,
46        sectional,
47        tolerance,
48        relative_to,
49        tag_start,
50        tag_end,
51        exec_state,
52        args,
53    )
54    .await?;
55    Ok(value.into())
56}
57
58#[allow(clippy::too_many_arguments)]
59async fn inner_sweep(
60    sketches: Vec<Sketch>,
61    path: SweepPath,
62    sectional: Option<bool>,
63    tolerance: Option<TyF64>,
64    relative_to: Option<String>,
65    tag_start: Option<TagNode>,
66    tag_end: Option<TagNode>,
67    exec_state: &mut ExecState,
68    args: Args,
69) -> Result<Vec<Solid>, KclError> {
70    let trajectory = match path {
71        SweepPath::Sketch(sketch) => sketch.id.into(),
72        SweepPath::Helix(helix) => helix.value.into(),
73    };
74    let relative_to = match relative_to.as_deref() {
75        Some("sketchPlane") => RelativeTo::SketchPlane,
76        Some("trajectoryCurve") | None => RelativeTo::TrajectoryCurve,
77        Some(_) => {
78            return Err(KclError::Syntax(crate::errors::KclErrorDetails::new(
79                "If you provide relativeTo, it must either be 'sketchPlane' or 'trajectoryCurve'".to_owned(),
80                vec![args.source_range],
81            )))
82        }
83    };
84
85    let mut solids = Vec::new();
86    for sketch in &sketches {
87        let id = exec_state.next_uuid();
88        args.batch_modeling_cmd(
89            id,
90            ModelingCmd::from(mcmd::Sweep {
91                target: sketch.id.into(),
92                trajectory,
93                sectional: sectional.unwrap_or(false),
94                tolerance: LengthUnit(tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE)),
95                relative_to,
96            }),
97        )
98        .await?;
99
100        solids.push(
101            do_post_extrude(
102                sketch,
103                id.into(),
104                TyF64::new(0.0, NumericType::mm()),
105                sectional.unwrap_or(false),
106                &super::extrude::NamedCapTags {
107                    start: tag_start.as_ref(),
108                    end: tag_end.as_ref(),
109                },
110                exec_state,
111                &args,
112                None,
113            )
114            .await?,
115        );
116    }
117
118    // Hide the artifact from the sketch or helix.
119    args.batch_modeling_cmd(
120        exec_state.next_uuid(),
121        ModelingCmd::from(mcmd::ObjectVisible {
122            object_id: trajectory.into(),
123            hidden: true,
124        }),
125    )
126    .await?;
127
128    Ok(solids)
129}