Skip to main content

kcl_lib/std/
patterns.rs

1//! Standard library patterns.
2
3use std::cmp::Ordering;
4
5use anyhow::Result;
6use kcmc::ModelingCmd;
7use kcmc::each_cmd as mcmd;
8use kcmc::length_unit::LengthUnit;
9use kcmc::ok_response::OkModelingCmdResponse;
10use kcmc::shared::Transform;
11use kcmc::websocket::OkWebSocketResponseData;
12use kittycad_modeling_cmds::shared::Angle;
13use kittycad_modeling_cmds::shared::OriginType;
14use kittycad_modeling_cmds::shared::Rotation;
15use kittycad_modeling_cmds::{self as kcmc};
16use serde::Serialize;
17use uuid::Uuid;
18
19use super::axis_or_reference::Axis3dOrPoint3d;
20use crate::ExecutorContext;
21use crate::NodePath;
22use crate::SourceRange;
23use crate::errors::KclError;
24use crate::errors::KclErrorDetails;
25use crate::execution::ArtifactId;
26use crate::execution::ControlFlowKind;
27use crate::execution::ExecState;
28use crate::execution::Geometries;
29use crate::execution::Geometry;
30use crate::execution::KclObjectFields;
31use crate::execution::KclValue;
32use crate::execution::ModelingCmdMeta;
33use crate::execution::Sketch;
34use crate::execution::Solid;
35use crate::execution::fn_call::Arg;
36use crate::execution::fn_call::Args;
37use crate::execution::kcl_value::FunctionSource;
38use crate::execution::types::NumericType;
39use crate::execution::types::PrimitiveType;
40use crate::execution::types::RuntimeType;
41use crate::std::args::TyF64;
42use crate::std::axis_or_reference::Axis2dOrPoint2d;
43use crate::std::shapes::POINT_ZERO_ZERO;
44use crate::std::utils::point_3d_to_mm;
45use crate::std::utils::point_to_mm;
46pub const POINT_ZERO_ZERO_ZERO: [TyF64; 3] = [
47    TyF64::new(0.0, crate::exec::NumericType::mm()),
48    TyF64::new(0.0, crate::exec::NumericType::mm()),
49    TyF64::new(0.0, crate::exec::NumericType::mm()),
50];
51
52const MUST_HAVE_ONE_INSTANCE: &str = "There must be at least 1 instance of your geometry";
53
54/// Repeat some 3D solid, changing each repetition slightly.
55pub async fn pattern_transform(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
56    let solids = args.get_unlabeled_kw_arg("solids", &RuntimeType::solids(), exec_state)?;
57    let instances: u32 = args.get_kw_arg("instances", &RuntimeType::count(), exec_state)?;
58    let transform: FunctionSource = args.get_kw_arg("transform", &RuntimeType::function(), exec_state)?;
59    let use_original = args.get_kw_arg_opt("useOriginal", &RuntimeType::bool(), exec_state)?;
60
61    let solids = inner_pattern_transform(solids, instances, transform, use_original, exec_state, &args).await?;
62    Ok(solids.into())
63}
64
65/// Repeat some 2D sketch, changing each repetition slightly.
66pub async fn pattern_transform_2d(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
67    let sketches = args.get_unlabeled_kw_arg("sketches", &RuntimeType::sketches(), exec_state)?;
68    let instances: u32 = args.get_kw_arg("instances", &RuntimeType::count(), exec_state)?;
69    let transform: FunctionSource = args.get_kw_arg("transform", &RuntimeType::function(), exec_state)?;
70    let use_original = args.get_kw_arg_opt("useOriginal", &RuntimeType::bool(), exec_state)?;
71
72    let sketches = inner_pattern_transform_2d(sketches, instances, transform, use_original, exec_state, &args).await?;
73    Ok(sketches.into())
74}
75
76async fn inner_pattern_transform(
77    solids: Vec<Solid>,
78    instances: u32,
79    transform: FunctionSource,
80    use_original: Option<bool>,
81    exec_state: &mut ExecState,
82    args: &Args,
83) -> Result<Vec<Solid>, KclError> {
84    // Build the vec of transforms, one for each repetition.
85    let mut transform_vec = Vec::with_capacity(usize::try_from(instances).unwrap());
86    if instances < 1 {
87        return Err(KclError::new_semantic(KclErrorDetails::new(
88            MUST_HAVE_ONE_INSTANCE.to_owned(),
89            vec![args.source_range],
90        )));
91    }
92    for i in 1..instances {
93        let t = make_transform::<Solid>(
94            i,
95            &transform,
96            args.source_range,
97            args.node_path.clone(),
98            exec_state,
99            &args.ctx,
100        )
101        .await?;
102        transform_vec.push(t);
103    }
104    execute_pattern_transform(
105        transform_vec,
106        solids,
107        use_original.unwrap_or_default(),
108        exec_state,
109        args,
110    )
111    .await
112}
113
114async fn inner_pattern_transform_2d(
115    sketches: Vec<Sketch>,
116    instances: u32,
117    transform: FunctionSource,
118    use_original: Option<bool>,
119    exec_state: &mut ExecState,
120    args: &Args,
121) -> Result<Vec<Sketch>, KclError> {
122    // Build the vec of transforms, one for each repetition.
123    let mut transform_vec = Vec::with_capacity(usize::try_from(instances).unwrap());
124    if instances < 1 {
125        return Err(KclError::new_semantic(KclErrorDetails::new(
126            MUST_HAVE_ONE_INSTANCE.to_owned(),
127            vec![args.source_range],
128        )));
129    }
130    for i in 1..instances {
131        let t = make_transform::<Sketch>(
132            i,
133            &transform,
134            args.source_range,
135            args.node_path.clone(),
136            exec_state,
137            &args.ctx,
138        )
139        .await?;
140        transform_vec.push(t);
141    }
142    execute_pattern_transform(
143        transform_vec,
144        sketches,
145        use_original.unwrap_or_default(),
146        exec_state,
147        args,
148    )
149    .await
150}
151
152async fn execute_pattern_transform<T: GeometryTrait>(
153    transforms: Vec<Vec<Transform>>,
154    geo_set: T::Set,
155    use_original: bool,
156    exec_state: &mut ExecState,
157    args: &Args,
158) -> Result<Vec<T>, KclError> {
159    // Flush the batch for our fillets/chamfers if there are any.
160    // If we do not flush these, then you won't be able to pattern something with fillets.
161    // Flush just the fillets/chamfers that apply to these solids.
162    T::flush_batch(args, exec_state, &geo_set).await?;
163    let starting: Vec<T> = geo_set.into();
164
165    let mut output = Vec::new();
166    for geo in starting {
167        let new = send_pattern_transform(transforms.clone(), &geo, use_original, exec_state, args).await?;
168        output.extend(new)
169    }
170    Ok(output)
171}
172
173async fn send_pattern_transform<T: GeometryTrait>(
174    // This should be passed via reference, see
175    // https://github.com/KittyCAD/modeling-app/issues/2821
176    transforms: Vec<Vec<Transform>>,
177    solid: &T,
178    use_original: bool,
179    exec_state: &mut ExecState,
180    args: &Args,
181) -> Result<Vec<T>, KclError> {
182    let extra_instances = transforms.len();
183
184    let resp = exec_state
185        .send_modeling_cmd(
186            ModelingCmdMeta::from_args(exec_state, args),
187            ModelingCmd::from(
188                mcmd::EntityLinearPatternTransform::builder()
189                    .entity_id(if use_original { solid.original_id() } else { solid.id() })
190                    .transform(Default::default())
191                    .transforms(transforms)
192                    .build(),
193            ),
194        )
195        .await?;
196
197    let mut mock_ids = Vec::new();
198    let entity_ids = if let OkWebSocketResponseData::Modeling {
199        modeling_response: OkModelingCmdResponse::EntityLinearPatternTransform(pattern_info),
200    } = &resp
201    {
202        &pattern_info.entity_face_edge_ids.iter().map(|x| x.object_id).collect()
203    } else if args.ctx.no_engine_commands().await {
204        mock_ids.reserve(extra_instances);
205        for _ in 0..extra_instances {
206            mock_ids.push(exec_state.next_uuid());
207        }
208        &mock_ids
209    } else {
210        return Err(KclError::new_engine(KclErrorDetails::new(
211            format!("EntityLinearPattern response was not as expected: {resp:?}"),
212            vec![args.source_range],
213        )));
214    };
215
216    let mut geometries = vec![solid.clone()];
217    for id in entity_ids.iter().copied() {
218        let mut new_solid = solid.clone();
219        new_solid.set_id(id);
220        new_solid.set_artifact_id(id);
221        geometries.push(new_solid);
222    }
223    Ok(geometries)
224}
225
226async fn make_transform<T: GeometryTrait>(
227    i: u32,
228    transform: &FunctionSource,
229    source_range: SourceRange,
230    node_path: Option<NodePath>,
231    exec_state: &mut ExecState,
232    ctxt: &ExecutorContext,
233) -> Result<Vec<Transform>, KclError> {
234    // Call the transform fn for this repetition.
235    let repetition_num = KclValue::Number {
236        value: i.into(),
237        ty: NumericType::count(),
238        meta: vec![source_range.into()],
239    };
240    let transform_fn_args = Args::new(
241        Default::default(),
242        vec![(None, Arg::new(repetition_num, source_range))],
243        source_range,
244        node_path,
245        exec_state,
246        ctxt.clone(),
247        Some("transform closure".to_owned()),
248    );
249    let transform_fn_return = transform
250        .call_kw(None, exec_state, ctxt, transform_fn_args, source_range)
251        .await?;
252
253    // Unpack the returned transform object.
254    let source_ranges = vec![source_range];
255    let transform_fn_return = transform_fn_return.ok_or_else(|| {
256        KclError::new_semantic(KclErrorDetails::new(
257            "Transform function must return a value".to_string(),
258            source_ranges.clone(),
259        ))
260    })?;
261
262    let transform_fn_return = match transform_fn_return.control {
263        ControlFlowKind::Continue => transform_fn_return.into_value(),
264        ControlFlowKind::Exit => {
265            let message = "Early return inside pattern transform function is currently not supported".to_owned();
266            debug_assert!(false, "{}", &message);
267            return Err(KclError::new_internal(KclErrorDetails::new(
268                message,
269                vec![source_range],
270            )));
271        }
272    };
273
274    let transforms = match transform_fn_return {
275        KclValue::Object { value, .. } => vec![value],
276        KclValue::Tuple { value, .. } | KclValue::HomArray { value, .. } => {
277            let transforms: Vec<_> = value
278                .into_iter()
279                .map(|val| {
280                    val.into_object().ok_or(KclError::new_semantic(KclErrorDetails::new(
281                        "Transform function must return a transform object".to_string(),
282                        source_ranges.clone(),
283                    )))
284                })
285                .collect::<Result<_, _>>()?;
286            transforms
287        }
288        _ => {
289            return Err(KclError::new_semantic(KclErrorDetails::new(
290                "Transform function must return a transform object".to_string(),
291                source_ranges,
292            )));
293        }
294    };
295
296    transforms
297        .into_iter()
298        .map(|obj| transform_from_obj_fields::<T>(obj, source_ranges.clone(), exec_state))
299        .collect()
300}
301
302fn transform_from_obj_fields<T: GeometryTrait>(
303    transform: KclObjectFields,
304    source_ranges: Vec<SourceRange>,
305    exec_state: &mut ExecState,
306) -> Result<Transform, KclError> {
307    // Apply defaults to the transform.
308    let replicate = match transform.get("replicate") {
309        Some(KclValue::Bool { value: true, .. }) => true,
310        Some(KclValue::Bool { value: false, .. }) => false,
311        Some(_) => {
312            return Err(KclError::new_semantic(KclErrorDetails::new(
313                "The 'replicate' key must be a bool".to_string(),
314                source_ranges,
315            )));
316        }
317        None => true,
318    };
319
320    let scale = match transform.get("scale") {
321        Some(x) => point_3d_to_mm(T::array_to_point3d(x, source_ranges.clone(), exec_state)?).into(),
322        None => kcmc::shared::Point3d { x: 1.0, y: 1.0, z: 1.0 },
323    };
324
325    for (dim, name) in [(scale.x, "x"), (scale.y, "y"), (scale.z, "z")] {
326        if dim == 0.0 {
327            return Err(KclError::new_semantic(KclErrorDetails::new(
328                format!("cannot set {name} = 0, scale factor must be nonzero"),
329                source_ranges,
330            )));
331        }
332    }
333    let translate = match transform.get("translate") {
334        Some(x) => {
335            let arr = point_3d_to_mm(T::array_to_point3d(x, source_ranges.clone(), exec_state)?);
336            kcmc::shared::Point3d::<LengthUnit> {
337                x: LengthUnit(arr[0]),
338                y: LengthUnit(arr[1]),
339                z: LengthUnit(arr[2]),
340            }
341        }
342        None => kcmc::shared::Point3d::<LengthUnit> {
343            x: LengthUnit(0.0),
344            y: LengthUnit(0.0),
345            z: LengthUnit(0.0),
346        },
347    };
348
349    let mut rotation = Rotation::default();
350    if let Some(rot) = transform.get("rotation") {
351        let KclValue::Object { value: rot, .. } = rot else {
352            return Err(KclError::new_semantic(KclErrorDetails::new(
353                "The 'rotation' key must be an object (with optional fields 'angle', 'axis' and 'origin')".to_owned(),
354                source_ranges,
355            )));
356        };
357        if let Some(axis) = rot.get("axis") {
358            rotation.axis = point_3d_to_mm(T::array_to_point3d(axis, source_ranges.clone(), exec_state)?).into();
359        }
360        if let Some(angle) = rot.get("angle") {
361            match angle {
362                KclValue::Number { value: number, .. } => {
363                    rotation.angle = Angle::from_degrees(*number);
364                }
365                _ => {
366                    return Err(KclError::new_semantic(KclErrorDetails::new(
367                        "The 'rotation.angle' key must be a number (of degrees)".to_owned(),
368                        source_ranges,
369                    )));
370                }
371            }
372        }
373        if let Some(origin) = rot.get("origin") {
374            rotation.origin = match origin {
375                KclValue::String { value: s, meta: _ } if s == "local" => OriginType::Local,
376                KclValue::String { value: s, meta: _ } if s == "global" => OriginType::Global,
377                other => {
378                    let origin = point_3d_to_mm(T::array_to_point3d(other, source_ranges, exec_state)?).into();
379                    OriginType::Custom { origin }
380                }
381            };
382        }
383    }
384
385    let transform = Transform::builder()
386        .replicate(replicate)
387        .scale(scale)
388        .translate(translate)
389        .rotation(rotation)
390        .build();
391    Ok(transform)
392}
393
394fn array_to_point3d(
395    val: &KclValue,
396    source_ranges: Vec<SourceRange>,
397    exec_state: &mut ExecState,
398) -> Result<[TyF64; 3], KclError> {
399    val.coerce(&RuntimeType::point3d(), true, exec_state)
400        .map_err(|e| {
401            KclError::new_semantic(KclErrorDetails::new(
402                format!(
403                    "Expected an array of 3 numbers (i.e., a 3D point), found {}",
404                    e.found
405                        .map(|t| t.human_friendly_type())
406                        .unwrap_or_else(|| val.human_friendly_type())
407                ),
408                source_ranges,
409            ))
410        })
411        .map(|val| val.as_point3d().unwrap())
412}
413
414fn array_to_point2d(
415    val: &KclValue,
416    source_ranges: Vec<SourceRange>,
417    exec_state: &mut ExecState,
418) -> Result<[TyF64; 2], KclError> {
419    val.coerce(&RuntimeType::point2d(), true, exec_state)
420        .map_err(|e| {
421            KclError::new_semantic(KclErrorDetails::new(
422                format!(
423                    "Expected an array of 2 numbers (i.e., a 2D point), found {}",
424                    e.found
425                        .map(|t| t.human_friendly_type())
426                        .unwrap_or_else(|| val.human_friendly_type())
427                ),
428                source_ranges,
429            ))
430        })
431        .map(|val| val.as_point2d().unwrap())
432}
433
434pub trait GeometryTrait: Clone {
435    type Set: Into<Vec<Self>> + Clone;
436    fn id(&self) -> Uuid;
437    fn original_id(&self) -> Uuid;
438    fn set_id(&mut self, id: Uuid);
439    fn set_artifact_id(&mut self, id: Uuid);
440    fn array_to_point3d(
441        val: &KclValue,
442        source_ranges: Vec<SourceRange>,
443        exec_state: &mut ExecState,
444    ) -> Result<[TyF64; 3], KclError>;
445    #[allow(async_fn_in_trait)]
446    async fn flush_batch(args: &Args, exec_state: &mut ExecState, set: &Self::Set) -> Result<(), KclError>;
447}
448
449impl GeometryTrait for Sketch {
450    type Set = Vec<Sketch>;
451    fn set_id(&mut self, id: Uuid) {
452        self.id = id;
453    }
454    fn set_artifact_id(&mut self, id: Uuid) {
455        self.artifact_id = ArtifactId::new(id);
456    }
457    fn id(&self) -> Uuid {
458        self.id
459    }
460    fn original_id(&self) -> Uuid {
461        self.original_id
462    }
463    fn array_to_point3d(
464        val: &KclValue,
465        source_ranges: Vec<SourceRange>,
466        exec_state: &mut ExecState,
467    ) -> Result<[TyF64; 3], KclError> {
468        let [x, y] = array_to_point2d(val, source_ranges, exec_state)?;
469        let ty = x.ty;
470        Ok([x, y, TyF64::new(0.0, ty)])
471    }
472
473    async fn flush_batch(_: &Args, _: &mut ExecState, _: &Self::Set) -> Result<(), KclError> {
474        Ok(())
475    }
476}
477
478impl GeometryTrait for Solid {
479    type Set = Vec<Solid>;
480    fn set_id(&mut self, id: Uuid) {
481        self.id = id;
482        // We need this for in extrude.rs when you sketch on face.
483        if let Some(sketch) = self.sketch_mut() {
484            sketch.id = id;
485        }
486    }
487
488    fn set_artifact_id(&mut self, id: Uuid) {
489        self.artifact_id = ArtifactId::new(id);
490    }
491
492    fn id(&self) -> Uuid {
493        self.id
494    }
495
496    fn original_id(&self) -> Uuid {
497        Solid::original_id(self)
498    }
499
500    fn array_to_point3d(
501        val: &KclValue,
502        source_ranges: Vec<SourceRange>,
503        exec_state: &mut ExecState,
504    ) -> Result<[TyF64; 3], KclError> {
505        array_to_point3d(val, source_ranges, exec_state)
506    }
507
508    async fn flush_batch(args: &Args, exec_state: &mut ExecState, solid_set: &Self::Set) -> Result<(), KclError> {
509        exec_state
510            .flush_batch_for_solids(ModelingCmdMeta::from_args(exec_state, args), solid_set)
511            .await
512    }
513}
514
515#[cfg(test)]
516mod tests {
517    use super::*;
518    use crate::execution::types::NumericType;
519    use crate::execution::types::PrimitiveType;
520
521    #[tokio::test(flavor = "multi_thread")]
522    async fn test_array_to_point3d() {
523        let ctx = ExecutorContext::new_mock(None).await;
524        let mut exec_state = ExecState::new(&ctx);
525        let input = KclValue::HomArray {
526            value: vec![
527                KclValue::Number {
528                    value: 1.1,
529                    meta: Default::default(),
530                    ty: NumericType::mm(),
531                },
532                KclValue::Number {
533                    value: 2.2,
534                    meta: Default::default(),
535                    ty: NumericType::mm(),
536                },
537                KclValue::Number {
538                    value: 3.3,
539                    meta: Default::default(),
540                    ty: NumericType::mm(),
541                },
542            ],
543            ty: RuntimeType::Primitive(PrimitiveType::Number(NumericType::mm())),
544        };
545        let expected = [
546            TyF64::new(1.1, NumericType::mm()),
547            TyF64::new(2.2, NumericType::mm()),
548            TyF64::new(3.3, NumericType::mm()),
549        ];
550        let actual = array_to_point3d(&input, Vec::new(), &mut exec_state);
551        assert_eq!(actual.unwrap(), expected);
552        ctx.close().await;
553    }
554
555    #[tokio::test(flavor = "multi_thread")]
556    async fn test_tuple_to_point3d() {
557        let ctx = ExecutorContext::new_mock(None).await;
558        let mut exec_state = ExecState::new(&ctx);
559        let input = KclValue::Tuple {
560            value: vec![
561                KclValue::Number {
562                    value: 1.1,
563                    meta: Default::default(),
564                    ty: NumericType::mm(),
565                },
566                KclValue::Number {
567                    value: 2.2,
568                    meta: Default::default(),
569                    ty: NumericType::mm(),
570                },
571                KclValue::Number {
572                    value: 3.3,
573                    meta: Default::default(),
574                    ty: NumericType::mm(),
575                },
576            ],
577            meta: Default::default(),
578        };
579        let expected = [
580            TyF64::new(1.1, NumericType::mm()),
581            TyF64::new(2.2, NumericType::mm()),
582            TyF64::new(3.3, NumericType::mm()),
583        ];
584        let actual = array_to_point3d(&input, Vec::new(), &mut exec_state);
585        assert_eq!(actual.unwrap(), expected);
586        ctx.close().await;
587    }
588}
589
590/// A linear pattern on a 2D sketch.
591pub async fn pattern_linear_2d(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
592    let sketches = args.get_unlabeled_kw_arg("sketches", &RuntimeType::sketches(), exec_state)?;
593    let instances: u32 = args.get_kw_arg("instances", &RuntimeType::count(), exec_state)?;
594    let distance: TyF64 = args.get_kw_arg("distance", &RuntimeType::length(), exec_state)?;
595    let axis: Axis2dOrPoint2d = args.get_kw_arg(
596        "axis",
597        &RuntimeType::Union(vec![
598            RuntimeType::Primitive(PrimitiveType::Axis2d),
599            RuntimeType::point2d(),
600        ]),
601        exec_state,
602    )?;
603    let use_original = args.get_kw_arg_opt("useOriginal", &RuntimeType::bool(), exec_state)?;
604
605    let axis = axis.to_point2d();
606    if axis[0].n == 0.0 && axis[1].n == 0.0 {
607        return Err(KclError::new_semantic(KclErrorDetails::new(
608            "The axis of the linear pattern cannot be the zero vector. Otherwise they will just duplicate in place."
609                .to_owned(),
610            vec![args.source_range],
611        )));
612    }
613
614    let sketches = inner_pattern_linear_2d(sketches, instances, distance, axis, use_original, exec_state, args).await?;
615    Ok(sketches.into())
616}
617
618async fn inner_pattern_linear_2d(
619    sketches: Vec<Sketch>,
620    instances: u32,
621    distance: TyF64,
622    axis: [TyF64; 2],
623    use_original: Option<bool>,
624    exec_state: &mut ExecState,
625    args: Args,
626) -> Result<Vec<Sketch>, KclError> {
627    let [x, y] = point_to_mm(axis);
628    let axis_len = f64::sqrt(x * x + y * y);
629    let normalized_axis = kcmc::shared::Point2d::from([x / axis_len, y / axis_len]);
630    let transforms: Vec<_> = (1..instances)
631        .map(|i| {
632            let d = distance.to_mm() * (i as f64);
633            let translate = (normalized_axis * d).with_z(0.0).map(LengthUnit);
634            vec![Transform::builder().translate(translate).build()]
635        })
636        .collect();
637    execute_pattern_transform(
638        transforms,
639        sketches,
640        use_original.unwrap_or_default(),
641        exec_state,
642        &args,
643    )
644    .await
645}
646
647/// A linear pattern on a 3D model.
648pub async fn pattern_linear_3d(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
649    let solids = args.get_unlabeled_kw_arg("solids", &RuntimeType::solids(), exec_state)?;
650    let instances: u32 = args.get_kw_arg("instances", &RuntimeType::count(), exec_state)?;
651    let distance: TyF64 = args.get_kw_arg("distance", &RuntimeType::length(), exec_state)?;
652    let axis: Axis3dOrPoint3d = args.get_kw_arg(
653        "axis",
654        &RuntimeType::Union(vec![
655            RuntimeType::Primitive(PrimitiveType::Axis3d),
656            RuntimeType::point3d(),
657        ]),
658        exec_state,
659    )?;
660    let use_original = args.get_kw_arg_opt("useOriginal", &RuntimeType::bool(), exec_state)?;
661
662    let axis = axis.to_point3d();
663    if axis[0].n == 0.0 && axis[1].n == 0.0 && axis[2].n == 0.0 {
664        return Err(KclError::new_semantic(KclErrorDetails::new(
665            "The axis of the linear pattern cannot be the zero vector. Otherwise they will just duplicate in place."
666                .to_owned(),
667            vec![args.source_range],
668        )));
669    }
670
671    let solids = inner_pattern_linear_3d(solids, instances, distance, axis, use_original, exec_state, args).await?;
672    Ok(solids.into())
673}
674
675async fn inner_pattern_linear_3d(
676    solids: Vec<Solid>,
677    instances: u32,
678    distance: TyF64,
679    axis: [TyF64; 3],
680    use_original: Option<bool>,
681    exec_state: &mut ExecState,
682    args: Args,
683) -> Result<Vec<Solid>, KclError> {
684    let [x, y, z] = point_3d_to_mm(axis);
685    let axis_len = f64::sqrt(x * x + y * y + z * z);
686    let normalized_axis = kcmc::shared::Point3d::from([x / axis_len, y / axis_len, z / axis_len]);
687    let transforms: Vec<_> = (1..instances)
688        .map(|i| {
689            let d = distance.to_mm() * (i as f64);
690            let translate = (normalized_axis * d).map(LengthUnit);
691            vec![Transform::builder().translate(translate).build()]
692        })
693        .collect();
694    execute_pattern_transform(transforms, solids, use_original.unwrap_or_default(), exec_state, &args).await
695}
696
697/// Data for a circular pattern on a 2D sketch.
698#[derive(Debug, Clone, Serialize, PartialEq)]
699#[serde(rename_all = "camelCase")]
700struct CircularPattern2dData {
701    /// The number of total instances. Must be greater than or equal to 1.
702    /// This includes the original entity. For example, if instances is 2,
703    /// there will be two copies -- the original, and one new copy.
704    /// If instances is 1, this has no effect.
705    pub instances: u32,
706    /// The center about which to make the pattern. This is a 2D vector.
707    pub center: [TyF64; 2],
708    /// The arc angle (in degrees) to place the repetitions. Must be greater than 0.
709    pub arc_degrees: Option<f64>,
710    /// Whether or not to rotate the duplicates as they are copied.
711    pub rotate_duplicates: Option<bool>,
712    /// If the target being patterned is itself a pattern, then, should you use the original solid,
713    /// or the pattern?
714    #[serde(default)]
715    pub use_original: Option<bool>,
716}
717
718/// Data for a circular pattern on a 3D model.
719#[derive(Debug, Clone, Serialize, PartialEq)]
720#[serde(rename_all = "camelCase")]
721struct CircularPattern3dData {
722    /// The number of total instances. Must be greater than or equal to 1.
723    /// This includes the original entity. For example, if instances is 2,
724    /// there will be two copies -- the original, and one new copy.
725    /// If instances is 1, this has no effect.
726    pub instances: u32,
727    /// The axis around which to make the pattern. This is a 3D vector.
728    // Only the direction should matter, not the magnitude so don't adjust units to avoid normalisation issues.
729    pub axis: [f64; 3],
730    /// The center about which to make the pattern. This is a 3D vector.
731    pub center: [TyF64; 3],
732    /// The arc angle (in degrees) to place the repetitions. Must be greater than 0.
733    pub arc_degrees: Option<f64>,
734    /// Whether or not to rotate the duplicates as they are copied.
735    pub rotate_duplicates: Option<bool>,
736    /// If the target being patterned is itself a pattern, then, should you use the original solid,
737    /// or the pattern?
738    #[serde(default)]
739    pub use_original: Option<bool>,
740}
741
742#[allow(clippy::large_enum_variant)]
743enum CircularPattern {
744    ThreeD(CircularPattern3dData),
745    TwoD(CircularPattern2dData),
746}
747
748enum RepetitionsNeeded {
749    /// Add this number of repetitions
750    More(u32),
751    /// No repetitions needed
752    None,
753    /// Invalid number of total instances.
754    Invalid,
755}
756
757impl From<u32> for RepetitionsNeeded {
758    fn from(n: u32) -> Self {
759        match n.cmp(&1) {
760            Ordering::Less => Self::Invalid,
761            Ordering::Equal => Self::None,
762            Ordering::Greater => Self::More(n - 1),
763        }
764    }
765}
766
767impl CircularPattern {
768    pub fn axis(&self) -> [f64; 3] {
769        match self {
770            CircularPattern::TwoD(_lp) => [0.0, 0.0, 0.0],
771            CircularPattern::ThreeD(lp) => [lp.axis[0], lp.axis[1], lp.axis[2]],
772        }
773    }
774
775    pub fn center_mm(&self) -> [f64; 3] {
776        match self {
777            CircularPattern::TwoD(lp) => [lp.center[0].to_mm(), lp.center[1].to_mm(), 0.0],
778            CircularPattern::ThreeD(lp) => [lp.center[0].to_mm(), lp.center[1].to_mm(), lp.center[2].to_mm()],
779        }
780    }
781
782    fn repetitions(&self) -> RepetitionsNeeded {
783        let n = match self {
784            CircularPattern::TwoD(lp) => lp.instances,
785            CircularPattern::ThreeD(lp) => lp.instances,
786        };
787        RepetitionsNeeded::from(n)
788    }
789
790    pub fn arc_degrees(&self) -> Option<f64> {
791        match self {
792            CircularPattern::TwoD(lp) => lp.arc_degrees,
793            CircularPattern::ThreeD(lp) => lp.arc_degrees,
794        }
795    }
796
797    pub fn rotate_duplicates(&self) -> Option<bool> {
798        match self {
799            CircularPattern::TwoD(lp) => lp.rotate_duplicates,
800            CircularPattern::ThreeD(lp) => lp.rotate_duplicates,
801        }
802    }
803
804    pub fn use_original(&self) -> bool {
805        match self {
806            CircularPattern::TwoD(lp) => lp.use_original.unwrap_or_default(),
807            CircularPattern::ThreeD(lp) => lp.use_original.unwrap_or_default(),
808        }
809    }
810}
811
812/// A circular pattern on a 2D sketch.
813pub async fn pattern_circular_2d(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
814    let sketches = args.get_unlabeled_kw_arg("sketches", &RuntimeType::sketches(), exec_state)?;
815    let instances: u32 = args.get_kw_arg("instances", &RuntimeType::count(), exec_state)?;
816    let center: Option<[TyF64; 2]> = args.get_kw_arg_opt("center", &RuntimeType::point2d(), exec_state)?;
817    let arc_degrees: Option<TyF64> = args.get_kw_arg_opt("arcDegrees", &RuntimeType::degrees(), exec_state)?;
818    let rotate_duplicates = args.get_kw_arg_opt("rotateDuplicates", &RuntimeType::bool(), exec_state)?;
819    let use_original = args.get_kw_arg_opt("useOriginal", &RuntimeType::bool(), exec_state)?;
820
821    let sketches = inner_pattern_circular_2d(
822        sketches,
823        instances,
824        center,
825        arc_degrees.map(|x| x.n),
826        rotate_duplicates,
827        use_original,
828        exec_state,
829        args,
830    )
831    .await?;
832    Ok(sketches.into())
833}
834
835#[allow(clippy::too_many_arguments)]
836async fn inner_pattern_circular_2d(
837    sketch_set: Vec<Sketch>,
838    instances: u32,
839    center: Option<[TyF64; 2]>,
840    arc_degrees: Option<f64>,
841    rotate_duplicates: Option<bool>,
842    use_original: Option<bool>,
843    exec_state: &mut ExecState,
844    args: Args,
845) -> Result<Vec<Sketch>, KclError> {
846    let starting_sketches = sketch_set;
847
848    if args.ctx.context_type == crate::execution::ContextType::Mock {
849        return Ok(starting_sketches);
850    }
851    let center = center.unwrap_or(POINT_ZERO_ZERO);
852    let data = CircularPattern2dData {
853        instances,
854        center,
855        arc_degrees,
856        rotate_duplicates,
857        use_original,
858    };
859
860    let mut sketches = Vec::new();
861    for sketch in starting_sketches.iter() {
862        let geometries = pattern_circular(
863            CircularPattern::TwoD(data.clone()),
864            Geometry::Sketch(sketch.clone()),
865            exec_state,
866            args.clone(),
867        )
868        .await?;
869
870        let Geometries::Sketches(new_sketches) = geometries else {
871            return Err(KclError::new_semantic(KclErrorDetails::new(
872                "Expected a vec of sketches".to_string(),
873                vec![args.source_range],
874            )));
875        };
876
877        sketches.extend(new_sketches);
878    }
879
880    Ok(sketches)
881}
882
883/// A circular pattern on a 3D model.
884pub async fn pattern_circular_3d(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
885    let solids = args.get_unlabeled_kw_arg("solids", &RuntimeType::solids(), exec_state)?;
886    // The number of total instances. Must be greater than or equal to 1.
887    // This includes the original entity. For example, if instances is 2,
888    // there will be two copies -- the original, and one new copy.
889    // If instances is 1, this has no effect.
890    let instances: u32 = args.get_kw_arg("instances", &RuntimeType::count(), exec_state)?;
891    // The axis around which to make the pattern. This is a 3D vector.
892    let axis: Axis3dOrPoint3d = args.get_kw_arg(
893        "axis",
894        &RuntimeType::Union(vec![
895            RuntimeType::Primitive(PrimitiveType::Axis3d),
896            RuntimeType::point3d(),
897        ]),
898        exec_state,
899    )?;
900    let axis = axis.to_point3d();
901
902    // The center about which to make the pattern. This is a 3D vector.
903    let center: Option<[TyF64; 3]> = args.get_kw_arg_opt("center", &RuntimeType::point3d(), exec_state)?;
904    // The arc angle (in degrees) to place the repetitions. Must be greater than 0.
905    let arc_degrees: Option<TyF64> = args.get_kw_arg_opt("arcDegrees", &RuntimeType::degrees(), exec_state)?;
906    // Whether or not to rotate the duplicates as they are copied.
907    let rotate_duplicates = args.get_kw_arg_opt("rotateDuplicates", &RuntimeType::bool(), exec_state)?;
908    // If the target being patterned is itself a pattern, then, should you use the original solid,
909    // or the pattern?
910    let use_original = args.get_kw_arg_opt("useOriginal", &RuntimeType::bool(), exec_state)?;
911
912    let solids = inner_pattern_circular_3d(
913        solids,
914        instances,
915        [axis[0].n, axis[1].n, axis[2].n],
916        center,
917        arc_degrees.map(|x| x.n),
918        rotate_duplicates,
919        use_original,
920        exec_state,
921        args,
922    )
923    .await?;
924    Ok(solids.into())
925}
926
927#[allow(clippy::too_many_arguments)]
928async fn inner_pattern_circular_3d(
929    solids: Vec<Solid>,
930    instances: u32,
931    axis: [f64; 3],
932    center: Option<[TyF64; 3]>,
933    arc_degrees: Option<f64>,
934    rotate_duplicates: Option<bool>,
935    use_original: Option<bool>,
936    exec_state: &mut ExecState,
937    args: Args,
938) -> Result<Vec<Solid>, KclError> {
939    // Flush the batch for our fillets/chamfers if there are any.
940    // If we do not flush these, then you won't be able to pattern something with fillets.
941    // Flush just the fillets/chamfers that apply to these solids.
942    exec_state
943        .flush_batch_for_solids(ModelingCmdMeta::from_args(exec_state, &args), &solids)
944        .await?;
945
946    let starting_solids = solids;
947
948    if args.ctx.context_type == crate::execution::ContextType::Mock {
949        return Ok(starting_solids);
950    }
951
952    let mut solids = Vec::new();
953    let center = center.unwrap_or(POINT_ZERO_ZERO_ZERO);
954    let data = CircularPattern3dData {
955        instances,
956        axis,
957        center,
958        arc_degrees,
959        rotate_duplicates,
960        use_original,
961    };
962    for solid in starting_solids.iter() {
963        let geometries = pattern_circular(
964            CircularPattern::ThreeD(data.clone()),
965            Geometry::Solid(solid.clone()),
966            exec_state,
967            args.clone(),
968        )
969        .await?;
970
971        let Geometries::Solids(new_solids) = geometries else {
972            return Err(KclError::new_semantic(KclErrorDetails::new(
973                "Expected a vec of solids".to_string(),
974                vec![args.source_range],
975            )));
976        };
977
978        solids.extend(new_solids);
979    }
980
981    Ok(solids)
982}
983
984async fn pattern_circular(
985    data: CircularPattern,
986    geometry: Geometry,
987    exec_state: &mut ExecState,
988    args: Args,
989) -> Result<Geometries, KclError> {
990    let num_repetitions = match data.repetitions() {
991        RepetitionsNeeded::More(n) => n,
992        RepetitionsNeeded::None => {
993            return Ok(Geometries::from(geometry));
994        }
995        RepetitionsNeeded::Invalid => {
996            return Err(KclError::new_semantic(KclErrorDetails::new(
997                MUST_HAVE_ONE_INSTANCE.to_owned(),
998                vec![args.source_range],
999            )));
1000        }
1001    };
1002
1003    let center = data.center_mm();
1004    let resp = exec_state
1005        .send_modeling_cmd(
1006            ModelingCmdMeta::from_args(exec_state, &args),
1007            ModelingCmd::from(
1008                mcmd::EntityCircularPattern::builder()
1009                    .axis(kcmc::shared::Point3d::from(data.axis()))
1010                    .entity_id(if data.use_original() {
1011                        geometry.original_id()
1012                    } else {
1013                        geometry.id()
1014                    })
1015                    .center(kcmc::shared::Point3d {
1016                        x: LengthUnit(center[0]),
1017                        y: LengthUnit(center[1]),
1018                        z: LengthUnit(center[2]),
1019                    })
1020                    .num_repetitions(num_repetitions)
1021                    .arc_degrees(data.arc_degrees().unwrap_or(360.0))
1022                    .rotate_duplicates(data.rotate_duplicates().unwrap_or(true))
1023                    .build(),
1024            ),
1025        )
1026        .await?;
1027
1028    // The common case is borrowing from the response.  Instead of cloning,
1029    // create a Vec to borrow from in mock mode.
1030    let mut mock_ids = Vec::new();
1031    let entity_ids = if let OkWebSocketResponseData::Modeling {
1032        modeling_response: OkModelingCmdResponse::EntityCircularPattern(pattern_info),
1033    } = &resp
1034    {
1035        &pattern_info.entity_face_edge_ids.iter().map(|e| e.object_id).collect()
1036    } else if args.ctx.no_engine_commands().await {
1037        mock_ids.reserve(num_repetitions as usize);
1038        for _ in 0..num_repetitions {
1039            mock_ids.push(exec_state.next_uuid());
1040        }
1041        &mock_ids
1042    } else {
1043        return Err(KclError::new_engine(KclErrorDetails::new(
1044            format!("EntityCircularPattern response was not as expected: {resp:?}"),
1045            vec![args.source_range],
1046        )));
1047    };
1048
1049    let geometries = match geometry {
1050        Geometry::Sketch(sketch) => {
1051            let mut geometries = vec![sketch.clone()];
1052            for id in entity_ids.iter().copied() {
1053                let mut new_sketch = sketch.clone();
1054                new_sketch.id = id;
1055                new_sketch.artifact_id = ArtifactId::new(id);
1056                geometries.push(new_sketch);
1057            }
1058            Geometries::Sketches(geometries)
1059        }
1060        Geometry::Solid(solid) => {
1061            let mut geometries = vec![solid.clone()];
1062            for id in entity_ids.iter().copied() {
1063                let mut new_solid = solid.clone();
1064                new_solid.id = id;
1065                new_solid.artifact_id = ArtifactId::new(id);
1066                geometries.push(new_solid);
1067            }
1068            Geometries::Solids(geometries)
1069        }
1070    };
1071
1072    Ok(geometries)
1073}