1use anyhow::Result;
4use kcmc::ModelingCmd;
5use kcmc::each_cmd as mcmd;
6use kcmc::length_unit::LengthUnit;
7use kcmc::shared;
8use kcmc::shared::OriginType;
9use kcmc::shared::Point3d;
10use kittycad_modeling_cmds as kcmc;
11
12use crate::errors::KclError;
13use crate::errors::KclErrorDetails;
14use crate::execution::ExecState;
15use crate::execution::HideableGeometry;
16use crate::execution::KclValue;
17use crate::execution::ModelingCmdMeta;
18use crate::execution::SolidOrSketchOrImportedGeometry;
19use crate::execution::types::PrimitiveType;
20use crate::execution::types::RuntimeType;
21use crate::std::Args;
22use crate::std::args::TyF64;
23use crate::std::axis_or_reference::Axis3dOrPoint3d;
24
25fn transform_by<T>(property: T, set: bool, origin: OriginType) -> shared::TransformBy<T> {
26 shared::TransformBy::builder()
27 .property(property)
28 .set(set)
29 .origin(origin)
30 .build()
31}
32
33pub async fn scale(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
35 let objects = args.get_unlabeled_kw_arg(
36 "objects",
37 &RuntimeType::Union(vec![
38 RuntimeType::sketches(),
39 RuntimeType::solids(),
40 RuntimeType::imported(),
41 ]),
42 exec_state,
43 )?;
44 let scale_x: Option<TyF64> = args.get_kw_arg_opt("x", &RuntimeType::count(), exec_state)?;
45 let scale_y: Option<TyF64> = args.get_kw_arg_opt("y", &RuntimeType::count(), exec_state)?;
46 let scale_z: Option<TyF64> = args.get_kw_arg_opt("z", &RuntimeType::count(), exec_state)?;
47 let factor: Option<TyF64> = args.get_kw_arg_opt("factor", &RuntimeType::count(), exec_state)?;
48 for scale_dim in [&scale_x, &scale_y, &scale_z, &factor] {
49 if let Some(num) = scale_dim
50 && num.n == 0.0
51 {
52 return Err(KclError::new_semantic(KclErrorDetails::new(
53 "Cannot scale by 0".to_string(),
54 vec![args.source_range],
55 )));
56 }
57 }
58 let (scale_x, scale_y, scale_z) = match (scale_x, scale_y, scale_z, factor) {
59 (None, None, None, Some(factor)) => (Some(factor.clone()), Some(factor.clone()), Some(factor)),
60 (None, None, None, None) => {
62 return Err(KclError::new_semantic(KclErrorDetails::new(
63 "Expected `x`, `y`, `z` or `factor` to be provided.".to_string(),
64 vec![args.source_range],
65 )));
66 }
67 (x, y, z, None) => (x, y, z),
68 _ => {
69 return Err(KclError::new_semantic(KclErrorDetails::new(
70 "If you give `factor` then you cannot use `x`, `y`, or `z`".to_string(),
71 vec![args.source_range],
72 )));
73 }
74 };
75 let global = args.get_kw_arg_opt("global", &RuntimeType::bool(), exec_state)?;
76
77 let objects = inner_scale(
78 objects,
79 scale_x.map(|t| t.n),
80 scale_y.map(|t| t.n),
81 scale_z.map(|t| t.n),
82 global,
83 exec_state,
84 args,
85 )
86 .await?;
87 Ok(objects.into())
88}
89
90async fn inner_scale(
91 objects: SolidOrSketchOrImportedGeometry,
92 x: Option<f64>,
93 y: Option<f64>,
94 z: Option<f64>,
95 global: Option<bool>,
96 exec_state: &mut ExecState,
97 args: Args,
98) -> Result<SolidOrSketchOrImportedGeometry, KclError> {
99 if let SolidOrSketchOrImportedGeometry::SolidSet(solids) = &objects {
102 exec_state
103 .flush_batch_for_solids(ModelingCmdMeta::from_args(exec_state, &args), solids)
104 .await?;
105 }
106
107 let is_global = global.unwrap_or(false);
108 let origin = if is_global {
109 OriginType::Global
110 } else {
111 OriginType::Local
112 };
113
114 let mut objects = objects.clone();
115 for object_id in objects.ids(&args.ctx).await? {
116 let transform = shared::ComponentTransform::builder()
117 .scale(transform_by(
118 Point3d {
119 x: x.unwrap_or(1.0),
120 y: y.unwrap_or(1.0),
121 z: z.unwrap_or(1.0),
122 },
123 false,
124 origin,
125 ))
126 .build();
127 let transforms = vec![transform];
128 exec_state
129 .batch_modeling_cmd(
130 ModelingCmdMeta::from_args(exec_state, &args),
131 ModelingCmd::from(
132 mcmd::SetObjectTransform::builder()
133 .object_id(object_id)
134 .transforms(transforms)
135 .build(),
136 ),
137 )
138 .await?;
139 }
140
141 Ok(objects)
142}
143
144pub async fn translate(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
146 let objects = args.get_unlabeled_kw_arg(
147 "objects",
148 &RuntimeType::Union(vec![
149 RuntimeType::sketches(),
150 RuntimeType::solids(),
151 RuntimeType::imported(),
152 ]),
153 exec_state,
154 )?;
155 let translate_x: Option<TyF64> = args.get_kw_arg_opt("x", &RuntimeType::length(), exec_state)?;
156 let translate_y: Option<TyF64> = args.get_kw_arg_opt("y", &RuntimeType::length(), exec_state)?;
157 let translate_z: Option<TyF64> = args.get_kw_arg_opt("z", &RuntimeType::length(), exec_state)?;
158 let xyz: Option<[TyF64; 3]> = args.get_kw_arg_opt("xyz", &RuntimeType::point3d(), exec_state)?;
159 let global = args.get_kw_arg_opt("global", &RuntimeType::bool(), exec_state)?;
160
161 let objects = inner_translate(
162 objects,
163 xyz,
164 translate_x,
165 translate_y,
166 translate_z,
167 global,
168 exec_state,
169 args,
170 )
171 .await?;
172 Ok(objects.into())
173}
174
175#[allow(clippy::too_many_arguments)]
176async fn inner_translate(
177 objects: SolidOrSketchOrImportedGeometry,
178 xyz: Option<[TyF64; 3]>,
179 x: Option<TyF64>,
180 y: Option<TyF64>,
181 z: Option<TyF64>,
182 global: Option<bool>,
183 exec_state: &mut ExecState,
184 args: Args,
185) -> Result<SolidOrSketchOrImportedGeometry, KclError> {
186 let (x, y, z) = match (xyz, x, y, z) {
187 (None, None, None, None) => {
188 return Err(KclError::new_semantic(KclErrorDetails::new(
189 "Expected `x`, `y`, or `z` to be provided.".to_string(),
190 vec![args.source_range],
191 )));
192 }
193 (Some(xyz), None, None, None) => {
194 let [x, y, z] = xyz;
195 (Some(x), Some(y), Some(z))
196 }
197 (None, x, y, z) => (x, y, z),
198 (Some(_), _, _, _) => {
199 return Err(KclError::new_semantic(KclErrorDetails::new(
200 "If you provide all 3 distances via the `xyz` arg, you cannot provide them separately via the `x`, `y` or `z` args."
201 .to_string(),
202 vec![args.source_range],
203 )));
204 }
205 };
206 if let SolidOrSketchOrImportedGeometry::SolidSet(solids) = &objects {
209 exec_state
210 .flush_batch_for_solids(ModelingCmdMeta::from_args(exec_state, &args), solids)
211 .await?;
212 }
213
214 let is_global = global.unwrap_or(false);
215 let origin = if is_global {
216 OriginType::Global
217 } else {
218 OriginType::Local
219 };
220
221 let translation = shared::Point3d {
222 x: LengthUnit(x.as_ref().map(|t| t.to_mm()).unwrap_or_default()),
223 y: LengthUnit(y.as_ref().map(|t| t.to_mm()).unwrap_or_default()),
224 z: LengthUnit(z.as_ref().map(|t| t.to_mm()).unwrap_or_default()),
225 };
226 let mut objects = objects.clone();
227 for object_id in objects.ids(&args.ctx).await? {
228 let transform = shared::ComponentTransform::builder()
229 .translate(transform_by(translation, false, origin))
230 .build();
231 let transforms = vec![transform];
232 exec_state
233 .batch_modeling_cmd(
234 ModelingCmdMeta::from_args(exec_state, &args),
235 ModelingCmd::from(
236 mcmd::SetObjectTransform::builder()
237 .object_id(object_id)
238 .transforms(transforms)
239 .build(),
240 ),
241 )
242 .await?;
243 }
244
245 Ok(objects)
246}
247
248pub async fn rotate(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
250 let objects = args.get_unlabeled_kw_arg(
251 "objects",
252 &RuntimeType::Union(vec![
253 RuntimeType::sketches(),
254 RuntimeType::solids(),
255 RuntimeType::imported(),
256 ]),
257 exec_state,
258 )?;
259 let roll: Option<TyF64> = args.get_kw_arg_opt("roll", &RuntimeType::degrees(), exec_state)?;
260 let pitch: Option<TyF64> = args.get_kw_arg_opt("pitch", &RuntimeType::degrees(), exec_state)?;
261 let yaw: Option<TyF64> = args.get_kw_arg_opt("yaw", &RuntimeType::degrees(), exec_state)?;
262 let axis: Option<Axis3dOrPoint3d> = args.get_kw_arg_opt(
263 "axis",
264 &RuntimeType::Union(vec![
265 RuntimeType::Primitive(PrimitiveType::Axis3d),
266 RuntimeType::point3d(),
267 ]),
268 exec_state,
269 )?;
270 let origin = axis.clone().map(|a| a.axis_origin()).unwrap_or_default();
271 let axis = axis.map(|a| a.to_point3d());
272 let angle: Option<TyF64> = args.get_kw_arg_opt("angle", &RuntimeType::degrees(), exec_state)?;
273 let global = args.get_kw_arg_opt("global", &RuntimeType::bool(), exec_state)?;
274
275 if roll.is_none() && pitch.is_none() && yaw.is_none() && axis.is_none() && angle.is_none() {
277 return Err(KclError::new_semantic(KclErrorDetails::new(
278 "Expected `roll`, `pitch`, and `yaw` or `axis` and `angle` to be provided.".to_string(),
279 vec![args.source_range],
280 )));
281 }
282
283 if roll.is_some() || pitch.is_some() || yaw.is_some() {
285 if axis.is_some() || angle.is_some() {
287 return Err(KclError::new_semantic(KclErrorDetails::new(
288 "Expected `axis` and `angle` to not be provided when `roll`, `pitch`, and `yaw` are provided."
289 .to_owned(),
290 vec![args.source_range],
291 )));
292 }
293 }
294
295 if axis.is_some() || angle.is_some() {
297 if axis.is_none() {
298 return Err(KclError::new_semantic(KclErrorDetails::new(
299 "Expected `axis` to be provided when `angle` is provided.".to_string(),
300 vec![args.source_range],
301 )));
302 }
303 if angle.is_none() {
304 return Err(KclError::new_semantic(KclErrorDetails::new(
305 "Expected `angle` to be provided when `axis` is provided.".to_string(),
306 vec![args.source_range],
307 )));
308 }
309
310 if roll.is_some() || pitch.is_some() || yaw.is_some() {
312 return Err(KclError::new_semantic(KclErrorDetails::new(
313 "Expected `roll`, `pitch`, and `yaw` to not be provided when `axis` and `angle` are provided."
314 .to_owned(),
315 vec![args.source_range],
316 )));
317 }
318 }
319
320 if let Some(roll) = &roll
322 && !(-360.0..=360.0).contains(&roll.n)
323 {
324 return Err(KclError::new_semantic(KclErrorDetails::new(
325 format!("Expected roll to be between -360 and 360, found `{}`", roll.n),
326 vec![args.source_range],
327 )));
328 }
329 if let Some(pitch) = &pitch
330 && !(-360.0..=360.0).contains(&pitch.n)
331 {
332 return Err(KclError::new_semantic(KclErrorDetails::new(
333 format!("Expected pitch to be between -360 and 360, found `{}`", pitch.n),
334 vec![args.source_range],
335 )));
336 }
337 if let Some(yaw) = &yaw
338 && !(-360.0..=360.0).contains(&yaw.n)
339 {
340 return Err(KclError::new_semantic(KclErrorDetails::new(
341 format!("Expected yaw to be between -360 and 360, found `{}`", yaw.n),
342 vec![args.source_range],
343 )));
344 }
345
346 if let Some(angle) = &angle
348 && !(-360.0..=360.0).contains(&angle.n)
349 {
350 return Err(KclError::new_semantic(KclErrorDetails::new(
351 format!("Expected angle to be between -360 and 360, found `{}`", angle.n),
352 vec![args.source_range],
353 )));
354 }
355
356 let objects = inner_rotate(
357 objects,
358 roll.map(|t| t.n),
359 pitch.map(|t| t.n),
360 yaw.map(|t| t.n),
361 axis.map(|a| [a[0].n, a[1].n, a[2].n]),
364 origin.map(|a| [a[0].n, a[1].n, a[2].n]),
365 angle.map(|t| t.n),
366 global,
367 exec_state,
368 args,
369 )
370 .await?;
371 Ok(objects.into())
372}
373
374#[allow(clippy::too_many_arguments)]
375async fn inner_rotate(
376 objects: SolidOrSketchOrImportedGeometry,
377 roll: Option<f64>,
378 pitch: Option<f64>,
379 yaw: Option<f64>,
380 axis: Option<[f64; 3]>,
381 origin: Option<[f64; 3]>,
382 angle: Option<f64>,
383 global: Option<bool>,
384 exec_state: &mut ExecState,
385 args: Args,
386) -> Result<SolidOrSketchOrImportedGeometry, KclError> {
387 if let SolidOrSketchOrImportedGeometry::SolidSet(solids) = &objects {
390 exec_state
391 .flush_batch_for_solids(ModelingCmdMeta::from_args(exec_state, &args), solids)
392 .await?;
393 }
394
395 let origin = if let Some(origin) = origin {
396 OriginType::Custom {
397 origin: shared::Point3d {
398 x: origin[0],
399 y: origin[1],
400 z: origin[2],
401 },
402 }
403 } else if global.unwrap_or(false) {
404 OriginType::Global
405 } else {
406 OriginType::Local
407 };
408
409 let mut objects = objects.clone();
410 for object_id in objects.ids(&args.ctx).await? {
411 if let (Some(axis), Some(angle)) = (&axis, angle) {
412 let transform = shared::ComponentTransform::builder()
413 .rotate_angle_axis(transform_by(
414 shared::Point4d {
415 x: axis[0],
416 y: axis[1],
417 z: axis[2],
418 w: angle,
419 },
420 false,
421 origin,
422 ))
423 .build();
424 let transforms = vec![transform];
425 exec_state
426 .batch_modeling_cmd(
427 ModelingCmdMeta::from_args(exec_state, &args),
428 ModelingCmd::from(
429 mcmd::SetObjectTransform::builder()
430 .object_id(object_id)
431 .transforms(transforms)
432 .build(),
433 ),
434 )
435 .await?;
436 } else {
437 let transform = shared::ComponentTransform::builder()
439 .rotate_rpy(transform_by(
440 shared::Point3d {
441 x: roll.unwrap_or(0.0),
442 y: pitch.unwrap_or(0.0),
443 z: yaw.unwrap_or(0.0),
444 },
445 false,
446 origin,
447 ))
448 .build();
449 let transforms = vec![transform];
450 exec_state
451 .batch_modeling_cmd(
452 ModelingCmdMeta::from_args(exec_state, &args),
453 ModelingCmd::from(
454 mcmd::SetObjectTransform::builder()
455 .object_id(object_id)
456 .transforms(transforms)
457 .build(),
458 ),
459 )
460 .await?;
461 }
462 }
463
464 Ok(objects)
465}
466
467pub async fn hide(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
469 let objects = args.get_unlabeled_kw_arg(
470 "objects",
471 &RuntimeType::Union(vec![
472 RuntimeType::sketches(),
473 RuntimeType::solids(),
474 RuntimeType::helices(),
475 RuntimeType::imported(),
476 RuntimeType::gdts(),
477 ]),
478 exec_state,
479 )?;
480
481 let objects = hide_inner(objects, true, exec_state, args).await?;
482 Ok(objects.into())
483}
484
485async fn hide_inner(
486 mut objects: HideableGeometry,
487 hidden: bool,
488 exec_state: &mut ExecState,
489 args: Args,
490) -> Result<HideableGeometry, KclError> {
491 for object_id in objects.ids(&args.ctx).await? {
492 exec_state
493 .batch_modeling_cmd(
494 ModelingCmdMeta::from_args(exec_state, &args),
495 ModelingCmd::from(
496 mcmd::ObjectVisible::builder()
497 .object_id(object_id)
498 .hidden(hidden)
499 .build(),
500 ),
501 )
502 .await?;
503 }
504
505 Ok(objects)
506}
507
508pub async fn delete(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
510 let objects = args.get_unlabeled_kw_arg(
511 "objects",
512 &RuntimeType::Union(vec![
513 RuntimeType::sketches(),
514 RuntimeType::solids(),
515 RuntimeType::helices(),
516 RuntimeType::imported(),
517 RuntimeType::gdts(),
518 ]),
519 exec_state,
520 )?;
521
522 delete_inner(objects, exec_state, args).await.map(|()| KclValue::none())
523}
524
525async fn delete_inner(mut objects: HideableGeometry, exec_state: &mut ExecState, args: Args) -> Result<(), KclError> {
526 let ids = objects.ids(&args.ctx).await?.into_iter().collect();
527 exec_state
528 .batch_modeling_cmd(
529 ModelingCmdMeta::from_args(exec_state, &args),
530 ModelingCmd::from(mcmd::RemoveSceneObjects::builder().object_ids(ids).build()),
531 )
532 .await
533}
534
535#[cfg(test)]
536mod tests {
537 use pretty_assertions::assert_eq;
538
539 use crate::errors::Severity;
540 use crate::errors::Tag;
541 use crate::execution::MockConfig;
542 use crate::execution::parse_execute;
543
544 const PIPE: &str = r#"sweepPath = startSketchOn(XZ)
545 |> startProfile(at = [0.05, 0.05])
546 |> line(end = [0, 7])
547 |> tangentialArc(angle = 90, radius = 5)
548 |> line(end = [-3, 0])
549 |> tangentialArc(angle = -90, radius = 5)
550 |> line(end = [0, 7])
551
552// Create a hole for the pipe.
553pipeHole = startSketchOn(XY)
554 |> circle(
555 center = [0, 0],
556 radius = 1.5,
557 )
558sweepSketch = startSketchOn(XY)
559 |> circle(
560 center = [0, 0],
561 radius = 2,
562 )
563 |> subtract2d(tool = pipeHole)
564 |> sweep(
565 path = sweepPath,
566 )"#;
567
568 #[tokio::test(flavor = "multi_thread")]
569 async fn test_rotate_empty() {
570 let ast = PIPE.to_string()
571 + r#"
572 |> rotate()
573"#;
574 let result = parse_execute(&ast).await;
575 assert!(result.is_err());
576 assert_eq!(
577 result.unwrap_err().message(),
578 r#"Expected `roll`, `pitch`, and `yaw` or `axis` and `angle` to be provided."#.to_string()
579 );
580 }
581
582 #[tokio::test(flavor = "multi_thread")]
583 async fn test_rotate_axis_no_angle() {
584 let ast = PIPE.to_string()
585 + r#"
586 |> rotate(
587 axis = [0, 0, 1.0],
588 )
589"#;
590 let result = parse_execute(&ast).await;
591 assert!(result.is_err());
592 assert_eq!(
593 result.unwrap_err().message(),
594 r#"Expected `angle` to be provided when `axis` is provided."#.to_string()
595 );
596 }
597
598 #[tokio::test(flavor = "multi_thread")]
599 async fn test_rotate_angle_no_axis() {
600 let ast = PIPE.to_string()
601 + r#"
602 |> rotate(
603 angle = 90,
604 )
605"#;
606 let result = parse_execute(&ast).await;
607 assert!(result.is_err());
608 assert_eq!(
609 result.unwrap_err().message(),
610 r#"Expected `axis` to be provided when `angle` is provided."#.to_string()
611 );
612 }
613
614 #[tokio::test(flavor = "multi_thread")]
615 async fn test_rotate_angle_out_of_range() {
616 let ast = PIPE.to_string()
617 + r#"
618 |> rotate(
619 axis = [0, 0, 1.0],
620 angle = 900,
621 )
622"#;
623 let result = parse_execute(&ast).await;
624 assert!(result.is_err());
625 assert_eq!(
626 result.unwrap_err().message(),
627 r#"Expected angle to be between -360 and 360, found `900`"#.to_string()
628 );
629 }
630
631 #[tokio::test(flavor = "multi_thread")]
632 async fn test_rotate_angle_axis_yaw() {
633 let ast = PIPE.to_string()
634 + r#"
635 |> rotate(
636 axis = [0, 0, 1.0],
637 angle = 90,
638 yaw = 90,
639 )
640"#;
641 let result = parse_execute(&ast).await;
642 assert!(result.is_err());
643 assert_eq!(
644 result.unwrap_err().message(),
645 r#"Expected `axis` and `angle` to not be provided when `roll`, `pitch`, and `yaw` are provided."#
646 .to_string()
647 );
648 }
649
650 #[tokio::test(flavor = "multi_thread")]
651 async fn test_rotate_yaw_only() {
652 let ast = PIPE.to_string()
653 + r#"
654 |> rotate(
655 yaw = 90,
656 )
657"#;
658 parse_execute(&ast).await.unwrap();
659 }
660
661 #[tokio::test(flavor = "multi_thread")]
662 async fn test_rotate_pitch_only() {
663 let ast = PIPE.to_string()
664 + r#"
665 |> rotate(
666 pitch = 90,
667 )
668"#;
669 parse_execute(&ast).await.unwrap();
670 }
671
672 #[tokio::test(flavor = "multi_thread")]
673 async fn test_rotate_roll_only() {
674 let ast = PIPE.to_string()
675 + r#"
676 |> rotate(
677 pitch = 90,
678 )
679"#;
680 parse_execute(&ast).await.unwrap();
681 }
682
683 #[tokio::test(flavor = "multi_thread")]
684 async fn test_rotate_yaw_out_of_range() {
685 let ast = PIPE.to_string()
686 + r#"
687 |> rotate(
688 yaw = 900,
689 pitch = 90,
690 roll = 90,
691 )
692"#;
693 let result = parse_execute(&ast).await;
694 assert!(result.is_err());
695 assert_eq!(
696 result.unwrap_err().message(),
697 r#"Expected yaw to be between -360 and 360, found `900`"#.to_string()
698 );
699 }
700
701 #[tokio::test(flavor = "multi_thread")]
702 async fn test_rotate_roll_out_of_range() {
703 let ast = PIPE.to_string()
704 + r#"
705 |> rotate(
706 yaw = 90,
707 pitch = 90,
708 roll = 900,
709 )
710"#;
711 let result = parse_execute(&ast).await;
712 assert!(result.is_err());
713 assert_eq!(
714 result.unwrap_err().message(),
715 r#"Expected roll to be between -360 and 360, found `900`"#.to_string()
716 );
717 }
718
719 #[tokio::test(flavor = "multi_thread")]
720 async fn test_rotate_pitch_out_of_range() {
721 let ast = PIPE.to_string()
722 + r#"
723 |> rotate(
724 yaw = 90,
725 pitch = 900,
726 roll = 90,
727 )
728"#;
729 let result = parse_execute(&ast).await;
730 assert!(result.is_err());
731 assert_eq!(
732 result.unwrap_err().message(),
733 r#"Expected pitch to be between -360 and 360, found `900`"#.to_string()
734 );
735 }
736
737 #[tokio::test(flavor = "multi_thread")]
738 async fn test_rotate_roll_pitch_yaw_with_angle() {
739 let ast = PIPE.to_string()
740 + r#"
741 |> rotate(
742 yaw = 90,
743 pitch = 90,
744 roll = 90,
745 angle = 90,
746 )
747"#;
748 let result = parse_execute(&ast).await;
749 assert!(result.is_err());
750 assert_eq!(
751 result.unwrap_err().message(),
752 r#"Expected `axis` and `angle` to not be provided when `roll`, `pitch`, and `yaw` are provided."#
753 .to_string()
754 );
755 }
756
757 #[tokio::test(flavor = "multi_thread")]
758 async fn test_translate_no_args() {
759 let ast = PIPE.to_string()
760 + r#"
761 |> translate(
762 )
763"#;
764 let result = parse_execute(&ast).await;
765 assert!(result.is_err());
766 assert_eq!(
767 result.unwrap_err().message(),
768 r#"Expected `x`, `y`, or `z` to be provided."#.to_string()
769 );
770 }
771
772 #[tokio::test(flavor = "multi_thread")]
773 async fn test_scale_no_args() {
774 let ast = PIPE.to_string()
775 + r#"
776 |> scale(
777 )
778"#;
779 let result = parse_execute(&ast).await;
780 assert!(result.is_err());
781 assert_eq!(
782 result.unwrap_err().message(),
783 r#"Expected `x`, `y`, `z` or `factor` to be provided."#.to_string()
784 );
785 }
786
787 #[tokio::test(flavor = "multi_thread")]
788 async fn test_hide_pipe_solid_ok() {
789 let ast = PIPE.to_string()
790 + r#"
791 |> hide()
792"#;
793 parse_execute(&ast).await.unwrap();
794 }
795
796 #[tokio::test(flavor = "multi_thread")]
797 async fn hide_consumed_solid_reports_deprecation_warning() {
798 let code = r#"
799targetSketch = sketch(on = XY) {
800 line1 = line(start = [var -10, var -10], end = [var 10, var -10])
801 line2 = line(start = [var 10, var -10], end = [var 10, var 10])
802 line3 = line(start = [var 10, var 10], end = [var -10, var 10])
803 line4 = line(start = [var -10, var 10], end = [var -10, var -10])
804 coincident([line1.end, line2.start])
805 coincident([line2.end, line3.start])
806 coincident([line3.end, line4.start])
807 coincident([line4.end, line1.start])
808 equalLength([line1, line2, line3, line4])
809}
810
811target = extrude(region(point = [0, 0], sketch = targetSketch), length = 20)
812
813toolSketch = sketch(on = XY) {
814 line1 = line(start = [var -2, var -2], end = [var 2, var -2])
815 line2 = line(start = [var 2, var -2], end = [var 2, var 2])
816 line3 = line(start = [var 2, var 2], end = [var -2, var 2])
817 line4 = line(start = [var -2, var 2], end = [var -2, var -2])
818 coincident([line1.end, line2.start])
819 coincident([line2.end, line3.start])
820 coincident([line3.end, line4.start])
821 coincident([line4.end, line1.start])
822 equalLength([line1, line2, line3, line4])
823}
824
825tool = extrude(region(point = [0, 0], sketch = toolSketch), length = 4)
826
827result = subtract(target, tools = [tool])
828hidden = hide(target)
829"#;
830
831 let program = crate::Program::parse_no_errs(code).unwrap();
832 let ctx = crate::ExecutorContext::new_mock(None).await;
833 let outcome = ctx.run_mock(&program, &MockConfig::default()).await;
834 ctx.close().await;
835 let outcome = outcome.unwrap();
836
837 assert!(
838 outcome.issues.iter().any(|issue| {
839 issue.severity == Severity::Warning
840 && issue.tag == Tag::Deprecated
841 && issue
842 .message
843 .contains("Calling `hide` with a consumed solid is deprecated")
844 && issue
845 .message
846 .contains("`target` was already consumed by a `subtract` operation")
847 }),
848 "expected hide consumed-solid deprecation warning, got: {:#?}",
849 outcome.issues
850 );
851 }
852
853 #[tokio::test(flavor = "multi_thread")]
854 async fn test_hide_helix() {
855 let ast = r#"helixPath = helix(
856 axis = Z,
857 radius = 5,
858 length = 10,
859 revolutions = 3,
860 angleStart = 360,
861 ccw = false,
862)
863
864hide(helixPath)
865"#;
866 parse_execute(ast).await.unwrap();
867 }
868
869 #[tokio::test(flavor = "multi_thread")]
870 async fn test_hide_sketch_block() {
871 let ast = r#"sketch001 = sketch(on = XY) {
872 circle001 = circle(start = [var 1.16mm, var 4.24mm], center = [var -1.81mm, var -0.5mm])
873}
874
875hide(sketch001)
876"#;
877 parse_execute(ast).await.unwrap();
878 }
879
880 #[tokio::test(flavor = "multi_thread")]
881 async fn test_hide_no_objects() {
882 let ast = r#"hidden = hide()"#;
883 let result = parse_execute(ast).await;
884 assert!(result.is_err());
885 assert_eq!(
886 result.unwrap_err().message(),
887 r#"This function expects an unlabeled first parameter, but you haven't passed it one."#.to_string()
888 );
889 }
890}