1use 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#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
22#[ts(export)]
23#[serde(untagged)]
24#[allow(clippy::large_enum_variant)]
25pub enum SweepPath {
26 Sketch(Sketch),
27 Helix(Box<Helix>),
28}
29
30pub async fn sweep(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
32 let sketches = args.get_unlabeled_kw_arg_typed("sketches", &RuntimeType::sketches(), exec_state)?;
33 let path: SweepPath = args.get_kw_arg_typed(
34 "path",
35 &RuntimeType::Union(vec![RuntimeType::sketch(), RuntimeType::helix()]),
36 exec_state,
37 )?;
38 let sectional = args.get_kw_arg_opt_typed("sectional", &RuntimeType::bool(), exec_state)?;
39 let tolerance: Option<TyF64> = args.get_kw_arg_opt_typed("tolerance", &RuntimeType::length(), exec_state)?;
40 let relative_to: Option<String> = args.get_kw_arg_opt_typed("relativeTo", &RuntimeType::string(), exec_state)?;
41 let tag_start = args.get_kw_arg_opt("tagStart")?;
42 let tag_end = args.get_kw_arg_opt("tagEnd")?;
43
44 let value = inner_sweep(
45 sketches,
46 path,
47 sectional,
48 tolerance,
49 relative_to,
50 tag_start,
51 tag_end,
52 exec_state,
53 args,
54 )
55 .await?;
56 Ok(value.into())
57}
58
59#[allow(clippy::too_many_arguments)]
60async fn inner_sweep(
61 sketches: Vec<Sketch>,
62 path: SweepPath,
63 sectional: Option<bool>,
64 tolerance: Option<TyF64>,
65 relative_to: Option<String>,
66 tag_start: Option<TagNode>,
67 tag_end: Option<TagNode>,
68 exec_state: &mut ExecState,
69 args: Args,
70) -> Result<Vec<Solid>, KclError> {
71 let trajectory = match path {
72 SweepPath::Sketch(sketch) => sketch.id.into(),
73 SweepPath::Helix(helix) => helix.value.into(),
74 };
75 let relative_to = match relative_to.as_deref() {
76 Some("sketchPlane") => RelativeTo::SketchPlane,
77 Some("trajectoryCurve") | None => RelativeTo::TrajectoryCurve,
78 Some(_) => {
79 return Err(KclError::new_syntax(crate::errors::KclErrorDetails::new(
80 "If you provide relativeTo, it must either be 'sketchPlane' or 'trajectoryCurve'".to_owned(),
81 vec![args.source_range],
82 )))
83 }
84 };
85
86 let mut solids = Vec::new();
87 for sketch in &sketches {
88 let id = exec_state.next_uuid();
89 args.batch_modeling_cmd(
90 id,
91 ModelingCmd::from(mcmd::Sweep {
92 target: sketch.id.into(),
93 trajectory,
94 sectional: sectional.unwrap_or(false),
95 tolerance: LengthUnit(tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE)),
96 relative_to,
97 }),
98 )
99 .await?;
100
101 solids.push(
102 do_post_extrude(
103 sketch,
104 id.into(),
105 TyF64::new(0.0, NumericType::mm()),
106 sectional.unwrap_or(false),
107 &super::extrude::NamedCapTags {
108 start: tag_start.as_ref(),
109 end: tag_end.as_ref(),
110 },
111 exec_state,
112 &args,
113 None,
114 )
115 .await?,
116 );
117 }
118
119 args.batch_modeling_cmd(
121 exec_state.next_uuid(),
122 ModelingCmd::from(mcmd::ObjectVisible {
123 object_id: trajectory.into(),
124 hidden: true,
125 }),
126 )
127 .await?;
128
129 Ok(solids)
130}