kcl_lib/std/shell.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
//! Standard library shells.
use anyhow::Result;
use derive_docs::stdlib;
use kcmc::{each_cmd as mcmd, length_unit::LengthUnit, ModelingCmd};
use kittycad_modeling_cmds as kcmc;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
errors::{KclError, KclErrorDetails},
executor::{ExecState, KclValue, Solid, SolidSet},
std::{sketch::FaceTag, Args},
};
/// Data for shells.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct ShellData {
/// The thickness of the shell.
pub thickness: f64,
/// The faces you want removed.
pub faces: Vec<FaceTag>,
}
/// Create a shell.
pub async fn shell(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (data, solid_set): (ShellData, SolidSet) = args.get_data_and_solid_set()?;
let result = inner_shell(data, solid_set, exec_state, args).await?;
Ok(result.into())
}
/// Remove volume from a 3-dimensional shape such that a wall of the
/// provided thickness remains, taking volume starting at the provided
/// face, leaving it open in that direction.
///
/// ```no_run
/// // Remove the end face for the extrusion.
/// const firstSketch = startSketchOn('XY')
/// |> startProfileAt([-12, 12], %)
/// |> line([24, 0], %)
/// |> line([0, -24], %)
/// |> line([-24, 0], %)
/// |> close(%)
/// |> extrude(6, %)
///
/// // Remove the end face for the extrusion.
/// shell({
/// faces: ['end'],
/// thickness: 0.25,
/// }, firstSketch)
/// ```
///
/// ```no_run
/// // Remove the start face for the extrusion.
/// const firstSketch = startSketchOn('-XZ')
/// |> startProfileAt([-12, 12], %)
/// |> line([24, 0], %)
/// |> line([0, -24], %)
/// |> line([-24, 0], %)
/// |> close(%)
/// |> extrude(6, %)
///
/// // Remove the start face for the extrusion.
/// shell({
/// faces: ['start'],
/// thickness: 0.25,
/// }, firstSketch)
/// ```
///
/// ```no_run
/// // Remove a tagged face and the end face for the extrusion.
/// const firstSketch = startSketchOn('XY')
/// |> startProfileAt([-12, 12], %)
/// |> line([24, 0], %)
/// |> line([0, -24], %)
/// |> line([-24, 0], %, $myTag)
/// |> close(%)
/// |> extrude(6, %)
///
/// // Remove a tagged face for the extrusion.
/// shell({
/// faces: [myTag],
/// thickness: 0.25,
/// }, firstSketch)
/// ```
///
/// ```no_run
/// // Remove multiple faces at once.
/// const firstSketch = startSketchOn('XY')
/// |> startProfileAt([-12, 12], %)
/// |> line([24, 0], %)
/// |> line([0, -24], %)
/// |> line([-24, 0], %, $myTag)
/// |> close(%)
/// |> extrude(6, %)
///
/// // Remove a tagged face and the end face for the extrusion.
/// shell({
/// faces: [myTag, 'end'],
/// thickness: 0.25,
/// }, firstSketch)
/// ```
///
/// ```no_run
/// // Shell a sketch on face.
/// let size = 100
/// const case = startSketchOn('-XZ')
/// |> startProfileAt([-size, -size], %)
/// |> line([2 * size, 0], %)
/// |> line([0, 2 * size], %)
/// |> tangentialArcTo([-size, size], %)
/// |> close(%)
/// |> extrude(65, %)
///
/// const thing1 = startSketchOn(case, 'end')
/// |> circle({ center: [-size / 2, -size / 2], radius: 25 }, %)
/// |> extrude(50, %)
///
/// const thing2 = startSketchOn(case, 'end')
/// |> circle({ center: [size / 2, -size / 2], radius: 25 }, %)
/// |> extrude(50, %)
///
/// // We put "case" in the shell function to shell the entire object.
/// shell({ faces: ['start'], thickness: 5 }, case)
/// ```
///
/// ```no_run
/// // Shell a sketch on face object on the end face.
/// let size = 100
/// const case = startSketchOn('XY')
/// |> startProfileAt([-size, -size], %)
/// |> line([2 * size, 0], %)
/// |> line([0, 2 * size], %)
/// |> tangentialArcTo([-size, size], %)
/// |> close(%)
/// |> extrude(65, %)
///
/// const thing1 = startSketchOn(case, 'end')
/// |> circle({ center: [-size / 2, -size / 2], radius: 25 }, %)
/// |> extrude(50, %)
///
/// const thing2 = startSketchOn(case, 'end')
/// |> circle({ center: [size / 2, -size / 2], radius: 25 }, %)
/// |> extrude(50, %)
///
/// // We put "thing1" in the shell function to shell the end face of the object.
/// shell({ faces: ['end'], thickness: 5 }, thing1)
/// ```
///
/// ```no_run
/// // Shell sketched on face objects on the end face, include all sketches to shell
/// // the entire object.
///
/// let size = 100
/// const case = startSketchOn('XY')
/// |> startProfileAt([-size, -size], %)
/// |> line([2 * size, 0], %)
/// |> line([0, 2 * size], %)
/// |> tangentialArcTo([-size, size], %)
/// |> close(%)
/// |> extrude(65, %)
///
/// const thing1 = startSketchOn(case, 'end')
/// |> circle({ center: [-size / 2, -size / 2], radius: 25 }, %)
/// |> extrude(50, %)
///
/// const thing2 = startSketchOn(case, 'end')
/// |> circle({ center: [size / 2, -size / 2], radius: 25 }, %)
/// |> extrude(50, %)
///
/// // We put "thing1" and "thing2" in the shell function to shell the end face of the object.
/// shell({ faces: ['end'], thickness: 5 }, [thing1, thing2])
/// ```
#[stdlib {
name = "shell",
}]
async fn inner_shell(
data: ShellData,
solid_set: SolidSet,
exec_state: &mut ExecState,
args: Args,
) -> Result<SolidSet, KclError> {
if data.faces.is_empty() {
return Err(KclError::Type(KclErrorDetails {
message: "Expected at least one face".to_string(),
source_ranges: vec![args.source_range],
}));
}
let solids: Vec<Box<Solid>> = solid_set.clone().into();
if solids.is_empty() {
return Err(KclError::Type(KclErrorDetails {
message: "Expected at least one solid".to_string(),
source_ranges: vec![args.source_range],
}));
}
let mut face_ids = Vec::new();
for solid in &solids {
// Flush the batch for our fillets/chamfers if there are any.
// If we do not do these for sketch on face, things will fail with face does not exist.
args.flush_batch_for_solid_set(exec_state, solid.clone().into()).await?;
for tag in &data.faces {
let extrude_plane_id = tag.get_face_id(solid, exec_state, &args, false).await?;
face_ids.push(extrude_plane_id);
}
}
if face_ids.is_empty() {
return Err(KclError::Type(KclErrorDetails {
message: "Expected at least one valid face".to_string(),
source_ranges: vec![args.source_range],
}));
}
// Make sure all the solids have the same id, as we are going to shell them all at
// once.
if !solids.iter().all(|eg| eg.id == solids[0].id) {
return Err(KclError::Type(KclErrorDetails {
message: "All solids stem from the same root object, like multiple sketch on face extrusions, etc."
.to_string(),
source_ranges: vec![args.source_range],
}));
}
args.batch_modeling_cmd(
exec_state.id_generator.next_uuid(),
ModelingCmd::from(mcmd::Solid3dShellFace {
hollow: false,
face_ids,
object_id: solids[0].id,
shell_thickness: LengthUnit(data.thickness),
}),
)
.await?;
Ok(solid_set)
}
/// Make the inside of a 3D object hollow.
pub async fn hollow(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (thickness, solid): (f64, Box<Solid>) = args.get_data_and_solid()?;
let solid = inner_hollow(thickness, solid, exec_state, args).await?;
Ok(KclValue::Solid(solid))
}
/// Make the inside of a 3D object hollow.
///
/// Remove volume from a 3-dimensional shape such that a wall of the
/// provided thickness remains around the exterior of the shape.
///
/// ```no_run
/// // Hollow a basic sketch.
/// const firstSketch = startSketchOn('XY')
/// |> startProfileAt([-12, 12], %)
/// |> line([24, 0], %)
/// |> line([0, -24], %)
/// |> line([-24, 0], %)
/// |> close(%)
/// |> extrude(6, %)
/// |> hollow (0.25, %)
/// ```
///
/// ```no_run
/// // Hollow a basic sketch.
/// const firstSketch = startSketchOn('-XZ')
/// |> startProfileAt([-12, 12], %)
/// |> line([24, 0], %)
/// |> line([0, -24], %)
/// |> line([-24, 0], %)
/// |> close(%)
/// |> extrude(6, %)
/// |> hollow (0.5, %)
/// ```
///
/// ```no_run
/// // Hollow a sketch on face object.
/// let size = 100
/// const case = startSketchOn('-XZ')
/// |> startProfileAt([-size, -size], %)
/// |> line([2 * size, 0], %)
/// |> line([0, 2 * size], %)
/// |> tangentialArcTo([-size, size], %)
/// |> close(%)
/// |> extrude(65, %)
///
/// const thing1 = startSketchOn(case, 'end')
/// |> circle({ center: [-size / 2, -size / 2], radius: 25 }, %)
/// |> extrude(50, %)
///
/// const thing2 = startSketchOn(case, 'end')
/// |> circle({ center: [size / 2, -size / 2], radius: 25 }, %)
/// |> extrude(50, %)
///
/// hollow(0.5, case)
/// ```
#[stdlib {
name = "hollow",
}]
async fn inner_hollow(
thickness: f64,
solid: Box<Solid>,
exec_state: &mut ExecState,
args: Args,
) -> Result<Box<Solid>, KclError> {
// Flush the batch for our fillets/chamfers if there are any.
// If we do not do these for sketch on face, things will fail with face does not exist.
args.flush_batch_for_solid_set(exec_state, solid.clone().into()).await?;
args.batch_modeling_cmd(
exec_state.id_generator.next_uuid(),
ModelingCmd::from(mcmd::Solid3dShellFace {
hollow: true,
face_ids: Vec::new(), // This is empty because we want to hollow the entire object.
object_id: solid.id,
shell_thickness: LengthUnit(thickness),
}),
)
.await?;
Ok(solid)
}