kcl_lib/std/
sketch.rs

1//! Functions related to sketching.
2
3use anyhow::Result;
4use derive_docs::stdlib;
5use indexmap::IndexMap;
6use kcmc::shared::Point2d as KPoint2d; // Point2d is already defined in this pkg, to impl ts_rs traits.
7use kcmc::{each_cmd as mcmd, length_unit::LengthUnit, shared::Angle, ModelingCmd};
8use kittycad_modeling_cmds as kcmc;
9use kittycad_modeling_cmds::shared::PathSegment;
10use parse_display::{Display, FromStr};
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13
14use crate::execution::{CodeRef, StartSketchOnFace, StartSketchOnPlane};
15use crate::{
16    errors::{KclError, KclErrorDetails},
17    execution::{
18        Artifact, ArtifactId, BasePath, ExecState, Face, GeoMeta, KclValue, Path, Plane, Point2d, Point3d, Sketch,
19        SketchSet, SketchSurface, Solid, TagEngineInfo, TagIdentifier,
20    },
21    parsing::ast::types::TagNode,
22    std::{
23        args::{Args, TyF64},
24        utils::{
25            arc_angles, arc_center_and_end, calculate_circle_center, get_tangential_arc_to_info, get_x_component,
26            get_y_component, intersection_with_parallel_line, TangentialArcInfoInput,
27        },
28    },
29};
30
31/// A tag for a face.
32#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
33#[ts(export)]
34#[serde(rename_all = "snake_case", untagged)]
35pub enum FaceTag {
36    StartOrEnd(StartOrEnd),
37    /// A tag for the face.
38    Tag(Box<TagIdentifier>),
39}
40
41impl std::fmt::Display for FaceTag {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match self {
44            FaceTag::Tag(t) => write!(f, "{}", t),
45            FaceTag::StartOrEnd(StartOrEnd::Start) => write!(f, "start"),
46            FaceTag::StartOrEnd(StartOrEnd::End) => write!(f, "end"),
47        }
48    }
49}
50
51impl FaceTag {
52    /// Get the face id from the tag.
53    pub async fn get_face_id(
54        &self,
55        solid: &Solid,
56        exec_state: &mut ExecState,
57        args: &Args,
58        must_be_planar: bool,
59    ) -> Result<uuid::Uuid, KclError> {
60        match self {
61            FaceTag::Tag(ref t) => args.get_adjacent_face_to_tag(exec_state, t, must_be_planar).await,
62            FaceTag::StartOrEnd(StartOrEnd::Start) => solid.start_cap_id.ok_or_else(|| {
63                KclError::Type(KclErrorDetails {
64                    message: "Expected a start face".to_string(),
65                    source_ranges: vec![args.source_range],
66                })
67            }),
68            FaceTag::StartOrEnd(StartOrEnd::End) => solid.end_cap_id.ok_or_else(|| {
69                KclError::Type(KclErrorDetails {
70                    message: "Expected an end face".to_string(),
71                    source_ranges: vec![args.source_range],
72                })
73            }),
74        }
75    }
76}
77
78#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display)]
79#[ts(export)]
80#[serde(rename_all = "snake_case")]
81#[display(style = "snake_case")]
82pub enum StartOrEnd {
83    /// The start face as in before you extruded. This could also be known as the bottom
84    /// face. But we do not call it bottom because it would be the top face if you
85    /// extruded it in the opposite direction or flipped the camera.
86    #[serde(rename = "start", alias = "START")]
87    Start,
88    /// The end face after you extruded. This could also be known as the top
89    /// face. But we do not call it top because it would be the bottom face if you
90    /// extruded it in the opposite direction or flipped the camera.
91    #[serde(rename = "end", alias = "END")]
92    End,
93}
94
95pub const NEW_TAG_KW: &str = "tag";
96
97/// Draw a line to a point.
98pub async fn line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
99    // let (to, sketch, tag): ([f64; 2], Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
100    let sketch = args.get_unlabeled_kw_arg("sketch")?;
101    let end = args.get_kw_arg_opt("end")?;
102    let end_absolute = args.get_kw_arg_opt("endAbsolute")?;
103    let tag = args.get_kw_arg_opt(NEW_TAG_KW)?;
104
105    let new_sketch = inner_line(sketch, end_absolute, end, tag, exec_state, args).await?;
106    Ok(KclValue::Sketch {
107        value: Box::new(new_sketch),
108    })
109}
110
111/// Extend the current sketch with a new straight line.
112///
113/// ```no_run
114/// triangle = startSketchOn(XZ)
115///   |> startProfileAt([0, 0], %)
116///   // The 'end' argument means it ends at exactly [10, 0].
117///   // This is an absolute measurement, it is NOT relative to
118///   // the start of the sketch.
119///   |> line(endAbsolute = [10, 0])
120///   |> line(endAbsolute = [0, 10])
121///   |> line(endAbsolute = [-10, 0], tag = $thirdLineOfTriangle)
122///   |> close()
123///   |> extrude(length = 5)
124///
125/// box = startSketchOn(XZ)
126///   |> startProfileAt([10, 10], %)
127///   // The 'to' argument means move the pen this much.
128///   // So, [10, 0] is a relative distance away from the current point.
129///   |> line(end = [10, 0])
130///   |> line(end = [0, 10])
131///   |> line(end = [-10, 0], tag = $thirdLineOfBox)
132///   |> close()
133///   |> extrude(length = 5)
134/// ```
135#[stdlib {
136    name = "line",
137    keywords = true,
138    unlabeled_first = true,
139    args = {
140        sketch = { docs = "Which sketch should this path be added to?"},
141        end_absolute = { docs = "Which absolute point should this line go to? Incompatible with `end`."},
142        end = { docs = "How far away (along the X and Y axes) should this line go? Incompatible with `endAbsolute`.", include_in_snippet = true},
143        tag = { docs = "Create a new tag which refers to this line"},
144    }
145}]
146async fn inner_line(
147    sketch: Sketch,
148    end_absolute: Option<[f64; 2]>,
149    end: Option<[f64; 2]>,
150    tag: Option<TagNode>,
151    exec_state: &mut ExecState,
152    args: Args,
153) -> Result<Sketch, KclError> {
154    straight_line(
155        StraightLineParams {
156            sketch,
157            end_absolute,
158            end,
159            tag,
160        },
161        exec_state,
162        args,
163    )
164    .await
165}
166
167struct StraightLineParams {
168    sketch: Sketch,
169    end_absolute: Option<[f64; 2]>,
170    end: Option<[f64; 2]>,
171    tag: Option<TagNode>,
172}
173
174impl StraightLineParams {
175    fn relative(p: [f64; 2], sketch: Sketch, tag: Option<TagNode>) -> Self {
176        Self {
177            sketch,
178            tag,
179            end: Some(p),
180            end_absolute: None,
181        }
182    }
183    fn absolute(p: [f64; 2], sketch: Sketch, tag: Option<TagNode>) -> Self {
184        Self {
185            sketch,
186            tag,
187            end: None,
188            end_absolute: Some(p),
189        }
190    }
191}
192
193async fn straight_line(
194    StraightLineParams {
195        sketch,
196        end,
197        end_absolute,
198        tag,
199    }: StraightLineParams,
200    exec_state: &mut ExecState,
201    args: Args,
202) -> Result<Sketch, KclError> {
203    let from = sketch.current_pen_position()?;
204    let (point, is_absolute) = match (end_absolute, end) {
205        (Some(_), Some(_)) => {
206            return Err(KclError::Semantic(KclErrorDetails {
207                source_ranges: vec![args.source_range],
208                message: "You cannot give both `end` and `endAbsolute` params, you have to choose one or the other"
209                    .to_owned(),
210            }));
211        }
212        (Some(end_absolute), None) => (end_absolute, true),
213        (None, Some(end)) => (end, false),
214        (None, None) => {
215            return Err(KclError::Semantic(KclErrorDetails {
216                source_ranges: vec![args.source_range],
217                message: "You must supply either `end` or `endAbsolute` arguments".to_owned(),
218            }));
219        }
220    };
221
222    let id = exec_state.next_uuid();
223    args.batch_modeling_cmd(
224        id,
225        ModelingCmd::from(mcmd::ExtendPath {
226            path: sketch.id.into(),
227            segment: PathSegment::Line {
228                end: KPoint2d::from(point).with_z(0.0).map(LengthUnit),
229                relative: !is_absolute,
230            },
231        }),
232    )
233    .await?;
234    let end = if is_absolute {
235        point
236    } else {
237        let from = sketch.current_pen_position()?;
238        [from.x + point[0], from.y + point[1]]
239    };
240
241    let current_path = Path::ToPoint {
242        base: BasePath {
243            from: from.into(),
244            to: end,
245            tag: tag.clone(),
246            units: sketch.units,
247            geo_meta: GeoMeta {
248                id,
249                metadata: args.source_range.into(),
250            },
251        },
252    };
253
254    let mut new_sketch = sketch.clone();
255    if let Some(tag) = &tag {
256        new_sketch.add_tag(tag, &current_path);
257    }
258
259    new_sketch.paths.push(current_path);
260
261    Ok(new_sketch)
262}
263
264/// Draw a line to a point on the x-axis.
265pub async fn x_line_to(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
266    let (to, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
267
268    let new_sketch = inner_x_line_to(to, sketch, tag, exec_state, args).await?;
269    Ok(KclValue::Sketch {
270        value: Box::new(new_sketch),
271    })
272}
273
274/// Draw a line parallel to the X axis, that ends at the given X.
275/// E.g. if the previous line ended at (1, 1),
276/// then xLineTo(4) draws a line from (1, 1) to (4, 1)
277///
278/// ```no_run
279/// exampleSketch = startSketchOn(XZ)
280///   |> startProfileAt([0, 0], %)
281///   |> xLineTo(15, %)
282///   |> angledLine({
283///     angle = 80,
284///     length = 15,
285///   }, %)
286///   |> line(end = [8, -10])
287///   |> xLineTo(40, %)
288///   |> angledLine({
289///     angle = 135,
290///     length = 30,
291///   }, %)
292///   |> xLineTo(10, %)
293///   |> close()
294///
295/// example = extrude(exampleSketch, length = 10)
296/// ```
297#[stdlib {
298    name = "xLineTo",
299}]
300async fn inner_x_line_to(
301    to: f64,
302    sketch: Sketch,
303    tag: Option<TagNode>,
304    exec_state: &mut ExecState,
305    args: Args,
306) -> Result<Sketch, KclError> {
307    let from = sketch.current_pen_position()?;
308
309    let new_sketch = straight_line(
310        StraightLineParams::absolute([to, from.y], sketch, tag),
311        exec_state,
312        args,
313    )
314    .await?;
315
316    Ok(new_sketch)
317}
318
319/// Draw a line to a point on the y-axis.
320pub async fn y_line_to(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
321    let (to, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
322
323    let new_sketch = inner_y_line_to(to, sketch, tag, exec_state, args).await?;
324    Ok(KclValue::Sketch {
325        value: Box::new(new_sketch),
326    })
327}
328
329/// Draw a line parallel to the Y axis, that ends at the given Y.
330/// E.g. if the previous line ended at (1, 1),
331/// then yLineTo(4) draws a line from (1, 1) to (1, 4)
332///
333/// ```no_run
334/// exampleSketch = startSketchOn(XZ)
335///   |> startProfileAt([0, 0], %)
336///   |> angledLine({
337///     angle = 50,
338///     length = 45,
339///   }, %)
340///   |> yLineTo(0, %)
341///   |> close()
342///
343/// example = extrude(exampleSketch, length = 5)
344/// ```
345#[stdlib {
346    name = "yLineTo",
347}]
348async fn inner_y_line_to(
349    to: f64,
350    sketch: Sketch,
351    tag: Option<TagNode>,
352    exec_state: &mut ExecState,
353    args: Args,
354) -> Result<Sketch, KclError> {
355    let from = sketch.current_pen_position()?;
356
357    let new_sketch = straight_line(
358        StraightLineParams::absolute([from.x, to], sketch, tag),
359        exec_state,
360        args,
361    )
362    .await?;
363    Ok(new_sketch)
364}
365
366/// Draw a line on the x-axis.
367pub async fn x_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
368    let (length, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
369
370    let new_sketch = inner_x_line(length, sketch, tag, exec_state, args).await?;
371    Ok(KclValue::Sketch {
372        value: Box::new(new_sketch),
373    })
374}
375
376/// Draw a line relative to the current origin to a specified distance away
377/// from the current position along the 'x' axis.
378///
379/// ```no_run
380/// exampleSketch = startSketchOn(XZ)
381///   |> startProfileAt([0, 0], %)
382///   |> xLine(15, %)
383///   |> angledLine({
384///     angle = 80,
385///     length = 15,
386///   }, %)
387///   |> line(end = [8, -10])
388///   |> xLine(10, %)
389///   |> angledLine({
390///     angle = 120,
391///     length = 30,
392///   }, %)
393///   |> xLine(-15, %)
394///   |> close()
395///
396/// example = extrude(exampleSketch, length = 10)
397/// ```
398#[stdlib {
399    name = "xLine",
400}]
401async fn inner_x_line(
402    length: f64,
403    sketch: Sketch,
404    tag: Option<TagNode>,
405    exec_state: &mut ExecState,
406    args: Args,
407) -> Result<Sketch, KclError> {
408    straight_line(
409        StraightLineParams::relative([length, 0.0], sketch, tag),
410        exec_state,
411        args,
412    )
413    .await
414}
415
416/// Draw a line on the y-axis.
417pub async fn y_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
418    let (length, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
419
420    let new_sketch = inner_y_line(length, sketch, tag, exec_state, args).await?;
421    Ok(KclValue::Sketch {
422        value: Box::new(new_sketch),
423    })
424}
425
426/// Draw a line relative to the current origin to a specified distance away
427/// from the current position along the 'y' axis.
428///
429/// ```no_run
430/// exampleSketch = startSketchOn(XZ)
431///   |> startProfileAt([0, 0], %)
432///   |> yLine(15, %)
433///   |> angledLine({
434///     angle = 30,
435///     length = 15,
436///   }, %)
437///   |> line(end = [8, -10])
438///   |> yLine(-5, %)
439///   |> close()
440///
441/// example = extrude(exampleSketch, length = 10)
442/// ```
443#[stdlib {
444    name = "yLine",
445}]
446async fn inner_y_line(
447    length: f64,
448    sketch: Sketch,
449    tag: Option<TagNode>,
450    exec_state: &mut ExecState,
451    args: Args,
452) -> Result<Sketch, KclError> {
453    straight_line(
454        StraightLineParams::relative([0.0, length], sketch, tag),
455        exec_state,
456        args,
457    )
458    .await
459}
460
461/// Data to draw an angled line.
462#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
463#[ts(export)]
464#[serde(rename_all = "camelCase", untagged)]
465pub enum AngledLineData {
466    /// An angle and length with explicitly named parameters
467    AngleAndLengthNamed {
468        /// The angle of the line (in degrees).
469        angle: f64,
470        /// The length of the line.
471        length: f64,
472    },
473    /// An angle and length given as a pair
474    AngleAndLengthPair([f64; 2]),
475}
476
477/// Draw an angled line.
478pub async fn angled_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
479    let (data, sketch, tag): (AngledLineData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
480
481    let new_sketch = inner_angled_line(data, sketch, tag, exec_state, args).await?;
482    Ok(KclValue::Sketch {
483        value: Box::new(new_sketch),
484    })
485}
486
487/// Draw a line segment relative to the current origin using the polar
488/// measure of some angle and distance.
489///
490/// ```no_run
491/// exampleSketch = startSketchOn(XZ)
492///   |> startProfileAt([0, 0], %)
493///   |> yLineTo(15, %)
494///   |> angledLine({
495///     angle = 30,
496///     length = 15,
497///   }, %)
498///   |> line(end = [8, -10])
499///   |> yLineTo(0, %)
500///   |> close()
501///
502/// example = extrude(exampleSketch, length = 10)
503/// ```
504#[stdlib {
505    name = "angledLine",
506}]
507async fn inner_angled_line(
508    data: AngledLineData,
509    sketch: Sketch,
510    tag: Option<TagNode>,
511    exec_state: &mut ExecState,
512    args: Args,
513) -> Result<Sketch, KclError> {
514    let from = sketch.current_pen_position()?;
515    let (angle, length) = match data {
516        AngledLineData::AngleAndLengthNamed { angle, length } => (angle, length),
517        AngledLineData::AngleAndLengthPair(pair) => (pair[0], pair[1]),
518    };
519
520    //double check me on this one - mike
521    let delta: [f64; 2] = [
522        length * f64::cos(angle.to_radians()),
523        length * f64::sin(angle.to_radians()),
524    ];
525    let relative = true;
526
527    let to: [f64; 2] = [from.x + delta[0], from.y + delta[1]];
528
529    let id = exec_state.next_uuid();
530
531    args.batch_modeling_cmd(
532        id,
533        ModelingCmd::from(mcmd::ExtendPath {
534            path: sketch.id.into(),
535            segment: PathSegment::Line {
536                end: KPoint2d::from(delta).with_z(0.0).map(LengthUnit),
537                relative,
538            },
539        }),
540    )
541    .await?;
542
543    let current_path = Path::ToPoint {
544        base: BasePath {
545            from: from.into(),
546            to,
547            tag: tag.clone(),
548            units: sketch.units,
549            geo_meta: GeoMeta {
550                id,
551                metadata: args.source_range.into(),
552            },
553        },
554    };
555
556    let mut new_sketch = sketch.clone();
557    if let Some(tag) = &tag {
558        new_sketch.add_tag(tag, &current_path);
559    }
560
561    new_sketch.paths.push(current_path);
562    Ok(new_sketch)
563}
564
565/// Draw an angled line of a given x length.
566pub async fn angled_line_of_x_length(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
567    let (data, sketch, tag): (AngledLineData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
568
569    let new_sketch = inner_angled_line_of_x_length(data, sketch, tag, exec_state, args).await?;
570    Ok(KclValue::Sketch {
571        value: Box::new(new_sketch),
572    })
573}
574
575/// Create a line segment from the current 2-dimensional sketch origin
576/// along some angle (in degrees) for some relative length in the 'x' dimension.
577///
578/// ```no_run
579/// sketch001 = startSketchOn(XZ)
580///   |> startProfileAt([0, 0], %)
581///   |> angledLineOfXLength({ angle = 45, length = 10 }, %, $edge1)
582///   |> angledLineOfXLength({ angle = -15, length = 20 }, %, $edge2)
583///   |> line(end = [0, -5])
584///   |> close(tag = $edge3)
585///
586/// extrusion = extrude(sketch001, length = 10)
587/// ```
588#[stdlib {
589    name = "angledLineOfXLength",
590}]
591async fn inner_angled_line_of_x_length(
592    data: AngledLineData,
593    sketch: Sketch,
594    tag: Option<TagNode>,
595    exec_state: &mut ExecState,
596    args: Args,
597) -> Result<Sketch, KclError> {
598    let (angle, length) = match data {
599        AngledLineData::AngleAndLengthNamed { angle, length } => (angle, length),
600        AngledLineData::AngleAndLengthPair(pair) => (pair[0], pair[1]),
601    };
602
603    if angle.abs() == 270.0 {
604        return Err(KclError::Type(KclErrorDetails {
605            message: "Cannot have an x constrained angle of 270 degrees".to_string(),
606            source_ranges: vec![args.source_range],
607        }));
608    }
609
610    if angle.abs() == 90.0 {
611        return Err(KclError::Type(KclErrorDetails {
612            message: "Cannot have an x constrained angle of 90 degrees".to_string(),
613            source_ranges: vec![args.source_range],
614        }));
615    }
616
617    let to = get_y_component(Angle::from_degrees(angle), length);
618
619    let new_sketch = straight_line(StraightLineParams::relative(to.into(), sketch, tag), exec_state, args).await?;
620
621    Ok(new_sketch)
622}
623
624/// Data to draw an angled line to a point.
625#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
626#[ts(export)]
627#[serde(rename_all = "camelCase")]
628pub struct AngledLineToData {
629    /// The angle of the line.
630    pub angle: f64,
631    /// The point to draw to.
632    pub to: f64,
633}
634
635/// Draw an angled line to a given x coordinate.
636pub async fn angled_line_to_x(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
637    let (data, sketch, tag): (AngledLineToData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
638
639    let new_sketch = inner_angled_line_to_x(data, sketch, tag, exec_state, args).await?;
640    Ok(KclValue::Sketch {
641        value: Box::new(new_sketch),
642    })
643}
644
645/// Create a line segment from the current 2-dimensional sketch origin
646/// along some angle (in degrees) for some length, ending at the provided value
647/// in the 'x' dimension.
648///
649/// ```no_run
650/// exampleSketch = startSketchOn(XZ)
651///   |> startProfileAt([0, 0], %)
652///   |> angledLineToX({ angle = 30, to = 10 }, %)
653///   |> line(end = [0, 10])
654///   |> line(end = [-10, 0])
655///   |> close()
656///
657/// example = extrude(exampleSketch, length = 10)
658/// ```
659#[stdlib {
660    name = "angledLineToX",
661}]
662async fn inner_angled_line_to_x(
663    data: AngledLineToData,
664    sketch: Sketch,
665    tag: Option<TagNode>,
666    exec_state: &mut ExecState,
667    args: Args,
668) -> Result<Sketch, KclError> {
669    let from = sketch.current_pen_position()?;
670    let AngledLineToData { angle, to: x_to } = data;
671
672    if angle.abs() == 270.0 {
673        return Err(KclError::Type(KclErrorDetails {
674            message: "Cannot have an x constrained angle of 270 degrees".to_string(),
675            source_ranges: vec![args.source_range],
676        }));
677    }
678
679    if angle.abs() == 90.0 {
680        return Err(KclError::Type(KclErrorDetails {
681            message: "Cannot have an x constrained angle of 90 degrees".to_string(),
682            source_ranges: vec![args.source_range],
683        }));
684    }
685
686    let x_component = x_to - from.x;
687    let y_component = x_component * f64::tan(angle.to_radians());
688    let y_to = from.y + y_component;
689
690    let new_sketch = straight_line(
691        StraightLineParams::absolute([x_to, y_to], sketch, tag),
692        exec_state,
693        args,
694    )
695    .await?;
696    Ok(new_sketch)
697}
698
699/// Draw an angled line of a given y length.
700pub async fn angled_line_of_y_length(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
701    let (data, sketch, tag): (AngledLineData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
702
703    let new_sketch = inner_angled_line_of_y_length(data, sketch, tag, exec_state, args).await?;
704
705    Ok(KclValue::Sketch {
706        value: Box::new(new_sketch),
707    })
708}
709
710/// Create a line segment from the current 2-dimensional sketch origin
711/// along some angle (in degrees) for some relative length in the 'y' dimension.
712///
713/// ```no_run
714/// exampleSketch = startSketchOn(XZ)
715///   |> startProfileAt([0, 0], %)
716///   |> line(end = [10, 0])
717///   |> angledLineOfYLength({ angle = 45, length = 10 }, %)
718///   |> line(end = [0, 10])
719///   |> angledLineOfYLength({ angle = 135, length = 10 }, %)
720///   |> line(end = [-10, 0])
721///   |> line(end = [0, -30])
722///
723/// example = extrude(exampleSketch, length = 10)
724/// ```
725#[stdlib {
726    name = "angledLineOfYLength",
727}]
728async fn inner_angled_line_of_y_length(
729    data: AngledLineData,
730    sketch: Sketch,
731    tag: Option<TagNode>,
732    exec_state: &mut ExecState,
733    args: Args,
734) -> Result<Sketch, KclError> {
735    let (angle, length) = match data {
736        AngledLineData::AngleAndLengthNamed { angle, length } => (angle, length),
737        AngledLineData::AngleAndLengthPair(pair) => (pair[0], pair[1]),
738    };
739
740    if angle.abs() == 0.0 {
741        return Err(KclError::Type(KclErrorDetails {
742            message: "Cannot have a y constrained angle of 0 degrees".to_string(),
743            source_ranges: vec![args.source_range],
744        }));
745    }
746
747    if angle.abs() == 180.0 {
748        return Err(KclError::Type(KclErrorDetails {
749            message: "Cannot have a y constrained angle of 180 degrees".to_string(),
750            source_ranges: vec![args.source_range],
751        }));
752    }
753
754    let to = get_x_component(Angle::from_degrees(angle), length);
755
756    let new_sketch = straight_line(StraightLineParams::relative(to.into(), sketch, tag), exec_state, args).await?;
757
758    Ok(new_sketch)
759}
760
761/// Draw an angled line to a given y coordinate.
762pub async fn angled_line_to_y(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
763    let (data, sketch, tag): (AngledLineToData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
764
765    let new_sketch = inner_angled_line_to_y(data, sketch, tag, exec_state, args).await?;
766    Ok(KclValue::Sketch {
767        value: Box::new(new_sketch),
768    })
769}
770
771/// Create a line segment from the current 2-dimensional sketch origin
772/// along some angle (in degrees) for some length, ending at the provided value
773/// in the 'y' dimension.
774///
775/// ```no_run
776/// exampleSketch = startSketchOn(XZ)
777///   |> startProfileAt([0, 0], %)
778///   |> angledLineToY({ angle = 60, to = 20 }, %)
779///   |> line(end = [-20, 0])
780///   |> angledLineToY({ angle = 70, to = 10 }, %)
781///   |> close()
782///
783/// example = extrude(exampleSketch, length = 10)
784/// ```
785#[stdlib {
786    name = "angledLineToY",
787}]
788async fn inner_angled_line_to_y(
789    data: AngledLineToData,
790    sketch: Sketch,
791    tag: Option<TagNode>,
792    exec_state: &mut ExecState,
793    args: Args,
794) -> Result<Sketch, KclError> {
795    let from = sketch.current_pen_position()?;
796    let AngledLineToData { angle, to: y_to } = data;
797
798    if angle.abs() == 0.0 {
799        return Err(KclError::Type(KclErrorDetails {
800            message: "Cannot have a y constrained angle of 0 degrees".to_string(),
801            source_ranges: vec![args.source_range],
802        }));
803    }
804
805    if angle.abs() == 180.0 {
806        return Err(KclError::Type(KclErrorDetails {
807            message: "Cannot have a y constrained angle of 180 degrees".to_string(),
808            source_ranges: vec![args.source_range],
809        }));
810    }
811
812    let y_component = y_to - from.y;
813    let x_component = y_component / f64::tan(angle.to_radians());
814    let x_to = from.x + x_component;
815
816    let new_sketch = straight_line(
817        StraightLineParams::absolute([x_to, y_to], sketch, tag),
818        exec_state,
819        args,
820    )
821    .await?;
822    Ok(new_sketch)
823}
824
825/// Data for drawing an angled line that intersects with a given line.
826#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
827#[ts(export)]
828#[serde(rename_all = "camelCase")]
829// TODO: make sure the docs on the args below are correct.
830pub struct AngledLineThatIntersectsData {
831    /// The angle of the line.
832    pub angle: f64,
833    /// The tag of the line to intersect with.
834    pub intersect_tag: TagIdentifier,
835    /// The offset from the intersecting line.
836    pub offset: Option<f64>,
837}
838
839/// Draw an angled line that intersects with a given line.
840pub async fn angled_line_that_intersects(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
841    let (data, sketch, tag): (AngledLineThatIntersectsData, Sketch, Option<TagNode>) =
842        args.get_data_and_sketch_and_tag()?;
843    let new_sketch = inner_angled_line_that_intersects(data, sketch, tag, exec_state, args).await?;
844    Ok(KclValue::Sketch {
845        value: Box::new(new_sketch),
846    })
847}
848
849/// Draw an angled line from the current origin, constructing a line segment
850/// such that the newly created line intersects the desired target line
851/// segment.
852///
853/// ```no_run
854/// exampleSketch = startSketchOn(XZ)
855///   |> startProfileAt([0, 0], %)
856///   |> line(endAbsolute = [5, 10])
857///   |> line(endAbsolute = [-10, 10], tag = $lineToIntersect)
858///   |> line(endAbsolute = [0, 20])
859///   |> angledLineThatIntersects({
860///        angle = 80,
861///        intersectTag = lineToIntersect,
862///        offset = 10
863///      }, %)
864///   |> close()
865///
866/// example = extrude(exampleSketch, length = 10)
867/// ```
868#[stdlib {
869    name = "angledLineThatIntersects",
870}]
871async fn inner_angled_line_that_intersects(
872    data: AngledLineThatIntersectsData,
873    sketch: Sketch,
874    tag: Option<TagNode>,
875    exec_state: &mut ExecState,
876    args: Args,
877) -> Result<Sketch, KclError> {
878    let intersect_path = args.get_tag_engine_info(exec_state, &data.intersect_tag)?;
879    let path = intersect_path.path.clone().ok_or_else(|| {
880        KclError::Type(KclErrorDetails {
881            message: format!("Expected an intersect path with a path, found `{:?}`", intersect_path),
882            source_ranges: vec![args.source_range],
883        })
884    })?;
885
886    let from = sketch.current_pen_position()?;
887    let to = intersection_with_parallel_line(
888        &[path.get_from().into(), path.get_to().into()],
889        data.offset.unwrap_or_default(),
890        data.angle,
891        from,
892    );
893
894    let new_sketch = straight_line(StraightLineParams::absolute(to.into(), sketch, tag), exec_state, args).await?;
895    Ok(new_sketch)
896}
897
898/// Start a sketch at a given point.
899pub async fn start_sketch_at(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
900    let data: [f64; 2] = args.get_data()?;
901
902    let sketch = inner_start_sketch_at(data, exec_state, args).await?;
903    Ok(KclValue::Sketch {
904        value: Box::new(sketch),
905    })
906}
907
908/// Start a new 2-dimensional sketch at a given point on the 'XY' plane.
909///
910/// ```no_run
911/// exampleSketch = startSketchAt([0, 0])
912///   |> line(end = [10, 0])
913///   |> line(end = [0, 10])
914///   |> line(end = [-10, 0])
915///   |> close()
916///
917/// example = extrude(exampleSketch, length = 5)
918/// ```
919///
920/// ```no_run
921/// exampleSketch = startSketchAt([10, 10])
922///   |> line(end = [10, 0])
923///   |> line(end = [0, 10])
924///   |> line(end = [-10, 0])
925///   |> close()
926///
927/// example = extrude(exampleSketch, length = 5)
928/// ```
929///
930/// ```no_run
931/// exampleSketch = startSketchAt([-10, 23])
932///   |> line(end = [10, 0])
933///   |> line(end = [0, 10])
934///   |> line(end = [-10, 0])
935///   |> close()
936///
937/// example = extrude(exampleSketch, length = 5)
938/// ```
939#[stdlib {
940    name = "startSketchAt",
941    deprecated = true,
942}]
943async fn inner_start_sketch_at(data: [f64; 2], exec_state: &mut ExecState, args: Args) -> Result<Sketch, KclError> {
944    // Let's assume it's the XY plane for now, this is just for backwards compatibility.
945    let xy_plane = PlaneData::XY;
946    let sketch_surface = inner_start_sketch_on(SketchData::PlaneOrientation(xy_plane), None, exec_state, &args).await?;
947    let sketch = inner_start_profile_at(data, sketch_surface, None, exec_state, args).await?;
948    Ok(sketch)
949}
950
951/// Data for start sketch on.
952/// You can start a sketch on a plane or an solid.
953#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
954#[ts(export)]
955#[serde(rename_all = "camelCase", untagged)]
956#[allow(clippy::large_enum_variant)]
957pub enum SketchData {
958    PlaneOrientation(PlaneData),
959    Plane(Box<Plane>),
960    Solid(Box<Solid>),
961}
962
963/// Orientation data that can be used to construct a plane, not a plane in itself.
964#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
965#[ts(export)]
966#[serde(rename_all = "camelCase")]
967#[allow(clippy::large_enum_variant)]
968pub enum PlaneData {
969    /// The XY plane.
970    #[serde(rename = "XY", alias = "xy")]
971    XY,
972    /// The opposite side of the XY plane.
973    #[serde(rename = "-XY", alias = "-xy")]
974    NegXY,
975    /// The XZ plane.
976    #[serde(rename = "XZ", alias = "xz")]
977    XZ,
978    /// The opposite side of the XZ plane.
979    #[serde(rename = "-XZ", alias = "-xz")]
980    NegXZ,
981    /// The YZ plane.
982    #[serde(rename = "YZ", alias = "yz")]
983    YZ,
984    /// The opposite side of the YZ plane.
985    #[serde(rename = "-YZ", alias = "-yz")]
986    NegYZ,
987    /// A defined plane.
988    Plane {
989        /// Origin of the plane.
990        origin: Point3d,
991        /// What should the plane’s X axis be?
992        #[serde(rename = "xAxis")]
993        x_axis: Point3d,
994        /// What should the plane’s Y axis be?
995        #[serde(rename = "yAxis")]
996        y_axis: Point3d,
997        /// The z-axis (normal).
998        #[serde(rename = "zAxis")]
999        z_axis: Point3d,
1000    },
1001}
1002
1003/// Start a sketch on a specific plane or face.
1004pub async fn start_sketch_on(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1005    let (data, tag): (SketchData, Option<FaceTag>) = args.get_data_and_optional_tag()?;
1006
1007    match inner_start_sketch_on(data, tag, exec_state, &args).await? {
1008        SketchSurface::Plane(value) => Ok(KclValue::Plane { value }),
1009        SketchSurface::Face(value) => Ok(KclValue::Face { value }),
1010    }
1011}
1012
1013/// Start a new 2-dimensional sketch on a specific plane or face.
1014///
1015/// ### Sketch on Face Behavior
1016///
1017/// There are some important behaviors to understand when sketching on a face:
1018///
1019/// The resulting sketch will _include_ the face and thus Solid
1020/// that was sketched on. So say you were to export the resulting Sketch / Solid
1021/// from a sketch on a face, you would get both the artifact of the sketch
1022/// on the face and the parent face / Solid itself.
1023///
1024/// This is important to understand because if you were to then sketch on the
1025/// resulting Solid, it would again include the face and parent Solid that was
1026/// sketched on. This could go on indefinitely.
1027///
1028/// The point is if you want to export the result of a sketch on a face, you
1029/// only need to export the final Solid that was created from the sketch on the
1030/// face, since it will include all the parent faces and Solids.
1031///
1032///
1033/// ```no_run
1034/// exampleSketch = startSketchOn(XY)
1035///   |> startProfileAt([0, 0], %)
1036///   |> line(end = [10, 0])
1037///   |> line(end = [0, 10])
1038///   |> line(end = [-10, 0])
1039///   |> close()
1040///
1041/// example = extrude(exampleSketch, length = 5)
1042///
1043/// exampleSketch002 = startSketchOn(example, 'end')
1044///   |> startProfileAt([1, 1], %)
1045///   |> line(end = [8, 0])
1046///   |> line(end = [0, 8])
1047///   |> line(end = [-8, 0])
1048///   |> close()
1049///
1050/// example002 = extrude(exampleSketch002, length = 5)
1051///
1052/// exampleSketch003 = startSketchOn(example002, 'end')
1053///   |> startProfileAt([2, 2], %)
1054///   |> line(end = [6, 0])
1055///   |> line(end = [0, 6])
1056///   |> line(end = [-6, 0])
1057///   |> close()
1058///
1059/// example003 = extrude(exampleSketch003, length = 5)
1060/// ```
1061///
1062/// ```no_run
1063/// exampleSketch = startSketchOn(XY)
1064///   |> startProfileAt([0, 0], %)
1065///   |> line(end = [10, 0])
1066///   |> line(end = [0, 10], tag = $sketchingFace)
1067///   |> line(end = [-10, 0])
1068///   |> close()
1069///
1070/// example = extrude(exampleSketch, length = 10)
1071///
1072/// exampleSketch002 = startSketchOn(example, sketchingFace)
1073///   |> startProfileAt([1, 1], %)
1074///   |> line(end = [8, 0])
1075///   |> line(end = [0, 8])
1076///   |> line(end = [-8, 0])
1077///   |> close(tag = $sketchingFace002)
1078///
1079/// example002 = extrude(exampleSketch002, length = 10)
1080///
1081/// exampleSketch003 = startSketchOn(example002, sketchingFace002)
1082///   |> startProfileAt([-8, 12], %)
1083///   |> line(end = [0, 6])
1084///   |> line(end = [6, 0])
1085///   |> line(end = [0, -6])
1086///   |> close()
1087///
1088/// example003 = extrude(exampleSketch003, length = 5)
1089/// ```
1090///
1091/// ```no_run
1092/// exampleSketch = startSketchOn(XY)
1093///   |> startProfileAt([4, 12], %)
1094///   |> line(end = [2, 0])
1095///   |> line(end = [0, -6])
1096///   |> line(end = [4, -6])
1097///   |> line(end = [0, -6])
1098///   |> line(end = [-3.75, -4.5])
1099///   |> line(end = [0, -5.5])
1100///   |> line(end = [-2, 0])
1101///   |> close()
1102///
1103/// example = revolve({ axis: 'y', angle: 180 }, exampleSketch)
1104///
1105/// exampleSketch002 = startSketchOn(example, 'end')
1106///   |> startProfileAt([4.5, -5], %)
1107///   |> line(end = [0, 5])
1108///   |> line(end = [5, 0])
1109///   |> line(end = [0, -5])
1110///   |> close()
1111///
1112/// example002 = extrude(exampleSketch002, length = 5)
1113/// ```
1114///
1115/// ```no_run
1116/// a1 = startSketchOn({
1117///       plane: {
1118///         origin = { x = 0, y = 0, z = 0 },
1119///         xAxis = { x = 1, y = 0, z = 0 },
1120///         yAxis = { x = 0, y = 1, z = 0 },
1121///         zAxis = { x = 0, y = 0, z = 1 }
1122///       }
1123///     })
1124///  |> startProfileAt([0, 0], %)
1125///  |> line(end = [100.0, 0])
1126///  |> yLine(-100.0, %)
1127///  |> xLine(-100.0, %)
1128///  |> yLine(100.0, %)
1129///  |> close()
1130///  |> extrude(length = 3.14)
1131/// ```
1132#[stdlib {
1133    name = "startSketchOn",
1134    feature_tree_operation = true,
1135}]
1136async fn inner_start_sketch_on(
1137    data: SketchData,
1138    tag: Option<FaceTag>,
1139    exec_state: &mut ExecState,
1140    args: &Args,
1141) -> Result<SketchSurface, KclError> {
1142    match data {
1143        SketchData::PlaneOrientation(plane_data) => {
1144            let plane = make_sketch_plane_from_orientation(plane_data, exec_state, args).await?;
1145            Ok(SketchSurface::Plane(plane))
1146        }
1147        SketchData::Plane(plane) => {
1148            if plane.value == crate::exec::PlaneType::Uninit {
1149                let plane = make_sketch_plane_from_orientation(plane.into_plane_data(), exec_state, args).await?;
1150                Ok(SketchSurface::Plane(plane))
1151            } else {
1152                // Create artifact used only by the UI, not the engine.
1153                let id = exec_state.next_uuid();
1154                exec_state.add_artifact(Artifact::StartSketchOnPlane(StartSketchOnPlane {
1155                    id: ArtifactId::from(id),
1156                    plane_id: plane.artifact_id,
1157                    code_ref: CodeRef::placeholder(args.source_range),
1158                }));
1159
1160                Ok(SketchSurface::Plane(plane))
1161            }
1162        }
1163        SketchData::Solid(solid) => {
1164            let Some(tag) = tag else {
1165                return Err(KclError::Type(KclErrorDetails {
1166                    message: "Expected a tag for the face to sketch on".to_string(),
1167                    source_ranges: vec![args.source_range],
1168                }));
1169            };
1170            let face = start_sketch_on_face(solid, tag, exec_state, args).await?;
1171
1172            // Create artifact used only by the UI, not the engine.
1173            let id = exec_state.next_uuid();
1174            exec_state.add_artifact(Artifact::StartSketchOnFace(StartSketchOnFace {
1175                id: ArtifactId::from(id),
1176                face_id: face.artifact_id,
1177                code_ref: CodeRef::placeholder(args.source_range),
1178            }));
1179
1180            Ok(SketchSurface::Face(face))
1181        }
1182    }
1183}
1184
1185async fn start_sketch_on_face(
1186    solid: Box<Solid>,
1187    tag: FaceTag,
1188    exec_state: &mut ExecState,
1189    args: &Args,
1190) -> Result<Box<Face>, KclError> {
1191    let extrude_plane_id = tag.get_face_id(&solid, exec_state, args, true).await?;
1192
1193    Ok(Box::new(Face {
1194        id: extrude_plane_id,
1195        artifact_id: extrude_plane_id.into(),
1196        value: tag.to_string(),
1197        // TODO: get this from the extrude plane data.
1198        x_axis: solid.sketch.on.x_axis(),
1199        y_axis: solid.sketch.on.y_axis(),
1200        z_axis: solid.sketch.on.z_axis(),
1201        units: solid.units,
1202        solid,
1203        meta: vec![args.source_range.into()],
1204    }))
1205}
1206
1207async fn make_sketch_plane_from_orientation(
1208    data: PlaneData,
1209    exec_state: &mut ExecState,
1210    args: &Args,
1211) -> Result<Box<Plane>, KclError> {
1212    let plane = Plane::from_plane_data(data.clone(), exec_state);
1213
1214    // Create the plane on the fly.
1215    let clobber = false;
1216    let size = LengthUnit(60.0);
1217    let hide = Some(true);
1218    match data {
1219        PlaneData::XY | PlaneData::NegXY | PlaneData::XZ | PlaneData::NegXZ | PlaneData::YZ | PlaneData::NegYZ => {
1220            let x_axis = match data {
1221                PlaneData::NegXY => Point3d::new(-1.0, 0.0, 0.0),
1222                PlaneData::NegXZ => Point3d::new(-1.0, 0.0, 0.0),
1223                PlaneData::NegYZ => Point3d::new(0.0, -1.0, 0.0),
1224                _ => plane.x_axis,
1225            };
1226            args.batch_modeling_cmd(
1227                plane.id,
1228                ModelingCmd::from(mcmd::MakePlane {
1229                    clobber,
1230                    origin: plane.origin.into(),
1231                    size,
1232                    x_axis: x_axis.into(),
1233                    y_axis: plane.y_axis.into(),
1234                    hide,
1235                }),
1236            )
1237            .await?;
1238        }
1239        PlaneData::Plane {
1240            origin,
1241            x_axis,
1242            y_axis,
1243            z_axis: _,
1244        } => {
1245            args.batch_modeling_cmd(
1246                plane.id,
1247                ModelingCmd::from(mcmd::MakePlane {
1248                    clobber,
1249                    origin: origin.into(),
1250                    size,
1251                    x_axis: x_axis.into(),
1252                    y_axis: y_axis.into(),
1253                    hide,
1254                }),
1255            )
1256            .await?;
1257        }
1258    }
1259
1260    Ok(Box::new(plane))
1261}
1262
1263/// Start a new profile at a given point.
1264pub async fn start_profile_at(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1265    let (start, sketch_surface, tag): ([f64; 2], SketchSurface, Option<TagNode>) =
1266        args.get_data_and_sketch_surface()?;
1267
1268    let sketch = inner_start_profile_at(start, sketch_surface, tag, exec_state, args).await?;
1269    Ok(KclValue::Sketch {
1270        value: Box::new(sketch),
1271    })
1272}
1273
1274/// Start a new profile at a given point.
1275///
1276/// ```no_run
1277/// exampleSketch = startSketchOn(XZ)
1278///   |> startProfileAt([0, 0], %)
1279///   |> line(end = [10, 0])
1280///   |> line(end = [0, 10])
1281///   |> line(end = [-10, 0])
1282///   |> close()
1283///
1284/// example = extrude(exampleSketch, length = 5)
1285/// ```
1286///
1287/// ```no_run
1288/// exampleSketch = startSketchOn(-XZ)
1289///   |> startProfileAt([10, 10], %)
1290///   |> line(end = [10, 0])
1291///   |> line(end = [0, 10])
1292///   |> line(end = [-10, 0])
1293///   |> close()
1294///
1295/// example = extrude(exampleSketch, length = 5)
1296/// ```
1297///
1298/// ```no_run
1299/// exampleSketch = startSketchOn(-XZ)
1300///   |> startProfileAt([-10, 23], %)
1301///   |> line(end = [10, 0])
1302///   |> line(end = [0, 10])
1303///   |> line(end = [-10, 0])
1304///   |> close()
1305///
1306/// example = extrude(exampleSketch, length = 5)
1307/// ```
1308#[stdlib {
1309    name = "startProfileAt",
1310}]
1311pub(crate) async fn inner_start_profile_at(
1312    to: [f64; 2],
1313    sketch_surface: SketchSurface,
1314    tag: Option<TagNode>,
1315    exec_state: &mut ExecState,
1316    args: Args,
1317) -> Result<Sketch, KclError> {
1318    match &sketch_surface {
1319        SketchSurface::Face(face) => {
1320            // Flush the batch for our fillets/chamfers if there are any.
1321            // If we do not do these for sketch on face, things will fail with face does not exist.
1322            args.flush_batch_for_solid_set(exec_state, face.solid.clone().into())
1323                .await?;
1324        }
1325        SketchSurface::Plane(plane) if !plane.is_standard() => {
1326            // Hide whatever plane we are sketching on.
1327            // This is especially helpful for offset planes, which would be visible otherwise.
1328            args.batch_end_cmd(
1329                exec_state.next_uuid(),
1330                ModelingCmd::from(mcmd::ObjectVisible {
1331                    object_id: plane.id,
1332                    hidden: true,
1333                }),
1334            )
1335            .await?;
1336        }
1337        _ => {}
1338    }
1339
1340    // Enter sketch mode on the surface.
1341    // We call this here so you can reuse the sketch surface for multiple sketches.
1342    let id = exec_state.next_uuid();
1343    args.batch_modeling_cmd(
1344        id,
1345        ModelingCmd::from(mcmd::EnableSketchMode {
1346            animated: false,
1347            ortho: false,
1348            entity_id: sketch_surface.id(),
1349            adjust_camera: false,
1350            planar_normal: if let SketchSurface::Plane(plane) = &sketch_surface {
1351                // We pass in the normal for the plane here.
1352                Some(plane.z_axis.into())
1353            } else {
1354                None
1355            },
1356        }),
1357    )
1358    .await?;
1359
1360    let id = exec_state.next_uuid();
1361    let path_id = exec_state.next_uuid();
1362
1363    args.batch_modeling_cmd(path_id, ModelingCmd::from(mcmd::StartPath::default()))
1364        .await?;
1365    args.batch_modeling_cmd(
1366        id,
1367        ModelingCmd::from(mcmd::MovePathPen {
1368            path: path_id.into(),
1369            to: KPoint2d::from(to).with_z(0.0).map(LengthUnit),
1370        }),
1371    )
1372    .await?;
1373
1374    let current_path = BasePath {
1375        from: to,
1376        to,
1377        tag: tag.clone(),
1378        units: sketch_surface.units(),
1379        geo_meta: GeoMeta {
1380            id,
1381            metadata: args.source_range.into(),
1382        },
1383    };
1384
1385    let sketch = Sketch {
1386        id: path_id,
1387        original_id: path_id,
1388        artifact_id: path_id.into(),
1389        on: sketch_surface.clone(),
1390        paths: vec![],
1391        units: sketch_surface.units(),
1392        meta: vec![args.source_range.into()],
1393        tags: if let Some(tag) = &tag {
1394            let mut tag_identifier: TagIdentifier = tag.into();
1395            tag_identifier.info = Some(TagEngineInfo {
1396                id: current_path.geo_meta.id,
1397                sketch: path_id,
1398                path: Some(Path::Base {
1399                    base: current_path.clone(),
1400                }),
1401                surface: None,
1402            });
1403            IndexMap::from([(tag.name.to_string(), tag_identifier)])
1404        } else {
1405            Default::default()
1406        },
1407        start: current_path,
1408    };
1409    Ok(sketch)
1410}
1411
1412/// Returns the X component of the sketch profile start point.
1413pub async fn profile_start_x(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1414    let sketch: Sketch = args.get_sketch()?;
1415    let ty = sketch.units.into();
1416    let x = inner_profile_start_x(sketch)?;
1417    Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
1418}
1419
1420/// Extract the provided 2-dimensional sketch's profile's origin's 'x'
1421/// value.
1422///
1423/// ```no_run
1424/// sketch001 = startSketchOn(XY)
1425///  |> startProfileAt([5, 2], %)
1426///  |> angledLine([-26.6, 50], %)
1427///  |> angledLine([90, 50], %)
1428///  |> angledLineToX({ angle = 30, to = profileStartX(%) }, %)
1429/// ```
1430#[stdlib {
1431    name = "profileStartX"
1432}]
1433pub(crate) fn inner_profile_start_x(sketch: Sketch) -> Result<f64, KclError> {
1434    Ok(sketch.start.to[0])
1435}
1436
1437/// Returns the Y component of the sketch profile start point.
1438pub async fn profile_start_y(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1439    let sketch: Sketch = args.get_sketch()?;
1440    let ty = sketch.units.into();
1441    let x = inner_profile_start_y(sketch)?;
1442    Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
1443}
1444
1445/// Extract the provided 2-dimensional sketch's profile's origin's 'y'
1446/// value.
1447///
1448/// ```no_run
1449/// sketch001 = startSketchOn(XY)
1450///  |> startProfileAt([5, 2], %)
1451///  |> angledLine({ angle = -60, length = 14 }, %)
1452///  |> angledLineToY({ angle = 30, to = profileStartY(%) }, %)
1453/// ```
1454#[stdlib {
1455    name = "profileStartY"
1456}]
1457pub(crate) fn inner_profile_start_y(sketch: Sketch) -> Result<f64, KclError> {
1458    Ok(sketch.start.to[1])
1459}
1460
1461/// Returns the sketch profile start point.
1462pub async fn profile_start(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1463    let sketch: Sketch = args.get_sketch()?;
1464    let ty = sketch.units.into();
1465    let point = inner_profile_start(sketch)?;
1466    Ok(KclValue::from_point2d(point, ty, args.into()))
1467}
1468
1469/// Extract the provided 2-dimensional sketch's profile's origin
1470/// value.
1471///
1472/// ```no_run
1473/// sketch001 = startSketchOn(XY)
1474///  |> startProfileAt([5, 2], %)
1475///  |> angledLine({ angle = 120, length = 50 }, %, $seg01)
1476///  |> angledLine({ angle = segAng(seg01) + 120, length = 50 }, %)
1477///  |> line(end = profileStart(%))
1478///  |> close()
1479///  |> extrude(length = 20)
1480/// ```
1481#[stdlib {
1482    name = "profileStart"
1483}]
1484pub(crate) fn inner_profile_start(sketch: Sketch) -> Result<[f64; 2], KclError> {
1485    Ok(sketch.start.to)
1486}
1487
1488/// Close the current sketch.
1489pub async fn close(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1490    let sketch = args.get_unlabeled_kw_arg("sketch")?;
1491    let tag = args.get_kw_arg_opt(NEW_TAG_KW)?;
1492    let new_sketch = inner_close(sketch, tag, exec_state, args).await?;
1493    Ok(KclValue::Sketch {
1494        value: Box::new(new_sketch),
1495    })
1496}
1497
1498/// Construct a line segment from the current origin back to the profile's
1499/// origin, ensuring the resulting 2-dimensional sketch is not open-ended.
1500///
1501/// ```no_run
1502/// startSketchOn(XZ)
1503///    |> startProfileAt([0, 0], %)
1504///    |> line(end = [10, 10])
1505///    |> line(end = [10, 0])
1506///    |> close()
1507///    |> extrude(length = 10)
1508/// ```
1509///
1510/// ```no_run
1511/// exampleSketch = startSketchOn(-XZ)
1512///   |> startProfileAt([0, 0], %)
1513///   |> line(end = [10, 0])
1514///   |> line(end = [0, 10])
1515///   |> close()
1516///
1517/// example = extrude(exampleSketch, length = 10)
1518/// ```
1519#[stdlib {
1520    name = "close",
1521    keywords = true,
1522    unlabeled_first = true,
1523    args = {
1524        sketch = { docs = "The sketch you want to close"},
1525        tag = { docs = "Create a new tag which refers to this line"},
1526    }
1527}]
1528pub(crate) async fn inner_close(
1529    sketch: Sketch,
1530    tag: Option<TagNode>,
1531    exec_state: &mut ExecState,
1532    args: Args,
1533) -> Result<Sketch, KclError> {
1534    let from = sketch.current_pen_position()?;
1535    let to: Point2d = sketch.start.from.into();
1536
1537    let id = exec_state.next_uuid();
1538
1539    args.batch_modeling_cmd(id, ModelingCmd::from(mcmd::ClosePath { path_id: sketch.id }))
1540        .await?;
1541
1542    // If we are sketching on a plane we can close the sketch now.
1543    if let SketchSurface::Plane(_) = sketch.on {
1544        // We were on a plane, disable the sketch mode.
1545        args.batch_modeling_cmd(
1546            exec_state.next_uuid(),
1547            ModelingCmd::SketchModeDisable(mcmd::SketchModeDisable::default()),
1548        )
1549        .await?;
1550    }
1551
1552    let current_path = Path::ToPoint {
1553        base: BasePath {
1554            from: from.into(),
1555            to: to.into(),
1556            tag: tag.clone(),
1557            units: sketch.units,
1558            geo_meta: GeoMeta {
1559                id,
1560                metadata: args.source_range.into(),
1561            },
1562        },
1563    };
1564
1565    let mut new_sketch = sketch.clone();
1566    if let Some(tag) = &tag {
1567        new_sketch.add_tag(tag, &current_path);
1568    }
1569
1570    new_sketch.paths.push(current_path);
1571
1572    Ok(new_sketch)
1573}
1574
1575/// Data to draw an arc.
1576#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
1577#[ts(export)]
1578#[serde(rename_all = "camelCase", untagged)]
1579pub enum ArcData {
1580    /// Angles and radius with an optional tag.
1581    AnglesAndRadius {
1582        /// The start angle.
1583        #[serde(rename = "angleStart")]
1584        #[schemars(range(min = -360.0, max = 360.0))]
1585        angle_start: f64,
1586        /// The end angle.
1587        #[serde(rename = "angleEnd")]
1588        #[schemars(range(min = -360.0, max = 360.0))]
1589        angle_end: f64,
1590        /// The radius.
1591        radius: f64,
1592    },
1593    /// Center, to and radius with an optional tag.
1594    CenterToRadius {
1595        /// The center.
1596        center: [f64; 2],
1597        /// The to point.
1598        to: [f64; 2],
1599        /// The radius.
1600        radius: f64,
1601    },
1602}
1603
1604/// Data to draw a three point arc (arcTo).
1605#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
1606#[ts(export)]
1607#[serde(rename_all = "camelCase")]
1608pub struct ArcToData {
1609    /// End point of the arc. A point in 3D space
1610    pub end: [f64; 2],
1611    /// Interior point of the arc. A point in 3D space
1612    pub interior: [f64; 2],
1613}
1614
1615/// Draw an arc.
1616pub async fn arc(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1617    let (data, sketch, tag): (ArcData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
1618
1619    let new_sketch = inner_arc(data, sketch, tag, exec_state, args).await?;
1620    Ok(KclValue::Sketch {
1621        value: Box::new(new_sketch),
1622    })
1623}
1624
1625/// Draw a curved line segment along an imaginary circle.
1626/// The arc is constructed such that the current position of the sketch is
1627/// placed along an imaginary circle of the specified radius, at angleStart
1628/// degrees. The resulting arc is the segment of the imaginary circle from
1629/// that origin point to angleEnd, radius away from the center of the imaginary
1630/// circle.
1631///
1632/// Unless this makes a lot of sense and feels like what you're looking
1633/// for to construct your shape, you're likely looking for tangentialArc.
1634///
1635/// ```no_run
1636/// exampleSketch = startSketchOn(XZ)
1637///   |> startProfileAt([0, 0], %)
1638///   |> line(end = [10, 0])
1639///   |> arc({
1640///        angleStart = 0,
1641///        angleEnd = 280,
1642///        radius = 16
1643///      }, %)
1644///   |> close()
1645/// example = extrude(exampleSketch, length = 10)
1646/// ```
1647#[stdlib {
1648    name = "arc",
1649}]
1650pub(crate) async fn inner_arc(
1651    data: ArcData,
1652    sketch: Sketch,
1653    tag: Option<TagNode>,
1654    exec_state: &mut ExecState,
1655    args: Args,
1656) -> Result<Sketch, KclError> {
1657    let from: Point2d = sketch.current_pen_position()?;
1658
1659    let (center, angle_start, angle_end, radius, end) = match &data {
1660        ArcData::AnglesAndRadius {
1661            angle_start,
1662            angle_end,
1663            radius,
1664        } => {
1665            let a_start = Angle::from_degrees(*angle_start);
1666            let a_end = Angle::from_degrees(*angle_end);
1667            let (center, end) = arc_center_and_end(from, a_start, a_end, *radius);
1668            (center, a_start, a_end, *radius, end)
1669        }
1670        ArcData::CenterToRadius { center, to, radius } => {
1671            let (angle_start, angle_end) = arc_angles(from, to.into(), center.into(), *radius, args.source_range)?;
1672            (center.into(), angle_start, angle_end, *radius, to.into())
1673        }
1674    };
1675
1676    if angle_start == angle_end {
1677        return Err(KclError::Type(KclErrorDetails {
1678            message: "Arc start and end angles must be different".to_string(),
1679            source_ranges: vec![args.source_range],
1680        }));
1681    }
1682    let ccw = angle_start < angle_end;
1683
1684    let id = exec_state.next_uuid();
1685
1686    args.batch_modeling_cmd(
1687        id,
1688        ModelingCmd::from(mcmd::ExtendPath {
1689            path: sketch.id.into(),
1690            segment: PathSegment::Arc {
1691                start: angle_start,
1692                end: angle_end,
1693                center: KPoint2d::from(center).map(LengthUnit),
1694                radius: LengthUnit(radius),
1695                relative: false,
1696            },
1697        }),
1698    )
1699    .await?;
1700
1701    let current_path = Path::Arc {
1702        base: BasePath {
1703            from: from.into(),
1704            to: end.into(),
1705            tag: tag.clone(),
1706            units: sketch.units,
1707            geo_meta: GeoMeta {
1708                id,
1709                metadata: args.source_range.into(),
1710            },
1711        },
1712        center: center.into(),
1713        radius,
1714        ccw,
1715    };
1716
1717    let mut new_sketch = sketch.clone();
1718    if let Some(tag) = &tag {
1719        new_sketch.add_tag(tag, &current_path);
1720    }
1721
1722    new_sketch.paths.push(current_path);
1723
1724    Ok(new_sketch)
1725}
1726
1727/// Draw a three point arc.
1728pub async fn arc_to(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1729    let (data, sketch, tag): (ArcToData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
1730
1731    let new_sketch = inner_arc_to(data, sketch, tag, exec_state, args).await?;
1732    Ok(KclValue::Sketch {
1733        value: Box::new(new_sketch),
1734    })
1735}
1736
1737/// Draw a 3 point arc.
1738///
1739/// The arc is constructed such that the start point is the current position of the sketch and two more points defined as the end and interior point.
1740/// The interior point is placed between the start point and end point. The radius of the arc will be controlled by how far the interior point is placed from
1741/// the start and end.
1742///
1743/// ```no_run
1744/// exampleSketch = startSketchOn(XZ)
1745///   |> startProfileAt([0, 0], %)
1746///   |> arcTo({
1747///         end = [10,0],
1748///         interior = [5,5]
1749///      }, %)
1750///   |> close()
1751/// example = extrude(exampleSketch, length = 10)
1752/// ```
1753#[stdlib {
1754    name = "arcTo",
1755}]
1756pub(crate) async fn inner_arc_to(
1757    data: ArcToData,
1758    sketch: Sketch,
1759    tag: Option<TagNode>,
1760    exec_state: &mut ExecState,
1761    args: Args,
1762) -> Result<Sketch, KclError> {
1763    let from: Point2d = sketch.current_pen_position()?;
1764    let id = exec_state.next_uuid();
1765
1766    // The start point is taken from the path you are extending.
1767    args.batch_modeling_cmd(
1768        id,
1769        ModelingCmd::from(mcmd::ExtendPath {
1770            path: sketch.id.into(),
1771            segment: PathSegment::ArcTo {
1772                end: kcmc::shared::Point3d {
1773                    x: LengthUnit(data.end[0]),
1774                    y: LengthUnit(data.end[1]),
1775                    z: LengthUnit(0.0),
1776                },
1777                interior: kcmc::shared::Point3d {
1778                    x: LengthUnit(data.interior[0]),
1779                    y: LengthUnit(data.interior[1]),
1780                    z: LengthUnit(0.0),
1781                },
1782                relative: false,
1783            },
1784        }),
1785    )
1786    .await?;
1787
1788    let start = [from.x, from.y];
1789    let interior = data.interior;
1790    let end = data.end;
1791
1792    // compute the center of the circle since we do not have the value returned from the engine
1793    let center = calculate_circle_center(start, interior, end);
1794
1795    // compute the radius since we do not have the value returned from the engine
1796    // Pick any of the 3 points since they all lie along the circle
1797    let sum_of_square_differences =
1798        (center[0] - start[0] * center[0] - start[0]) + (center[1] - start[1] * center[1] - start[1]);
1799    let radius = sum_of_square_differences.sqrt();
1800
1801    let ccw = is_ccw(start, interior, end);
1802
1803    let current_path = Path::Arc {
1804        base: BasePath {
1805            from: from.into(),
1806            to: data.end,
1807            tag: tag.clone(),
1808            units: sketch.units,
1809            geo_meta: GeoMeta {
1810                id,
1811                metadata: args.source_range.into(),
1812            },
1813        },
1814        center,
1815        radius,
1816        ccw,
1817    };
1818
1819    let mut new_sketch = sketch.clone();
1820    if let Some(tag) = &tag {
1821        new_sketch.add_tag(tag, &current_path);
1822    }
1823
1824    new_sketch.paths.push(current_path);
1825
1826    Ok(new_sketch)
1827}
1828
1829/// Returns true if the three-point arc is counterclockwise.  The order of
1830/// parameters is critical.
1831///
1832/// |   end
1833/// |  /
1834/// |  |    / interior
1835/// |  /  /
1836/// | | /
1837/// |/_____________
1838/// start
1839///
1840/// If the slope of the line from start to interior is less than the slope of
1841/// the line from start to end, the arc is counterclockwise.
1842fn is_ccw(start: [f64; 2], interior: [f64; 2], end: [f64; 2]) -> bool {
1843    let t1 = (interior[0] - start[0]) * (end[1] - start[1]);
1844    let t2 = (end[0] - start[0]) * (interior[1] - start[1]);
1845    // If these terms are equal, the points are collinear.
1846    t1 > t2
1847}
1848
1849/// Data to draw a tangential arc.
1850#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, JsonSchema, ts_rs::TS)]
1851#[ts(export)]
1852#[serde(rename_all = "camelCase", untagged)]
1853pub enum TangentialArcData {
1854    RadiusAndOffset {
1855        /// Radius of the arc.
1856        /// Not to be confused with Raiders of the Lost Ark.
1857        radius: f64,
1858        /// Offset of the arc, in degrees.
1859        offset: f64,
1860    },
1861}
1862
1863/// Draw a tangential arc.
1864pub async fn tangential_arc(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1865    let (data, sketch, tag): (TangentialArcData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
1866
1867    let new_sketch = inner_tangential_arc(data, sketch, tag, exec_state, args).await?;
1868    Ok(KclValue::Sketch {
1869        value: Box::new(new_sketch),
1870    })
1871}
1872
1873/// Draw a curved line segment along part of an imaginary circle.
1874///
1875/// The arc is constructed such that the last line segment is placed tangent
1876/// to the imaginary circle of the specified radius. The resulting arc is the
1877/// segment of the imaginary circle from that tangent point for 'offset'
1878/// degrees along the imaginary circle.
1879///
1880/// ```no_run
1881/// exampleSketch = startSketchOn(XZ)
1882///   |> startProfileAt([0, 0], %)
1883///   |> angledLine({
1884///     angle = 60,
1885///     length = 10,
1886///   }, %)
1887///   |> tangentialArc({ radius = 10, offset = -120 }, %)
1888///   |> angledLine({
1889///     angle = -60,
1890///     length = 10,
1891///   }, %)
1892///   |> close()
1893///
1894/// example = extrude(exampleSketch, length = 10)
1895/// ```
1896#[stdlib {
1897    name = "tangentialArc",
1898}]
1899async fn inner_tangential_arc(
1900    data: TangentialArcData,
1901    sketch: Sketch,
1902    tag: Option<TagNode>,
1903    exec_state: &mut ExecState,
1904    args: Args,
1905) -> Result<Sketch, KclError> {
1906    let from: Point2d = sketch.current_pen_position()?;
1907    // next set of lines is some undocumented voodoo from get_tangential_arc_to_info
1908    let tangent_info = sketch.get_tangential_info_from_paths(); //this function desperately needs some documentation
1909    let tan_previous_point = tangent_info.tan_previous_point(from.into());
1910
1911    let id = exec_state.next_uuid();
1912
1913    let (center, to, ccw) = match data {
1914        TangentialArcData::RadiusAndOffset { radius, offset } => {
1915            // KCL stdlib types use degrees.
1916            let offset = Angle::from_degrees(offset);
1917
1918            // Calculate the end point from the angle and radius.
1919            // atan2 outputs radians.
1920            let previous_end_tangent = Angle::from_radians(f64::atan2(
1921                from.y - tan_previous_point[1],
1922                from.x - tan_previous_point[0],
1923            ));
1924            // make sure the arc center is on the correct side to guarantee deterministic behavior
1925            // note the engine automatically rejects an offset of zero, if we want to flag that at KCL too to avoid engine errors
1926            let ccw = offset.to_degrees() > 0.0;
1927            let tangent_to_arc_start_angle = if ccw {
1928                // CCW turn
1929                Angle::from_degrees(-90.0)
1930            } else {
1931                // CW turn
1932                Angle::from_degrees(90.0)
1933            };
1934            // may need some logic and / or modulo on the various angle values to prevent them from going "backwards"
1935            // but the above logic *should* capture that behavior
1936            let start_angle = previous_end_tangent + tangent_to_arc_start_angle;
1937            let end_angle = start_angle + offset;
1938            let (center, to) = arc_center_and_end(from, start_angle, end_angle, radius);
1939
1940            args.batch_modeling_cmd(
1941                id,
1942                ModelingCmd::from(mcmd::ExtendPath {
1943                    path: sketch.id.into(),
1944                    segment: PathSegment::TangentialArc {
1945                        radius: LengthUnit(radius),
1946                        offset,
1947                    },
1948                }),
1949            )
1950            .await?;
1951            (center, to.into(), ccw)
1952        }
1953    };
1954
1955    let current_path = Path::TangentialArc {
1956        ccw,
1957        center: center.into(),
1958        base: BasePath {
1959            from: from.into(),
1960            to,
1961            tag: tag.clone(),
1962            units: sketch.units,
1963            geo_meta: GeoMeta {
1964                id,
1965                metadata: args.source_range.into(),
1966            },
1967        },
1968    };
1969
1970    let mut new_sketch = sketch.clone();
1971    if let Some(tag) = &tag {
1972        new_sketch.add_tag(tag, &current_path);
1973    }
1974
1975    new_sketch.paths.push(current_path);
1976
1977    Ok(new_sketch)
1978}
1979
1980fn tan_arc_to(sketch: &Sketch, to: &[f64; 2]) -> ModelingCmd {
1981    ModelingCmd::from(mcmd::ExtendPath {
1982        path: sketch.id.into(),
1983        segment: PathSegment::TangentialArcTo {
1984            angle_snap_increment: None,
1985            to: KPoint2d::from(*to).with_z(0.0).map(LengthUnit),
1986        },
1987    })
1988}
1989
1990/// Draw a tangential arc to a specific point.
1991pub async fn tangential_arc_to(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1992    let (to, sketch, tag): ([f64; 2], Sketch, Option<TagNode>) = super::args::FromArgs::from_args(&args, 0)?;
1993
1994    let new_sketch = inner_tangential_arc_to(to, sketch, tag, exec_state, args).await?;
1995    Ok(KclValue::Sketch {
1996        value: Box::new(new_sketch),
1997    })
1998}
1999
2000/// Draw a tangential arc to point some distance away..
2001pub async fn tangential_arc_to_relative(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2002    let (delta, sketch, tag): ([f64; 2], Sketch, Option<TagNode>) = super::args::FromArgs::from_args(&args, 0)?;
2003
2004    let new_sketch = inner_tangential_arc_to_relative(delta, sketch, tag, exec_state, args).await?;
2005    Ok(KclValue::Sketch {
2006        value: Box::new(new_sketch),
2007    })
2008}
2009
2010/// Starting at the current sketch's origin, draw a curved line segment along
2011/// some part of an imaginary circle until it reaches the desired (x, y)
2012/// coordinates.
2013///
2014/// ```no_run
2015/// exampleSketch = startSketchOn(XZ)
2016///   |> startProfileAt([0, 0], %)
2017///   |> angledLine({
2018///     angle = 60,
2019///     length = 10,
2020///   }, %)
2021///   |> tangentialArcTo([15, 15], %)
2022///   |> line(end = [10, -15])
2023///   |> close()
2024///
2025/// example = extrude(exampleSketch, length = 10)
2026/// ```
2027#[stdlib {
2028    name = "tangentialArcTo",
2029}]
2030async fn inner_tangential_arc_to(
2031    to: [f64; 2],
2032    sketch: Sketch,
2033    tag: Option<TagNode>,
2034    exec_state: &mut ExecState,
2035    args: Args,
2036) -> Result<Sketch, KclError> {
2037    let from: Point2d = sketch.current_pen_position()?;
2038    let tangent_info = sketch.get_tangential_info_from_paths();
2039    let tan_previous_point = tangent_info.tan_previous_point(from.into());
2040    let [to_x, to_y] = to;
2041    let result = get_tangential_arc_to_info(TangentialArcInfoInput {
2042        arc_start_point: [from.x, from.y],
2043        arc_end_point: to,
2044        tan_previous_point,
2045        obtuse: true,
2046    });
2047
2048    let delta = [to_x - from.x, to_y - from.y];
2049    let id = exec_state.next_uuid();
2050    args.batch_modeling_cmd(id, tan_arc_to(&sketch, &delta)).await?;
2051
2052    let current_path = Path::TangentialArcTo {
2053        base: BasePath {
2054            from: from.into(),
2055            to,
2056            tag: tag.clone(),
2057            units: sketch.units,
2058            geo_meta: GeoMeta {
2059                id,
2060                metadata: args.source_range.into(),
2061            },
2062        },
2063        center: result.center,
2064        ccw: result.ccw > 0,
2065    };
2066
2067    let mut new_sketch = sketch.clone();
2068    if let Some(tag) = &tag {
2069        new_sketch.add_tag(tag, &current_path);
2070    }
2071
2072    new_sketch.paths.push(current_path);
2073
2074    Ok(new_sketch)
2075}
2076
2077/// Starting at the current sketch's origin, draw a curved line segment along
2078/// some part of an imaginary circle until it reaches a point the given (x, y)
2079/// distance away.
2080///
2081/// ```no_run
2082/// exampleSketch = startSketchOn(XZ)
2083///   |> startProfileAt([0, 0], %)
2084///   |> angledLine({
2085///     angle = 45,
2086///     length = 10,
2087///   }, %)
2088///   |> tangentialArcToRelative([0, -10], %)
2089///   |> line(end = [-10, 0])
2090///   |> close()
2091///
2092/// example = extrude(exampleSketch, length = 10)
2093/// ```
2094#[stdlib {
2095    name = "tangentialArcToRelative",
2096}]
2097async fn inner_tangential_arc_to_relative(
2098    delta: [f64; 2],
2099    sketch: Sketch,
2100    tag: Option<TagNode>,
2101    exec_state: &mut ExecState,
2102    args: Args,
2103) -> Result<Sketch, KclError> {
2104    let from: Point2d = sketch.current_pen_position()?;
2105    let to = [from.x + delta[0], from.y + delta[1]];
2106    let tangent_info = sketch.get_tangential_info_from_paths();
2107    let tan_previous_point = tangent_info.tan_previous_point(from.into());
2108
2109    let [dx, dy] = delta;
2110    let result = get_tangential_arc_to_info(TangentialArcInfoInput {
2111        arc_start_point: [from.x, from.y],
2112        arc_end_point: [from.x + dx, from.y + dy],
2113        tan_previous_point,
2114        obtuse: true,
2115    });
2116
2117    if result.center[0].is_infinite() {
2118        return Err(KclError::Semantic(KclErrorDetails {
2119            source_ranges: vec![args.source_range],
2120            message:
2121                "could not sketch tangential arc, because its center would be infinitely far away in the X direction"
2122                    .to_owned(),
2123        }));
2124    } else if result.center[1].is_infinite() {
2125        return Err(KclError::Semantic(KclErrorDetails {
2126            source_ranges: vec![args.source_range],
2127            message:
2128                "could not sketch tangential arc, because its center would be infinitely far away in the Y direction"
2129                    .to_owned(),
2130        }));
2131    }
2132
2133    let id = exec_state.next_uuid();
2134    args.batch_modeling_cmd(id, tan_arc_to(&sketch, &delta)).await?;
2135
2136    let current_path = Path::TangentialArcTo {
2137        base: BasePath {
2138            from: from.into(),
2139            to,
2140            tag: tag.clone(),
2141            units: sketch.units,
2142            geo_meta: GeoMeta {
2143                id,
2144                metadata: args.source_range.into(),
2145            },
2146        },
2147        center: result.center,
2148        ccw: result.ccw > 0,
2149    };
2150
2151    let mut new_sketch = sketch.clone();
2152    if let Some(tag) = &tag {
2153        new_sketch.add_tag(tag, &current_path);
2154    }
2155
2156    new_sketch.paths.push(current_path);
2157
2158    Ok(new_sketch)
2159}
2160
2161/// Data to draw a bezier curve.
2162#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
2163#[ts(export)]
2164#[serde(rename_all = "camelCase")]
2165pub struct BezierData {
2166    /// The to point.
2167    pub to: [f64; 2],
2168    /// The first control point.
2169    pub control1: [f64; 2],
2170    /// The second control point.
2171    pub control2: [f64; 2],
2172}
2173
2174/// Draw a bezier curve.
2175pub async fn bezier_curve(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2176    let (data, sketch, tag): (BezierData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
2177
2178    let new_sketch = inner_bezier_curve(data, sketch, tag, exec_state, args).await?;
2179    Ok(KclValue::Sketch {
2180        value: Box::new(new_sketch),
2181    })
2182}
2183
2184/// Draw a smooth, continuous, curved line segment from the current origin to
2185/// the desired (x, y), using a number of control points to shape the curve's
2186/// shape.
2187///
2188/// ```no_run
2189/// exampleSketch = startSketchOn(XZ)
2190///   |> startProfileAt([0, 0], %)
2191///   |> line(end = [0, 10])
2192///   |> bezierCurve({
2193///        to = [10, 10],
2194///        control1 = [5, 0],
2195///        control2 = [5, 10]
2196///      }, %)
2197///   |> line(endAbsolute = [10, 0])
2198///   |> close()
2199///
2200/// example = extrude(exampleSketch, length = 10)
2201/// ```
2202#[stdlib {
2203    name = "bezierCurve",
2204}]
2205async fn inner_bezier_curve(
2206    data: BezierData,
2207    sketch: Sketch,
2208    tag: Option<TagNode>,
2209    exec_state: &mut ExecState,
2210    args: Args,
2211) -> Result<Sketch, KclError> {
2212    let from = sketch.current_pen_position()?;
2213
2214    let relative = true;
2215    let delta = data.to;
2216    let to = [from.x + data.to[0], from.y + data.to[1]];
2217
2218    let id = exec_state.next_uuid();
2219
2220    args.batch_modeling_cmd(
2221        id,
2222        ModelingCmd::from(mcmd::ExtendPath {
2223            path: sketch.id.into(),
2224            segment: PathSegment::Bezier {
2225                control1: KPoint2d::from(data.control1).with_z(0.0).map(LengthUnit),
2226                control2: KPoint2d::from(data.control2).with_z(0.0).map(LengthUnit),
2227                end: KPoint2d::from(delta).with_z(0.0).map(LengthUnit),
2228                relative,
2229            },
2230        }),
2231    )
2232    .await?;
2233
2234    let current_path = Path::ToPoint {
2235        base: BasePath {
2236            from: from.into(),
2237            to,
2238            tag: tag.clone(),
2239            units: sketch.units,
2240            geo_meta: GeoMeta {
2241                id,
2242                metadata: args.source_range.into(),
2243            },
2244        },
2245    };
2246
2247    let mut new_sketch = sketch.clone();
2248    if let Some(tag) = &tag {
2249        new_sketch.add_tag(tag, &current_path);
2250    }
2251
2252    new_sketch.paths.push(current_path);
2253
2254    Ok(new_sketch)
2255}
2256
2257/// Use a sketch to cut a hole in another sketch.
2258pub async fn hole(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2259    let (hole_sketch, sketch): (SketchSet, Sketch) = args.get_sketches()?;
2260
2261    let new_sketch = inner_hole(hole_sketch, sketch, exec_state, args).await?;
2262    Ok(KclValue::Sketch {
2263        value: Box::new(new_sketch),
2264    })
2265}
2266
2267/// Use a 2-dimensional sketch to cut a hole in another 2-dimensional sketch.
2268///
2269/// ```no_run
2270/// exampleSketch = startSketchOn(XY)
2271///   |> startProfileAt([0, 0], %)
2272///   |> line(end = [0, 5])
2273///   |> line(end = [5, 0])
2274///   |> line(end = [0, -5])
2275///   |> close()
2276///   |> hole(circle( center = [1, 1], radius = .25 ), %)
2277///   |> hole(circle( center = [1, 4], radius = .25 ), %)
2278///
2279/// example = extrude(exampleSketch, length = 1)
2280/// ```
2281///
2282/// ```no_run
2283/// fn squareHoleSketch() {
2284///   squareSketch = startSketchOn(-XZ)
2285///     |> startProfileAt([-1, -1], %)
2286///     |> line(end = [2, 0])
2287///     |> line(end = [0, 2])
2288///     |> line(end = [-2, 0])
2289///     |> close()
2290///   return squareSketch
2291/// }
2292///
2293/// exampleSketch = startSketchOn(-XZ)
2294///     |> circle( center = [0, 0], radius = 3 )
2295///     |> hole(squareHoleSketch(), %)
2296/// example = extrude(exampleSketch, length = 1)
2297/// ```
2298#[stdlib {
2299    name = "hole",
2300    feature_tree_operation = true,
2301}]
2302async fn inner_hole(
2303    hole_sketch: SketchSet,
2304    sketch: Sketch,
2305    exec_state: &mut ExecState,
2306    args: Args,
2307) -> Result<Sketch, KclError> {
2308    let hole_sketches: Vec<Sketch> = hole_sketch.into();
2309    for hole_sketch in hole_sketches {
2310        args.batch_modeling_cmd(
2311            exec_state.next_uuid(),
2312            ModelingCmd::from(mcmd::Solid2dAddHole {
2313                object_id: sketch.id,
2314                hole_id: hole_sketch.id,
2315            }),
2316        )
2317        .await?;
2318
2319        // suggestion (mike)
2320        // we also hide the source hole since its essentially "consumed" by this operation
2321        args.batch_modeling_cmd(
2322            exec_state.next_uuid(),
2323            ModelingCmd::from(mcmd::ObjectVisible {
2324                object_id: hole_sketch.id,
2325                hidden: true,
2326            }),
2327        )
2328        .await?;
2329    }
2330
2331    Ok(sketch)
2332}
2333
2334#[cfg(test)]
2335mod tests {
2336
2337    use pretty_assertions::assert_eq;
2338
2339    use crate::{
2340        execution::TagIdentifier,
2341        std::{sketch::PlaneData, utils::calculate_circle_center},
2342    };
2343
2344    #[test]
2345    fn test_deserialize_plane_data() {
2346        let data = PlaneData::XY;
2347        let mut str_json = serde_json::to_string(&data).unwrap();
2348        assert_eq!(str_json, "\"XY\"");
2349
2350        str_json = "\"YZ\"".to_string();
2351        let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2352        assert_eq!(data, PlaneData::YZ);
2353
2354        str_json = "\"-YZ\"".to_string();
2355        let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2356        assert_eq!(data, PlaneData::NegYZ);
2357
2358        str_json = "\"-xz\"".to_string();
2359        let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2360        assert_eq!(data, PlaneData::NegXZ);
2361    }
2362
2363    #[test]
2364    fn test_deserialize_sketch_on_face_tag() {
2365        let data = "start";
2366        let mut str_json = serde_json::to_string(&data).unwrap();
2367        assert_eq!(str_json, "\"start\"");
2368
2369        str_json = "\"end\"".to_string();
2370        let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2371        assert_eq!(
2372            data,
2373            crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
2374        );
2375
2376        str_json = serde_json::to_string(&TagIdentifier {
2377            value: "thing".to_string(),
2378            info: None,
2379            meta: Default::default(),
2380        })
2381        .unwrap();
2382        let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2383        assert_eq!(
2384            data,
2385            crate::std::sketch::FaceTag::Tag(Box::new(TagIdentifier {
2386                value: "thing".to_string(),
2387                info: None,
2388                meta: Default::default()
2389            }))
2390        );
2391
2392        str_json = "\"END\"".to_string();
2393        let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2394        assert_eq!(
2395            data,
2396            crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
2397        );
2398
2399        str_json = "\"start\"".to_string();
2400        let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2401        assert_eq!(
2402            data,
2403            crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
2404        );
2405
2406        str_json = "\"START\"".to_string();
2407        let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2408        assert_eq!(
2409            data,
2410            crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
2411        );
2412    }
2413
2414    #[test]
2415    fn test_circle_center() {
2416        let actual = calculate_circle_center([0.0, 0.0], [5.0, 5.0], [10.0, 0.0]);
2417        assert_eq!(actual[0], 5.0);
2418        assert_eq!(actual[1], 0.0);
2419    }
2420}