Skip to main content

kcl_lib/std/
revolve.rs

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