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;
30use crate::std::csg::CsgAlgorithm;
31
32#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
34#[serde(untagged)]
35pub enum EdgeReference {
36 Uuid(uuid::Uuid),
38 Tag(Box<TagIdentifier>),
40}
41
42impl EdgeReference {
43 pub fn get_engine_id(&self, exec_state: &mut ExecState, args: &Args) -> Result<uuid::Uuid, KclError> {
44 match self {
45 EdgeReference::Uuid(uuid) => Ok(*uuid),
46 EdgeReference::Tag(tag) => Ok(args.get_tag_engine_info(exec_state, tag)?.id),
47 }
48 }
49
50 pub fn get_all_engine_ids(&self, exec_state: &mut ExecState, args: &Args) -> Result<Vec<uuid::Uuid>, KclError> {
53 match self {
54 EdgeReference::Uuid(uuid) => Ok(vec![*uuid]),
55 EdgeReference::Tag(tag) => {
56 let infos = tag.get_all_cur_info();
57 if infos.is_empty() {
58 Ok(vec![args.get_tag_engine_info(exec_state, tag)?.id])
60 } else {
61 Ok(infos.iter().map(|i| i.id).collect())
62 }
63 }
64 }
65 }
66}
67
68pub(super) fn validate_unique<T: Eq + std::hash::Hash>(tags: &[(T, SourceRange)]) -> Result<(), KclError> {
69 let mut tag_counts: IndexMap<&T, Vec<SourceRange>> = Default::default();
71 for tag in tags {
72 tag_counts.entry(&tag.0).or_insert(Vec::new()).push(tag.1);
73 }
74 let mut duplicate_tags_source = Vec::new();
75 for (_tag, count) in tag_counts {
76 if count.len() > 1 {
77 duplicate_tags_source.extend(count)
78 }
79 }
80 if !duplicate_tags_source.is_empty() {
81 return Err(KclError::new_type(KclErrorDetails::new(
82 "The same edge ID is being referenced multiple times, which is not allowed. Please select a different edge"
83 .to_string(),
84 duplicate_tags_source,
85 )));
86 }
87 Ok(())
88}
89
90pub async fn fillet(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
92 let solid = args.get_unlabeled_kw_arg("solid", &RuntimeType::solid(), exec_state)?;
93 let radius: TyF64 = args.get_kw_arg("radius", &RuntimeType::length(), exec_state)?;
94 let tolerance: Option<TyF64> = args.get_kw_arg_opt("tolerance", &RuntimeType::length(), exec_state)?;
95 let tags = args.kw_arg_edge_array_and_source("tags")?;
96 let tag = args.get_kw_arg_opt("tag", &RuntimeType::tag_decl(), exec_state)?;
97 let legacy_csg: Option<bool> = args.get_kw_arg_opt("legacyMethod", &RuntimeType::bool(), exec_state)?;
98 let csg_algorithm = CsgAlgorithm::legacy(legacy_csg.unwrap_or_default());
99
100 validate_unique(&tags)?;
102 let tags: Vec<EdgeReference> = tags.into_iter().map(|item| item.0).collect();
103 let value = inner_fillet(solid, radius, tags, tolerance, csg_algorithm, tag, exec_state, args).await?;
104 Ok(KclValue::Solid { value })
105}
106
107#[allow(clippy::too_many_arguments)]
108async fn inner_fillet(
109 solid: Box<Solid>,
110 radius: TyF64,
111 tags: Vec<EdgeReference>,
112 tolerance: Option<TyF64>,
113 csg_algorithm: CsgAlgorithm,
114 tag: Option<TagNode>,
115 exec_state: &mut ExecState,
116 args: Args,
117) -> Result<Box<Solid>, KclError> {
118 if tag.is_some() && tags.len() > 1 {
121 return Err(KclError::new_type(KclErrorDetails {
122 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(),
123 source_ranges: vec![args.source_range],
124 backtrace: Default::default(),
125 }));
126 }
127 if tags.is_empty() {
128 return Err(KclError::new_semantic(KclErrorDetails {
129 source_ranges: vec![args.source_range],
130 message: "You must fillet at least one tag".to_owned(),
131 backtrace: Default::default(),
132 }));
133 }
134
135 let mut solid = solid.clone();
136 let edge_ids = tags
137 .into_iter()
138 .map(|edge_tag| edge_tag.get_all_engine_ids(exec_state, &args))
139 .try_fold(Vec::new(), |mut acc, item| match item {
140 Ok(ids) => {
141 acc.extend(ids);
142 Ok(acc)
143 }
144 Err(e) => Err(e),
145 })?;
146
147 let id = exec_state.next_uuid();
148 let mut extra_face_ids = Vec::new();
149 let num_extra_ids = edge_ids.len() - 1;
150 for _ in 0..num_extra_ids {
151 extra_face_ids.push(exec_state.next_uuid());
152 }
153 exec_state
154 .batch_end_cmd(
155 ModelingCmdMeta::from_args_id(exec_state, &args, id),
156 ModelingCmd::from(
157 mcmd::Solid3dFilletEdge::builder()
158 .use_legacy(csg_algorithm.is_legacy())
159 .edge_ids(edge_ids.clone())
160 .extra_face_ids(extra_face_ids)
161 .strategy(Default::default())
162 .object_id(solid.id)
163 .radius(LengthUnit(radius.to_mm()))
164 .tolerance(LengthUnit(
165 tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE_MM),
166 ))
167 .cut_type(CutType::Fillet)
168 .build(),
169 ),
170 )
171 .await?;
172
173 let new_edge_cuts = edge_ids.into_iter().map(|edge_id| EdgeCut::Fillet {
174 id,
175 edge_id,
176 radius: radius.clone(),
177 tag: Box::new(tag.clone()),
178 });
179 solid.edge_cuts.extend(new_edge_cuts);
180
181 if let Some(ref tag) = tag {
182 solid.value.push(ExtrudeSurface::Fillet(FilletSurface {
183 face_id: id,
184 tag: Some(tag.clone()),
185 geo_meta: GeoMeta {
186 id,
187 metadata: args.source_range.into(),
188 },
189 }));
190 }
191
192 Ok(solid)
193}
194
195#[cfg(test)]
196mod tests {
197 use super::*;
198
199 #[test]
200 fn test_validate_unique() {
201 let dup_a = SourceRange::from([1, 3, 0]);
202 let dup_b = SourceRange::from([10, 30, 0]);
203 let tags = vec![("abc", dup_a), ("abc", dup_b), ("def", SourceRange::from([2, 4, 0]))];
205 let actual = validate_unique(&tags);
206 let expected = vec![dup_a, dup_b];
210 assert_eq!(actual.err().unwrap().source_ranges(), expected);
211 }
212}