1use anyhow::Result;
4use derive_docs::stdlib;
5use indexmap::IndexMap;
6use kcmc::shared::Point2d as KPoint2d; use 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::{
15 errors::{KclError, KclErrorDetails},
16 execution::{
17 Artifact, ArtifactId, BasePath, ExecState, Face, GeoMeta, KclValue, Path, Plane, Point2d, Point3d, Sketch,
18 SketchSet, SketchSurface, Solid, TagEngineInfo, TagIdentifier,
19 },
20 parsing::ast::types::TagNode,
21 std::{
22 args::{Args, TyF64},
23 utils::{
24 arc_angles, arc_center_and_end, calculate_circle_center, get_tangential_arc_to_info, get_x_component,
25 get_y_component, intersection_with_parallel_line, TangentialArcInfoInput,
26 },
27 },
28};
29
30#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
32#[ts(export)]
33#[serde(rename_all = "snake_case", untagged)]
34pub enum FaceTag {
35 StartOrEnd(StartOrEnd),
36 Tag(Box<TagIdentifier>),
38}
39
40impl std::fmt::Display for FaceTag {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 match self {
43 FaceTag::Tag(t) => write!(f, "{}", t),
44 FaceTag::StartOrEnd(StartOrEnd::Start) => write!(f, "start"),
45 FaceTag::StartOrEnd(StartOrEnd::End) => write!(f, "end"),
46 }
47 }
48}
49
50impl FaceTag {
51 pub async fn get_face_id(
53 &self,
54 solid: &Solid,
55 exec_state: &mut ExecState,
56 args: &Args,
57 must_be_planar: bool,
58 ) -> Result<uuid::Uuid, KclError> {
59 match self {
60 FaceTag::Tag(ref t) => args.get_adjacent_face_to_tag(exec_state, t, must_be_planar).await,
61 FaceTag::StartOrEnd(StartOrEnd::Start) => solid.start_cap_id.ok_or_else(|| {
62 KclError::Type(KclErrorDetails {
63 message: "Expected a start face".to_string(),
64 source_ranges: vec![args.source_range],
65 })
66 }),
67 FaceTag::StartOrEnd(StartOrEnd::End) => solid.end_cap_id.ok_or_else(|| {
68 KclError::Type(KclErrorDetails {
69 message: "Expected an end face".to_string(),
70 source_ranges: vec![args.source_range],
71 })
72 }),
73 }
74 }
75}
76
77#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display)]
78#[ts(export)]
79#[serde(rename_all = "snake_case")]
80#[display(style = "snake_case")]
81pub enum StartOrEnd {
82 #[serde(rename = "start", alias = "START")]
86 Start,
87 #[serde(rename = "end", alias = "END")]
91 End,
92}
93
94pub const NEW_TAG_KW: &str = "tag";
95
96pub async fn line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
98 let sketch = args.get_unlabeled_kw_arg("sketch")?;
100 let end = args.get_kw_arg_opt("end")?;
101 let end_absolute = args.get_kw_arg_opt("endAbsolute")?;
102 let tag = args.get_kw_arg_opt(NEW_TAG_KW)?;
103
104 let new_sketch = inner_line(sketch, end_absolute, end, tag, exec_state, args).await?;
105 Ok(KclValue::Sketch {
106 value: Box::new(new_sketch),
107 })
108}
109
110#[stdlib {
135 name = "line",
136 keywords = true,
137 unlabeled_first = true,
138 args = {
139 sketch = { docs = "Which sketch should this path be added to?"},
140 end_absolute = { docs = "Which absolute point should this line go to? Incompatible with `end`."},
141 end = { docs = "How far away (along the X and Y axes) should this line go? Incompatible with `endAbsolute`.", include_in_snippet = true},
142 tag = { docs = "Create a new tag which refers to this line"},
143 }
144}]
145async fn inner_line(
146 sketch: Sketch,
147 end_absolute: Option<[f64; 2]>,
148 end: Option<[f64; 2]>,
149 tag: Option<TagNode>,
150 exec_state: &mut ExecState,
151 args: Args,
152) -> Result<Sketch, KclError> {
153 straight_line(
154 StraightLineParams {
155 sketch,
156 end_absolute,
157 end,
158 tag,
159 },
160 exec_state,
161 args,
162 )
163 .await
164}
165
166struct StraightLineParams {
167 sketch: Sketch,
168 end_absolute: Option<[f64; 2]>,
169 end: Option<[f64; 2]>,
170 tag: Option<TagNode>,
171}
172
173impl StraightLineParams {
174 fn relative(p: [f64; 2], sketch: Sketch, tag: Option<TagNode>) -> Self {
175 Self {
176 sketch,
177 tag,
178 end: Some(p),
179 end_absolute: None,
180 }
181 }
182 fn absolute(p: [f64; 2], sketch: Sketch, tag: Option<TagNode>) -> Self {
183 Self {
184 sketch,
185 tag,
186 end: None,
187 end_absolute: Some(p),
188 }
189 }
190}
191
192async fn straight_line(
193 StraightLineParams {
194 sketch,
195 end,
196 end_absolute,
197 tag,
198 }: StraightLineParams,
199 exec_state: &mut ExecState,
200 args: Args,
201) -> Result<Sketch, KclError> {
202 let from = sketch.current_pen_position()?;
203 let (point, is_absolute) = match (end_absolute, end) {
204 (Some(_), Some(_)) => {
205 return Err(KclError::Semantic(KclErrorDetails {
206 source_ranges: vec![args.source_range],
207 message: "You cannot give both `end` and `endAbsolute` params, you have to choose one or the other"
208 .to_owned(),
209 }));
210 }
211 (Some(end_absolute), None) => (end_absolute, true),
212 (None, Some(end)) => (end, false),
213 (None, None) => {
214 return Err(KclError::Semantic(KclErrorDetails {
215 source_ranges: vec![args.source_range],
216 message: "You must supply either `end` or `endAbsolute` arguments".to_owned(),
217 }));
218 }
219 };
220
221 let id = exec_state.next_uuid();
222 args.batch_modeling_cmd(
223 id,
224 ModelingCmd::from(mcmd::ExtendPath {
225 path: sketch.id.into(),
226 segment: PathSegment::Line {
227 end: KPoint2d::from(point).with_z(0.0).map(LengthUnit),
228 relative: !is_absolute,
229 },
230 }),
231 )
232 .await?;
233 let end = if is_absolute {
234 point
235 } else {
236 let from = sketch.current_pen_position()?;
237 [from.x + point[0], from.y + point[1]]
238 };
239
240 let current_path = Path::ToPoint {
241 base: BasePath {
242 from: from.into(),
243 to: end,
244 tag: tag.clone(),
245 units: sketch.units,
246 geo_meta: GeoMeta {
247 id,
248 metadata: args.source_range.into(),
249 },
250 },
251 };
252
253 let mut new_sketch = sketch.clone();
254 if let Some(tag) = &tag {
255 new_sketch.add_tag(tag, ¤t_path);
256 }
257
258 new_sketch.paths.push(current_path);
259
260 Ok(new_sketch)
261}
262
263pub async fn x_line_to(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
265 let (to, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
266
267 let new_sketch = inner_x_line_to(to, sketch, tag, exec_state, args).await?;
268 Ok(KclValue::Sketch {
269 value: Box::new(new_sketch),
270 })
271}
272
273#[stdlib {
297 name = "xLineTo",
298}]
299async fn inner_x_line_to(
300 to: f64,
301 sketch: Sketch,
302 tag: Option<TagNode>,
303 exec_state: &mut ExecState,
304 args: Args,
305) -> Result<Sketch, KclError> {
306 let from = sketch.current_pen_position()?;
307
308 let new_sketch = straight_line(
309 StraightLineParams::absolute([to, from.y], sketch, tag),
310 exec_state,
311 args,
312 )
313 .await?;
314
315 Ok(new_sketch)
316}
317
318pub async fn y_line_to(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
320 let (to, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
321
322 let new_sketch = inner_y_line_to(to, sketch, tag, exec_state, args).await?;
323 Ok(KclValue::Sketch {
324 value: Box::new(new_sketch),
325 })
326}
327
328#[stdlib {
345 name = "yLineTo",
346}]
347async fn inner_y_line_to(
348 to: f64,
349 sketch: Sketch,
350 tag: Option<TagNode>,
351 exec_state: &mut ExecState,
352 args: Args,
353) -> Result<Sketch, KclError> {
354 let from = sketch.current_pen_position()?;
355
356 let new_sketch = straight_line(
357 StraightLineParams::absolute([from.x, to], sketch, tag),
358 exec_state,
359 args,
360 )
361 .await?;
362 Ok(new_sketch)
363}
364
365pub async fn x_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
367 let (length, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
368
369 let new_sketch = inner_x_line(length, sketch, tag, exec_state, args).await?;
370 Ok(KclValue::Sketch {
371 value: Box::new(new_sketch),
372 })
373}
374
375#[stdlib {
398 name = "xLine",
399}]
400async fn inner_x_line(
401 length: f64,
402 sketch: Sketch,
403 tag: Option<TagNode>,
404 exec_state: &mut ExecState,
405 args: Args,
406) -> Result<Sketch, KclError> {
407 straight_line(
408 StraightLineParams::relative([length, 0.0], sketch, tag),
409 exec_state,
410 args,
411 )
412 .await
413}
414
415pub async fn y_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
417 let (length, sketch, tag): (f64, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
418
419 let new_sketch = inner_y_line(length, sketch, tag, exec_state, args).await?;
420 Ok(KclValue::Sketch {
421 value: Box::new(new_sketch),
422 })
423}
424
425#[stdlib {
443 name = "yLine",
444}]
445async fn inner_y_line(
446 length: f64,
447 sketch: Sketch,
448 tag: Option<TagNode>,
449 exec_state: &mut ExecState,
450 args: Args,
451) -> Result<Sketch, KclError> {
452 straight_line(
453 StraightLineParams::relative([0.0, length], sketch, tag),
454 exec_state,
455 args,
456 )
457 .await
458}
459
460#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
462#[ts(export)]
463#[serde(rename_all = "camelCase", untagged)]
464pub enum AngledLineData {
465 AngleAndLengthNamed {
467 angle: f64,
469 length: f64,
471 },
472 AngleAndLengthPair([f64; 2]),
474}
475
476pub async fn angled_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
478 let (data, sketch, tag): (AngledLineData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
479
480 let new_sketch = inner_angled_line(data, sketch, tag, exec_state, args).await?;
481 Ok(KclValue::Sketch {
482 value: Box::new(new_sketch),
483 })
484}
485
486#[stdlib {
504 name = "angledLine",
505}]
506async fn inner_angled_line(
507 data: AngledLineData,
508 sketch: Sketch,
509 tag: Option<TagNode>,
510 exec_state: &mut ExecState,
511 args: Args,
512) -> Result<Sketch, KclError> {
513 let from = sketch.current_pen_position()?;
514 let (angle, length) = match data {
515 AngledLineData::AngleAndLengthNamed { angle, length } => (angle, length),
516 AngledLineData::AngleAndLengthPair(pair) => (pair[0], pair[1]),
517 };
518
519 let delta: [f64; 2] = [
521 length * f64::cos(angle.to_radians()),
522 length * f64::sin(angle.to_radians()),
523 ];
524 let relative = true;
525
526 let to: [f64; 2] = [from.x + delta[0], from.y + delta[1]];
527
528 let id = exec_state.next_uuid();
529
530 args.batch_modeling_cmd(
531 id,
532 ModelingCmd::from(mcmd::ExtendPath {
533 path: sketch.id.into(),
534 segment: PathSegment::Line {
535 end: KPoint2d::from(delta).with_z(0.0).map(LengthUnit),
536 relative,
537 },
538 }),
539 )
540 .await?;
541
542 let current_path = Path::ToPoint {
543 base: BasePath {
544 from: from.into(),
545 to,
546 tag: tag.clone(),
547 units: sketch.units,
548 geo_meta: GeoMeta {
549 id,
550 metadata: args.source_range.into(),
551 },
552 },
553 };
554
555 let mut new_sketch = sketch.clone();
556 if let Some(tag) = &tag {
557 new_sketch.add_tag(tag, ¤t_path);
558 }
559
560 new_sketch.paths.push(current_path);
561 Ok(new_sketch)
562}
563
564pub async fn angled_line_of_x_length(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
566 let (data, sketch, tag): (AngledLineData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
567
568 let new_sketch = inner_angled_line_of_x_length(data, sketch, tag, exec_state, args).await?;
569 Ok(KclValue::Sketch {
570 value: Box::new(new_sketch),
571 })
572}
573
574#[stdlib {
588 name = "angledLineOfXLength",
589}]
590async fn inner_angled_line_of_x_length(
591 data: AngledLineData,
592 sketch: Sketch,
593 tag: Option<TagNode>,
594 exec_state: &mut ExecState,
595 args: Args,
596) -> Result<Sketch, KclError> {
597 let (angle, length) = match data {
598 AngledLineData::AngleAndLengthNamed { angle, length } => (angle, length),
599 AngledLineData::AngleAndLengthPair(pair) => (pair[0], pair[1]),
600 };
601
602 if angle.abs() == 270.0 {
603 return Err(KclError::Type(KclErrorDetails {
604 message: "Cannot have an x constrained angle of 270 degrees".to_string(),
605 source_ranges: vec![args.source_range],
606 }));
607 }
608
609 if angle.abs() == 90.0 {
610 return Err(KclError::Type(KclErrorDetails {
611 message: "Cannot have an x constrained angle of 90 degrees".to_string(),
612 source_ranges: vec![args.source_range],
613 }));
614 }
615
616 let to = get_y_component(Angle::from_degrees(angle), length);
617
618 let new_sketch = straight_line(StraightLineParams::relative(to.into(), sketch, tag), exec_state, args).await?;
619
620 Ok(new_sketch)
621}
622
623#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
625#[ts(export)]
626#[serde(rename_all = "camelCase")]
627pub struct AngledLineToData {
628 pub angle: f64,
630 pub to: f64,
632}
633
634pub async fn angled_line_to_x(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
636 let (data, sketch, tag): (AngledLineToData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
637
638 let new_sketch = inner_angled_line_to_x(data, sketch, tag, exec_state, args).await?;
639 Ok(KclValue::Sketch {
640 value: Box::new(new_sketch),
641 })
642}
643
644#[stdlib {
659 name = "angledLineToX",
660}]
661async fn inner_angled_line_to_x(
662 data: AngledLineToData,
663 sketch: Sketch,
664 tag: Option<TagNode>,
665 exec_state: &mut ExecState,
666 args: Args,
667) -> Result<Sketch, KclError> {
668 let from = sketch.current_pen_position()?;
669 let AngledLineToData { angle, to: x_to } = data;
670
671 if angle.abs() == 270.0 {
672 return Err(KclError::Type(KclErrorDetails {
673 message: "Cannot have an x constrained angle of 270 degrees".to_string(),
674 source_ranges: vec![args.source_range],
675 }));
676 }
677
678 if angle.abs() == 90.0 {
679 return Err(KclError::Type(KclErrorDetails {
680 message: "Cannot have an x constrained angle of 90 degrees".to_string(),
681 source_ranges: vec![args.source_range],
682 }));
683 }
684
685 let x_component = x_to - from.x;
686 let y_component = x_component * f64::tan(angle.to_radians());
687 let y_to = from.y + y_component;
688
689 let new_sketch = straight_line(
690 StraightLineParams::absolute([x_to, y_to], sketch, tag),
691 exec_state,
692 args,
693 )
694 .await?;
695 Ok(new_sketch)
696}
697
698pub async fn angled_line_of_y_length(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
700 let (data, sketch, tag): (AngledLineData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
701
702 let new_sketch = inner_angled_line_of_y_length(data, sketch, tag, exec_state, args).await?;
703
704 Ok(KclValue::Sketch {
705 value: Box::new(new_sketch),
706 })
707}
708
709#[stdlib {
725 name = "angledLineOfYLength",
726}]
727async fn inner_angled_line_of_y_length(
728 data: AngledLineData,
729 sketch: Sketch,
730 tag: Option<TagNode>,
731 exec_state: &mut ExecState,
732 args: Args,
733) -> Result<Sketch, KclError> {
734 let (angle, length) = match data {
735 AngledLineData::AngleAndLengthNamed { angle, length } => (angle, length),
736 AngledLineData::AngleAndLengthPair(pair) => (pair[0], pair[1]),
737 };
738
739 if angle.abs() == 0.0 {
740 return Err(KclError::Type(KclErrorDetails {
741 message: "Cannot have a y constrained angle of 0 degrees".to_string(),
742 source_ranges: vec![args.source_range],
743 }));
744 }
745
746 if angle.abs() == 180.0 {
747 return Err(KclError::Type(KclErrorDetails {
748 message: "Cannot have a y constrained angle of 180 degrees".to_string(),
749 source_ranges: vec![args.source_range],
750 }));
751 }
752
753 let to = get_x_component(Angle::from_degrees(angle), length);
754
755 let new_sketch = straight_line(StraightLineParams::relative(to.into(), sketch, tag), exec_state, args).await?;
756
757 Ok(new_sketch)
758}
759
760pub async fn angled_line_to_y(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
762 let (data, sketch, tag): (AngledLineToData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
763
764 let new_sketch = inner_angled_line_to_y(data, sketch, tag, exec_state, args).await?;
765 Ok(KclValue::Sketch {
766 value: Box::new(new_sketch),
767 })
768}
769
770#[stdlib {
785 name = "angledLineToY",
786}]
787async fn inner_angled_line_to_y(
788 data: AngledLineToData,
789 sketch: Sketch,
790 tag: Option<TagNode>,
791 exec_state: &mut ExecState,
792 args: Args,
793) -> Result<Sketch, KclError> {
794 let from = sketch.current_pen_position()?;
795 let AngledLineToData { angle, to: y_to } = data;
796
797 if angle.abs() == 0.0 {
798 return Err(KclError::Type(KclErrorDetails {
799 message: "Cannot have a y constrained angle of 0 degrees".to_string(),
800 source_ranges: vec![args.source_range],
801 }));
802 }
803
804 if angle.abs() == 180.0 {
805 return Err(KclError::Type(KclErrorDetails {
806 message: "Cannot have a y constrained angle of 180 degrees".to_string(),
807 source_ranges: vec![args.source_range],
808 }));
809 }
810
811 let y_component = y_to - from.y;
812 let x_component = y_component / f64::tan(angle.to_radians());
813 let x_to = from.x + x_component;
814
815 let new_sketch = straight_line(
816 StraightLineParams::absolute([x_to, y_to], sketch, tag),
817 exec_state,
818 args,
819 )
820 .await?;
821 Ok(new_sketch)
822}
823
824#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
826#[ts(export)]
827#[serde(rename_all = "camelCase")]
828pub struct AngledLineThatIntersectsData {
830 pub angle: f64,
832 pub intersect_tag: TagIdentifier,
834 pub offset: Option<f64>,
836}
837
838pub async fn angled_line_that_intersects(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
840 let (data, sketch, tag): (AngledLineThatIntersectsData, Sketch, Option<TagNode>) =
841 args.get_data_and_sketch_and_tag()?;
842 let new_sketch = inner_angled_line_that_intersects(data, sketch, tag, exec_state, args).await?;
843 Ok(KclValue::Sketch {
844 value: Box::new(new_sketch),
845 })
846}
847
848#[stdlib {
868 name = "angledLineThatIntersects",
869}]
870async fn inner_angled_line_that_intersects(
871 data: AngledLineThatIntersectsData,
872 sketch: Sketch,
873 tag: Option<TagNode>,
874 exec_state: &mut ExecState,
875 args: Args,
876) -> Result<Sketch, KclError> {
877 let intersect_path = args.get_tag_engine_info(exec_state, &data.intersect_tag)?;
878 let path = intersect_path.path.clone().ok_or_else(|| {
879 KclError::Type(KclErrorDetails {
880 message: format!("Expected an intersect path with a path, found `{:?}`", intersect_path),
881 source_ranges: vec![args.source_range],
882 })
883 })?;
884
885 let from = sketch.current_pen_position()?;
886 let to = intersection_with_parallel_line(
887 &[path.get_from().into(), path.get_to().into()],
888 data.offset.unwrap_or_default(),
889 data.angle,
890 from,
891 );
892
893 let new_sketch = straight_line(StraightLineParams::absolute(to.into(), sketch, tag), exec_state, args).await?;
894 Ok(new_sketch)
895}
896
897pub async fn start_sketch_at(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
899 let data: [f64; 2] = args.get_data()?;
900
901 let sketch = inner_start_sketch_at(data, exec_state, args).await?;
902 Ok(KclValue::Sketch {
903 value: Box::new(sketch),
904 })
905}
906
907#[stdlib {
939 name = "startSketchAt",
940 deprecated = true,
941}]
942async fn inner_start_sketch_at(data: [f64; 2], exec_state: &mut ExecState, args: Args) -> Result<Sketch, KclError> {
943 let xy_plane = PlaneData::XY;
945 let sketch_surface = inner_start_sketch_on(SketchData::PlaneOrientation(xy_plane), None, exec_state, &args).await?;
946 let sketch = inner_start_profile_at(data, sketch_surface, None, exec_state, args).await?;
947 Ok(sketch)
948}
949
950#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
953#[ts(export)]
954#[serde(rename_all = "camelCase", untagged)]
955#[allow(clippy::large_enum_variant)]
956pub enum SketchData {
957 PlaneOrientation(PlaneData),
958 Plane(Box<Plane>),
959 Solid(Box<Solid>),
960}
961
962#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
964#[ts(export)]
965#[serde(rename_all = "camelCase")]
966#[allow(clippy::large_enum_variant)]
967pub enum PlaneData {
968 #[serde(rename = "XY", alias = "xy")]
970 XY,
971 #[serde(rename = "-XY", alias = "-xy")]
973 NegXY,
974 #[serde(rename = "XZ", alias = "xz")]
976 XZ,
977 #[serde(rename = "-XZ", alias = "-xz")]
979 NegXZ,
980 #[serde(rename = "YZ", alias = "yz")]
982 YZ,
983 #[serde(rename = "-YZ", alias = "-yz")]
985 NegYZ,
986 Plane {
988 origin: Point3d,
990 #[serde(rename = "xAxis")]
992 x_axis: Point3d,
993 #[serde(rename = "yAxis")]
995 y_axis: Point3d,
996 #[serde(rename = "zAxis")]
998 z_axis: Point3d,
999 },
1000}
1001
1002pub async fn start_sketch_on(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1004 let (data, tag): (SketchData, Option<FaceTag>) = args.get_data_and_optional_tag()?;
1005
1006 match inner_start_sketch_on(data, tag, exec_state, &args).await? {
1007 SketchSurface::Plane(value) => Ok(KclValue::Plane { value }),
1008 SketchSurface::Face(value) => Ok(KclValue::Face { value }),
1009 }
1010}
1011
1012#[stdlib {
1132 name = "startSketchOn",
1133 feature_tree_operation = true,
1134}]
1135async fn inner_start_sketch_on(
1136 data: SketchData,
1137 tag: Option<FaceTag>,
1138 exec_state: &mut ExecState,
1139 args: &Args,
1140) -> Result<SketchSurface, KclError> {
1141 match data {
1142 SketchData::PlaneOrientation(plane_data) => {
1143 let plane = make_sketch_plane_from_orientation(plane_data, exec_state, args).await?;
1144 Ok(SketchSurface::Plane(plane))
1145 }
1146 SketchData::Plane(plane) => {
1147 if plane.value == crate::exec::PlaneType::Uninit {
1148 let plane = make_sketch_plane_from_orientation(plane.into_plane_data(), exec_state, args).await?;
1149 Ok(SketchSurface::Plane(plane))
1150 } else {
1151 let id = exec_state.next_uuid();
1153 exec_state.add_artifact(Artifact::StartSketchOnPlane {
1154 id: ArtifactId::from(id),
1155 plane_id: plane.id,
1156 source_range: args.source_range,
1157 });
1158
1159 Ok(SketchSurface::Plane(plane))
1160 }
1161 }
1162 SketchData::Solid(solid) => {
1163 let Some(tag) = tag else {
1164 return Err(KclError::Type(KclErrorDetails {
1165 message: "Expected a tag for the face to sketch on".to_string(),
1166 source_ranges: vec![args.source_range],
1167 }));
1168 };
1169 let face = start_sketch_on_face(solid, tag, exec_state, args).await?;
1170
1171 let id = exec_state.next_uuid();
1173 exec_state.add_artifact(Artifact::StartSketchOnFace {
1174 id: ArtifactId::from(id),
1175 face_id: face.id,
1176 source_range: args.source_range,
1177 });
1178
1179 Ok(SketchSurface::Face(face))
1180 }
1181 }
1182}
1183
1184async fn start_sketch_on_face(
1185 solid: Box<Solid>,
1186 tag: FaceTag,
1187 exec_state: &mut ExecState,
1188 args: &Args,
1189) -> Result<Box<Face>, KclError> {
1190 let extrude_plane_id = tag.get_face_id(&solid, exec_state, args, true).await?;
1191
1192 Ok(Box::new(Face {
1193 id: extrude_plane_id,
1194 artifact_id: extrude_plane_id.into(),
1195 value: tag.to_string(),
1196 x_axis: solid.sketch.on.x_axis(),
1198 y_axis: solid.sketch.on.y_axis(),
1199 z_axis: solid.sketch.on.z_axis(),
1200 units: solid.units,
1201 solid,
1202 meta: vec![args.source_range.into()],
1203 }))
1204}
1205
1206async fn make_sketch_plane_from_orientation(
1207 data: PlaneData,
1208 exec_state: &mut ExecState,
1209 args: &Args,
1210) -> Result<Box<Plane>, KclError> {
1211 let plane = Plane::from_plane_data(data.clone(), exec_state);
1212
1213 let clobber = false;
1215 let size = LengthUnit(60.0);
1216 let hide = Some(true);
1217 match data {
1218 PlaneData::XY | PlaneData::NegXY | PlaneData::XZ | PlaneData::NegXZ | PlaneData::YZ | PlaneData::NegYZ => {
1219 let x_axis = match data {
1220 PlaneData::NegXY => Point3d::new(-1.0, 0.0, 0.0),
1221 PlaneData::NegXZ => Point3d::new(-1.0, 0.0, 0.0),
1222 PlaneData::NegYZ => Point3d::new(0.0, -1.0, 0.0),
1223 _ => plane.x_axis,
1224 };
1225 args.batch_modeling_cmd(
1226 plane.id,
1227 ModelingCmd::from(mcmd::MakePlane {
1228 clobber,
1229 origin: plane.origin.into(),
1230 size,
1231 x_axis: x_axis.into(),
1232 y_axis: plane.y_axis.into(),
1233 hide,
1234 }),
1235 )
1236 .await?;
1237 }
1238 PlaneData::Plane {
1239 origin,
1240 x_axis,
1241 y_axis,
1242 z_axis: _,
1243 } => {
1244 args.batch_modeling_cmd(
1245 plane.id,
1246 ModelingCmd::from(mcmd::MakePlane {
1247 clobber,
1248 origin: origin.into(),
1249 size,
1250 x_axis: x_axis.into(),
1251 y_axis: y_axis.into(),
1252 hide,
1253 }),
1254 )
1255 .await?;
1256 }
1257 }
1258
1259 Ok(Box::new(plane))
1260}
1261
1262pub async fn start_profile_at(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1264 let (start, sketch_surface, tag): ([f64; 2], SketchSurface, Option<TagNode>) =
1265 args.get_data_and_sketch_surface()?;
1266
1267 let sketch = inner_start_profile_at(start, sketch_surface, tag, exec_state, args).await?;
1268 Ok(KclValue::Sketch {
1269 value: Box::new(sketch),
1270 })
1271}
1272
1273#[stdlib {
1308 name = "startProfileAt",
1309}]
1310pub(crate) async fn inner_start_profile_at(
1311 to: [f64; 2],
1312 sketch_surface: SketchSurface,
1313 tag: Option<TagNode>,
1314 exec_state: &mut ExecState,
1315 args: Args,
1316) -> Result<Sketch, KclError> {
1317 match &sketch_surface {
1318 SketchSurface::Face(face) => {
1319 args.flush_batch_for_solid_set(exec_state, face.solid.clone().into())
1322 .await?;
1323 }
1324 SketchSurface::Plane(plane) if !plane.is_standard() => {
1325 args.batch_end_cmd(
1328 exec_state.next_uuid(),
1329 ModelingCmd::from(mcmd::ObjectVisible {
1330 object_id: plane.id,
1331 hidden: true,
1332 }),
1333 )
1334 .await?;
1335 }
1336 _ => {}
1337 }
1338
1339 let id = exec_state.next_uuid();
1342 args.batch_modeling_cmd(
1343 id,
1344 ModelingCmd::from(mcmd::EnableSketchMode {
1345 animated: false,
1346 ortho: false,
1347 entity_id: sketch_surface.id(),
1348 adjust_camera: false,
1349 planar_normal: if let SketchSurface::Plane(plane) = &sketch_surface {
1350 Some(plane.z_axis.into())
1352 } else {
1353 None
1354 },
1355 }),
1356 )
1357 .await?;
1358
1359 let id = exec_state.next_uuid();
1360 let path_id = exec_state.next_uuid();
1361
1362 args.batch_modeling_cmd(path_id, ModelingCmd::from(mcmd::StartPath::default()))
1363 .await?;
1364 args.batch_modeling_cmd(
1365 id,
1366 ModelingCmd::from(mcmd::MovePathPen {
1367 path: path_id.into(),
1368 to: KPoint2d::from(to).with_z(0.0).map(LengthUnit),
1369 }),
1370 )
1371 .await?;
1372
1373 let current_path = BasePath {
1374 from: to,
1375 to,
1376 tag: tag.clone(),
1377 units: sketch_surface.units(),
1378 geo_meta: GeoMeta {
1379 id,
1380 metadata: args.source_range.into(),
1381 },
1382 };
1383
1384 let sketch = Sketch {
1385 id: path_id,
1386 original_id: path_id,
1387 artifact_id: path_id.into(),
1388 on: sketch_surface.clone(),
1389 paths: vec![],
1390 units: sketch_surface.units(),
1391 meta: vec![args.source_range.into()],
1392 tags: if let Some(tag) = &tag {
1393 let mut tag_identifier: TagIdentifier = tag.into();
1394 tag_identifier.info = Some(TagEngineInfo {
1395 id: current_path.geo_meta.id,
1396 sketch: path_id,
1397 path: Some(Path::Base {
1398 base: current_path.clone(),
1399 }),
1400 surface: None,
1401 });
1402 IndexMap::from([(tag.name.to_string(), tag_identifier)])
1403 } else {
1404 Default::default()
1405 },
1406 start: current_path,
1407 };
1408 Ok(sketch)
1409}
1410
1411pub async fn profile_start_x(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1413 let sketch: Sketch = args.get_sketch()?;
1414 let ty = sketch.units.into();
1415 let x = inner_profile_start_x(sketch)?;
1416 Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
1417}
1418
1419#[stdlib {
1430 name = "profileStartX"
1431}]
1432pub(crate) fn inner_profile_start_x(sketch: Sketch) -> Result<f64, KclError> {
1433 Ok(sketch.start.to[0])
1434}
1435
1436pub async fn profile_start_y(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1438 let sketch: Sketch = args.get_sketch()?;
1439 let ty = sketch.units.into();
1440 let x = inner_profile_start_y(sketch)?;
1441 Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
1442}
1443
1444#[stdlib {
1454 name = "profileStartY"
1455}]
1456pub(crate) fn inner_profile_start_y(sketch: Sketch) -> Result<f64, KclError> {
1457 Ok(sketch.start.to[1])
1458}
1459
1460pub async fn profile_start(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1462 let sketch: Sketch = args.get_sketch()?;
1463 let ty = sketch.units.into();
1464 let point = inner_profile_start(sketch)?;
1465 Ok(KclValue::from_point2d(point, ty, args.into()))
1466}
1467
1468#[stdlib {
1481 name = "profileStart"
1482}]
1483pub(crate) fn inner_profile_start(sketch: Sketch) -> Result<[f64; 2], KclError> {
1484 Ok(sketch.start.to)
1485}
1486
1487pub async fn close(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1489 let sketch = args.get_unlabeled_kw_arg("sketch")?;
1490 let tag = args.get_kw_arg_opt(NEW_TAG_KW)?;
1491 let new_sketch = inner_close(sketch, tag, exec_state, args).await?;
1492 Ok(KclValue::Sketch {
1493 value: Box::new(new_sketch),
1494 })
1495}
1496
1497#[stdlib {
1519 name = "close",
1520 keywords = true,
1521 unlabeled_first = true,
1522 args = {
1523 sketch = { docs = "The sketch you want to close"},
1524 tag = { docs = "Create a new tag which refers to this line"},
1525 }
1526}]
1527pub(crate) async fn inner_close(
1528 sketch: Sketch,
1529 tag: Option<TagNode>,
1530 exec_state: &mut ExecState,
1531 args: Args,
1532) -> Result<Sketch, KclError> {
1533 let from = sketch.current_pen_position()?;
1534 let to: Point2d = sketch.start.from.into();
1535
1536 let id = exec_state.next_uuid();
1537
1538 args.batch_modeling_cmd(id, ModelingCmd::from(mcmd::ClosePath { path_id: sketch.id }))
1539 .await?;
1540
1541 if let SketchSurface::Plane(_) = sketch.on {
1543 args.batch_modeling_cmd(
1545 exec_state.next_uuid(),
1546 ModelingCmd::SketchModeDisable(mcmd::SketchModeDisable::default()),
1547 )
1548 .await?;
1549 }
1550
1551 let current_path = Path::ToPoint {
1552 base: BasePath {
1553 from: from.into(),
1554 to: to.into(),
1555 tag: tag.clone(),
1556 units: sketch.units,
1557 geo_meta: GeoMeta {
1558 id,
1559 metadata: args.source_range.into(),
1560 },
1561 },
1562 };
1563
1564 let mut new_sketch = sketch.clone();
1565 if let Some(tag) = &tag {
1566 new_sketch.add_tag(tag, ¤t_path);
1567 }
1568
1569 new_sketch.paths.push(current_path);
1570
1571 Ok(new_sketch)
1572}
1573
1574#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
1576#[ts(export)]
1577#[serde(rename_all = "camelCase", untagged)]
1578pub enum ArcData {
1579 AnglesAndRadius {
1581 #[serde(rename = "angleStart")]
1583 #[schemars(range(min = -360.0, max = 360.0))]
1584 angle_start: f64,
1585 #[serde(rename = "angleEnd")]
1587 #[schemars(range(min = -360.0, max = 360.0))]
1588 angle_end: f64,
1589 radius: f64,
1591 },
1592 CenterToRadius {
1594 center: [f64; 2],
1596 to: [f64; 2],
1598 radius: f64,
1600 },
1601}
1602
1603#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
1605#[ts(export)]
1606#[serde(rename_all = "camelCase")]
1607pub struct ArcToData {
1608 pub end: [f64; 2],
1610 pub interior: [f64; 2],
1612}
1613
1614pub async fn arc(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1616 let (data, sketch, tag): (ArcData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
1617
1618 let new_sketch = inner_arc(data, sketch, tag, exec_state, args).await?;
1619 Ok(KclValue::Sketch {
1620 value: Box::new(new_sketch),
1621 })
1622}
1623
1624#[stdlib {
1647 name = "arc",
1648}]
1649pub(crate) async fn inner_arc(
1650 data: ArcData,
1651 sketch: Sketch,
1652 tag: Option<TagNode>,
1653 exec_state: &mut ExecState,
1654 args: Args,
1655) -> Result<Sketch, KclError> {
1656 let from: Point2d = sketch.current_pen_position()?;
1657
1658 let (center, angle_start, angle_end, radius, end) = match &data {
1659 ArcData::AnglesAndRadius {
1660 angle_start,
1661 angle_end,
1662 radius,
1663 } => {
1664 let a_start = Angle::from_degrees(*angle_start);
1665 let a_end = Angle::from_degrees(*angle_end);
1666 let (center, end) = arc_center_and_end(from, a_start, a_end, *radius);
1667 (center, a_start, a_end, *radius, end)
1668 }
1669 ArcData::CenterToRadius { center, to, radius } => {
1670 let (angle_start, angle_end) = arc_angles(from, to.into(), center.into(), *radius, args.source_range)?;
1671 (center.into(), angle_start, angle_end, *radius, to.into())
1672 }
1673 };
1674
1675 if angle_start == angle_end {
1676 return Err(KclError::Type(KclErrorDetails {
1677 message: "Arc start and end angles must be different".to_string(),
1678 source_ranges: vec![args.source_range],
1679 }));
1680 }
1681 let ccw = angle_start < angle_end;
1682
1683 let id = exec_state.next_uuid();
1684
1685 args.batch_modeling_cmd(
1686 id,
1687 ModelingCmd::from(mcmd::ExtendPath {
1688 path: sketch.id.into(),
1689 segment: PathSegment::Arc {
1690 start: angle_start,
1691 end: angle_end,
1692 center: KPoint2d::from(center).map(LengthUnit),
1693 radius: LengthUnit(radius),
1694 relative: false,
1695 },
1696 }),
1697 )
1698 .await?;
1699
1700 let current_path = Path::Arc {
1701 base: BasePath {
1702 from: from.into(),
1703 to: end.into(),
1704 tag: tag.clone(),
1705 units: sketch.units,
1706 geo_meta: GeoMeta {
1707 id,
1708 metadata: args.source_range.into(),
1709 },
1710 },
1711 center: center.into(),
1712 radius,
1713 ccw,
1714 };
1715
1716 let mut new_sketch = sketch.clone();
1717 if let Some(tag) = &tag {
1718 new_sketch.add_tag(tag, ¤t_path);
1719 }
1720
1721 new_sketch.paths.push(current_path);
1722
1723 Ok(new_sketch)
1724}
1725
1726pub async fn arc_to(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1728 let (data, sketch, tag): (ArcToData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
1729
1730 let new_sketch = inner_arc_to(data, sketch, tag, exec_state, args).await?;
1731 Ok(KclValue::Sketch {
1732 value: Box::new(new_sketch),
1733 })
1734}
1735
1736#[stdlib {
1753 name = "arcTo",
1754}]
1755pub(crate) async fn inner_arc_to(
1756 data: ArcToData,
1757 sketch: Sketch,
1758 tag: Option<TagNode>,
1759 exec_state: &mut ExecState,
1760 args: Args,
1761) -> Result<Sketch, KclError> {
1762 let from: Point2d = sketch.current_pen_position()?;
1763 let id = exec_state.next_uuid();
1764
1765 args.batch_modeling_cmd(
1767 id,
1768 ModelingCmd::from(mcmd::ExtendPath {
1769 path: sketch.id.into(),
1770 segment: PathSegment::ArcTo {
1771 end: kcmc::shared::Point3d {
1772 x: LengthUnit(data.end[0]),
1773 y: LengthUnit(data.end[1]),
1774 z: LengthUnit(0.0),
1775 },
1776 interior: kcmc::shared::Point3d {
1777 x: LengthUnit(data.interior[0]),
1778 y: LengthUnit(data.interior[1]),
1779 z: LengthUnit(0.0),
1780 },
1781 relative: false,
1782 },
1783 }),
1784 )
1785 .await?;
1786
1787 let start = [from.x, from.y];
1788 let interior = data.interior;
1789 let end = data.end;
1790
1791 let center = calculate_circle_center(start, interior, end);
1793
1794 let sum_of_square_differences =
1797 (center[0] - start[0] * center[0] - start[0]) + (center[1] - start[1] * center[1] - start[1]);
1798 let radius = sum_of_square_differences.sqrt();
1799
1800 let ccw = is_ccw(start, interior, end);
1801
1802 let current_path = Path::Arc {
1803 base: BasePath {
1804 from: from.into(),
1805 to: data.end,
1806 tag: tag.clone(),
1807 units: sketch.units,
1808 geo_meta: GeoMeta {
1809 id,
1810 metadata: args.source_range.into(),
1811 },
1812 },
1813 center,
1814 radius,
1815 ccw,
1816 };
1817
1818 let mut new_sketch = sketch.clone();
1819 if let Some(tag) = &tag {
1820 new_sketch.add_tag(tag, ¤t_path);
1821 }
1822
1823 new_sketch.paths.push(current_path);
1824
1825 Ok(new_sketch)
1826}
1827
1828fn is_ccw(start: [f64; 2], interior: [f64; 2], end: [f64; 2]) -> bool {
1842 let t1 = (interior[0] - start[0]) * (end[1] - start[1]);
1843 let t2 = (end[0] - start[0]) * (interior[1] - start[1]);
1844 t1 > t2
1846}
1847
1848#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, JsonSchema, ts_rs::TS)]
1850#[ts(export)]
1851#[serde(rename_all = "camelCase", untagged)]
1852pub enum TangentialArcData {
1853 RadiusAndOffset {
1854 radius: f64,
1857 offset: f64,
1859 },
1860}
1861
1862pub async fn tangential_arc(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1864 let (data, sketch, tag): (TangentialArcData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
1865
1866 let new_sketch = inner_tangential_arc(data, sketch, tag, exec_state, args).await?;
1867 Ok(KclValue::Sketch {
1868 value: Box::new(new_sketch),
1869 })
1870}
1871
1872#[stdlib {
1896 name = "tangentialArc",
1897}]
1898async fn inner_tangential_arc(
1899 data: TangentialArcData,
1900 sketch: Sketch,
1901 tag: Option<TagNode>,
1902 exec_state: &mut ExecState,
1903 args: Args,
1904) -> Result<Sketch, KclError> {
1905 let from: Point2d = sketch.current_pen_position()?;
1906 let tangent_info = sketch.get_tangential_info_from_paths(); let tan_previous_point = tangent_info.tan_previous_point(from.into());
1909
1910 let id = exec_state.next_uuid();
1911
1912 let (center, to, ccw) = match data {
1913 TangentialArcData::RadiusAndOffset { radius, offset } => {
1914 let offset = Angle::from_degrees(offset);
1916
1917 let previous_end_tangent = Angle::from_radians(f64::atan2(
1920 from.y - tan_previous_point[1],
1921 from.x - tan_previous_point[0],
1922 ));
1923 let ccw = offset.to_degrees() > 0.0;
1926 let tangent_to_arc_start_angle = if ccw {
1927 Angle::from_degrees(-90.0)
1929 } else {
1930 Angle::from_degrees(90.0)
1932 };
1933 let start_angle = previous_end_tangent + tangent_to_arc_start_angle;
1936 let end_angle = start_angle + offset;
1937 let (center, to) = arc_center_and_end(from, start_angle, end_angle, radius);
1938
1939 args.batch_modeling_cmd(
1940 id,
1941 ModelingCmd::from(mcmd::ExtendPath {
1942 path: sketch.id.into(),
1943 segment: PathSegment::TangentialArc {
1944 radius: LengthUnit(radius),
1945 offset,
1946 },
1947 }),
1948 )
1949 .await?;
1950 (center, to.into(), ccw)
1951 }
1952 };
1953
1954 let current_path = Path::TangentialArc {
1955 ccw,
1956 center: center.into(),
1957 base: BasePath {
1958 from: from.into(),
1959 to,
1960 tag: tag.clone(),
1961 units: sketch.units,
1962 geo_meta: GeoMeta {
1963 id,
1964 metadata: args.source_range.into(),
1965 },
1966 },
1967 };
1968
1969 let mut new_sketch = sketch.clone();
1970 if let Some(tag) = &tag {
1971 new_sketch.add_tag(tag, ¤t_path);
1972 }
1973
1974 new_sketch.paths.push(current_path);
1975
1976 Ok(new_sketch)
1977}
1978
1979fn tan_arc_to(sketch: &Sketch, to: &[f64; 2]) -> ModelingCmd {
1980 ModelingCmd::from(mcmd::ExtendPath {
1981 path: sketch.id.into(),
1982 segment: PathSegment::TangentialArcTo {
1983 angle_snap_increment: None,
1984 to: KPoint2d::from(*to).with_z(0.0).map(LengthUnit),
1985 },
1986 })
1987}
1988
1989pub async fn tangential_arc_to(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1991 let (to, sketch, tag): ([f64; 2], Sketch, Option<TagNode>) = super::args::FromArgs::from_args(&args, 0)?;
1992
1993 let new_sketch = inner_tangential_arc_to(to, sketch, tag, exec_state, args).await?;
1994 Ok(KclValue::Sketch {
1995 value: Box::new(new_sketch),
1996 })
1997}
1998
1999pub async fn tangential_arc_to_relative(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2001 let (delta, sketch, tag): ([f64; 2], Sketch, Option<TagNode>) = super::args::FromArgs::from_args(&args, 0)?;
2002
2003 let new_sketch = inner_tangential_arc_to_relative(delta, sketch, tag, exec_state, args).await?;
2004 Ok(KclValue::Sketch {
2005 value: Box::new(new_sketch),
2006 })
2007}
2008
2009#[stdlib {
2027 name = "tangentialArcTo",
2028}]
2029async fn inner_tangential_arc_to(
2030 to: [f64; 2],
2031 sketch: Sketch,
2032 tag: Option<TagNode>,
2033 exec_state: &mut ExecState,
2034 args: Args,
2035) -> Result<Sketch, KclError> {
2036 let from: Point2d = sketch.current_pen_position()?;
2037 let tangent_info = sketch.get_tangential_info_from_paths();
2038 let tan_previous_point = tangent_info.tan_previous_point(from.into());
2039 let [to_x, to_y] = to;
2040 let result = get_tangential_arc_to_info(TangentialArcInfoInput {
2041 arc_start_point: [from.x, from.y],
2042 arc_end_point: to,
2043 tan_previous_point,
2044 obtuse: true,
2045 });
2046
2047 let delta = [to_x - from.x, to_y - from.y];
2048 let id = exec_state.next_uuid();
2049 args.batch_modeling_cmd(id, tan_arc_to(&sketch, &delta)).await?;
2050
2051 let current_path = Path::TangentialArcTo {
2052 base: BasePath {
2053 from: from.into(),
2054 to,
2055 tag: tag.clone(),
2056 units: sketch.units,
2057 geo_meta: GeoMeta {
2058 id,
2059 metadata: args.source_range.into(),
2060 },
2061 },
2062 center: result.center,
2063 ccw: result.ccw > 0,
2064 };
2065
2066 let mut new_sketch = sketch.clone();
2067 if let Some(tag) = &tag {
2068 new_sketch.add_tag(tag, ¤t_path);
2069 }
2070
2071 new_sketch.paths.push(current_path);
2072
2073 Ok(new_sketch)
2074}
2075
2076#[stdlib {
2094 name = "tangentialArcToRelative",
2095}]
2096async fn inner_tangential_arc_to_relative(
2097 delta: [f64; 2],
2098 sketch: Sketch,
2099 tag: Option<TagNode>,
2100 exec_state: &mut ExecState,
2101 args: Args,
2102) -> Result<Sketch, KclError> {
2103 let from: Point2d = sketch.current_pen_position()?;
2104 let to = [from.x + delta[0], from.y + delta[1]];
2105 let tangent_info = sketch.get_tangential_info_from_paths();
2106 let tan_previous_point = tangent_info.tan_previous_point(from.into());
2107
2108 let [dx, dy] = delta;
2109 let result = get_tangential_arc_to_info(TangentialArcInfoInput {
2110 arc_start_point: [from.x, from.y],
2111 arc_end_point: [from.x + dx, from.y + dy],
2112 tan_previous_point,
2113 obtuse: true,
2114 });
2115
2116 if result.center[0].is_infinite() {
2117 return Err(KclError::Semantic(KclErrorDetails {
2118 source_ranges: vec![args.source_range],
2119 message:
2120 "could not sketch tangential arc, because its center would be infinitely far away in the X direction"
2121 .to_owned(),
2122 }));
2123 } else if result.center[1].is_infinite() {
2124 return Err(KclError::Semantic(KclErrorDetails {
2125 source_ranges: vec![args.source_range],
2126 message:
2127 "could not sketch tangential arc, because its center would be infinitely far away in the Y direction"
2128 .to_owned(),
2129 }));
2130 }
2131
2132 let id = exec_state.next_uuid();
2133 args.batch_modeling_cmd(id, tan_arc_to(&sketch, &delta)).await?;
2134
2135 let current_path = Path::TangentialArcTo {
2136 base: BasePath {
2137 from: from.into(),
2138 to,
2139 tag: tag.clone(),
2140 units: sketch.units,
2141 geo_meta: GeoMeta {
2142 id,
2143 metadata: args.source_range.into(),
2144 },
2145 },
2146 center: result.center,
2147 ccw: result.ccw > 0,
2148 };
2149
2150 let mut new_sketch = sketch.clone();
2151 if let Some(tag) = &tag {
2152 new_sketch.add_tag(tag, ¤t_path);
2153 }
2154
2155 new_sketch.paths.push(current_path);
2156
2157 Ok(new_sketch)
2158}
2159
2160#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
2162#[ts(export)]
2163#[serde(rename_all = "camelCase")]
2164pub struct BezierData {
2165 pub to: [f64; 2],
2167 pub control1: [f64; 2],
2169 pub control2: [f64; 2],
2171}
2172
2173pub async fn bezier_curve(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2175 let (data, sketch, tag): (BezierData, Sketch, Option<TagNode>) = args.get_data_and_sketch_and_tag()?;
2176
2177 let new_sketch = inner_bezier_curve(data, sketch, tag, exec_state, args).await?;
2178 Ok(KclValue::Sketch {
2179 value: Box::new(new_sketch),
2180 })
2181}
2182
2183#[stdlib {
2202 name = "bezierCurve",
2203}]
2204async fn inner_bezier_curve(
2205 data: BezierData,
2206 sketch: Sketch,
2207 tag: Option<TagNode>,
2208 exec_state: &mut ExecState,
2209 args: Args,
2210) -> Result<Sketch, KclError> {
2211 let from = sketch.current_pen_position()?;
2212
2213 let relative = true;
2214 let delta = data.to;
2215 let to = [from.x + data.to[0], from.y + data.to[1]];
2216
2217 let id = exec_state.next_uuid();
2218
2219 args.batch_modeling_cmd(
2220 id,
2221 ModelingCmd::from(mcmd::ExtendPath {
2222 path: sketch.id.into(),
2223 segment: PathSegment::Bezier {
2224 control1: KPoint2d::from(data.control1).with_z(0.0).map(LengthUnit),
2225 control2: KPoint2d::from(data.control2).with_z(0.0).map(LengthUnit),
2226 end: KPoint2d::from(delta).with_z(0.0).map(LengthUnit),
2227 relative,
2228 },
2229 }),
2230 )
2231 .await?;
2232
2233 let current_path = Path::ToPoint {
2234 base: BasePath {
2235 from: from.into(),
2236 to,
2237 tag: tag.clone(),
2238 units: sketch.units,
2239 geo_meta: GeoMeta {
2240 id,
2241 metadata: args.source_range.into(),
2242 },
2243 },
2244 };
2245
2246 let mut new_sketch = sketch.clone();
2247 if let Some(tag) = &tag {
2248 new_sketch.add_tag(tag, ¤t_path);
2249 }
2250
2251 new_sketch.paths.push(current_path);
2252
2253 Ok(new_sketch)
2254}
2255
2256pub async fn hole(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2258 let (hole_sketch, sketch): (SketchSet, Sketch) = args.get_sketches()?;
2259
2260 let new_sketch = inner_hole(hole_sketch, sketch, exec_state, args).await?;
2261 Ok(KclValue::Sketch {
2262 value: Box::new(new_sketch),
2263 })
2264}
2265
2266#[stdlib {
2298 name = "hole",
2299 feature_tree_operation = true,
2300}]
2301async fn inner_hole(
2302 hole_sketch: SketchSet,
2303 sketch: Sketch,
2304 exec_state: &mut ExecState,
2305 args: Args,
2306) -> Result<Sketch, KclError> {
2307 let hole_sketches: Vec<Sketch> = hole_sketch.into();
2308 for hole_sketch in hole_sketches {
2309 args.batch_modeling_cmd(
2310 exec_state.next_uuid(),
2311 ModelingCmd::from(mcmd::Solid2dAddHole {
2312 object_id: sketch.id,
2313 hole_id: hole_sketch.id,
2314 }),
2315 )
2316 .await?;
2317
2318 args.batch_modeling_cmd(
2321 exec_state.next_uuid(),
2322 ModelingCmd::from(mcmd::ObjectVisible {
2323 object_id: hole_sketch.id,
2324 hidden: true,
2325 }),
2326 )
2327 .await?;
2328 }
2329
2330 Ok(sketch)
2331}
2332
2333#[cfg(test)]
2334mod tests {
2335
2336 use pretty_assertions::assert_eq;
2337
2338 use crate::{
2339 execution::TagIdentifier,
2340 std::{sketch::PlaneData, utils::calculate_circle_center},
2341 };
2342
2343 #[test]
2344 fn test_deserialize_plane_data() {
2345 let data = PlaneData::XY;
2346 let mut str_json = serde_json::to_string(&data).unwrap();
2347 assert_eq!(str_json, "\"XY\"");
2348
2349 str_json = "\"YZ\"".to_string();
2350 let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2351 assert_eq!(data, PlaneData::YZ);
2352
2353 str_json = "\"-YZ\"".to_string();
2354 let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2355 assert_eq!(data, PlaneData::NegYZ);
2356
2357 str_json = "\"-xz\"".to_string();
2358 let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2359 assert_eq!(data, PlaneData::NegXZ);
2360 }
2361
2362 #[test]
2363 fn test_deserialize_sketch_on_face_tag() {
2364 let data = "start";
2365 let mut str_json = serde_json::to_string(&data).unwrap();
2366 assert_eq!(str_json, "\"start\"");
2367
2368 str_json = "\"end\"".to_string();
2369 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2370 assert_eq!(
2371 data,
2372 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
2373 );
2374
2375 str_json = serde_json::to_string(&TagIdentifier {
2376 value: "thing".to_string(),
2377 info: None,
2378 meta: Default::default(),
2379 })
2380 .unwrap();
2381 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2382 assert_eq!(
2383 data,
2384 crate::std::sketch::FaceTag::Tag(Box::new(TagIdentifier {
2385 value: "thing".to_string(),
2386 info: None,
2387 meta: Default::default()
2388 }))
2389 );
2390
2391 str_json = "\"END\"".to_string();
2392 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2393 assert_eq!(
2394 data,
2395 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
2396 );
2397
2398 str_json = "\"start\"".to_string();
2399 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2400 assert_eq!(
2401 data,
2402 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
2403 );
2404
2405 str_json = "\"START\"".to_string();
2406 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2407 assert_eq!(
2408 data,
2409 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
2410 );
2411 }
2412
2413 #[test]
2414 fn test_circle_center() {
2415 let actual = calculate_circle_center([0.0, 0.0], [5.0, 5.0], [10.0, 0.0]);
2416 assert_eq!(actual[0], 5.0);
2417 assert_eq!(actual[1], 0.0);
2418 }
2419}