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
//! Standard library helices.

use anyhow::Result;
use derive_docs::stdlib;
use kittycad::types::ModelingCmd;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    errors::KclError,
    executor::{ExtrudeGroup, MemoryItem},
    std::Args,
};

/// Data for helices.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
pub struct HelixData {
    /// Number of revolutions.
    pub revolutions: f64,
    /// Start angle (in degrees).
    pub angle_start: f64,
    /// Is the helix rotation counter clockwise?
    /// The default is `false`.
    #[serde(default)]
    pub ccw: bool,
    /// Length of the helix. If this argument is not provided, the height of
    /// the extrude group is used.
    pub length: Option<f64>,
}

/// Create a helix on a cylinder.
pub async fn helix(args: Args) -> Result<MemoryItem, KclError> {
    let (data, extrude_group): (HelixData, Box<ExtrudeGroup>) = args.get_data_and_extrude_group()?;

    let extrude_group = inner_helix(data, extrude_group, args).await?;
    Ok(MemoryItem::ExtrudeGroup(extrude_group))
}

/// Create a helix on a cylinder.
///
/// ```no_run
/// const part001 = startSketchOn('XY')
///   |> circle([5, 5], 10, %)
///   |> extrude(10, %)
///   |> helix({
///     angle_start: 0,
///     ccw: true,
///     revolutions: 16,
///     angle_start: 0
///  }, %)
/// ```
#[stdlib {
    name = "helix",
}]
async fn inner_helix(
    data: HelixData,
    extrude_group: Box<ExtrudeGroup>,
    args: Args,
) -> Result<Box<ExtrudeGroup>, KclError> {
    let id = uuid::Uuid::new_v4();
    args.batch_modeling_cmd(
        id,
        ModelingCmd::EntityMakeHelix {
            cylinder_id: extrude_group.id,
            is_clockwise: !data.ccw,
            length: data.length.unwrap_or(extrude_group.height),
            revolutions: data.revolutions,
            start_angle: kittycad::types::Angle::from_degrees(data.angle_start),
        },
    )
    .await?;

    Ok(extrude_group)
}