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