1use anyhow::Result;
4use kcmc::{each_cmd as mcmd, length_unit::LengthUnit, shared::Angle, ModelingCmd};
5use kittycad_modeling_cmds::{self as kcmc, shared::Point3d};
6
7use super::args::TyF64;
8use crate::{
9 errors::KclError,
10 execution::{
11 types::{PrimitiveType, RuntimeType},
12 ExecState, Helix as HelixValue, KclValue, Solid,
13 },
14 std::{axis_or_reference::Axis3dOrEdgeReference, Args},
15};
16
17pub async fn helix(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
19 let angle_start: TyF64 = args.get_kw_arg_typed("angleStart", &RuntimeType::degrees(), exec_state)?;
20 let revolutions: TyF64 = args.get_kw_arg_typed("revolutions", &RuntimeType::count(), exec_state)?;
21 let ccw = args.get_kw_arg_opt("ccw")?;
22 let radius: Option<TyF64> = args.get_kw_arg_opt_typed("radius", &RuntimeType::length(), exec_state)?;
23 let axis: Option<Axis3dOrEdgeReference> = args.get_kw_arg_opt_typed(
24 "axis",
25 &RuntimeType::Union(vec![
26 RuntimeType::Primitive(PrimitiveType::Edge),
27 RuntimeType::Primitive(PrimitiveType::Axis3d),
28 ]),
29 exec_state,
30 )?;
31 let length: Option<TyF64> = args.get_kw_arg_opt_typed("length", &RuntimeType::length(), exec_state)?;
32 let cylinder = args.get_kw_arg_opt_typed("cylinder", &RuntimeType::solid(), exec_state)?;
33
34 if radius.is_none() && cylinder.is_none() {
36 return Err(KclError::Semantic(crate::errors::KclErrorDetails {
37 message: "Radius is required when creating a helix without a cylinder.".to_string(),
38 source_ranges: vec![args.source_range],
39 }));
40 }
41
42 if radius.is_some() && cylinder.is_some() {
44 return Err(KclError::Semantic(crate::errors::KclErrorDetails {
45 message: "Radius is not allowed when creating a helix with a cylinder.".to_string(),
46 source_ranges: vec![args.source_range],
47 }));
48 }
49
50 if axis.is_none() && cylinder.is_none() {
52 return Err(KclError::Semantic(crate::errors::KclErrorDetails {
53 message: "Axis is required when creating a helix without a cylinder.".to_string(),
54 source_ranges: vec![args.source_range],
55 }));
56 }
57
58 if axis.is_some() && cylinder.is_some() {
60 return Err(KclError::Semantic(crate::errors::KclErrorDetails {
61 message: "Axis is not allowed when creating a helix with a cylinder.".to_string(),
62 source_ranges: vec![args.source_range],
63 }));
64 }
65
66 if radius.is_none() && axis.is_some() {
68 return Err(KclError::Semantic(crate::errors::KclErrorDetails {
69 message: "Radius is required when creating a helix around an axis.".to_string(),
70 source_ranges: vec![args.source_range],
71 }));
72 }
73
74 if axis.is_none() && radius.is_some() {
76 return Err(KclError::Semantic(crate::errors::KclErrorDetails {
77 message: "Axis is required when creating a helix around an axis.".to_string(),
78 source_ranges: vec![args.source_range],
79 }));
80 }
81
82 let value = inner_helix(
83 revolutions.n,
84 angle_start.n,
85 ccw,
86 radius,
87 axis,
88 length,
89 cylinder,
90 exec_state,
91 args,
92 )
93 .await?;
94 Ok(KclValue::Helix { value })
95}
96
97#[allow(clippy::too_many_arguments)]
98async fn inner_helix(
99 revolutions: f64,
100 angle_start: f64,
101 ccw: Option<bool>,
102 radius: Option<TyF64>,
103 axis: Option<Axis3dOrEdgeReference>,
104 length: Option<TyF64>,
105 cylinder: Option<Solid>,
106 exec_state: &mut ExecState,
107 args: Args,
108) -> Result<Box<HelixValue>, KclError> {
109 let id = exec_state.next_uuid();
110
111 let helix_result = Box::new(HelixValue {
112 value: id,
113 #[cfg(feature = "artifact-graph")]
114 artifact_id: id.into(),
115 revolutions,
116 angle_start,
117 cylinder_id: cylinder.as_ref().map(|c| c.id),
118 ccw: ccw.unwrap_or(false),
119 units: exec_state.length_unit(),
120 meta: vec![args.source_range.into()],
121 });
122
123 if args.ctx.no_engine_commands().await {
124 return Ok(helix_result);
125 }
126
127 if let Some(cylinder) = cylinder {
128 args.batch_modeling_cmd(
129 id,
130 ModelingCmd::from(mcmd::EntityMakeHelix {
131 cylinder_id: cylinder.id,
132 is_clockwise: !helix_result.ccw,
133 length: LengthUnit(length.as_ref().map(|t| t.to_mm()).unwrap_or(cylinder.height_in_mm())),
134 revolutions,
135 start_angle: Angle::from_degrees(angle_start),
136 }),
137 )
138 .await?;
139 } else if let (Some(axis), Some(radius)) = (axis, radius) {
140 match axis {
141 Axis3dOrEdgeReference::Axis { direction, origin } => {
142 let Some(length) = length else {
144 return Err(KclError::Semantic(crate::errors::KclErrorDetails {
145 message: "Length is required when creating a helix around an axis.".to_string(),
146 source_ranges: vec![args.source_range],
147 }));
148 };
149
150 args.batch_modeling_cmd(
151 id,
152 ModelingCmd::from(mcmd::EntityMakeHelixFromParams {
153 radius: LengthUnit(radius.to_mm()),
154 is_clockwise: !helix_result.ccw,
155 length: LengthUnit(length.to_mm()),
156 revolutions,
157 start_angle: Angle::from_degrees(angle_start),
158 axis: Point3d {
159 x: direction[0].to_mm(),
160 y: direction[1].to_mm(),
161 z: direction[2].to_mm(),
162 },
163 center: Point3d {
164 x: LengthUnit(origin[0].to_mm()),
165 y: LengthUnit(origin[1].to_mm()),
166 z: LengthUnit(origin[2].to_mm()),
167 },
168 }),
169 )
170 .await?;
171 }
172 Axis3dOrEdgeReference::Edge(edge) => {
173 let edge_id = edge.get_engine_id(exec_state, &args)?;
174
175 args.batch_modeling_cmd(
176 id,
177 ModelingCmd::from(mcmd::EntityMakeHelixFromEdge {
178 radius: LengthUnit(radius.to_mm()),
179 is_clockwise: !helix_result.ccw,
180 length: length.map(|t| LengthUnit(t.to_mm())),
181 revolutions,
182 start_angle: Angle::from_degrees(angle_start),
183 edge_id,
184 }),
185 )
186 .await?;
187 }
188 };
189 }
190
191 Ok(helix_result)
192}