Skip to content

Commit

Permalink
KCL: Sweep stdlib fn now uses keyword args
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Feb 7, 2025
1 parent 5dc4213 commit ad468ed
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 54 deletions.
10 changes: 5 additions & 5 deletions src/lang/modifyAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,19 +431,19 @@ export function addSweep(
} {
const modifiedAst = structuredClone(node)
const name = findUniqueName(node, KCL_DEFAULT_CONSTANT_PREFIXES.SWEEP)
const sweep = createCallExpressionStdLib('sweep', [
createObjectExpression({ path: createIdentifier(pathDeclarator.id.name) }),
const sweep = createCallExpressionStdLibKw(
'sweep',
createIdentifier(profileDeclarator.id.name),
])
[createLabeledArg('path', createIdentifier(pathDeclarator.id.name))]
)
const declaration = createVariableDeclaration(name, sweep)
modifiedAst.body.push(declaration)
const pathToNode: PathToNode = [
['body', ''],
[modifiedAst.body.length - 1, 'index'],
['declaration', 'VariableDeclaration'],
['init', 'VariableDeclarator'],
['arguments', 'CallExpression'],
[0, 'index'],
['unlabeled', 'CallExpressionKw'],
]

return {
Expand Down
7 changes: 1 addition & 6 deletions src/wasm-lib/kcl/src/docs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,12 +1038,7 @@ mod tests {
fn get_autocomplete_snippet_sweep() {
let sweep_fn: Box<dyn StdLibFn> = Box::new(crate::std::sweep::Sweep);
let snippet = sweep_fn.to_autocomplete_snippet().unwrap();
assert_eq!(
snippet,
r#"sweep({
path = ${0:sketch000},
}, ${1:%})${}"#
);
assert_eq!(snippet, r#"sweep(path = ${0:sketch000})${}"#);
}

#[test]
Expand Down
4 changes: 1 addition & 3 deletions src/wasm-lib/kcl/src/std/appearance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,7 @@ pub async fn appearance(_exec_state: &mut ExecState, args: Args) -> Result<KclVa
/// radius = 2,
/// }, %)
/// |> hole(pipeHole, %)
/// |> sweep({
/// path: sweepPath,
/// }, %)
/// |> sweep(path = sweepPath)
/// |> appearance({
/// color: "#ff0000",
/// metalness: 50,
Expand Down
14 changes: 0 additions & 14 deletions src/wasm-lib/kcl/src/std/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,20 +1042,6 @@ impl<'a> FromKclValue<'a> for super::fillet::FilletData {
}
}

impl<'a> FromKclValue<'a> for super::sweep::SweepData {
fn from_kcl_val(arg: &'a KclValue) -> Option<Self> {
let obj = arg.as_object()?;
let_field_of!(obj, path);
let_field_of!(obj, sectional?);
let_field_of!(obj, tolerance?);
Some(Self {
path,
sectional,
tolerance,
})
}
}

impl<'a> FromKclValue<'a> for super::appearance::AppearanceData {
fn from_kcl_val(arg: &'a KclValue) -> Option<Self> {
let obj = arg.as_object()?;
Expand Down
6 changes: 3 additions & 3 deletions src/wasm-lib/kcl/src/std/helix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub async fn helix(exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
/// // Create a spring by sweeping around the helix path.
/// springSketch = startSketchOn('YZ')
/// |> circle({ center = [0, 0], radius = 0.5 }, %)
/// |> sweep({ path = helixPath }, %)
/// |> sweep(path = helixPath)
/// ```
///
/// ```no_run
Expand All @@ -64,7 +64,7 @@ pub async fn helix(exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
/// // Create a spring by sweeping around the helix path.
/// springSketch = startSketchOn('XY')
/// |> circle({ center = [0, 0], radius = 0.5 }, %)
/// |> sweep({ path = helixPath }, %)
/// |> sweep(path = helixPath)
/// ```
///
/// ```no_run
Expand All @@ -86,7 +86,7 @@ pub async fn helix(exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
/// // Create a spring by sweeping around the helix path.
/// springSketch = startSketchOn('XY')
/// |> circle({ center = [0, 0], radius = 1 }, %)
/// |> sweep({ path = helixPath }, %)
/// |> sweep(path = helixPath)
/// ```
#[stdlib {
name = "helix",
Expand Down
44 changes: 21 additions & 23 deletions src/wasm-lib/kcl/src/std/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,14 @@ pub enum SweepPath {
Helix(Box<Helix>),
}

/// Data for a sweep.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
pub struct SweepData {
/// The path to sweep along.
pub path: SweepPath,
/// If true, the sweep will be broken up into sub-sweeps (extrusions, revolves, sweeps) based on the trajectory path components.
pub sectional: Option<bool>,
/// Tolerance for the sweep operation.
#[serde(default)]
pub tolerance: Option<f64>,
}

/// Extrude a sketch along a path.
pub async fn sweep(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (data, sketch): (SweepData, Sketch) = args.get_data_and_sketch()?;
let sketch = args.get_unlabeled_kw_arg("sketch")?;
let path: SweepPath = args.get_kw_arg("path")?;
let sectional = args.get_kw_arg_opt("sectional")?;
let tolerance = args.get_kw_arg_opt("tolerance")?;

let value = inner_sweep(data, sketch, exec_state, args).await?;
let value = inner_sweep(sketch, path, sectional, tolerance, exec_state, args).await?;
Ok(KclValue::Solid { value })
}

Expand Down Expand Up @@ -82,9 +72,7 @@ pub async fn sweep(exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
/// radius = 2,
/// }, %)
/// |> hole(pipeHole, %)
/// |> sweep({
/// path: sweepPath,
/// }, %)
/// |> sweep(path = sweepPath)
/// ```
///
/// ```no_run
Expand All @@ -104,15 +92,25 @@ pub async fn sweep(exec_state: &mut ExecState, args: Args) -> Result<KclValue, K
/// // Create a spring by sweeping around the helix path.
/// springSketch = startSketchOn('YZ')
/// |> circle({ center = [0, 0], radius = 1 }, %)
/// |> sweep({ path = helixPath }, %)
/// |> sweep(path = helixPath)
/// ```
#[stdlib {
name = "sweep",
feature_tree_operation = true,
keywords = true,
unlabeled_first = true,
args = {
sketch = { docs = "The sketch that should be swept in space" },
path = { docs = "The path to sweep the sketch along" },
sectional = { docs = "If true, the sweep will be broken up into sub-sweeps (extrusions, revolves, sweeps) based on the trajectory path components." },
tolerance = { docs = "Tolerance for this operation" },
}
}]
async fn inner_sweep(
data: SweepData,
sketch: Sketch,
path: SweepPath,
sectional: Option<bool>,
tolerance: Option<f64>,
exec_state: &mut ExecState,
args: Args,
) -> Result<Box<Solid>, KclError> {
Expand All @@ -121,12 +119,12 @@ async fn inner_sweep(
id,
ModelingCmd::from(mcmd::Sweep {
target: sketch.id.into(),
trajectory: match data.path {
trajectory: match path {
SweepPath::Sketch(sketch) => sketch.id.into(),
SweepPath::Helix(helix) => helix.value.into(),
},
sectional: data.sectional.unwrap_or(false),
tolerance: LengthUnit(data.tolerance.unwrap_or(default_tolerance(&args.ctx.settings.units))),
sectional: sectional.unwrap_or(false),
tolerance: LengthUnit(tolerance.unwrap_or(default_tolerance(&args.ctx.settings.units))),
}),
)
.await?;
Expand Down

0 comments on commit ad468ed

Please sign in to comment.