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