1use anyhow::Result;
4use kcmc::{ModelingCmd, each_cmd as mcmd, length_unit::LengthUnit};
5use kittycad_modeling_cmds as kcmc;
6
7use super::args::TyF64;
8use crate::{
9 errors::{KclError, KclErrorDetails},
10 execution::{
11 ExecState, KclValue, Solid,
12 types::{ArrayLen, RuntimeType},
13 },
14 std::{Args, sketch::FaceTag},
15};
16
17pub async fn shell(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
19 let solids = args.get_unlabeled_kw_arg("solids", &RuntimeType::solids(), exec_state)?;
20 let thickness: TyF64 = args.get_kw_arg("thickness", &RuntimeType::length(), exec_state)?;
21 let faces = args.get_kw_arg(
22 "faces",
23 &RuntimeType::Array(Box::new(RuntimeType::tagged_face()), ArrayLen::Minimum(1)),
24 exec_state,
25 )?;
26
27 let result = inner_shell(solids, thickness, faces, exec_state, args).await?;
28 Ok(result.into())
29}
30
31async fn inner_shell(
32 solids: Vec<Solid>,
33 thickness: TyF64,
34 faces: Vec<FaceTag>,
35 exec_state: &mut ExecState,
36 args: Args,
37) -> Result<Vec<Solid>, KclError> {
38 if faces.is_empty() {
39 return Err(KclError::new_type(KclErrorDetails::new(
40 "You must shell at least one face".to_owned(),
41 vec![args.source_range],
42 )));
43 }
44
45 if solids.is_empty() {
46 return Err(KclError::new_type(KclErrorDetails::new(
47 "You must shell at least one solid".to_owned(),
48 vec![args.source_range],
49 )));
50 }
51
52 let mut face_ids = Vec::new();
53 for solid in &solids {
54 exec_state
57 .flush_batch_for_solids((&args).into(), &[solid.clone()])
58 .await?;
59
60 for tag in &faces {
61 let extrude_plane_id = tag.get_face_id(solid, exec_state, &args, false).await?;
62
63 face_ids.push(extrude_plane_id);
64 }
65 }
66
67 if face_ids.is_empty() {
68 return Err(KclError::new_type(KclErrorDetails::new(
69 "Expected at least one valid face".to_owned(),
70 vec![args.source_range],
71 )));
72 }
73
74 if !solids.iter().all(|eg| eg.id == solids[0].id) {
77 return Err(KclError::new_type(KclErrorDetails::new(
78 "All solids stem from the same root object, like multiple sketch on face extrusions, etc.".to_owned(),
79 vec![args.source_range],
80 )));
81 }
82
83 exec_state
84 .batch_modeling_cmd(
85 (&args).into(),
86 ModelingCmd::from(mcmd::Solid3dShellFace {
87 hollow: false,
88 face_ids,
89 object_id: solids[0].id,
90 shell_thickness: LengthUnit(thickness.to_mm()),
91 }),
92 )
93 .await?;
94
95 Ok(solids)
96}
97
98pub async fn hollow(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
100 let solid = args.get_unlabeled_kw_arg("solid", &RuntimeType::solid(), exec_state)?;
101 let thickness: TyF64 = args.get_kw_arg("thickness", &RuntimeType::length(), exec_state)?;
102
103 let value = inner_hollow(solid, thickness, exec_state, args).await?;
104 Ok(KclValue::Solid { value })
105}
106
107async fn inner_hollow(
108 solid: Box<Solid>,
109 thickness: TyF64,
110 exec_state: &mut ExecState,
111 args: Args,
112) -> Result<Box<Solid>, KclError> {
113 exec_state
116 .flush_batch_for_solids((&args).into(), &[(*solid).clone()])
117 .await?;
118
119 exec_state
120 .batch_modeling_cmd(
121 (&args).into(),
122 ModelingCmd::from(mcmd::Solid3dShellFace {
123 hollow: true,
124 face_ids: Vec::new(), object_id: solid.id,
126 shell_thickness: LengthUnit(thickness.to_mm()),
127 }),
128 )
129 .await?;
130
131 Ok(solid)
132}