kcl_lib/std/
chamfer.rs

1//! Standard library chamfers.
2
3use anyhow::Result;
4use kcl_derive_docs::stdlib;
5use kcmc::{each_cmd as mcmd, length_unit::LengthUnit, shared::CutType, ModelingCmd};
6use kittycad_modeling_cmds as kcmc;
7
8use crate::{
9    errors::{KclError, KclErrorDetails},
10    execution::{ChamferSurface, EdgeCut, ExecState, ExtrudeSurface, GeoMeta, KclValue, Solid},
11    parsing::ast::types::TagNode,
12    std::{fillet::EdgeReference, Args},
13};
14
15pub(crate) const DEFAULT_TOLERANCE: f64 = 0.0000001;
16
17/// Create chamfers on tagged paths.
18pub async fn chamfer(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
19    let solid = args.get_unlabeled_kw_arg("solid")?;
20    let length = args.get_kw_arg("length")?;
21    let tags = args.kw_arg_array_and_source::<EdgeReference>("tags")?;
22    let tag = args.get_kw_arg_opt("tag")?;
23
24    super::fillet::validate_unique(&tags)?;
25    let tags: Vec<EdgeReference> = tags.into_iter().map(|item| item.0).collect();
26    let value = inner_chamfer(solid, length, tags, tag, exec_state, args).await?;
27    Ok(KclValue::Solid { value })
28}
29
30/// Cut a straight transitional edge along a tagged path.
31///
32/// Chamfer is similar in function and use to a fillet, except
33/// a fillet will blend the transition along an edge, rather than cut
34/// a sharp, straight transitional edge.
35///
36/// ```no_run
37/// // Chamfer a mounting plate.
38/// width = 20
39/// length = 10
40/// thickness = 1
41/// chamferLength = 2
42///
43/// mountingPlateSketch = startSketchOn("XY")
44///   |> startProfileAt([-width/2, -length/2], %)
45///   |> line(endAbsolute = [width/2, -length/2], tag = $edge1)
46///   |> line(endAbsolute = [width/2, length/2], tag = $edge2)
47///   |> line(endAbsolute = [-width/2, length/2], tag = $edge3)
48///   |> close(tag = $edge4)
49///
50/// mountingPlate = extrude(mountingPlateSketch, length = thickness)
51///   |> chamfer(
52///     length = chamferLength,
53///     tags = [
54///       getNextAdjacentEdge(edge1),
55///       getNextAdjacentEdge(edge2),
56///       getNextAdjacentEdge(edge3),
57///       getNextAdjacentEdge(edge4)
58///     ],
59///   )
60/// ```
61///
62/// ```no_run
63/// // Sketch on the face of a chamfer.
64/// fn cube(pos, scale) {
65/// sg = startSketchOn('XY')
66///     |> startProfileAt(pos, %)
67///     |> line(end = [0, scale])
68///     |> line(end = [scale, 0])
69///     |> line(end = [0, -scale])
70///
71///     return sg
72/// }
73///
74/// part001 = cube([0,0], 20)
75///     |> close(tag = $line1)
76///     |> extrude(length = 20)
77///     // We tag the chamfer to reference it later.
78///     |> chamfer(
79///         length = 10,
80///         tags = [getOppositeEdge(line1)],
81///         tag = $chamfer1,
82///     )  
83///
84/// sketch001 = startSketchOn(part001, chamfer1)
85///     |> startProfileAt([10, 10], %)
86///     |> line(end = [2, 0])
87///     |> line(end = [0, 2])
88///     |> line(end = [-2, 0])
89///     |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
90///     |> close()
91///     |> extrude(length = 10)
92/// ```
93#[stdlib {
94    name = "chamfer",
95    feature_tree_operation = true,
96    keywords = true,
97    unlabeled_first = true,
98    args = {
99        solid = { docs = "The solid whose edges should be chamfered" },
100        length = { docs = "The length of the chamfer" },
101        tags = { docs = "The paths you want to chamfer" },
102        tag = { docs = "Create a new tag which refers to this chamfer"},
103    }
104}]
105async fn inner_chamfer(
106    solid: Box<Solid>,
107    length: f64,
108    tags: Vec<EdgeReference>,
109    tag: Option<TagNode>,
110    exec_state: &mut ExecState,
111    args: Args,
112) -> Result<Box<Solid>, KclError> {
113    // If you try and tag multiple edges with a tagged chamfer, we want to return an
114    // error to the user that they can only tag one edge at a time.
115    if tag.is_some() && tags.len() > 1 {
116        return Err(KclError::Type(KclErrorDetails {
117            message: "You can only tag one edge at a time with a tagged chamfer. Either delete the tag for the chamfer fn if you don't need it OR separate into individual chamfer functions for each tag.".to_string(),
118            source_ranges: vec![args.source_range],
119        }));
120    }
121
122    let mut solid = solid.clone();
123    for edge_tag in tags {
124        let edge_id = match edge_tag {
125            EdgeReference::Uuid(uuid) => uuid,
126            EdgeReference::Tag(edge_tag) => args.get_tag_engine_info(exec_state, &edge_tag)?.id,
127        };
128
129        let id = exec_state.next_uuid();
130        args.batch_end_cmd(
131            id,
132            ModelingCmd::from(mcmd::Solid3dFilletEdge {
133                edge_id,
134                object_id: solid.id,
135                radius: LengthUnit(length),
136                tolerance: LengthUnit(DEFAULT_TOLERANCE), // We can let the user set this in the future.
137                cut_type: CutType::Chamfer,
138                // We make this a none so that we can remove it in the future.
139                face_id: None,
140            }),
141        )
142        .await?;
143
144        solid.edge_cuts.push(EdgeCut::Chamfer {
145            id,
146            edge_id,
147            length,
148            tag: Box::new(tag.clone()),
149        });
150
151        if let Some(ref tag) = tag {
152            solid.value.push(ExtrudeSurface::Chamfer(ChamferSurface {
153                face_id: id,
154                tag: Some(tag.clone()),
155                geo_meta: GeoMeta {
156                    id,
157                    metadata: args.source_range.into(),
158                },
159            }));
160        }
161    }
162
163    Ok(solid)
164}