Files
modeling-app/rust/kcl-lib/src/std/fillet.rs
Adam Chalmers 85ccc6900c Do multiple chamfer/fillet in one API call (#6750)
KCL's `fillet` function takes an array of edges to fillet. Previously this would do `n` fillet API commands, one per edge. This PR combines them all into one call, which should improve performance. You can see the effect in the  artifact_commands snapshots, e.g. `rust/kcl-lib/tests/kcl_samples/axial-fan/artifact_commands.snap` 

Besides performance, this should fix a bug where some KCL fillets would fail, when they should have succeeded. Example from @max-mrgrsk:

```kcl
sketch001 = startSketchOn(XY)
  |> startProfile(at = [-12, -6])
  |> line(end = [0, 12], tag = $seg04)
  |> line(end = [24, 0], tag = $seg03)
  |> line(end = [0, -12], tag = $seg02)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)], tag = $seg01)
  |> close()
extrude001 = extrude(
       sketch001,
       length = 12,
       tagEnd = $capEnd001,
       tagStart = $capStart001,
     )
  |> fillet(
       radius = 5,
       tags = [
         getCommonEdge(faces = [seg02, capEnd001]),
         getCommonEdge(faces = [seg01, capEnd001]),
         getCommonEdge(faces = [seg03, capEnd001]),
         getCommonEdge(faces = [seg04, capEnd001])
       ],
     )
```

This program fails on main, but succeeds on this branch.
2025-05-22 21:25:55 +00:00

169 lines
5.8 KiB
Rust

//! Standard library fillets.
use anyhow::Result;
use indexmap::IndexMap;
use kcmc::{each_cmd as mcmd, length_unit::LengthUnit, shared::CutType, ModelingCmd};
use kittycad_modeling_cmds as kcmc;
use serde::{Deserialize, Serialize};
use super::{args::TyF64, DEFAULT_TOLERANCE};
use crate::{
errors::{KclError, KclErrorDetails},
execution::{
types::RuntimeType, EdgeCut, ExecState, ExtrudeSurface, FilletSurface, GeoMeta, KclValue, Solid, TagIdentifier,
},
parsing::ast::types::TagNode,
std::Args,
SourceRange,
};
/// A tag or a uuid of an edge.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum EdgeReference {
/// A uuid of an edge.
Uuid(uuid::Uuid),
/// A tag of an edge.
Tag(Box<TagIdentifier>),
}
impl EdgeReference {
pub fn get_engine_id(&self, exec_state: &mut ExecState, args: &Args) -> Result<uuid::Uuid, KclError> {
match self {
EdgeReference::Uuid(uuid) => Ok(*uuid),
EdgeReference::Tag(tag) => Ok(args.get_tag_engine_info(exec_state, tag)?.id),
}
}
}
pub(super) fn validate_unique<T: Eq + std::hash::Hash>(tags: &[(T, SourceRange)]) -> Result<(), KclError> {
// Check if tags contains any duplicate values.
let mut tag_counts: IndexMap<&T, Vec<SourceRange>> = Default::default();
for tag in tags {
tag_counts.entry(&tag.0).or_insert(Vec::new()).push(tag.1);
}
let mut duplicate_tags_source = Vec::new();
for (_tag, count) in tag_counts {
if count.len() > 1 {
duplicate_tags_source.extend(count)
}
}
if !duplicate_tags_source.is_empty() {
return Err(KclError::Type(KclErrorDetails::new(
"The same edge ID is being referenced multiple times, which is not allowed. Please select a different edge"
.to_string(),
duplicate_tags_source,
)));
}
Ok(())
}
/// Create fillets on tagged paths.
pub async fn fillet(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let solid = args.get_unlabeled_kw_arg_typed("solid", &RuntimeType::solid(), exec_state)?;
let radius: TyF64 = args.get_kw_arg_typed("radius", &RuntimeType::length(), exec_state)?;
let tolerance: Option<TyF64> = args.get_kw_arg_opt_typed("tolerance", &RuntimeType::length(), exec_state)?;
let tags = args.kw_arg_array_and_source::<EdgeReference>("tags")?;
let tag = args.get_kw_arg_opt("tag")?;
// Run the function.
validate_unique(&tags)?;
let tags: Vec<EdgeReference> = tags.into_iter().map(|item| item.0).collect();
let value = inner_fillet(solid, radius, tags, tolerance, tag, exec_state, args).await?;
Ok(KclValue::Solid { value })
}
async fn inner_fillet(
solid: Box<Solid>,
radius: TyF64,
tags: Vec<EdgeReference>,
tolerance: Option<TyF64>,
tag: Option<TagNode>,
exec_state: &mut ExecState,
args: Args,
) -> Result<Box<Solid>, KclError> {
// If you try and tag multiple edges with a tagged fillet, we want to return an
// error to the user that they can only tag one edge at a time.
if tag.is_some() && tags.len() > 1 {
return Err(KclError::Type(KclErrorDetails {
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(),
source_ranges: vec![args.source_range],
backtrace: Default::default(),
}));
}
if tags.is_empty() {
return Err(KclError::Semantic(KclErrorDetails {
source_ranges: vec![args.source_range],
message: "You must fillet at least one tag".to_owned(),
backtrace: Default::default(),
}));
}
let mut solid = solid.clone();
let edge_ids = tags
.into_iter()
.map(|edge_tag| edge_tag.get_engine_id(exec_state, &args))
.collect::<Result<Vec<_>, _>>()?;
let id = exec_state.next_uuid();
let mut extra_face_ids = Vec::new();
let num_extra_ids = edge_ids.len() - 1;
for _ in 0..num_extra_ids {
extra_face_ids.push(exec_state.next_uuid());
}
args.batch_end_cmd(
id,
ModelingCmd::from(mcmd::Solid3dFilletEdge {
edge_id: None,
edge_ids: edge_ids.clone(),
extra_face_ids,
strategy: Default::default(),
object_id: solid.id,
radius: LengthUnit(radius.to_mm()),
tolerance: LengthUnit(tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE)),
cut_type: CutType::Fillet,
}),
)
.await?;
let new_edge_cuts = edge_ids.into_iter().map(|edge_id| EdgeCut::Fillet {
id,
edge_id,
radius: radius.clone(),
tag: Box::new(tag.clone()),
});
solid.edge_cuts.extend(new_edge_cuts);
if let Some(ref tag) = tag {
solid.value.push(ExtrudeSurface::Fillet(FilletSurface {
face_id: id,
tag: Some(tag.clone()),
geo_meta: GeoMeta {
id,
metadata: args.source_range.into(),
},
}));
}
Ok(solid)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validate_unique() {
let dup_a = SourceRange::from([1, 3, 0]);
let dup_b = SourceRange::from([10, 30, 0]);
// Two entries are duplicates (abc) with different source ranges.
let tags = vec![("abc", dup_a), ("abc", dup_b), ("def", SourceRange::from([2, 4, 0]))];
let actual = validate_unique(&tags);
// Both the duplicates should show up as errors, with both of the
// source ranges they correspond to.
// But the unique source range 'def' should not.
let expected = vec![dup_a, dup_b];
assert_eq!(actual.err().unwrap().source_ranges(), expected);
}
}