kcl_lib/std/revolve.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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
//! Standard library revolution surfaces.
use anyhow::Result;
use derive_docs::stdlib;
use kcmc::{each_cmd as mcmd, length_unit::LengthUnit, shared::Angle, ModelingCmd};
use kittycad_modeling_cmds::{self as kcmc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
errors::{KclError, KclErrorDetails},
executor::{ExecState, KclValue, Sketch, Solid},
std::{
extrude::do_post_extrude,
fillet::{default_tolerance, EdgeReference},
Args,
},
};
/// Data for revolution surfaces.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
pub struct RevolveData {
/// Angle to revolve (in degrees). Default is 360.
#[serde(default)]
#[schemars(range(min = -360.0, max = 360.0))]
pub angle: Option<f64>,
/// Axis of revolution.
pub axis: AxisOrEdgeReference,
/// Tolerance for the revolve operation.
#[serde(default)]
pub tolerance: Option<f64>,
}
/// Axis or tagged edge.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(untagged)]
pub enum AxisOrEdgeReference {
/// Axis and origin.
Axis(AxisAndOrigin),
/// Tagged edge.
Edge(EdgeReference),
}
/// Axis and origin.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub enum AxisAndOrigin {
/// X-axis.
#[serde(rename = "X", alias = "x")]
X,
/// Y-axis.
#[serde(rename = "Y", alias = "y")]
Y,
/// Flip the X-axis.
#[serde(rename = "-X", alias = "-x")]
NegX,
/// Flip the Y-axis.
#[serde(rename = "-Y", alias = "-y")]
NegY,
Custom {
/// The axis.
axis: [f64; 2],
/// The origin.
origin: [f64; 2],
},
}
impl AxisAndOrigin {
/// Get the axis and origin.
pub fn axis_and_origin(&self) -> Result<(kcmc::shared::Point3d<f64>, kcmc::shared::Point3d<LengthUnit>), KclError> {
let (axis, origin) = match self {
AxisAndOrigin::X => ([1.0, 0.0, 0.0], [0.0, 0.0, 0.0]),
AxisAndOrigin::Y => ([0.0, 1.0, 0.0], [0.0, 0.0, 0.0]),
AxisAndOrigin::NegX => ([-1.0, 0.0, 0.0], [0.0, 0.0, 0.0]),
AxisAndOrigin::NegY => ([0.0, -1.0, 0.0], [0.0, 0.0, 0.0]),
AxisAndOrigin::Custom { axis, origin } => ([axis[0], axis[1], 0.0], [origin[0], origin[1], 0.0]),
};
Ok((
kcmc::shared::Point3d {
x: axis[0],
y: axis[1],
z: axis[2],
},
kcmc::shared::Point3d {
x: LengthUnit(origin[0]),
y: LengthUnit(origin[1]),
z: LengthUnit(origin[2]),
},
))
}
}
/// Revolve a sketch around an axis.
pub async fn revolve(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (data, sketch): (RevolveData, Sketch) = args.get_data_and_sketch()?;
let solid = inner_revolve(data, sketch, exec_state, args).await?;
Ok(KclValue::Solid(solid))
}
/// Rotate a sketch around some provided axis, creating a solid from its extent.
///
/// This, like extrude, is able to create a 3-dimensional solid from a
/// 2-dimensional sketch. However, unlike extrude, this creates a solid
/// by using the extent of the sketch as its revolved around an axis rather
/// than using the extent of the sketch linearly translated through a third
/// dimension.
///
/// Revolve occurs around a local sketch axis rather than a global axis.
///
/// ```no_run
/// const part001 = startSketchOn('XY')
/// |> startProfileAt([4, 12], %)
/// |> line([2, 0], %)
/// |> line([0, -6], %)
/// |> line([4, -6], %)
/// |> line([0, -6], %)
/// |> line([-3.75, -4.5], %)
/// |> line([0, -5.5], %)
/// |> line([-2, 0], %)
/// |> close(%)
/// |> revolve({axis: 'y'}, %) // default angle is 360
/// ```
///
/// ```no_run
/// // A donut shape.
/// const sketch001 = startSketchOn('XY')
/// |> circle({ center: [15, 0], radius: 5 }, %)
/// |> revolve({
/// angle: 360,
/// axis: 'y'
/// }, %)
/// ```
///
/// ```no_run
/// const part001 = startSketchOn('XY')
/// |> startProfileAt([4, 12], %)
/// |> line([2, 0], %)
/// |> line([0, -6], %)
/// |> line([4, -6], %)
/// |> line([0, -6], %)
/// |> line([-3.75, -4.5], %)
/// |> line([0, -5.5], %)
/// |> line([-2, 0], %)
/// |> close(%)
/// |> revolve({axis: 'y', angle: 180}, %)
/// ```
///
/// ```no_run
/// const part001 = startSketchOn('XY')
/// |> startProfileAt([4, 12], %)
/// |> line([2, 0], %)
/// |> line([0, -6], %)
/// |> line([4, -6], %)
/// |> line([0, -6], %)
/// |> line([-3.75, -4.5], %)
/// |> line([0, -5.5], %)
/// |> line([-2, 0], %)
/// |> close(%)
/// |> revolve({axis: 'y', angle: 180}, %)
/// const part002 = startSketchOn(part001, 'end')
/// |> startProfileAt([4.5, -5], %)
/// |> line([0, 5], %)
/// |> line([5, 0], %)
/// |> line([0, -5], %)
/// |> close(%)
/// |> extrude(5, %)
/// ```
///
/// ```no_run
/// const box = startSketchOn('XY')
/// |> startProfileAt([0, 0], %)
/// |> line([0, 20], %)
/// |> line([20, 0], %)
/// |> line([0, -20], %)
/// |> close(%)
/// |> extrude(20, %)
///
/// const sketch001 = startSketchOn(box, "END")
/// |> circle({ center: [10,10], radius: 4 }, %)
/// |> revolve({
/// angle: -90,
/// axis: 'y'
/// }, %)
/// ```
///
/// ```no_run
/// const box = startSketchOn('XY')
/// |> startProfileAt([0, 0], %)
/// |> line([0, 20], %)
/// |> line([20, 0], %)
/// |> line([0, -20], %, $revolveAxis)
/// |> close(%)
/// |> extrude(20, %)
///
/// const sketch001 = startSketchOn(box, "END")
/// |> circle({ center: [10,10], radius: 4 }, %)
/// |> revolve({
/// angle: 90,
/// axis: getOppositeEdge(revolveAxis)
/// }, %)
/// ```
///
/// ```no_run
/// const box = startSketchOn('XY')
/// |> startProfileAt([0, 0], %)
/// |> line([0, 20], %)
/// |> line([20, 0], %)
/// |> line([0, -20], %, $revolveAxis)
/// |> close(%)
/// |> extrude(20, %)
///
/// const sketch001 = startSketchOn(box, "END")
/// |> circle({ center: [10,10], radius: 4 }, %)
/// |> revolve({
/// angle: 90,
/// axis: getOppositeEdge(revolveAxis),
/// tolerance: 0.0001
/// }, %)
/// ```
///
/// ```no_run
/// const sketch001 = startSketchOn('XY')
/// |> startProfileAt([10, 0], %)
/// |> line([5, -5], %)
/// |> line([5, 5], %)
/// |> lineTo([profileStartX(%), profileStartY(%)], %)
/// |> close(%)
///
/// const part001 = revolve({
/// axis: {
/// custom: {
/// axis: [0.0, 1.0],
/// origin: [0.0, 0.0]
/// }
/// }
/// }, sketch001)
/// ```
#[stdlib {
name = "revolve",
}]
async fn inner_revolve(
data: RevolveData,
sketch: Sketch,
exec_state: &mut ExecState,
args: Args,
) -> Result<Box<Solid>, KclError> {
if let Some(angle) = data.angle {
// Return an error if the angle is zero.
// We don't use validate() here because we want to return a specific error message that is
// nice and we use the other data in the docs, so we still need use the derive above for the json schema.
if !(-360.0..=360.0).contains(&angle) || angle == 0.0 {
return Err(KclError::Semantic(KclErrorDetails {
message: format!("Expected angle to be between -360 and 360 and not 0, found `{}`", angle),
source_ranges: vec![args.source_range],
}));
}
}
let angle = Angle::from_degrees(data.angle.unwrap_or(360.0));
let id = uuid::Uuid::new_v4();
match data.axis {
AxisOrEdgeReference::Axis(axis) => {
let (axis, origin) = axis.axis_and_origin()?;
args.batch_modeling_cmd(
id,
ModelingCmd::from(mcmd::Revolve {
angle,
target: sketch.id.into(),
axis,
origin,
tolerance: LengthUnit(data.tolerance.unwrap_or(default_tolerance(&args.ctx.settings.units))),
axis_is_2d: true,
}),
)
.await?;
}
AxisOrEdgeReference::Edge(edge) => {
let edge_id = edge.get_engine_id(exec_state, &args)?;
args.batch_modeling_cmd(
id,
ModelingCmd::from(mcmd::RevolveAboutEdge {
angle,
target: sketch.id.into(),
edge_id,
tolerance: LengthUnit(data.tolerance.unwrap_or(default_tolerance(&args.ctx.settings.units))),
}),
)
.await?;
}
}
do_post_extrude(sketch, 0.0, args).await
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use crate::std::revolve::{AxisAndOrigin, AxisOrEdgeReference};
#[test]
fn test_deserialize_revolve_axis() {
let data = AxisOrEdgeReference::Axis(AxisAndOrigin::X);
let mut str_json = serde_json::to_string(&data).unwrap();
assert_eq!(str_json, "\"X\"");
str_json = "\"Y\"".to_string();
let data: AxisOrEdgeReference = serde_json::from_str(&str_json).unwrap();
assert_eq!(data, AxisOrEdgeReference::Axis(AxisAndOrigin::Y));
str_json = "\"-Y\"".to_string();
let data: AxisOrEdgeReference = serde_json::from_str(&str_json).unwrap();
assert_eq!(data, AxisOrEdgeReference::Axis(AxisAndOrigin::NegY));
str_json = "\"-x\"".to_string();
let data: AxisOrEdgeReference = serde_json::from_str(&str_json).unwrap();
assert_eq!(data, AxisOrEdgeReference::Axis(AxisAndOrigin::NegX));
let data = AxisOrEdgeReference::Axis(AxisAndOrigin::Custom {
axis: [0.0, -1.0],
origin: [1.0, 0.0],
});
str_json = serde_json::to_string(&data).unwrap();
assert_eq!(str_json, r#"{"custom":{"axis":[0.0,-1.0],"origin":[1.0,0.0]}}"#);
str_json = r#"{"custom": {"axis": [0,-1], "origin": [1,2.0]}}"#.to_string();
let data: AxisOrEdgeReference = serde_json::from_str(&str_json).unwrap();
assert_eq!(
data,
AxisOrEdgeReference::Axis(AxisAndOrigin::Custom {
axis: [0.0, -1.0],
origin: [1.0, 2.0]
})
);
}
}