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::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#[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 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 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 #[serde(rename = "start", alias = "START")]
87 Start,
88 #[serde(rename = "end", alias = "END")]
92 End,
93}
94
95pub const NEW_TAG_KW: &str = "tag";
96
97pub async fn line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
99 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#[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, ¤t_path);
257 }
258
259 new_sketch.paths.push(current_path);
260
261 Ok(new_sketch)
262}
263
264pub 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#[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
319pub 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#[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
366pub 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#[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
416pub 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#[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#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
463#[ts(export)]
464#[serde(rename_all = "camelCase", untagged)]
465pub enum AngledLineData {
466 AngleAndLengthNamed {
468 angle: f64,
470 length: f64,
472 },
473 AngleAndLengthPair([f64; 2]),
475}
476
477pub 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#[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 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, ¤t_path);
559 }
560
561 new_sketch.paths.push(current_path);
562 Ok(new_sketch)
563}
564
565pub 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#[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#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
626#[ts(export)]
627#[serde(rename_all = "camelCase")]
628pub struct AngledLineToData {
629 pub angle: f64,
631 pub to: f64,
633}
634
635pub 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#[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
699pub 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#[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
761pub 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#[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#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
827#[ts(export)]
828#[serde(rename_all = "camelCase")]
829pub struct AngledLineThatIntersectsData {
831 pub angle: f64,
833 pub intersect_tag: TagIdentifier,
835 pub offset: Option<f64>,
837}
838
839pub 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#[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
898pub 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#[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 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#[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#[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 #[serde(rename = "XY", alias = "xy")]
971 XY,
972 #[serde(rename = "-XY", alias = "-xy")]
974 NegXY,
975 #[serde(rename = "XZ", alias = "xz")]
977 XZ,
978 #[serde(rename = "-XZ", alias = "-xz")]
980 NegXZ,
981 #[serde(rename = "YZ", alias = "yz")]
983 YZ,
984 #[serde(rename = "-YZ", alias = "-yz")]
986 NegYZ,
987 Plane {
989 origin: Point3d,
991 #[serde(rename = "xAxis")]
993 x_axis: Point3d,
994 #[serde(rename = "yAxis")]
996 y_axis: Point3d,
997 #[serde(rename = "zAxis")]
999 z_axis: Point3d,
1000 },
1001}
1002
1003pub 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#[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 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 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 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 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
1263pub 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#[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 args.flush_batch_for_solid_set(exec_state, face.solid.clone().into())
1323 .await?;
1324 }
1325 SketchSurface::Plane(plane) if !plane.is_standard() => {
1326 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 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 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
1412pub 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#[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
1437pub 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#[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
1461pub 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#[stdlib {
1482 name = "profileStart"
1483}]
1484pub(crate) fn inner_profile_start(sketch: Sketch) -> Result<[f64; 2], KclError> {
1485 Ok(sketch.start.to)
1486}
1487
1488pub 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#[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 let SketchSurface::Plane(_) = sketch.on {
1544 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, ¤t_path);
1568 }
1569
1570 new_sketch.paths.push(current_path);
1571
1572 Ok(new_sketch)
1573}
1574
1575#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
1577#[ts(export)]
1578#[serde(rename_all = "camelCase", untagged)]
1579pub enum ArcData {
1580 AnglesAndRadius {
1582 #[serde(rename = "angleStart")]
1584 #[schemars(range(min = -360.0, max = 360.0))]
1585 angle_start: f64,
1586 #[serde(rename = "angleEnd")]
1588 #[schemars(range(min = -360.0, max = 360.0))]
1589 angle_end: f64,
1590 radius: f64,
1592 },
1593 CenterToRadius {
1595 center: [f64; 2],
1597 to: [f64; 2],
1599 radius: f64,
1601 },
1602}
1603
1604#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
1606#[ts(export)]
1607#[serde(rename_all = "camelCase")]
1608pub struct ArcToData {
1609 pub end: [f64; 2],
1611 pub interior: [f64; 2],
1613}
1614
1615pub 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#[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, ¤t_path);
1720 }
1721
1722 new_sketch.paths.push(current_path);
1723
1724 Ok(new_sketch)
1725}
1726
1727pub 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#[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 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 let center = calculate_circle_center(start, interior, end);
1794
1795 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, ¤t_path);
1822 }
1823
1824 new_sketch.paths.push(current_path);
1825
1826 Ok(new_sketch)
1827}
1828
1829fn 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 t1 > t2
1847}
1848
1849#[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: f64,
1858 offset: f64,
1860 },
1861}
1862
1863pub 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#[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 let tangent_info = sketch.get_tangential_info_from_paths(); 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 let offset = Angle::from_degrees(offset);
1917
1918 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 let ccw = offset.to_degrees() > 0.0;
1927 let tangent_to_arc_start_angle = if ccw {
1928 Angle::from_degrees(-90.0)
1930 } else {
1931 Angle::from_degrees(90.0)
1933 };
1934 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, ¤t_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
1990pub 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
2000pub 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#[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, ¤t_path);
2070 }
2071
2072 new_sketch.paths.push(current_path);
2073
2074 Ok(new_sketch)
2075}
2076
2077#[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, ¤t_path);
2154 }
2155
2156 new_sketch.paths.push(current_path);
2157
2158 Ok(new_sketch)
2159}
2160
2161#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
2163#[ts(export)]
2164#[serde(rename_all = "camelCase")]
2165pub struct BezierData {
2166 pub to: [f64; 2],
2168 pub control1: [f64; 2],
2170 pub control2: [f64; 2],
2172}
2173
2174pub 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#[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, ¤t_path);
2250 }
2251
2252 new_sketch.paths.push(current_path);
2253
2254 Ok(new_sketch)
2255}
2256
2257pub 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#[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 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}