1use anyhow::Result;
4use kcmc::{ModelingCmd, each_cmd as mcmd, length_unit::LengthUnit, shared::BodyType};
5use kittycad_modeling_cmds::{self as kcmc, shared::RelativeTo};
6use serde::Serialize;
7
8use super::{DEFAULT_TOLERANCE_MM, args::TyF64};
9use crate::{
10 errors::KclError,
11 execution::{ExecState, Helix, KclValue, ModelingCmdMeta, Sketch, Solid, types::RuntimeType},
12 parsing::ast::types::TagNode,
13 std::{Args, extrude::do_post_extrude},
14};
15
16#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
18#[ts(export)]
19#[serde(untagged)]
20#[allow(clippy::large_enum_variant)]
21pub enum SweepPath {
22 Sketch(Sketch),
23 Helix(Box<Helix>),
24}
25
26pub async fn sweep(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
28 let sketches = args.get_unlabeled_kw_arg("sketches", &RuntimeType::sketches(), exec_state)?;
29 let path: SweepPath = args.get_kw_arg(
30 "path",
31 &RuntimeType::Union(vec![RuntimeType::sketch(), RuntimeType::helix()]),
32 exec_state,
33 )?;
34 let sectional = args.get_kw_arg_opt("sectional", &RuntimeType::bool(), exec_state)?;
35 let tolerance: Option<TyF64> = args.get_kw_arg_opt("tolerance", &RuntimeType::length(), exec_state)?;
36 let relative_to: Option<String> = args.get_kw_arg_opt("relativeTo", &RuntimeType::string(), exec_state)?;
37 let tag_start = args.get_kw_arg_opt("tagStart", &RuntimeType::tag_decl(), exec_state)?;
38 let tag_end = args.get_kw_arg_opt("tagEnd", &RuntimeType::tag_decl(), exec_state)?;
39
40 let value = inner_sweep(
41 sketches,
42 path,
43 sectional,
44 tolerance,
45 relative_to,
46 tag_start,
47 tag_end,
48 exec_state,
49 args,
50 )
51 .await?;
52 Ok(value.into())
53}
54
55#[allow(clippy::too_many_arguments)]
56async fn inner_sweep(
57 sketches: Vec<Sketch>,
58 path: SweepPath,
59 sectional: Option<bool>,
60 tolerance: Option<TyF64>,
61 relative_to: Option<String>,
62 tag_start: Option<TagNode>,
63 tag_end: Option<TagNode>,
64 exec_state: &mut ExecState,
65 args: Args,
66) -> Result<Vec<Solid>, KclError> {
67 let trajectory = match path {
68 SweepPath::Sketch(sketch) => sketch.id.into(),
69 SweepPath::Helix(helix) => helix.value.into(),
70 };
71 let relative_to = match relative_to.as_deref() {
72 Some("sketchPlane") => RelativeTo::SketchPlane,
73 Some("trajectoryCurve") | None => RelativeTo::TrajectoryCurve,
74 Some(_) => {
75 return Err(KclError::new_syntax(crate::errors::KclErrorDetails::new(
76 "If you provide relativeTo, it must either be 'sketchPlane' or 'trajectoryCurve'".to_owned(),
77 vec![args.source_range],
78 )));
79 }
80 };
81
82 let mut solids = Vec::new();
83 for sketch in &sketches {
84 let id = exec_state.next_uuid();
85 exec_state
86 .batch_modeling_cmd(
87 ModelingCmdMeta::from_args_id(exec_state, &args, id),
88 ModelingCmd::from(
89 mcmd::Sweep::builder()
90 .target(sketch.id.into())
91 .trajectory(trajectory)
92 .sectional(sectional.unwrap_or(false))
93 .tolerance(LengthUnit(
94 tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE_MM),
95 ))
96 .relative_to(relative_to)
97 .build(),
98 ),
99 )
100 .await?;
101
102 solids.push(
103 do_post_extrude(
104 sketch,
105 id.into(),
106 sectional.unwrap_or(false),
107 &super::extrude::NamedCapTags {
108 start: tag_start.as_ref(),
109 end: tag_end.as_ref(),
110 },
111 kittycad_modeling_cmds::shared::ExtrudeMethod::Merge,
112 exec_state,
113 &args,
114 None,
115 None,
116 BodyType::Solid, )
118 .await?,
119 );
120 }
121
122 exec_state
124 .batch_modeling_cmd(
125 ModelingCmdMeta::from_args(exec_state, &args),
126 ModelingCmd::from(
127 mcmd::ObjectVisible::builder()
128 .object_id(trajectory.into())
129 .hidden(true)
130 .build(),
131 ),
132 )
133 .await?;
134
135 Ok(solids)
136}