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
//! Functions related to extruding.

use anyhow::Result;
use derive_docs::stdlib;
use schemars::JsonSchema;
use uuid::Uuid;

use crate::{
    errors::{KclError, KclErrorDetails},
    executor::{
        ExtrudeGroup, ExtrudeGroupSet, ExtrudeSurface, ExtrudeTransform, GeoMeta, MemoryItem, Path, SketchGroup,
        SketchGroupSet, SketchSurface,
    },
    std::Args,
};

/// Extrudes by a given amount.
pub async fn extrude(args: Args) -> Result<MemoryItem, KclError> {
    let (length, sketch_group_set) = args.get_number_sketch_group_set()?;

    let result = inner_extrude(length, sketch_group_set, args).await?;

    match result {
        ExtrudeGroupSet::ExtrudeGroup(extrude_group) => Ok(MemoryItem::ExtrudeGroup(extrude_group)),
        ExtrudeGroupSet::ExtrudeGroups(extrude_groups) => Ok(MemoryItem::ExtrudeGroups { value: extrude_groups }),
    }
}

/// Extrudes by a given amount.
///
/// ```no_run
/// startSketchOn('XY')
///     |> startProfileAt([0, 0], %)
///     |> line([0, 10], %)
///     |> line([10, 0], %)
///     |> line([0, -10], %)
///     |> close(%)
///     |> extrude(5, %)
/// ```
#[stdlib {
    name = "extrude"
}]
async fn inner_extrude(length: f64, sketch_group_set: SketchGroupSet, args: Args) -> Result<ExtrudeGroupSet, KclError> {
    let id = uuid::Uuid::new_v4();

    // Extrude the element(s).
    let sketch_groups = match sketch_group_set {
        SketchGroupSet::SketchGroup(sketch_group) => vec![sketch_group],
        SketchGroupSet::SketchGroups(sketch_groups) => sketch_groups,
    };
    let mut extrude_groups = Vec::new();
    for sketch_group in sketch_groups.iter() {
        args.send_modeling_cmd(
            id,
            kittycad::types::ModelingCmd::Extrude {
                target: sketch_group.id,
                distance: length,
                cap: true,
            },
        )
        .await?;
        extrude_groups.push(do_post_extrude(sketch_group.clone(), length, id, args.clone()).await?);
    }

    if extrude_groups.len() == 1 {
        Ok(ExtrudeGroupSet::ExtrudeGroup(extrude_groups.pop().unwrap()))
    } else {
        Ok(ExtrudeGroupSet::ExtrudeGroups(extrude_groups))
    }
}

pub(crate) async fn do_post_extrude(
    sketch_group: Box<SketchGroup>,
    length: f64,
    id: Uuid,
    args: Args,
) -> Result<Box<ExtrudeGroup>, KclError> {
    // We need to do this after extrude for sketch on face.
    if let SketchSurface::Face(_) = sketch_group.on {
        // Disable the sketch mode.
        args.send_modeling_cmd(uuid::Uuid::new_v4(), kittycad::types::ModelingCmd::SketchModeDisable {})
            .await?;
    }

    // Bring the object to the front of the scene.
    // See: https://github.com/KittyCAD/modeling-app/issues/806
    args.send_modeling_cmd(
        uuid::Uuid::new_v4(),
        kittycad::types::ModelingCmd::ObjectBringToFront {
            object_id: sketch_group.id,
        },
    )
    .await?;

    if sketch_group.value.is_empty() {
        return Err(KclError::Type(KclErrorDetails {
            message: "Expected a non-empty sketch group".to_string(),
            source_ranges: vec![args.source_range],
        }));
    }

    let mut edge_id = None;
    for segment in sketch_group.value.iter() {
        if let Path::ToPoint { base } = segment {
            edge_id = Some(base.geo_meta.id);
            break;
        }
    }

    let Some(edge_id) = edge_id else {
        return Err(KclError::Type(KclErrorDetails {
            message: "Expected a Path::ToPoint variant".to_string(),
            source_ranges: vec![args.source_range],
        }));
    };

    let mut sketch_group = *sketch_group.clone();

    // If we were sketching on a face, we need the original face id.
    if let SketchSurface::Face(face) = sketch_group.on {
        sketch_group.id = face.sketch_group_id;
    }

    let solid3d_info = args
        .send_modeling_cmd(
            id,
            kittycad::types::ModelingCmd::Solid3DGetExtrusionFaceInfo {
                edge_id,
                object_id: sketch_group.id,
            },
        )
        .await?;

    let face_infos = if let kittycad::types::OkWebSocketResponseData::Modeling {
        modeling_response: kittycad::types::OkModelingCmdResponse::Solid3DGetExtrusionFaceInfo { data },
    } = solid3d_info
    {
        data.faces
    } else {
        vec![]
    };

    // Create a hashmap for quick id lookup
    let mut face_id_map = std::collections::HashMap::new();
    // creating fake ids for start and end caps is to make extrudes mock-execute safe
    let mut start_cap_id = if args.ctx.is_mock { Some(Uuid::new_v4()) } else { None };
    let mut end_cap_id = if args.ctx.is_mock { Some(Uuid::new_v4()) } else { None };

    for face_info in face_infos {
        match face_info.cap {
            kittycad::types::ExtrusionFaceCapType::Bottom => start_cap_id = face_info.face_id,
            kittycad::types::ExtrusionFaceCapType::Top => end_cap_id = face_info.face_id,
            _ => {
                if let Some(curve_id) = face_info.curve_id {
                    face_id_map.insert(curve_id, face_info.face_id);
                }
            }
        }
    }

    // Iterate over the sketch_group.value array and add face_id to GeoMeta
    let mut new_value: Vec<ExtrudeSurface> = Vec::new();
    for path in sketch_group.value.iter() {
        if let Some(Some(actual_face_id)) = face_id_map.get(&path.get_base().geo_meta.id) {
            match path {
                Path::TangentialArc { .. } | Path::TangentialArcTo { .. } => {
                    let extrude_surface = ExtrudeSurface::ExtrudeArc(crate::executor::ExtrudeArc {
                        position: sketch_group.position, // TODO should be for the extrude surface
                        rotation: sketch_group.rotation, // TODO should be for the extrude surface
                        face_id: *actual_face_id,
                        name: path.get_base().name.clone(),
                        geo_meta: GeoMeta {
                            id: path.get_base().geo_meta.id,
                            metadata: path.get_base().geo_meta.metadata.clone(),
                        },
                    });
                    new_value.push(extrude_surface);
                }
                Path::Base { .. } | Path::ToPoint { .. } | Path::Horizontal { .. } | Path::AngledLineTo { .. } => {
                    let extrude_surface = ExtrudeSurface::ExtrudePlane(crate::executor::ExtrudePlane {
                        position: sketch_group.position, // TODO should be for the extrude surface
                        rotation: sketch_group.rotation, // TODO should be for the extrude surface
                        face_id: *actual_face_id,
                        name: path.get_base().name.clone(),
                        geo_meta: GeoMeta {
                            id: path.get_base().geo_meta.id,
                            metadata: path.get_base().geo_meta.metadata.clone(),
                        },
                    });
                    new_value.push(extrude_surface);
                }
            }
        } else if args.ctx.is_mock {
            // Only pre-populate the extrude surface if we are in mock mode.
            new_value.push(ExtrudeSurface::ExtrudePlane(crate::executor::ExtrudePlane {
                position: sketch_group.position, // TODO should be for the extrude surface
                rotation: sketch_group.rotation, // TODO should be for the extrude surface
                // pushing this values with a fake face_id to make extrudes mock-execute safe
                face_id: Uuid::new_v4(),
                name: path.get_base().name.clone(),
                geo_meta: GeoMeta {
                    id: path.get_base().geo_meta.id,
                    metadata: path.get_base().geo_meta.metadata.clone(),
                },
            }));
        }
    }

    Ok(Box::new(ExtrudeGroup {
        // Ok so you would think that the id would be the id of the extrude group,
        // that we passed in to the function, but it's actually the id of the
        // sketch group.
        id: sketch_group.id,
        value: new_value,
        sketch_group_values: sketch_group.value.clone(),
        height: length,
        position: sketch_group.position,
        rotation: sketch_group.rotation,
        x_axis: sketch_group.x_axis,
        y_axis: sketch_group.y_axis,
        z_axis: sketch_group.z_axis,
        start_cap_id,
        end_cap_id,
        meta: sketch_group.meta,
    }))
}

/// Returns the extrude wall transform.
pub async fn get_extrude_wall_transform(args: Args) -> Result<MemoryItem, KclError> {
    let (surface_name, extrude_group) = args.get_path_name_extrude_group()?;
    let result = inner_get_extrude_wall_transform(&surface_name, *extrude_group, args)?;
    Ok(MemoryItem::ExtrudeTransform(result))
}

/// Returns the extrude wall transform.
///
/// ```no_run
/// const box = startSketchOn('XY')
///     |> startProfileAt([0, 0], %)
///     |> line([0, 10], %)
///     |> line([10, 0], %)
///     |> line([0, -10], %, "surface")
///     |> close(%)
///     |> extrude(5, %)
///
/// const transform = getExtrudeWallTransform('surface', box)
/// ```
#[stdlib {
    name = "getExtrudeWallTransform"
}]
fn inner_get_extrude_wall_transform(
    surface_name: &str,
    extrude_group: ExtrudeGroup,
    args: Args,
) -> Result<Box<ExtrudeTransform>, KclError> {
    let surface = extrude_group.get_path_by_name(surface_name).ok_or_else(|| {
        KclError::Type(KclErrorDetails {
            message: format!(
                "Expected a surface name that exists in the given ExtrudeGroup, found `{}`",
                surface_name
            ),
            source_ranges: vec![args.source_range],
        })
    })?;

    Ok(Box::new(ExtrudeTransform {
        position: surface.get_position(),
        rotation: surface.get_rotation(),
        meta: extrude_group.meta,
    }))
}