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, KclErrorDetails},
11 execution::{ExecState, Helix, KclValue, ModelingCmdMeta, ProfileClosed, 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 let body_type: Option<BodyType> = args.get_kw_arg_opt("bodyType", &RuntimeType::string(), exec_state)?;
40
41 let value = inner_sweep(
42 sketches,
43 path,
44 sectional,
45 tolerance,
46 relative_to,
47 tag_start,
48 tag_end,
49 body_type,
50 exec_state,
51 args,
52 )
53 .await?;
54 Ok(value.into())
55}
56
57#[allow(clippy::too_many_arguments)]
58async fn inner_sweep(
59 sketches: Vec<Sketch>,
60 path: SweepPath,
61 sectional: Option<bool>,
62 tolerance: Option<TyF64>,
63 relative_to: Option<String>,
64 tag_start: Option<TagNode>,
65 tag_end: Option<TagNode>,
66 body_type: Option<BodyType>,
67 exec_state: &mut ExecState,
68 args: Args,
69) -> Result<Vec<Solid>, KclError> {
70 let body_type = body_type.unwrap_or_default();
71 if matches!(body_type, BodyType::Solid) && sketches.iter().any(|sk| matches!(sk.is_closed, ProfileClosed::No)) {
72 return Err(KclError::new_semantic(KclErrorDetails::new(
73 "Cannot solid sweep an open profile. Either close the profile, or use a surface sweep.".to_owned(),
74 vec![args.source_range],
75 )));
76 }
77
78 let trajectory = match path {
79 SweepPath::Sketch(sketch) => sketch.id.into(),
80 SweepPath::Helix(helix) => helix.value.into(),
81 };
82 let relative_to = match relative_to.as_deref() {
83 Some("sketchPlane") => RelativeTo::SketchPlane,
84 Some("trajectoryCurve") | None => RelativeTo::TrajectoryCurve,
85 Some(_) => {
86 return Err(KclError::new_syntax(crate::errors::KclErrorDetails::new(
87 "If you provide relativeTo, it must either be 'sketchPlane' or 'trajectoryCurve'".to_owned(),
88 vec![args.source_range],
89 )));
90 }
91 };
92
93 let mut solids = Vec::new();
94 for sketch in &sketches {
95 let id = exec_state.next_uuid();
96 exec_state
97 .batch_modeling_cmd(
98 ModelingCmdMeta::from_args_id(exec_state, &args, id),
99 ModelingCmd::from(
100 mcmd::Sweep::builder()
101 .target(sketch.id.into())
102 .trajectory(trajectory)
103 .sectional(sectional.unwrap_or(false))
104 .tolerance(LengthUnit(
105 tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE_MM),
106 ))
107 .relative_to(relative_to)
108 .body_type(body_type)
109 .build(),
110 ),
111 )
112 .await?;
113
114 solids.push(
115 do_post_extrude(
116 sketch,
117 id.into(),
118 sectional.unwrap_or(false),
119 &super::extrude::NamedCapTags {
120 start: tag_start.as_ref(),
121 end: tag_end.as_ref(),
122 },
123 kittycad_modeling_cmds::shared::ExtrudeMethod::Merge,
124 exec_state,
125 &args,
126 None,
127 None,
128 body_type,
129 crate::std::extrude::BeingExtruded::Sketch,
130 )
131 .await?,
132 );
133 }
134
135 exec_state
137 .batch_modeling_cmd(
138 ModelingCmdMeta::from_args(exec_state, &args),
139 ModelingCmd::from(
140 mcmd::ObjectVisible::builder()
141 .object_id(trajectory.into())
142 .hidden(true)
143 .build(),
144 ),
145 )
146 .await?;
147
148 Ok(solids)
149}