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(exec_state, &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(exec_state, &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(exec_state, &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(exec_state, 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(ModelingCmdMeta::from_args(exec_state, &args), &[(*face.solid).clone()])
1086 .await?;
1087 }
1088 SketchSurface::Plane(plane) if !plane.is_standard() => {
1089 exec_state
1092 .batch_end_cmd(
1093 ModelingCmdMeta::from_args(exec_state, &args),
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 ModelingCmdMeta::from_args(exec_state, &args),
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(exec_state, &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(exec_state, 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(exec_state, 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(exec_state, &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(
1743 ModelingCmdMeta::from_args_id(exec_state, &args, id),
1744 tan_arc_to(&sketch, delta),
1745 )
1746 .await?;
1747
1748 let current_path = Path::TangentialArcTo {
1749 base: BasePath {
1750 from: from.ignore_units(),
1751 to,
1752 tag: tag.clone(),
1753 units: sketch.units,
1754 geo_meta: GeoMeta {
1755 id,
1756 metadata: args.source_range.into(),
1757 },
1758 },
1759 center: result.center,
1760 ccw: result.ccw > 0,
1761 };
1762
1763 let mut new_sketch = sketch;
1764 if let Some(tag) = &tag {
1765 new_sketch.add_tag(tag, ¤t_path, exec_state, None);
1766 }
1767
1768 new_sketch.paths.push(current_path);
1769
1770 Ok(new_sketch)
1771}
1772
1773pub async fn bezier_curve(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1775 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1776 let control1 = args.get_kw_arg_opt("control1", &RuntimeType::point2d(), exec_state)?;
1777 let control2 = args.get_kw_arg_opt("control2", &RuntimeType::point2d(), exec_state)?;
1778 let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
1779 let control1_absolute = args.get_kw_arg_opt("control1Absolute", &RuntimeType::point2d(), exec_state)?;
1780 let control2_absolute = args.get_kw_arg_opt("control2Absolute", &RuntimeType::point2d(), exec_state)?;
1781 let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
1782 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
1783
1784 let new_sketch = inner_bezier_curve(
1785 sketch,
1786 control1,
1787 control2,
1788 end,
1789 control1_absolute,
1790 control2_absolute,
1791 end_absolute,
1792 tag,
1793 exec_state,
1794 args,
1795 )
1796 .await?;
1797 Ok(KclValue::Sketch {
1798 value: Box::new(new_sketch),
1799 })
1800}
1801
1802#[allow(clippy::too_many_arguments)]
1803async fn inner_bezier_curve(
1804 sketch: Sketch,
1805 control1: Option<[TyF64; 2]>,
1806 control2: Option<[TyF64; 2]>,
1807 end: Option<[TyF64; 2]>,
1808 control1_absolute: Option<[TyF64; 2]>,
1809 control2_absolute: Option<[TyF64; 2]>,
1810 end_absolute: Option<[TyF64; 2]>,
1811 tag: Option<TagNode>,
1812 exec_state: &mut ExecState,
1813 args: Args,
1814) -> Result<Sketch, KclError> {
1815 let from = sketch.current_pen_position()?;
1816 let id = exec_state.next_uuid();
1817
1818 let to = match (
1819 control1,
1820 control2,
1821 end,
1822 control1_absolute,
1823 control2_absolute,
1824 end_absolute,
1825 ) {
1826 (Some(control1), Some(control2), Some(end), None, None, None) => {
1828 let delta = end.clone();
1829 let to = [
1830 from.x + end[0].to_length_units(from.units),
1831 from.y + end[1].to_length_units(from.units),
1832 ];
1833
1834 exec_state
1835 .batch_modeling_cmd(
1836 ModelingCmdMeta::from_args_id(exec_state, &args, id),
1837 ModelingCmd::from(mcmd::ExtendPath {
1838 label: Default::default(),
1839 path: sketch.id.into(),
1840 segment: PathSegment::Bezier {
1841 control1: KPoint2d::from(point_to_mm(control1)).with_z(0.0).map(LengthUnit),
1842 control2: KPoint2d::from(point_to_mm(control2)).with_z(0.0).map(LengthUnit),
1843 end: KPoint2d::from(point_to_mm(delta)).with_z(0.0).map(LengthUnit),
1844 relative: true,
1845 },
1846 }),
1847 )
1848 .await?;
1849 to
1850 }
1851 (None, None, None, Some(control1), Some(control2), Some(end)) => {
1853 let to = [end[0].to_length_units(from.units), end[1].to_length_units(from.units)];
1854 exec_state
1855 .batch_modeling_cmd(
1856 ModelingCmdMeta::from_args_id(exec_state, &args, id),
1857 ModelingCmd::from(mcmd::ExtendPath {
1858 label: Default::default(),
1859 path: sketch.id.into(),
1860 segment: PathSegment::Bezier {
1861 control1: KPoint2d::from(point_to_mm(control1)).with_z(0.0).map(LengthUnit),
1862 control2: KPoint2d::from(point_to_mm(control2)).with_z(0.0).map(LengthUnit),
1863 end: KPoint2d::from(point_to_mm(end)).with_z(0.0).map(LengthUnit),
1864 relative: false,
1865 },
1866 }),
1867 )
1868 .await?;
1869 to
1870 }
1871 _ => {
1872 return Err(KclError::new_semantic(KclErrorDetails::new(
1873 "You must either give `control1`, `control2` and `end`, or `control1Absolute`, `control2Absolute` and `endAbsolute`.".to_owned(),
1874 vec![args.source_range],
1875 )));
1876 }
1877 };
1878
1879 let current_path = Path::ToPoint {
1880 base: BasePath {
1881 from: from.ignore_units(),
1882 to,
1883 tag: tag.clone(),
1884 units: sketch.units,
1885 geo_meta: GeoMeta {
1886 id,
1887 metadata: args.source_range.into(),
1888 },
1889 },
1890 };
1891
1892 let mut new_sketch = sketch;
1893 if let Some(tag) = &tag {
1894 new_sketch.add_tag(tag, ¤t_path, exec_state, None);
1895 }
1896
1897 new_sketch.paths.push(current_path);
1898
1899 Ok(new_sketch)
1900}
1901
1902pub async fn subtract_2d(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1904 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
1905
1906 let tool: Vec<Sketch> = args.get_kw_arg(
1907 "tool",
1908 &RuntimeType::Array(
1909 Box::new(RuntimeType::Primitive(PrimitiveType::Sketch)),
1910 ArrayLen::Minimum(1),
1911 ),
1912 exec_state,
1913 )?;
1914
1915 let new_sketch = inner_subtract_2d(sketch, tool, exec_state, args).await?;
1916 Ok(KclValue::Sketch {
1917 value: Box::new(new_sketch),
1918 })
1919}
1920
1921async fn inner_subtract_2d(
1922 mut sketch: Sketch,
1923 tool: Vec<Sketch>,
1924 exec_state: &mut ExecState,
1925 args: Args,
1926) -> Result<Sketch, KclError> {
1927 for hole_sketch in tool {
1928 exec_state
1929 .batch_modeling_cmd(
1930 ModelingCmdMeta::from_args(exec_state, &args),
1931 ModelingCmd::from(mcmd::Solid2dAddHole {
1932 object_id: sketch.id,
1933 hole_id: hole_sketch.id,
1934 }),
1935 )
1936 .await?;
1937
1938 exec_state
1941 .batch_modeling_cmd(
1942 ModelingCmdMeta::from_args(exec_state, &args),
1943 ModelingCmd::from(mcmd::ObjectVisible {
1944 object_id: hole_sketch.id,
1945 hidden: true,
1946 }),
1947 )
1948 .await?;
1949
1950 sketch.inner_paths.extend_from_slice(&hole_sketch.paths);
1955 }
1956
1957 Ok(sketch)
1960}
1961
1962pub async fn elliptic_point(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
1964 let x = args.get_kw_arg_opt("x", &RuntimeType::length(), exec_state)?;
1965 let y = args.get_kw_arg_opt("y", &RuntimeType::length(), exec_state)?;
1966 let major_radius = args.get_kw_arg("majorRadius", &RuntimeType::num_any(), exec_state)?;
1967 let minor_radius = args.get_kw_arg("minorRadius", &RuntimeType::num_any(), exec_state)?;
1968
1969 let elliptic_point = inner_elliptic_point(x, y, major_radius, minor_radius, &args).await?;
1970
1971 args.make_kcl_val_from_point(elliptic_point, exec_state.length_unit().into())
1972}
1973
1974async fn inner_elliptic_point(
1975 x: Option<TyF64>,
1976 y: Option<TyF64>,
1977 major_radius: TyF64,
1978 minor_radius: TyF64,
1979 args: &Args,
1980) -> Result<[f64; 2], KclError> {
1981 let major_radius = major_radius.n;
1982 let minor_radius = minor_radius.n;
1983 if let Some(x) = x {
1984 if x.n.abs() > major_radius {
1985 Err(KclError::Type {
1986 details: KclErrorDetails::new(
1987 format!(
1988 "Invalid input. The x value, {}, cannot be larger than the major radius {}.",
1989 x.n, major_radius
1990 ),
1991 vec![args.source_range],
1992 ),
1993 })
1994 } else {
1995 Ok((
1996 x.n,
1997 minor_radius * (1.0 - x.n.powf(2.0) / major_radius.powf(2.0)).sqrt(),
1998 )
1999 .into())
2000 }
2001 } else if let Some(y) = y {
2002 if y.n > minor_radius {
2003 Err(KclError::Type {
2004 details: KclErrorDetails::new(
2005 format!(
2006 "Invalid input. The y value, {}, cannot be larger than the minor radius {}.",
2007 y.n, minor_radius
2008 ),
2009 vec![args.source_range],
2010 ),
2011 })
2012 } else {
2013 Ok((
2014 major_radius * (1.0 - y.n.powf(2.0) / minor_radius.powf(2.0)).sqrt(),
2015 y.n,
2016 )
2017 .into())
2018 }
2019 } else {
2020 Err(KclError::Type {
2021 details: KclErrorDetails::new(
2022 "Invalid input. Must have either x or y, you cannot have both or neither.".to_owned(),
2023 vec![args.source_range],
2024 ),
2025 })
2026 }
2027}
2028
2029pub async fn elliptic(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2031 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
2032
2033 let center = args.get_kw_arg("center", &RuntimeType::point2d(), exec_state)?;
2034 let angle_start = args.get_kw_arg("angleStart", &RuntimeType::degrees(), exec_state)?;
2035 let angle_end = args.get_kw_arg("angleEnd", &RuntimeType::degrees(), exec_state)?;
2036 let major_radius = args.get_kw_arg_opt("majorRadius", &RuntimeType::length(), exec_state)?;
2037 let major_axis = args.get_kw_arg_opt("majorAxis", &RuntimeType::point2d(), exec_state)?;
2038 let minor_radius = args.get_kw_arg("minorRadius", &RuntimeType::length(), exec_state)?;
2039 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
2040
2041 let new_sketch = inner_elliptic(
2042 sketch,
2043 center,
2044 angle_start,
2045 angle_end,
2046 major_radius,
2047 major_axis,
2048 minor_radius,
2049 tag,
2050 exec_state,
2051 args,
2052 )
2053 .await?;
2054 Ok(KclValue::Sketch {
2055 value: Box::new(new_sketch),
2056 })
2057}
2058
2059#[allow(clippy::too_many_arguments)]
2060pub(crate) async fn inner_elliptic(
2061 sketch: Sketch,
2062 center: [TyF64; 2],
2063 angle_start: TyF64,
2064 angle_end: TyF64,
2065 major_radius: Option<TyF64>,
2066 major_axis: Option<[TyF64; 2]>,
2067 minor_radius: TyF64,
2068 tag: Option<TagNode>,
2069 exec_state: &mut ExecState,
2070 args: Args,
2071) -> Result<Sketch, KclError> {
2072 let from: Point2d = sketch.current_pen_position()?;
2073 let id = exec_state.next_uuid();
2074
2075 let (center_u, _) = untype_point(center);
2076
2077 let major_axis = match (major_axis, major_radius) {
2078 (Some(_), Some(_)) | (None, None) => {
2079 return Err(KclError::new_type(KclErrorDetails::new(
2080 "Provide either `majorAxis` or `majorRadius`.".to_string(),
2081 vec![args.source_range],
2082 )));
2083 }
2084 (Some(major_axis), None) => major_axis,
2085 (None, Some(major_radius)) => [
2086 major_radius.clone(),
2087 TyF64 {
2088 n: 0.0,
2089 ty: major_radius.ty,
2090 },
2091 ],
2092 };
2093 let start_angle = Angle::from_degrees(angle_start.to_degrees(exec_state, args.source_range));
2094 let end_angle = Angle::from_degrees(angle_end.to_degrees(exec_state, args.source_range));
2095 let major_axis_magnitude = (major_axis[0].to_length_units(from.units) * major_axis[0].to_length_units(from.units)
2096 + major_axis[1].to_length_units(from.units) * major_axis[1].to_length_units(from.units))
2097 .sqrt();
2098 let to = [
2099 major_axis_magnitude * libm::cos(end_angle.to_radians()),
2100 minor_radius.to_length_units(from.units) * libm::sin(end_angle.to_radians()),
2101 ];
2102 let major_axis_angle = libm::atan2(major_axis[1].n, major_axis[0].n);
2103
2104 let point = [
2105 center_u[0] + to[0] * libm::cos(major_axis_angle) - to[1] * libm::sin(major_axis_angle),
2106 center_u[1] + to[0] * libm::sin(major_axis_angle) + to[1] * libm::cos(major_axis_angle),
2107 ];
2108
2109 let axis = major_axis.map(|x| x.to_mm());
2110 exec_state
2111 .batch_modeling_cmd(
2112 ModelingCmdMeta::from_args_id(exec_state, &args, id),
2113 ModelingCmd::from(mcmd::ExtendPath {
2114 label: Default::default(),
2115 path: sketch.id.into(),
2116 segment: PathSegment::Ellipse {
2117 center: KPoint2d::from(untyped_point_to_mm(center_u, from.units)).map(LengthUnit),
2118 major_axis: axis.map(LengthUnit).into(),
2119 minor_radius: LengthUnit(minor_radius.to_mm()),
2120 start_angle,
2121 end_angle,
2122 },
2123 }),
2124 )
2125 .await?;
2126
2127 let current_path = Path::Ellipse {
2128 ccw: start_angle < end_angle,
2129 center: center_u,
2130 major_axis: axis,
2131 minor_radius: minor_radius.to_mm(),
2132 base: BasePath {
2133 from: from.ignore_units(),
2134 to: point,
2135 tag: tag.clone(),
2136 units: sketch.units,
2137 geo_meta: GeoMeta {
2138 id,
2139 metadata: args.source_range.into(),
2140 },
2141 },
2142 };
2143 let mut new_sketch = sketch;
2144 if let Some(tag) = &tag {
2145 new_sketch.add_tag(tag, ¤t_path, exec_state, None);
2146 }
2147
2148 new_sketch.paths.push(current_path);
2149
2150 Ok(new_sketch)
2151}
2152
2153pub async fn hyperbolic_point(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2155 let x = args.get_kw_arg_opt("x", &RuntimeType::length(), exec_state)?;
2156 let y = args.get_kw_arg_opt("y", &RuntimeType::length(), exec_state)?;
2157 let semi_major = args.get_kw_arg("semiMajor", &RuntimeType::num_any(), exec_state)?;
2158 let semi_minor = args.get_kw_arg("semiMinor", &RuntimeType::num_any(), exec_state)?;
2159
2160 let hyperbolic_point = inner_hyperbolic_point(x, y, semi_major, semi_minor, &args).await?;
2161
2162 args.make_kcl_val_from_point(hyperbolic_point, exec_state.length_unit().into())
2163}
2164
2165async fn inner_hyperbolic_point(
2166 x: Option<TyF64>,
2167 y: Option<TyF64>,
2168 semi_major: TyF64,
2169 semi_minor: TyF64,
2170 args: &Args,
2171) -> Result<[f64; 2], KclError> {
2172 let semi_major = semi_major.n;
2173 let semi_minor = semi_minor.n;
2174 if let Some(x) = x {
2175 if x.n.abs() < semi_major {
2176 Err(KclError::Type {
2177 details: KclErrorDetails::new(
2178 format!(
2179 "Invalid input. The x value, {}, cannot be less than the semi major value, {}.",
2180 x.n, semi_major
2181 ),
2182 vec![args.source_range],
2183 ),
2184 })
2185 } else {
2186 Ok((x.n, semi_minor * (x.n.powf(2.0) / semi_major.powf(2.0) - 1.0).sqrt()).into())
2187 }
2188 } else if let Some(y) = y {
2189 Ok((semi_major * (y.n.powf(2.0) / semi_minor.powf(2.0) + 1.0).sqrt(), y.n).into())
2190 } else {
2191 Err(KclError::Type {
2192 details: KclErrorDetails::new(
2193 "Invalid input. Must have either x or y, cannot have both or neither.".to_owned(),
2194 vec![args.source_range],
2195 ),
2196 })
2197 }
2198}
2199
2200pub async fn hyperbolic(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2202 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
2203
2204 let semi_major = args.get_kw_arg("semiMajor", &RuntimeType::length(), exec_state)?;
2205 let semi_minor = args.get_kw_arg("semiMinor", &RuntimeType::length(), exec_state)?;
2206 let interior = args.get_kw_arg_opt("interior", &RuntimeType::point2d(), exec_state)?;
2207 let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
2208 let interior_absolute = args.get_kw_arg_opt("interiorAbsolute", &RuntimeType::point2d(), exec_state)?;
2209 let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
2210 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
2211
2212 let new_sketch = inner_hyperbolic(
2213 sketch,
2214 semi_major,
2215 semi_minor,
2216 interior,
2217 end,
2218 interior_absolute,
2219 end_absolute,
2220 tag,
2221 exec_state,
2222 args,
2223 )
2224 .await?;
2225 Ok(KclValue::Sketch {
2226 value: Box::new(new_sketch),
2227 })
2228}
2229
2230fn hyperbolic_tangent(point: Point2d, semi_major: f64, semi_minor: f64) -> [f64; 2] {
2232 (point.y * semi_major.powf(2.0), point.x * semi_minor.powf(2.0)).into()
2233}
2234
2235#[allow(clippy::too_many_arguments)]
2236pub(crate) async fn inner_hyperbolic(
2237 sketch: Sketch,
2238 semi_major: TyF64,
2239 semi_minor: TyF64,
2240 interior: Option<[TyF64; 2]>,
2241 end: Option<[TyF64; 2]>,
2242 interior_absolute: Option<[TyF64; 2]>,
2243 end_absolute: Option<[TyF64; 2]>,
2244 tag: Option<TagNode>,
2245 exec_state: &mut ExecState,
2246 args: Args,
2247) -> Result<Sketch, KclError> {
2248 let from = sketch.current_pen_position()?;
2249 let id = exec_state.next_uuid();
2250
2251 let (interior, end, relative) = match (interior, end, interior_absolute, end_absolute) {
2252 (Some(interior), Some(end), None, None) => (interior, end, true),
2253 (None, None, Some(interior_absolute), Some(end_absolute)) => (interior_absolute, end_absolute, false),
2254 _ => return Err(KclError::Type {
2255 details: KclErrorDetails::new(
2256 "Invalid combination of arguments. Either provide (end, interior) or (endAbsolute, interiorAbsolute)"
2257 .to_owned(),
2258 vec![args.source_range],
2259 ),
2260 }),
2261 };
2262
2263 let (interior, _) = untype_point(interior);
2264 let (end, _) = untype_point(end);
2265 let end_point = Point2d {
2266 x: end[0],
2267 y: end[1],
2268 units: from.units,
2269 };
2270
2271 let semi_major_u = semi_major.to_length_units(from.units);
2272 let semi_minor_u = semi_minor.to_length_units(from.units);
2273
2274 let start_tangent = hyperbolic_tangent(from, semi_major_u, semi_minor_u);
2275 let end_tangent = hyperbolic_tangent(end_point, semi_major_u, semi_minor_u);
2276
2277 exec_state
2278 .batch_modeling_cmd(
2279 ModelingCmdMeta::from_args_id(exec_state, &args, id),
2280 ModelingCmd::from(mcmd::ExtendPath {
2281 label: Default::default(),
2282 path: sketch.id.into(),
2283 segment: PathSegment::ConicTo {
2284 start_tangent: KPoint2d::from(untyped_point_to_mm(start_tangent, from.units)).map(LengthUnit),
2285 end_tangent: KPoint2d::from(untyped_point_to_mm(end_tangent, from.units)).map(LengthUnit),
2286 end: KPoint2d::from(untyped_point_to_mm(end, from.units)).map(LengthUnit),
2287 interior: KPoint2d::from(untyped_point_to_mm(interior, from.units)).map(LengthUnit),
2288 relative,
2289 },
2290 }),
2291 )
2292 .await?;
2293
2294 let current_path = Path::Conic {
2295 base: BasePath {
2296 from: from.ignore_units(),
2297 to: end,
2298 tag: tag.clone(),
2299 units: sketch.units,
2300 geo_meta: GeoMeta {
2301 id,
2302 metadata: args.source_range.into(),
2303 },
2304 },
2305 };
2306
2307 let mut new_sketch = sketch;
2308 if let Some(tag) = &tag {
2309 new_sketch.add_tag(tag, ¤t_path, exec_state, None);
2310 }
2311
2312 new_sketch.paths.push(current_path);
2313
2314 Ok(new_sketch)
2315}
2316
2317pub async fn parabolic_point(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2319 let x = args.get_kw_arg_opt("x", &RuntimeType::length(), exec_state)?;
2320 let y = args.get_kw_arg_opt("y", &RuntimeType::length(), exec_state)?;
2321 let coefficients = args.get_kw_arg(
2322 "coefficients",
2323 &RuntimeType::Array(Box::new(RuntimeType::num_any()), ArrayLen::Known(3)),
2324 exec_state,
2325 )?;
2326
2327 let parabolic_point = inner_parabolic_point(x, y, &coefficients, &args).await?;
2328
2329 args.make_kcl_val_from_point(parabolic_point, exec_state.length_unit().into())
2330}
2331
2332async fn inner_parabolic_point(
2333 x: Option<TyF64>,
2334 y: Option<TyF64>,
2335 coefficients: &[TyF64; 3],
2336 args: &Args,
2337) -> Result<[f64; 2], KclError> {
2338 let a = coefficients[0].n;
2339 let b = coefficients[1].n;
2340 let c = coefficients[2].n;
2341 if let Some(x) = x {
2342 Ok((x.n, a * x.n.powf(2.0) + b * x.n + c).into())
2343 } else if let Some(y) = y {
2344 let det = (b.powf(2.0) - 4.0 * a * (c - y.n)).sqrt();
2345 Ok(((-b + det) / (2.0 * a), y.n).into())
2346 } else {
2347 Err(KclError::Type {
2348 details: KclErrorDetails::new(
2349 "Invalid input. Must have either x or y, cannot have both or neither.".to_owned(),
2350 vec![args.source_range],
2351 ),
2352 })
2353 }
2354}
2355
2356pub async fn parabolic(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2358 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
2359
2360 let coefficients = args.get_kw_arg_opt(
2361 "coefficients",
2362 &RuntimeType::Array(Box::new(RuntimeType::num_any()), ArrayLen::Known(3)),
2363 exec_state,
2364 )?;
2365 let interior = args.get_kw_arg_opt("interior", &RuntimeType::point2d(), exec_state)?;
2366 let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
2367 let interior_absolute = args.get_kw_arg_opt("interiorAbsolute", &RuntimeType::point2d(), exec_state)?;
2368 let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
2369 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
2370
2371 let new_sketch = inner_parabolic(
2372 sketch,
2373 coefficients,
2374 interior,
2375 end,
2376 interior_absolute,
2377 end_absolute,
2378 tag,
2379 exec_state,
2380 args,
2381 )
2382 .await?;
2383 Ok(KclValue::Sketch {
2384 value: Box::new(new_sketch),
2385 })
2386}
2387
2388fn parabolic_tangent(point: Point2d, a: f64, b: f64) -> [f64; 2] {
2389 (1.0, 2.0 * a * point.x + b).into()
2392}
2393
2394#[allow(clippy::too_many_arguments)]
2395pub(crate) async fn inner_parabolic(
2396 sketch: Sketch,
2397 coefficients: Option<[TyF64; 3]>,
2398 interior: Option<[TyF64; 2]>,
2399 end: Option<[TyF64; 2]>,
2400 interior_absolute: Option<[TyF64; 2]>,
2401 end_absolute: Option<[TyF64; 2]>,
2402 tag: Option<TagNode>,
2403 exec_state: &mut ExecState,
2404 args: Args,
2405) -> Result<Sketch, KclError> {
2406 let from = sketch.current_pen_position()?;
2407 let id = exec_state.next_uuid();
2408
2409 if (coefficients.is_some() && interior.is_some()) || (coefficients.is_none() && interior.is_none()) {
2410 return Err(KclError::Type {
2411 details: KclErrorDetails::new(
2412 "Invalid combination of arguments. Either provide (a, b, c) or (interior)".to_owned(),
2413 vec![args.source_range],
2414 ),
2415 });
2416 }
2417
2418 let (interior, end, relative) = match (coefficients.clone(), interior, end, interior_absolute, end_absolute) {
2419 (None, Some(interior), Some(end), None, None) => {
2420 let (interior, _) = untype_point(interior);
2421 let (end, _) = untype_point(end);
2422 (interior,end, true)
2423 },
2424 (None, None, None, Some(interior_absolute), Some(end_absolute)) => {
2425 let (interior_absolute, _) = untype_point(interior_absolute);
2426 let (end_absolute, _) = untype_point(end_absolute);
2427 (interior_absolute, end_absolute, false)
2428 }
2429 (Some(coefficients), _, Some(end), _, _) => {
2430 let (end, _) = untype_point(end);
2431 let interior =
2432 inner_parabolic_point(
2433 Some(TyF64::count(0.5 * (from.x + end[0]))),
2434 None,
2435 &coefficients,
2436 &args,
2437 )
2438 .await?;
2439 (interior, end, true)
2440 }
2441 (Some(coefficients), _, _, _, Some(end)) => {
2442 let (end, _) = untype_point(end);
2443 let interior =
2444 inner_parabolic_point(
2445 Some(TyF64::count(0.5 * (from.x + end[0]))),
2446 None,
2447 &coefficients,
2448 &args,
2449 )
2450 .await?;
2451 (interior, end, false)
2452 }
2453 _ => return
2454 Err(KclError::Type{details: KclErrorDetails::new(
2455 "Invalid combination of arguments. Either provide (end, interior) or (endAbsolute, interiorAbsolute) if coefficients are not provided."
2456 .to_owned(),
2457 vec![args.source_range],
2458 )}),
2459 };
2460
2461 let end_point = Point2d {
2462 x: end[0],
2463 y: end[1],
2464 units: from.units,
2465 };
2466
2467 let (a, b, _c) = if let Some([a, b, c]) = coefficients {
2468 (a.n, b.n, c.n)
2469 } else {
2470 let denom = (from.x - interior[0]) * (from.x - end_point.x) * (interior[0] - end_point.x);
2472 let a = (end_point.x * (interior[1] - from.y)
2473 + interior[0] * (from.y - end_point.y)
2474 + from.x * (end_point.y - interior[1]))
2475 / denom;
2476 let b = (end_point.x.powf(2.0) * (from.y - interior[1])
2477 + interior[0].powf(2.0) * (end_point.y - from.y)
2478 + from.x.powf(2.0) * (interior[1] - end_point.y))
2479 / denom;
2480 let c = (interior[0] * end_point.x * (interior[0] - end_point.x) * from.y
2481 + end_point.x * from.x * (end_point.x - from.x) * interior[1]
2482 + from.x * interior[0] * (from.x - interior[0]) * end_point.y)
2483 / denom;
2484
2485 (a, b, c)
2486 };
2487
2488 let start_tangent = parabolic_tangent(from, a, b);
2489 let end_tangent = parabolic_tangent(end_point, a, b);
2490
2491 exec_state
2492 .batch_modeling_cmd(
2493 ModelingCmdMeta::from_args_id(exec_state, &args, id),
2494 ModelingCmd::from(mcmd::ExtendPath {
2495 label: Default::default(),
2496 path: sketch.id.into(),
2497 segment: PathSegment::ConicTo {
2498 start_tangent: KPoint2d::from(untyped_point_to_mm(start_tangent, from.units)).map(LengthUnit),
2499 end_tangent: KPoint2d::from(untyped_point_to_mm(end_tangent, from.units)).map(LengthUnit),
2500 end: KPoint2d::from(untyped_point_to_mm(end, from.units)).map(LengthUnit),
2501 interior: KPoint2d::from(untyped_point_to_mm(interior, from.units)).map(LengthUnit),
2502 relative,
2503 },
2504 }),
2505 )
2506 .await?;
2507
2508 let current_path = Path::Conic {
2509 base: BasePath {
2510 from: from.ignore_units(),
2511 to: end,
2512 tag: tag.clone(),
2513 units: sketch.units,
2514 geo_meta: GeoMeta {
2515 id,
2516 metadata: args.source_range.into(),
2517 },
2518 },
2519 };
2520
2521 let mut new_sketch = sketch;
2522 if let Some(tag) = &tag {
2523 new_sketch.add_tag(tag, ¤t_path, exec_state, None);
2524 }
2525
2526 new_sketch.paths.push(current_path);
2527
2528 Ok(new_sketch)
2529}
2530
2531fn conic_tangent(coefficients: [f64; 6], point: [f64; 2]) -> [f64; 2] {
2532 let [a, b, c, d, e, _] = coefficients;
2533
2534 (
2535 c * point[0] + 2.0 * b * point[1] + e,
2536 -(2.0 * a * point[0] + c * point[1] + d),
2537 )
2538 .into()
2539}
2540
2541pub async fn conic(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
2543 let sketch = args.get_unlabeled_kw_arg("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
2544
2545 let start_tangent = args.get_kw_arg_opt("startTangent", &RuntimeType::point2d(), exec_state)?;
2546 let end_tangent = args.get_kw_arg_opt("endTangent", &RuntimeType::point2d(), exec_state)?;
2547 let end = args.get_kw_arg_opt("end", &RuntimeType::point2d(), exec_state)?;
2548 let interior = args.get_kw_arg_opt("interior", &RuntimeType::point2d(), exec_state)?;
2549 let end_absolute = args.get_kw_arg_opt("endAbsolute", &RuntimeType::point2d(), exec_state)?;
2550 let interior_absolute = args.get_kw_arg_opt("interiorAbsolute", &RuntimeType::point2d(), exec_state)?;
2551 let coefficients = args.get_kw_arg_opt(
2552 "coefficients",
2553 &RuntimeType::Array(Box::new(RuntimeType::num_any()), ArrayLen::Known(6)),
2554 exec_state,
2555 )?;
2556 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
2557
2558 let new_sketch = inner_conic(
2559 sketch,
2560 start_tangent,
2561 end,
2562 end_tangent,
2563 interior,
2564 coefficients,
2565 interior_absolute,
2566 end_absolute,
2567 tag,
2568 exec_state,
2569 args,
2570 )
2571 .await?;
2572 Ok(KclValue::Sketch {
2573 value: Box::new(new_sketch),
2574 })
2575}
2576
2577#[allow(clippy::too_many_arguments)]
2578pub(crate) async fn inner_conic(
2579 sketch: Sketch,
2580 start_tangent: Option<[TyF64; 2]>,
2581 end: Option<[TyF64; 2]>,
2582 end_tangent: Option<[TyF64; 2]>,
2583 interior: Option<[TyF64; 2]>,
2584 coefficients: Option<[TyF64; 6]>,
2585 interior_absolute: Option<[TyF64; 2]>,
2586 end_absolute: Option<[TyF64; 2]>,
2587 tag: Option<TagNode>,
2588 exec_state: &mut ExecState,
2589 args: Args,
2590) -> Result<Sketch, KclError> {
2591 let from: Point2d = sketch.current_pen_position()?;
2592 let id = exec_state.next_uuid();
2593
2594 if (coefficients.is_some() && (start_tangent.is_some() || end_tangent.is_some()))
2595 || (coefficients.is_none() && (start_tangent.is_none() && end_tangent.is_none()))
2596 {
2597 return Err(KclError::Type {
2598 details: KclErrorDetails::new(
2599 "Invalid combination of arguments. Either provide coefficients or (startTangent, endTangent)"
2600 .to_owned(),
2601 vec![args.source_range],
2602 ),
2603 });
2604 }
2605
2606 let (interior, end, relative) = match (interior, end, interior_absolute, end_absolute) {
2607 (Some(interior), Some(end), None, None) => (interior, end, true),
2608 (None, None, Some(interior_absolute), Some(end_absolute)) => (interior_absolute, end_absolute, false),
2609 _ => return Err(KclError::Type {
2610 details: KclErrorDetails::new(
2611 "Invalid combination of arguments. Either provide (end, interior) or (endAbsolute, interiorAbsolute)"
2612 .to_owned(),
2613 vec![args.source_range],
2614 ),
2615 }),
2616 };
2617
2618 let (end, _) = untype_array(end);
2619 let (interior, _) = untype_point(interior);
2620
2621 let (start_tangent, end_tangent) = if let Some(coeffs) = coefficients {
2622 let (coeffs, _) = untype_array(coeffs);
2623 (conic_tangent(coeffs, [from.x, from.y]), conic_tangent(coeffs, end))
2624 } else {
2625 let start = if let Some(start_tangent) = start_tangent {
2626 let (start, _) = untype_point(start_tangent);
2627 start
2628 } else {
2629 let previous_point = sketch
2630 .get_tangential_info_from_paths()
2631 .tan_previous_point(from.ignore_units());
2632 let from = from.ignore_units();
2633 [from[0] - previous_point[0], from[1] - previous_point[1]]
2634 };
2635
2636 let Some(end_tangent) = end_tangent else {
2637 return Err(KclError::new_semantic(KclErrorDetails::new(
2638 "You must either provide either `coefficients` or `endTangent`.".to_owned(),
2639 vec![args.source_range],
2640 )));
2641 };
2642 let (end_tan, _) = untype_point(end_tangent);
2643 (start, end_tan)
2644 };
2645
2646 exec_state
2647 .batch_modeling_cmd(
2648 ModelingCmdMeta::from_args_id(exec_state, &args, id),
2649 ModelingCmd::from(mcmd::ExtendPath {
2650 label: Default::default(),
2651 path: sketch.id.into(),
2652 segment: PathSegment::ConicTo {
2653 start_tangent: KPoint2d::from(untyped_point_to_mm(start_tangent, from.units)).map(LengthUnit),
2654 end_tangent: KPoint2d::from(untyped_point_to_mm(end_tangent, from.units)).map(LengthUnit),
2655 end: KPoint2d::from(untyped_point_to_mm(end, from.units)).map(LengthUnit),
2656 interior: KPoint2d::from(untyped_point_to_mm(interior, from.units)).map(LengthUnit),
2657 relative,
2658 },
2659 }),
2660 )
2661 .await?;
2662
2663 let current_path = Path::Conic {
2664 base: BasePath {
2665 from: from.ignore_units(),
2666 to: end,
2667 tag: tag.clone(),
2668 units: sketch.units,
2669 geo_meta: GeoMeta {
2670 id,
2671 metadata: args.source_range.into(),
2672 },
2673 },
2674 };
2675
2676 let mut new_sketch = sketch;
2677 if let Some(tag) = &tag {
2678 new_sketch.add_tag(tag, ¤t_path, exec_state, None);
2679 }
2680
2681 new_sketch.paths.push(current_path);
2682
2683 Ok(new_sketch)
2684}
2685#[cfg(test)]
2686mod tests {
2687
2688 use pretty_assertions::assert_eq;
2689
2690 use crate::{
2691 execution::TagIdentifier,
2692 std::{sketch::PlaneData, utils::calculate_circle_center},
2693 };
2694
2695 #[test]
2696 fn test_deserialize_plane_data() {
2697 let data = PlaneData::XY;
2698 let mut str_json = serde_json::to_string(&data).unwrap();
2699 assert_eq!(str_json, "\"XY\"");
2700
2701 str_json = "\"YZ\"".to_string();
2702 let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2703 assert_eq!(data, PlaneData::YZ);
2704
2705 str_json = "\"-YZ\"".to_string();
2706 let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2707 assert_eq!(data, PlaneData::NegYZ);
2708
2709 str_json = "\"-xz\"".to_string();
2710 let data: PlaneData = serde_json::from_str(&str_json).unwrap();
2711 assert_eq!(data, PlaneData::NegXZ);
2712 }
2713
2714 #[test]
2715 fn test_deserialize_sketch_on_face_tag() {
2716 let data = "start";
2717 let mut str_json = serde_json::to_string(&data).unwrap();
2718 assert_eq!(str_json, "\"start\"");
2719
2720 str_json = "\"end\"".to_string();
2721 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2722 assert_eq!(
2723 data,
2724 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
2725 );
2726
2727 str_json = serde_json::to_string(&TagIdentifier {
2728 value: "thing".to_string(),
2729 info: Vec::new(),
2730 meta: Default::default(),
2731 })
2732 .unwrap();
2733 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2734 assert_eq!(
2735 data,
2736 crate::std::sketch::FaceTag::Tag(Box::new(TagIdentifier {
2737 value: "thing".to_string(),
2738 info: Vec::new(),
2739 meta: Default::default()
2740 }))
2741 );
2742
2743 str_json = "\"END\"".to_string();
2744 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2745 assert_eq!(
2746 data,
2747 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::End)
2748 );
2749
2750 str_json = "\"start\"".to_string();
2751 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2752 assert_eq!(
2753 data,
2754 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
2755 );
2756
2757 str_json = "\"START\"".to_string();
2758 let data: crate::std::sketch::FaceTag = serde_json::from_str(&str_json).unwrap();
2759 assert_eq!(
2760 data,
2761 crate::std::sketch::FaceTag::StartOrEnd(crate::std::sketch::StartOrEnd::Start)
2762 );
2763 }
2764
2765 #[test]
2766 fn test_circle_center() {
2767 let actual = calculate_circle_center([0.0, 0.0], [5.0, 5.0], [10.0, 0.0]);
2768 assert_eq!(actual[0], 5.0);
2769 assert_eq!(actual[1], 0.0);
2770 }
2771}