kcl_lib/std/
sketch.rs

1//! Functions related to sketching.
2
3use std::f64;
4
5use anyhow::Result;
6use indexmap::IndexMap;
7use kcmc::shared::Point2d as KPoint2d; // Point2d is already defined in this pkg, to impl ts_rs traits.
8use kcmc::shared::Point3d as KPoint3d; // Point3d is already defined in this pkg, to impl ts_rs traits.
9use kcmc::{ModelingCmd, each_cmd as mcmd, length_unit::LengthUnit, shared::Angle, websocket::ModelingCmdReq};
10use kittycad_modeling_cmds as kcmc;
11use kittycad_modeling_cmds::{shared::PathSegment, units::UnitLength};
12use parse_display::{Display, FromStr};
13use serde::{Deserialize, Serialize};
14
15use super::{
16    shapes::{get_radius, get_radius_labelled},
17    utils::{untype_array, untype_point},
18};
19#[cfg(feature = "artifact-graph")]
20use crate::execution::{Artifact, ArtifactId, CodeRef, StartSketchOnFace, StartSketchOnPlane};
21use crate::{
22    errors::{KclError, KclErrorDetails},
23    execution::{
24        BasePath, ExecState, Face, GeoMeta, KclValue, ModelingCmdMeta, Path, Plane, PlaneInfo, Point2d, Point3d,
25        Sketch, SketchSurface, Solid, TagEngineInfo, TagIdentifier, annotations,
26        types::{ArrayLen, NumericType, PrimitiveType, RuntimeType},
27    },
28    parsing::ast::types::TagNode,
29    std::{
30        args::{Args, TyF64},
31        axis_or_reference::Axis2dOrEdgeReference,
32        planes::inner_plane_of,
33        utils::{
34            TangentialArcInfoInput, arc_center_and_end, get_tangential_arc_to_info, get_x_component, get_y_component,
35            intersection_with_parallel_line, point_to_len_unit, point_to_mm, untyped_point_to_mm,
36        },
37    },
38};
39
40/// A tag for a face.
41#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS)]
42#[ts(export)]
43#[serde(rename_all = "snake_case", untagged)]
44pub enum FaceTag {
45    StartOrEnd(StartOrEnd),
46    /// A tag for the face.
47    Tag(Box<TagIdentifier>),
48}
49
50impl std::fmt::Display for FaceTag {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        match self {
53            FaceTag::Tag(t) => write!(f, "{t}"),
54            FaceTag::StartOrEnd(StartOrEnd::Start) => write!(f, "start"),
55            FaceTag::StartOrEnd(StartOrEnd::End) => write!(f, "end"),
56        }
57    }
58}
59
60impl FaceTag {
61    /// Get the face id from the tag.
62    pub async fn get_face_id(
63        &self,
64        solid: &Solid,
65        exec_state: &mut ExecState,
66        args: &Args,
67        must_be_planar: bool,
68    ) -> Result<uuid::Uuid, KclError> {
69        match self {
70            FaceTag::Tag(t) => args.get_adjacent_face_to_tag(exec_state, t, must_be_planar).await,
71            FaceTag::StartOrEnd(StartOrEnd::Start) => solid.start_cap_id.ok_or_else(|| {
72                KclError::new_type(KclErrorDetails::new(
73                    "Expected a start face".to_string(),
74                    vec![args.source_range],
75                ))
76            }),
77            FaceTag::StartOrEnd(StartOrEnd::End) => solid.end_cap_id.ok_or_else(|| {
78                KclError::new_type(KclErrorDetails::new(
79                    "Expected an end face".to_string(),
80                    vec![args.source_range],
81                ))
82            }),
83        }
84    }
85
86    pub async fn get_face_id_from_tag(
87        &self,
88        exec_state: &mut ExecState,
89        args: &Args,
90        must_be_planar: bool,
91    ) -> Result<uuid::Uuid, KclError> {
92        match self {
93            FaceTag::Tag(t) => args.get_adjacent_face_to_tag(exec_state, t, must_be_planar).await,
94            _ => Err(KclError::new_type(KclErrorDetails::new(
95                "Could not find the face corresponding to this tag".to_string(),
96                vec![args.source_range],
97            ))),
98        }
99    }
100}
101
102#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, FromStr, Display)]
103#[ts(export)]
104#[serde(rename_all = "snake_case")]
105#[display(style = "snake_case")]
106pub enum StartOrEnd {
107    /// The start face as in before you extruded. This could also be known as the bottom
108    /// face. But we do not call it bottom because it would be the top face if you
109    /// extruded it in the opposite direction or flipped the camera.
110    #[serde(rename = "start", alias = "START")]
111    Start,
112    /// The end face after you extruded. This could also be known as the top
113    /// face. But we do not call it top because it would be the bottom face if you
114    /// extruded it in the opposite direction or flipped the camera.
115    #[serde(rename = "end", alias = "END")]
116    End,
117}
118
119pub const NEW_TAG_KW: &str = "tag";
120
121pub async fn involute_circular(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
122    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::sketch(), exec_state)?;
123
124    let start_radius: Option<TyF64> = args.get_kw_arg_opt("startRadius", &RuntimeType::length(), exec_state)?;
125    let end_radius: Option<TyF64> = args.get_kw_arg_opt("endRadius", &RuntimeType::length(), exec_state)?;
126    let start_diameter: Option<TyF64> = args.get_kw_arg_opt("startDiameter", &RuntimeType::length(), exec_state)?;
127    let end_diameter: Option<TyF64> = args.get_kw_arg_opt("endDiameter", &RuntimeType::length(), exec_state)?;
128    let angle: TyF64 = args.get_kw_arg("angle", &RuntimeType::angle(), exec_state)?;
129    let reverse = args.get_kw_arg_opt("reverse", &RuntimeType::bool(), exec_state)?;
130    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
131    let new_sketch = inner_involute_circular(
132        sketch,
133        start_radius,
134        end_radius,
135        start_diameter,
136        end_diameter,
137        angle,
138        reverse,
139        tag,
140        exec_state,
141        args,
142    )
143    .await?;
144    Ok(KclValue::Sketch {
145        value: Box::new(new_sketch),
146    })
147}
148
149fn involute_curve(radius: f64, angle: f64) -> (f64, f64) {
150    (
151        radius * (libm::cos(angle) + angle * libm::sin(angle)),
152        radius * (libm::sin(angle) - angle * libm::cos(angle)),
153    )
154}
155
156#[allow(clippy::too_many_arguments)]
157async fn inner_involute_circular(
158    sketch: Sketch,
159    start_radius: Option<TyF64>,
160    end_radius: Option<TyF64>,
161    start_diameter: Option<TyF64>,
162    end_diameter: Option<TyF64>,
163    angle: TyF64,
164    reverse: Option<bool>,
165    tag: Option<TagNode>,
166    exec_state: &mut ExecState,
167    args: Args,
168) -> Result<Sketch, KclError> {
169    let id = exec_state.next_uuid();
170    let angle_deg = angle.to_degrees(exec_state, args.source_range);
171    let angle_rad = angle.to_radians(exec_state, args.source_range);
172
173    let longer_args_dot_source_range = args.source_range;
174    let start_radius = get_radius_labelled(
175        start_radius,
176        start_diameter,
177        args.source_range,
178        "startRadius",
179        "startDiameter",
180    )?;
181    let end_radius = get_radius_labelled(
182        end_radius,
183        end_diameter,
184        longer_args_dot_source_range,
185        "endRadius",
186        "endDiameter",
187    )?;
188
189    exec_state
190        .batch_modeling_cmd(
191            ModelingCmdMeta::from_args_id(&args, id),
192            ModelingCmd::from(mcmd::ExtendPath {
193                label: Default::default(),
194                path: sketch.id.into(),
195                segment: PathSegment::CircularInvolute {
196                    start_radius: LengthUnit(start_radius.to_mm()),
197                    end_radius: LengthUnit(end_radius.to_mm()),
198                    angle: Angle::from_degrees(angle_deg),
199                    reverse: reverse.unwrap_or_default(),
200                },
201            }),
202        )
203        .await?;
204
205    let from = sketch.current_pen_position()?;
206
207    let start_radius = start_radius.to_length_units(from.units);
208    let end_radius = end_radius.to_length_units(from.units);
209
210    let mut end: KPoint3d<f64> = Default::default(); // ADAM: TODO impl this below.
211    let theta = f64::sqrt(end_radius * end_radius - start_radius * start_radius) / start_radius;
212    let (x, y) = involute_curve(start_radius, theta);
213
214    end.x = x * libm::cos(angle_rad) - y * libm::sin(angle_rad);
215    end.y = x * libm::sin(angle_rad) + y * libm::cos(angle_rad);
216
217    end.x -= start_radius * libm::cos(angle_rad);
218    end.y -= start_radius * libm::sin(angle_rad);
219
220    if reverse.unwrap_or_default() {
221        end.x = -end.x;
222    }
223
224    end.x += from.x;
225    end.y += from.y;
226
227    let current_path = Path::ToPoint {
228        base: BasePath {
229            from: from.ignore_units(),
230            to: [end.x, end.y],
231            tag: tag.clone(),
232            units: sketch.units,
233            geo_meta: GeoMeta {
234                id,
235                metadata: args.source_range.into(),
236            },
237        },
238    };
239
240    let mut new_sketch = sketch;
241    if let Some(tag) = &tag {
242        new_sketch.add_tag(tag, &current_path, exec_state, None);
243    }
244    new_sketch.paths.push(current_path);
245    Ok(new_sketch)
246}
247
248/// Draw a line to a point.
249pub async fn line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
250    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::sketch(), exec_state)?;
251    let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
252    let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
253    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
254
255    let new_sketch = inner_line(sketch, end_absolute, end, tag, exec_state, args).await?;
256    Ok(KclValue::Sketch {
257        value: Box::new(new_sketch),
258    })
259}
260
261async fn inner_line(
262    sketch: Sketch,
263    end_absolute: Option<[TyF64; 2]>,
264    end: Option<[TyF64; 2]>,
265    tag: Option<TagNode>,
266    exec_state: &mut ExecState,
267    args: Args,
268) -> Result<Sketch, KclError> {
269    straight_line(
270        StraightLineParams {
271            sketch,
272            end_absolute,
273            end,
274            tag,
275            relative_name: "end",
276        },
277        exec_state,
278        args,
279    )
280    .await
281}
282
283struct StraightLineParams {
284    sketch: Sketch,
285    end_absolute: Option<[TyF64; 2]>,
286    end: Option<[TyF64; 2]>,
287    tag: Option<TagNode>,
288    relative_name: &'static str,
289}
290
291impl StraightLineParams {
292    fn relative(p: [TyF64; 2], sketch: Sketch, tag: Option<TagNode>) -> Self {
293        Self {
294            sketch,
295            tag,
296            end: Some(p),
297            end_absolute: None,
298            relative_name: "end",
299        }
300    }
301    fn absolute(p: [TyF64; 2], sketch: Sketch, tag: Option<TagNode>) -> Self {
302        Self {
303            sketch,
304            tag,
305            end: None,
306            end_absolute: Some(p),
307            relative_name: "end",
308        }
309    }
310}
311
312async fn straight_line(
313    StraightLineParams {
314        sketch,
315        end,
316        end_absolute,
317        tag,
318        relative_name,
319    }: StraightLineParams,
320    exec_state: &mut ExecState,
321    args: Args,
322) -> Result<Sketch, KclError> {
323    let from = sketch.current_pen_position()?;
324    let (point, is_absolute) = match (end_absolute, end) {
325        (Some(_), Some(_)) => {
326            return Err(KclError::new_semantic(KclErrorDetails::new(
327                "You cannot give both `end` and `endAbsolute` params, you have to choose one or the other".to_owned(),
328                vec![args.source_range],
329            )));
330        }
331        (Some(end_absolute), None) => (end_absolute, true),
332        (None, Some(end)) => (end, false),
333        (None, None) => {
334            return Err(KclError::new_semantic(KclErrorDetails::new(
335                format!("You must supply either `{relative_name}` or `endAbsolute` arguments"),
336                vec![args.source_range],
337            )));
338        }
339    };
340
341    let id = exec_state.next_uuid();
342    exec_state
343        .batch_modeling_cmd(
344            ModelingCmdMeta::from_args_id(&args, id),
345            ModelingCmd::from(mcmd::ExtendPath {
346                label: Default::default(),
347                path: sketch.id.into(),
348                segment: PathSegment::Line {
349                    end: KPoint2d::from(point_to_mm(point.clone())).with_z(0.0).map(LengthUnit),
350                    relative: !is_absolute,
351                },
352            }),
353        )
354        .await?;
355
356    let end = if is_absolute {
357        point_to_len_unit(point, from.units)
358    } else {
359        let from = sketch.current_pen_position()?;
360        let point = point_to_len_unit(point, from.units);
361        [from.x + point[0], from.y + point[1]]
362    };
363
364    let current_path = Path::ToPoint {
365        base: BasePath {
366            from: from.ignore_units(),
367            to: end,
368            tag: tag.clone(),
369            units: sketch.units,
370            geo_meta: GeoMeta {
371                id,
372                metadata: args.source_range.into(),
373            },
374        },
375    };
376
377    let mut new_sketch = sketch;
378    if let Some(tag) = &tag {
379        new_sketch.add_tag(tag, &current_path, exec_state, None);
380    }
381
382    new_sketch.paths.push(current_path);
383
384    Ok(new_sketch)
385}
386
387/// Draw a line on the x-axis.
388pub async fn x_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
389    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
390    let length: Option<TyF64> = args.get_kw_arg_opt("length", &RuntimeType::length(), exec_state)?;
391    let end_absolute: Option<TyF64> = args.get_kw_arg_opt("endAbsolute", &RuntimeType::length(), exec_state)?;
392    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
393
394    let new_sketch = inner_x_line(sketch, length, end_absolute, tag, exec_state, args).await?;
395    Ok(KclValue::Sketch {
396        value: Box::new(new_sketch),
397    })
398}
399
400async fn inner_x_line(
401    sketch: Sketch,
402    length: Option<TyF64>,
403    end_absolute: Option<TyF64>,
404    tag: Option<TagNode>,
405    exec_state: &mut ExecState,
406    args: Args,
407) -> Result<Sketch, KclError> {
408    let from = sketch.current_pen_position()?;
409    straight_line(
410        StraightLineParams {
411            sketch,
412            end_absolute: end_absolute.map(|x| [x, from.into_y()]),
413            end: length.map(|x| [x, TyF64::new(0.0, NumericType::mm())]),
414            tag,
415            relative_name: "length",
416        },
417        exec_state,
418        args,
419    )
420    .await
421}
422
423/// Draw a line on the y-axis.
424pub async fn y_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
425    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
426    let length: Option<TyF64> = args.get_kw_arg_opt("length", &RuntimeType::length(), exec_state)?;
427    let end_absolute: Option<TyF64> = args.get_kw_arg_opt("endAbsolute", &RuntimeType::length(), exec_state)?;
428    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
429
430    let new_sketch = inner_y_line(sketch, length, end_absolute, tag, exec_state, args).await?;
431    Ok(KclValue::Sketch {
432        value: Box::new(new_sketch),
433    })
434}
435
436async fn inner_y_line(
437    sketch: Sketch,
438    length: Option<TyF64>,
439    end_absolute: Option<TyF64>,
440    tag: Option<TagNode>,
441    exec_state: &mut ExecState,
442    args: Args,
443) -> Result<Sketch, KclError> {
444    let from = sketch.current_pen_position()?;
445    straight_line(
446        StraightLineParams {
447            sketch,
448            end_absolute: end_absolute.map(|y| [from.into_x(), y]),
449            end: length.map(|y| [TyF64::new(0.0, NumericType::mm()), y]),
450            tag,
451            relative_name: "length",
452        },
453        exec_state,
454        args,
455    )
456    .await
457}
458
459/// Draw an angled line.
460pub async fn angled_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
461    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::sketch(), exec_state)?;
462    let angle: TyF64 = args.get_kw_arg("angle", &RuntimeType::degrees(), exec_state)?;
463    let length: Option<TyF64> = args.get_kw_arg_opt("length", &RuntimeType::length(), exec_state)?;
464    let length_x: Option<TyF64> = args.get_kw_arg_opt("lengthX", &RuntimeType::length(), exec_state)?;
465    let length_y: Option<TyF64> = args.get_kw_arg_opt("lengthY", &RuntimeType::length(), exec_state)?;
466    let end_absolute_x: Option<TyF64> = args.get_kw_arg_opt("endAbsoluteX", &RuntimeType::length(), exec_state)?;
467    let end_absolute_y: Option<TyF64> = args.get_kw_arg_opt("endAbsoluteY", &RuntimeType::length(), exec_state)?;
468    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
469
470    let new_sketch = inner_angled_line(
471        sketch,
472        angle.n,
473        length,
474        length_x,
475        length_y,
476        end_absolute_x,
477        end_absolute_y,
478        tag,
479        exec_state,
480        args,
481    )
482    .await?;
483    Ok(KclValue::Sketch {
484        value: Box::new(new_sketch),
485    })
486}
487
488#[allow(clippy::too_many_arguments)]
489async fn inner_angled_line(
490    sketch: Sketch,
491    angle: f64,
492    length: Option<TyF64>,
493    length_x: Option<TyF64>,
494    length_y: Option<TyF64>,
495    end_absolute_x: Option<TyF64>,
496    end_absolute_y: Option<TyF64>,
497    tag: Option<TagNode>,
498    exec_state: &mut ExecState,
499    args: Args,
500) -> Result<Sketch, KclError> {
501    let options_given = [&length, &length_x, &length_y, &end_absolute_x, &end_absolute_y]
502        .iter()
503        .filter(|x| x.is_some())
504        .count();
505    if options_given > 1 {
506        return Err(KclError::new_type(KclErrorDetails::new(
507            " one of `length`, `lengthX`, `lengthY`, `endAbsoluteX`, `endAbsoluteY` can be given".to_string(),
508            vec![args.source_range],
509        )));
510    }
511    if let Some(length_x) = length_x {
512        return inner_angled_line_of_x_length(angle, length_x, sketch, tag, exec_state, args).await;
513    }
514    if let Some(length_y) = length_y {
515        return inner_angled_line_of_y_length(angle, length_y, sketch, tag, exec_state, args).await;
516    }
517    let angle_degrees = angle;
518    match (length, length_x, length_y, end_absolute_x, end_absolute_y) {
519        (Some(length), None, None, None, None) => {
520            inner_angled_line_length(sketch, angle_degrees, length, tag, exec_state, args).await
521        }
522        (None, Some(length_x), None, None, None) => {
523            inner_angled_line_of_x_length(angle_degrees, length_x, sketch, tag, exec_state, args).await
524        }
525        (None, None, Some(length_y), None, None) => {
526            inner_angled_line_of_y_length(angle_degrees, length_y, sketch, tag, exec_state, args).await
527        }
528        (None, None, None, Some(end_absolute_x), None) => {
529            inner_angled_line_to_x(angle_degrees, end_absolute_x, sketch, tag, exec_state, args).await
530        }
531        (None, None, None, None, Some(end_absolute_y)) => {
532            inner_angled_line_to_y(angle_degrees, end_absolute_y, sketch, tag, exec_state, args).await
533        }
534        (None, None, None, None, None) => Err(KclError::new_type(KclErrorDetails::new(
535            "One of `length`, `lengthX`, `lengthY`, `endAbsoluteX`, `endAbsoluteY` must be given".to_string(),
536            vec![args.source_range],
537        ))),
538        _ => Err(KclError::new_type(KclErrorDetails::new(
539            "Only One of `length`, `lengthX`, `lengthY`, `endAbsoluteX`, `endAbsoluteY` can be given".to_owned(),
540            vec![args.source_range],
541        ))),
542    }
543}
544
545async fn inner_angled_line_length(
546    sketch: Sketch,
547    angle_degrees: f64,
548    length: TyF64,
549    tag: Option<TagNode>,
550    exec_state: &mut ExecState,
551    args: Args,
552) -> Result<Sketch, KclError> {
553    let from = sketch.current_pen_position()?;
554    let length = length.to_length_units(from.units);
555
556    //double check me on this one - mike
557    let delta: [f64; 2] = [
558        length * libm::cos(angle_degrees.to_radians()),
559        length * libm::sin(angle_degrees.to_radians()),
560    ];
561    let relative = true;
562
563    let to: [f64; 2] = [from.x + delta[0], from.y + delta[1]];
564
565    let id = exec_state.next_uuid();
566
567    exec_state
568        .batch_modeling_cmd(
569            ModelingCmdMeta::from_args_id(&args, id),
570            ModelingCmd::from(mcmd::ExtendPath {
571                label: Default::default(),
572                path: sketch.id.into(),
573                segment: PathSegment::Line {
574                    end: KPoint2d::from(untyped_point_to_mm(delta, from.units))
575                        .with_z(0.0)
576                        .map(LengthUnit),
577                    relative,
578                },
579            }),
580        )
581        .await?;
582
583    let current_path = Path::ToPoint {
584        base: BasePath {
585            from: from.ignore_units(),
586            to,
587            tag: tag.clone(),
588            units: sketch.units,
589            geo_meta: GeoMeta {
590                id,
591                metadata: args.source_range.into(),
592            },
593        },
594    };
595
596    let mut new_sketch = sketch;
597    if let Some(tag) = &tag {
598        new_sketch.add_tag(tag, &current_path, exec_state, None);
599    }
600
601    new_sketch.paths.push(current_path);
602    Ok(new_sketch)
603}
604
605async fn inner_angled_line_of_x_length(
606    angle_degrees: f64,
607    length: TyF64,
608    sketch: Sketch,
609    tag: Option<TagNode>,
610    exec_state: &mut ExecState,
611    args: Args,
612) -> Result<Sketch, KclError> {
613    if angle_degrees.abs() == 270.0 {
614        return Err(KclError::new_type(KclErrorDetails::new(
615            "Cannot have an x constrained angle of 270 degrees".to_string(),
616            vec![args.source_range],
617        )));
618    }
619
620    if angle_degrees.abs() == 90.0 {
621        return Err(KclError::new_type(KclErrorDetails::new(
622            "Cannot have an x constrained angle of 90 degrees".to_string(),
623            vec![args.source_range],
624        )));
625    }
626
627    let to = get_y_component(Angle::from_degrees(angle_degrees), length.n);
628    let to = [TyF64::new(to[0], length.ty), TyF64::new(to[1], length.ty)];
629
630    let new_sketch = straight_line(StraightLineParams::relative(to, sketch, tag), exec_state, args).await?;
631
632    Ok(new_sketch)
633}
634
635async fn inner_angled_line_to_x(
636    angle_degrees: f64,
637    x_to: TyF64,
638    sketch: Sketch,
639    tag: Option<TagNode>,
640    exec_state: &mut ExecState,
641    args: Args,
642) -> Result<Sketch, KclError> {
643    let from = sketch.current_pen_position()?;
644
645    if angle_degrees.abs() == 270.0 {
646        return Err(KclError::new_type(KclErrorDetails::new(
647            "Cannot have an x constrained angle of 270 degrees".to_string(),
648            vec![args.source_range],
649        )));
650    }
651
652    if angle_degrees.abs() == 90.0 {
653        return Err(KclError::new_type(KclErrorDetails::new(
654            "Cannot have an x constrained angle of 90 degrees".to_string(),
655            vec![args.source_range],
656        )));
657    }
658
659    let x_component = x_to.to_length_units(from.units) - from.x;
660    let y_component = x_component * libm::tan(angle_degrees.to_radians());
661    let y_to = from.y + y_component;
662
663    let new_sketch = straight_line(
664        StraightLineParams::absolute([x_to, TyF64::new(y_to, from.units.into())], sketch, tag),
665        exec_state,
666        args,
667    )
668    .await?;
669    Ok(new_sketch)
670}
671
672async fn inner_angled_line_of_y_length(
673    angle_degrees: f64,
674    length: TyF64,
675    sketch: Sketch,
676    tag: Option<TagNode>,
677    exec_state: &mut ExecState,
678    args: Args,
679) -> Result<Sketch, KclError> {
680    if angle_degrees.abs() == 0.0 {
681        return Err(KclError::new_type(KclErrorDetails::new(
682            "Cannot have a y constrained angle of 0 degrees".to_string(),
683            vec![args.source_range],
684        )));
685    }
686
687    if angle_degrees.abs() == 180.0 {
688        return Err(KclError::new_type(KclErrorDetails::new(
689            "Cannot have a y constrained angle of 180 degrees".to_string(),
690            vec![args.source_range],
691        )));
692    }
693
694    let to = get_x_component(Angle::from_degrees(angle_degrees), length.n);
695    let to = [TyF64::new(to[0], length.ty), TyF64::new(to[1], length.ty)];
696
697    let new_sketch = straight_line(StraightLineParams::relative(to, sketch, tag), exec_state, args).await?;
698
699    Ok(new_sketch)
700}
701
702async fn inner_angled_line_to_y(
703    angle_degrees: f64,
704    y_to: TyF64,
705    sketch: Sketch,
706    tag: Option<TagNode>,
707    exec_state: &mut ExecState,
708    args: Args,
709) -> Result<Sketch, KclError> {
710    let from = sketch.current_pen_position()?;
711
712    if angle_degrees.abs() == 0.0 {
713        return Err(KclError::new_type(KclErrorDetails::new(
714            "Cannot have a y constrained angle of 0 degrees".to_string(),
715            vec![args.source_range],
716        )));
717    }
718
719    if angle_degrees.abs() == 180.0 {
720        return Err(KclError::new_type(KclErrorDetails::new(
721            "Cannot have a y constrained angle of 180 degrees".to_string(),
722            vec![args.source_range],
723        )));
724    }
725
726    let y_component = y_to.to_length_units(from.units) - from.y;
727    let x_component = y_component / libm::tan(angle_degrees.to_radians());
728    let x_to = from.x + x_component;
729
730    let new_sketch = straight_line(
731        StraightLineParams::absolute([TyF64::new(x_to, from.units.into()), y_to], sketch, tag),
732        exec_state,
733        args,
734    )
735    .await?;
736    Ok(new_sketch)
737}
738
739/// Draw an angled line that intersects with a given line.
740pub async fn angled_line_that_intersects(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
741    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
742    let angle: TyF64 = args.get_kw_arg("angle", &RuntimeType::angle(), exec_state)?;
743    let intersect_tag: TagIdentifier = args.get_kw_arg("intersectTag", &RuntimeType::tagged_edge(), exec_state)?;
744    let offset = args.get_kw_arg_opt("offset", &RuntimeType::length(), exec_state)?;
745    let tag: Option<TagNode> = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
746    let new_sketch =
747        inner_angled_line_that_intersects(sketch, angle, intersect_tag, offset, tag, exec_state, args).await?;
748    Ok(KclValue::Sketch {
749        value: Box::new(new_sketch),
750    })
751}
752
753pub async fn inner_angled_line_that_intersects(
754    sketch: Sketch,
755    angle: TyF64,
756    intersect_tag: TagIdentifier,
757    offset: Option<TyF64>,
758    tag: Option<TagNode>,
759    exec_state: &mut ExecState,
760    args: Args,
761) -> Result<Sketch, KclError> {
762    let intersect_path = args.get_tag_engine_info(exec_state, &intersect_tag)?;
763    let path = intersect_path.path.clone().ok_or_else(|| {
764        KclError::new_type(KclErrorDetails::new(
765            format!("Expected an intersect path with a path, found `{intersect_path:?}`"),
766            vec![args.source_range],
767        ))
768    })?;
769
770    let from = sketch.current_pen_position()?;
771    let to = intersection_with_parallel_line(
772        &[
773            point_to_len_unit(path.get_from(), from.units),
774            point_to_len_unit(path.get_to(), from.units),
775        ],
776        offset.map(|t| t.to_length_units(from.units)).unwrap_or_default(),
777        angle.to_degrees(exec_state, args.source_range),
778        from.ignore_units(),
779    );
780    let to = [
781        TyF64::new(to[0], from.units.into()),
782        TyF64::new(to[1], from.units.into()),
783    ];
784
785    straight_line(StraightLineParams::absolute(to, sketch, tag), exec_state, args).await
786}
787
788/// Data for start sketch on.
789/// You can start a sketch on a plane or an solid.
790#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
791#[ts(export)]
792#[serde(rename_all = "camelCase", untagged)]
793#[allow(clippy::large_enum_variant)]
794pub enum SketchData {
795    PlaneOrientation(PlaneData),
796    Plane(Box<Plane>),
797    Solid(Box<Solid>),
798}
799
800/// Orientation data that can be used to construct a plane, not a plane in itself.
801#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS)]
802#[ts(export)]
803#[serde(rename_all = "camelCase")]
804#[allow(clippy::large_enum_variant)]
805pub enum PlaneData {
806    /// The XY plane.
807    #[serde(rename = "XY", alias = "xy")]
808    XY,
809    /// The opposite side of the XY plane.
810    #[serde(rename = "-XY", alias = "-xy")]
811    NegXY,
812    /// The XZ plane.
813    #[serde(rename = "XZ", alias = "xz")]
814    XZ,
815    /// The opposite side of the XZ plane.
816    #[serde(rename = "-XZ", alias = "-xz")]
817    NegXZ,
818    /// The YZ plane.
819    #[serde(rename = "YZ", alias = "yz")]
820    YZ,
821    /// The opposite side of the YZ plane.
822    #[serde(rename = "-YZ", alias = "-yz")]
823    NegYZ,
824    /// A defined plane.
825    Plane(PlaneInfo),
826}
827
828/// Start a sketch on a specific plane or face.
829pub async fn start_sketch_on(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
830    let data = args.get_unlabeled_kw_arg(
831        "planeOrSolid",
832        &RuntimeType::Union(vec![RuntimeType::solid(), RuntimeType::plane()]),
833        exec_state,
834    )?;
835    let face = args.get_kw_arg_opt("face", &RuntimeType::tagged_face(), exec_state)?;
836    let normal_to_face = args.get_kw_arg_opt("normalToFace", &RuntimeType::tagged_face(), exec_state)?;
837    let align_axis = args.get_kw_arg_opt("alignAxis", &RuntimeType::Primitive(PrimitiveType::Axis2d), exec_state)?;
838    let normal_offset = args.get_kw_arg_opt("normalOffset", &RuntimeType::length(), exec_state)?;
839
840    match inner_start_sketch_on(data, face, normal_to_face, align_axis, normal_offset, exec_state, &args).await? {
841        SketchSurface::Plane(value) => Ok(KclValue::Plane { value }),
842        SketchSurface::Face(value) => Ok(KclValue::Face { value }),
843    }
844}
845
846async fn inner_start_sketch_on(
847    plane_or_solid: SketchData,
848    face: Option<FaceTag>,
849    normal_to_face: Option<FaceTag>,
850    align_axis: Option<Axis2dOrEdgeReference>,
851    normal_offset: Option<TyF64>,
852    exec_state: &mut ExecState,
853    args: &Args,
854) -> Result<SketchSurface, KclError> {
855    let face = match (face, normal_to_face, &align_axis, &normal_offset) {
856        (Some(_), Some(_), _, _) => {
857            return Err(KclError::new_semantic(KclErrorDetails::new(
858                "You cannot give both `face` and `normalToFace` params, you have to choose one or the other."
859                    .to_owned(),
860                vec![args.source_range],
861            )));
862        }
863        (Some(face), None, None, None) => Some(face),
864        (_, Some(_), None, _) => {
865            return Err(KclError::new_semantic(KclErrorDetails::new(
866                "`alignAxis` is required if `normalToFace` is specified.".to_owned(),
867                vec![args.source_range],
868            )));
869        }
870        (_, None, Some(_), _) => {
871            return Err(KclError::new_semantic(KclErrorDetails::new(
872                "`normalToFace` is required if `alignAxis` is specified.".to_owned(),
873                vec![args.source_range],
874            )));
875        }
876        (_, None, _, Some(_)) => {
877            return Err(KclError::new_semantic(KclErrorDetails::new(
878                "`normalToFace` is required if `normalOffset` is specified.".to_owned(),
879                vec![args.source_range],
880            )));
881        }
882        (_, Some(face), Some(_), _) => Some(face),
883        (None, None, None, None) => None,
884    };
885
886    match plane_or_solid {
887        SketchData::PlaneOrientation(plane_data) => {
888            let plane = make_sketch_plane_from_orientation(plane_data, exec_state, args).await?;
889            Ok(SketchSurface::Plane(plane))
890        }
891        SketchData::Plane(plane) => {
892            if plane.value == crate::exec::PlaneType::Uninit {
893                let plane = make_sketch_plane_from_orientation(plane.info.into_plane_data(), exec_state, args).await?;
894                Ok(SketchSurface::Plane(plane))
895            } else {
896                // Create artifact used only by the UI, not the engine.
897                #[cfg(feature = "artifact-graph")]
898                {
899                    let id = exec_state.next_uuid();
900                    exec_state.add_artifact(Artifact::StartSketchOnPlane(StartSketchOnPlane {
901                        id: ArtifactId::from(id),
902                        plane_id: plane.artifact_id,
903                        code_ref: CodeRef::placeholder(args.source_range),
904                    }));
905                }
906
907                Ok(SketchSurface::Plane(plane))
908            }
909        }
910        SketchData::Solid(solid) => {
911            let Some(tag) = face else {
912                return Err(KclError::new_type(KclErrorDetails::new(
913                    "Expected a tag for the face to sketch on".to_string(),
914                    vec![args.source_range],
915                )));
916            };
917            if let Some(align_axis) = align_axis {
918                let plane_of = inner_plane_of(*solid, tag, exec_state, args).await?;
919
920                let offset = normal_offset.map_or(0.0, |x| x.n);
921                let (x_axis, y_axis, normal_offset) = match align_axis {
922                    Axis2dOrEdgeReference::Axis { direction, origin: _ } => {
923                        if (direction[0].n - 1.0).abs() < f64::EPSILON {
924                            //X axis chosen
925                            (
926                                plane_of.info.x_axis,
927                                plane_of.info.z_axis,
928                                plane_of.info.y_axis * offset,
929                            )
930                        } else if (direction[0].n + 1.0).abs() < f64::EPSILON {
931                            // -X axis chosen
932                            (
933                                plane_of.info.x_axis.negated(),
934                                plane_of.info.z_axis,
935                                plane_of.info.y_axis * offset,
936                            )
937                        } else if (direction[1].n - 1.0).abs() < f64::EPSILON {
938                            // Y axis chosen
939                            (
940                                plane_of.info.y_axis,
941                                plane_of.info.z_axis,
942                                plane_of.info.x_axis * offset,
943                            )
944                        } else if (direction[1].n + 1.0).abs() < f64::EPSILON {
945                            // -Y axis chosen
946                            (
947                                plane_of.info.y_axis.negated(),
948                                plane_of.info.z_axis,
949                                plane_of.info.x_axis * offset,
950                            )
951                        } else {
952                            return Err(KclError::new_semantic(KclErrorDetails::new(
953                                "Unsupported axis detected. This function only supports using X, -X, Y and -Y."
954                                    .to_owned(),
955                                vec![args.source_range],
956                            )));
957                        }
958                    }
959                    Axis2dOrEdgeReference::Edge(_) => {
960                        return Err(KclError::new_semantic(KclErrorDetails::new(
961                            "Use of an edge here is unsupported, please specify an `Axis2d` (e.g. `X`) instead."
962                                .to_owned(),
963                            vec![args.source_range],
964                        )));
965                    }
966                };
967                let origin = Point3d::new(0.0, 0.0, 0.0, plane_of.info.origin.units);
968                let plane_data = PlaneData::Plane(PlaneInfo {
969                    origin: plane_of.project(origin) + normal_offset,
970                    x_axis,
971                    y_axis,
972                    z_axis: x_axis.axes_cross_product(&y_axis),
973                });
974                let plane = make_sketch_plane_from_orientation(plane_data, exec_state, args).await?;
975
976                // Create artifact used only by the UI, not the engine.
977                #[cfg(feature = "artifact-graph")]
978                {
979                    let id = exec_state.next_uuid();
980                    exec_state.add_artifact(Artifact::StartSketchOnPlane(StartSketchOnPlane {
981                        id: ArtifactId::from(id),
982                        plane_id: plane.artifact_id,
983                        code_ref: CodeRef::placeholder(args.source_range),
984                    }));
985                }
986
987                Ok(SketchSurface::Plane(plane))
988            } else {
989                let face = start_sketch_on_face(solid, tag, exec_state, args).await?;
990
991                #[cfg(feature = "artifact-graph")]
992                {
993                    // Create artifact used only by the UI, not the engine.
994                    let id = exec_state.next_uuid();
995                    exec_state.add_artifact(Artifact::StartSketchOnFace(StartSketchOnFace {
996                        id: ArtifactId::from(id),
997                        face_id: face.artifact_id,
998                        code_ref: CodeRef::placeholder(args.source_range),
999                    }));
1000                }
1001
1002                Ok(SketchSurface::Face(face))
1003            }
1004        }
1005    }
1006}
1007
1008async fn start_sketch_on_face(
1009    solid: Box<Solid>,
1010    tag: FaceTag,
1011    exec_state: &mut ExecState,
1012    args: &Args,
1013) -> Result<Box<Face>, KclError> {
1014    let extrude_plane_id = tag.get_face_id(&solid, exec_state, args, true).await?;
1015
1016    Ok(Box::new(Face {
1017        id: extrude_plane_id,
1018        artifact_id: extrude_plane_id.into(),
1019        value: tag.to_string(),
1020        // TODO: get this from the extrude plane data.
1021        x_axis: solid.sketch.on.x_axis(),
1022        y_axis: solid.sketch.on.y_axis(),
1023        units: solid.units,
1024        solid,
1025        meta: vec![args.source_range.into()],
1026    }))
1027}
1028
1029pub async fn make_sketch_plane_from_orientation(
1030    data: PlaneData,
1031    exec_state: &mut ExecState,
1032    args: &Args,
1033) -> Result<Box<Plane>, KclError> {
1034    let plane = Plane::from_plane_data(data.clone(), exec_state)?;
1035
1036    // Create the plane on the fly.
1037    let clobber = false;
1038    let size = LengthUnit(60.0);
1039    let hide = Some(true);
1040    exec_state
1041        .batch_modeling_cmd(
1042            ModelingCmdMeta::from_args_id(args, plane.id),
1043            ModelingCmd::from(mcmd::MakePlane {
1044                clobber,
1045                origin: plane.info.origin.into(),
1046                size,
1047                x_axis: plane.info.x_axis.into(),
1048                y_axis: plane.info.y_axis.into(),
1049                hide,
1050            }),
1051        )
1052        .await?;
1053
1054    Ok(Box::new(plane))
1055}
1056
1057/// Start a new profile at a given point.
1058pub async fn start_profile(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1059    let sketch_surface = args.get_unlabeled_kw_arg(
1060        "startProfileOn",
1061        &RuntimeType::Union(vec![RuntimeType::plane(), RuntimeType::face()]),
1062        exec_state,
1063    )?;
1064    let start: [TyF64; 2] = args.get_kw_arg("at", &RuntimeType::point2d(), exec_state)?;
1065    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
1066
1067    let sketch = inner_start_profile(sketch_surface, start, tag, exec_state, args).await?;
1068    Ok(KclValue::Sketch {
1069        value: Box::new(sketch),
1070    })
1071}
1072
1073pub(crate) async fn inner_start_profile(
1074    sketch_surface: SketchSurface,
1075    at: [TyF64; 2],
1076    tag: Option<TagNode>,
1077    exec_state: &mut ExecState,
1078    args: Args,
1079) -> Result<Sketch, KclError> {
1080    match &sketch_surface {
1081        SketchSurface::Face(face) => {
1082            // Flush the batch for our fillets/chamfers if there are any.
1083            // If we do not do these for sketch on face, things will fail with face does not exist.
1084            exec_state
1085                .flush_batch_for_solids((&args).into(), &[(*face.solid).clone()])
1086                .await?;
1087        }
1088        SketchSurface::Plane(plane) if !plane.is_standard() => {
1089            // Hide whatever plane we are sketching on.
1090            // This is especially helpful for offset planes, which would be visible otherwise.
1091            exec_state
1092                .batch_end_cmd(
1093                    (&args).into(),
1094                    ModelingCmd::from(mcmd::ObjectVisible {
1095                        object_id: plane.id,
1096                        hidden: true,
1097                    }),
1098                )
1099                .await?;
1100        }
1101        _ => {}
1102    }
1103
1104    let enable_sketch_id = exec_state.next_uuid();
1105    let path_id = exec_state.next_uuid();
1106    let move_pen_id = exec_state.next_uuid();
1107    let disable_sketch_id = exec_state.next_uuid();
1108    exec_state
1109        .batch_modeling_cmds(
1110            (&args).into(),
1111            &[
1112                // Enter sketch mode on the surface.
1113                // We call this here so you can reuse the sketch surface for multiple sketches.
1114                ModelingCmdReq {
1115                    cmd: ModelingCmd::from(mcmd::EnableSketchMode {
1116                        animated: false,
1117                        ortho: false,
1118                        entity_id: sketch_surface.id(),
1119                        adjust_camera: false,
1120                        planar_normal: if let SketchSurface::Plane(plane) = &sketch_surface {
1121                            // We pass in the normal for the plane here.
1122                            let normal = plane.info.x_axis.axes_cross_product(&plane.info.y_axis);
1123                            Some(normal.into())
1124                        } else {
1125                            None
1126                        },
1127                    }),
1128                    cmd_id: enable_sketch_id.into(),
1129                },
1130                ModelingCmdReq {
1131                    cmd: ModelingCmd::from(mcmd::StartPath::default()),
1132                    cmd_id: path_id.into(),
1133                },
1134                ModelingCmdReq {
1135                    cmd: ModelingCmd::from(mcmd::MovePathPen {
1136                        path: path_id.into(),
1137                        to: KPoint2d::from(point_to_mm(at.clone())).with_z(0.0).map(LengthUnit),
1138                    }),
1139                    cmd_id: move_pen_id.into(),
1140                },
1141                ModelingCmdReq {
1142                    cmd: ModelingCmd::SketchModeDisable(mcmd::SketchModeDisable::default()),
1143                    cmd_id: disable_sketch_id.into(),
1144                },
1145            ],
1146        )
1147        .await?;
1148
1149    // Convert to the units of the module.  This is what the frontend expects.
1150    let units = exec_state.length_unit();
1151    let to = point_to_len_unit(at, units);
1152    let current_path = BasePath {
1153        from: to,
1154        to,
1155        tag: tag.clone(),
1156        units,
1157        geo_meta: GeoMeta {
1158            id: move_pen_id,
1159            metadata: args.source_range.into(),
1160        },
1161    };
1162
1163    let sketch = Sketch {
1164        id: path_id,
1165        original_id: path_id,
1166        artifact_id: path_id.into(),
1167        on: sketch_surface.clone(),
1168        paths: vec![],
1169        inner_paths: vec![],
1170        units,
1171        mirror: Default::default(),
1172        meta: vec![args.source_range.into()],
1173        tags: if let Some(tag) = &tag {
1174            let mut tag_identifier: TagIdentifier = tag.into();
1175            tag_identifier.info = vec![(
1176                exec_state.stack().current_epoch(),
1177                TagEngineInfo {
1178                    id: current_path.geo_meta.id,
1179                    sketch: path_id,
1180                    path: Some(Path::Base {
1181                        base: current_path.clone(),
1182                    }),
1183                    surface: None,
1184                },
1185            )];
1186            IndexMap::from([(tag.name.to_string(), tag_identifier)])
1187        } else {
1188            Default::default()
1189        },
1190        start: current_path,
1191        is_closed: false,
1192    };
1193    Ok(sketch)
1194}
1195
1196/// Returns the X component of the sketch profile start point.
1197pub async fn profile_start_x(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1198    let sketch: Sketch = args.get_unlabeled_kw_arg("profile", &RuntimeType::sketch(), exec_state)?;
1199    let ty = sketch.units.into();
1200    let x = inner_profile_start_x(sketch)?;
1201    Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
1202}
1203
1204pub(crate) fn inner_profile_start_x(profile: Sketch) -> Result<f64, KclError> {
1205    Ok(profile.start.to[0])
1206}
1207
1208/// Returns the Y component of the sketch profile start point.
1209pub async fn profile_start_y(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1210    let sketch: Sketch = args.get_unlabeled_kw_arg("profile", &RuntimeType::sketch(), exec_state)?;
1211    let ty = sketch.units.into();
1212    let x = inner_profile_start_y(sketch)?;
1213    Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
1214}
1215
1216pub(crate) fn inner_profile_start_y(profile: Sketch) -> Result<f64, KclError> {
1217    Ok(profile.start.to[1])
1218}
1219
1220/// Returns the sketch profile start point.
1221pub async fn profile_start(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1222    let sketch: Sketch = args.get_unlabeled_kw_arg("profile", &RuntimeType::sketch(), exec_state)?;
1223    let ty = sketch.units.into();
1224    let point = inner_profile_start(sketch)?;
1225    Ok(KclValue::from_point2d(point, ty, args.into()))
1226}
1227
1228pub(crate) fn inner_profile_start(profile: Sketch) -> Result<[f64; 2], KclError> {
1229    Ok(profile.start.to)
1230}
1231
1232/// Close the current sketch.
1233pub async fn close(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1234    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1235    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
1236    let new_sketch = inner_close(sketch, tag, exec_state, args).await?;
1237    Ok(KclValue::Sketch {
1238        value: Box::new(new_sketch),
1239    })
1240}
1241
1242pub(crate) async fn inner_close(
1243    sketch: Sketch,
1244    tag: Option<TagNode>,
1245    exec_state: &mut ExecState,
1246    args: Args,
1247) -> Result<Sketch, KclError> {
1248    if sketch.is_closed {
1249        exec_state.warn(
1250            crate::CompilationError {
1251                source_range: args.source_range,
1252                message: "This sketch is already closed. Remove this unnecessary `close()` call".to_string(),
1253                suggestion: None,
1254                severity: crate::errors::Severity::Warning,
1255                tag: crate::errors::Tag::Unnecessary,
1256            },
1257            annotations::WARN_UNNECESSARY_CLOSE,
1258        );
1259        return Ok(sketch);
1260    }
1261    let from = sketch.current_pen_position()?;
1262    let to = point_to_len_unit(sketch.start.get_from(), from.units);
1263
1264    let id = exec_state.next_uuid();
1265
1266    exec_state
1267        .batch_modeling_cmd(
1268            ModelingCmdMeta::from_args_id(&args, id),
1269            ModelingCmd::from(mcmd::ClosePath { path_id: sketch.id }),
1270        )
1271        .await?;
1272
1273    let current_path = Path::ToPoint {
1274        base: BasePath {
1275            from: from.ignore_units(),
1276            to,
1277            tag: tag.clone(),
1278            units: sketch.units,
1279            geo_meta: GeoMeta {
1280                id,
1281                metadata: args.source_range.into(),
1282            },
1283        },
1284    };
1285
1286    let mut new_sketch = sketch;
1287    if let Some(tag) = &tag {
1288        new_sketch.add_tag(tag, &current_path, exec_state, None);
1289    }
1290    new_sketch.paths.push(current_path);
1291    new_sketch.is_closed = true;
1292
1293    Ok(new_sketch)
1294}
1295
1296/// Draw an arc.
1297pub async fn arc(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1298    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1299
1300    let angle_start: Option<TyF64> = args.get_kw_arg_opt("angleStart", &RuntimeType::degrees(), exec_state)?;
1301    let angle_end: Option<TyF64> = args.get_kw_arg_opt("angleEnd", &RuntimeType::degrees(), exec_state)?;
1302    let radius: Option<TyF64> = args.get_kw_arg_opt("radius", &RuntimeType::length(), exec_state)?;
1303    let diameter: Option<TyF64> = args.get_kw_arg_opt("diameter", &RuntimeType::length(), exec_state)?;
1304    let end_absolute: Option<[TyF64; 2]> = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
1305    let interior_absolute: Option<[TyF64; 2]> =
1306        args.get_kw_arg_opt("interiorAbsolute", &RuntimeType::point2d(), exec_state)?;
1307    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
1308    let new_sketch = inner_arc(
1309        sketch,
1310        angle_start,
1311        angle_end,
1312        radius,
1313        diameter,
1314        interior_absolute,
1315        end_absolute,
1316        tag,
1317        exec_state,
1318        args,
1319    )
1320    .await?;
1321    Ok(KclValue::Sketch {
1322        value: Box::new(new_sketch),
1323    })
1324}
1325
1326#[allow(clippy::too_many_arguments)]
1327pub(crate) async fn inner_arc(
1328    sketch: Sketch,
1329    angle_start: Option<TyF64>,
1330    angle_end: Option<TyF64>,
1331    radius: Option<TyF64>,
1332    diameter: Option<TyF64>,
1333    interior_absolute: Option<[TyF64; 2]>,
1334    end_absolute: Option<[TyF64; 2]>,
1335    tag: Option<TagNode>,
1336    exec_state: &mut ExecState,
1337    args: Args,
1338) -> Result<Sketch, KclError> {
1339    let from: Point2d = sketch.current_pen_position()?;
1340    let id = exec_state.next_uuid();
1341
1342    match (angle_start, angle_end, radius, diameter, interior_absolute, end_absolute) {
1343        (Some(angle_start), Some(angle_end), radius, diameter, None, None) => {
1344            let radius = get_radius(radius, diameter, args.source_range)?;
1345            relative_arc(&args, id, exec_state, sketch, from, angle_start, angle_end, radius, tag).await
1346        }
1347        (None, None, None, None, Some(interior_absolute), Some(end_absolute)) => {
1348            absolute_arc(&args, id, exec_state, sketch, from, interior_absolute, end_absolute, tag).await
1349        }
1350        _ => {
1351            Err(KclError::new_type(KclErrorDetails::new(
1352                "Invalid combination of arguments. Either provide (angleStart, angleEnd, radius) or (endAbsolute, interiorAbsolute)".to_owned(),
1353                vec![args.source_range],
1354            )))
1355        }
1356    }
1357}
1358
1359#[allow(clippy::too_many_arguments)]
1360pub async fn absolute_arc(
1361    args: &Args,
1362    id: uuid::Uuid,
1363    exec_state: &mut ExecState,
1364    sketch: Sketch,
1365    from: Point2d,
1366    interior_absolute: [TyF64; 2],
1367    end_absolute: [TyF64; 2],
1368    tag: Option<TagNode>,
1369) -> Result<Sketch, KclError> {
1370    // The start point is taken from the path you are extending.
1371    exec_state
1372        .batch_modeling_cmd(
1373            ModelingCmdMeta::from_args_id(args, id),
1374            ModelingCmd::from(mcmd::ExtendPath {
1375                label: Default::default(),
1376                path: sketch.id.into(),
1377                segment: PathSegment::ArcTo {
1378                    end: kcmc::shared::Point3d {
1379                        x: LengthUnit(end_absolute[0].to_mm()),
1380                        y: LengthUnit(end_absolute[1].to_mm()),
1381                        z: LengthUnit(0.0),
1382                    },
1383                    interior: kcmc::shared::Point3d {
1384                        x: LengthUnit(interior_absolute[0].to_mm()),
1385                        y: LengthUnit(interior_absolute[1].to_mm()),
1386                        z: LengthUnit(0.0),
1387                    },
1388                    relative: false,
1389                },
1390            }),
1391        )
1392        .await?;
1393
1394    let start = [from.x, from.y];
1395    let end = point_to_len_unit(end_absolute, from.units);
1396
1397    let current_path = Path::ArcThreePoint {
1398        base: BasePath {
1399            from: from.ignore_units(),
1400            to: end,
1401            tag: tag.clone(),
1402            units: sketch.units,
1403            geo_meta: GeoMeta {
1404                id,
1405                metadata: args.source_range.into(),
1406            },
1407        },
1408        p1: start,
1409        p2: point_to_len_unit(interior_absolute, from.units),
1410        p3: end,
1411    };
1412
1413    let mut new_sketch = sketch;
1414    if let Some(tag) = &tag {
1415        new_sketch.add_tag(tag, &current_path, exec_state, None);
1416    }
1417
1418    new_sketch.paths.push(current_path);
1419
1420    Ok(new_sketch)
1421}
1422
1423#[allow(clippy::too_many_arguments)]
1424pub async fn relative_arc(
1425    args: &Args,
1426    id: uuid::Uuid,
1427    exec_state: &mut ExecState,
1428    sketch: Sketch,
1429    from: Point2d,
1430    angle_start: TyF64,
1431    angle_end: TyF64,
1432    radius: TyF64,
1433    tag: Option<TagNode>,
1434) -> Result<Sketch, KclError> {
1435    let a_start = Angle::from_degrees(angle_start.to_degrees(exec_state, args.source_range));
1436    let a_end = Angle::from_degrees(angle_end.to_degrees(exec_state, args.source_range));
1437    let radius = radius.to_length_units(from.units);
1438    let (center, end) = arc_center_and_end(from.ignore_units(), a_start, a_end, radius);
1439    if a_start == a_end {
1440        return Err(KclError::new_type(KclErrorDetails::new(
1441            "Arc start and end angles must be different".to_string(),
1442            vec![args.source_range],
1443        )));
1444    }
1445    let ccw = a_start < a_end;
1446
1447    exec_state
1448        .batch_modeling_cmd(
1449            ModelingCmdMeta::from_args_id(args, id),
1450            ModelingCmd::from(mcmd::ExtendPath {
1451                label: Default::default(),
1452                path: sketch.id.into(),
1453                segment: PathSegment::Arc {
1454                    start: a_start,
1455                    end: a_end,
1456                    center: KPoint2d::from(untyped_point_to_mm(center, from.units)).map(LengthUnit),
1457                    radius: LengthUnit(
1458                        crate::execution::types::adjust_length(from.units, radius, UnitLength::Millimeters).0,
1459                    ),
1460                    relative: false,
1461                },
1462            }),
1463        )
1464        .await?;
1465
1466    let current_path = Path::Arc {
1467        base: BasePath {
1468            from: from.ignore_units(),
1469            to: end,
1470            tag: tag.clone(),
1471            units: from.units,
1472            geo_meta: GeoMeta {
1473                id,
1474                metadata: args.source_range.into(),
1475            },
1476        },
1477        center,
1478        radius,
1479        ccw,
1480    };
1481
1482    let mut new_sketch = sketch;
1483    if let Some(tag) = &tag {
1484        new_sketch.add_tag(tag, &current_path, exec_state, None);
1485    }
1486
1487    new_sketch.paths.push(current_path);
1488
1489    Ok(new_sketch)
1490}
1491
1492/// Draw a tangential arc to a specific point.
1493pub async fn tangential_arc(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1494    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1495    let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
1496    let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
1497    let radius = args.get_kw_arg_opt("radius", &RuntimeType::length(), exec_state)?;
1498    let diameter = args.get_kw_arg_opt("diameter", &RuntimeType::length(), exec_state)?;
1499    let angle = args.get_kw_arg_opt("angle", &RuntimeType::angle(), exec_state)?;
1500    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
1501
1502    let new_sketch = inner_tangential_arc(
1503        sketch,
1504        end_absolute,
1505        end,
1506        radius,
1507        diameter,
1508        angle,
1509        tag,
1510        exec_state,
1511        args,
1512    )
1513    .await?;
1514    Ok(KclValue::Sketch {
1515        value: Box::new(new_sketch),
1516    })
1517}
1518
1519#[allow(clippy::too_many_arguments)]
1520async fn inner_tangential_arc(
1521    sketch: Sketch,
1522    end_absolute: Option<[TyF64; 2]>,
1523    end: Option<[TyF64; 2]>,
1524    radius: Option<TyF64>,
1525    diameter: Option<TyF64>,
1526    angle: Option<TyF64>,
1527    tag: Option<TagNode>,
1528    exec_state: &mut ExecState,
1529    args: Args,
1530) -> Result<Sketch, KclError> {
1531    match (end_absolute, end, radius, diameter, angle) {
1532        (Some(point), None, None, None, None) => {
1533            inner_tangential_arc_to_point(sketch, point, true, tag, exec_state, args).await
1534        }
1535        (None, Some(point), None, None, None) => {
1536            inner_tangential_arc_to_point(sketch, point, false, tag, exec_state, args).await
1537        }
1538        (None, None, radius, diameter, Some(angle)) => {
1539            let radius = get_radius(radius, diameter, args.source_range)?;
1540            let data = TangentialArcData::RadiusAndOffset { radius, offset: angle };
1541            inner_tangential_arc_radius_angle(data, sketch, tag, exec_state, args).await
1542        }
1543        (Some(_), Some(_), None, None, None) => Err(KclError::new_semantic(KclErrorDetails::new(
1544            "You cannot give both `end` and `endAbsolute` params, you have to choose one or the other".to_owned(),
1545            vec![args.source_range],
1546        ))),
1547        (_, _, _, _, _) => Err(KclError::new_semantic(KclErrorDetails::new(
1548            "You must supply `end`, `endAbsolute`, or both `angle` and `radius`/`diameter` arguments".to_owned(),
1549            vec![args.source_range],
1550        ))),
1551    }
1552}
1553
1554/// Data to draw a tangential arc.
1555#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
1556#[ts(export)]
1557#[serde(rename_all = "camelCase", untagged)]
1558pub enum TangentialArcData {
1559    RadiusAndOffset {
1560        /// Radius of the arc.
1561        /// Not to be confused with Raiders of the Lost Ark.
1562        radius: TyF64,
1563        /// Offset of the arc, in degrees.
1564        offset: TyF64,
1565    },
1566}
1567
1568/// Draw a curved line segment along part of an imaginary circle.
1569///
1570/// The arc is constructed such that the last line segment is placed tangent
1571/// to the imaginary circle of the specified radius. The resulting arc is the
1572/// segment of the imaginary circle from that tangent point for 'angle'
1573/// degrees along the imaginary circle.
1574async fn inner_tangential_arc_radius_angle(
1575    data: TangentialArcData,
1576    sketch: Sketch,
1577    tag: Option<TagNode>,
1578    exec_state: &mut ExecState,
1579    args: Args,
1580) -> Result<Sketch, KclError> {
1581    let from: Point2d = sketch.current_pen_position()?;
1582    // next set of lines is some undocumented voodoo from get_tangential_arc_to_info
1583    let tangent_info = sketch.get_tangential_info_from_paths(); //this function desperately needs some documentation
1584    let tan_previous_point = tangent_info.tan_previous_point(from.ignore_units());
1585
1586    let id = exec_state.next_uuid();
1587
1588    let (center, to, ccw) = match data {
1589        TangentialArcData::RadiusAndOffset { radius, offset } => {
1590            // KCL stdlib types use degrees.
1591            let offset = Angle::from_degrees(offset.to_degrees(exec_state, args.source_range));
1592
1593            // Calculate the end point from the angle and radius.
1594            // atan2 outputs radians.
1595            let previous_end_tangent = Angle::from_radians(libm::atan2(
1596                from.y - tan_previous_point[1],
1597                from.x - tan_previous_point[0],
1598            ));
1599            // make sure the arc center is on the correct side to guarantee deterministic behavior
1600            // note the engine automatically rejects an offset of zero, if we want to flag that at KCL too to avoid engine errors
1601            let ccw = offset.to_degrees() > 0.0;
1602            let tangent_to_arc_start_angle = if ccw {
1603                // CCW turn
1604                Angle::from_degrees(-90.0)
1605            } else {
1606                // CW turn
1607                Angle::from_degrees(90.0)
1608            };
1609            // may need some logic and / or modulo on the various angle values to prevent them from going "backwards"
1610            // but the above logic *should* capture that behavior
1611            let start_angle = previous_end_tangent + tangent_to_arc_start_angle;
1612            let end_angle = start_angle + offset;
1613            let (center, to) = arc_center_and_end(
1614                from.ignore_units(),
1615                start_angle,
1616                end_angle,
1617                radius.to_length_units(from.units),
1618            );
1619
1620            exec_state
1621                .batch_modeling_cmd(
1622                    ModelingCmdMeta::from_args_id(&args, id),
1623                    ModelingCmd::from(mcmd::ExtendPath {
1624                        label: Default::default(),
1625                        path: sketch.id.into(),
1626                        segment: PathSegment::TangentialArc {
1627                            radius: LengthUnit(radius.to_mm()),
1628                            offset,
1629                        },
1630                    }),
1631                )
1632                .await?;
1633            (center, to, ccw)
1634        }
1635    };
1636
1637    let current_path = Path::TangentialArc {
1638        ccw,
1639        center,
1640        base: BasePath {
1641            from: from.ignore_units(),
1642            to,
1643            tag: tag.clone(),
1644            units: sketch.units,
1645            geo_meta: GeoMeta {
1646                id,
1647                metadata: args.source_range.into(),
1648            },
1649        },
1650    };
1651
1652    let mut new_sketch = sketch;
1653    if let Some(tag) = &tag {
1654        new_sketch.add_tag(tag, &current_path, exec_state, None);
1655    }
1656
1657    new_sketch.paths.push(current_path);
1658
1659    Ok(new_sketch)
1660}
1661
1662// `to` must be in sketch.units
1663fn tan_arc_to(sketch: &Sketch, to: [f64; 2]) -> ModelingCmd {
1664    ModelingCmd::from(mcmd::ExtendPath {
1665        label: Default::default(),
1666        path: sketch.id.into(),
1667        segment: PathSegment::TangentialArcTo {
1668            angle_snap_increment: None,
1669            to: KPoint2d::from(untyped_point_to_mm(to, sketch.units))
1670                .with_z(0.0)
1671                .map(LengthUnit),
1672        },
1673    })
1674}
1675
1676async fn inner_tangential_arc_to_point(
1677    sketch: Sketch,
1678    point: [TyF64; 2],
1679    is_absolute: bool,
1680    tag: Option<TagNode>,
1681    exec_state: &mut ExecState,
1682    args: Args,
1683) -> Result<Sketch, KclError> {
1684    let from: Point2d = sketch.current_pen_position()?;
1685    let tangent_info = sketch.get_tangential_info_from_paths();
1686    let tan_previous_point = tangent_info.tan_previous_point(from.ignore_units());
1687
1688    let point = point_to_len_unit(point, from.units);
1689
1690    let to = if is_absolute {
1691        point
1692    } else {
1693        [from.x + point[0], from.y + point[1]]
1694    };
1695    let [to_x, to_y] = to;
1696    let result = get_tangential_arc_to_info(TangentialArcInfoInput {
1697        arc_start_point: [from.x, from.y],
1698        arc_end_point: [to_x, to_y],
1699        tan_previous_point,
1700        obtuse: true,
1701    });
1702
1703    if result.center[0].is_infinite() {
1704        return Err(KclError::new_semantic(KclErrorDetails::new(
1705            "could not sketch tangential arc, because its center would be infinitely far away in the X direction"
1706                .to_owned(),
1707            vec![args.source_range],
1708        )));
1709    } else if result.center[1].is_infinite() {
1710        return Err(KclError::new_semantic(KclErrorDetails::new(
1711            "could not sketch tangential arc, because its center would be infinitely far away in the Y direction"
1712                .to_owned(),
1713            vec![args.source_range],
1714        )));
1715    }
1716
1717    let delta = if is_absolute {
1718        [to_x - from.x, to_y - from.y]
1719    } else {
1720        point
1721    };
1722    let id = exec_state.next_uuid();
1723    exec_state
1724        .batch_modeling_cmd(ModelingCmdMeta::from_args_id(&args, id), tan_arc_to(&sketch, delta))
1725        .await?;
1726
1727    let current_path = Path::TangentialArcTo {
1728        base: BasePath {
1729            from: from.ignore_units(),
1730            to,
1731            tag: tag.clone(),
1732            units: sketch.units,
1733            geo_meta: GeoMeta {
1734                id,
1735                metadata: args.source_range.into(),
1736            },
1737        },
1738        center: result.center,
1739        ccw: result.ccw > 0,
1740    };
1741
1742    let mut new_sketch = sketch;
1743    if let Some(tag) = &tag {
1744        new_sketch.add_tag(tag, &current_path, exec_state, None);
1745    }
1746
1747    new_sketch.paths.push(current_path);
1748
1749    Ok(new_sketch)
1750}
1751
1752/// Draw a bezier curve.
1753pub async fn bezier_curve(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1754    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1755    let control1 = args.get_kw_arg_opt("control1", &RuntimeType::point2d(), exec_state)?;
1756    let control2 = args.get_kw_arg_opt("control2", &RuntimeType::point2d(), exec_state)?;
1757    let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
1758    let control1_absolute = args.get_kw_arg_opt("control1Absolute", &RuntimeType::point2d(), exec_state)?;
1759    let control2_absolute = args.get_kw_arg_opt("control2Absolute", &RuntimeType::point2d(), exec_state)?;
1760    let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
1761    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
1762
1763    let new_sketch = inner_bezier_curve(
1764        sketch,
1765        control1,
1766        control2,
1767        end,
1768        control1_absolute,
1769        control2_absolute,
1770        end_absolute,
1771        tag,
1772        exec_state,
1773        args,
1774    )
1775    .await?;
1776    Ok(KclValue::Sketch {
1777        value: Box::new(new_sketch),
1778    })
1779}
1780
1781#[allow(clippy::too_many_arguments)]
1782async fn inner_bezier_curve(
1783    sketch: Sketch,
1784    control1: Option<[TyF64; 2]>,
1785    control2: Option<[TyF64; 2]>,
1786    end: Option<[TyF64; 2]>,
1787    control1_absolute: Option<[TyF64; 2]>,
1788    control2_absolute: Option<[TyF64; 2]>,
1789    end_absolute: Option<[TyF64; 2]>,
1790    tag: Option<TagNode>,
1791    exec_state: &mut ExecState,
1792    args: Args,
1793) -> Result<Sketch, KclError> {
1794    let from = sketch.current_pen_position()?;
1795    let id = exec_state.next_uuid();
1796
1797    let to = match (
1798        control1,
1799        control2,
1800        end,
1801        control1_absolute,
1802        control2_absolute,
1803        end_absolute,
1804    ) {
1805        // Relative
1806        (Some(control1), Some(control2), Some(end), None, None, None) => {
1807            let delta = end.clone();
1808            let to = [
1809                from.x + end[0].to_length_units(from.units),
1810                from.y + end[1].to_length_units(from.units),
1811            ];
1812
1813            exec_state
1814                .batch_modeling_cmd(
1815                    ModelingCmdMeta::from_args_id(&args, id),
1816                    ModelingCmd::from(mcmd::ExtendPath {
1817                        label: Default::default(),
1818                        path: sketch.id.into(),
1819                        segment: PathSegment::Bezier {
1820                            control1: KPoint2d::from(point_to_mm(control1)).with_z(0.0).map(LengthUnit),
1821                            control2: KPoint2d::from(point_to_mm(control2)).with_z(0.0).map(LengthUnit),
1822                            end: KPoint2d::from(point_to_mm(delta)).with_z(0.0).map(LengthUnit),
1823                            relative: true,
1824                        },
1825                    }),
1826                )
1827                .await?;
1828            to
1829        }
1830        // Absolute
1831        (None, None, None, Some(control1), Some(control2), Some(end)) => {
1832            let to = [end[0].to_length_units(from.units), end[1].to_length_units(from.units)];
1833            exec_state
1834                .batch_modeling_cmd(
1835                    ModelingCmdMeta::from_args_id(&args, id),
1836                    ModelingCmd::from(mcmd::ExtendPath {
1837                        label: Default::default(),
1838                        path: sketch.id.into(),
1839                        segment: PathSegment::Bezier {
1840                            control1: KPoint2d::from(point_to_mm(control1)).with_z(0.0).map(LengthUnit),
1841                            control2: KPoint2d::from(point_to_mm(control2)).with_z(0.0).map(LengthUnit),
1842                            end: KPoint2d::from(point_to_mm(end)).with_z(0.0).map(LengthUnit),
1843                            relative: false,
1844                        },
1845                    }),
1846                )
1847                .await?;
1848            to
1849        }
1850        _ => {
1851            return Err(KclError::new_semantic(KclErrorDetails::new(
1852                "You must either give `control1`, `control2` and `end`, or `control1Absolute`, `control2Absolute` and `endAbsolute`.".to_owned(),
1853                vec![args.source_range],
1854            )));
1855        }
1856    };
1857
1858    let current_path = Path::ToPoint {
1859        base: BasePath {
1860            from: from.ignore_units(),
1861            to,
1862            tag: tag.clone(),
1863            units: sketch.units,
1864            geo_meta: GeoMeta {
1865                id,
1866                metadata: args.source_range.into(),
1867            },
1868        },
1869    };
1870
1871    let mut new_sketch = sketch;
1872    if let Some(tag) = &tag {
1873        new_sketch.add_tag(tag, &current_path, exec_state, None);
1874    }
1875
1876    new_sketch.paths.push(current_path);
1877
1878    Ok(new_sketch)
1879}
1880
1881/// Use a sketch to cut a hole in another sketch.
1882pub async fn subtract_2d(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1883    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1884
1885    let tool: Vec<Sketch> = args.get_kw_arg(
1886        "tool",
1887        &RuntimeType::Array(
1888            Box::new(RuntimeType::Primitive(PrimitiveType::Sketch)),
1889            ArrayLen::Minimum(1),
1890        ),
1891        exec_state,
1892    )?;
1893
1894    let new_sketch = inner_subtract_2d(sketch, tool, exec_state, args).await?;
1895    Ok(KclValue::Sketch {
1896        value: Box::new(new_sketch),
1897    })
1898}
1899
1900async fn inner_subtract_2d(
1901    mut sketch: Sketch,
1902    tool: Vec<Sketch>,
1903    exec_state: &mut ExecState,
1904    args: Args,
1905) -> Result<Sketch, KclError> {
1906    for hole_sketch in tool {
1907        exec_state
1908            .batch_modeling_cmd(
1909                ModelingCmdMeta::from(&args),
1910                ModelingCmd::from(mcmd::Solid2dAddHole {
1911                    object_id: sketch.id,
1912                    hole_id: hole_sketch.id,
1913                }),
1914            )
1915            .await?;
1916
1917        // Hide the source hole since it's no longer its own profile,
1918        // it's just used to modify some other profile.
1919        exec_state
1920            .batch_modeling_cmd(
1921                ModelingCmdMeta::from(&args),
1922                ModelingCmd::from(mcmd::ObjectVisible {
1923                    object_id: hole_sketch.id,
1924                    hidden: true,
1925                }),
1926            )
1927            .await?;
1928
1929        // NOTE: We don't look at the inner paths of the hole/tool sketch.
1930        // So if you have circle A, and it has a circular hole cut out (B),
1931        // then you cut A out of an even bigger circle C, we will lose that info.
1932        // Not really sure what to do about this.
1933        sketch.inner_paths.extend_from_slice(&hole_sketch.paths);
1934    }
1935
1936    // Returns the input sketch, exactly as it was, zero modifications.
1937    // This means the edges from `tool` are basically ignored, they're not in the output.
1938    Ok(sketch)
1939}
1940
1941/// Calculate the (x, y) point on an ellipse given x or y and the major/minor radii of the ellipse.
1942pub async fn elliptic_point(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1943    let x = args.get_kw_arg_opt("x", &RuntimeType::length(), exec_state)?;
1944    let y = args.get_kw_arg_opt("y", &RuntimeType::length(), exec_state)?;
1945    let major_radius = args.get_kw_arg("majorRadius", &RuntimeType::num_any(), exec_state)?;
1946    let minor_radius = args.get_kw_arg("minorRadius", &RuntimeType::num_any(), exec_state)?;
1947
1948    let elliptic_point = inner_elliptic_point(x, y, major_radius, minor_radius, &args).await?;
1949
1950    args.make_kcl_val_from_point(elliptic_point, exec_state.length_unit().into())
1951}
1952
1953async fn inner_elliptic_point(
1954    x: Option<TyF64>,
1955    y: Option<TyF64>,
1956    major_radius: TyF64,
1957    minor_radius: TyF64,
1958    args: &Args,
1959) -> Result<[f64; 2], KclError> {
1960    let major_radius = major_radius.n;
1961    let minor_radius = minor_radius.n;
1962    if let Some(x) = x {
1963        if x.n.abs() > major_radius {
1964            Err(KclError::Type {
1965                details: KclErrorDetails::new(
1966                    format!(
1967                        "Invalid input. The x value, {}, cannot be larger than the major radius {}.",
1968                        x.n, major_radius
1969                    )
1970                    .to_owned(),
1971                    vec![args.source_range],
1972                ),
1973            })
1974        } else {
1975            Ok((
1976                x.n,
1977                minor_radius * (1.0 - x.n.powf(2.0) / major_radius.powf(2.0)).sqrt(),
1978            )
1979                .into())
1980        }
1981    } else if let Some(y) = y {
1982        if y.n > minor_radius {
1983            Err(KclError::Type {
1984                details: KclErrorDetails::new(
1985                    format!(
1986                        "Invalid input. The y value, {}, cannot be larger than the minor radius {}.",
1987                        y.n, minor_radius
1988                    )
1989                    .to_owned(),
1990                    vec![args.source_range],
1991                ),
1992            })
1993        } else {
1994            Ok((
1995                major_radius * (1.0 - y.n.powf(2.0) / minor_radius.powf(2.0)).sqrt(),
1996                y.n,
1997            )
1998                .into())
1999        }
2000    } else {
2001        Err(KclError::Type {
2002            details: KclErrorDetails::new(
2003                "Invalid input. Must have either x or y, you cannot have both or neither.".to_owned(),
2004                vec![args.source_range],
2005            ),
2006        })
2007    }
2008}
2009
2010/// Draw an elliptical arc.
2011pub async fn elliptic(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2012    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
2013
2014    let center = args.get_kw_arg("center", &RuntimeType::point2d(), exec_state)?;
2015    let angle_start = args.get_kw_arg("angleStart", &RuntimeType::degrees(), exec_state)?;
2016    let angle_end = args.get_kw_arg("angleEnd", &RuntimeType::degrees(), exec_state)?;
2017    let major_radius = args.get_kw_arg_opt("majorRadius", &RuntimeType::length(), exec_state)?;
2018    let major_axis = args.get_kw_arg_opt("majorAxis", &RuntimeType::point2d(), exec_state)?;
2019    let minor_radius = args.get_kw_arg("minorRadius", &RuntimeType::length(), exec_state)?;
2020    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
2021
2022    let new_sketch = inner_elliptic(
2023        sketch,
2024        center,
2025        angle_start,
2026        angle_end,
2027        major_radius,
2028        major_axis,
2029        minor_radius,
2030        tag,
2031        exec_state,
2032        args,
2033    )
2034    .await?;
2035    Ok(KclValue::Sketch {
2036        value: Box::new(new_sketch),
2037    })
2038}
2039
2040#[allow(clippy::too_many_arguments)]
2041pub(crate) async fn inner_elliptic(
2042    sketch: Sketch,
2043    center: [TyF64; 2],
2044    angle_start: TyF64,
2045    angle_end: TyF64,
2046    major_radius: Option<TyF64>,
2047    major_axis: Option<[TyF64; 2]>,
2048    minor_radius: TyF64,
2049    tag: Option<TagNode>,
2050    exec_state: &mut ExecState,
2051    args: Args,
2052) -> Result<Sketch, KclError> {
2053    let from: Point2d = sketch.current_pen_position()?;
2054    let id = exec_state.next_uuid();
2055
2056    let (center_u, _) = untype_point(center);
2057
2058    let major_axis = match (major_axis, major_radius) {
2059        (Some(_), Some(_)) | (None, None) => {
2060            return Err(KclError::new_type(KclErrorDetails::new(
2061                "Provide either `majorAxis` or `majorRadius`.".to_string(),
2062                vec![args.source_range],
2063            )));
2064        }
2065        (Some(major_axis), None) => major_axis,
2066        (None, Some(major_radius)) => [
2067            major_radius.clone(),
2068            TyF64 {
2069                n: 0.0,
2070                ty: major_radius.ty,
2071            },
2072        ],
2073    };
2074    let start_angle = Angle::from_degrees(angle_start.to_degrees(exec_state, args.source_range));
2075    let end_angle = Angle::from_degrees(angle_end.to_degrees(exec_state, args.source_range));
2076    let major_axis_magnitude = (major_axis[0].to_length_units(from.units) * major_axis[0].to_length_units(from.units)
2077        + major_axis[1].to_length_units(from.units) * major_axis[1].to_length_units(from.units))
2078    .sqrt();
2079    let to = [
2080        major_axis_magnitude * libm::cos(end_angle.to_radians()),
2081        minor_radius.to_length_units(from.units) * libm::sin(end_angle.to_radians()),
2082    ];
2083    let major_axis_angle = libm::atan2(major_axis[1].n, major_axis[0].n);
2084
2085    let point = [
2086        center_u[0] + to[0] * libm::cos(major_axis_angle) - to[1] * libm::sin(major_axis_angle),
2087        center_u[1] + to[0] * libm::sin(major_axis_angle) + to[1] * libm::cos(major_axis_angle),
2088    ];
2089
2090    let axis = major_axis.map(|x| x.to_mm());
2091    exec_state
2092        .batch_modeling_cmd(
2093            ModelingCmdMeta::from_args_id(&args, id),
2094            ModelingCmd::from(mcmd::ExtendPath {
2095                label: Default::default(),
2096                path: sketch.id.into(),
2097                segment: PathSegment::Ellipse {
2098                    center: KPoint2d::from(untyped_point_to_mm(center_u, from.units)).map(LengthUnit),
2099                    major_axis: axis.map(LengthUnit).into(),
2100                    minor_radius: LengthUnit(minor_radius.to_mm()),
2101                    start_angle,
2102                    end_angle,
2103                },
2104            }),
2105        )
2106        .await?;
2107
2108    let current_path = Path::Ellipse {
2109        ccw: start_angle < end_angle,
2110        center: center_u,
2111        major_axis: axis,
2112        minor_radius: minor_radius.to_mm(),
2113        base: BasePath {
2114            from: from.ignore_units(),
2115            to: point,
2116            tag: tag.clone(),
2117            units: sketch.units,
2118            geo_meta: GeoMeta {
2119                id,
2120                metadata: args.source_range.into(),
2121            },
2122        },
2123    };
2124    let mut new_sketch = sketch;
2125    if let Some(tag) = &tag {
2126        new_sketch.add_tag(tag, &current_path, exec_state, None);
2127    }
2128
2129    new_sketch.paths.push(current_path);
2130
2131    Ok(new_sketch)
2132}
2133
2134/// Calculate the (x, y) point on an hyperbola given x or y and the semi major/minor of the ellipse.
2135pub async fn hyperbolic_point(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2136    let x = args.get_kw_arg_opt("x", &RuntimeType::length(), exec_state)?;
2137    let y = args.get_kw_arg_opt("y", &RuntimeType::length(), exec_state)?;
2138    let semi_major = args.get_kw_arg("semiMajor", &RuntimeType::num_any(), exec_state)?;
2139    let semi_minor = args.get_kw_arg("semiMinor", &RuntimeType::num_any(), exec_state)?;
2140
2141    let hyperbolic_point = inner_hyperbolic_point(x, y, semi_major, semi_minor, &args).await?;
2142
2143    args.make_kcl_val_from_point(hyperbolic_point, exec_state.length_unit().into())
2144}
2145
2146async fn inner_hyperbolic_point(
2147    x: Option<TyF64>,
2148    y: Option<TyF64>,
2149    semi_major: TyF64,
2150    semi_minor: TyF64,
2151    args: &Args,
2152) -> Result<[f64; 2], KclError> {
2153    let semi_major = semi_major.n;
2154    let semi_minor = semi_minor.n;
2155    if let Some(x) = x {
2156        if x.n.abs() < semi_major {
2157            Err(KclError::Type {
2158                details: KclErrorDetails::new(
2159                    format!(
2160                        "Invalid input. The x value, {}, cannot be less than the semi major value, {}.",
2161                        x.n, semi_major
2162                    )
2163                    .to_owned(),
2164                    vec![args.source_range],
2165                ),
2166            })
2167        } else {
2168            Ok((x.n, semi_minor * (x.n.powf(2.0) / semi_major.powf(2.0) - 1.0).sqrt()).into())
2169        }
2170    } else if let Some(y) = y {
2171        Ok((semi_major * (y.n.powf(2.0) / semi_minor.powf(2.0) + 1.0).sqrt(), y.n).into())
2172    } else {
2173        Err(KclError::Type {
2174            details: KclErrorDetails::new(
2175                "Invalid input. Must have either x or y, cannot have both or neither.".to_owned(),
2176                vec![args.source_range],
2177            ),
2178        })
2179    }
2180}
2181
2182/// Draw a hyperbolic arc.
2183pub async fn hyperbolic(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2184    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
2185
2186    let semi_major = args.get_kw_arg("semiMajor", &RuntimeType::length(), exec_state)?;
2187    let semi_minor = args.get_kw_arg("semiMinor", &RuntimeType::length(), exec_state)?;
2188    let interior = args.get_kw_arg_opt("interior", &RuntimeType::point2d(), exec_state)?;
2189    let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
2190    let interior_absolute = args.get_kw_arg_opt("interiorAbsolute", &RuntimeType::point2d(), exec_state)?;
2191    let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
2192    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
2193
2194    let new_sketch = inner_hyperbolic(
2195        sketch,
2196        semi_major,
2197        semi_minor,
2198        interior,
2199        end,
2200        interior_absolute,
2201        end_absolute,
2202        tag,
2203        exec_state,
2204        args,
2205    )
2206    .await?;
2207    Ok(KclValue::Sketch {
2208        value: Box::new(new_sketch),
2209    })
2210}
2211
2212/// Calculate the tangent of a hyperbolic given a point on the curve
2213fn hyperbolic_tangent(point: Point2d, semi_major: f64, semi_minor: f64) -> [f64; 2] {
2214    (point.y * semi_major.powf(2.0), point.x * semi_minor.powf(2.0)).into()
2215}
2216
2217#[allow(clippy::too_many_arguments)]
2218pub(crate) async fn inner_hyperbolic(
2219    sketch: Sketch,
2220    semi_major: TyF64,
2221    semi_minor: TyF64,
2222    interior: Option<[TyF64; 2]>,
2223    end: Option<[TyF64; 2]>,
2224    interior_absolute: Option<[TyF64; 2]>,
2225    end_absolute: Option<[TyF64; 2]>,
2226    tag: Option<TagNode>,
2227    exec_state: &mut ExecState,
2228    args: Args,
2229) -> Result<Sketch, KclError> {
2230    let from = sketch.current_pen_position()?;
2231    let id = exec_state.next_uuid();
2232
2233    let (interior, end, relative) = match (interior, end, interior_absolute, end_absolute) {
2234        (Some(interior), Some(end), None, None) => (interior, end, true),
2235        (None, None, Some(interior_absolute), Some(end_absolute)) => (interior_absolute, end_absolute, false),
2236        _ => return Err(KclError::Type {
2237            details: KclErrorDetails::new(
2238                "Invalid combination of arguments. Either provide (end, interior) or (endAbsolute, interiorAbsolute)"
2239                    .to_owned(),
2240                vec![args.source_range],
2241            ),
2242        }),
2243    };
2244
2245    let (interior, _) = untype_point(interior);
2246    let (end, _) = untype_point(end);
2247    let end_point = Point2d {
2248        x: end[0],
2249        y: end[1],
2250        units: from.units,
2251    };
2252
2253    let semi_major_u = semi_major.to_length_units(from.units);
2254    let semi_minor_u = semi_minor.to_length_units(from.units);
2255
2256    let start_tangent = hyperbolic_tangent(from, semi_major_u, semi_minor_u);
2257    let end_tangent = hyperbolic_tangent(end_point, semi_major_u, semi_minor_u);
2258
2259    exec_state
2260        .batch_modeling_cmd(
2261            ModelingCmdMeta::from_args_id(&args, id),
2262            ModelingCmd::from(mcmd::ExtendPath {
2263                label: Default::default(),
2264                path: sketch.id.into(),
2265                segment: PathSegment::ConicTo {
2266                    start_tangent: KPoint2d::from(untyped_point_to_mm(start_tangent, from.units)).map(LengthUnit),
2267                    end_tangent: KPoint2d::from(untyped_point_to_mm(end_tangent, from.units)).map(LengthUnit),
2268                    end: KPoint2d::from(untyped_point_to_mm(end, from.units)).map(LengthUnit),
2269                    interior: KPoint2d::from(untyped_point_to_mm(interior, from.units)).map(LengthUnit),
2270                    relative,
2271                },
2272            }),
2273        )
2274        .await?;
2275
2276    let current_path = Path::Conic {
2277        base: BasePath {
2278            from: from.ignore_units(),
2279            to: end,
2280            tag: tag.clone(),
2281            units: sketch.units,
2282            geo_meta: GeoMeta {
2283                id,
2284                metadata: args.source_range.into(),
2285            },
2286        },
2287    };
2288
2289    let mut new_sketch = sketch;
2290    if let Some(tag) = &tag {
2291        new_sketch.add_tag(tag, &current_path, exec_state, None);
2292    }
2293
2294    new_sketch.paths.push(current_path);
2295
2296    Ok(new_sketch)
2297}
2298
2299/// Calculate the point on a parabola given the coefficient of the parabola and either x or y
2300pub async fn parabolic_point(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2301    let x = args.get_kw_arg_opt("x", &RuntimeType::length(), exec_state)?;
2302    let y = args.get_kw_arg_opt("y", &RuntimeType::length(), exec_state)?;
2303    let coefficients = args.get_kw_arg(
2304        "coefficients",
2305        &RuntimeType::Array(Box::new(RuntimeType::num_any()), ArrayLen::Known(3)),
2306        exec_state,
2307    )?;
2308
2309    let parabolic_point = inner_parabolic_point(x, y, &coefficients, &args).await?;
2310
2311    args.make_kcl_val_from_point(parabolic_point, exec_state.length_unit().into())
2312}
2313
2314async fn inner_parabolic_point(
2315    x: Option<TyF64>,
2316    y: Option<TyF64>,
2317    coefficients: &[TyF64; 3],
2318    args: &Args,
2319) -> Result<[f64; 2], KclError> {
2320    let a = coefficients[0].n;
2321    let b = coefficients[1].n;
2322    let c = coefficients[2].n;
2323    if let Some(x) = x {
2324        Ok((x.n, a * x.n.powf(2.0) + b * x.n + c).into())
2325    } else if let Some(y) = y {
2326        let det = (b.powf(2.0) - 4.0 * a * (c - y.n)).sqrt();
2327        Ok(((-b + det) / (2.0 * a), y.n).into())
2328    } else {
2329        Err(KclError::Type {
2330            details: KclErrorDetails::new(
2331                "Invalid input. Must have either x or y, cannot have both or neither.".to_owned(),
2332                vec![args.source_range],
2333            ),
2334        })
2335    }
2336}
2337
2338/// Draw a parabolic arc.
2339pub async fn parabolic(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2340    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
2341
2342    let coefficients = args.get_kw_arg_opt(
2343        "coefficients",
2344        &RuntimeType::Array(Box::new(RuntimeType::num_any()), ArrayLen::Known(3)),
2345        exec_state,
2346    )?;
2347    let interior = args.get_kw_arg_opt("interior", &RuntimeType::point2d(), exec_state)?;
2348    let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
2349    let interior_absolute = args.get_kw_arg_opt("interiorAbsolute", &RuntimeType::point2d(), exec_state)?;
2350    let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
2351    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
2352
2353    let new_sketch = inner_parabolic(
2354        sketch,
2355        coefficients,
2356        interior,
2357        end,
2358        interior_absolute,
2359        end_absolute,
2360        tag,
2361        exec_state,
2362        args,
2363    )
2364    .await?;
2365    Ok(KclValue::Sketch {
2366        value: Box::new(new_sketch),
2367    })
2368}
2369
2370fn parabolic_tangent(point: Point2d, a: f64, b: f64) -> [f64; 2] {
2371    //f(x) = ax^2 + bx + c
2372    //f'(x) = 2ax + b
2373    (1.0, 2.0 * a * point.x + b).into()
2374}
2375
2376#[allow(clippy::too_many_arguments)]
2377pub(crate) async fn inner_parabolic(
2378    sketch: Sketch,
2379    coefficients: Option<[TyF64; 3]>,
2380    interior: Option<[TyF64; 2]>,
2381    end: Option<[TyF64; 2]>,
2382    interior_absolute: Option<[TyF64; 2]>,
2383    end_absolute: Option<[TyF64; 2]>,
2384    tag: Option<TagNode>,
2385    exec_state: &mut ExecState,
2386    args: Args,
2387) -> Result<Sketch, KclError> {
2388    let from = sketch.current_pen_position()?;
2389    let id = exec_state.next_uuid();
2390
2391    if (coefficients.is_some() && interior.is_some()) || (coefficients.is_none() && interior.is_none()) {
2392        return Err(KclError::Type {
2393            details: KclErrorDetails::new(
2394                "Invalid combination of arguments. Either provide (a, b, c) or (interior)".to_owned(),
2395                vec![args.source_range],
2396            ),
2397        });
2398    }
2399
2400    let (interior, end, relative) = match (coefficients.clone(), interior, end, interior_absolute, end_absolute) {
2401        (None, Some(interior), Some(end), None, None) => {
2402            let (interior, _) = untype_point(interior);
2403            let (end, _) = untype_point(end);
2404            (interior,end, true)
2405        },
2406        (None, None, None, Some(interior_absolute), Some(end_absolute)) => {
2407            let (interior_absolute, _) = untype_point(interior_absolute);
2408            let (end_absolute, _) = untype_point(end_absolute);
2409            (interior_absolute, end_absolute, false)
2410        }
2411        (Some(coefficients), _, Some(end), _, _) => {
2412            let (end, _) = untype_point(end);
2413            let interior =
2414            inner_parabolic_point(
2415                Some(TyF64::count(0.5 * (from.x + end[0]))),
2416                None,
2417                &coefficients,
2418                &args,
2419            )
2420            .await?;
2421            (interior, end, true)
2422        }
2423        (Some(coefficients), _, _, _, Some(end)) => {
2424            let (end, _) = untype_point(end);
2425            let interior =
2426            inner_parabolic_point(
2427                Some(TyF64::count(0.5 * (from.x + end[0]))),
2428                None,
2429                &coefficients,
2430                &args,
2431            )
2432            .await?;
2433            (interior, end, false)
2434        }
2435        _ => return
2436            Err(KclError::Type{details: KclErrorDetails::new(
2437                "Invalid combination of arguments. Either provide (end, interior) or (endAbsolute, interiorAbsolute) if coefficients are not provided."
2438                    .to_owned(),
2439                vec![args.source_range],
2440            )}),
2441    };
2442
2443    let end_point = Point2d {
2444        x: end[0],
2445        y: end[1],
2446        units: from.units,
2447    };
2448
2449    let (a, b, _c) = if let Some([a, b, c]) = coefficients {
2450        (a.n, b.n, c.n)
2451    } else {
2452        // Any three points is enough to uniquely define a parabola
2453        let denom = (from.x - interior[0]) * (from.x - end_point.x) * (interior[0] - end_point.x);
2454        let a = (end_point.x * (interior[1] - from.y)
2455            + interior[0] * (from.y - end_point.y)
2456            + from.x * (end_point.y - interior[1]))
2457            / denom;
2458        let b = (end_point.x.powf(2.0) * (from.y - interior[1])
2459            + interior[0].powf(2.0) * (end_point.y - from.y)
2460            + from.x.powf(2.0) * (interior[1] - end_point.y))
2461            / denom;
2462        let c = (interior[0] * end_point.x * (interior[0] - end_point.x) * from.y
2463            + end_point.x * from.x * (end_point.x - from.x) * interior[1]
2464            + from.x * interior[0] * (from.x - interior[0]) * end_point.y)
2465            / denom;
2466
2467        (a, b, c)
2468    };
2469
2470    let start_tangent = parabolic_tangent(from, a, b);
2471    let end_tangent = parabolic_tangent(end_point, a, b);
2472
2473    exec_state
2474        .batch_modeling_cmd(
2475            ModelingCmdMeta::from_args_id(&args, id),
2476            ModelingCmd::from(mcmd::ExtendPath {
2477                label: Default::default(),
2478                path: sketch.id.into(),
2479                segment: PathSegment::ConicTo {
2480                    start_tangent: KPoint2d::from(untyped_point_to_mm(start_tangent, from.units)).map(LengthUnit),
2481                    end_tangent: KPoint2d::from(untyped_point_to_mm(end_tangent, from.units)).map(LengthUnit),
2482                    end: KPoint2d::from(untyped_point_to_mm(end, from.units)).map(LengthUnit),
2483                    interior: KPoint2d::from(untyped_point_to_mm(interior, from.units)).map(LengthUnit),
2484                    relative,
2485                },
2486            }),
2487        )
2488        .await?;
2489
2490    let current_path = Path::Conic {
2491        base: BasePath {
2492            from: from.ignore_units(),
2493            to: end,
2494            tag: tag.clone(),
2495            units: sketch.units,
2496            geo_meta: GeoMeta {
2497                id,
2498                metadata: args.source_range.into(),
2499            },
2500        },
2501    };
2502
2503    let mut new_sketch = sketch;
2504    if let Some(tag) = &tag {
2505        new_sketch.add_tag(tag, &current_path, exec_state, None);
2506    }
2507
2508    new_sketch.paths.push(current_path);
2509
2510    Ok(new_sketch)
2511}
2512
2513fn conic_tangent(coefficients: [f64; 6], point: [f64; 2]) -> [f64; 2] {
2514    let [a, b, c, d, e, _] = coefficients;
2515
2516    (
2517        c * point[0] + 2.0 * b * point[1] + e,
2518        -(2.0 * a * point[0] + c * point[1] + d),
2519    )
2520        .into()
2521}
2522
2523/// Draw a conic section
2524pub async fn conic(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2525    let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
2526
2527    let start_tangent = args.get_kw_arg_opt("startTangent", &RuntimeType::point2d(), exec_state)?;
2528    let end_tangent = args.get_kw_arg_opt("endTangent", &RuntimeType::point2d(), exec_state)?;
2529    let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
2530    let interior = args.get_kw_arg_opt("interior", &RuntimeType::point2d(), exec_state)?;
2531    let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
2532    let interior_absolute = args.get_kw_arg_opt("interiorAbsolute", &RuntimeType::point2d(), exec_state)?;
2533    let coefficients = args.get_kw_arg_opt(
2534        "coefficients",
2535        &RuntimeType::Array(Box::new(RuntimeType::num_any()), ArrayLen::Known(6)),
2536        exec_state,
2537    )?;
2538    let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
2539
2540    let new_sketch = inner_conic(
2541        sketch,
2542        start_tangent,
2543        end,
2544        end_tangent,
2545        interior,
2546        coefficients,
2547        interior_absolute,
2548        end_absolute,
2549        tag,
2550        exec_state,
2551        args,
2552    )
2553    .await?;
2554    Ok(KclValue::Sketch {
2555        value: Box::new(new_sketch),
2556    })
2557}
2558
2559#[allow(clippy::too_many_arguments)]
2560pub(crate) async fn inner_conic(
2561    sketch: Sketch,
2562    start_tangent: Option<[TyF64; 2]>,
2563    end: Option<[TyF64; 2]>,
2564    end_tangent: Option<[TyF64; 2]>,
2565    interior: Option<[TyF64; 2]>,
2566    coefficients: Option<[TyF64; 6]>,
2567    interior_absolute: Option<[TyF64; 2]>,
2568    end_absolute: Option<[TyF64; 2]>,
2569    tag: Option<TagNode>,
2570    exec_state: &mut ExecState,
2571    args: Args,
2572) -> Result<Sketch, KclError> {
2573    let from: Point2d = sketch.current_pen_position()?;
2574    let id = exec_state.next_uuid();
2575
2576    if (coefficients.is_some() && (start_tangent.is_some() || end_tangent.is_some()))
2577        || (coefficients.is_none() && (start_tangent.is_none() && end_tangent.is_none()))
2578    {
2579        return Err(KclError::Type {
2580            details: KclErrorDetails::new(
2581                "Invalid combination of arguments. Either provide coefficients or (startTangent, endTangent)"
2582                    .to_owned(),
2583                vec![args.source_range],
2584            ),
2585        });
2586    }
2587
2588    let (interior, end, relative) = match (interior, end, interior_absolute, end_absolute) {
2589        (Some(interior), Some(end), None, None) => (interior, end, true),
2590        (None, None, Some(interior_absolute), Some(end_absolute)) => (interior_absolute, end_absolute, false),
2591        _ => return Err(KclError::Type {
2592            details: KclErrorDetails::new(
2593                "Invalid combination of arguments. Either provide (end, interior) or (endAbsolute, interiorAbsolute)"
2594                    .to_owned(),
2595                vec![args.source_range],
2596            ),
2597        }),
2598    };
2599
2600    let (end, _) = untype_array(end);
2601    let (interior, _) = untype_point(interior);
2602
2603    let (start_tangent, end_tangent) = if let Some(coeffs) = coefficients {
2604        let (coeffs, _) = untype_array(coeffs);
2605        (conic_tangent(coeffs, [from.x, from.y]), conic_tangent(coeffs, end))
2606    } else {
2607        let start = if let Some(start_tangent) = start_tangent {
2608            let (start, _) = untype_point(start_tangent);
2609            start
2610        } else {
2611            let previous_point = sketch
2612                .get_tangential_info_from_paths()
2613                .tan_previous_point(from.ignore_units());
2614            let from = from.ignore_units();
2615            [from[0] - previous_point[0], from[1] - previous_point[1]]
2616        };
2617
2618        let Some(end_tangent) = end_tangent else {
2619            return Err(KclError::new_semantic(KclErrorDetails::new(
2620                "You must either provide either `coefficients` or `endTangent`.".to_owned(),
2621                vec![args.source_range],
2622            )));
2623        };
2624        let (end_tan, _) = untype_point(end_tangent);
2625        (start, end_tan)
2626    };
2627
2628    exec_state
2629        .batch_modeling_cmd(
2630            ModelingCmdMeta::from_args_id(&args, id),
2631            ModelingCmd::from(mcmd::ExtendPath {
2632                label: Default::default(),
2633                path: sketch.id.into(),
2634                segment: PathSegment::ConicTo {
2635                    start_tangent: KPoint2d::from(untyped_point_to_mm(start_tangent, from.units)).map(LengthUnit),
2636                    end_tangent: KPoint2d::from(untyped_point_to_mm(end_tangent, from.units)).map(LengthUnit),
2637                    end: KPoint2d::from(untyped_point_to_mm(end, from.units)).map(LengthUnit),
2638                    interior: KPoint2d::from(untyped_point_to_mm(interior, from.units)).map(LengthUnit),
2639                    relative,
2640                },
2641            }),
2642        )
2643        .await?;
2644
2645    let current_path = Path::Conic {
2646        base: BasePath {
2647            from: from.ignore_units(),
2648            to: end,
2649            tag: tag.clone(),
2650            units: sketch.units,
2651            geo_meta: GeoMeta {
2652                id,
2653                metadata: args.source_range.into(),
2654            },
2655        },
2656    };
2657
2658    let mut new_sketch = sketch;
2659    if let Some(tag) = &tag {
2660        new_sketch.add_tag(tag, &current_path, exec_state, None);
2661    }
2662
2663    new_sketch.paths.push(current_path);
2664
2665    Ok(new_sketch)
2666}
2667#[cfg(test)]
2668mod tests {
2669
2670    use pretty_assertions::assert_eq;
2671
2672    use crate::{
2673        execution::TagIdentifier,
2674        std::{sketch::PlaneData, utils::calculate_circle_center},
2675    };
2676
2677    #[test]
2678    fn test_deserialize_plane_data() {
2679        let data = PlaneData::XY;
2680        let mut str_json = serde_json::to_string(&data).unwrap();
2681        assert_eq!(str_json, "\"XY\"");
2682
2683        str_json = "\"YZ\"".to_string();
2684        let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2685        assert_eq!(data, PlaneData::YZ);
2686
2687        str_json = "\"-YZ\"".to_string();
2688        let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2689        assert_eq!(data, PlaneData::NegYZ);
2690
2691        str_json = "\"-xz\"".to_string();
2692        let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2693        assert_eq!(data, PlaneData::NegXZ);
2694    }
2695
2696    #[test]
2697    fn test_deserialize_sketch_on_face_tag() {
2698        let data = "start";
2699        let mut str_json = serde_json::to_string(&data).unwrap();
2700        assert_eq!(str_json, "\"start\"");
2701
2702        str_json = "\"end\"".to_string();
2703        let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2704        assert_eq!(
2705            data,
2706            crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
2707        );
2708
2709        str_json = serde_json::to_string(&TagIdentifier {
2710            value: "thing".to_string(),
2711            info: Vec::new(),
2712            meta: Default::default(),
2713        })
2714        .unwrap();
2715        let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2716        assert_eq!(
2717            data,
2718            crate::std::sketch::FaceTag::Tag(Box::new(TagIdentifier {
2719                value: "thing".to_string(),
2720                info: Vec::new(),
2721                meta: Default::default()
2722            }))
2723        );
2724
2725        str_json = "\"END\"".to_string();
2726        let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2727        assert_eq!(
2728            data,
2729            crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
2730        );
2731
2732        str_json = "\"start\"".to_string();
2733        let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2734        assert_eq!(
2735            data,
2736            crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
2737        );
2738
2739        str_json = "\"START\"".to_string();
2740        let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2741        assert_eq!(
2742            data,
2743            crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
2744        );
2745    }
2746
2747    #[test]
2748    fn test_circle_center() {
2749        let actual = calculate_circle_center([0.0, 0.0], [5.0, 5.0], [10.0, 0.0]);
2750        assert_eq!(actual[0], 5.0);
2751        assert_eq!(actual[1], 0.0);
2752    }
2753}