Skip to main content

kcl_lib/std/
loft.rs

1//! Standard library lofts.
2
3use std::num::NonZeroU32;
4
5use anyhow::Result;
6use kcmc::ModelingCmd;
7use kcmc::each_cmd as mcmd;
8use kcmc::length_unit::LengthUnit;
9use kcmc::shared::BodyType;
10use kittycad_modeling_cmds as kcmc;
11
12use super::DEFAULT_TOLERANCE_MM;
13use super::args::TyF64;
14use crate::errors::KclError;
15use crate::errors::KclErrorDetails;
16use crate::execution::ExecState;
17use crate::execution::ExecutorContext;
18use crate::execution::KclValue;
19use crate::execution::ModelingCmdMeta;
20use crate::execution::ProfileClosed;
21use crate::execution::Sketch;
22use crate::execution::Solid;
23use crate::execution::types::ArrayLen;
24use crate::execution::types::RuntimeType;
25use crate::parsing::ast::types::TagNode;
26use crate::std::Args;
27use crate::std::args::FromKclValue;
28use crate::std::extrude::build_segment_surface_sketch;
29use crate::std::extrude::do_post_extrude;
30
31const DEFAULT_V_DEGREE: u32 = 2;
32
33/// Create a 3D surface or solid by interpolating between two or more sketches.
34pub async fn loft(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
35    let sketch_values: Vec<KclValue> = args.get_unlabeled_kw_arg(
36        "sketches",
37        &RuntimeType::Array(
38            Box::new(RuntimeType::Union(vec![RuntimeType::sketch(), RuntimeType::segment()])),
39            ArrayLen::Minimum(2),
40        ),
41        exec_state,
42    )?;
43    let v_degree: NonZeroU32 = args
44        .get_kw_arg_opt("vDegree", &RuntimeType::count(), exec_state)?
45        .unwrap_or(NonZeroU32::new(DEFAULT_V_DEGREE).unwrap());
46    // Attempt to approximate rational curves (such as arcs) using a bezier.
47    // This will remove banding around interpolations between arcs and non-arcs.  It may produce errors in other scenarios
48    // Over time, this field won't be necessary.
49    let bez_approximate_rational = args
50        .get_kw_arg_opt("bezApproximateRational", &RuntimeType::bool(), exec_state)?
51        .unwrap_or(false);
52    // This can be set to override the automatically determined topological base curve, which is usually the first section encountered.
53    let base_curve_index: Option<u32> = args.get_kw_arg_opt("baseCurveIndex", &RuntimeType::count(), exec_state)?;
54    // Tolerance for the loft operation.
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 body_type: Option<BodyType> = args.get_kw_arg_opt("bodyType", &RuntimeType::string(), exec_state)?;
59
60    let sketches = coerce_loft_targets(
61        sketch_values,
62        body_type.unwrap_or_default(),
63        tag_start.as_ref(),
64        tag_end.as_ref(),
65        exec_state,
66        &args.ctx,
67        args.source_range,
68    )
69    .await?;
70    let value = inner_loft(
71        sketches,
72        v_degree,
73        bez_approximate_rational,
74        base_curve_index,
75        tolerance,
76        tag_start,
77        tag_end,
78        body_type,
79        exec_state,
80        args,
81    )
82    .await?;
83    Ok(KclValue::Solid { value })
84}
85
86async fn coerce_loft_targets(
87    sketch_values: Vec<KclValue>,
88    body_type: BodyType,
89    tag_start: Option<&TagNode>,
90    tag_end: Option<&TagNode>,
91    exec_state: &mut ExecState,
92    ctx: &ExecutorContext,
93    source_range: crate::SourceRange,
94) -> Result<Vec<Sketch>, KclError> {
95    let mut sketches = Vec::new();
96    let mut segments = Vec::new();
97
98    for value in sketch_values {
99        if let Some(segment) = value.clone().into_segment() {
100            segments.push(segment);
101            continue;
102        }
103
104        let Some(sketch) = Sketch::from_kcl_val(&value) else {
105            return Err(KclError::new_type(KclErrorDetails::new(
106                "Expected sketches or solved sketch segments for loft.".to_owned(),
107                vec![source_range],
108            )));
109        };
110        sketches.push(sketch);
111    }
112
113    if !segments.is_empty() && !sketches.is_empty() {
114        return Err(KclError::new_semantic(KclErrorDetails::new(
115            "Cannot loft sketch segments together with sketches in the same call. Use separate `loft()` calls."
116                .to_owned(),
117            vec![source_range],
118        )));
119    }
120
121    if !segments.is_empty() {
122        if !matches!(body_type, BodyType::Surface) {
123            return Err(KclError::new_semantic(KclErrorDetails::new(
124                "Lofting sketch segments is only supported for surface lofts. Set `bodyType = SURFACE`.".to_owned(),
125                vec![source_range],
126            )));
127        }
128
129        if tag_start.is_some() || tag_end.is_some() {
130            return Err(KclError::new_semantic(KclErrorDetails::new(
131                "`tagStart` and `tagEnd` are not supported when lofting sketch segments. Segment surface lofts do not create start or end caps."
132                    .to_owned(),
133                vec![source_range],
134            )));
135        }
136
137        let mut loft_sections = Vec::with_capacity(segments.len());
138        for segment in segments {
139            loft_sections.push(build_segment_surface_sketch(vec![segment], exec_state, ctx, source_range).await?);
140        }
141        return Ok(loft_sections);
142    }
143
144    Ok(sketches)
145}
146
147#[allow(clippy::too_many_arguments)]
148async fn inner_loft(
149    sketches: Vec<Sketch>,
150    v_degree: NonZeroU32,
151    bez_approximate_rational: bool,
152    base_curve_index: Option<u32>,
153    tolerance: Option<TyF64>,
154    tag_start: Option<TagNode>,
155    tag_end: Option<TagNode>,
156    body_type: Option<BodyType>,
157    exec_state: &mut ExecState,
158    args: Args,
159) -> Result<Box<Solid>, KclError> {
160    let body_type = body_type.unwrap_or_default();
161    if matches!(body_type, BodyType::Solid) && sketches.iter().any(|sk| matches!(sk.is_closed, ProfileClosed::No)) {
162        return Err(KclError::new_semantic(KclErrorDetails::new(
163            "Cannot solid loft an open profile. Either close the profile, or use a surface loft.".to_owned(),
164            vec![args.source_range],
165        )));
166    }
167
168    // Make sure we have at least two sketches.
169    if sketches.len() < 2 {
170        return Err(KclError::new_semantic(KclErrorDetails::new(
171            format!(
172                "Loft requires at least two sketches, but only {} were provided.",
173                sketches.len()
174            ),
175            vec![args.source_range],
176        )));
177    }
178
179    let id = exec_state.next_uuid();
180    exec_state
181        .batch_modeling_cmd(
182            ModelingCmdMeta::from_args_id(exec_state, &args, id),
183            ModelingCmd::from(if let Some(base_curve_index) = base_curve_index {
184                mcmd::Loft::builder()
185                    .section_ids(sketches.iter().map(|group| group.id).collect())
186                    .bez_approximate_rational(bez_approximate_rational)
187                    .tolerance(LengthUnit(
188                        tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE_MM),
189                    ))
190                    .v_degree(v_degree)
191                    .body_type(body_type)
192                    .base_curve_index(base_curve_index)
193                    .build()
194            } else {
195                mcmd::Loft::builder()
196                    .section_ids(sketches.iter().map(|group| group.id).collect())
197                    .bez_approximate_rational(bez_approximate_rational)
198                    .tolerance(LengthUnit(
199                        tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE_MM),
200                    ))
201                    .v_degree(v_degree)
202                    .body_type(body_type)
203                    .build()
204            }),
205        )
206        .await?;
207
208    // Using the first sketch as the base curve, idk we might want to change this later.
209    let mut sketch = sketches[0].clone();
210    // Override its id with the loft id so we can get its faces later
211    sketch.id = id;
212    Ok(Box::new(
213        do_post_extrude(
214            &sketch,
215            id.into(),
216            false,
217            &super::extrude::NamedCapTags {
218                start: tag_start.as_ref(),
219                end: tag_end.as_ref(),
220            },
221            kittycad_modeling_cmds::shared::ExtrudeMethod::New,
222            exec_state,
223            &args,
224            None,
225            None,
226            body_type,
227            crate::std::extrude::BeingExtruded::Sketch,
228        )
229        .await?,
230    ))
231}
232
233#[cfg(test)]
234mod tests {
235    use kcl_api::UnitLength;
236
237    use super::*;
238    use crate::execution::AbstractSegment;
239    use crate::execution::Plane;
240    use crate::execution::Segment;
241    use crate::execution::SegmentKind;
242    use crate::execution::SegmentRepr;
243    use crate::execution::SketchSurface;
244    use crate::execution::types::NumericType;
245    use crate::execution::types::NumericTypeExt;
246    use crate::front::Expr;
247    use crate::front::LineCtor;
248    use crate::front::Number;
249    use crate::front::ObjectId;
250    use crate::front::Point2d;
251    use crate::parsing::ast::types::TagDeclarator;
252    use crate::std::sketch::PlaneData;
253
254    fn point_expr(x: f64, y: f64) -> Point2d<Expr> {
255        Point2d {
256            x: Expr::Var(Number::from((x, UnitLength::Millimeters))),
257            y: Expr::Var(Number::from((y, UnitLength::Millimeters))),
258        }
259    }
260
261    fn line_segment_value(exec_state: &mut ExecState, plane_data: PlaneData, object_id_seed: usize) -> KclValue {
262        let plane = Plane::from_plane_data_skipping_engine(plane_data, exec_state).unwrap();
263        let start = [TyF64::new(-2.0, NumericType::mm()), TyF64::new(0.0, NumericType::mm())];
264        let end = [TyF64::new(2.0, NumericType::mm()), TyF64::new(0.0, NumericType::mm())];
265        let segment = Segment {
266            id: exec_state.next_uuid(),
267            object_id: ObjectId(object_id_seed),
268            kind: SegmentKind::Line {
269                start,
270                end,
271                ctor: Box::new(LineCtor {
272                    start: point_expr(-2.0, 0.0),
273                    end: point_expr(2.0, 0.0),
274                    construction: None,
275                }),
276                start_object_id: ObjectId(object_id_seed + 1),
277                end_object_id: ObjectId(object_id_seed + 2),
278                start_freedom: None,
279                end_freedom: None,
280                construction: false,
281            },
282            surface: SketchSurface::Plane(Box::new(plane)),
283            sketch_id: exec_state.next_uuid(),
284            sketch: None,
285            tag: None,
286            meta: vec![],
287            node_path: None,
288        };
289        KclValue::Segment {
290            value: Box::new(AbstractSegment {
291                repr: SegmentRepr::Solved {
292                    segment: Box::new(segment),
293                },
294                meta: vec![],
295            }),
296        }
297    }
298
299    #[tokio::test(flavor = "multi_thread")]
300    async fn segment_loft_supports_sections_from_different_sketches() {
301        let ctx = ExecutorContext::new_mock(None).await;
302        let mut exec_state = ExecState::new(&ctx);
303        let sketches = coerce_loft_targets(
304            vec![
305                line_segment_value(&mut exec_state, PlaneData::XY, 1),
306                line_segment_value(&mut exec_state, PlaneData::NegXY, 10),
307                line_segment_value(&mut exec_state, PlaneData::XZ, 20),
308            ],
309            BodyType::Surface,
310            None,
311            None,
312            &mut exec_state,
313            &ctx,
314            crate::SourceRange::default(),
315        )
316        .await
317        .unwrap();
318
319        assert_eq!(sketches.len(), 3);
320        assert!(sketches.iter().all(|sketch| sketch.paths.len() == 1));
321        assert_ne!(sketches[0].id, sketches[1].id);
322        assert_ne!(sketches[1].id, sketches[2].id);
323        ctx.close().await;
324    }
325
326    #[tokio::test(flavor = "multi_thread")]
327    async fn segment_loft_rejects_cap_tags() {
328        let ctx = ExecutorContext::new_mock(None).await;
329        let mut exec_state = ExecState::new(&ctx);
330        let err = coerce_loft_targets(
331            vec![line_segment_value(&mut exec_state, PlaneData::XY, 1)],
332            BodyType::Surface,
333            Some(&TagDeclarator::new("cap_start")),
334            None,
335            &mut exec_state,
336            &ctx,
337            crate::SourceRange::default(),
338        )
339        .await
340        .unwrap_err();
341
342        assert!(
343            err.message()
344                .contains("`tagStart` and `tagEnd` are not supported when lofting sketch segments"),
345            "{err:?}"
346        );
347        ctx.close().await;
348    }
349}