KCL: Migrate hole to kwargs (#6382)

Previously:

`|> hole(circle(radius = 2, center = p), %)`

Now:

`|> subtract2d(tool = circle(radius = 2, center = p))`
This commit is contained in:
Adam Chalmers
2025-04-26 15:31:51 -05:00
committed by GitHub
parent 5a18f551aa
commit 6e115c19d5
116 changed files with 12912 additions and 12847 deletions

View File

@ -262,7 +262,7 @@ pub async fn appearance(exec_state: &mut ExecState, args: Args) -> Result<KclVal
/// center = [0, 0],
/// radius = 2,
/// )
/// |> hole(pipeHole, %)
/// |> subtract2d(tool = pipeHole)
/// |> sweep(path = sweepPath)
/// |> appearance(
/// color = "#ff0000",

View File

@ -615,50 +615,6 @@ impl Args {
Ok(numbers)
}
pub(crate) fn get_sketches(&self, exec_state: &mut ExecState) -> Result<(Vec<Sketch>, Sketch), KclError> {
let Some(arg0) = self.args.first() else {
return Err(KclError::Semantic(KclErrorDetails {
message: "Expected a sketch argument".to_owned(),
source_ranges: vec![self.source_range],
}));
};
let sarg = arg0.value.coerce(&RuntimeType::sketches(), exec_state).map_err(|_| {
KclError::Type(KclErrorDetails {
message: format!(
"Expected an array of sketches, found {}",
arg0.value.human_friendly_type()
),
source_ranges: vec![self.source_range],
})
})?;
let sketches = match sarg {
KclValue::HomArray { value, .. } => value.iter().map(|v| v.as_sketch().unwrap().clone()).collect(),
_ => unreachable!(),
};
let Some(arg1) = self.args.get(1) else {
return Err(KclError::Semantic(KclErrorDetails {
message: "Expected a second sketch argument".to_owned(),
source_ranges: vec![self.source_range],
}));
};
let sarg = arg1
.value
.coerce(&RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)
.map_err(|_| {
KclError::Type(KclErrorDetails {
message: format!("Expected a sketch, found {}", arg1.value.human_friendly_type()),
source_ranges: vec![self.source_range],
})
})?;
let sketch = match sarg {
KclValue::Sketch { value } => *value,
_ => unreachable!(),
};
Ok((sketches, sketch))
}
pub(crate) async fn get_adjacent_face_to_tag(
&self,
exec_state: &mut ExecState,

View File

@ -89,7 +89,7 @@ lazy_static! {
Box::new(crate::std::sketch::Arc),
Box::new(crate::std::sketch::TangentialArc),
Box::new(crate::std::sketch::BezierCurve),
Box::new(crate::std::sketch::Hole),
Box::new(crate::std::sketch::Subtract2D),
Box::new(crate::std::clone::Clone),
Box::new(crate::std::patterns::PatternLinear2D),
Box::new(crate::std::patterns::PatternLinear3D),

View File

@ -13,6 +13,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::utils::{point_to_len_unit, point_to_mm, untype_point, untyped_point_to_mm};
use crate::execution::types::ArrayLen;
use crate::{
errors::{KclError, KclErrorDetails},
execution::{
@ -2253,10 +2254,20 @@ async fn inner_bezier_curve(
}
/// Use a sketch to cut a hole in another sketch.
pub async fn hole(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (hole_sketch, sketch): (Vec<Sketch>, Sketch) = args.get_sketches(exec_state)?;
pub async fn subtract_2d(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let sketch =
args.get_unlabeled_kw_arg_typed("sketch", &RuntimeType::Primitive(PrimitiveType::Sketch), exec_state)?;
let new_sketch = inner_hole(hole_sketch, sketch, exec_state, args).await?;
let tool: Vec<Sketch> = args.get_kw_arg_typed(
"tool",
&RuntimeType::Array(
Box::new(RuntimeType::Primitive(PrimitiveType::Sketch)),
ArrayLen::NonEmpty,
),
exec_state,
)?;
let new_sketch = inner_subtract_2d(sketch, tool, exec_state, args).await?;
Ok(KclValue::Sketch {
value: Box::new(new_sketch),
})
@ -2271,8 +2282,8 @@ pub async fn hole(exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kc
/// |> line(end = [5, 0])
/// |> line(end = [0, -5])
/// |> close()
/// |> hole(circle( center = [1, 1], radius = .25 ), %)
/// |> hole(circle( center = [1, 4], radius = .25 ), %)
/// |> subtract2d(tool =circle( center = [1, 1], radius = .25 ))
/// |> subtract2d(tool =circle( center = [1, 4], radius = .25 ))
///
/// example = extrude(exampleSketch, length = 1)
/// ```
@ -2290,20 +2301,26 @@ pub async fn hole(exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kc
///
/// exampleSketch = startSketchOn(-XZ)
/// |> circle( center = [0, 0], radius = 3 )
/// |> hole(squareHoleSketch(), %)
/// |> subtract2d(tool = squareHoleSketch())
/// example = extrude(exampleSketch, length = 1)
/// ```
#[stdlib {
name = "hole",
name = "subtract2d",
feature_tree_operation = true,
keywords = true,
unlabeled_first = true,
args = {
sketch = { docs = "Which sketch should this path be added to?" },
tool = { docs = "The shape(s) which should be cut out of the sketch." },
}
}]
async fn inner_hole(
hole_sketch: Vec<Sketch>,
async fn inner_subtract_2d(
sketch: Sketch,
tool: Vec<Sketch>,
exec_state: &mut ExecState,
args: Args,
) -> Result<Sketch, KclError> {
for hole_sketch in hole_sketch {
for hole_sketch in tool {
args.batch_modeling_cmd(
exec_state.next_uuid(),
ModelingCmd::from(mcmd::Solid2dAddHole {

View File

@ -82,7 +82,7 @@ pub async fn sweep(exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
/// center = [0, 0],
/// radius = 2,
/// )
/// |> hole(pipeHole, %)
/// |> subtract2d(tool = pipeHole)
/// |> sweep(path = sweepPath)
/// ```
///

View File

@ -95,7 +95,7 @@ pub async fn scale(exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
/// center = [0, 0],
/// radius = 2,
/// )
/// |> hole(pipeHole, %)
/// |> subtract2d(tool = pipeHole)
/// |> sweep(path = sweepPath)
/// |> scale(
/// z = 2.5,
@ -261,7 +261,7 @@ pub async fn translate(exec_state: &mut ExecState, args: Args) -> Result<KclValu
/// center = [0, 0],
/// radius = 2,
/// )
/// |> hole(pipeHole, %)
/// |> subtract2d(tool = pipeHole)
/// |> sweep(path = sweepPath)
/// |> translate(
/// x = 1.0,
@ -600,7 +600,7 @@ pub async fn rotate(exec_state: &mut ExecState, args: Args) -> Result<KclValue,
/// center = [0, 0],
/// radius = 2,
/// )
/// |> hole(pipeHole, %)
/// |> subtract2d(tool = pipeHole)
/// |> sweep(path = sweepPath)
/// |> rotate(
/// roll = 10,
@ -633,7 +633,7 @@ pub async fn rotate(exec_state: &mut ExecState, args: Args) -> Result<KclValue,
/// center = [0, 0],
/// radius = 2,
/// )
/// |> hole(pipeHole, %)
/// |> subtract2d(tool = pipeHole)
/// |> sweep(path = sweepPath)
/// |> rotate(
/// roll = 10,
@ -664,7 +664,7 @@ pub async fn rotate(exec_state: &mut ExecState, args: Args) -> Result<KclValue,
/// center = [0, 0],
/// radius = 2,
/// )
/// |> hole(pipeHole, %)
/// |> subtract2d(tool = pipeHole)
/// |> sweep(path = sweepPath)
/// |> rotate(
/// axis = [0, 0, 1.0],
@ -850,7 +850,7 @@ sweepSketch = startSketchOn('XY')
center = [0, 0],
radius = 2,
)
|> hole(pipeHole, %)
|> subtract2d(tool = pipeHole)
|> sweep(
path = sweepPath,
)"#;