2024-03-05 11:52:45 -08:00
|
|
|
//! Standard library fillets.
|
|
|
|
|
|
|
|
use anyhow::Result;
|
2025-03-12 11:24:27 -05:00
|
|
|
use indexmap::IndexMap;
|
2025-03-19 15:12:27 -07:00
|
|
|
use kcmc::{each_cmd as mcmd, length_unit::LengthUnit, shared::CutType, ModelingCmd};
|
2024-09-18 17:04:04 -05:00
|
|
|
use kittycad_modeling_cmds as kcmc;
|
2024-03-05 11:52:45 -08:00
|
|
|
use schemars::JsonSchema;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2025-04-14 05:58:19 -04:00
|
|
|
use super::{args::TyF64, DEFAULT_TOLERANCE};
|
2024-03-05 11:52:45 -08:00
|
|
|
use crate::{
|
|
|
|
errors::{KclError, KclErrorDetails},
|
2025-03-17 17:57:26 +13:00
|
|
|
execution::{
|
2025-04-14 05:58:19 -04:00
|
|
|
types::RuntimeType, EdgeCut, ExecState, ExtrudeSurface, FilletSurface, GeoMeta, KclValue, Solid, TagIdentifier,
|
2025-03-17 17:57:26 +13:00
|
|
|
},
|
2024-12-05 17:56:49 +13:00
|
|
|
parsing::ast::types::TagNode,
|
2024-03-05 11:52:45 -08:00
|
|
|
std::Args,
|
2025-03-12 11:24:27 -05:00
|
|
|
SourceRange,
|
2024-03-05 11:52:45 -08:00
|
|
|
};
|
|
|
|
|
2024-06-24 14:45:07 -07:00
|
|
|
/// A tag or a uuid of an edge.
|
2025-03-01 02:31:38 -05:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Eq, Hash)]
|
2024-03-05 11:52:45 -08:00
|
|
|
#[ts(export)]
|
|
|
|
#[serde(untagged)]
|
2024-03-26 19:07:16 -07:00
|
|
|
pub enum EdgeReference {
|
|
|
|
/// A uuid of an edge.
|
|
|
|
Uuid(uuid::Uuid),
|
2024-06-24 14:45:07 -07:00
|
|
|
/// A tag of an edge.
|
2024-07-27 22:56:46 -07:00
|
|
|
Tag(Box<TagIdentifier>),
|
2024-03-05 11:52:45 -08:00
|
|
|
}
|
|
|
|
|
2024-09-25 16:12:18 -07:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-12 11:24:27 -05:00
|
|
|
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 {
|
|
|
|
message: "The same edge ID is being referenced multiple times, which is not allowed. Please select a different edge".to_string(),
|
|
|
|
source_ranges: duplicate_tags_source,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-03-05 11:52:45 -08:00
|
|
|
/// Create fillets on tagged paths.
|
2024-09-16 15:10:33 -04:00
|
|
|
pub async fn fillet(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
2025-04-14 05:58:19 -04:00
|
|
|
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)?;
|
2025-04-23 10:58:35 +12:00
|
|
|
let tolerance: Option<TyF64> = args.get_kw_arg_opt_typed("tolerance", &RuntimeType::length(), exec_state)?;
|
2025-03-12 11:24:27 -05:00
|
|
|
let tags = args.kw_arg_array_and_source::<EdgeReference>("tags")?;
|
2025-02-21 14:41:25 -06:00
|
|
|
let tag = args.get_kw_arg_opt("tag")?;
|
2025-03-12 11:24:27 -05:00
|
|
|
|
|
|
|
// Run the function.
|
|
|
|
validate_unique(&tags)?;
|
|
|
|
let tags: Vec<EdgeReference> = tags.into_iter().map(|item| item.0).collect();
|
2025-04-23 10:58:35 +12:00
|
|
|
let value = inner_fillet(solid, radius, tags, tolerance, tag, exec_state, args).await?;
|
2025-01-22 09:42:09 +13:00
|
|
|
Ok(KclValue::Solid { value })
|
2024-03-05 11:52:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn inner_fillet(
|
2024-09-27 15:44:44 -07:00
|
|
|
solid: Box<Solid>,
|
2025-04-14 05:58:19 -04:00
|
|
|
radius: TyF64,
|
2025-02-21 14:41:25 -06:00
|
|
|
tags: Vec<EdgeReference>,
|
2025-04-23 10:58:35 +12:00
|
|
|
tolerance: Option<TyF64>,
|
2024-10-30 16:52:17 -04:00
|
|
|
tag: Option<TagNode>,
|
2024-09-16 15:10:33 -04:00
|
|
|
exec_state: &mut ExecState,
|
2024-03-05 11:52:45 -08:00
|
|
|
args: Args,
|
2024-09-27 15:44:44 -07:00
|
|
|
) -> Result<Box<Solid>, KclError> {
|
|
|
|
let mut solid = solid.clone();
|
2025-02-21 14:41:25 -06:00
|
|
|
for edge_tag in tags {
|
2024-09-25 16:12:18 -07:00
|
|
|
let edge_id = edge_tag.get_engine_id(exec_state, &args)?;
|
2024-03-05 11:52:45 -08:00
|
|
|
|
2024-12-17 09:38:32 +13:00
|
|
|
let id = exec_state.next_uuid();
|
2024-06-22 14:31:37 -07:00
|
|
|
args.batch_end_cmd(
|
2024-06-23 19:19:24 -07:00
|
|
|
id,
|
2024-09-18 17:04:04 -05:00
|
|
|
ModelingCmd::from(mcmd::Solid3dFilletEdge {
|
2024-03-05 11:52:45 -08:00
|
|
|
edge_id,
|
2024-09-27 15:44:44 -07:00
|
|
|
object_id: solid.id,
|
2025-04-23 10:58:35 +12:00
|
|
|
radius: LengthUnit(radius.to_mm()),
|
|
|
|
tolerance: LengthUnit(tolerance.as_ref().map(|t| t.to_mm()).unwrap_or(DEFAULT_TOLERANCE)),
|
2024-09-18 17:04:04 -05:00
|
|
|
cut_type: CutType::Fillet,
|
|
|
|
}),
|
2024-03-05 11:52:45 -08:00
|
|
|
)
|
|
|
|
.await?;
|
2024-06-23 19:19:24 -07:00
|
|
|
|
2024-09-27 15:44:44 -07:00
|
|
|
solid.edge_cuts.push(EdgeCut::Fillet {
|
2024-06-23 19:19:24 -07:00
|
|
|
id,
|
|
|
|
edge_id,
|
2025-04-14 05:58:19 -04:00
|
|
|
radius: radius.clone(),
|
2024-07-28 00:30:04 -07:00
|
|
|
tag: Box::new(tag.clone()),
|
2024-06-23 19:19:24 -07:00
|
|
|
});
|
2024-07-28 00:30:04 -07:00
|
|
|
|
|
|
|
if let Some(ref tag) = tag {
|
2024-09-27 15:44:44 -07:00
|
|
|
solid.value.push(ExtrudeSurface::Fillet(FilletSurface {
|
2024-09-19 14:06:29 -07:00
|
|
|
face_id: id,
|
2024-07-28 00:30:04 -07:00
|
|
|
tag: Some(tag.clone()),
|
|
|
|
geo_meta: GeoMeta {
|
|
|
|
id,
|
|
|
|
metadata: args.source_range.into(),
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
2024-03-05 11:52:45 -08:00
|
|
|
}
|
|
|
|
|
2024-09-27 15:44:44 -07:00
|
|
|
Ok(solid)
|
2024-03-05 11:52:45 -08:00
|
|
|
}
|
|
|
|
|
2025-03-12 11:24:27 -05:00
|
|
|
#[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);
|
|
|
|
}
|
|
|
|
}
|