1use anyhow::Result;
4use kcmc::{ModelingCmd, each_cmd as mcmd, length_unit::LengthUnit};
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(&args, id),
88 ModelingCmd::from(mcmd::Sweep {
89 target: sketch.id.into(),
90 trajectory,
91 sectional: sectional.unwrap_or(false),
92 tolerance: LengthUnit(tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE_MM)),
93 relative_to,
94 }),
95 )
96 .await?;
97
98 solids.push(
99 do_post_extrude(
100 sketch,
101 id.into(),
102 sectional.unwrap_or(false),
103 &super::extrude::NamedCapTags {
104 start: tag_start.as_ref(),
105 end: tag_end.as_ref(),
106 },
107 kittycad_modeling_cmds::shared::ExtrudeMethod::Merge,
108 exec_state,
109 &args,
110 None,
111 )
112 .await?,
113 );
114 }
115
116 exec_state
118 .batch_modeling_cmd(
119 (&args).into(),
120 ModelingCmd::from(mcmd::ObjectVisible {
121 object_id: trajectory.into(),
122 hidden: true,
123 }),
124 )
125 .await?;
126
127 Ok(solids)
128}