Skip to main content

kcl_lib/std/
revolve.rs

1//! Standard library revolution surfaces.
2
3use anyhow::Result;
4use kcmc::ModelingCmd;
5use kcmc::each_cmd as mcmd;
6use kcmc::length_unit::LengthUnit;
7use kcmc::shared::Angle;
8use kcmc::shared::Opposite;
9use kittycad_modeling_cmds::shared::BodyType;
10use kittycad_modeling_cmds::shared::Point3d;
11use kittycad_modeling_cmds::{self as kcmc};
12
13use super::DEFAULT_TOLERANCE_MM;
14use super::args::TyF64;
15use crate::errors::KclError;
16use crate::errors::KclErrorDetails;
17use crate::execution::ExecState;
18use crate::execution::KclValue;
19use crate::execution::ModelingCmdMeta;
20use crate::execution::Sketch;
21use crate::execution::Solid;
22use crate::execution::types::PrimitiveType;
23use crate::execution::types::RuntimeType;
24use crate::parsing::ast::types::TagNode;
25use crate::std::Args;
26use crate::std::axis_or_reference::Axis2dOrEdgeReference;
27use crate::std::extrude::do_post_extrude;
28
29extern crate nalgebra_glm as glm;
30
31/// Revolve a sketch or set of sketches around an axis.
32pub async fn revolve(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
33    let sketches = args.get_unlabeled_kw_arg("sketches", &RuntimeType::sketches(), exec_state)?;
34    let axis = args.get_kw_arg(
35        "axis",
36        &RuntimeType::Union(vec![
37            RuntimeType::Primitive(PrimitiveType::Edge),
38            RuntimeType::Primitive(PrimitiveType::Axis2d),
39        ]),
40        exec_state,
41    )?;
42    let angle: Option<TyF64> = args.get_kw_arg_opt("angle", &RuntimeType::degrees(), exec_state)?;
43    let tolerance: Option<TyF64> = args.get_kw_arg_opt("tolerance", &RuntimeType::length(), exec_state)?;
44    let tag_start = args.get_kw_arg_opt("tagStart", &RuntimeType::tag_decl(), exec_state)?;
45    let tag_end = args.get_kw_arg_opt("tagEnd", &RuntimeType::tag_decl(), exec_state)?;
46    let symmetric = args.get_kw_arg_opt("symmetric", &RuntimeType::bool(), exec_state)?;
47    let bidirectional_angle: Option<TyF64> =
48        args.get_kw_arg_opt("bidirectionalAngle", &RuntimeType::angle(), exec_state)?;
49    let body_type: Option<BodyType> = args.get_kw_arg_opt("bodyType", &RuntimeType::string(), exec_state)?;
50
51    let value = inner_revolve(
52        sketches,
53        axis,
54        angle.map(|t| t.n),
55        tolerance,
56        tag_start,
57        tag_end,
58        symmetric,
59        bidirectional_angle.map(|t| t.n),
60        body_type,
61        exec_state,
62        args,
63    )
64    .await?;
65    Ok(value.into())
66}
67
68#[allow(clippy::too_many_arguments)]
69async fn inner_revolve(
70    sketches: Vec<Sketch>,
71    axis: Axis2dOrEdgeReference,
72    angle: Option<f64>,
73    tolerance: Option<TyF64>,
74    tag_start: Option<TagNode>,
75    tag_end: Option<TagNode>,
76    symmetric: Option<bool>,
77    bidirectional_angle: Option<f64>,
78    body_type: Option<BodyType>,
79    exec_state: &mut ExecState,
80    args: Args,
81) -> Result<Vec<Solid>, KclError> {
82    let body_type = body_type.unwrap_or_default();
83    if let Some(angle) = 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(&angle) || angle == 0.0 {
88            return Err(KclError::new_semantic(KclErrorDetails::new(
89                format!("Expected angle to be between -360 and 360 and not 0, found `{angle}`"),
90                vec![args.source_range],
91            )));
92        }
93    }
94
95    if let Some(bidirectional_angle) = bidirectional_angle {
96        // Return an error if the angle is zero.
97        // We don't use validate() here because we want to return a specific error message that is
98        // nice and we use the other data in the docs, so we still need use the derive above for the json schema.
99        if !(-360.0..=360.0).contains(&bidirectional_angle) || bidirectional_angle == 0.0 {
100            return Err(KclError::new_semantic(KclErrorDetails::new(
101                format!(
102                    "Expected bidirectional angle to be between -360 and 360 and not 0, found `{bidirectional_angle}`"
103                ),
104                vec![args.source_range],
105            )));
106        }
107
108        if let Some(angle) = angle {
109            let ang = angle.signum() * bidirectional_angle + angle;
110            if !(-360.0..=360.0).contains(&ang) {
111                return Err(KclError::new_semantic(KclErrorDetails::new(
112                    format!("Combined angle and bidirectional must be between -360 and 360, found '{ang}'"),
113                    vec![args.source_range],
114                )));
115            }
116        }
117    }
118
119    if symmetric.unwrap_or(false) && bidirectional_angle.is_some() {
120        return Err(KclError::new_semantic(KclErrorDetails::new(
121            "You cannot give both `symmetric` and `bidirectional` params, you have to choose one or the other"
122                .to_owned(),
123            vec![args.source_range],
124        )));
125    }
126
127    let angle = Angle::from_degrees(angle.unwrap_or(360.0));
128
129    let bidirectional_angle = bidirectional_angle.map(Angle::from_degrees);
130
131    let opposite = match (symmetric, bidirectional_angle) {
132        (Some(true), _) => Opposite::Symmetric,
133        (None, None) => Opposite::None,
134        (Some(false), None) => Opposite::None,
135        (None, Some(angle)) => Opposite::Other(angle),
136        (Some(false), Some(angle)) => Opposite::Other(angle),
137    };
138
139    let mut solids = Vec::new();
140    for sketch in &sketches {
141        let new_solid_id = exec_state.next_uuid();
142        let tolerance = tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE_MM);
143
144        let direction = match &axis {
145            Axis2dOrEdgeReference::Axis { direction, origin } => {
146                exec_state
147                    .batch_modeling_cmd(
148                        ModelingCmdMeta::from_args_id(exec_state, &args, new_solid_id),
149                        ModelingCmd::from(
150                            mcmd::Revolve::builder()
151                                .angle(angle)
152                                .target(sketch.id.into())
153                                .axis(Point3d {
154                                    x: direction[0].to_mm(),
155                                    y: direction[1].to_mm(),
156                                    z: 0.0,
157                                })
158                                .origin(Point3d {
159                                    x: LengthUnit(origin[0].to_mm()),
160                                    y: LengthUnit(origin[1].to_mm()),
161                                    z: LengthUnit(0.0),
162                                })
163                                .tolerance(LengthUnit(tolerance))
164                                .axis_is_2d(true)
165                                .opposite(opposite.clone())
166                                .body_type(body_type)
167                                .build(),
168                        ),
169                    )
170                    .await?;
171                glm::DVec2::new(direction[0].to_mm(), direction[1].to_mm())
172            }
173            Axis2dOrEdgeReference::Edge(edge) => {
174                let edge_id = edge.get_engine_id(exec_state, &args)?;
175                exec_state
176                    .batch_modeling_cmd(
177                        ModelingCmdMeta::from_args_id(exec_state, &args, new_solid_id),
178                        ModelingCmd::from(
179                            mcmd::RevolveAboutEdge::builder()
180                                .angle(angle)
181                                .target(sketch.id.into())
182                                .edge_id(edge_id)
183                                .tolerance(LengthUnit(tolerance))
184                                .opposite(opposite.clone())
185                                .body_type(body_type)
186                                .build(),
187                        ),
188                    )
189                    .await?;
190                //TODO: fix me! Need to be able to calculate this to ensure the path isn't colinear
191                glm::DVec2::new(0.0, 1.0)
192            }
193        };
194
195        let mut edge_id = None;
196        // If an edge lies on the axis of revolution it will not exist after the revolve, so
197        // it cannot be used to retrieve data about the solid
198        for path in sketch.paths.clone() {
199            if !path.is_straight_line() {
200                edge_id = Some(path.get_id());
201                break;
202            }
203
204            let from = path.get_from();
205            let to = path.get_to();
206
207            let dir = glm::DVec2::new(to[0].n - from[0].n, to[1].n - from[1].n);
208            if glm::are_collinear2d(&dir, &direction, tolerance) {
209                continue;
210            }
211            edge_id = Some(path.get_id());
212            break;
213        }
214
215        solids.push(
216            do_post_extrude(
217                sketch,
218                new_solid_id.into(),
219                false,
220                &super::extrude::NamedCapTags {
221                    start: tag_start.as_ref(),
222                    end: tag_end.as_ref(),
223                },
224                kittycad_modeling_cmds::shared::ExtrudeMethod::New,
225                exec_state,
226                &args,
227                edge_id,
228                None,
229                body_type,
230                crate::std::extrude::BeingExtruded::Sketch,
231            )
232            .await?,
233        );
234    }
235
236    Ok(solids)
237}