2024-03-25 17:07:53 -07:00
//! Standard library helices.
use anyhow ::Result ;
2025-03-01 13:59:01 -08:00
use kcl_derive_docs ::stdlib ;
2024-09-19 14:06:29 -07:00
use kcmc ::{ each_cmd as mcmd , length_unit ::LengthUnit , shared ::Angle , ModelingCmd } ;
2024-09-18 17:04:04 -05:00
use kittycad_modeling_cmds as kcmc ;
2024-03-25 17:07:53 -07:00
use crate ::{
errors ::KclError ,
2025-01-07 19:10:53 -08:00
execution ::{ ExecState , Helix as HelixValue , KclValue , Solid } ,
std ::{ axis_or_reference ::Axis3dOrEdgeReference , Args } ,
2024-03-25 17:07:53 -07:00
} ;
2025-01-07 19:10:53 -08:00
/// Create a helix.
pub async fn helix ( exec_state : & mut ExecState , args : Args ) -> Result < KclValue , KclError > {
2025-02-05 15:58:32 -08:00
let angle_start = args . get_kw_arg ( " angleStart " ) ? ;
let revolutions = args . get_kw_arg ( " revolutions " ) ? ;
let ccw = args . get_kw_arg_opt ( " ccw " ) ? ;
2025-03-21 15:38:08 -07:00
let radius = args . get_kw_arg_opt ( " radius " ) ? ;
let axis = args . get_kw_arg_opt ( " axis " ) ? ;
2025-02-05 15:58:32 -08:00
let length = args . get_kw_arg_opt ( " length " ) ? ;
2025-03-21 15:38:08 -07:00
let cylinder = args . get_kw_arg_opt ( " cylinder " ) ? ;
2025-02-05 15:58:32 -08:00
2025-03-21 15:38:08 -07:00
// Make sure we have a radius if we don't have a cylinder.
if radius . is_none ( ) & & cylinder . is_none ( ) {
return Err ( KclError ::Semantic ( crate ::errors ::KclErrorDetails {
message : " Radius is required when creating a helix without a cylinder. " . to_string ( ) ,
source_ranges : vec ! [ args . source_range ] ,
} ) ) ;
}
// Make sure we don't have a radius if we have a cylinder.
if radius . is_some ( ) & & cylinder . is_some ( ) {
return Err ( KclError ::Semantic ( crate ::errors ::KclErrorDetails {
message : " Radius is not allowed when creating a helix with a cylinder. " . to_string ( ) ,
source_ranges : vec ! [ args . source_range ] ,
} ) ) ;
}
// Make sure we have an axis if we don't have a cylinder.
if axis . is_none ( ) & & cylinder . is_none ( ) {
return Err ( KclError ::Semantic ( crate ::errors ::KclErrorDetails {
message : " Axis is required when creating a helix without a cylinder. " . to_string ( ) ,
source_ranges : vec ! [ args . source_range ] ,
} ) ) ;
}
// Make sure we don't have an axis if we have a cylinder.
if axis . is_some ( ) & & cylinder . is_some ( ) {
return Err ( KclError ::Semantic ( crate ::errors ::KclErrorDetails {
message : " Axis is not allowed when creating a helix with a cylinder. " . to_string ( ) ,
source_ranges : vec ! [ args . source_range ] ,
} ) ) ;
}
// Make sure we have a radius if we have an axis.
if radius . is_none ( ) & & axis . is_some ( ) {
return Err ( KclError ::Semantic ( crate ::errors ::KclErrorDetails {
message : " Radius is required when creating a helix around an axis. " . to_string ( ) ,
source_ranges : vec ! [ args . source_range ] ,
} ) ) ;
}
// Make sure we have an axis if we have a radius.
if axis . is_none ( ) & & radius . is_some ( ) {
return Err ( KclError ::Semantic ( crate ::errors ::KclErrorDetails {
message : " Axis is required when creating a helix around an axis. " . to_string ( ) ,
source_ranges : vec ! [ args . source_range ] ,
} ) ) ;
}
let value = inner_helix (
revolutions ,
angle_start ,
ccw ,
radius ,
axis ,
length ,
cylinder ,
exec_state ,
args ,
)
. await ? ;
2025-01-22 09:42:09 +13:00
Ok ( KclValue ::Helix { value } )
2025-01-07 19:10:53 -08:00
}
/// Create a helix.
///
/// ```no_run
/// // Create a helix around the Z axis.
2025-02-05 15:58:32 -08:00
/// helixPath = helix(
2025-01-07 19:10:53 -08:00
/// angleStart = 0,
/// ccw = true,
2025-01-16 13:50:13 -08:00
/// revolutions = 5,
2025-01-07 19:10:53 -08:00
/// length = 10,
/// radius = 5,
/// axis = 'Z',
2025-02-05 15:58:32 -08:00
/// )
2025-01-07 19:10:53 -08:00
///
///
/// // Create a spring by sweeping around the helix path.
/// springSketch = startSketchOn('YZ')
2025-02-28 17:40:01 -08:00
/// |> circle( center = [0, 0], radius = 0.5)
2025-02-07 12:35:04 -06:00
/// |> sweep(path = helixPath)
2025-01-07 19:10:53 -08:00
/// ```
///
/// ```no_run
/// // Create a helix around an edge.
2025-01-13 15:34:43 -08:00
/// helper001 = startSketchOn('XZ')
2025-01-07 19:10:53 -08:00
/// |> startProfileAt([0, 0], %)
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(end = [0, 10], tag = $edge001)
2025-01-07 19:10:53 -08:00
///
2025-02-05 15:58:32 -08:00
/// helixPath = helix(
2025-01-07 19:10:53 -08:00
/// angleStart = 0,
/// ccw = true,
2025-01-16 13:50:13 -08:00
/// revolutions = 5,
2025-01-07 19:10:53 -08:00
/// length = 10,
/// radius = 5,
/// axis = edge001,
2025-02-05 15:58:32 -08:00
/// )
2025-01-07 19:10:53 -08:00
///
/// // Create a spring by sweeping around the helix path.
/// springSketch = startSketchOn('XY')
2025-02-28 17:40:01 -08:00
/// |> circle( center = [0, 0], radius = 0.5 )
2025-02-07 12:35:04 -06:00
/// |> sweep(path = helixPath)
2025-01-14 12:05:36 -08:00
/// ```
///
/// ```no_run
/// // Create a helix around a custom axis.
2025-02-05 15:58:32 -08:00
/// helixPath = helix(
2025-01-14 12:05:36 -08:00
/// angleStart = 0,
/// ccw = true,
2025-01-16 13:50:13 -08:00
/// revolutions = 5,
2025-01-14 12:05:36 -08:00
/// length = 10,
/// radius = 5,
/// axis = {
/// custom = {
/// axis = [0, 0, 1.0],
/// origin = [0, 0.25, 0]
/// }
/// }
2025-02-05 15:58:32 -08:00
/// )
2025-01-14 12:05:36 -08:00
///
/// // Create a spring by sweeping around the helix path.
/// springSketch = startSketchOn('XY')
2025-02-28 17:40:01 -08:00
/// |> circle( center = [0, 0], radius = 1 )
2025-02-07 12:35:04 -06:00
/// |> sweep(path = helixPath)
2025-01-07 19:10:53 -08:00
/// ```
2025-03-21 15:38:08 -07:00
///
///
///
/// ```no_run
/// // Create a helix on a cylinder.
///
/// part001 = startSketchOn('XY')
/// |> circle( center= [5, 5], radius= 10 )
/// |> extrude(length = 10)
///
/// helix(
/// angleStart = 0,
/// ccw = true,
/// revolutions = 16,
/// cylinder = part001,
/// )
/// ```
2025-01-07 19:10:53 -08:00
#[ stdlib {
name = " helix " ,
2025-02-05 15:58:32 -08:00
keywords = true ,
unlabeled_first = false ,
args = {
revolutions = { docs = " Number of revolutions. " } ,
angle_start = { docs = " Start angle (in degrees). " } ,
ccw = { docs = " Is the helix rotation counter clockwise? The default is `false`. " , include_in_snippet = false } ,
2025-03-21 15:38:08 -07:00
radius = { docs = " Radius of the helix. " , include_in_snippet = true } ,
axis = { docs = " Axis to use for the helix. " , include_in_snippet = true } ,
2025-02-05 15:58:32 -08:00
length = { docs = " Length of the helix. This is not necessary if the helix is created around an edge. If not given the length of the edge is used. " , include_in_snippet = true } ,
2025-03-21 15:38:08 -07:00
cylinder = { docs = " Cylinder to create the helix on. " , include_in_snippet = false } ,
2025-02-05 15:58:32 -08:00
} ,
2025-01-07 19:10:53 -08:00
feature_tree_operation = true ,
} ]
2025-02-05 15:58:32 -08:00
#[ allow(clippy::too_many_arguments) ]
async fn inner_helix (
revolutions : f64 ,
angle_start : f64 ,
ccw : Option < bool > ,
2025-03-21 15:38:08 -07:00
radius : Option < f64 > ,
axis : Option < Axis3dOrEdgeReference > ,
2025-02-05 15:58:32 -08:00
length : Option < f64 > ,
2025-03-21 15:38:08 -07:00
cylinder : Option < Solid > ,
2025-02-05 15:58:32 -08:00
exec_state : & mut ExecState ,
args : Args ,
) -> Result < Box < HelixValue > , KclError > {
2025-01-07 19:10:53 -08:00
let id = exec_state . next_uuid ( ) ;
let helix_result = Box ::new ( HelixValue {
value : id ,
2025-02-05 23:50:00 -05:00
artifact_id : id . into ( ) ,
2025-02-05 15:58:32 -08:00
revolutions ,
angle_start ,
2025-03-21 15:38:08 -07:00
cylinder_id : cylinder . as_ref ( ) . map ( | c | c . id ) ,
2025-02-05 15:58:32 -08:00
ccw : ccw . unwrap_or ( false ) ,
2025-01-22 09:42:09 +13:00
units : exec_state . length_unit ( ) ,
2025-01-07 19:10:53 -08:00
meta : vec ! [ args . source_range . into ( ) ] ,
} ) ;
2025-02-18 13:50:13 -08:00
if args . ctx . no_engine_commands ( ) . await {
2025-01-07 19:10:53 -08:00
return Ok ( helix_result ) ;
}
2025-03-21 15:38:08 -07:00
if let Some ( cylinder ) = cylinder {
args . batch_modeling_cmd (
id ,
ModelingCmd ::from ( mcmd ::EntityMakeHelix {
cylinder_id : cylinder . id ,
is_clockwise : ! helix_result . ccw ,
length : LengthUnit ( length . unwrap_or ( cylinder . height ) ) ,
revolutions ,
start_angle : Angle ::from_degrees ( angle_start ) ,
} ) ,
)
. await ? ;
} else if let ( Some ( axis ) , Some ( radius ) ) = ( axis , radius ) {
match axis {
Axis3dOrEdgeReference ::Axis ( axis ) = > {
let ( axis , origin ) = axis . axis_and_origin ( ) ? ;
2024-03-25 17:07:53 -07:00
2025-03-21 15:38:08 -07:00
// Make sure they gave us a length.
let Some ( length ) = length else {
return Err ( KclError ::Semantic ( crate ::errors ::KclErrorDetails {
message : " Length is required when creating a helix around an axis. " . to_string ( ) ,
source_ranges : vec ! [ args . source_range ] ,
} ) ) ;
} ;
2024-03-25 17:07:53 -07:00
2025-03-21 15:38:08 -07:00
args . batch_modeling_cmd (
id ,
ModelingCmd ::from ( mcmd ::EntityMakeHelixFromParams {
radius : LengthUnit ( radius ) ,
is_clockwise : ! helix_result . ccw ,
length : LengthUnit ( length ) ,
revolutions ,
start_angle : Angle ::from_degrees ( angle_start ) ,
axis ,
center : origin ,
} ) ,
)
. await ? ;
}
Axis3dOrEdgeReference ::Edge ( edge ) = > {
let edge_id = edge . get_engine_id ( exec_state , & args ) ? ;
2024-03-25 17:07:53 -07:00
2025-03-21 15:38:08 -07:00
args . batch_modeling_cmd (
id ,
ModelingCmd ::from ( mcmd ::EntityMakeHelixFromEdge {
radius : LengthUnit ( radius ) ,
is_clockwise : ! helix_result . ccw ,
length : length . map ( LengthUnit ) ,
revolutions ,
start_angle : Angle ::from_degrees ( angle_start ) ,
edge_id ,
} ) ,
)
. await ? ;
}
} ;
}
2024-03-25 17:07:53 -07:00
2025-03-21 15:38:08 -07:00
Ok ( helix_result )
2024-03-25 17:07:53 -07:00
}