1use anyhow::Result;
4use indexmap::IndexMap;
5use kcmc::{ModelingCmd, each_cmd as mcmd, length_unit::LengthUnit, shared::CutType};
6use kittycad_modeling_cmds as kcmc;
7use serde::{Deserialize, Serialize};
8
9use super::{DEFAULT_TOLERANCE_MM, args::TyF64};
10use crate::{
11 SourceRange,
12 errors::{KclError, KclErrorDetails},
13 execution::{
14 EdgeCut, ExecState, ExtrudeSurface, FilletSurface, GeoMeta, KclValue, ModelingCmdMeta, Solid, TagIdentifier,
15 types::RuntimeType,
16 },
17 parsing::ast::types::TagNode,
18 std::Args,
19};
20
21#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
23#[serde(untagged)]
24pub enum EdgeReference {
25 Uuid(uuid::Uuid),
27 Tag(Box<TagIdentifier>),
29}
30
31impl EdgeReference {
32 pub fn get_engine_id(&self, exec_state: &mut ExecState, args: &Args) -> Result<uuid::Uuid, KclError> {
33 match self {
34 EdgeReference::Uuid(uuid) => Ok(*uuid),
35 EdgeReference::Tag(tag) => Ok(args.get_tag_engine_info(exec_state, tag)?.id),
36 }
37 }
38}
39
40pub(super) fn validate_unique<T: Eq + std::hash::Hash>(tags: &[(T, SourceRange)]) -> Result<(), KclError> {
41 let mut tag_counts: IndexMap<&T, Vec<SourceRange>> = Default::default();
43 for tag in tags {
44 tag_counts.entry(&tag.0).or_insert(Vec::new()).push(tag.1);
45 }
46 let mut duplicate_tags_source = Vec::new();
47 for (_tag, count) in tag_counts {
48 if count.len() > 1 {
49 duplicate_tags_source.extend(count)
50 }
51 }
52 if !duplicate_tags_source.is_empty() {
53 return Err(KclError::new_type(KclErrorDetails::new(
54 "The same edge ID is being referenced multiple times, which is not allowed. Please select a different edge"
55 .to_string(),
56 duplicate_tags_source,
57 )));
58 }
59 Ok(())
60}
61
62pub async fn fillet(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
64 let solid = args.get_unlabeled_kw_arg("solid", &RuntimeType::solid(), exec_state)?;
65 let radius: TyF64 = args.get_kw_arg("radius", &RuntimeType::length(), exec_state)?;
66 let tolerance: Option<TyF64> = args.get_kw_arg_opt("tolerance", &RuntimeType::length(), exec_state)?;
67 let tags = args.kw_arg_edge_array_and_source("tags")?;
68 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
69
70 validate_unique(&tags)?;
72 let tags: Vec<EdgeReference> = tags.into_iter().map(|item| item.0).collect();
73 let value = inner_fillet(solid, radius, tags, tolerance, tag, exec_state, args).await?;
74 Ok(KclValue::Solid { value })
75}
76
77async fn inner_fillet(
78 solid: Box<Solid>,
79 radius: TyF64,
80 tags: Vec<EdgeReference>,
81 tolerance: Option<TyF64>,
82 tag: Option<TagNode>,
83 exec_state: &mut ExecState,
84 args: Args,
85) -> Result<Box<Solid>, KclError> {
86 if tag.is_some() && tags.len() > 1 {
89 return Err(KclError::new_type(KclErrorDetails {
90 message: "You can only tag one edge at a time with a tagged fillet. Either delete the tag for the fillet fn if you don't need it OR separate into individual fillet functions for each tag.".to_string(),
91 source_ranges: vec![args.source_range],
92 backtrace: Default::default(),
93 }));
94 }
95 if tags.is_empty() {
96 return Err(KclError::new_semantic(KclErrorDetails {
97 source_ranges: vec![args.source_range],
98 message: "You must fillet at least one tag".to_owned(),
99 backtrace: Default::default(),
100 }));
101 }
102
103 let mut solid = solid.clone();
104 let edge_ids = tags
105 .into_iter()
106 .map(|edge_tag| edge_tag.get_engine_id(exec_state, &args))
107 .collect::<Result<Vec<_>, _>>()?;
108
109 let id = exec_state.next_uuid();
110 let mut extra_face_ids = Vec::new();
111 let num_extra_ids = edge_ids.len() - 1;
112 for _ in 0..num_extra_ids {
113 extra_face_ids.push(exec_state.next_uuid());
114 }
115 exec_state
116 .batch_end_cmd(
117 ModelingCmdMeta::from_args_id(&args, id),
118 ModelingCmd::from(mcmd::Solid3dFilletEdge {
119 edge_id: None,
120 edge_ids: edge_ids.clone(),
121 extra_face_ids,
122 strategy: Default::default(),
123 object_id: solid.id,
124 radius: LengthUnit(radius.to_mm()),
125 tolerance: LengthUnit(tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE_MM)),
126 cut_type: CutType::Fillet,
127 }),
128 )
129 .await?;
130
131 let new_edge_cuts = edge_ids.into_iter().map(|edge_id| EdgeCut::Fillet {
132 id,
133 edge_id,
134 radius: radius.clone(),
135 tag: Box::new(tag.clone()),
136 });
137 solid.edge_cuts.extend(new_edge_cuts);
138
139 if let Some(ref tag) = tag {
140 solid.value.push(ExtrudeSurface::Fillet(FilletSurface {
141 face_id: id,
142 tag: Some(tag.clone()),
143 geo_meta: GeoMeta {
144 id,
145 metadata: args.source_range.into(),
146 },
147 }));
148 }
149
150 Ok(solid)
151}
152
153#[cfg(test)]
154mod tests {
155 use super::*;
156
157 #[test]
158 fn test_validate_unique() {
159 let dup_a = SourceRange::from([1, 3, 0]);
160 let dup_b = SourceRange::from([10, 30, 0]);
161 let tags = vec![("abc", dup_a), ("abc", dup_b), ("def", SourceRange::from([2, 4, 0]))];
163 let actual = validate_unique(&tags);
164 let expected = vec![dup_a, dup_b];
168 assert_eq!(actual.err().unwrap().source_ranges(), expected);
169 }
170}