1use anyhow::Result;
4use kcmc::ModelingCmd;
5use kcmc::each_cmd as mcmd;
6use kcmc::length_unit::LengthUnit;
7use kcmc::shared::Angle;
8use kcmc::shared::Opposite;
9use kittycad_modeling_cmds::shared::BodyType;
10use kittycad_modeling_cmds::shared::Point3d;
11use kittycad_modeling_cmds::{self as kcmc};
12
13use super::DEFAULT_TOLERANCE_MM;
14use super::args::TyF64;
15use crate::errors::KclError;
16use crate::errors::KclErrorDetails;
17use crate::execution::ExecState;
18use crate::execution::ExecutorContext;
19use crate::execution::KclValue;
20use crate::execution::ModelingCmdMeta;
21use crate::execution::Sketch;
22use crate::execution::Solid;
23use crate::execution::types::ArrayLen;
24use crate::execution::types::PrimitiveType;
25use crate::execution::types::RuntimeType;
26use crate::parsing::ast::types::TagNode;
27use crate::std::Args;
28use crate::std::args::FromKclValue;
29use crate::std::axis_or_reference::Axis2dOrEdgeReference;
30use crate::std::extrude::build_segment_surface_sketch;
31use crate::std::extrude::do_post_extrude;
32
33extern crate nalgebra_glm as glm;
34
35pub async fn revolve(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
37 let sketch_values: Vec<KclValue> = args.get_unlabeled_kw_arg(
38 "sketches",
39 &RuntimeType::Array(
40 Box::new(RuntimeType::Union(vec![RuntimeType::sketch(), RuntimeType::segment()])),
41 ArrayLen::Minimum(1),
42 ),
43 exec_state,
44 )?;
45 let axis = args.get_kw_arg(
46 "axis",
47 &RuntimeType::Union(vec![
48 RuntimeType::Primitive(PrimitiveType::Edge),
49 RuntimeType::Primitive(PrimitiveType::Axis2d),
50 RuntimeType::segment(),
51 ]),
52 exec_state,
53 )?;
54 let angle: Option<TyF64> = args.get_kw_arg_opt("angle", &RuntimeType::degrees(), exec_state)?;
55 let tolerance: Option<TyF64> = args.get_kw_arg_opt("tolerance", &RuntimeType::length(), exec_state)?;
56 let tag_start = args.get_kw_arg_opt("tagStart", &RuntimeType::tag_decl(), exec_state)?;
57 let tag_end = args.get_kw_arg_opt("tagEnd", &RuntimeType::tag_decl(), exec_state)?;
58 let symmetric = args.get_kw_arg_opt("symmetric", &RuntimeType::bool(), exec_state)?;
59 let bidirectional_angle: Option<TyF64> =
60 args.get_kw_arg_opt("bidirectionalAngle", &RuntimeType::angle(), exec_state)?;
61 let body_type: BodyType = args
62 .get_kw_arg_opt("bodyType", &RuntimeType::string(), exec_state)?
63 .unwrap_or_default();
64 let sketches = coerce_revolve_targets(
65 sketch_values,
66 body_type,
67 tag_start.as_ref(),
68 tag_end.as_ref(),
69 exec_state,
70 &args.ctx,
71 args.source_range,
72 )
73 .await?;
74
75 let value = inner_revolve(
76 sketches,
77 axis,
78 angle.map(|t| t.n),
79 tolerance,
80 tag_start,
81 tag_end,
82 symmetric,
83 bidirectional_angle.map(|t| t.n),
84 body_type,
85 exec_state,
86 args,
87 )
88 .await?;
89 Ok(value.into())
90}
91
92#[allow(clippy::too_many_arguments)]
93async fn inner_revolve(
94 sketches: Vec<Sketch>,
95 axis: Axis2dOrEdgeReference,
96 angle: Option<f64>,
97 tolerance: Option<TyF64>,
98 tag_start: Option<TagNode>,
99 tag_end: Option<TagNode>,
100 symmetric: Option<bool>,
101 bidirectional_angle: Option<f64>,
102 body_type: BodyType,
103 exec_state: &mut ExecState,
104 args: Args,
105) -> Result<Vec<Solid>, KclError> {
106 if let Some(angle) = angle {
107 if !(-360.0..=360.0).contains(&angle) || angle == 0.0 {
111 return Err(KclError::new_semantic(KclErrorDetails::new(
112 format!("Expected angle to be between -360 and 360 and not 0, found `{angle}`"),
113 vec![args.source_range],
114 )));
115 }
116 }
117
118 if let Some(bidirectional_angle) = bidirectional_angle {
119 if !(-360.0..=360.0).contains(&bidirectional_angle) || bidirectional_angle == 0.0 {
123 return Err(KclError::new_semantic(KclErrorDetails::new(
124 format!(
125 "Expected bidirectional angle to be between -360 and 360 and not 0, found `{bidirectional_angle}`"
126 ),
127 vec![args.source_range],
128 )));
129 }
130
131 if let Some(angle) = angle {
132 let ang = angle.signum() * bidirectional_angle + angle;
133 if !(-360.0..=360.0).contains(&ang) {
134 return Err(KclError::new_semantic(KclErrorDetails::new(
135 format!("Combined angle and bidirectional must be between -360 and 360, found '{ang}'"),
136 vec![args.source_range],
137 )));
138 }
139 }
140 }
141
142 if symmetric.unwrap_or(false) && bidirectional_angle.is_some() {
143 return Err(KclError::new_semantic(KclErrorDetails::new(
144 "You cannot give both `symmetric` and `bidirectional` params, you have to choose one or the other"
145 .to_owned(),
146 vec![args.source_range],
147 )));
148 }
149
150 let angle = Angle::from_degrees(angle.unwrap_or(360.0));
151
152 let bidirectional_angle = bidirectional_angle.map(Angle::from_degrees);
153
154 let opposite = match (symmetric, bidirectional_angle) {
155 (Some(true), _) => Opposite::Symmetric,
156 (None, None) => Opposite::None,
157 (Some(false), None) => Opposite::None,
158 (None, Some(angle)) => Opposite::Other(angle),
159 (Some(false), Some(angle)) => Opposite::Other(angle),
160 };
161
162 let mut solids = Vec::new();
163 for sketch in &sketches {
164 let new_solid_id = exec_state.next_uuid();
165 let tolerance = tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE_MM);
166
167 let direction = match &axis {
168 Axis2dOrEdgeReference::Axis { direction, origin } => {
169 exec_state
170 .batch_modeling_cmd(
171 ModelingCmdMeta::from_args_id(exec_state, &args, new_solid_id),
172 ModelingCmd::from(
173 mcmd::Revolve::builder()
174 .angle(angle)
175 .target(sketch.id.into())
176 .axis(Point3d {
177 x: direction[0].to_mm(),
178 y: direction[1].to_mm(),
179 z: 0.0,
180 })
181 .origin(Point3d {
182 x: LengthUnit(origin[0].to_mm()),
183 y: LengthUnit(origin[1].to_mm()),
184 z: LengthUnit(0.0),
185 })
186 .tolerance(LengthUnit(tolerance))
187 .axis_is_2d(true)
188 .opposite(opposite.clone())
189 .body_type(body_type)
190 .build(),
191 ),
192 )
193 .await?;
194 glm::DVec2::new(direction[0].to_mm(), direction[1].to_mm())
195 }
196 Axis2dOrEdgeReference::Edge(edge) => {
197 let edge_id = edge.get_engine_id(exec_state, &args)?;
198 exec_state
199 .batch_modeling_cmd(
200 ModelingCmdMeta::from_args_id(exec_state, &args, new_solid_id),
201 ModelingCmd::from(
202 mcmd::RevolveAboutEdge::builder()
203 .angle(angle)
204 .target(sketch.id.into())
205 .edge_id(edge_id)
206 .tolerance(LengthUnit(tolerance))
207 .opposite(opposite.clone())
208 .body_type(body_type)
209 .build(),
210 ),
211 )
212 .await?;
213 glm::DVec2::new(0.0, 1.0)
215 }
216 };
217
218 let mut edge_id = None;
219 for path in sketch.paths.clone() {
222 if sketch.synthetic_jump_path_ids.contains(&path.get_id()) {
223 continue;
224 }
225
226 if !path.is_straight_line() {
227 edge_id = Some(path.get_id());
228 break;
229 }
230
231 let from = path.get_from();
232 let to = path.get_to();
233
234 let dir = glm::DVec2::new(to[0].n - from[0].n, to[1].n - from[1].n);
235 if glm::are_collinear2d(&dir, &direction, tolerance) {
236 continue;
237 }
238 edge_id = Some(path.get_id());
239 break;
240 }
241
242 solids.push(
243 do_post_extrude(
244 sketch,
245 new_solid_id.into(),
246 false,
247 &super::extrude::NamedCapTags {
248 start: tag_start.as_ref(),
249 end: tag_end.as_ref(),
250 },
251 kittycad_modeling_cmds::shared::ExtrudeMethod::New,
252 exec_state,
253 &args,
254 edge_id,
255 None,
256 body_type,
257 crate::std::extrude::BeingExtruded::Sketch,
258 )
259 .await?,
260 );
261 }
262
263 Ok(solids)
264}
265
266pub async fn coerce_revolve_targets(
267 sketch_values: Vec<KclValue>,
268 body_type: BodyType,
269 tag_start: Option<&TagNode>,
270 tag_end: Option<&TagNode>,
271 exec_state: &mut ExecState,
272 ctx: &ExecutorContext,
273 source_range: crate::SourceRange,
274) -> Result<Vec<Sketch>, KclError> {
275 let mut sketches = Vec::new();
276 let mut segments = Vec::new();
277
278 for value in sketch_values {
279 if let Some(segment) = value.clone().into_segment() {
280 segments.push(segment);
281 continue;
282 }
283
284 let Some(sketch) = Sketch::from_kcl_val(&value) else {
285 return Err(KclError::new_type(KclErrorDetails::new(
286 "Expected sketches or solved sketch segments for revolve.".to_owned(),
287 vec![source_range],
288 )));
289 };
290 sketches.push(sketch);
291 }
292
293 if !segments.is_empty() && !sketches.is_empty() {
294 return Err(KclError::new_semantic(KclErrorDetails::new(
295 "Cannot revolve sketch segments together with sketches in the same call. Use separate `revolve()` calls."
296 .to_owned(),
297 vec![source_range],
298 )));
299 }
300
301 if !segments.is_empty() {
302 if !matches!(body_type, BodyType::Surface) {
303 return Err(KclError::new_semantic(KclErrorDetails::new(
304 "Revolving sketch segments is only supported for surface revolves. Set `bodyType = SURFACE`."
305 .to_owned(),
306 vec![source_range],
307 )));
308 }
309
310 if tag_start.is_some() || tag_end.is_some() {
311 return Err(KclError::new_semantic(KclErrorDetails::new(
312 "`tagStart` and `tagEnd` are not supported when revolving sketch segments. Segment surface revolves do not create start or end caps."
313 .to_owned(),
314 vec![source_range],
315 )));
316 }
317
318 let synthetic_sketch = build_segment_surface_sketch(segments, exec_state, ctx, source_range).await?;
319 return Ok(vec![synthetic_sketch]);
320 }
321
322 Ok(sketches)
323}
324
325#[cfg(test)]
326mod tests {
327 use kittycad_modeling_cmds::units::UnitLength;
328
329 use super::*;
330 use crate::execution::AbstractSegment;
331 use crate::execution::Plane;
332 use crate::execution::Segment;
333 use crate::execution::SegmentKind;
334 use crate::execution::SegmentRepr;
335 use crate::execution::SketchSurface;
336 use crate::execution::types::NumericType;
337 use crate::front::Expr;
338 use crate::front::Number;
339 use crate::front::ObjectId;
340 use crate::front::Point2d;
341 use crate::front::PointCtor;
342 use crate::parsing::ast::types::TagDeclarator;
343 use crate::std::sketch::PlaneData;
344
345 fn point_expr(x: f64, y: f64) -> Point2d<Expr> {
346 Point2d {
347 x: Expr::Var(Number::from((x, UnitLength::Millimeters))),
348 y: Expr::Var(Number::from((y, UnitLength::Millimeters))),
349 }
350 }
351
352 fn segment_value(exec_state: &mut ExecState) -> KclValue {
353 let plane = Plane::from_plane_data_skipping_engine(PlaneData::XY, exec_state).unwrap();
354 let segment = Segment {
355 id: exec_state.next_uuid(),
356 object_id: ObjectId(1),
357 kind: SegmentKind::Point {
358 position: [TyF64::new(0.0, NumericType::mm()), TyF64::new(0.0, NumericType::mm())],
359 ctor: Box::new(PointCtor {
360 position: point_expr(0.0, 0.0),
361 }),
362 freedom: None,
363 },
364 surface: SketchSurface::Plane(Box::new(plane)),
365 sketch_id: exec_state.next_uuid(),
366 sketch: None,
367 tag: None,
368 node_path: None,
369 meta: vec![],
370 };
371 KclValue::Segment {
372 value: Box::new(AbstractSegment {
373 repr: SegmentRepr::Solved {
374 segment: Box::new(segment),
375 },
376 meta: vec![],
377 }),
378 }
379 }
380
381 #[tokio::test(flavor = "multi_thread")]
382 async fn segment_revolve_rejects_cap_tags() {
383 let ctx = ExecutorContext::new_mock(None).await;
384 let mut exec_state = ExecState::new(&ctx);
385 let err = coerce_revolve_targets(
386 vec![segment_value(&mut exec_state)],
387 BodyType::Surface,
388 Some(&TagDeclarator::new("cap_start")),
389 None,
390 &mut exec_state,
391 &ctx,
392 crate::SourceRange::default(),
393 )
394 .await
395 .unwrap_err();
396
397 assert!(
398 err.message()
399 .contains("`tagStart` and `tagEnd` are not supported when revolving sketch segments"),
400 "{err:?}"
401 );
402 ctx.close().await;
403 }
404}