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-01 13:59:01 -08:00
|
|
|
use kcl_derive_docs::stdlib;
|
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};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
errors::{KclError, KclErrorDetails},
|
2025-03-17 17:57:26 +13:00
|
|
|
execution::{
|
2025-03-21 10:56:55 +13:00
|
|
|
types::{PrimitiveType, 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-08-12 21:39:49 -07:00
|
|
|
settings::types::UnitLength,
|
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-03-17 17:57:26 +13:00
|
|
|
let solid = args.get_unlabeled_kw_arg_typed("solid", &RuntimeType::Primitive(PrimitiveType::Solid), exec_state)?;
|
2025-02-21 14:41:25 -06:00
|
|
|
let radius = args.get_kw_arg("radius")?;
|
|
|
|
let tolerance = args.get_kw_arg_opt("tolerance")?;
|
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-02-21 14:41:25 -06: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
|
|
|
}
|
|
|
|
|
2024-08-06 20:27:26 -04:00
|
|
|
/// Blend a transitional edge along a tagged path, smoothing the sharp edge.
|
|
|
|
///
|
|
|
|
/// Fillet is similar in function and use to a chamfer, except
|
|
|
|
/// a chamfer will cut a sharp transition along an edge while fillet
|
|
|
|
/// will smoothly blend the transition.
|
2024-03-13 12:56:46 -07:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2024-12-12 11:33:37 -05:00
|
|
|
/// width = 20
|
|
|
|
/// length = 10
|
|
|
|
/// thickness = 1
|
|
|
|
/// filletRadius = 2
|
2024-05-14 17:10:47 -07:00
|
|
|
///
|
2024-12-12 11:33:37 -05:00
|
|
|
/// mountingPlateSketch = startSketchOn("XY")
|
2024-05-14 17:10:47 -07:00
|
|
|
/// |> startProfileAt([-width/2, -length/2], %)
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
/// |> line(endAbsolute = [width/2, -length/2], tag = $edge1)
|
|
|
|
/// |> line(endAbsolute = [width/2, length/2], tag = $edge2)
|
|
|
|
/// |> line(endAbsolute = [-width/2, length/2], tag = $edge3)
|
|
|
|
/// |> close(tag = $edge4)
|
2024-05-14 17:10:47 -07:00
|
|
|
///
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
/// mountingPlate = extrude(mountingPlateSketch, length = thickness)
|
2025-02-21 14:41:25 -06:00
|
|
|
/// |> fillet(
|
2024-11-25 09:21:55 +13:00
|
|
|
/// radius = filletRadius,
|
|
|
|
/// tags = [
|
2024-07-27 22:56:46 -07:00
|
|
|
/// getNextAdjacentEdge(edge1),
|
|
|
|
/// getNextAdjacentEdge(edge2),
|
|
|
|
/// getNextAdjacentEdge(edge3),
|
|
|
|
/// getNextAdjacentEdge(edge4)
|
2024-05-14 17:10:47 -07:00
|
|
|
/// ],
|
2025-02-21 14:41:25 -06:00
|
|
|
/// )
|
2024-03-13 12:56:46 -07:00
|
|
|
/// ```
|
2024-08-12 21:39:49 -07:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2024-12-12 11:33:37 -05:00
|
|
|
/// width = 20
|
|
|
|
/// length = 10
|
|
|
|
/// thickness = 1
|
|
|
|
/// filletRadius = 1
|
2024-08-12 21:39:49 -07:00
|
|
|
///
|
2024-12-12 11:33:37 -05:00
|
|
|
/// mountingPlateSketch = startSketchOn("XY")
|
2024-08-12 21:39:49 -07:00
|
|
|
/// |> startProfileAt([-width/2, -length/2], %)
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
/// |> line(endAbsolute = [width/2, -length/2], tag = $edge1)
|
|
|
|
/// |> line(endAbsolute = [width/2, length/2], tag = $edge2)
|
|
|
|
/// |> line(endAbsolute = [-width/2, length/2], tag = $edge3)
|
|
|
|
/// |> close(tag = $edge4)
|
2024-08-12 21:39:49 -07:00
|
|
|
///
|
KCL: Use keyword arguments for line, lineTo, extrude and close (#5249)
Part of #4600.
PR: https://github.com/KittyCAD/modeling-app/pull/4826
# Changes to KCL stdlib
- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@sketch, length)`
Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:
```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```
Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as
```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```
Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main
The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:
```
line\(([^=]*), %\)
line(end = $1)
line\((.*), %, (.*)\)
line(end = $1, tag = $2)
lineTo\((.*), %\)
line(endAbsolute = $1)
lineTo\((.*), %, (.*)\)
line(endAbsolute = $1, tag = $2)
extrude\((.*), %\)
extrude(length = $1)
extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)
close\(%, (.*)\)
close(tag = $1)
```
# Selected notes from commits before I squash them all
* Fix test 'yRelative to horizontal distance'
Fixes:
- Make a lineTo helper
- Fix pathToNode to go through the labeled arg .arg property
* Fix test by changing lookups into transformMap
Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.
So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.
* Stop asserting on exact source ranges
When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.
* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.
* Fixed another queryAst test
There were 2 problems:
- Test was looking for the old style of `line` call to choose an offset
for pathToNode
- Test assumed that the `tag` param was always the third one, but in
a kwarg call, you have to look it up by label
* Fix test: traverse was not handling CallExpressionKw
* Fix another test, addTagKw
addTag helper was not aware of kw args.
* Convert close from positional to kwargs
If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.
But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.
The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.
* Fix the last test
Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.
Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.
* Fix many bad regex find-replaces
I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.
Fixes this.
* Fixes test 'Basic sketch › code pane closed at start'
Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.
This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.
* Fix: Optional args given with wrong type were silently ignored
Optional args don't have to be given. But if the user gives them, they
should be the right type.
Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.
Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.
---------
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <frank@kittycad.io>
Co-authored-by: Kevin Nadro <kevin@zoo.dev>
Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
2025-02-04 08:31:43 -06:00
|
|
|
/// mountingPlate = extrude(mountingPlateSketch, length = thickness)
|
2025-02-21 14:41:25 -06:00
|
|
|
/// |> fillet(
|
2024-11-25 09:21:55 +13:00
|
|
|
/// radius = filletRadius,
|
|
|
|
/// tolerance = 0.000001,
|
|
|
|
/// tags = [
|
2024-08-12 21:39:49 -07:00
|
|
|
/// getNextAdjacentEdge(edge1),
|
|
|
|
/// getNextAdjacentEdge(edge2),
|
|
|
|
/// getNextAdjacentEdge(edge3),
|
|
|
|
/// getNextAdjacentEdge(edge4)
|
|
|
|
/// ],
|
2025-02-21 14:41:25 -06:00
|
|
|
/// )
|
2024-08-12 21:39:49 -07:00
|
|
|
/// ```
|
2024-03-05 11:52:45 -08:00
|
|
|
#[stdlib {
|
|
|
|
name = "fillet",
|
2024-12-16 13:10:31 -05:00
|
|
|
feature_tree_operation = true,
|
2025-02-21 14:41:25 -06:00
|
|
|
keywords = true,
|
|
|
|
unlabeled_first = true,
|
|
|
|
args = {
|
|
|
|
solid = { docs = "The solid whose edges should be filletted" },
|
|
|
|
radius = { docs = "The radius of the fillet" },
|
|
|
|
tags = { docs = "The paths you want to fillet" },
|
|
|
|
tolerance = { docs = "The tolerance for this fillet" },
|
|
|
|
tag = { docs = "Create a new tag which refers to this fillet"},
|
|
|
|
}
|
2024-03-05 11:52:45 -08:00
|
|
|
}]
|
|
|
|
async fn inner_fillet(
|
2024-09-27 15:44:44 -07:00
|
|
|
solid: Box<Solid>,
|
2025-02-21 14:41:25 -06:00
|
|
|
radius: f64,
|
|
|
|
tags: Vec<EdgeReference>,
|
|
|
|
tolerance: Option<f64>,
|
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-02-21 14:41:25 -06:00
|
|
|
radius: LengthUnit(radius),
|
|
|
|
tolerance: LengthUnit(tolerance.unwrap_or(default_tolerance(&args.ctx.settings.units))),
|
2024-09-18 17:04:04 -05:00
|
|
|
cut_type: CutType::Fillet,
|
2025-01-28 14:22:19 -08:00
|
|
|
// We make this a none so that we can remove it in the future.
|
|
|
|
face_id: None,
|
2024-09-18 17:04:04 -05:00
|
|
|
}),
|
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-02-21 14:41:25 -06:00
|
|
|
radius,
|
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
|
|
|
}
|
|
|
|
|
2024-08-12 21:39:49 -07:00
|
|
|
pub(crate) fn default_tolerance(units: &UnitLength) -> f64 {
|
|
|
|
match units {
|
|
|
|
UnitLength::Mm => 0.0000001,
|
|
|
|
UnitLength::Cm => 0.0000001,
|
|
|
|
UnitLength::In => 0.0000001,
|
|
|
|
UnitLength::Ft => 0.0001,
|
|
|
|
UnitLength::Yd => 0.001,
|
|
|
|
UnitLength::M => 0.001,
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|