1use anyhow::Result;
4use indexmap::IndexMap;
5use kcmc::shared::Point2d as KPoint2d; use kcmc::shared::Point3d as KPoint3d; use kcmc::{ModelingCmd, each_cmd as mcmd, length_unit::LengthUnit, shared::Angle, websocket::ModelingCmdReq};
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 super::shapes::{get_radius, get_radius_labelled};
15#[cfg(feature = "artifact-graph")]
16use crate::execution::{Artifact, ArtifactId, CodeRef, StartSketchOnFace, StartSketchOnPlane};
17use crate::{
18 errors::{KclError, KclErrorDetails},
19 execution::{
20 BasePath, ExecState, Face, GeoMeta, KclValue, ModelingCmdMeta, Path, Plane, PlaneInfo, Point2d, Sketch,
21 SketchSurface, Solid, TagEngineInfo, TagIdentifier,
22 types::{ArrayLen, NumericType, PrimitiveType, RuntimeType, UnitLen},
23 },
24 parsing::ast::types::TagNode,
25 std::{
26 args::{Args, TyF64},
27 utils::{
28 TangentialArcInfoInput, arc_center_and_end, get_tangential_arc_to_info, get_x_component, get_y_component,
29 intersection_with_parallel_line, point_to_len_unit, point_to_mm, untyped_point_to_mm,
30 },
31 },
32};
33
34#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
36#[ts(export)]
37#[serde(rename_all = "snake_case", untagged)]
38pub enum FaceTag {
39 StartOrEnd(StartOrEnd),
40 Tag(Box<TagIdentifier>),
42}
43
44impl std::fmt::Display for FaceTag {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 match self {
47 FaceTag::Tag(t) => write!(f, "{t}"),
48 FaceTag::StartOrEnd(StartOrEnd::Start) => write!(f, "start"),
49 FaceTag::StartOrEnd(StartOrEnd::End) => write!(f, "end"),
50 }
51 }
52}
53
54impl FaceTag {
55 pub async fn get_face_id(
57 &self,
58 solid: &Solid,
59 exec_state: &mut ExecState,
60 args: &Args,
61 must_be_planar: bool,
62 ) -> Result<uuid::Uuid, KclError> {
63 match self {
64 FaceTag::Tag(t) => args.get_adjacent_face_to_tag(exec_state, t, must_be_planar).await,
65 FaceTag::StartOrEnd(StartOrEnd::Start) => solid.start_cap_id.ok_or_else(|| {
66 KclError::new_type(KclErrorDetails::new(
67 "Expected a start face".to_string(),
68 vec![args.source_range],
69 ))
70 }),
71 FaceTag::StartOrEnd(StartOrEnd::End) => solid.end_cap_id.ok_or_else(|| {
72 KclError::new_type(KclErrorDetails::new(
73 "Expected an end face".to_string(),
74 vec![args.source_range],
75 ))
76 }),
77 }
78 }
79}
80
81#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, FromStr, Display)]
82#[ts(export)]
83#[serde(rename_all = "snake_case")]
84#[display(style = "snake_case")]
85pub enum StartOrEnd {
86 #[serde(rename = "start", alias = "START")]
90 Start,
91 #[serde(rename = "end", alias = "END")]
95 End,
96}
97
98pub const NEW_TAG_KW: &str = "tag";
99
100pub async fn involute_circular(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
101 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::sketch(), exec_state)?;
102
103 let start_radius: Option<TyF64> = args.get_kw_arg_opt("startRadius", &RuntimeType::length(), exec_state)?;
104 let end_radius: Option<TyF64> = args.get_kw_arg_opt("endRadius", &RuntimeType::length(), exec_state)?;
105 let start_diameter: Option<TyF64> = args.get_kw_arg_opt("startDiameter", &RuntimeType::length(), exec_state)?;
106 let end_diameter: Option<TyF64> = args.get_kw_arg_opt("endDiameter", &RuntimeType::length(), exec_state)?;
107 let angle: TyF64 = args.get_kw_arg("angle", &RuntimeType::angle(), exec_state)?;
108 let reverse = args.get_kw_arg_opt("reverse", &RuntimeType::bool(), exec_state)?;
109 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
110 let new_sketch = inner_involute_circular(
111 sketch,
112 start_radius,
113 end_radius,
114 start_diameter,
115 end_diameter,
116 angle,
117 reverse,
118 tag,
119 exec_state,
120 args,
121 )
122 .await?;
123 Ok(KclValue::Sketch {
124 value: Box::new(new_sketch),
125 })
126}
127
128fn involute_curve(radius: f64, angle: f64) -> (f64, f64) {
129 (
130 radius * (libm::cos(angle) + angle * libm::sin(angle)),
131 radius * (libm::sin(angle) - angle * libm::cos(angle)),
132 )
133}
134
135#[allow(clippy::too_many_arguments)]
136async fn inner_involute_circular(
137 sketch: Sketch,
138 start_radius: Option<TyF64>,
139 end_radius: Option<TyF64>,
140 start_diameter: Option<TyF64>,
141 end_diameter: Option<TyF64>,
142 angle: TyF64,
143 reverse: Option<bool>,
144 tag: Option<TagNode>,
145 exec_state: &mut ExecState,
146 args: Args,
147) -> Result<Sketch, KclError> {
148 let id = exec_state.next_uuid();
149
150 let longer_args_dot_source_range = args.source_range;
151 let start_radius = get_radius_labelled(
152 start_radius,
153 start_diameter,
154 args.source_range,
155 "startRadius",
156 "startDiameter",
157 )?;
158 let end_radius = get_radius_labelled(
159 end_radius,
160 end_diameter,
161 longer_args_dot_source_range,
162 "endRadius",
163 "endDiameter",
164 )?;
165
166 exec_state
167 .batch_modeling_cmd(
168 ModelingCmdMeta::from_args_id(&args, id),
169 ModelingCmd::from(mcmd::ExtendPath {
170 path: sketch.id.into(),
171 segment: PathSegment::CircularInvolute {
172 start_radius: LengthUnit(start_radius.to_mm()),
173 end_radius: LengthUnit(end_radius.to_mm()),
174 angle: Angle::from_degrees(angle.to_degrees()),
175 reverse: reverse.unwrap_or_default(),
176 },
177 }),
178 )
179 .await?;
180
181 let from = sketch.current_pen_position()?;
182
183 let start_radius = start_radius.to_length_units(from.units);
184 let end_radius = end_radius.to_length_units(from.units);
185
186 let mut end: KPoint3d<f64> = Default::default(); let theta = f64::sqrt(end_radius * end_radius - start_radius * start_radius) / start_radius;
188 let (x, y) = involute_curve(start_radius, theta);
189
190 end.x = x * libm::cos(angle.to_radians()) - y * libm::sin(angle.to_radians());
191 end.y = x * libm::sin(angle.to_radians()) + y * libm::cos(angle.to_radians());
192
193 end.x -= start_radius * libm::cos(angle.to_radians());
194 end.y -= start_radius * libm::sin(angle.to_radians());
195
196 if reverse.unwrap_or_default() {
197 end.x = -end.x;
198 }
199
200 end.x += from.x;
201 end.y += from.y;
202
203 let current_path = Path::ToPoint {
204 base: BasePath {
205 from: from.ignore_units(),
206 to: [end.x, end.y],
207 tag: tag.clone(),
208 units: sketch.units,
209 geo_meta: GeoMeta {
210 id,
211 metadata: args.source_range.into(),
212 },
213 },
214 };
215
216 let mut new_sketch = sketch.clone();
217 if let Some(tag) = &tag {
218 new_sketch.add_tag(tag, ¤t_path, exec_state);
219 }
220 new_sketch.paths.push(current_path);
221 Ok(new_sketch)
222}
223
224pub async fn line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
226 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::sketch(), exec_state)?;
227 let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
228 let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
229 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
230
231 let new_sketch = inner_line(sketch, end_absolute, end, tag, exec_state, args).await?;
232 Ok(KclValue::Sketch {
233 value: Box::new(new_sketch),
234 })
235}
236
237async fn inner_line(
238 sketch: Sketch,
239 end_absolute: Option<[TyF64; 2]>,
240 end: Option<[TyF64; 2]>,
241 tag: Option<TagNode>,
242 exec_state: &mut ExecState,
243 args: Args,
244) -> Result<Sketch, KclError> {
245 straight_line(
246 StraightLineParams {
247 sketch,
248 end_absolute,
249 end,
250 tag,
251 relative_name: "end",
252 },
253 exec_state,
254 args,
255 )
256 .await
257}
258
259struct StraightLineParams {
260 sketch: Sketch,
261 end_absolute: Option<[TyF64; 2]>,
262 end: Option<[TyF64; 2]>,
263 tag: Option<TagNode>,
264 relative_name: &'static str,
265}
266
267impl StraightLineParams {
268 fn relative(p: [TyF64; 2], sketch: Sketch, tag: Option<TagNode>) -> Self {
269 Self {
270 sketch,
271 tag,
272 end: Some(p),
273 end_absolute: None,
274 relative_name: "end",
275 }
276 }
277 fn absolute(p: [TyF64; 2], sketch: Sketch, tag: Option<TagNode>) -> Self {
278 Self {
279 sketch,
280 tag,
281 end: None,
282 end_absolute: Some(p),
283 relative_name: "end",
284 }
285 }
286}
287
288async fn straight_line(
289 StraightLineParams {
290 sketch,
291 end,
292 end_absolute,
293 tag,
294 relative_name,
295 }: StraightLineParams,
296 exec_state: &mut ExecState,
297 args: Args,
298) -> Result<Sketch, KclError> {
299 let from = sketch.current_pen_position()?;
300 let (point, is_absolute) = match (end_absolute, end) {
301 (Some(_), Some(_)) => {
302 return Err(KclError::new_semantic(KclErrorDetails::new(
303 "You cannot give both `end` and `endAbsolute` params, you have to choose one or the other".to_owned(),
304 vec![args.source_range],
305 )));
306 }
307 (Some(end_absolute), None) => (end_absolute, true),
308 (None, Some(end)) => (end, false),
309 (None, None) => {
310 return Err(KclError::new_semantic(KclErrorDetails::new(
311 format!("You must supply either `{relative_name}` or `endAbsolute` arguments"),
312 vec![args.source_range],
313 )));
314 }
315 };
316
317 let id = exec_state.next_uuid();
318 exec_state
319 .batch_modeling_cmd(
320 ModelingCmdMeta::from_args_id(&args, id),
321 ModelingCmd::from(mcmd::ExtendPath {
322 path: sketch.id.into(),
323 segment: PathSegment::Line {
324 end: KPoint2d::from(point_to_mm(point.clone())).with_z(0.0).map(LengthUnit),
325 relative: !is_absolute,
326 },
327 }),
328 )
329 .await?;
330
331 let end = if is_absolute {
332 point_to_len_unit(point, from.units)
333 } else {
334 let from = sketch.current_pen_position()?;
335 let point = point_to_len_unit(point, from.units);
336 [from.x + point[0], from.y + point[1]]
337 };
338
339 let current_path = Path::ToPoint {
340 base: BasePath {
341 from: from.ignore_units(),
342 to: end,
343 tag: tag.clone(),
344 units: sketch.units,
345 geo_meta: GeoMeta {
346 id,
347 metadata: args.source_range.into(),
348 },
349 },
350 };
351
352 let mut new_sketch = sketch.clone();
353 if let Some(tag) = &tag {
354 new_sketch.add_tag(tag, ¤t_path, exec_state);
355 }
356
357 new_sketch.paths.push(current_path);
358
359 Ok(new_sketch)
360}
361
362pub async fn x_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
364 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
365 let length: Option<TyF64> = args.get_kw_arg_opt("length", &RuntimeType::length(), exec_state)?;
366 let end_absolute: Option<TyF64> = args.get_kw_arg_opt("endAbsolute", &RuntimeType::length(), exec_state)?;
367 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
368
369 let new_sketch = inner_x_line(sketch, length, end_absolute, tag, exec_state, args).await?;
370 Ok(KclValue::Sketch {
371 value: Box::new(new_sketch),
372 })
373}
374
375async fn inner_x_line(
376 sketch: Sketch,
377 length: Option<TyF64>,
378 end_absolute: Option<TyF64>,
379 tag: Option<TagNode>,
380 exec_state: &mut ExecState,
381 args: Args,
382) -> Result<Sketch, KclError> {
383 let from = sketch.current_pen_position()?;
384 straight_line(
385 StraightLineParams {
386 sketch,
387 end_absolute: end_absolute.map(|x| [x, from.into_y()]),
388 end: length.map(|x| [x, TyF64::new(0.0, NumericType::mm())]),
389 tag,
390 relative_name: "length",
391 },
392 exec_state,
393 args,
394 )
395 .await
396}
397
398pub async fn y_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
400 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
401 let length: Option<TyF64> = args.get_kw_arg_opt("length", &RuntimeType::length(), exec_state)?;
402 let end_absolute: Option<TyF64> = args.get_kw_arg_opt("endAbsolute", &RuntimeType::length(), exec_state)?;
403 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
404
405 let new_sketch = inner_y_line(sketch, length, end_absolute, tag, exec_state, args).await?;
406 Ok(KclValue::Sketch {
407 value: Box::new(new_sketch),
408 })
409}
410
411async fn inner_y_line(
412 sketch: Sketch,
413 length: Option<TyF64>,
414 end_absolute: Option<TyF64>,
415 tag: Option<TagNode>,
416 exec_state: &mut ExecState,
417 args: Args,
418) -> Result<Sketch, KclError> {
419 let from = sketch.current_pen_position()?;
420 straight_line(
421 StraightLineParams {
422 sketch,
423 end_absolute: end_absolute.map(|y| [from.into_x(), y]),
424 end: length.map(|y| [TyF64::new(0.0, NumericType::mm()), y]),
425 tag,
426 relative_name: "length",
427 },
428 exec_state,
429 args,
430 )
431 .await
432}
433
434pub async fn angled_line(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
436 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::sketch(), exec_state)?;
437 let angle: TyF64 = args.get_kw_arg("angle", &RuntimeType::degrees(), exec_state)?;
438 let length: Option<TyF64> = args.get_kw_arg_opt("length", &RuntimeType::length(), exec_state)?;
439 let length_x: Option<TyF64> = args.get_kw_arg_opt("lengthX", &RuntimeType::length(), exec_state)?;
440 let length_y: Option<TyF64> = args.get_kw_arg_opt("lengthY", &RuntimeType::length(), exec_state)?;
441 let end_absolute_x: Option<TyF64> = args.get_kw_arg_opt("endAbsoluteX", &RuntimeType::length(), exec_state)?;
442 let end_absolute_y: Option<TyF64> = args.get_kw_arg_opt("endAbsoluteY", &RuntimeType::length(), exec_state)?;
443 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
444
445 let new_sketch = inner_angled_line(
446 sketch,
447 angle.n,
448 length,
449 length_x,
450 length_y,
451 end_absolute_x,
452 end_absolute_y,
453 tag,
454 exec_state,
455 args,
456 )
457 .await?;
458 Ok(KclValue::Sketch {
459 value: Box::new(new_sketch),
460 })
461}
462
463#[allow(clippy::too_many_arguments)]
464async fn inner_angled_line(
465 sketch: Sketch,
466 angle: f64,
467 length: Option<TyF64>,
468 length_x: Option<TyF64>,
469 length_y: Option<TyF64>,
470 end_absolute_x: Option<TyF64>,
471 end_absolute_y: Option<TyF64>,
472 tag: Option<TagNode>,
473 exec_state: &mut ExecState,
474 args: Args,
475) -> Result<Sketch, KclError> {
476 let options_given = [&length, &length_x, &length_y, &end_absolute_x, &end_absolute_y]
477 .iter()
478 .filter(|x| x.is_some())
479 .count();
480 if options_given > 1 {
481 return Err(KclError::new_type(KclErrorDetails::new(
482 " one of `length`, `lengthX`, `lengthY`, `endAbsoluteX`, `endAbsoluteY` can be given".to_string(),
483 vec![args.source_range],
484 )));
485 }
486 if let Some(length_x) = length_x {
487 return inner_angled_line_of_x_length(angle, length_x, sketch, tag, exec_state, args).await;
488 }
489 if let Some(length_y) = length_y {
490 return inner_angled_line_of_y_length(angle, length_y, sketch, tag, exec_state, args).await;
491 }
492 let angle_degrees = angle;
493 match (length, length_x, length_y, end_absolute_x, end_absolute_y) {
494 (Some(length), None, None, None, None) => {
495 inner_angled_line_length(sketch, angle_degrees, length, tag, exec_state, args).await
496 }
497 (None, Some(length_x), None, None, None) => {
498 inner_angled_line_of_x_length(angle_degrees, length_x, sketch, tag, exec_state, args).await
499 }
500 (None, None, Some(length_y), None, None) => {
501 inner_angled_line_of_y_length(angle_degrees, length_y, sketch, tag, exec_state, args).await
502 }
503 (None, None, None, Some(end_absolute_x), None) => {
504 inner_angled_line_to_x(angle_degrees, end_absolute_x, sketch, tag, exec_state, args).await
505 }
506 (None, None, None, None, Some(end_absolute_y)) => {
507 inner_angled_line_to_y(angle_degrees, end_absolute_y, sketch, tag, exec_state, args).await
508 }
509 (None, None, None, None, None) => Err(KclError::new_type(KclErrorDetails::new(
510 "One of `length`, `lengthX`, `lengthY`, `endAbsoluteX`, `endAbsoluteY` must be given".to_string(),
511 vec![args.source_range],
512 ))),
513 _ => Err(KclError::new_type(KclErrorDetails::new(
514 "Only One of `length`, `lengthX`, `lengthY`, `endAbsoluteX`, `endAbsoluteY` can be given".to_owned(),
515 vec![args.source_range],
516 ))),
517 }
518}
519
520async fn inner_angled_line_length(
521 sketch: Sketch,
522 angle_degrees: f64,
523 length: TyF64,
524 tag: Option<TagNode>,
525 exec_state: &mut ExecState,
526 args: Args,
527) -> Result<Sketch, KclError> {
528 let from = sketch.current_pen_position()?;
529 let length = length.to_length_units(from.units);
530
531 let delta: [f64; 2] = [
533 length * libm::cos(angle_degrees.to_radians()),
534 length * libm::sin(angle_degrees.to_radians()),
535 ];
536 let relative = true;
537
538 let to: [f64; 2] = [from.x + delta[0], from.y + delta[1]];
539
540 let id = exec_state.next_uuid();
541
542 exec_state
543 .batch_modeling_cmd(
544 ModelingCmdMeta::from_args_id(&args, id),
545 ModelingCmd::from(mcmd::ExtendPath {
546 path: sketch.id.into(),
547 segment: PathSegment::Line {
548 end: KPoint2d::from(untyped_point_to_mm(delta, from.units))
549 .with_z(0.0)
550 .map(LengthUnit),
551 relative,
552 },
553 }),
554 )
555 .await?;
556
557 let current_path = Path::ToPoint {
558 base: BasePath {
559 from: from.ignore_units(),
560 to,
561 tag: tag.clone(),
562 units: sketch.units,
563 geo_meta: GeoMeta {
564 id,
565 metadata: args.source_range.into(),
566 },
567 },
568 };
569
570 let mut new_sketch = sketch.clone();
571 if let Some(tag) = &tag {
572 new_sketch.add_tag(tag, ¤t_path, exec_state);
573 }
574
575 new_sketch.paths.push(current_path);
576 Ok(new_sketch)
577}
578
579async fn inner_angled_line_of_x_length(
580 angle_degrees: f64,
581 length: TyF64,
582 sketch: Sketch,
583 tag: Option<TagNode>,
584 exec_state: &mut ExecState,
585 args: Args,
586) -> Result<Sketch, KclError> {
587 if angle_degrees.abs() == 270.0 {
588 return Err(KclError::new_type(KclErrorDetails::new(
589 "Cannot have an x constrained angle of 270 degrees".to_string(),
590 vec![args.source_range],
591 )));
592 }
593
594 if angle_degrees.abs() == 90.0 {
595 return Err(KclError::new_type(KclErrorDetails::new(
596 "Cannot have an x constrained angle of 90 degrees".to_string(),
597 vec![args.source_range],
598 )));
599 }
600
601 let to = get_y_component(Angle::from_degrees(angle_degrees), length.n);
602 let to = [TyF64::new(to[0], length.ty), TyF64::new(to[1], length.ty)];
603
604 let new_sketch = straight_line(StraightLineParams::relative(to, sketch, tag), exec_state, args).await?;
605
606 Ok(new_sketch)
607}
608
609async fn inner_angled_line_to_x(
610 angle_degrees: f64,
611 x_to: TyF64,
612 sketch: Sketch,
613 tag: Option<TagNode>,
614 exec_state: &mut ExecState,
615 args: Args,
616) -> Result<Sketch, KclError> {
617 let from = sketch.current_pen_position()?;
618
619 if angle_degrees.abs() == 270.0 {
620 return Err(KclError::new_type(KclErrorDetails::new(
621 "Cannot have an x constrained angle of 270 degrees".to_string(),
622 vec![args.source_range],
623 )));
624 }
625
626 if angle_degrees.abs() == 90.0 {
627 return Err(KclError::new_type(KclErrorDetails::new(
628 "Cannot have an x constrained angle of 90 degrees".to_string(),
629 vec![args.source_range],
630 )));
631 }
632
633 let x_component = x_to.to_length_units(from.units) - from.x;
634 let y_component = x_component * libm::tan(angle_degrees.to_radians());
635 let y_to = from.y + y_component;
636
637 let new_sketch = straight_line(
638 StraightLineParams::absolute([x_to, TyF64::new(y_to, from.units.into())], sketch, tag),
639 exec_state,
640 args,
641 )
642 .await?;
643 Ok(new_sketch)
644}
645
646async fn inner_angled_line_of_y_length(
647 angle_degrees: f64,
648 length: TyF64,
649 sketch: Sketch,
650 tag: Option<TagNode>,
651 exec_state: &mut ExecState,
652 args: Args,
653) -> Result<Sketch, KclError> {
654 if angle_degrees.abs() == 0.0 {
655 return Err(KclError::new_type(KclErrorDetails::new(
656 "Cannot have a y constrained angle of 0 degrees".to_string(),
657 vec![args.source_range],
658 )));
659 }
660
661 if angle_degrees.abs() == 180.0 {
662 return Err(KclError::new_type(KclErrorDetails::new(
663 "Cannot have a y constrained angle of 180 degrees".to_string(),
664 vec![args.source_range],
665 )));
666 }
667
668 let to = get_x_component(Angle::from_degrees(angle_degrees), length.n);
669 let to = [TyF64::new(to[0], length.ty), TyF64::new(to[1], length.ty)];
670
671 let new_sketch = straight_line(StraightLineParams::relative(to, sketch, tag), exec_state, args).await?;
672
673 Ok(new_sketch)
674}
675
676async fn inner_angled_line_to_y(
677 angle_degrees: f64,
678 y_to: TyF64,
679 sketch: Sketch,
680 tag: Option<TagNode>,
681 exec_state: &mut ExecState,
682 args: Args,
683) -> Result<Sketch, KclError> {
684 let from = sketch.current_pen_position()?;
685
686 if angle_degrees.abs() == 0.0 {
687 return Err(KclError::new_type(KclErrorDetails::new(
688 "Cannot have a y constrained angle of 0 degrees".to_string(),
689 vec![args.source_range],
690 )));
691 }
692
693 if angle_degrees.abs() == 180.0 {
694 return Err(KclError::new_type(KclErrorDetails::new(
695 "Cannot have a y constrained angle of 180 degrees".to_string(),
696 vec![args.source_range],
697 )));
698 }
699
700 let y_component = y_to.to_length_units(from.units) - from.y;
701 let x_component = y_component / libm::tan(angle_degrees.to_radians());
702 let x_to = from.x + x_component;
703
704 let new_sketch = straight_line(
705 StraightLineParams::absolute([TyF64::new(x_to, from.units.into()), y_to], sketch, tag),
706 exec_state,
707 args,
708 )
709 .await?;
710 Ok(new_sketch)
711}
712
713pub async fn angled_line_that_intersects(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
715 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
716 let angle: TyF64 = args.get_kw_arg("angle", &RuntimeType::angle(), exec_state)?;
717 let intersect_tag: TagIdentifier = args.get_kw_arg("intersectTag", &RuntimeType::tagged_edge(), exec_state)?;
718 let offset = args.get_kw_arg_opt("offset", &RuntimeType::length(), exec_state)?;
719 let tag: Option<TagNode> = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
720 let new_sketch =
721 inner_angled_line_that_intersects(sketch, angle, intersect_tag, offset, tag, exec_state, args).await?;
722 Ok(KclValue::Sketch {
723 value: Box::new(new_sketch),
724 })
725}
726
727pub async fn inner_angled_line_that_intersects(
728 sketch: Sketch,
729 angle: TyF64,
730 intersect_tag: TagIdentifier,
731 offset: Option<TyF64>,
732 tag: Option<TagNode>,
733 exec_state: &mut ExecState,
734 args: Args,
735) -> Result<Sketch, KclError> {
736 let intersect_path = args.get_tag_engine_info(exec_state, &intersect_tag)?;
737 let path = intersect_path.path.clone().ok_or_else(|| {
738 KclError::new_type(KclErrorDetails::new(
739 format!("Expected an intersect path with a path, found `{intersect_path:?}`"),
740 vec![args.source_range],
741 ))
742 })?;
743
744 let from = sketch.current_pen_position()?;
745 let to = intersection_with_parallel_line(
746 &[
747 point_to_len_unit(path.get_from(), from.units),
748 point_to_len_unit(path.get_to(), from.units),
749 ],
750 offset.map(|t| t.to_length_units(from.units)).unwrap_or_default(),
751 angle.to_degrees(),
752 from.ignore_units(),
753 );
754 let to = [
755 TyF64::new(to[0], from.units.into()),
756 TyF64::new(to[1], from.units.into()),
757 ];
758
759 straight_line(StraightLineParams::absolute(to, sketch, tag), exec_state, args).await
760}
761
762#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
765#[ts(export)]
766#[serde(rename_all = "camelCase", untagged)]
767#[allow(clippy::large_enum_variant)]
768pub enum SketchData {
769 PlaneOrientation(PlaneData),
770 Plane(Box<Plane>),
771 Solid(Box<Solid>),
772}
773
774#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
776#[ts(export)]
777#[serde(rename_all = "camelCase")]
778#[allow(clippy::large_enum_variant)]
779pub enum PlaneData {
780 #[serde(rename = "XY", alias = "xy")]
782 XY,
783 #[serde(rename = "-XY", alias = "-xy")]
785 NegXY,
786 #[serde(rename = "XZ", alias = "xz")]
788 XZ,
789 #[serde(rename = "-XZ", alias = "-xz")]
791 NegXZ,
792 #[serde(rename = "YZ", alias = "yz")]
794 YZ,
795 #[serde(rename = "-YZ", alias = "-yz")]
797 NegYZ,
798 Plane(PlaneInfo),
800}
801
802pub async fn start_sketch_on(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
804 let data = args.get_unlabeled_kw_arg(
805 "planeOrSolid",
806 &RuntimeType::Union(vec![RuntimeType::solid(), RuntimeType::plane()]),
807 exec_state,
808 )?;
809 let face = args.get_kw_arg_opt("face", &RuntimeType::tagged_face(), exec_state)?;
810
811 match inner_start_sketch_on(data, face, exec_state, &args).await? {
812 SketchSurface::Plane(value) => Ok(KclValue::Plane { value }),
813 SketchSurface::Face(value) => Ok(KclValue::Face { value }),
814 }
815}
816
817async fn inner_start_sketch_on(
818 plane_or_solid: SketchData,
819 face: Option<FaceTag>,
820 exec_state: &mut ExecState,
821 args: &Args,
822) -> Result<SketchSurface, KclError> {
823 match plane_or_solid {
824 SketchData::PlaneOrientation(plane_data) => {
825 let plane = make_sketch_plane_from_orientation(plane_data, exec_state, args).await?;
826 Ok(SketchSurface::Plane(plane))
827 }
828 SketchData::Plane(plane) => {
829 if plane.value == crate::exec::PlaneType::Uninit {
830 if plane.info.origin.units == UnitLen::Unknown {
831 return Err(KclError::new_semantic(KclErrorDetails::new(
832 "Origin of plane has unknown units".to_string(),
833 vec![args.source_range],
834 )));
835 }
836 let plane = make_sketch_plane_from_orientation(plane.info.into_plane_data(), exec_state, args).await?;
837 Ok(SketchSurface::Plane(plane))
838 } else {
839 #[cfg(feature = "artifact-graph")]
841 {
842 let id = exec_state.next_uuid();
843 exec_state.add_artifact(Artifact::StartSketchOnPlane(StartSketchOnPlane {
844 id: ArtifactId::from(id),
845 plane_id: plane.artifact_id,
846 code_ref: CodeRef::placeholder(args.source_range),
847 }));
848 }
849
850 Ok(SketchSurface::Plane(plane))
851 }
852 }
853 SketchData::Solid(solid) => {
854 let Some(tag) = face else {
855 return Err(KclError::new_type(KclErrorDetails::new(
856 "Expected a tag for the face to sketch on".to_string(),
857 vec![args.source_range],
858 )));
859 };
860 let face = start_sketch_on_face(solid, tag, exec_state, args).await?;
861
862 #[cfg(feature = "artifact-graph")]
863 {
864 let id = exec_state.next_uuid();
866 exec_state.add_artifact(Artifact::StartSketchOnFace(StartSketchOnFace {
867 id: ArtifactId::from(id),
868 face_id: face.artifact_id,
869 code_ref: CodeRef::placeholder(args.source_range),
870 }));
871 }
872
873 Ok(SketchSurface::Face(face))
874 }
875 }
876}
877
878async fn start_sketch_on_face(
879 solid: Box<Solid>,
880 tag: FaceTag,
881 exec_state: &mut ExecState,
882 args: &Args,
883) -> Result<Box<Face>, KclError> {
884 let extrude_plane_id = tag.get_face_id(&solid, exec_state, args, true).await?;
885
886 Ok(Box::new(Face {
887 id: extrude_plane_id,
888 artifact_id: extrude_plane_id.into(),
889 value: tag.to_string(),
890 x_axis: solid.sketch.on.x_axis(),
892 y_axis: solid.sketch.on.y_axis(),
893 units: solid.units,
894 solid,
895 meta: vec![args.source_range.into()],
896 }))
897}
898
899async fn make_sketch_plane_from_orientation(
900 data: PlaneData,
901 exec_state: &mut ExecState,
902 args: &Args,
903) -> Result<Box<Plane>, KclError> {
904 let plane = Plane::from_plane_data(data.clone(), exec_state)?;
905
906 let clobber = false;
908 let size = LengthUnit(60.0);
909 let hide = Some(true);
910 exec_state
911 .batch_modeling_cmd(
912 ModelingCmdMeta::from_args_id(args, plane.id),
913 ModelingCmd::from(mcmd::MakePlane {
914 clobber,
915 origin: plane.info.origin.into(),
916 size,
917 x_axis: plane.info.x_axis.into(),
918 y_axis: plane.info.y_axis.into(),
919 hide,
920 }),
921 )
922 .await?;
923
924 Ok(Box::new(plane))
925}
926
927pub async fn start_profile(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
929 let sketch_surface = args.get_unlabeled_kw_arg(
930 "startProfileOn",
931 &RuntimeType::Union(vec![RuntimeType::plane(), RuntimeType::face()]),
932 exec_state,
933 )?;
934 let start: [TyF64; 2] = args.get_kw_arg("at", &RuntimeType::point2d(), exec_state)?;
935 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
936
937 let sketch = inner_start_profile(sketch_surface, start, tag, exec_state, args).await?;
938 Ok(KclValue::Sketch {
939 value: Box::new(sketch),
940 })
941}
942
943pub(crate) async fn inner_start_profile(
944 sketch_surface: SketchSurface,
945 at: [TyF64; 2],
946 tag: Option<TagNode>,
947 exec_state: &mut ExecState,
948 args: Args,
949) -> Result<Sketch, KclError> {
950 match &sketch_surface {
951 SketchSurface::Face(face) => {
952 exec_state
955 .flush_batch_for_solids((&args).into(), &[(*face.solid).clone()])
956 .await?;
957 }
958 SketchSurface::Plane(plane) if !plane.is_standard() => {
959 exec_state
962 .batch_end_cmd(
963 (&args).into(),
964 ModelingCmd::from(mcmd::ObjectVisible {
965 object_id: plane.id,
966 hidden: true,
967 }),
968 )
969 .await?;
970 }
971 _ => {}
972 }
973
974 let enable_sketch_id = exec_state.next_uuid();
975 let path_id = exec_state.next_uuid();
976 let move_pen_id = exec_state.next_uuid();
977 let disable_sketch_id = exec_state.next_uuid();
978 exec_state
979 .batch_modeling_cmds(
980 (&args).into(),
981 &[
982 ModelingCmdReq {
985 cmd: ModelingCmd::from(mcmd::EnableSketchMode {
986 animated: false,
987 ortho: false,
988 entity_id: sketch_surface.id(),
989 adjust_camera: false,
990 planar_normal: if let SketchSurface::Plane(plane) = &sketch_surface {
991 let normal = plane.info.x_axis.axes_cross_product(&plane.info.y_axis);
993 Some(normal.into())
994 } else {
995 None
996 },
997 }),
998 cmd_id: enable_sketch_id.into(),
999 },
1000 ModelingCmdReq {
1001 cmd: ModelingCmd::from(mcmd::StartPath::default()),
1002 cmd_id: path_id.into(),
1003 },
1004 ModelingCmdReq {
1005 cmd: ModelingCmd::from(mcmd::MovePathPen {
1006 path: path_id.into(),
1007 to: KPoint2d::from(point_to_mm(at.clone())).with_z(0.0).map(LengthUnit),
1008 }),
1009 cmd_id: move_pen_id.into(),
1010 },
1011 ModelingCmdReq {
1012 cmd: ModelingCmd::SketchModeDisable(mcmd::SketchModeDisable::default()),
1013 cmd_id: disable_sketch_id.into(),
1014 },
1015 ],
1016 )
1017 .await?;
1018
1019 let units = exec_state.length_unit();
1021 let to = point_to_len_unit(at, units);
1022 let current_path = BasePath {
1023 from: to,
1024 to,
1025 tag: tag.clone(),
1026 units,
1027 geo_meta: GeoMeta {
1028 id: move_pen_id,
1029 metadata: args.source_range.into(),
1030 },
1031 };
1032
1033 let sketch = Sketch {
1034 id: path_id,
1035 original_id: path_id,
1036 artifact_id: path_id.into(),
1037 on: sketch_surface.clone(),
1038 paths: vec![],
1039 units,
1040 mirror: Default::default(),
1041 meta: vec![args.source_range.into()],
1042 tags: if let Some(tag) = &tag {
1043 let mut tag_identifier: TagIdentifier = tag.into();
1044 tag_identifier.info = vec![(
1045 exec_state.stack().current_epoch(),
1046 TagEngineInfo {
1047 id: current_path.geo_meta.id,
1048 sketch: path_id,
1049 path: Some(Path::Base {
1050 base: current_path.clone(),
1051 }),
1052 surface: None,
1053 },
1054 )];
1055 IndexMap::from([(tag.name.to_string(), tag_identifier)])
1056 } else {
1057 Default::default()
1058 },
1059 start: current_path,
1060 };
1061 Ok(sketch)
1062}
1063
1064pub async fn profile_start_x(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1066 let sketch: Sketch = args.get_unlabeled_kw_arg("profile", &RuntimeType::sketch(), exec_state)?;
1067 let ty = sketch.units.into();
1068 let x = inner_profile_start_x(sketch)?;
1069 Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
1070}
1071
1072pub(crate) fn inner_profile_start_x(profile: Sketch) -> Result<f64, KclError> {
1073 Ok(profile.start.to[0])
1074}
1075
1076pub async fn profile_start_y(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1078 let sketch: Sketch = args.get_unlabeled_kw_arg("profile", &RuntimeType::sketch(), exec_state)?;
1079 let ty = sketch.units.into();
1080 let x = inner_profile_start_y(sketch)?;
1081 Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
1082}
1083
1084pub(crate) fn inner_profile_start_y(profile: Sketch) -> Result<f64, KclError> {
1085 Ok(profile.start.to[1])
1086}
1087
1088pub async fn profile_start(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1090 let sketch: Sketch = args.get_unlabeled_kw_arg("profile", &RuntimeType::sketch(), exec_state)?;
1091 let ty = sketch.units.into();
1092 let point = inner_profile_start(sketch)?;
1093 Ok(KclValue::from_point2d(point, ty, args.into()))
1094}
1095
1096pub(crate) fn inner_profile_start(profile: Sketch) -> Result<[f64; 2], KclError> {
1097 Ok(profile.start.to)
1098}
1099
1100pub async fn close(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1102 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1103 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
1104 let new_sketch = inner_close(sketch, tag, exec_state, args).await?;
1105 Ok(KclValue::Sketch {
1106 value: Box::new(new_sketch),
1107 })
1108}
1109
1110pub(crate) async fn inner_close(
1111 sketch: Sketch,
1112 tag: Option<TagNode>,
1113 exec_state: &mut ExecState,
1114 args: Args,
1115) -> Result<Sketch, KclError> {
1116 let from = sketch.current_pen_position()?;
1117 let to = point_to_len_unit(sketch.start.get_from(), from.units);
1118
1119 let id = exec_state.next_uuid();
1120
1121 exec_state
1122 .batch_modeling_cmd(
1123 ModelingCmdMeta::from_args_id(&args, id),
1124 ModelingCmd::from(mcmd::ClosePath { path_id: sketch.id }),
1125 )
1126 .await?;
1127
1128 let current_path = Path::ToPoint {
1129 base: BasePath {
1130 from: from.ignore_units(),
1131 to,
1132 tag: tag.clone(),
1133 units: sketch.units,
1134 geo_meta: GeoMeta {
1135 id,
1136 metadata: args.source_range.into(),
1137 },
1138 },
1139 };
1140
1141 let mut new_sketch = sketch.clone();
1142 if let Some(tag) = &tag {
1143 new_sketch.add_tag(tag, ¤t_path, exec_state);
1144 }
1145
1146 new_sketch.paths.push(current_path);
1147
1148 Ok(new_sketch)
1149}
1150
1151pub async fn arc(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1153 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1154
1155 let angle_start: Option<TyF64> = args.get_kw_arg_opt("angleStart", &RuntimeType::degrees(), exec_state)?;
1156 let angle_end: Option<TyF64> = args.get_kw_arg_opt("angleEnd", &RuntimeType::degrees(), exec_state)?;
1157 let radius: Option<TyF64> = args.get_kw_arg_opt("radius", &RuntimeType::length(), exec_state)?;
1158 let diameter: Option<TyF64> = args.get_kw_arg_opt("diameter", &RuntimeType::length(), exec_state)?;
1159 let end_absolute: Option<[TyF64; 2]> = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
1160 let interior_absolute: Option<[TyF64; 2]> =
1161 args.get_kw_arg_opt("interiorAbsolute", &RuntimeType::point2d(), exec_state)?;
1162 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
1163 let new_sketch = inner_arc(
1164 sketch,
1165 angle_start,
1166 angle_end,
1167 radius,
1168 diameter,
1169 interior_absolute,
1170 end_absolute,
1171 tag,
1172 exec_state,
1173 args,
1174 )
1175 .await?;
1176 Ok(KclValue::Sketch {
1177 value: Box::new(new_sketch),
1178 })
1179}
1180
1181#[allow(clippy::too_many_arguments)]
1182pub(crate) async fn inner_arc(
1183 sketch: Sketch,
1184 angle_start: Option<TyF64>,
1185 angle_end: Option<TyF64>,
1186 radius: Option<TyF64>,
1187 diameter: Option<TyF64>,
1188 interior_absolute: Option<[TyF64; 2]>,
1189 end_absolute: Option<[TyF64; 2]>,
1190 tag: Option<TagNode>,
1191 exec_state: &mut ExecState,
1192 args: Args,
1193) -> Result<Sketch, KclError> {
1194 let from: Point2d = sketch.current_pen_position()?;
1195 let id = exec_state.next_uuid();
1196
1197 match (angle_start, angle_end, radius, diameter, interior_absolute, end_absolute) {
1198 (Some(angle_start), Some(angle_end), radius, diameter, None, None) => {
1199 let radius = get_radius(radius, diameter, args.source_range)?;
1200 relative_arc(&args, id, exec_state, sketch, from, angle_start, angle_end, radius, tag).await
1201 }
1202 (None, None, None, None, Some(interior_absolute), Some(end_absolute)) => {
1203 absolute_arc(&args, id, exec_state, sketch, from, interior_absolute, end_absolute, tag).await
1204 }
1205 _ => {
1206 Err(KclError::new_type(KclErrorDetails::new(
1207 "Invalid combination of arguments. Either provide (angleStart, angleEnd, radius) or (endAbsolute, interiorAbsolute)".to_owned(),
1208 vec![args.source_range],
1209 )))
1210 }
1211 }
1212}
1213
1214#[allow(clippy::too_many_arguments)]
1215pub async fn absolute_arc(
1216 args: &Args,
1217 id: uuid::Uuid,
1218 exec_state: &mut ExecState,
1219 sketch: Sketch,
1220 from: Point2d,
1221 interior_absolute: [TyF64; 2],
1222 end_absolute: [TyF64; 2],
1223 tag: Option<TagNode>,
1224) -> Result<Sketch, KclError> {
1225 exec_state
1227 .batch_modeling_cmd(
1228 ModelingCmdMeta::from_args_id(args, id),
1229 ModelingCmd::from(mcmd::ExtendPath {
1230 path: sketch.id.into(),
1231 segment: PathSegment::ArcTo {
1232 end: kcmc::shared::Point3d {
1233 x: LengthUnit(end_absolute[0].to_mm()),
1234 y: LengthUnit(end_absolute[1].to_mm()),
1235 z: LengthUnit(0.0),
1236 },
1237 interior: kcmc::shared::Point3d {
1238 x: LengthUnit(interior_absolute[0].to_mm()),
1239 y: LengthUnit(interior_absolute[1].to_mm()),
1240 z: LengthUnit(0.0),
1241 },
1242 relative: false,
1243 },
1244 }),
1245 )
1246 .await?;
1247
1248 let start = [from.x, from.y];
1249 let end = point_to_len_unit(end_absolute, from.units);
1250
1251 let current_path = Path::ArcThreePoint {
1252 base: BasePath {
1253 from: from.ignore_units(),
1254 to: end,
1255 tag: tag.clone(),
1256 units: sketch.units,
1257 geo_meta: GeoMeta {
1258 id,
1259 metadata: args.source_range.into(),
1260 },
1261 },
1262 p1: start,
1263 p2: point_to_len_unit(interior_absolute, from.units),
1264 p3: end,
1265 };
1266
1267 let mut new_sketch = sketch.clone();
1268 if let Some(tag) = &tag {
1269 new_sketch.add_tag(tag, ¤t_path, exec_state);
1270 }
1271
1272 new_sketch.paths.push(current_path);
1273
1274 Ok(new_sketch)
1275}
1276
1277#[allow(clippy::too_many_arguments)]
1278pub async fn relative_arc(
1279 args: &Args,
1280 id: uuid::Uuid,
1281 exec_state: &mut ExecState,
1282 sketch: Sketch,
1283 from: Point2d,
1284 angle_start: TyF64,
1285 angle_end: TyF64,
1286 radius: TyF64,
1287 tag: Option<TagNode>,
1288) -> Result<Sketch, KclError> {
1289 let a_start = Angle::from_degrees(angle_start.to_degrees());
1290 let a_end = Angle::from_degrees(angle_end.to_degrees());
1291 let radius = radius.to_length_units(from.units);
1292 let (center, end) = arc_center_and_end(from.ignore_units(), a_start, a_end, radius);
1293 if a_start == a_end {
1294 return Err(KclError::new_type(KclErrorDetails::new(
1295 "Arc start and end angles must be different".to_string(),
1296 vec![args.source_range],
1297 )));
1298 }
1299 let ccw = a_start < a_end;
1300
1301 exec_state
1302 .batch_modeling_cmd(
1303 ModelingCmdMeta::from_args_id(args, id),
1304 ModelingCmd::from(mcmd::ExtendPath {
1305 path: sketch.id.into(),
1306 segment: PathSegment::Arc {
1307 start: a_start,
1308 end: a_end,
1309 center: KPoint2d::from(untyped_point_to_mm(center, from.units)).map(LengthUnit),
1310 radius: LengthUnit(from.units.adjust_to(radius, UnitLen::Mm).0),
1311 relative: false,
1312 },
1313 }),
1314 )
1315 .await?;
1316
1317 let current_path = Path::Arc {
1318 base: BasePath {
1319 from: from.ignore_units(),
1320 to: end,
1321 tag: tag.clone(),
1322 units: from.units,
1323 geo_meta: GeoMeta {
1324 id,
1325 metadata: args.source_range.into(),
1326 },
1327 },
1328 center,
1329 radius,
1330 ccw,
1331 };
1332
1333 let mut new_sketch = sketch.clone();
1334 if let Some(tag) = &tag {
1335 new_sketch.add_tag(tag, ¤t_path, exec_state);
1336 }
1337
1338 new_sketch.paths.push(current_path);
1339
1340 Ok(new_sketch)
1341}
1342
1343pub async fn tangential_arc(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1345 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1346 let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
1347 let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
1348 let radius = args.get_kw_arg_opt("radius", &RuntimeType::length(), exec_state)?;
1349 let diameter = args.get_kw_arg_opt("diameter", &RuntimeType::length(), exec_state)?;
1350 let angle = args.get_kw_arg_opt("angle", &RuntimeType::angle(), exec_state)?;
1351 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
1352
1353 let new_sketch = inner_tangential_arc(
1354 sketch,
1355 end_absolute,
1356 end,
1357 radius,
1358 diameter,
1359 angle,
1360 tag,
1361 exec_state,
1362 args,
1363 )
1364 .await?;
1365 Ok(KclValue::Sketch {
1366 value: Box::new(new_sketch),
1367 })
1368}
1369
1370#[allow(clippy::too_many_arguments)]
1371async fn inner_tangential_arc(
1372 sketch: Sketch,
1373 end_absolute: Option<[TyF64; 2]>,
1374 end: Option<[TyF64; 2]>,
1375 radius: Option<TyF64>,
1376 diameter: Option<TyF64>,
1377 angle: Option<TyF64>,
1378 tag: Option<TagNode>,
1379 exec_state: &mut ExecState,
1380 args: Args,
1381) -> Result<Sketch, KclError> {
1382 match (end_absolute, end, radius, diameter, angle) {
1383 (Some(point), None, None, None, None) => {
1384 inner_tangential_arc_to_point(sketch, point, true, tag, exec_state, args).await
1385 }
1386 (None, Some(point), None, None, None) => {
1387 inner_tangential_arc_to_point(sketch, point, false, tag, exec_state, args).await
1388 }
1389 (None, None, radius, diameter, Some(angle)) => {
1390 let radius = get_radius(radius, diameter, args.source_range)?;
1391 let data = TangentialArcData::RadiusAndOffset { radius, offset: angle };
1392 inner_tangential_arc_radius_angle(data, sketch, tag, exec_state, args).await
1393 }
1394 (Some(_), Some(_), None, None, None) => Err(KclError::new_semantic(KclErrorDetails::new(
1395 "You cannot give both `end` and `endAbsolute` params, you have to choose one or the other".to_owned(),
1396 vec![args.source_range],
1397 ))),
1398 (_, _, _, _, _) => Err(KclError::new_semantic(KclErrorDetails::new(
1399 "You must supply `end`, `endAbsolute`, or both `angle` and `radius`/`diameter` arguments".to_owned(),
1400 vec![args.source_range],
1401 ))),
1402 }
1403}
1404
1405#[derive(Debug, Clone, Serialize, PartialEq, JsonSchema, ts_rs::TS)]
1407#[ts(export)]
1408#[serde(rename_all = "camelCase", untagged)]
1409pub enum TangentialArcData {
1410 RadiusAndOffset {
1411 radius: TyF64,
1414 offset: TyF64,
1416 },
1417}
1418
1419async fn inner_tangential_arc_radius_angle(
1426 data: TangentialArcData,
1427 sketch: Sketch,
1428 tag: Option<TagNode>,
1429 exec_state: &mut ExecState,
1430 args: Args,
1431) -> Result<Sketch, KclError> {
1432 let from: Point2d = sketch.current_pen_position()?;
1433 let tangent_info = sketch.get_tangential_info_from_paths(); let tan_previous_point = tangent_info.tan_previous_point(from.ignore_units());
1436
1437 let id = exec_state.next_uuid();
1438
1439 let (center, to, ccw) = match data {
1440 TangentialArcData::RadiusAndOffset { radius, offset } => {
1441 let offset = Angle::from_degrees(offset.to_degrees());
1443
1444 let previous_end_tangent = Angle::from_radians(libm::atan2(
1447 from.y - tan_previous_point[1],
1448 from.x - tan_previous_point[0],
1449 ));
1450 let ccw = offset.to_degrees() > 0.0;
1453 let tangent_to_arc_start_angle = if ccw {
1454 Angle::from_degrees(-90.0)
1456 } else {
1457 Angle::from_degrees(90.0)
1459 };
1460 let start_angle = previous_end_tangent + tangent_to_arc_start_angle;
1463 let end_angle = start_angle + offset;
1464 let (center, to) = arc_center_and_end(
1465 from.ignore_units(),
1466 start_angle,
1467 end_angle,
1468 radius.to_length_units(from.units),
1469 );
1470
1471 exec_state
1472 .batch_modeling_cmd(
1473 ModelingCmdMeta::from_args_id(&args, id),
1474 ModelingCmd::from(mcmd::ExtendPath {
1475 path: sketch.id.into(),
1476 segment: PathSegment::TangentialArc {
1477 radius: LengthUnit(radius.to_mm()),
1478 offset,
1479 },
1480 }),
1481 )
1482 .await?;
1483 (center, to, ccw)
1484 }
1485 };
1486
1487 let current_path = Path::TangentialArc {
1488 ccw,
1489 center,
1490 base: BasePath {
1491 from: from.ignore_units(),
1492 to,
1493 tag: tag.clone(),
1494 units: sketch.units,
1495 geo_meta: GeoMeta {
1496 id,
1497 metadata: args.source_range.into(),
1498 },
1499 },
1500 };
1501
1502 let mut new_sketch = sketch.clone();
1503 if let Some(tag) = &tag {
1504 new_sketch.add_tag(tag, ¤t_path, exec_state);
1505 }
1506
1507 new_sketch.paths.push(current_path);
1508
1509 Ok(new_sketch)
1510}
1511
1512fn tan_arc_to(sketch: &Sketch, to: [f64; 2]) -> ModelingCmd {
1514 ModelingCmd::from(mcmd::ExtendPath {
1515 path: sketch.id.into(),
1516 segment: PathSegment::TangentialArcTo {
1517 angle_snap_increment: None,
1518 to: KPoint2d::from(untyped_point_to_mm(to, sketch.units))
1519 .with_z(0.0)
1520 .map(LengthUnit),
1521 },
1522 })
1523}
1524
1525async fn inner_tangential_arc_to_point(
1526 sketch: Sketch,
1527 point: [TyF64; 2],
1528 is_absolute: bool,
1529 tag: Option<TagNode>,
1530 exec_state: &mut ExecState,
1531 args: Args,
1532) -> Result<Sketch, KclError> {
1533 let from: Point2d = sketch.current_pen_position()?;
1534 let tangent_info = sketch.get_tangential_info_from_paths();
1535 let tan_previous_point = tangent_info.tan_previous_point(from.ignore_units());
1536
1537 let point = point_to_len_unit(point, from.units);
1538
1539 let to = if is_absolute {
1540 point
1541 } else {
1542 [from.x + point[0], from.y + point[1]]
1543 };
1544 let [to_x, to_y] = to;
1545 let result = get_tangential_arc_to_info(TangentialArcInfoInput {
1546 arc_start_point: [from.x, from.y],
1547 arc_end_point: [to_x, to_y],
1548 tan_previous_point,
1549 obtuse: true,
1550 });
1551
1552 if result.center[0].is_infinite() {
1553 return Err(KclError::new_semantic(KclErrorDetails::new(
1554 "could not sketch tangential arc, because its center would be infinitely far away in the X direction"
1555 .to_owned(),
1556 vec![args.source_range],
1557 )));
1558 } else if result.center[1].is_infinite() {
1559 return Err(KclError::new_semantic(KclErrorDetails::new(
1560 "could not sketch tangential arc, because its center would be infinitely far away in the Y direction"
1561 .to_owned(),
1562 vec![args.source_range],
1563 )));
1564 }
1565
1566 let delta = if is_absolute {
1567 [to_x - from.x, to_y - from.y]
1568 } else {
1569 point
1570 };
1571 let id = exec_state.next_uuid();
1572 exec_state
1573 .batch_modeling_cmd(ModelingCmdMeta::from_args_id(&args, id), tan_arc_to(&sketch, delta))
1574 .await?;
1575
1576 let current_path = Path::TangentialArcTo {
1577 base: BasePath {
1578 from: from.ignore_units(),
1579 to,
1580 tag: tag.clone(),
1581 units: sketch.units,
1582 geo_meta: GeoMeta {
1583 id,
1584 metadata: args.source_range.into(),
1585 },
1586 },
1587 center: result.center,
1588 ccw: result.ccw > 0,
1589 };
1590
1591 let mut new_sketch = sketch.clone();
1592 if let Some(tag) = &tag {
1593 new_sketch.add_tag(tag, ¤t_path, exec_state);
1594 }
1595
1596 new_sketch.paths.push(current_path);
1597
1598 Ok(new_sketch)
1599}
1600
1601pub async fn bezier_curve(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1603 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1604 let control1 = args.get_kw_arg_opt("control1", &RuntimeType::point2d(), exec_state)?;
1605 let control2 = args.get_kw_arg_opt("control2", &RuntimeType::point2d(), exec_state)?;
1606 let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
1607 let control1_absolute = args.get_kw_arg_opt("control1Absolute", &RuntimeType::point2d(), exec_state)?;
1608 let control2_absolute = args.get_kw_arg_opt("control2Absolute", &RuntimeType::point2d(), exec_state)?;
1609 let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
1610 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
1611
1612 let new_sketch = inner_bezier_curve(
1613 sketch,
1614 control1,
1615 control2,
1616 end,
1617 control1_absolute,
1618 control2_absolute,
1619 end_absolute,
1620 tag,
1621 exec_state,
1622 args,
1623 )
1624 .await?;
1625 Ok(KclValue::Sketch {
1626 value: Box::new(new_sketch),
1627 })
1628}
1629
1630#[allow(clippy::too_many_arguments)]
1631async fn inner_bezier_curve(
1632 sketch: Sketch,
1633 control1: Option<[TyF64; 2]>,
1634 control2: Option<[TyF64; 2]>,
1635 end: Option<[TyF64; 2]>,
1636 control1_absolute: Option<[TyF64; 2]>,
1637 control2_absolute: Option<[TyF64; 2]>,
1638 end_absolute: Option<[TyF64; 2]>,
1639 tag: Option<TagNode>,
1640 exec_state: &mut ExecState,
1641 args: Args,
1642) -> Result<Sketch, KclError> {
1643 let from = sketch.current_pen_position()?;
1644 let id = exec_state.next_uuid();
1645
1646 let to = match (
1647 control1,
1648 control2,
1649 end,
1650 control1_absolute,
1651 control2_absolute,
1652 end_absolute,
1653 ) {
1654 (Some(control1), Some(control2), Some(end), None, None, None) => {
1656 let delta = end.clone();
1657 let to = [
1658 from.x + end[0].to_length_units(from.units),
1659 from.y + end[1].to_length_units(from.units),
1660 ];
1661
1662 exec_state
1663 .batch_modeling_cmd(
1664 ModelingCmdMeta::from_args_id(&args, id),
1665 ModelingCmd::from(mcmd::ExtendPath {
1666 path: sketch.id.into(),
1667 segment: PathSegment::Bezier {
1668 control1: KPoint2d::from(point_to_mm(control1)).with_z(0.0).map(LengthUnit),
1669 control2: KPoint2d::from(point_to_mm(control2)).with_z(0.0).map(LengthUnit),
1670 end: KPoint2d::from(point_to_mm(delta)).with_z(0.0).map(LengthUnit),
1671 relative: true,
1672 },
1673 }),
1674 )
1675 .await?;
1676 to
1677 }
1678 (None, None, None, Some(control1), Some(control2), Some(end)) => {
1680 let to = [end[0].to_length_units(from.units), end[1].to_length_units(from.units)];
1681 exec_state
1682 .batch_modeling_cmd(
1683 ModelingCmdMeta::from_args_id(&args, id),
1684 ModelingCmd::from(mcmd::ExtendPath {
1685 path: sketch.id.into(),
1686 segment: PathSegment::Bezier {
1687 control1: KPoint2d::from(point_to_mm(control1)).with_z(0.0).map(LengthUnit),
1688 control2: KPoint2d::from(point_to_mm(control2)).with_z(0.0).map(LengthUnit),
1689 end: KPoint2d::from(point_to_mm(end)).with_z(0.0).map(LengthUnit),
1690 relative: false,
1691 },
1692 }),
1693 )
1694 .await?;
1695 to
1696 }
1697 _ => {
1698 return Err(KclError::new_semantic(KclErrorDetails::new(
1699 "You must either give `control1`, `control2` and `end`, or `control1Absolute`, `control2Absolute` and `endAbsolute`.".to_owned(),
1700 vec![args.source_range],
1701 )));
1702 }
1703 };
1704
1705 let current_path = Path::ToPoint {
1706 base: BasePath {
1707 from: from.ignore_units(),
1708 to,
1709 tag: tag.clone(),
1710 units: sketch.units,
1711 geo_meta: GeoMeta {
1712 id,
1713 metadata: args.source_range.into(),
1714 },
1715 },
1716 };
1717
1718 let mut new_sketch = sketch.clone();
1719 if let Some(tag) = &tag {
1720 new_sketch.add_tag(tag, ¤t_path, exec_state);
1721 }
1722
1723 new_sketch.paths.push(current_path);
1724
1725 Ok(new_sketch)
1726}
1727
1728pub async fn subtract_2d(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1730 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1731
1732 let tool: Vec<Sketch> = args.get_kw_arg(
1733 "tool",
1734 &RuntimeType::Array(
1735 Box::new(RuntimeType::Primitive(PrimitiveType::Sketch)),
1736 ArrayLen::Minimum(1),
1737 ),
1738 exec_state,
1739 )?;
1740
1741 let new_sketch = inner_subtract_2d(sketch, tool, exec_state, args).await?;
1742 Ok(KclValue::Sketch {
1743 value: Box::new(new_sketch),
1744 })
1745}
1746
1747async fn inner_subtract_2d(
1748 sketch: Sketch,
1749 tool: Vec<Sketch>,
1750 exec_state: &mut ExecState,
1751 args: Args,
1752) -> Result<Sketch, KclError> {
1753 for hole_sketch in tool {
1754 exec_state
1755 .batch_modeling_cmd(
1756 ModelingCmdMeta::from(&args),
1757 ModelingCmd::from(mcmd::Solid2dAddHole {
1758 object_id: sketch.id,
1759 hole_id: hole_sketch.id,
1760 }),
1761 )
1762 .await?;
1763
1764 exec_state
1767 .batch_modeling_cmd(
1768 ModelingCmdMeta::from(&args),
1769 ModelingCmd::from(mcmd::ObjectVisible {
1770 object_id: hole_sketch.id,
1771 hidden: true,
1772 }),
1773 )
1774 .await?;
1775 }
1776
1777 Ok(sketch)
1778}
1779
1780#[cfg(test)]
1781mod tests {
1782
1783 use pretty_assertions::assert_eq;
1784
1785 use crate::{
1786 execution::TagIdentifier,
1787 std::{sketch::PlaneData, utils::calculate_circle_center},
1788 };
1789
1790 #[test]
1791 fn test_deserialize_plane_data() {
1792 let data = PlaneData::XY;
1793 let mut str_json = serde_json::to_string(&data).unwrap();
1794 assert_eq!(str_json, "\"XY\"");
1795
1796 str_json = "\"YZ\"".to_string();
1797 let data: PlaneData = serde_json::from_str(&str_json).unwrap();
1798 assert_eq!(data, PlaneData::YZ);
1799
1800 str_json = "\"-YZ\"".to_string();
1801 let data: PlaneData = serde_json::from_str(&str_json).unwrap();
1802 assert_eq!(data, PlaneData::NegYZ);
1803
1804 str_json = "\"-xz\"".to_string();
1805 let data: PlaneData = serde_json::from_str(&str_json).unwrap();
1806 assert_eq!(data, PlaneData::NegXZ);
1807 }
1808
1809 #[test]
1810 fn test_deserialize_sketch_on_face_tag() {
1811 let data = "start";
1812 let mut str_json = serde_json::to_string(&data).unwrap();
1813 assert_eq!(str_json, "\"start\"");
1814
1815 str_json = "\"end\"".to_string();
1816 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
1817 assert_eq!(
1818 data,
1819 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
1820 );
1821
1822 str_json = serde_json::to_string(&TagIdentifier {
1823 value: "thing".to_string(),
1824 info: Vec::new(),
1825 meta: Default::default(),
1826 })
1827 .unwrap();
1828 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
1829 assert_eq!(
1830 data,
1831 crate::std::sketch::FaceTag::Tag(Box::new(TagIdentifier {
1832 value: "thing".to_string(),
1833 info: Vec::new(),
1834 meta: Default::default()
1835 }))
1836 );
1837
1838 str_json = "\"END\"".to_string();
1839 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
1840 assert_eq!(
1841 data,
1842 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
1843 );
1844
1845 str_json = "\"start\"".to_string();
1846 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
1847 assert_eq!(
1848 data,
1849 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
1850 );
1851
1852 str_json = "\"START\"".to_string();
1853 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
1854 assert_eq!(
1855 data,
1856 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
1857 );
1858 }
1859
1860 #[test]
1861 fn test_circle_center() {
1862 let actual = calculate_circle_center([0.0, 0.0], [5.0, 5.0], [10.0, 0.0]);
1863 assert_eq!(actual[0], 5.0);
1864 assert_eq!(actual[1], 0.0);
1865 }
1866}