kcl_lib/std/
revolve.rs

1//! Standard library revolution surfaces.
2
3use anyhow::Result;
4use kcmc::{
5    each_cmd as mcmd,
6    length_unit::LengthUnit,
7    shared::{Angle, Opposite},
8    ModelingCmd,
9};
10use kittycad_modeling_cmds::{self as kcmc, shared::Point3d};
11
12use super::{args::TyF64, DEFAULT_TOLERANCE};
13use crate::{
14    errors::{KclError, KclErrorDetails},
15    execution::{
16        types::{NumericType, PrimitiveType, RuntimeType},
17        ExecState, KclValue, Sketch, Solid,
18    },
19    parsing::ast::types::TagNode,
20    std::{axis_or_reference::Axis2dOrEdgeReference, extrude::do_post_extrude, Args},
21};
22
23/// Revolve a sketch or set of sketches around an axis.
24pub async fn revolve(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
25    let sketches = args.get_unlabeled_kw_arg_typed("sketches", &RuntimeType::sketches(), exec_state)?;
26    let axis = args.get_kw_arg_typed(
27        "axis",
28        &RuntimeType::Union(vec![
29            RuntimeType::Primitive(PrimitiveType::Edge),
30            RuntimeType::Primitive(PrimitiveType::Axis2d),
31        ]),
32        exec_state,
33    )?;
34    let angle: Option<TyF64> = args.get_kw_arg_opt_typed("angle", &RuntimeType::degrees(), exec_state)?;
35    let tolerance: Option<TyF64> = args.get_kw_arg_opt_typed("tolerance", &RuntimeType::length(), exec_state)?;
36    let tag_start = args.get_kw_arg_opt("tagStart")?;
37    let tag_end = args.get_kw_arg_opt("tagEnd")?;
38    let symmetric = args.get_kw_arg_opt("symmetric")?;
39    let bidirectional_angle: Option<TyF64> =
40        args.get_kw_arg_opt_typed("bidirectionalAngle", &RuntimeType::angle(), exec_state)?;
41
42    let value = inner_revolve(
43        sketches,
44        axis,
45        angle.map(|t| t.n),
46        tolerance,
47        tag_start,
48        tag_end,
49        symmetric,
50        bidirectional_angle.map(|t| t.n),
51        exec_state,
52        args,
53    )
54    .await?;
55    Ok(value.into())
56}
57
58#[allow(clippy::too_many_arguments)]
59async fn inner_revolve(
60    sketches: Vec<Sketch>,
61    axis: Axis2dOrEdgeReference,
62    angle: Option<f64>,
63    tolerance: Option<TyF64>,
64    tag_start: Option<TagNode>,
65    tag_end: Option<TagNode>,
66    symmetric: Option<bool>,
67    bidirectional_angle: Option<f64>,
68    exec_state: &mut ExecState,
69    args: Args,
70) -> Result<Vec<Solid>, KclError> {
71    if let Some(angle) = angle {
72        // Return an error if the angle is zero.
73        // We don't use validate() here because we want to return a specific error message that is
74        // nice and we use the other data in the docs, so we still need use the derive above for the json schema.
75        if !(-360.0..=360.0).contains(&angle) || angle == 0.0 {
76            return Err(KclError::Semantic(KclErrorDetails {
77                message: format!("Expected angle to be between -360 and 360 and not 0, found `{}`", angle),
78                source_ranges: vec![args.source_range],
79            }));
80        }
81    }
82
83    if let Some(bidirectional_angle) = bidirectional_angle {
84        // Return an error if the angle is zero.
85        // We don't use validate() here because we want to return a specific error message that is
86        // nice and we use the other data in the docs, so we still need use the derive above for the json schema.
87        if !(-360.0..=360.0).contains(&bidirectional_angle) || bidirectional_angle == 0.0 {
88            return Err(KclError::Semantic(KclErrorDetails {
89                message: format!(
90                    "Expected bidirectional angle to be between -360 and 360 and not 0, found `{}`",
91                    bidirectional_angle
92                ),
93                source_ranges: vec![args.source_range],
94            }));
95        }
96
97        if let Some(angle) = angle {
98            let ang = angle.signum() * bidirectional_angle + angle;
99            if !(-360.0..=360.0).contains(&ang) {
100                return Err(KclError::Semantic(KclErrorDetails {
101                    message: format!(
102                        "Combined angle and bidirectional must be between -360 and 360, found '{}'",
103                        ang
104                    ),
105                    source_ranges: vec![args.source_range],
106                }));
107            }
108        }
109    }
110
111    if symmetric.unwrap_or(false) && bidirectional_angle.is_some() {
112        return Err(KclError::Semantic(KclErrorDetails {
113            source_ranges: vec![args.source_range],
114            message: "You cannot give both `symmetric` and `bidirectional` params, you have to choose one or the other"
115                .to_owned(),
116        }));
117    }
118
119    let angle = Angle::from_degrees(angle.unwrap_or(360.0));
120
121    let bidirectional_angle = bidirectional_angle.map(Angle::from_degrees);
122
123    let opposite = match (symmetric, bidirectional_angle) {
124        (Some(true), _) => Opposite::Symmetric,
125        (None, None) => Opposite::None,
126        (Some(false), None) => Opposite::None,
127        (None, Some(angle)) => Opposite::Other(angle),
128        (Some(false), Some(angle)) => Opposite::Other(angle),
129    };
130
131    let mut solids = Vec::new();
132    for sketch in &sketches {
133        let id = exec_state.next_uuid();
134
135        match &axis {
136            Axis2dOrEdgeReference::Axis { direction, origin } => {
137                args.batch_modeling_cmd(
138                    id,
139                    ModelingCmd::from(mcmd::Revolve {
140                        angle,
141                        target: sketch.id.into(),
142                        axis: Point3d {
143                            x: direction[0].to_mm(),
144                            y: direction[1].to_mm(),
145                            z: 0.0,
146                        },
147                        origin: Point3d {
148                            x: LengthUnit(origin[0].to_mm()),
149                            y: LengthUnit(origin[1].to_mm()),
150                            z: LengthUnit(0.0),
151                        },
152                        tolerance: LengthUnit(tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE)),
153                        axis_is_2d: true,
154                        opposite: opposite.clone(),
155                    }),
156                )
157                .await?;
158            }
159            Axis2dOrEdgeReference::Edge(edge) => {
160                let edge_id = edge.get_engine_id(exec_state, &args)?;
161                args.batch_modeling_cmd(
162                    id,
163                    ModelingCmd::from(mcmd::RevolveAboutEdge {
164                        angle,
165                        target: sketch.id.into(),
166                        edge_id,
167                        tolerance: LengthUnit(tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE)),
168                        opposite: opposite.clone(),
169                    }),
170                )
171                .await?;
172            }
173        }
174
175        solids.push(
176            do_post_extrude(
177                sketch,
178                #[cfg(feature = "artifact-graph")]
179                id.into(),
180                TyF64::new(0.0, NumericType::mm()),
181                false,
182                &super::extrude::NamedCapTags {
183                    start: tag_start.as_ref(),
184                    end: tag_end.as_ref(),
185                },
186                exec_state,
187                &args,
188            )
189            .await?,
190        );
191    }
192
193    Ok(solids)
194}