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